WELCOME

a:hover { cursor:url(https://files.cnblogs.com/files/laoguantongxiegogo/click_24px_1231393_easyicon.net.ico),auto; } body { cursor:url(https://files.cnblogs.com/files/laoguantongxiegogo/pointer_24px_1231389_easyicon.net.ico),auto; }

Dynamo几何库调用

dll文件引用

  1. DynamoRevitDS.dll
  2. DynamoServices.dll
  3. LibG.Interface.dll
  4. ProtoGeometry.dll
  5. RevitNodes.dll

初始化Dynamo环境

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类的测试代码

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;
        }
    }
}

转换方式

Revit元素,转dynamo元素,使用ToProtoType()方法
dynamo元素,转Revit元素,使用ToRevitType()方法

dynamo案例

计算斜板距离

using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DG = Autodesk.DesignScript.Geometry;
using Revit.GeometryConversion;

namespace FloorSpaceAnalysis
{
    /// <summary>
    /// 求两个斜板的最小距离,射线法实现短线显式
    /// </summary>
    [Transaction(TransactionMode.Manual)]
    public class Class1 : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIDocument uidoc = commandData.Application.ActiveUIDocument;
            Document doc = uidoc.Document;
            //选取一个对象
            Reference firstRef = uidoc.Selection.PickObject(ObjectType.Element, "拾取一个物体");
            Element firstEle = doc.GetElement(firstRef);
            //获取对象的solid
            Solid firstSolid = GetSolidsOfElement(firstEle).First();
            //拾取第二个对象
            Reference secondRef = uidoc.Selection.PickObject(ObjectType.Element, "拾取一个物体");
            Element secondelem = doc.GetElement(secondRef);
            //获取第二个对象的solid
            Solid secondSolid = GetSolidsOfElement(secondelem).First();
            //将revit几何信息转化为dynamo几何信息
            DG.Solid dgsolid1 = firstSolid.ToProtoType() as DG.Solid;
            DG.Solid dgsolid2 =secondSolid.ToProtoType() as DG.Solid;
            //采用dynamo 几何库distance方法求两个对象的最小距离
            double distance = dgsolid1.DistanceTo(dgsolid2);


            //采用dynamo几何库ClosestPointTo求距离最近的点
            DG.Point dgPoint= dgsolid1.ClosestPointTo(dgsolid2);
            //将计算获得的点坐标转化为revit坐标
            XYZ xYZ = dgPoint.ToRevitType();
            TaskDialog.Show("信息提示", "最小净距:" + distance+"\n"+"最近点是"+xYZ);
            return Result.Succeeded;
        }
        #region
        #region GetSolidsOfElement:从element里面获取实体的方法
        public List<Solid> GetSolidsOfElement(Element ele)
        {
            //生成事件,指定返回数据的特征
            Options options = new Options();
            options.DetailLevel = ViewDetailLevel.Fine;
            options.ComputeReferences = true;
            options.IncludeNonVisibleObjects = true;
            //取得构件元素
            GeometryElement geoElement = ele.get_Geometry(options);
            List<GeometryObject> geoObj = new List<GeometryObject>();
            //递归获取集合元素的所有geometryobject
            GetAllObj(geoElement, ref geoObj);
            //转为solid的集合
            List<Solid> solids = geoObj.ConvertAll(m => m as Solid);
            return solids;
        }
        #endregion
        #region GetAllObj获得geometry的方法
        //获得geometryobject的递归算法
        public void GetAllObj(GeometryElement gele, ref List<GeometryObject> gobjs)
        {
            if (gele == null)
            {
                return;
            }
            //遍历geometryelement里面的geometryobject
            IEnumerator<GeometryObject> enumerator = gele.GetEnumerator();
            while (enumerator.MoveNext())
            {
                GeometryObject geoObject = enumerator.Current;
                Type type = geoObject.GetType();
                //如果是嵌套的GeometryElement 
                if (type.Equals(typeof(GeometryElement)))
                {
                    //则递归
                    GetAllObj(geoObject as GeometryElement, ref gobjs);
                }
                //如果嵌套的geometryinstance
                else if (type.Equals(typeof(GeometryInstance)))
                {
                    //则用getinstancegeometry取得其中的geometryelement再递归
                    GetAllObj((geoObject as GeometryInstance).GetInstanceGeometry(), ref gobjs);
                }
                //如果是solid,则存入集合,递归结束
                else
                {
                    if (type.Equals(typeof(Solid)))
                    {
                        Solid solid = geoObject as Solid;
                        //去掉可能存在的空Solid
                        if (solid.Faces.Size > 0 || solid.Edges.Size > 0)
                        {
                            gobjs.Add(geoObject);
                        }
                    }
                }
            }
        }
        #endregion
        #endregion
    }
}

posted @ 2021-06-22 12:53  waiting233  阅读(369)  评论(0)    收藏  举报
#Snow{ position: fixed; top: 0; left: 0; width: 100%; height: 100%; z-index: 99999; background: rgba(125,137,95,0.1); pointer-events: none; }