CreateReferenceInLink throwing exception

CreateReferenceInLink throwing exception

ZarkoMocnik
Contributor Contributor
1,008 Views
6 Replies
Message 1 of 7

CreateReferenceInLink throwing exception

ZarkoMocnik
Contributor
Contributor

Hi,

I am using ReferenceIntersector for finding some object. Everythink works as expected when found object is in the same file. But in case that found object is in linked file I can't (or not know how) to get appropriate face. I already checked online and found out that I have to explicitly manage with references and linked files. According pages online I think that the logic is OK, but when I call CreateReferenceInLink  exception "A managed exception was thrown by Revit or by one of its external applications." is thrown.

 

Here is the important part of code:

private XYZ FindNearest(XYZ origin,XYZ normal,XYZ xdirection)
        {
            if (_doc.ActiveView.GetType() != typeof(View3D))
                return null;
            List<ElementFilter> filters = new List<ElementFilter>();
            filters.Add(new ElementClassFilter(typeof(Wall)));
            filters.Add(new ElementClassFilter(typeof(RoofBase)));
            filters.Add(new ElementClassFilter(typeof(FootPrintRoof)));
            ElementLogicalFilter filter = new LogicalOrFilter(filters);

            ReferenceIntersector refIntersector = new ReferenceIntersector(filter,FindReferenceTarget.All, _doc.ActiveView as View3D);
            refIntersector.FindReferencesInRevitLinks = true;
            ReferenceWithContext foundReference=null;
            ReferenceWithContext foundReference = refIntersector.FindNearest(origin, xdirection);
            if (foundReference == null)
                return null;
            Reference rf = foundReference.GetReference();
            if(rf.ElementReferenceType == ElementReferenceType.REFERENCE_TYPE_SURFACE)
            {
                Element el;
                Face face;
                if (rf.LinkedElementId != ElementId.InvalidElementId) // <-------- Special processing in case object is found in linked file
                {
                    RevitLinkInstance rli = _doc.GetElement(rf.ElementId) as RevitLinkInstance;
                    el = rli.GetLinkDocument().GetElement(rf.LinkedElementId);
rf = rf.CreateReferenceInLink(); // <---------- HERE EXCEPTION IS THROWN
} else { el = _doc.GetElement(rf.ElementId); } face= el.GetGeometryObjectFromReference(rf) as Face; if (el == null) return null; return face.Project(origin)?.XYZPoint; } return null; }

I already googled and found some advices but If I understand them correctly they direct to use CreateReferenceInLink as I used in the code.

 

Any idea?

Žarko

0 Likes
1,009 Views
6 Replies
Replies (6)
Message 2 of 7

naveen.kumar.t
Autodesk Support
Autodesk Support

Hi @ZarkoMocnik ,

Have you already seen this link?

using-referenceintersector-in-linked-files.html 


Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 3 of 7

ZarkoMocnik
Contributor
Contributor

Hi @naveen.kumar.t 

Thanks for suggestion. I looked in many pages, but didn't find that one. Unfortunately it doesnt't help.

 

I successfully get reference to object from ReferenceIntersector even if it is in linked file.

ReferenceWithContext foundReference = refIntersector.FindNearest(origin, xdirection);

I can also extract that reference points to linked element:

if (rf.LinkedElementId != ElementId.InvalidElementId) // <-------- Special processing in case object is found in linked file
{
     RevitLinkInstance rli = _doc.GetElement(rf.ElementId) as RevitLinkInstance;
     el = rli.GetLinkDocument().GetElement(rf.LinkedElementId);

     rf = rf.CreateReferenceInLink(); // <---------- HERE EXCEPTION IS THROWN
}

But in the process of extracting GeometryObject from reference exception is thrown. (See line above).

 

I looked on many pages but nowhere found exact procedure how to navigate from reference returned by ReferenceIntersector to geometry object (Face) in linked file. There are some similar cases, but not identical. According to them i concluded that procedure should be as wrote in my original post, but from unknown reason revit throws exception when calling CreateReferenceInLink(). (probably the procedure is wrong)

 

If I delete all unnecessary code and renamed some variables to make procedure as compact as possible.  The goal is to get Face object in linked file from ReferenceIntersector and the procedure given below doesn't work (throws exception)

 

ReferenceIntersector refIntersector = new ReferenceIntersector(filter,FindReferenceTarget.All, _doc.ActiveView as View3D);
refIntersector.FindReferencesInRevitLinks = true;
ReferenceWithContext foundReference = refIntersector.FindNearest(origin, xdirection);
Reference rfFromDocument=foundReference.GetReference()
RevitLinkInstance rLinkInstance = _doc.GetElement(rfFromDocument.ElementId) as RevitLinkInstance; Element linkDocument= rLinkInstance.GetLinkDocument().GetElement(rfFromDocument.LinkedElementId); Reference rfLinked = rfFromDocument.CreateReferenceInLink(); // <---------- HERE EXCEPTION IS THROWN Face face= linkDocument.GetGeometryObjectFromReference(rfLinked) as Face;

If i create stable reference from rfFRomDocument I got string pointing to some surface in revit link. The question us how to get Face object to which that reference is pointing.

0 Likes
Message 4 of 7

jeremytammik
Autodesk
Autodesk

Can you share a minimal reproducible case with exact steps for reproducing the problem, please?

 

https://thebuildingcoder.typepad.com/blog/about-the-author.html#1b

 

A linked model and a host model with a macro should be sufficient, each containing only a handful of elements.

 

Thank you!

 



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

0 Likes
Message 5 of 7

jeremytammik
Autodesk
Autodesk

The forum placed my answer to the other thread into this one. Forum slow, user quick, problem ensues.

 

 

 



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

0 Likes
Message 6 of 7

ZarkoMocnik
Contributor
Contributor

Dear Jeremy,

Thanks for your interest. I tried to attach two files (about 7 MB in total) but after clicking to "Post" button message is not submitted. I suspect that reason is file size. So I uploaded botfh files in one Zip file on WeTransfer. You can download it here

 

In downloaded file (Sample.zip) you will find two files Host1.rvt and Link1.rvt. Please open Host1.rvt. In that file Link1.rvt is linked.

  • Please make view "Wall in host file" active. In that view one conduit and one wall (modelled in Host1.rvt) is shown.
  • Now run macro: TestFindNearest which is included in Host1.rvt.
  • Revit should respond with TaskDialog showing 1.99999 what is projected distance between specific point (on conduit) and wall. That is expected behaviour.
  • Now activate view "Wall in linked file". You will see the same -- Wall and conduit. The difference is that wall  shown in this view is modelled in Link1.rvt, (conduit exists only in Host1.rvt). Wall modelled in Host1.rvt is hidden in this view similarly as it is complete Link1.rvt hidden in "Wall in host file" view.
  • Run the same macro again.
  • Revit responds with exception thrown in statement (please see source code) where I am trying to call CreateReferenceInLink.

If you analyse the code, you will find out that ReferenceIntersector in second case returns reference to some geometry in linked file. After that I am trying to check if returned reference is pointing to host or linked file. If it is in linked file I slightly modify the procedure how to get appropriate face. I wrote that procedure after analysing some similar (but not the same) samples on net, so it is possible that the procedure is wrong.

 

Actually, my ultimate wish is to get the same result -- distance between point and nearest face of wall now modelled in linked file.

 

(Wall in Link1.rvt is not modelled on the same position as in Host1.rvt. That is intentionally. O moved origin of Link1. rvt in Host1. rvt accordingly, so that result will be the same -- 1.99999 only if procedure of retrieving distance is taking into account also relative position of linked file in host file, but up to now, due to the problem with retrieving geometry in linked file,  I didn't get so far to check also that part.)

 

Kind regards,

Žarko

0 Likes
Message 7 of 7

Bogdan1896
Enthusiast
Enthusiast

Hello,
Did you manage to find a solution to the problem ? I have the same problem https://forums.autodesk.com/t5/revit-api-forum/how-to-use-getgeometryobjectfromreference-if-referenc...