Finding distances to objects with ReferenceIntersector

Here’s a different way to find the position of each door in a wall using the ReferenceIntersector class to shoot a ray in a specified direction from a given point. ReferenceIntersector returns a list with information about the objects hit by the ray and the distance from the element to the ray’s origin.
refintersector

Here’s the source. I’ve tried to be generous with the comments. Let me know if it needs more description.

public void FindDoorsInWallWithReferenceInteresector()
{
    Document doc = this.ActiveUIDocument.Document;
    UIDocument uidoc = new UIDocument(doc);
    Wall wall = doc.GetElement(uidoc.Selection.PickObject(ObjectType.Element)) as Wall;

    LocationCurve locCurve = wall.Location as LocationCurve;
    Curve curve = locCurve.Curve;
    // Find the direction of the tangent to the curve at the curve's midpoint
    // ComputeDerivates finds 3 vectors that describe the curve at the specified point
    // BasisX is the tangent vector
    // "true" means that the point on the curve will be described with a normalized value
    // 0 = one end of the curve, 0.5 = middle, 1.0 = other end of the curve
    XYZ curveTangent = curve.ComputeDerivatives(0.5,true).BasisX;
    // XYZ coordinate of one of the wall's endpoints
    XYZ wallEnd = curve.get_EndPoint(0);

    // Filter to find only doors
    ElementCategoryFilter filter = new ElementCategoryFilter(BuiltInCategory.OST_Doors);

    // ReferenceIntersector uses the specified filter, find elements, in a 3d view (which is the active view for this example)
    ReferenceIntersector intersector = new ReferenceIntersector(filter, FindReferenceTarget.Element, (View3D)doc.ActiveView);

    string doorDistanceInfo = "";
    // ReferenceIntersector.Find shoots a ray in the specified direction from the specified point
    // it returns a list of ReferenceWithContext objects
    foreach (ReferenceWithContext refWithContext in intersector.Find(wallEnd,curveTangent))
    {
        // Each ReferenceWithContext object specifies the distance from the ray's origin to the object (proximity)
        // and the reference of the element hit by the ray
        double proximity = refWithContext.Proximity;
        FamilyInstance door = doc.GetElement(refWithContext.GetReference()) as FamilyInstance;
        doorDistanceInfo += door.Symbol.Family.Name + " - " + door.Name + " = " + proximity + "\n";
    }
    TaskDialog.Show("ReferenceIntersector", doorDistanceInfo);
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s