Message 1 of 7
CreateReferenceInLink throwing exception
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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