public Result Execute(
ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
UIApplication uiapp = commandData.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Application app = uiapp.Application;
Document doc = uidoc.Document;
// Get a lighting fixture family symbol:
FilteredElementCollector symbols
= Util.GetElementsOfType( doc,
typeof( FamilySymbol ),
BuiltInCategory.OST_LightingFixtures );
FamilySymbol sym = symbols.FirstElement()
as FamilySymbol;
if( null == sym )
{
message = "No lighting fixture symbol found.";
return Result.Failed;
}
// Pick the ceiling:
#if _2010
uidoc.Selection.StatusbarTip
= "Please select ceiling to host lighting fixture";
uidoc.Selection.PickOne();
Element ceiling = null;
foreach( Element e in uidoc.Selection.Elements )
{
ceiling = e as Element;
break;
}
#endif // _2010
Reference r = uidoc.Selection.PickObject(
ObjectType.Element,
"Please select ceiling to host lighting fixture" );
if( null == r )
{
message = "Nothing selected.";
return Result.Failed;
}
// 'Autodesk.Revit.DB.Reference.Element' is
// obsolete: Property will be removed. Use
// Document.GetElement(Reference) instead.
//Element ceiling = r.Element; // 2011
Element ceiling = doc.GetElement( r ) as Wall; // 2012
// Get the level 1:
Level level = Util.GetFirstElementOfTypeNamed(
doc, typeof( Level ), "Level 1" ) as Level;
if( null == level )
{
message = "Level 1 not found.";
return Result.Failed;
}
// Create the family instance:
XYZ p = app.Create.NewXYZ( -43, 28, 0 );
using ( Transaction t = new Transaction( doc ) )
{
t.Start( "Place New Lighting Fixture Instance" );
FamilyInstance instLight
= doc.Create.NewFamilyInstance(
p, sym, ceiling, level,
StructuralType.NonStructural );
t.Commit();
}
return Result.Succeeded;
}