Skip to content

Instantly share code, notes, and snippets.

@benkoshy
Created January 8, 2026 02:12
Show Gist options
  • Select an option

  • Save benkoshy/2b4c9be5a0d758ca1aa7701b848e3077 to your computer and use it in GitHub Desktop.

Select an option

Save benkoshy/2b4c9be5a0d758ca1aa7701b848e3077 to your computer and use it in GitHub Desktop.
autocad .net - matrix projection
// 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();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment