升級軟體包功能的工作過程演示
因為主要的工作都是在調整Python Script代碼,在這個過程中也時不時地需要去查詢API手冊,視頻里也清晰的演示了查詢API手冊獲取信息並使用這些信息的一些基本思路。
這個視頻在Dynamo的ActiveX技術應用:LinkDWG的課程中會是免費視頻,可以直接觀看,也希望通過這種方式能讓大家從中學到編製和調試Dynamo程序的一些方法及技巧。
本次節點升級主要實現了以下幾點:
- 測試能否從ImportInstance裡面讀取文本信息(結果不可以)
- 讀取ImportInstance中的Mesh並將之轉換為PolySurface,單純的Mesh列表,可以由Element.Geometry直接獲取。
- Mesh圖層顏色信息在ImportInstance中無法正常讀取出來,暫時使用0層和黑色統一代替。
通過升級Python Script代碼,整體程序的流程更加清晰,代碼也更加精簡,但是功能卻更加強大。以下是代碼的優化前後對比。
優化前代碼:
#LinkDWG Core DYF by Koz Jono YEOHn#kozmosovia@hotmail.comn#Copyright(C)1994-2017 KozMos Inc.n#Copyright(C) 2011-2017 Neila Heaven Networksnnimport clrnclr.AddReference(ProtoGeometry)nfrom Autodesk.DesignScript.Geometry import *nnclr.AddReference("RevitAPIUI")nfrom Autodesk.Revit.UI import TaskDialognnclr.AddReference("RevitAPI")nimport Autodesknfrom Autodesk.Revit.DB.Events import *nfrom Autodesk.Revit.DB import *nfrom math import *nnclr.AddReference("RevitNodes")nimport Revitnclr.ImportExtensions(Revit.Elements)nclr.ImportExtensions(Revit.GeometryConversion)nnclr.AddReference("RevitServices")nimport RevitServicesnfrom RevitServices.Persistence import DocumentManagernfrom RevitServices.Transactions import TransactionManagernndef _ConvertRevitCurves(xcrv):ntif str(xcrv.GetType()) != "Autodesk.Revit.DB.PolyLine":nttrtn=xcrv.ToProtoType()tntelse:nttpt = []nttfor abc in xcrv.GetCoordinates():ntttpt.append(abc.ToPoint())nttrtn = PolyCurve.ByPoints(pt)ntreturn rtnnnDOC = DocumentManager.Instance.CurrentDBDocumentnDWG = UnwrapElement(IN[0])nLID = DWG.Parameter[BuiltInParameter.IMPORT_BASE_LEVEL].AsElementId()nGEOM = DWG.Geometry[ Options() ].GetEnumerator()nCRV = []nCRX = []nLAY = []nCLR = []nnTransactionManager.Instance.EnsureInTransaction(DOC)nPLN = SketchPlane.Create(DOC, LID)nwhile GEOM.MoveNext(): ntobj = GEOM.Current.GetInstanceGeometry().GetEnumerator()ntwhile obj.MoveNext():nttcrv = obj.Currentntttry:ntttlay = DOC.GetElement(crv.GraphicsStyleId).GraphicsStyleCategory.Namentttccc = DOC.GetElement(crv.GraphicsStyleId).GraphicsStyleCategory.LineColorntttclr = [ccc.Red, ccc.Green, ccc.Blue]ntttCRV.append(crv)ntttCRX.append(_ConvertRevitCurves(crv))ntttLAY.append(lay)ntttCLR.append(clr)nttexcept:nttttry: nttttvol = crv.Volumenttttif vol == 0.0: ntttttedge = crv.Edges.GetEnumerator()ntttttwhile edge.MoveNext():nttttttcrv = edge.Currentntttttttry:ntttttttlay = DOC.GetElement(crv.GraphicsStyleId).GraphicsStyleCategory.Namentttttttccc = DOC.GetElement(crv.GraphicsStyleId).GraphicsStyleCategory.LineColorntttttttclr = [ccc.Red, ccc.Green, ccc.Green]ntttttttCRV.append(crv)ntttttttCRX.append(_ConvertRevitCurves(crv))ntttttttLAY.append(lay)ntttttttCLR.append(clr)nttttttexcept: passntttexcept: passnTransactionManager.Instance.TransactionTaskDone()nOUT = [CRV, CRX, LAY, CLR]n
優化後代碼:
#LinkDWG Core DYF by Koz Jono YEOHn#kozmosovia@hotmail.comn#Copyright(C)1994-2017 KozMos Inc.n#Copyright(C) 2011-2017 Neila Heaven Networksnnimport clrnclr.AddReference(ProtoGeometry)nfrom Autodesk.DesignScript.Geometry import *nnclr.AddReference("RevitAPIUI")nfrom Autodesk.Revit.UI import TaskDialognnclr.AddReference("RevitAPI")nimport Autodesknfrom Autodesk.Revit.DB.Events import *nfrom Autodesk.Revit.DB import *nfrom math import *nnclr.AddReference("RevitNodes")nimport Revitnclr.ImportExtensions(Revit.Elements)nclr.ImportExtensions(Revit.GeometryConversion)nnclr.AddReference("RevitServices")nimport RevitServicesnfrom RevitServices.Persistence import DocumentManagernfrom RevitServices.Transactions import TransactionManagernndef _ConvertRevitCurves(xcrv):ntif str(xcrv.GetType()) != "Autodesk.Revit.DB.PolyLine":nttrtn=xcrv.ToProtoType()tntelse:nttpt = []nttfor abc in xcrv.GetCoordinates():ntttpt.append(abc.ToPoint())nttrtn = PolyCurve.ByPoints(pt)ntreturn rtnntndef _Mesh2PolySurface(topo):ntvp = topo.VertexPositionsntfi = topo.FaceIndicesntxr1 = xrange(len(fi) )ntind = [(fi[i].A, fi[i].B, fi[i].C) for i in xr1]ntptslist = [ map(vp.__getitem__, ind[i]) for i in xr1]ntntsurf=[]ntfor i in ptslist:ttnttsurf.append(Autodesk.DesignScript.Geometry.Surface.ByPerimeterPoints(i))ntreturn Autodesk.DesignScript.Geometry.PolySurface.ByJoinedSurfaces(surf)nttnDOC = DocumentManager.Instance.CurrentDBDocumentnDWG = UnwrapElement(IN[0])nCRV = []nCRX = []nLAY = []nCLR = []nnfor abc in DWG.Geometry[ Options() ]:ntfor crv in abc.GetInstanceGeometry():ntttry:ntttlay = DOC.GetElement(crv.GraphicsStyleId).GraphicsStyleCategory.Namentttccc = DOC.GetElement(crv.GraphicsStyleId).GraphicsStyleCategory.LineColorntttclr = [ccc.Red, ccc.Green, ccc.Blue]ntttCRX.append(_ConvertRevitCurves(crv))ntttCRV.append(crv)ntttLAY.append(lay)ntttCLR.append(clr)nttexcept:tttnttttry:nttttlay="0"nttttclr=[0,0,0]nttttccc=crv.ToProtoType()nttttccc=_Mesh2PolySurface(ccc)nttttif ccc != None or ccc != []:ntttttCRX.append(ccc)ntttttCRV.append(crv)ntttttLAY.append(lay)ntttttCLR.append(clr)ntttexcept: passntnOUT = [CRV, CRX, LAY, CLR]n
推薦閱讀:
※大數據那些事(14):亂入的賣書的
※Dynamo應用秘籍:19:使用等高線創建簡易地形Solid
※Dynamo應用秘籍:15:斷裂的PolySurface.ByJoinedSurfaces
TAG:AutoDeskRevit | AutoCAD | Dynamo |