public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
Document doc = commandData.Application.ActiveUIDocument.Document;
FilteredElementCollector rcollector = new FilteredElementCollector(doc, doc.ActiveView.Id).WhereElementIsNotElementType();
IList<Element> rebsonview = rcollector.OfCategory(BuiltInCategory.OST_Rebar).ToElements();
Options opt = new Options()
{
ComputeReferences = true,
View = doc.ActiveView,
IncludeNonVisibleObjects = true
};
foreach (Element element in rebsonview)
{
if (element is Rebar rebar)
{
var rebarCenterCurve = rebar.GetCenterlineCurves(false, false, false, MultiplanarOption.IncludeOnlyPlanarCurves,0).FirstOrDefault();
if (rebarCenterCurve != null && rebarCenterCurve is Line rebarCenterLine &&
(rebarCenterLine.Direction.IsAlmostEqualTo(doc.ActiveView.UpDirection.Normalize()) ||
rebarCenterLine.Direction.IsAlmostEqualTo(doc.ActiveView.UpDirection.Negate().Normalize())))
{
ReferenceArray refArr = new ReferenceArray();
foreach (GeometryObject geometryObject in rebar.get_Geometry(opt))
{
if (geometryObject is Line line)
{
if (rebarCenterLine.Intersect(line, out var intersectionResult) == SetComparisonResult.Overlap &&
intersectionResult.Size == 1)
{
var intersectPt = intersectionResult.get_Item(0).XYZPoint;
if (intersectPt.IsAlmostEqualTo(rebarCenterLine.GetEndPoint(0)) ||
intersectPt.IsAlmostEqualTo(rebarCenterLine.GetEndPoint(1)))
{
refArr.Append(line.Reference);
}
}
}
}
if (refArr.Size >= 2)
{
XYZ dimmPoint = rebarCenterLine.GetEndPoint(0).Add(doc.ActiveView.RightDirection.Negate().Multiply(5));
Line dimmLine = Line.CreateUnbound(dimmPoint, rebarCenterLine.Direction);
using (Transaction tr = new Transaction(doc, "Dimm"))
{
tr.Start();
doc.Create.NewDimension(doc.ActiveView, dimmLine, refArr);
tr.Commit();
}
}
}
}
}
return Result.Succeeded;
}