Revit API 中再匯入RFA檔案的時候,可以透過LoadFamily 將RFA檔案載入。
但是僅是使用此方法載入RFA檔案的話,在第二次使用呼叫此方法時,會因為該RFA模型已經載入過而導致出現NullReference的錯誤。
1 |
document.LoadFamily(filepath, out f); |
因此當載入失敗時(代表曾經已經載入過了),則從Document中尋找目標的構建,完整程式碼如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
private FamilySymbol FindFamilySymbol(Document doc, string familyName, string symbolName) { FilteredElementCollector collector = new FilteredElementCollector(doc); FilteredElementIterator itr = collector.OfClass(typeof(Family)).GetElementIterator(); itr.Reset(); while (itr.MoveNext()) { Autodesk.Revit.DB.Element elem = (Autodesk.Revit.DB.Element)itr.Current; if (elem.GetType() == typeof(Autodesk.Revit.DB.Family)) { if (elem.Name == familyName) { Autodesk.Revit.DB.Family family = (Autodesk.Revit.DB.Family)elem; foreach (Autodesk.Revit.DB.ElementId symbolId in family.GetFamilySymbolIds()) { Autodesk.Revit.DB.FamilySymbol symbol = (Autodesk.Revit.DB.FamilySymbol)doc.GetElement(symbolId); if (symbol.Name == symbolName) { return symbol; } } } } } return null; } private Family loadRFA(Document document, string filepath) { Transaction transaction = new Transaction(document, "Create Schedules"); transaction.Start(); Family f = null; bool isload = document.LoadFamily(filepath, out f); string filename = Path.GetFileNameWithoutExtension(filepath); FamilySymbol symbol = null; if (f == null) { symbol = FindFamilySymbol(document, filename, "Industrial Stairway"); if (null != symbol) { symbol.Activate(); f = symbol.Family; } } transaction.Commit(); return f; } |
其中在第39行的,FindFamilySymbol中的參數,Family Name 為RFA檔案第一次匯入時的檔案名稱,而FamilySymbol Name 則是可以透過Type Properties 中察看,以下圖為例目標元件的FamilySymbol Name 即是Industrial Stairway。
當上述兩個函數放置好並設定好之後,透過以下程式碼即可成功匯入RFA檔案:
1 2 |
Document document = uidoc.Document; document.LoadFamily(filepath, out f); |
文章標籤
全站熱搜
留言列表