Get Host Element in Linked File From Face Based Family

rvtquestions
Advocate

Get Host Element in Linked File From Face Based Family

rvtquestions
Advocate
Advocate

Fam,

 

I was wondering if there was any way to get the Host element (from linked model) from the hosted face based family (in the active model)? The FamilyInstance.Host class seems to return an element of type "RevitLinkType" which is not what I am looking for. Say the host was a linked door, I've tried comparing the FamilyInstance.Host.Id to the element Id of all linked doors and none match. Getting the BuiltInParameter.HOST_ID_PARAM also returns nothing helpful. How can one get the specific host element from the RevitLinkType or even some workaround besides devising some containment test to check? Thank you!

Reply
2,587 Views
16 Replies
Replies (16)

thannaingoo.api
Advocate
Advocate

Hi,

This is working for me to extract the host element from linked file base on hosted family.

 

 foreach (Document linkedDoc in linkedDocList)
            {
                List<Element> col = new FilteredElementCollector(linkedDoc).WhereElementIsNotElementType().OfCategory(BuiltInCategory.OST_Doors).ToList<Element>();

                foreach (Element elmt in col)
                {
                    FamilyInstance fi = elmt as FamilyInstance;
                    string str = fi.Host.Id.ToString();
                }
            }

Regards,

Naing Oo

0 Likes

rvtquestions
Advocate
Advocate

Hi Than,

 

Thanks for your reply and help. Correct me if I am wrong but I am looking through your code and it seems to be saying for each linked document, look at each element that is not a door, cast it as a family instance and get its Host Id. What I am looking for is:

 

//From active document

- Family.Instance...   .PickObject() or from collector

- get .Host from that Family Instance (which I believe returns a Revit Link Instance)

- From that Revit Link Instance somehow get the actual Host Element that is in the Linked Document

0 Likes

thannaingoo.api
Advocate
Advocate

Hi,

 

If we want to access the inked file's elements ,firstly, we have to find the linked file document and then retrieve the elements base on that document. 

 

Please review the following link,

 

http://thebuildingcoder.typepad.com/blog/2013/09/access-to-individual-elements-in-linked-projects.ht...

 

Regards,

Naing Oo

 

 

 

0 Likes

Houba3314
Enthusiast
Enthusiast

did u find a solution?

Houba3314
Enthusiast
Enthusiast

Hello when i get the host it is telling me that is a link and host.id is giving me the link id how can i get the host which is a beam in my case and the not entire link

0 Likes

naveen.kumar.t
Autodesk Support
Autodesk Support

Hi @Houba3314 ,

 

I think this below code should work

Document linkedDoc = null;
foreach(Document d in app.Documents)
  {
   if(d.IsLinked && d.Title=="Your linked Document Name")
      {
        linkedDoc = d;
        break;
      }
  }

Reference r = uidoc.Selection.PickObject(ObjectType.LinkedElement, "Select Linked Element");
Element e = linkedDoc.GetElement(r.LinkedElementId);
FamilyInstance linkedFamInst = e as FamilyInstance;
Element host = linkedFamInst.Host; 

 

If you still face the same issue, Could you please send me a simple non-confidential reproducible sample Revit file and step by step approach to reproduce the issue?


Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

0 Likes

Houba3314
Enthusiast
Enthusiast

thank for your reply in my case i dont select the family instance how can i create reference for my familyinstance. i can do Reference f = new Reference(familyinstance) right? i think my problem is kinda different . You see i placed a family instance on a beam this beam is inside a linked model . so after i placed that familyinstance when i do familyinstance.Host it s telling me it a link 

Houba3314_0-1673627372958.png

 

0 Likes

Houba3314
Enthusiast
Enthusiast

he is an example that green family instance when i do family.host i want it to show me a beam 

0 Likes

Houba3314
Enthusiast
Enthusiast

As you can see in the example thelement is not linked the host is

0 Likes

Houba3314
Enthusiast
Enthusiast
for every beam iwant to the familyinstance attached to it ..
0 Likes

naveen.kumar.t
Autodesk Support
Autodesk Support

Hi @Houba3314 ,

 

As far as I understood, you have a family instance. You are attaching this family instance to a beam. The beam is the host here and the beam is in the linked file. You want to access the beam in the linked file by selecting the family instance in the main document.

Is my understanding correct?

If yes, then please take a look at this below link

https://forums.autodesk.com/t5/revit-api-forum/how-to-get-an-element-host-liveing-in-a-linked-model/...

This is how I tested it
1)I created a beam in a file and saved it. Let's call it beam.rvt.
2)I opened a new project and linked the "beam.rvt"
3)I placed the family instance "RESERVATION rectangulaire HORIZENTAL " in the beam face
4)I selected the "RESERVATION rectangulaire HORIZENTAL" element and executed the same code in the link
5)I am getting beam as the host element.

I hope this helps.


Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

0 Likes

Houba3314
Enthusiast
Enthusiast
i will try it and respond but i have a question in my case i dont want to select the family instance . i want to loop for every family instance i want to get the host . i created a collector for that and im looping for each faily instance., so how can i do that
0 Likes

naveen.kumar.t
Autodesk Support
Autodesk Support

HI @Houba3314 ,

 

Your issue is still unclear to me.

Yes, you can use FilteredElementCollector to filter and collect your family instance in the project.

 

//MAIN DOCUMENT
Document doc = commandData.Application.ActiveUIDocument.Document;
FilteredElementCollector collector = new FilteredElementCollector(doc).OfClass(typeof(FamilyInstance)).WhereElementIsNotElementType();

 

 

//LINKED DOCUMENT
Document linkedDoc = null;
foreach(Document d in app.Documents)
    {
     if(d.IsLinked)
       {
        linkedDoc = d;
        break;
       }
     }
FilteredElementCollector familyInstanceInLinkedDoc = new FilteredElementCollector(linkedDoc).OfClass(typeof(FamilyInstance)).WhereElementIsNotElementType();

Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

0 Likes

Houba3314
Enthusiast
Enthusiast

Sorry i will try to explain it again.

i did this 

foreach Element e in collector {

//the collector of familyinstance in doc

familyinstance f = e as familyinstance;

Element hosttbeam = f.host; // i want this to retrieve the beam and not the link like in the image that i shared previously

}

0 Likes

naveen.kumar.t
Autodesk Support
Autodesk Support

Hi @Houba3314 ,

 

Try using this below code

 foreach(Element e in collector)
     {
       FamilyInstance f = e as FamilyInstance;                
               
       //The below line will give you link not beam element
       /* Element host = famInst.Host as Element; */
               
       //To get beam element, use the below code
         ElementId hostIdLinkedDoc = f.HostFace.LinkedElementId;
         Element beamLinkedDoc = linkedDoc.GetElement(hostIdLinkedDoc);
     }

Please note: To get the linked beam element, you need to get the element from the linked document, not from the main document.


Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

fawzimasri7137
Advocate
Advocate

hi @naveen.kumar.t .. surprised to see me here 🙂

 

i have tried this code months ago, and i could not make work.

 

I think the Main problem is in this line:

ElementId hostIdLinkedDoc = f.HostFace.LinkedElementId;

because it assumes that the f is actually connected to/Placed on  something, and thus you can get its LinkedElementId.

But this does not have to be the case.

In my scenario, i have a model with Floors and i want to find the intersections of these Floors with the Pipes in my Host Document.

So the Floors are in a Loaded Document and the pipes are in my Host document, and  of course they do not know of each others existence.. no?

 

So sure i can go and get all the Floors in the Linked Doc, and i will get there element Ids, but these ids are not usable in the Host Document since they are relative to the linked document.

 

So i have to convert these element IDs to a meaningful element ID in the Host, after which i can look for intersections or what not.

 

am i correct???

 

 

if i am confusing the issue here,... please ignore and i will open another ticket ....