Today, a friend asked me how to read a single line from a CAD file using Revit. After searching online, I found no specific guides on this topic. Most resources discuss reading all lines or layers from a CAD file, but not individual lines. So, I decided to experiment on my own.
By using the PickObject method with the ObjectType.PointOnElement option, I was able to select a single line by picking a point on it. Then, I converted the selection into a geometry object using GetGeometryObjectFromReference. From there, you can extract and use any information you need. In this example, I’m reading the coordinates of the second endpoint of the selected line.
public class Class1 : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
Document revitDoc = commandData.Application.ActiveUIDocument.Document; // Get the document
Application revitApp = commandData.Application.Application; // Get the application
UIDocument uiDoc = commandData.Application.ActiveUIDocument;
Selection sel = uiDoc.Selection;
//Reference re = sel.PickObject(ObjectType.Element);
Reference re = sel.PickObject(ObjectType.PointOnElement);
ImportInstance dwg = revitDoc.GetElement(re) as ImportInstance;
var geoObj = (dwg as Element).GetGeometryObjectFromReference(re);
TaskDialog.Show("Revit", geoObj.GetType().ToString());
XYZ p1 = null;
XYZ p2 = null;
if (geoObj is Line)
{
Line line = geoObj as Line;
p1 = line.GetEndPoint(0);
p2 = line.GetEndPoint(1);
TaskDialog.Show("Revit", p2.X.ToString());
}
return Result.Succeeded;
}
}













Must log in before commenting!
Sign Up