软件环境:Revit2018 Dynamo1.2(其他待测试)
1、引用的dll
2、初始化Dynamo环境(这个必须在调用Dynamo类之前执行)
开启Dynamo但是禁止弹出Dynamo对话框(参考链接:https://blog.csdn.net/weixin_44153630/article/details/108013243)
示例代码:
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
public class DynamoScriptCmd : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
string Journal_Dynamo_Path = @"E:\常用测试项目\测试数据\Test.dyn"; //一个空的dynamo文件
DynamoRevit dynamoRevit = new DynamoRevit();
DynamoRevitCommandData dynamoRevitCommandData = new DynamoRevitCommandData();
dynamoRevitCommandData.Application = commandData.Application;
IDictionary<string, string> journalData = new Dictionary<string, string>
{
{ Dynamo.Applications.JournalKeys.ShowUiKey, false.ToString() }, // don't show DynamoUI at runtime
{ Dynamo.Applications.JournalKeys.AutomationModeKey, true.ToString() }, //run journal automatically
{ Dynamo.Applications.JournalKeys.DynPathKey, Journal_Dynamo_Path }, //run node at this file path
{ Dynamo.Applications.JournalKeys.DynPathExecuteKey, true.ToString() }, // The journal file can specify if the Dynamo workspace opened from DynPathKey will be executed or not. If we are in automation mode the workspace will be executed regardless of this key.
{ Dynamo.Applications.JournalKeys.ForceManualRunKey, false.ToString() }, // don't run in manual mode
{ Dynamo.Applications.JournalKeys.ModelShutDownKey, true.ToString() }
};
dynamoRevitCommandData.JournalData = journalData;
Result externalCommandResult = dynamoRevit.ExecuteCommand(dynamoRevitCommandData);
return externalCommandResult;
}
}
3、Dynamo类的测试代码
这里主要引用Dynamo的几何库以及几何转换节点
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Revit.GeometryConversion;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using DG = Autodesk.DesignScript.Geometry;
namespace RevitTest.DynamoTest
{
/// <summary>
/// 引用Dynamo类
/// </summary>
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
public class DynamoCmd : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
Document document = commandData.Application.ActiveUIDocument.Document;
try
{
DG.Point startPoint = DG.Point.ByCoordinates(0,0,0);
DG.Point endPoint = DG.Point.ByCoordinates(1000, 0, 0);
DG.Line line = DG.Line.ByStartPointEndPoint(startPoint,endPoint);
Line revitLine= line.ToRevitType() as Line; //将Dynamo的Geometry转化为Revit的Geometry
using (Transaction transaction=new Transaction(document,"Create Line"))
{
transaction.Start();
ModelCurve modelCurve = document.Create.NewModelCurve(revitLine,
SketchPlane.Create(document, Plane.CreateByNormalAndOrigin(XYZ.BasisZ, XYZ.Zero)));
transaction.Commit();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
return Result.Succeeded;
}
}
}