How to use GetGeometryObjectFromReference() if Reference in rvt-link

How to use GetGeometryObjectFromReference() if Reference in rvt-link

Bogdan1896
Enthusiast Enthusiast
700 Views
4 Replies
Message 1 of 5

How to use GetGeometryObjectFromReference() if Reference in rvt-link

Bogdan1896
Enthusiast
Enthusiast

Part of code on Python:

reference_intersector = ReferenceIntersector(
    ElementId(2471),
    FindReferenceTarget.Face,
    doc.ActiveView) #ElementId refers to RevitLinkInstance
reference_intersector.FindReferencesInRevitLinks = True

floors_refs = [i.GetReference() for i in reference_intersector.Find(point,point2)] #Getting Reference from ReferenceWithContext
linkdoc = doc.GetElement(ElementId(2471)).GetLinkDocument()
floors = [linkdoc.GetElement(i.LinkedElementId) for i in floors_refs] #Getting floor element from link

#Case 1:
face = floors[0].GetGeometryObjectFromReference(floors_refs[0]) #Exception: The id of this element is not same as that referenced by reference
Case 2:
link_inst = doc.GetElement(ElementId(2471))
face = floors[0].GetGeometryObjectFromReference(floors_refs[0].CreateLinkReference(link_inst))#EnvironmentError: System.Runtime.InteropServices.SEHException (0x80004005)

 What am I doing wrong ? In project only one lighting fixture and one floor. This "algorithm" works in a project without linked files

0 Likes
701 Views
4 Replies
Replies (4)
Message 2 of 5

RPTHOMAS108
Mentor
Mentor

Reference.CreateReferenceInLink

 

not

 

Reference.CreateLinkReference

 

i.e. you want to take a reference obtained in the host document where ReferenceIntersector was used and find the equivalent to that in the linked document where you query the element geometry.

 

The other option you used is the reverse of that i.e. to take a reference in the link and create a version of that in the host for dimensioning or hosting elements.

 

Reference.CreateReferenceInLink will only work if the Reference was obtained from a link to start with i.e. it inherently has the link information within the original reference.

0 Likes
Message 3 of 5

Bogdan1896
Enthusiast
Enthusiast

Hello,
Thanks for the answer
Yes, I also tried this option

refs = [i.GetReference().CreateReferenceInLink() for i in reference_intersector.Find(point,point2)]

and get error:

Exception: A managed exception was thrown by Revit or by one of its external applications.


Script Executor Traceback:
Autodesk.Revit.Exceptions.InternalException: A managed exception was thrown by Revit or by one of its external applications.
в Autodesk.Revit.DB.Reference.CreateReferenceInLink()
в Microsoft.Scripting.Interpreter.FuncCallInstruction`2.Run(InterpretedFrame frame)
в Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
в Microsoft.Scripting.Interpreter.LightLambda.Run3[T0,T1,T2,TRet](T0 arg0, T1 arg1, T2 arg2)
в System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
в Microsoft.Scripting.Interpreter.DynamicInstruction`3.Run(InterpretedFrame frame)
в Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
в Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1)
в IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx)
в PyRevitLabs.PyRevit.Runtime.IronPythonEngine.Execute(ScriptRuntime& runtime)

I guess the reference have information about link because:

refs = [i.GetReference().LinkedElementId for i in reference_intersector.Find(point,point2)]

Return Id of floor

0 Likes
Message 4 of 5

RPTHOMAS108
Mentor
Mentor

I followed the process through and below code works ok for me.

 

I think you just need to break it down into more lines and drop the iteration of 'i' part to understand where the actual issue is.

 

 

Public Function Obj_220826a(commandData As ExternalCommandData, ByRef message As String, elements As ElementSet) As Result
        Dim IntUIApp As UIApplication = commandData.Application
        Dim IntUIDoc As UIDocument = commandData.Application.ActiveUIDocument
        Dim IntDoc As Document = IntUIDoc.Document

        Dim AcView As View3D = TryCast(IntUIDoc.ActiveGraphicalView, View3D)
        If AcView Is Nothing Then Return Result.Cancelled Else

        Dim Ri As New ReferenceIntersector(IntUIApp.ActiveUIDocument.ActiveGraphicalView)
        Ri.FindReferencesInRevitLinks = True
        Ri.TargetType = FindReferenceTarget.Face
        Dim RwithC As ReferenceWithContext = Ri.FindNearest(XYZ.Zero, XYZ.BasisX)
        If RwithC Is Nothing Then Return Result.Cancelled Else
        Dim Ref As Reference = RwithC.GetReference

        Dim El As RevitLinkInstance = TryCast(IntDoc.GetElement(Ref), RevitLinkInstance)
        If El Is Nothing Then Return Result.Cancelled Else
        Dim LnkDoc As Document = El.GetLinkDocument

        Dim R1 As Reference = Ref.CreateReferenceInLink

        Dim LnkEl As Element = LnkDoc.GetElement(R1)
        Dim Geom As GeometryObject = LnkEl.GetGeometryObjectFromReference(R1)

        Return Result.Succeeded

    End Function

 

 

 

 

 

 

Message 5 of 5

Bogdan1896
Enthusiast
Enthusiast

So,
I don't not what's going on. I was trying to run python code in pyrevit, dynamo, sharp develop and get same error: "A managed exception was thrown by Revit or by one of its external applications."
I repeat my code on C# language and get this error. I give up))  Thaths for help )

Document doc = this.Document;
UIDocument uidoc = this;
			
XYZ point = uidoc.Selection.PickPoint();
XYZ point_high = new XYZ(point.X, point.Y, 20);
XYZ point_low = new XYZ(0,0,-1);
			
ReferenceIntersector reference_intersector = new ReferenceIntersector(
				(View3D)doc.ActiveView);
reference_intersector.FindReferencesInRevitLinks = true;
reference_intersector.TargetType = FindReferenceTarget.Face;
ReferenceWithContext ref_with_cont = reference_intersector.FindNearest(
				point_high,
				point_low);
Reference reference = ref_with_cont.GetReference();
Reference reference2 = reference.CreateReferenceInLink();