FamilyInstance PlaceFamilyInstanceOnFace(
UIDocument uidoc,
FamilySymbol symbol)
{
Document doc = uidoc.Document;
Reference r = uidoc.Selection.PickObject(
ObjectType.Face, "Please pick a point on "
+ " a face for family instance insertion" );
Element e = doc.GetElement( r.ElementId );
GeometryObject obj
= e.GetGeometryObjectFromReference( r );
if( obj is PlanarFace )
{
PlanarFace planarFace = obj as PlanarFace;
// Handle planar face case ...
}
else if( obj is CylindricalFace )
{
CylindricalFace cylindricalFace = obj
as CylindricalFace;
// Handle cylindrical face case ...
}
// Better than specialised individual handlers
// for each specific case, handle the general
// case in a generic fashion.
Debug.Assert(
ElementReferenceType.REFERENCE_TYPE_SURFACE
== r.ElementReferenceType,
"expected PickObject with ObjectType.Face to "
+ "return a surface reference" );
Face face = obj as Face;
UV q = r.UVPoint;
#if DEBUG
XYZ p = r.GlobalPoint;
IntersectionResult ir = face.Project( p );
UV q2 = ir.UVPoint;
Debug.Assert( q.IsAlmostEqualTo( q2 ),
"expected same UV point" );
#endif // DEBUG
Transform t = face.ComputeDerivatives( q );
XYZ v = t.BasisX; // or BasisY, or whatever...
return doc.Create.NewFamilyInstance( r, p, v, symbol );
}