如何获得List&链接和子链接Revit文件的层次结构

时间:2015-12-17 14:18:15

标签: hyperlink 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

2 个答案:

答案 0 :(得分: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博文。

答案 1 :(得分:-2)

谢谢你的回答。它帮助了解我的大部分问题,但我还有一个问题是:如何获取所有实例的完整路径名。在某些情况下,同一文件在Revit Link层次结构中重复使用两次。 此致