Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

GetLinkDocument() method for nested Revit link returns null

12 REPLIES 12
SOLVED
Reply
Message 1 of 13
swilliamsH8U9A
3131 Views, 12 Replies

GetLinkDocument() method for nested Revit link returns null

Even though I am able to collect the nested Revit links within a Revit link, and get some of its properties, I am unable to get the nested link's document like I can its parent link. 

 

The python script goes as below:

 


linkDoc = selectedElem.GetLinkDocument() linkTransform = selectedElem.GetTotalTransform() #Get Nested links nestLinkCollector = list(FilteredElementCollector(linkDoc).OfCategory(BuiltInCategory.OST_RvtLinks).WhereElementIsNotElementType()) linkElemCollector = [] for nl in nestLinkCollector: nestLinkDoc = nl.GetLinkDocument() nestLinkElemCollector = list(FilteredElementCollector(linkDoc).OfCategory(BuiltInCategory.OST_IOSModelGroups).WhereElementIsNotElementType().ToElements())

 

I can get the nl.Name property, but the nl.GetLinkDocument() call returns null/None. Thus, I am unable to retrieve elements within the nested Revit link as I can in the regular Revit UI. 

 

Any tips would be appreciated. 

 

12 REPLIES 12
Message 2 of 13
Sean_Page
in reply to: swilliamsH8U9A

Should you be using nestLinkDoc and not linkDoc for the nested collector?

Message 3 of 13
swilliamsH8U9A
in reply to: Sean_Page

Good catch; that was done in an attempt at throwing everything at the wall to find a solution. Thanks.

 

However, the problem occurs with nestLinkDoc = nl.GetLinkDocument() . This returns null/none.

Message 4 of 13
Sean_Page
in reply to: swilliamsH8U9A

Are the nested links set to Overlay? This would probably keep you from getting the document because the nested link isn't actually loaded. 

Message 5 of 13
swilliamsH8U9A
in reply to: Sean_Page

Thanks for following up. Yes, the nested and parent links are all set to "attachment" in their respective models. 

Message 6 of 13
Sean_Page
in reply to: swilliamsH8U9A

@swilliamsH8U9AI have some interesting although not entirely helpful results. Perhaps a post on the Dynamo forums may yield you more support for Python if you don't get an answer here.

 

1. I was able to reproduce you issue using Python via Dynamo

lnks.PNG

 

2. However, I was NOT able to reproduce this in C# via add-in. It worked as expected.

public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            Document doc = uiapp.ActiveUIDocument.Document;
            using (FilteredElementCollector fec = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_RvtLinks).WhereElementIsNotElementType())
            {
                foreach (RevitLinkInstance link in fec.ToElements())
                {
                    Document linkDoc = link.GetLinkDocument();
                    using (FilteredElementCollector linkfec = new FilteredElementCollector(linkDoc).OfCategory(BuiltInCategory.OST_RvtLinks).WhereElementIsNotElementType())
                    {
                        foreach (RevitLinkInstance nLink in fec.ToElements())
                        {
                            Document nestDoc = nLink.GetLinkDocument();
                            using (FilteredElementCollector nLinkWalls = new FilteredElementCollector(nestDoc).OfCategory(BuiltInCategory.OST_Walls).WhereElementIsElementType())
                            {
                                foreach (WallType wType in nLinkWalls.ToElements())
                                {
                                    MessageBox.Show(wType.Name);
                                }
                            }
                        }
                    }
                }
            }
            return Result.Succeeded;
        }
Message 7 of 13

Does Python wrap the Revit API `Document` object in some other class?

 

I thought I saw examples of it doing something like that in the past.

 

If so, you may need to unwrap it to access the original Document object to call GetLinkDocument on it, or maybe unwrap the result returned by GetLinkDocument to pass it into the collector?

 



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

Message 8 of 13
swilliamsH8U9A
in reply to: Sean_Page

Thanks for taking time to investigate! It's useful to know the issue is reproducible in python, but not via C# addin. And that the fix likely lies with a workaround in IronPython/RPW. 

 

I've taken a bit of time to explore how to unwrap and expose Revit API's 'document' in python. For now, I am seeing much discussion on UnwrapElement() in Dynamo, and unwrap() in the RPW documentation. Nothing immediately seems to work yet, but I will report back if it does. I will also take this to the Dynamo forums and pyRevit git. 

 

Thanks again!

Message 9 of 13

Hi,

 

I've faced this also. Revit apparently treats nested links as "normal" links (you can Tab select them and copy out of the mainlink file, go figure!) Behaves something like nested groups

 

The way to go depends on what you want to accomplish.

A. If you want a list of all modelgroups per link (not in treeform) then:

1.  use FilteredElementcollector on active project by OfClass RevitlinkInstance or ByCategory OST_RvtLinks

    (this will also include the nested links!)

2. Loop all links and retrieve modelgroups as in your code

 

B. If you want a list of all modelgroups of a picked link (You also can pick a Nested link! with Tab select)

1. Use the selected revitlinkinstance to retrieve the modelgroups.

 

C. If you want a list of all modelgroups of a picked Top-level link and from it's nested links (this will be tricky)

1. Pick a top-level link

2. See if selected link has nested links (and get the Fullpathnames per RevitLinkType !)

3. If yes, then retrieve all links from active model (FilteredElementcollector OfClass RevitlinkInstance etc) and also get all the RevitLinkType FullPathnames

4. compare the RevitLinkType fullpathnames to get the correct one(s) and retrieve the modelgroups from it.

 

Notes:

- Always retrieve document of a link from the workable project when using RevitLinkInstance.GetLinkDocument.

- Also look into some properties of the elements of RevitLinkType: GetTopLevelLink / IsNestedLink

- Also use "Revit Lookup" autodesk addin for handy info about element and properties

- Don't use elementid of nested RevitLinkInstance to compare: they differ from the link in the top-level link and the nested link in the workable document.

 

I hope it points you in the right direction, succes.

Best Regards, Michel

Message 10 of 13
TripleM-Dev.net
in reply to: Sean_Page

Hi,

 

In the C# code in the second ForEach loop you also reference to fec.ToElements, it retrieves the elements from the first Collector. If you replace it with the linkfec collection the second loop creates a error, no document retrieved.

 

The nested walls appear to be retrieved because Revit retrieves all links (including the nested ones) in the first FilteredElementCollector.

 

So I think Python and C# retrieve the links identical.

 

Michel

 

Message 11 of 13

Ah. So perhaps this might be a limitation of the Revit API?

 

If so, I will just create a workaround within the parent link's file to retrieve model group data from its nested links. 

Message 12 of 13

No I don't think so, it's just the way it's implemented in Revit API.

 

The GetLinkDocument can only be retrieved from a instance in the workable project, this includes the nested links.

So nested links info/data can be retrieved, how you code it depends on the desired endresult/workflow of the addin.

 

Michel

 

Message 13 of 13

Ah! I see the issue now. In my python script, I was relying on the user to select which link they'd want to retrieve the data from. The script then sought to filter any nested link within the selected link, which returned those aforementioned errors. 

 

In  @Sean_Page 's C# attempt to reproduce the issue, they instead filtered all links from within the host doc, which included nested links. This, combined with your suggestion to use "RevitLinkType" to match the path of the user-selected link will provide what I need to proceed. 

 

Thanks a ton guys! This community is amazing!

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Rail Community