用autocad.net写一个插件,当添加对象时,获取对象的objectid
时间: 2023-05-19 13:06:11 浏览: 153
可以使用以下代码来实现:
```
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
namespace MyPlugin
{
public class MyCommands
{
[CommandMethod("AddObject")]
public void AddObject()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
// 添加对象
Circle circle = new Circle(new Point3d(0, 0, 0), Vector3d.ZAxis, 1);
btr.AppendEntity(circle);
tr.AddNewlyCreatedDBObject(circle, true);
// 获取对象的objectid
ObjectId objectId = circle.ObjectId;
tr.Commit();
}
}
}
}
```
当执行 `AddObject` 命令时,会在当前图形中添加一个圆形,并获取该圆形的 `ObjectId`。
阅读全文