I want to create dimension for the detail item you can see in the attached revit model.
The 2 reference lines are the longes in family.
I am not getting any excaption, but the dimension is not created.
void AddDim()
{
using (t = new Transaction(rvtDoc, "Add Dim"))
{
t.Start();
ViewPlan curView = rvtDoc.ActiveView as ViewPlan;
List<Reference> sel = commandData.Application.ActiveUIDocument.Selection.PickObjects(Autodesk.Revit.UI.Selection.ObjectType.Element);
Element element = rvtDoc.GetElement(sel[0]);
FamilyInstance defDim = element as FamilyInstance;
Transform transform = defDim.GetTotalTransform();
XYZ xAxis = transform.BasisX.Normalize();
XYZ yAxis = transform.BasisY.Normalize();
Options options = new Options();
options.ComputeReferences = true;
options.IncludeNonVisibleObjects = true;
options.View = curView;
GeometryElement geometryElement = defDim.get_Geometry(options);
List<Line> lines = new List<Line>();
GetReferenceLinesFromGeometryElement(geometryElement, ref lines);
List<Line> horLines = lines.Select(l => l).Where(l => l.Direction.IsAlmostEqualTo(xAxis) || l.Direction.IsAlmostEqualTo(xAxis.Negate())).ToList();
horLines = horLines.OrderByDescending(l => l.Length).ToList();
double locY1 = (horLines[0].Origin).DotProduct(yAxis);
double locY2 = (horLines[1].Origin).DotProduct(yAxis);
Reference top, bot;
if (locY1 > locY2)
{
top = horLines[0].Reference;
bot = horLines[1].Reference;
}
else
{
top = horLines[1].Reference;
bot = horLines[0].Reference;
}
ReferenceArray referenceArray = new ReferenceArray();
referenceArray.Append(bot);
referenceArray.Append(top);
Line d = Line.CreateBound(curView.Origin, curView.Origin + curView.UpDirection);
Dimension dim = rvtDoc.Create.NewDimension(curView, d, referenceArray);
t.Commit();
}
}
void GetReferenceLinesFromGeometryElement(GeometryElement geometryElement, ref List<Line> lines)
{
foreach (GeometryObject geoObj in geometryElement)
{
if (geoObj is Line)
{
Line line = (Line)geoObj;
if (line.Reference != null)
lines.Add(line);
}
else if (geoObj is GeometryInstance)
{
GeometryInstance instance = (GeometryInstance)geoObj;
GetReferenceLinesFromGeometryElement(instance.GetInstanceGeometry(),ref lines);
}
}
}