2017-01-27 55 views
0

我在AutoCAD 2017年開始開發具有.NET和我創建了下一個代碼基本矩形:如何從.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(); 
    } 
} 

現在,我需要追加在(比方說)一個維度中的一個側面表現出高度。 這怎麼可以從.NET完成? 謝謝!

回答

1

由於您的命令未使用會話命令標誌進行註冊,因此不需要鎖定當前文檔,但是如果這樣做,則必須顯式處理返回的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(); 
     } 
    } 
+0

非常感謝!但是,如果鎖被刪除,則會引發eLockViolation,儘管該命令未註冊爲會話命令標誌。 –

相關問題