public Result Execute(
ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
UIApplication app = commandData.Application;
UIDocument uidoc = app.ActiveUIDocument;
Document doc = uidoc.Document;
Result rc = Result.Failed;
using ( Transaction t = new Transaction( doc ) )
{
t.Start( "Place a New Sprinkler Instance" );
// retrieve the sprinkler family symbol:
#if _2010
Filter filter = app.Create.Filter.NewFamilyFilter(
_name );
List<Element> families = new List<Element>();
doc.get_Elements( filter, families );
Family family = null;
foreach( Element e in families )
{
family = e as Family;
if( null != family )
break;
}
#endif // _2010
Family family = Util.GetFirstElementOfTypeNamed(
doc, typeof( Family ), _name ) as Family;
if ( null == family )
{
if ( !doc.LoadFamily( _filename, out family ) )
{
message = "Unable to load '" + _filename + "'.";
return rc;
}
}
FamilySymbol sprinklerSymbol = null;
//foreach( FamilySymbol fs in family.Symbols ) // 2014
foreach ( ElementId id in
family.GetFamilySymbolIds() ) // 2015
{
sprinklerSymbol = doc.GetElement( id )
as FamilySymbol;
break;
}
Debug.Assert( null != sprinklerSymbol,
"expected at least one sprinkler symbol"
+ " to be defined in family" );
// pick the host ceiling:
Element ceiling = Util.SelectSingleElement(
uidoc, "ceiling to host sprinkler" );
if ( null == ceiling
|| !ceiling.Category.Id.IntegerValue.Equals(
(int) BuiltInCategory.OST_Ceilings ) )
{
message = "No ceiling selected.";
return rc;
}
//Level level = ceiling.Level;
//XYZ p = new XYZ( 40.1432351841559, 30.09700395984548, 8.0000 );
// these two methods cannot create the sprinkler on the ceiling:
//FamilyInstance fi = doc.Create.NewFamilyInstance( p, sprinklerSymbol, ceiling, level, StructuralType.NonStructural );
//FamilyInstance fi = doc.Create.NewFamilyInstance( p, sprinklerSymbol, ceiling, StructuralType.NonStructural );
// use this overload so get the bottom face of the ceiling instead:
// FamilyInstance NewFamilyInstance( Face face, XYZ location, XYZ referenceDirection, FamilySymbol symbol )
// retrieve the bottom face of the ceiling:
Options opt = app.Application.Create.NewGeometryOptions();
opt.ComputeReferences = true;
GeometryElement geo = ceiling.get_Geometry( opt );
PlanarFace ceilingBottom = null;
foreach ( GeometryObject obj in geo )
{
Solid solid = obj as Solid;
if ( null != solid )
{
foreach ( Face face in solid.Faces )
{
PlanarFace pf = face as PlanarFace;
if ( null != pf )
{
XYZ normal = pf.FaceNormal.Normalize();
if ( Util.IsVertical( normal )
&& 0.0 > normal.Z )
{
ceilingBottom = pf;
break;
}
}
}
}
}
if ( null != ceilingBottom )
{
XYZ p = PointOnFace( ceilingBottom );
// Create the sprinkler family instance:
FamilyInstance fi = doc.Create.NewFamilyInstance(
ceilingBottom, p, XYZ.BasisX, sprinklerSymbol );
rc = Result.Succeeded;
}
t.Commit();
}
return rc;
}