public Result Execute(
ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
UIApplication uiapp = commandData.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Document doc = uidoc.Document;
Result commandResult = Result.Succeeded;
Categories cats = doc.Settings.Categories;
ElementId catDoorsId = cats.get_Item(
BuiltInCategory.OST_Doors ).Id;
ElementId catWindowsId = cats.get_Item(
BuiltInCategory.OST_Windows ).Id;
try
{
List<ElementId> selectedIds = uidoc.Selection
.GetElementIds().ToList();
using( Transaction trans = new Transaction( doc ) )
{
trans.Start( "Cmd: GetOpeningProfiles" );
List<ElementId> newIds = new List<ElementId>();
foreach( ElementId selectedId in selectedIds )
{
Wall wall = doc.GetElement( selectedId ) as Wall;
if( wall != null )
{
List<PlanarFace> faceList = new List<PlanarFace>();
List<ElementId> insertIds = wall.FindInserts(
true, false, false, false ).ToList();
foreach( ElementId insertId in insertIds )
{
Element elem = doc.GetElement( insertId );
if( elem is FamilyInstance )
{
FamilyInstance inst = elem as FamilyInstance;
CategoryType catType = inst.Category
.CategoryType;
Category cat = inst.Category;
if( catType == CategoryType.Model
&& ( cat.Id == catDoorsId
|| cat.Id == catWindowsId ) )
{
faceList.AddRange(
GetWallOpeningPlanarFaces(
wall, insertId ) );
}
}
else if( elem is Opening )
{
faceList.AddRange(
GetWallOpeningPlanarFaces(
wall, insertId ) );
}
}
foreach( PlanarFace face in faceList )
{
//Plane facePlane = new Plane(
// face.ComputeNormal( UV.Zero ),
// face.Origin ); // 2016
Plane facePlane = Plane.CreateByNormalAndOrigin(
face.ComputeNormal( UV.Zero ),
face.Origin ); // 2017
SketchPlane sketchPlane
= SketchPlane.Create( doc, facePlane );
foreach( CurveLoop curveLoop in
face.GetEdgesAsCurveLoops() )
{
foreach( Curve curve in curveLoop )
{
ModelCurve modelCurve = doc.Create
.NewModelCurve( curve, sketchPlane );
newIds.Add( modelCurve.Id );
}
}
}
}
}
if( newIds.Count > 0 )
{
View activeView = uidoc.ActiveGraphicalView;
activeView.IsolateElementsTemporary( newIds );
}
trans.Commit();
}
}
#region Exception Handling
catch( Autodesk.Revit.Exceptions
.ExternalApplicationException e )
{
message = e.Message;
Debug.WriteLine(
"Exception Encountered (Application)\n"
+ e.Message + "\nStack Trace: "
+ e.StackTrace );
commandResult = Result.Failed;
}
catch( Autodesk.Revit.Exceptions
.OperationCanceledException e )
{
Debug.WriteLine( "Operation cancelled. "
+ e.Message );
message = "Operation cancelled.";
commandResult = Result.Succeeded;
}
catch( Exception e )
{
message = e.Message;
Debug.WriteLine(
"Exception Encountered (General)\n"
+ e.Message + "\nStack Trace: "
+ e.StackTrace );
commandResult = Result.Failed;
}
#endregion
return commandResult;
}