Revit二次開發——引用dynamo中的幾何庫
阿新 • • 發佈:2021-02-01
技術標籤:Revit二次開發
前沿
dynamo的幾何庫其實是非常強大的,如果自己靠著RevitAPI去寫還是非常費勁的。所以想引用dynamo的幾何庫來做一些工作。主要參考的就是這篇文章。Revit二次開發——不開啟Dynamo使用Dynamo的類
環境
Revit2020
dynamo2.1.0
用到的動態連結庫:
1、DynamoRevitDS.dll
2、DynamoServices.dll
3、LibG.Interface.dll
4、ProtoGeometry.dll
5、RevitNodes.dll
初始化Dynamo環境(這個必須在呼叫Dynamo類之前執行)
開啟Dynamo但是禁止彈出Dynamo對話方塊(參考連結:https://blog.csdn.net/weixin_44153630/article/details/108013243)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI. Selection;
using Dynamo.Applications;
namespace Dynamo2Revit
{
[Transaction(TransactionMode.Manual)]
public class Command : IExternalCommand
{
Result IExternalCommand.Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
string Journal_Dynamo_Path = @"C:\Users\zyx\Desktop\2RevitArcBridge\Dynamo2Revit\Dynamo2Revit\22.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;
}
}
}
Dynamo類的測試程式碼
這裡主要引用Dynamo的幾何庫以及幾何轉換節點
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Revit.GeometryConversion;
using DG = Autodesk.DesignScript.Geometry;
namespace DynamoTest
{
[Transaction(TransactionMode.Manual)]
public class Command : IExternalCommand
{
Result IExternalCommand.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;
}
}
}
注意要點:
1、一旦關閉Revit就需要重新再指定一個空的dynamo檔案,不然就會提示你程式集"LibG.Protolnterface,Version=1.0.0.0,Culture-neutral,PublicKeyToken=null"中的型別"Autodesk.LibG.CurveHost"的方法Approximate"沒有實現。
所以每次都要修改並重新編譯初始化那段程式碼中的檔案地址,比較麻煩,之後有時間可以通過WPF做個介面來指定這個空的dynamo檔案,獲取其路徑。
2、一定要引用到那些庫,那些庫就在Revit的安裝包裡(如果不出意外的話),用everything搜尋一下就知道在哪裡了。
3、dynamo中用到的單位是公制的(例如米),revit二次開發中的單位是英制的,所以用數字在兩者之間傳遞資訊的時候,尤其要注意單位!單位!單位!
測試程式碼:
//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
XYZ p1 = new XYZ(0, 1000, 0);
XYZ p2 = new XYZ(0, 0, 0);
Line revitLine = Line.CreateBound(p1, p2);
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();
}