创建 Polyline 对象

创建 Polyline 对象

 

本实例是在模型空间里创建一条包含两段的多段线对象(包含三个点) 


 

添加AutoCAD的命名空间

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;

创建多段线对象的示例源码:

 

[CommandMethod(“AddLightweightPolyline”)]

public static void AddLightweightPolyline()

{

  //获取当前图形文档和数据库

  Document acDoc = Application.DocumentManager.MdiActiveDocument;

  Database acCurDb = acDoc.Database;

 

  //开始一个事务

  using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())

  {

      // 以只读的方式打开块表

      BlockTable acBlkTbl;

      acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,

                                   OpenMode.ForRead) as BlockTable;

 

      //以写的方式打开块记录

      BlockTableRecord acBlkTblRec;

      acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],

                                      OpenMode.ForWrite) as BlockTableRecord;

 

      //创建两段的多段线对象(3个点)

      Polyline acPoly = new Polyline();

      acPoly.SetDatabaseDefaults();

      acPoly.AddVertexAt(0, new Point2d(2, 4), 0, 0, 0);

      acPoly.AddVertexAt(1, new Point2d(4, 2), 0, 0, 0);

      acPoly.AddVertexAt(2, new Point2d(6, 4), 0, 0, 0);

 

      //把多段线对象添加到块记录和事务

      acBlkTblRec.AppendEntity(acPoly);

      acTrans.AddNewlyCreatedDBObject(acPoly, true);

 

      //提交事务,把点对象保存到数据库中

      acTrans.Commit();

  }

}

 

 

 


 

转载自:https://blog.csdn.net/lzx838/article/details/5346335

You may also like...