Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Dimension to Reference Lines from detail item instance

0 REPLIES 0
Reply
Message 1 of 1
atir5UTNF
80 Views, 0 Replies

Dimension to Reference Lines from detail item instance

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.

atir5UTNF_0-1704209626098.png

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);
      }
   }
}
0 REPLIES 0

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Forma Design Contest


Rail Community