Get parent of a part in a linked model

Get parent of a part in a linked model

a.sabatier
Contributor Contributor
1,014 Views
3 Replies
Message 1 of 4

Get parent of a part in a linked model

a.sabatier
Contributor
Contributor

Hi

I've created a part from a linked file and I'm trying to get the parent of that part.

 

I found that code that work when the parent is in the same document that the part but it returns "-1" when the parent is in the linked document.

 

Document documentlie = doc;
            //On crée la variable pour le document lié.
            foreach (Document dmt in uiapp.Application.Documents)
            {
                if (dmt != doc) documentlie = dmt;
            }
Element elt; 
 
Part elementpart = elt as Part;
ICollection<LinkElementId> lIds = elementpart.GetSourceElementIds();
LinkElementId lId = lIds.First<LinkElementId>();
ElementId parentElementId = lId.HostElementId;
 
TaskDialog.Show("parentElementId", parentElementId.ToString());//return the right id when the parent is in the same file and "-1" when it's in a linked document
 
 
Thanks for your help.
 
Alexandre
 
0 Likes
Accepted solutions (1)
1,015 Views
3 Replies
Replies (3)
Message 2 of 4

jeremytammik
Autodesk
Autodesk
Accepted solution

Dear Alexandre,

 

Thank you for your query.

 

GetSourceElementIds returns the ids of the elements from which a Part has been created by the PartMaker. There may be more than one source only if a merge is involved somewhere in the history of the Part.

 

In order to use it with a linked document, you probably have to check the LinkElementId lId first and see whether it represents a linked or local id.

 

As far as I can tell from the documentation, the HostElementId is designed to return -1 (representing an invalid element id, ElementId.Invalid) for linked elements.

 

For linked elements, you have to use the LinkedElementId property instead.

 

Look at the documentation of the LinkElementId members:

 

https://apidocs.co/apps/revit/2019/dccd1367-b891-92f1-f0d2-ece20af2b0cc.htm

 

  • HostElementId - The id of the element in the host, or invalidElementId if there is a link.
  • LinkedElementId - The id of the element in the link, or invalidElementId if no link.

 

I hope this helps.

 

Best regards,

 

Jeremy

 



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

Message 3 of 4

a.sabatier
Contributor
Contributor

Thanks Jeremy

 

I just changed 

ElementId parentElementId = lId.HostElementId;

 

with

ElementId parentElementId = lId.LinkedElementId;

 

and it works.

 

Alexandre

Message 4 of 4

jeremytammik
Autodesk
Autodesk

Lucky you!

 

And lucky me, for being able to guess the answer so easily  🙂

 

Cheers,

 

Jeremy

 



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

0 Likes