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;
// obtain the current selection and pick
// out all walls from it:
//Selection sel = uidoc.Selection; // 2014
ICollection<ElementId> ids = uidoc.Selection
.GetElementIds(); // 2015
List<Wall> walls = new List<Wall>( 2 );
//foreach( Element e in sel.Elements ) // 2014
foreach( ElementId id in ids ) // 2015
{
Element e = doc.GetElement( id );
if( e is Wall )
{
walls.Add( e as Wall );
}
}
if( 2 != walls.Count )
{
message = _prompt;
return Result.Failed;
}
// ensure the two selected walls are straight and
// parallel; determine their mutual normal vector
// and a point on each wall for distance
// calculations:
List<Line> lines = new List<Line>( 2 );
List<XYZ> midpoints = new List<XYZ>( 2 );
XYZ normal = null;
foreach( Wall wall in walls )
{
LocationCurve lc = wall.Location as LocationCurve;
Curve curve = lc.Curve;
if( !( curve is Line ) )
{
message = _prompt;
return Result.Failed;
}
Line l = curve as Line;
lines.Add( l );
midpoints.Add( Util.Midpoint( l ) );
if( null == normal )
{
normal = Util.Normal( l );
}
else
{
if( !Util.IsParallel( normal, Util.Normal( l ) ) )
{
message = _prompt;
return Result.Failed;
}
}
}
// find the two closest facing faces on the walls;
// they are vertical faces that are parallel to the
// wall curve and closest to the other wall.
Options opt = app.Create.NewGeometryOptions();
opt.ComputeReferences = true;
List<Face> faces = new List<Face>( 2 );
faces.Add( GetClosestFace( walls[0], midpoints[1], normal, opt ) );
faces.Add( GetClosestFace( walls[1], midpoints[0], normal, opt ) );
// create the dimensioning:
CreateDimensionElement( doc.ActiveView,
midpoints[0], faces[0].Reference,
midpoints[1], faces[1].Reference );
return Result.Succeeded;
}