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

你可能感兴趣的:(Dynamo,For,Revit)