BIM World
A Professional BIM Learning Platform


Revit API Tutorial: How to Read a Single Line from a CAD File

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;
    }
}
xuebim
Follow the latest BIM developments in the architecture industry, explore innovative building technologies, and discover cutting-edge industry insights.
← Scan with WeChat
Like(0) 打赏
BIM WORLD » Revit API Tutorial: How to Read a Single Line from a CAD File

Comment Get first!

Must log in before commenting!

 

BIM World, A Professional BIM Learning Platform

Stay updated on the latest architecture trends and share new building technologies.

Contact UsAbout Us

觉得文章有用就打赏一下小编吧

非常感谢你的打赏,我们将继续提供更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫

微信扫一扫

Account Login

By signing in, you agree toUser Agreement

Sign Up