How to create a simple AutoCad 2017 dimension from .NET?

I'm starting in AutoCAD 2017 development with .NET and I created a basic rectangle with the next code:

[CommandMethod("AddSimpleRectangle")]
public static void AddSimpleRectangle()
{
    var acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
    acDoc.LockDocument();
    var acCurDb = acDoc.Database;
    var pr = acDoc.Editor.GetPoint("\nEnter insertion point: ");
    if (pr.Status != PromptStatus.OK)
       return;

    using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
    {
        var acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                                         OpenMode.ForRead) as BlockTable;

        var acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                                         OpenMode.ForWrite) as BlockTableRecord;

        using (var acPoly = new Polyline())
        {
            acPoly.AddVertexAt(0, new Point2d(pr.Value.X, pr.Value.Y), 0, 10, 10);
            acPoly.AddVertexAt(1, new Point2d(pr.Value.X + 2000, pr.Value.Y), 0, 10, 10);
            acPoly.AddVertexAt(2, new Point2d(pr.Value.X + 2000, pr.Value.Y + 500), 0, 10, 10);
            acPoly.AddVertexAt(3, new Point2d(pr.Value.X, pr.Value.Y + 500), 0, 10, 10);
            acPoly.Closed = true;
            acPoly.Color = Autodesk.AutoCAD.Colors.Color.FromRgb(0, 125, 255);

            acBlkTblRec.AppendEntity(acPoly);
            acTrans.AddNewlyCreatedDBObject(acPoly, true);
        }
        acTrans.Commit();
    }
}

Now, I need to append a Dimension at (let's say) one side to show the height. How can this be done from .NET? Thanks!

Upvotes: 0

Views: 1541

Answers (1)

gileCAD
gileCAD

Reputation: 2493

As your command isn't registered with the Session command flag, you do not need to lock the current document, but if you do so, you must explicitly dispose of the returned DocumentLock instance (see this thread).

Here's your example with the creation of a dimension:

    [CommandMethod("AddSimpleRectangle")]
    public static void AddSimpleRectangle()
    {
        var acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
        var acCurDb = acDoc.Database;
        var pr = acDoc.Editor.GetPoint("\nEnter insertion point: ");
        if (pr.Status != PromptStatus.OK)
            return;
        using (acDoc.LockDocument())
        using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
        {
            var acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                                             OpenMode.ForRead) as BlockTable;

            var acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                                             OpenMode.ForWrite) as BlockTableRecord;

            using (var acPoly = new Polyline())
            {
                acPoly.AddVertexAt(0, new Point2d(pr.Value.X, pr.Value.Y), 0, 10, 10);
                acPoly.AddVertexAt(1, new Point2d(pr.Value.X + 2000, pr.Value.Y), 0, 10, 10);
                acPoly.AddVertexAt(2, new Point2d(pr.Value.X + 2000, pr.Value.Y + 500), 0, 10, 10);
                acPoly.AddVertexAt(3, new Point2d(pr.Value.X, pr.Value.Y + 500), 0, 10, 10);
                acPoly.Closed = true;
                acPoly.Color = Autodesk.AutoCAD.Colors.Color.FromRgb(0, 125, 255);

                acBlkTblRec.AppendEntity(acPoly);
                acTrans.AddNewlyCreatedDBObject(acPoly, true);
            }

            using (var dim = new AlignedDimension(
                pr.Value,
                pr.Value + new Vector3d(0.0, 500.0, 0.0),
                pr.Value + new Vector3d(-100.0, 0.0, 0.0),
                string.Empty,
                acCurDb.Dimstyle))
            {
                acBlkTblRec.AppendEntity(dim);
                acTrans.AddNewlyCreatedDBObject(dim, true);
            }
            acTrans.Commit();
        }
    }

Upvotes: 1

Related Questions