Revit二次开发——不开启Dynamo使用Dynamo的类

软件环境: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;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值