Description
Each family instance has a location in Revit but getting that position is not straightforward. Some instances have a Location parameter defined, other are hosted and to keep that reference to that host, the location can be obtained by evaluation the position parameter relative to the host curve.
The retrieved position is an XYZ object which holds values stored in feet for 3 axis.
Note : A Location object can be LocationPoint / LocationCurve. To retrieve the curve just add a check for the location type. In this example I focus only on retrieving the location point.
Links
Family Instance – This object represents a single instance of a family type.
FamilyInstance.Host – If the instance is contained within another element, this property returns the containing element. An instance that is face hosted will return the element containing the face.
FamilyInstance.HostParameter – If the instance is hosted by a wall, this property returns the parameter value of the insertion point of the instance along the wall’s location curve, as long as the family of the instance isn’t work plane based.
Location – Provides location functionality for all elements.
XYZ – Object representing coordinates in 3-dimensional space.
Curve.Evaluate – Evaluates and returns the point that matches a parameter along the curve.
Implementation
FamilyInstance familyInstance = (FamilyInstance)instance; LocationPoint locationPoint = familyInstance.Location as LocationPoint; if (null != locationPoint) { double x = ConvertUnits.FeetToMeter(locationPoint.Point.X); double y = ConvertUnits.FeetToMeter(locationPoint.Point.Y); double z = ConvertUnits.FeetToMeter(locationPoint.Point.Z); return new Point3d(x, y, z); } else { //is it hosted? Element host = familyInstance.Host; if (host == null) return null; LocationCurve locationCurve = (LocationCurve)host.Location; if (locationCurve == null) return null; XYZ point = locationCurve.Curve.Evaluate(familyInstance.HostParameter, false); if (point == null) { return null; } //ConvertUnits.FeetToMeter - a static method I created to convert to meters double x = ConvertUnits.FeetToMeter(point.X); double y = ConvertUnits.FeetToMeter(point.Y); double z = ConvertUnits.FeetToMeter(point.Z); //Point3d - an object I defined to store values return new Point3d(x, y, z); }