I’m allowed to merge two python scripts, both of which run independently in dynamo, but I won’t merge them.
1:
import clr
clr.AddReference(‘ProtoGeometry’)
from Autodesk.DesignScript.Geometry import *
clr.AddReference(‘RevitAPI’)
import Autodesk
from Autodesk.Revit.DB import *
clr.AddReference(‘RevitServices’)
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
import System
def tolist(x):
if hasattr(x,‘iter’): return x
else : return
获取当前文档
doc = DocumentManager.Instance.CurrentDBDocument
定义要查找的类别 ID
category_id = -2000196
通过类别 ID 尝试找到对应的类别
target_category = doc.Settings.Categories.get_Item(category_id)
如果没有找到指定的类别,抛出异常
if target_category is None:
raise ValueError(f"Category with ID {category_id} not found.")
收集指定类别下的非元素类型的元素
collector = FilteredElementCollector(doc).OfCategoryId(target_category.Id).WhereElementIsNotElementType().ToElements()
将 collector 包装成列表
collector = [collector] if not isinstance(collector, list) else collector
OUT = collector
2:
代码2:
import clr
clr.AddReference(‘ProtoGeometry’)
from Autodesk.DesignScript.Geometry import *
clr.AddReference(‘RevitAPI’)
import Autodesk
from Autodesk.Revit.DB import *
clr.AddReference(‘RevitServices’)
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
import System
elements = IN[0]
mepSystemTypes = IN[1] if len(IN) > 1 else None
desired_diameter = IN[2] if len(IN) > 2 else None
def get_element_faces(element):
if isinstance(element, list):
faces =
for e in element:
try:
element_faces = e.Faces
faces.extend(element_faces)
except AttributeError:
pass
return faces
else:
try:
return element.Faces
except AttributeError:
return None
faces = get_element_faces(elements)
circular_faces =
for face in faces:
try:
curves = face.PerimeterCurves()
surface_curves =
for curve in curves:
surface_curves.append(curve)
if any(isinstance(curve, Circle) for curve in surface_curves):
circular_faces.append(face)
except AttributeError:
pass
references =
for face in circular_faces:
try:
reference = face.Tags.LookupTag(“RevitFaceReference”)
references.append(reference)
except:
references.append(None)
确保处理的是族文档
doc = DocumentManager.Instance.CurrentDBDocument
if not doc.IsFamilyDocument:
raise ValueError(“The document is not a family document.”)
connector =
TransactionManager.Instance.ForceCloseTransaction()
TransactionManager.Instance.EnsureInTransaction(doc)
检查并处理 mepSystemTypes 的类型
if not isinstance(mepSystemTypes, list):
mepSystemTypes = [mepSystemTypes]
for ref in references:
if ref is not None:
for mepSystemType in mepSystemTypes:
if isinstance(mepSystemType, Autodesk.Revit.DB.Plumbing.PipeSystemType):
mepSys = mepSystemType
else:
# 如果 mepSystemType 是整数,先转换为字符串再进行解析
if isinstance(mepSystemType, int):
mepSystemType = str(mepSystemType)
mepSys = System.Enum.Parse(Autodesk.Revit.DB.Plumbing.PipeSystemType, mepSystemType)
created_connector = ConnectorElement.CreatePipeConnector(doc, mepSys, ref)
if desired_diameter is not None:
radius_param = created_connector.get_Parameter(BuiltInParameter.CONNECTOR_RADIUS)
radius_param.Set(desired_diameter / 2)
connector.append(created_connector)
TransactionManager.Instance.TransactionTaskDone()
OUT = connector