2015-12-17 17 views
1

我們試圖獲取所有鏈接的外部文件的列表和層次結構。現在我們嘗試了以下代碼:如何獲取鏈接和子鏈接的Revit文件的列表和層次結構

FilteredElementCollector collectorI = new FilteredElementCollector(DocChild); 
IList<Element> elemsI = collectorI.OfCategory(BuiltInCategory.OST_RvtLinks).OfClass(typeof(RevitLinkInstance)).ToElements(); 

foreach (Element eI in elemsI) 
{ 
    if (eI is RevitLinkInstance) 
    { 
     RevitLinkInstance InstanceType = eI as RevitLinkInstance; 
     RevitLinkType type = DocChild.GetElement(InstanceType.GetTypeId()) as RevitLinkType; 
     TaskDialog.Show("Debug", "IsNestedLink=" + type.IsNestedLink.ToString() + " IsLinked=" + DocChild.IsLinked.ToString()); 

     if (!type.IsNestedLink) 
     { 
      TaskDialog.Show("Debug", "Children=" + InstanceType.GetLinkDocument().PathName.ToString()); 
     } 
    } 
} 

我們成功獲取所有鏈接文件的列表,但沒有層次結構。我們不知道哪個文件是哪個父母的孩子。

這是我們正在試圖獲得的鏈接結構:

enter image description here

回答

0

您需要GetParentId和GetChilds方法發揮到閱讀的層次結構。下面是一個代碼:

public Result Execute(
ExternalCommandData commandData, 
ref string message, 
ElementSet elements) 
{ 
    // get active document 
    Document mainDoc = commandData.Application.ActiveUIDocument.Document; 

    // prepare to show the results... 
    TreeNode mainNode = new TreeNode(); 
    mainNode.Text = mainDoc.PathName; 

    // start by the root links (no parent node) 
    FilteredElementCollector coll = new FilteredElementCollector(mainDoc); 
    coll.OfClass(typeof(RevitLinkInstance)); 
    foreach (RevitLinkInstance inst in coll) 
    { 
    RevitLinkType type = mainDoc.GetElement(inst.GetTypeId()) as RevitLinkType; 
    if (type.GetParentId() == ElementId.InvalidElementId) 
    { 
     TreeNode parentNode = new TreeNode(inst.Name); 
     mainNode.Nodes.Add(parentNode); 

     GetChilds(mainDoc, type.GetChildIds(), parentNode); 
    } 
    } 

    // show the results in a form 
    System.Windows.Forms.Form resultForm = new System.Windows.Forms.Form(); 
    TreeView treeView = new TreeView(); 
    treeView.Size = resultForm.Size; 
    treeView.Anchor |= AnchorStyles.Bottom | AnchorStyles.Top; 
    treeView.Nodes.Add(mainNode); 
    resultForm.Controls.Add(treeView); 
    resultForm.ShowDialog(); 

    return Result.Succeeded; 
} 

private void GetChilds(Document mainDoc, ICollection<ElementId> ids, 
         TreeNode parentNode) 
{ 
    foreach (ElementId id in ids) 
    { 
    // get the child information 
    RevitLinkType type = mainDoc.GetElement(id) as RevitLinkType; 

    TreeNode subNode = new TreeNode(type.Name); 
    parentNode.Nodes.Add(subNode); 

    // then go to the next level 
    GetChilds(mainDoc, type.GetChildIds(), subNode); 
    } 
} 

而且結果應該是這樣的:

enter image description here

source博客文章。

+0

謝謝你的偉大答案。它幫助解決了我的問題的很大一部分,但我只剩下一個問題:如何獲取所有實例的完整路徑名。在某些情況下,相同的文件在Revit鏈接層次結構中重複使用兩次。對於您需要鏈接文件中Document對象的PathName的 – Philippe

+0

。它應該工作 –

+0

對不起,但在RevitLinkType中不能訪問路徑名只有名字。並且在子程序集中,AttachmentType始終是重疊的。 – Philippe

-2

謝謝你的出色答案。它幫助解決了我的問題的很大一部分,但我只剩下一個問題:如何獲取所有實例的完整路徑名。在某些情況下,相同的文件在Revit鏈接層次結構中重複使用兩次。 Regards

+0

這是一個新問題,而不是答案。 –