Create Hosted Element onto Linked Revit Element by PickObject

Create Hosted Element onto Linked Revit Element by PickObject

fwang52MYR
Contributor Contributor
421 Views
5 Replies
Message 1 of 6

Create Hosted Element onto Linked Revit Element by PickObject

fwang52MYR
Contributor
Contributor

Hi All -

 

I'm trying to create a new (hosted) family instance by having a user pick a point in the in the project.

I start with picking a point, which is straightforward.  

 

 

uidoc.Selection.PickObject(ObjectType.PointOnElement)

 

 

Then, retrieve the face in the linked element's geometry by:

 

 

Element element = doc.GetElement(pickedRef.ElementId);
RevitLinkInstance linkInstance = element as RevitLinkInstance;

Document linkedDoc = linkInstance.GetLinkDocument();
ElementId linkedElementId = pickedRef.LinkedElementId;
Element linkedElement = linkedDoc.GetElement(linkedElementId);

GeometryObject geomObj = linkedElement.GetGeometryObjectFromReference(pickedRef.CreateReferenceInLink());
Face face;
if (!(geomObj is Face f)) return Result.Cancelled;
face = geomObj as Face;

Options options = new Options();
options.ComputeReferences = true;
GeometryElement geomElem = linkedElement.get_Geometry(options);
foreach (GeometryObject go in geomElem)
     {
            if (go is Solid s)
            {
                foreach (Face fa in s.Faces)
                {
                    if (fa == face)
                    {
                        face = fa;
                    }
                }
            }
     }

FamilyInstance newInstance = doc.Create(face,
                                        pickedRef.GlobalPoint,
                                        XYZ.BasisZ,
                                        familySymbol);

 

 

 This is a bit convoluted for sure, but I don't know how else to get a Face that has references enabled as demanded by Create.NewFamilyInstance(Face, XYZ, XYZ, FamilySymbol) method. This is also the method that I'm trying to use to create the family instance. However, this gives the following error:

fwang52MYR_0-1727213376535.png

It appears that I've reached a dead end. But other methods I've tried including NewFamilyInstance(Reference, XYZ, XYZ, FamilySymbol) also don't work... I'm I using the wrong methods?

 

Any ideas on how to create a hosted element onto a linked element's face?

Thank you all in advance. 🙂

0 Likes
Accepted solutions (1)
422 Views
5 Replies
Replies (5)
Message 2 of 6

fwang52MYR
Contributor
Contributor

Continuing to research and experiment, I've stumbled upon this post How can I get the reference for a face that is queried from a linked project? - Autodesk Community

Knowing that I might need to juggle the reference back and forth, I've added the following code block to no avail:

 

 

 

 

string linkedRef = face.Reference.ConvertToStableRepresentation(linkedDoc);
Reference refToLinkedFace = Reference.ParseFromStableRepresentation(linkedDoc /* can't use current document to parse */, linkedRef);

// Updated the arguements for NewFamilyInstance method call. 
FamilyInstance newInstance = doc.Create.NewFamilyInstance(refToLinkedFace, 
                                                 pickedRef.GlobalPoint,
                                                 XYZ.BasisZ, 
                                                 familySymbol);

 

 

 

 

This gives me the following error message which helps very little...

fwang52MYR_0-1727271901222.png

 

0 Likes
Message 3 of 6

Moustafa_K
Collaborator
Collaborator

I recall i have posted a flow of how you can do it, 

you can get a the link using this statement

AlinkedReference.CreateLinkReference(rvtInstance);

see if this blogpost helps. 

Moustafa Khalil
Cropped-Sharp-Bim-500x125-Autodesk-1
0 Likes
Message 4 of 6

fwang52MYR
Contributor
Contributor

Hi Moustafa - 

 

Thanks for the response! Greatly appreciated. I was referencing your blog post when coding this, but I'm getting errors.

 

I got the following error, which doesn't say much about what's wrong: 

fwang52MYR_0-1728486808895.png

 

Here is the code segment of interest:

 

 

var linkRefToFace = faceInLinkWithRef.Reference.CreateLinkReference(linkInstance);

FamilyInstance fi = doc.Create.NewFamilyInstance(
        linkRefToFace,
        pickedRef.GlobalPoint,
        XYZ.BasisZ,
        fs);

 

 

0 Likes
Message 5 of 6

Moustafa_K
Collaborator
Collaborator
Accepted solution

Can't say much but I think your issue in the XYZ.BasisZ argument in the Create Family instance. You need to check which Direction your FamilyInstance is supposed to be directed.

Moustafa Khalil
Cropped-Sharp-Bim-500x125-Autodesk-1
0 Likes
Message 6 of 6

fwang52MYR
Contributor
Contributor
That work!

I spent so much time on troubleshooting references, and it was the issue with the vector...

Anyhow, thank you so much!
0 Likes