1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

RevitアドインでDynamoを実行する

Last updated at Posted at 2024-01-19

はじめに

この記事では、Revit APIを使用してDynamoスクリプトを実行する方法を紹介します。

本記事は一部ChatGPTを使用して執筆しています。
本記事では Visual Studio 2022 および Revit 2024 を使用しています。

実装

参照の追加

NuGetパッケージ "DynamoVisualProgramming.Core" と "DynamoVisualProgramming.Revit" をプロジェクトに追加します。
image.png

名前空間の追加

まず必要な名前空間をインポートします。

using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System;
using System.Windows;
using System.Collections.Generic;
using Dynamo.Applications;

コマンド作成

次に、TestCommand クラスを定義し、IExternalCommand インターフェースを実装します。これにより、Revitの外部コマンドとして機能します。

[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
public class TestCommand : IExternalCommand
{
    public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
    {
        try
        {
            // ここにDynamoを実行する処理を書く
        }

        catch (Exception ex)
        {
            TaskDialog.Show("ERROR", ex.Message);
            return Result.Failed;
        }
    }
}

ファイルパス取得

Dynamoスクリプトのファイルパスをユーザーに選択させます。ファイルオープンダイアログを使用してファイルパスを取得します。ダイアログの詳細は以下の記事を確認してください。

var dialog = new FileOpenDialog("ファイルを選択してください (*.dyn)|*.dyn");
var result = dialog.Show();
var path = ModelPathUtils.ConvertModelPathToUserVisiblePath(dialog.GetSelectedModelPath());

ジャーナルデータの設定

Dynamoを非表示で自動実行するためのジャーナルデータを設定します。

var journalData = new Dictionary<string, string>
{
    { JournalKeys.ShowUiKey, false.ToString()},
    { JournalKeys.AutomationModeKey, true.ToString()},
    { JournalKeys.DynPathKey, ""},
    { JournalKeys.DynPathExecuteKey, true.ToString()},
    { JournalKeys.ForceManualRunKey, false.ToString()},
    { JournalKeys.ModelShutDownKey, true.ToString()},
    { JournalKeys.ModelNodesInfo, false.ToString()}
};

実行

最後に、DynamoRevit クラスのインスタンスを作成し、コマンドデータを渡してDynamoスクリプトを実行します。

var dynamoCommandData = new DynamoRevitCommandData()
{
    Application = commandData.Application,
    JournalData = journalData
};

var dynamo = new DynamoRevit();
dynamo.ExecuteCommand(dynamoCommandData);
DynamoRevit.RevitDynamoModel.OpenFileFromPath(path, true);
DynamoRevit.RevitDynamoModel.ForceRun();

コード全体

using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System;
using System.Windows;
using System.Collections.Generic;
using Dynamo.Applications;

[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
public class TestCommand : IExternalCommand
{
    public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
    {
        try
        {
            // ファイルパス取得
            var dialog = new FileOpenDialog("ファイルを選択してください (*.dyn)|*.dyn");
            var result = dialog.Show();
            var path = ModelPathUtils.ConvertModelPathToUserVisiblePath(dialog.GetSelectedModelPath());
            
            // ジャーナルデータの設定
            var journalData = new Dictionary<string, string>
            {
                { JournalKeys.ShowUiKey, false.ToString()},
                { JournalKeys.AutomationModeKey, true.ToString()},
                { JournalKeys.DynPathKey, ""},
                { JournalKeys.DynPathExecuteKey, true.ToString()},
                { JournalKeys.ForceManualRunKey, false.ToString()},
                { JournalKeys.ModelShutDownKey, true.ToString()},
                { JournalKeys.ModelNodesInfo, false.ToString()}
            };
            
            // 実行
            var dynamoCommandData = new DynamoRevitCommandData()
            {
                Application = commandData.Application,
                JournalData = journalData
            };
            
            var dynamo = new DynamoRevit();
            dynamo.ExecuteCommand(dynamoCommandData);
            DynamoRevit.RevitDynamoModel.OpenFileFromPath(path, true);
            DynamoRevit.RevitDynamoModel.ForceRun();
        }

        catch (Exception ex)
        {
            TaskDialog.Show("ERROR", ex.Message);
            return Result.Failed;
        }
    }
}

テスト

”Dynamoが実行されました” というダイアログを表示するDynamoを用意して、これをアドインから実行してみます。
animation.gif

補足

プロジェクトにNuGetパッケージを追加した際、csprojファイルの PackageReferenceExcludeAssets タグを記述することで、ビルド時にdllファイルがローカルへコピーされないよう設定できます。
詳細は以下の公式ドキュメントを確認してください。

csproj
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  
  <!-- その他の設定 -->
  
  <ItemGroup>
    <PackageReference Include="DynamoVisualProgramming.Core">
      <Version>2.17.0.3493</Version>
+     <ExcludeAssets>runtime</ExcludeAssets>
    </PackageReference>
    <PackageReference Include="DynamoVisualProgramming.Revit">
      <Version>2.17.0.7404</Version>
+     <ExcludeAssets>runtime</ExcludeAssets>
    </PackageReference>
  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

参考リンク

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?