Now we have a user interface which allows us to click on items in a window and access them easily in AutoCAD. Written by Ben Koshy.

How to do some simple projections via AutoCAD’s .net API

Projections via AutoCAD’s .net API can be confusing. You need to specify a direction, and a plane, upon which you can project a point to. It can be confusing unless it’s clearly spelled out with an example: see below.:

// insert the usual references


Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;

using (Transaction tr = db.TransactionManager.StartTransaction())
{
    BlockTable blockTable = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
    BlockTableRecord modelSpace = tr.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

    // the original originalLine
    using (Line originalLine = new Line(Point3d.Origin, new Point3d(5, 5, 0)))
    {
        modelSpace.AppendEntity(originalLine);
        tr.AddNewlyCreatedDBObject(originalLine, true);

        // but we want to project it ONTO a plane.
        Plane plane = new Plane(Point3d.Origin,  new Vector3d(0,1,0));

        // project the originalLine onto a plane.
        Matrix3d projection = Matrix3d.Projection(plane, - Vector3d.YAxis);                            

        Line projectedLine = new Line(originalLine.StartPoint.TransformBy(projection), originalLine.EndPoint.Project(plane, -1 * Vector3d.YAxis));
        plane.Dispose();


        modelSpace.AppendEntity(projectedLine);
        tr.AddNewlyCreatedDBObject(projectedLine, true);                            
    }

    tr.Commit();
}

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *