Hyperlink 如何获取列表&;链接和子链接Revit文件的层次结构

Hyperlink 如何获取列表&;链接和子链接Revit文件的层次结构,hyperlink,revit,Hyperlink,Revit,我们正在尝试获取所有链接外部文件的列表和层次结构。现在,我们尝试了以下代码: FilteredElementCollector collectorI = new FilteredElementCollector(DocChild); IList<Element> elemsI = collectorI.OfCategory(BuiltInCategory.OST_RvtLinks).OfClass(typeof(RevitLinkInstance)).ToElements(); f

我们正在尝试获取所有链接外部文件的列表和层次结构。现在,我们尝试了以下代码:

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());
        }
    }
}
filteredelementcollectori=新的FilteredElementCollector(DocChild);
IList elemsI=collectorI.OfCategory(BuiltInCategory.ostrvtlinks).OfClass(typeof(RevitLinkInstance)).ToElements();
foreach(elemsI中的元素eI)
{
如果(eI是RevitLinkInstance)
{
RevitLinkInstance InstanceType=eI作为RevitLinkInstance;
RevitLinkType type=DocChild.GetElement(InstanceType.GetTypeId())作为RevitLinkType;
TaskDialog.Show(“Debug”、“IsNestedLink=“+type.IsNestedLink.ToString()+”IsLinked=“+DocChild.IsLinked.ToString());
如果(!type.IsNestedLink)
{
TaskDialog.Show(“调试”、“子项=“+InstanceType.GetLinkDocument().PathName.ToString());
}
}
}
我们成功地获得了所有链接文件的列表,但没有层次结构。我们不知道哪个文件是哪个父文件的子文件

这是我们试图获得的链接结构:


您需要使用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);
  }
}
公共结果执行(
外部命令数据命令数据,
参考字符串消息,
元素集元素)
{
//获取活动文档
Document mainDoc=commandData.Application.ActiveUIDocument.Document;
//准备显示结果。。。
TreeNode mainNode=新的TreeNode();
mainNode.Text=mainDoc.PathName;
//从根链接开始(无父节点)
FilterDeletementCollector coll=新的FilterDeletementCollector(mainDoc);
类的集合(typeof(RevitLinkInstance));
foreach(在coll中修改linkinstance inst)
{
RevitLinkType type=mainDoc.GetElement(inst.GetTypeId())作为RevitLinkType;
if(type.GetParentId()==ElementId.InvalidelElementId)
{
TreeNode parentNode=新的TreeNode(inst.Name);
mainNode.Nodes.Add(父节点);
GetChilds(mainDoc,type.getChildId(),parentNode);
}
}
//在表单中显示结果
System.Windows.Forms.Form resultForm=new System.Windows.Forms.Form();
TreeView TreeView=新的TreeView();
treeView.Size=resultForm.Size;
treeView.Anchor |=锚点样式。底部|锚点样式。顶部;
treeView.Nodes.Add(主节点);
结果表单.控件.添加(treeView);
resultForm.ShowDialog();
返回结果。成功;
}
私有void GetChilds(文档mainDoc、ICollection id、,
树节点(父节点)
{
foreach(id中的ElementId)
{
//获取孩子的信息
RevitLinkType=mainDoc.GetElement(id)作为RevitLinkType;
TreeNode子节点=新的TreeNode(type.Name);
parentNode.Nodes.Add(子节点);
//然后进入下一级
GetChilds(mainDoc,type.getChildId(),子节点);
}
}
结果应该是这样的:


原创博文。

谢谢你的回答。这在很大程度上解决了我的问题,但我还有一个问题:如何获得所有实例的完整路径名。在某些情况下,同一文件在Revit链接层次中重复使用两次。
问候

谢谢你的回答。这在很大程度上解决了我的问题,但我还有一个问题:如何获得所有实例的完整路径名。在某些情况下,同一文件在Revit链接层次中重复使用两次。Regardsf,以便您需要链接文件中文档对象的路径名。它应该可以正常工作,但在RevitLinkType中无法访问仅路径名。在分总成中,附件类型总是重叠的。我不知道在过去一两年中,这方面是否有任何改进。在以前的版本中,Revit API未在任何地方提供完整路径。您可以使用以下解决方法:1。确保有一个定义良好的根节点列表,所有链接都位于该列表下。2.确保链接的文件名是唯一的。3.使用操作系统文件系统功能根据目标文件名递归搜索和查找目标文件路径。这是一个新问题,不是答案。