Dynamo For Revit:Python 基础 - library 引用和 UnwrapElement

前言

本文介绍 Dynamo For Revit 中的 Python Node 的基础 - library 引用和 UnwrapElement。(主要是我自己经常忘了,还要重新找)

内容

如何引用普通的 Python 库,以及 Revit API。

普通的 Python 库

新建一个 Python 节点,内容如下。

# Load the Python Standard and DesignScript Libraries
import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

# The inputs to this node will be stored as a list in the IN variables.
dataEnteringNode = IN

# Place your code below this line

# Assign your output to the OUT variable.
OUT = 0

sys 模块提供了许多函数和变量来处理 Python 运行时环境的不同部分,参考:

  1. CSDN博客:Python之sys模块详解
  2. 更加全面的官方文档,注意自己选择 Python版本

clr 模块是公共语言运行时(Common Language Runtime,简称CRL),就是微软为.net产品构建的运行环境。Dynamo 的 Python 是基于 IronPython 的,因此这个 clr 是它提供的。
参考:
https://ironpython.net/documentation/dotnet/

IronPython aims to be a fully compatible implementation of the Python language. At the same time, the value of a separate implementation than CPython is to make available the .NET ecosystem of libraries. IronPython does this by exposing .NET concepts as Python entities. Existing Python syntax and new Python libraries (like clr) are used to make .NET features available to IronPython code.

因此,clr.AddReference('ProtoGeometry'),引用的是 [Build Output] \bin\AnyCPU\Debug\ProtoGeometry.dll

Revit API

有两种方式,一种是 DynamoRevit 里面提供的基于原始 Revit API 做过包装的,另一种是直接引用 Revit API 相关的 DLL 库。另外,这两种之间可以通过 UnwrapElement 来转化。
DynamoRevit 里面提供的基于原始 Revit API 做过包装的:

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

# Import RevitNodes
clr.AddReference("RevitNodes")
import Revit

# Import Revit elements
from Revit.Elements import *

# Import DocumentManager
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager

import System

RevitNodesRevitServices 可以在 DynamoRevit 的编译结果中找到,DynamoRevit\bin\AnyCPU\Debug\Revit

直接引用 Revit API 相关的 DLL 库

直接引用 RevitAPI.dll ,接下来的操作就和使用 API 二次开发类似了。

clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *

UnwrapElement

参考:Dynamo For Revit: 创建墙门窗
通过 UnwrapElement,可以把 DynamoRevit 包装出来的 Element 转化成 Revit API 中的 Element,然后可以被 Revit API 调用。

familyType = UnwrapElement(IN[0])
host = UnwrapElement(IN[1])
# 省略部分代码。。。
instance = document.Create.NewFamilyInstance(location, familyType, host, level, Structure.StructuralType.NonStructural)

其它

Dynamo For Revit: Python 调用 Revit API 之某些不能直接拿到的 property

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

极客BIM工作室

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值