autocad.net(C#)自定义的命令只能调用一次,将第一次画的直线删除后,再次调用画直线就不给画了,出现如下错误:eNullObjectId。
刚刚学习编程,比较菜,请高手指点一下。
下面是我写画直线的函数
public static ObjectId drawLine2pt(Point3d Pt1,Point3d Pt2,Color LineColor,ObjectId LayerId)
{
MyDoc doc = MyApp.DocumentManager.MdiActiveDocument;
MyEditor ed = doc.Editor;
Mydatabase db = doc.Database;
ObjectId obid = ObjectId.Null ;
using(Transaction trans=db.TransactionManager .StartTransaction())
{
try
{
BlockTable bt;
bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr;
btr = trans.GetObject(bt[BlockTableRecord.ModelSpace] , OpenMode.ForWrite) as BlockTableRecord;
Line ln = new Line(Pt1, Pt2);
ln.Color = LineColor;
ln.LayerId = LayerId;
obid = btr.AppendEntity(ln);
trans.AddNewlyCreatedDBObject(ln, true);
trans.Commit();
}
catch (System.Exception ex)
{
ed.WriteMessage("发生如下错误:" + ex.Message);
trans.Abort ();
}
}
return obid;
}