need help for creating auto dimension setting between grid and duct/pipes

need help for creating auto dimension setting between grid and duct/pipes

esthervinitha
Explorer Explorer
485 Views
1 Reply
Message 1 of 2

need help for creating auto dimension setting between grid and duct/pipes

esthervinitha
Explorer
Explorer
I am developing a revit plugin for the first time in C# for auto dimension setting between grid and duct/pipes... I got an error in which I cannot convert Autodesk.Revit.DB.Curve to Autodesk.Revit.DB.XYZ 
 
The code below:
 
u
{ 
[Transaction(TransactionMode.Manual)] 
    public class Dim : IExternalCommand 
{               
  public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) 
      {               
  Document doc = commandData.Application.ActiveUIDocument.Document;           
            View activeView = doc.ActiveView;              
 FilteredElementCollector collector = new FilteredElementCollector(doc, activeView.Id);          
      var ductAndChwElements = collector.OfCategory(BuiltInCategory.OST_DuctCurves).UnionWith(collector.OfCategory(BuiltInCategory.OST_PipeCurves)).WhereElementIsNotElementType().ToElements();                 
 var coreWalls = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Walls).WhereElementIsNotElementType();                           coreWalls.Where(e => e.Name.ToLower().Contains("Core"));         
     var gridLines = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Grids).WhereElementIsNotElementType();      
       using (Transaction t = new Transaction(doc, "set out Dimensions"))               
   {                   
    t.Start();          
          foreach (Element element in ductAndChwElements)             
      {                      
       LocationCurve curve = element.Location as LocationCurve;   
                                                 if (curve != null) 
                             {                                
   Reference nearestRef = FindNearestReference(doc, curve.Curve, coreWalls, gridLines);    
      if (nearestRef != null)  
 {                                   
              XYZ elementPoint = curve.Curve.GetEndPoint(0);                              
    XYZ refPoint = nearestRef.GlobalPoint;                                  
   Line dimensionLine = Line.CreateBound(elementPoint, refPoint);                                  
      ReferenceArray ra = new ReferenceArray();                                    
 ra.Append(nearestRef);                                 
     ra.Append(new Reference(element));                                   
 doc.Create.NewDimension(activeView, dimensionLine, ra);                            
       }
          }                      
 }                     
  t.Commit();           
  }                
 return Result.Succeeded;          
  }          
 private Reference FindNearestReference(Document doc, Curve curve1,FilteredElementCollector coreWalls, FilteredElementCollector gridLines)       {                
 Reference nearestRef = null;             
 double minDistance = double.MaxValue;                
 foreach (Element Wall in coreWalls)          
   {                      
 LocationCurve wallCurve = Wall.Location as LocationCurve;                   
             if (wallCurve != null)      
              {                       
     double distance = curve1.Distance(wallCurve.Curve);                          
             if (distance < minDistance)                       
    {                                 
  nearestRef = new Reference(Wall);                               
  minDistance = distance;                        
 }                   
    }              
   }               
  foreach (Element grid in gridLines)          
   {                       
Curve gridCurve = (grid.Location as LocationCurve).Curve;                  
   if (gridCurve != null)                  
  {                         
 double distance = curve1.Distance(gridCurve);                            
  if (distance < minDistance)                          
 {                                
   nearestRef = new Reference(grid);                                 
minDistance = distance;                       
  }                      
 }                
 }                
 return nearestRef;          
  }          
0 Likes
Accepted solutions (1)
486 Views
1 Reply
Reply (1)
Message 2 of 2

jeremy_tammik
Alumni
Alumni
Accepted solution

Dear Esther, 

  

Thank you for raising the issue here after our email conversation. Your code would be more readable if you format it as code using the </> Insert/Edit code sample button above. Also, it is rather long. I do not see off-hand where the conversion might be that is causing an issue for you. Furthermore, there is a discrepancy between your title and the text; in the title, you talk about creating dimensioning, which is a pretty high-level question; the text only mentions a very low-level question on extracting a 3D XYZ point or vector from a curve.

  

To address the latter: you cannot convert a curve to an XYZ point, because they are different things. You could easily extract an infinite number of XYZ points or vectors from a curve, however. For instance, you could query the start, mid and endpoints, or any point or tangent vector along the curve.

  

I hope this helps clarify and move forward.

  

Best regards,

  

Jeremy

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open