How to get List & Hierarchy of Linked and sub-linked Revit files

How to get List & Hierarchy of Linked and sub-linked Revit files

impararia
Contributor Contributor
4,512 Views
11 Replies
Message 1 of 12

How to get List & Hierarchy of Linked and sub-linked Revit files

impararia
Contributor
Contributor

Hello

We're trying to get the list and hierarchy of all linked external files. Right now we tried the following code:

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());

        }

    }

}

We succeded to get the list of all linked files but there's no hierarchy. We don't know which file is a chieldren of which parent.

This is the Link structure we're trying to get:

 

Links.jpg

0 Likes
Accepted solutions (2)
4,513 Views
11 Replies
Replies (11)
Message 2 of 12

Revitalizer
Advisor
Advisor

Hi,

 

you need to call the function recursively,

 

making InstanceType.GetLinkDocument() your new DocChild.

 

 

Revitalizer




Rudolf Honke
Software Developer
Mensch und Maschine





0 Likes
Message 3 of 12

impararia
Contributor
Contributor

Hi

Thank you for the replay. We tested the method and this is the result:

Document tmp= docchild.getlink();

Result: tmp= null

 

We tried another approach where we open in background the Sub-Assembly in order to get it's linked files using Open/Activate but the system said it's already open. We tried to find the API for "Read Only" mode but no solution.

 

We tried anothr approach where we unload all linked documents; the getlink doesn't find the lined documents.

 

Any advise is helpfull

Regards

0 Likes
Message 4 of 12

jeremytammik
Autodesk
Autodesk

Dear Impararia,

 

I agree with Rudi: you need to call the method recursively.

 

It is not at all clear to me what you have and have not tried so far, and in what way it is failing.

 

Maybe you need to submit a minimal reproducible case to clarify?

 

http://thebuildingcoder.typepad.com/blog/about-the-author.html#1b

 

Cheers,

 

Jeremy

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 5 of 12

impararia
Contributor
Contributor

Hi Jeremy

 

This is the detail to reproduce the case:

 

1- We have a main Revit Model called Assembly16122015-1.rvt. it contains sub-assemblies (external linked files ) and those ones contain other sub-assemblies (external linked files). So we have 3 level external files links.

Our purpose is to read the "links", "documents paths" and "document names" through all external links levels.

 

2- Behaviour versus expectation: We expected to get links, names and hierarchy of all documents. The problem is that, we can't get the names of the documents linked as attachement to the level2 linked document.

 

Level0:Assembly16122015-1.rvt.

    Level1: Linked document1

        Level2: Linked document12

            Level3: Linked document121

            Level3: Linked document122

Level1:Linked document2

we get the names of document 121 & 122 but we are not able to know that they're the children of document12

 

3- Revit Sample Models: All files are attached here. Extract all; open the Assembly16122015-1.rvt then load the external links as attachement to the other provided files.

 

4- Sample Code:

 

public class SaveCmd : IExternalCommand

   {

       public struct test

       {

           public int       Level   { get; set; }

           public Document  Doc     { get; set; }

       }

 

      // The main Execute method (inherited from IExternalCommand) must be public

     public Autodesk.Revit.UI.Result Execute(ExternalCommandData revit,

          ref string message, ElementSet elements)

      {

          UIDocument uidoc = revit.Application.ActiveUIDocument;

          Document Doc = null;

          try

          {

              Doc = uidoc.Document;

          }

          catch (SystemException e)

          {

              TaskDialog.Show("Warning", "No Actif Document");

              return Autodesk.Revit.UI.Result.Failed;

          }

 

          if (NumberLink(Doc) != 0)

          {

              List<test> Listtest = new List<test>();

              test new_test = new test();

 

              new_test.Level = 0;

              new_test.Doc = Doc;

 

              Listtest.Add(new_test);

              int i = 0;

              int AddLine = 0;

 

              do

              {

TaskDialog.Show("Warning", "i=" + i + " Pathname=" + Listtest[i].Doc.PathName);

                  AddLine = 0;

                  if (NumberLink(Listtest[i].Doc) != 0)

                  {

                      FilteredElementCollector collectorI = new FilteredElementCollector(Listtest[i].Doc);

                      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 = Listtest[i].Doc.GetElement(InstanceType.GetTypeId()) as RevitLinkType;

 

                              if (!type.IsNestedLink)

                              {

                                  new_test = new test();

                                  new_test.Level = Listtest[i].Level + 1;

                                  new_test.Doc = InstanceType.GetLinkDocument();

 

                                  AddLine++;

TaskDialog.Show("Warning", "Insert index=" + (i + AddLine) + " name=" + InstanceType.GetLinkDocument().PathName);

                                  Listtest.Insert(i + AddLine, new_test);

                              }

                          }

                      }

                  }

                  i++;

              }

              while (i < Listtest.Count);

 

              for (i = 0; i < Listtest.Count; i++)

              {

                  TaskDialog.Show("Debug", "Level = " + Listtest[i].Level + " Pathmane =" + Listtest[i].Doc.PathName);

              }

          }

 

          return Autodesk.Revit.UI.Result.Succeeded;

      }

      public static int NumberLink(Document Doc)

      {

          int nbLink = 0;

 

          FilteredElementCollector links = new FilteredElementCollector(Doc).OfCategory(BuiltInCategory.OST_RvtLinks);

          foreach (Element e in links)

          {

              if (e is RevitLinkType)

              {

                  nbLink++;

              }

          }

 

          return (nbLink);

      }

 

   }

 

5- Step-By-Step:

- Open the  Assembly16122015-1.rvt in Revit

- Reload all links form the provided files. You'll see that it's 3 level linked document

- Select the Assembly16122015-1.rvt  and run the code in VB

- The code will continue to show all info captured while reading all links starting from Assembly16122015-1.rvt. When it reaches the Level2 sub-assembly we are not able to see that it has children and what are their names.

 

Hope this will help understanding the problem.

Thank you for your help

0 Likes
Message 6 of 12

impararia
Contributor
Contributor

Please donwload these files because I can't upload more than 3 files at a time.

0 Likes
Message 7 of 12

impararia
Contributor
Contributor

Please donwload these files because I can't upload more than 3 files at a time.

0 Likes
Message 8 of 12

impararia
Contributor
Contributor

Please donwload these files because I can't upload more than 3 files at a time.

0 Likes
Message 9 of 12

Dale.Bartlett
Collaborator
Collaborator
Accepted solution

This looks like what you want:

http://adndevblog.typepad.com/aec/2015/12/reading-revit-links-hierarchy.html

Merry Xmas, Dale




______________
Yes, I'm Satoshi.
Message 10 of 12

jeremytammik
Autodesk
Autodesk

A handy way to share multiple files is to either zip them up into one single archive file or place them somewhere publicly accessible, e.g. in a public dropbox folder.

 

Thank you!



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 11 of 12

jeremytammik
Autodesk
Autodesk
Accepted solution

As also noted by Augusto on StackOverflow...

 

http://stackoverflow.com/questions/34336797/how-to-get-list-hierarchy-of-linked-and-sub-linked-revit...

 

Cheers,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 12 of 12

impararia
Contributor
Contributor

Thank you for the replay. The suggested link solved the problem.

Jeremy: I wasn't able to attach a .zip more than 5MB at a time. We'll use Dropbox in the future.

Regards

0 Likes