如何从.NET创建简单的AutoCad 2017维度?

时间:2017-01-27 20:34:16

标签: .net autocad

我开始使用.NET进行AutoCAD 2017开发,并使用下一个代码创建了一个基本矩形:

[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();
    }
}

现在,我需要在一侧添加一个Dimension(让我们说)来显示高度。 如何从.NET完成这项工作? 谢谢!

1 个答案:

答案 0 :(得分:1)

由于您的命令未使用Session命令标志注册,因此您无需锁定当前文档,但如果这样做,则必须显式处置返回的DocumentLock实例(请参阅this thread )。

以下是创建维度的示例:

    [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();
        }
    }