调用cad2015命令的问题
本帖最后由 xgr 于 2019-6-4 08:51 编辑NET可以直接调用CAD2015的命令,但是在按钮的点击事件里调用cad2015命令却出现错误。
成功调用代码:
public static void Test()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptEntityOptions peo = new PromptEntityOptions("\nSelect a curve: ");
peo.SetRejectMessage("Only a curve.");
peo.AddAllowedClass(typeof(Curve), false);
PromptEntityResult per = ed.GetEntity(peo);
if (per.Status != PromptStatus.OK) return;
ObjectId curveId = per.ObjectId;
PromptPointResult ppr = ed.GetPoint("\nBreak point on curve:");
if (ppr.Status != PromptStatus.OK) return;
Point3d breakPoint = ppr.Value;
ed.Command("_.break", curveId, "_first", breakPoint, breakPoint);
using (Transaction tr = db.TransactionManager.StartTransaction())
{
ObjectId id = Autodesk.AutoCAD.Internal.Utils.EntLast();
Entity ent = (Entity) tr.GetObject(id, OpenMode.ForWrite);
ent.Color = Color.FromColorIndex(ColorMethod.ByAci, 1);
tr.Commit();
}
}
窗口一按钮点击事件,一样的代码
private void button2_Click(object sender, EventArgs e)
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptEntityOptions peo = new PromptEntityOptions("\nSelect a curve: ");
peo.SetRejectMessage("Only a curve.");
peo.AddAllowedClass(typeof(Curve), false);
PromptEntityResult per = ed.GetEntity(peo);
if (per.Status != PromptStatus.OK) return;
ObjectId curveId = per.ObjectId;
PromptPointResult ppr = ed.GetPoint("\nBreak point on curve:");
if (ppr.Status != PromptStatus.OK) return;
Point3d breakPoint = ppr.Value;
ed.Command("_.break", curveId, "_first", breakPoint, breakPoint);
using (Transaction tr = db.TransactionManager.StartTransaction())
{
ObjectId id = Autodesk.AutoCAD.Internal.Utils.EntLast();
Entity ent = (Entity)tr.GetObject(id, OpenMode.ForWrite);
ent.Color = Color.FromColorIndex(ColorMethod.ByAci, 1);
tr.Commit();
}
}
结果报错:
************* 异常文本 **************
Autodesk.AutoCAD.Runtime.Exception: eInvalidInput
在 Autodesk.AutoCAD.EditorInput.Editor.Command(Object[] parameter)
在开始事务处理前,将文档锁一下
Using Lock As DocumentLock = Doc.LockDocument
Using Trans As Transaction = Doc.TransactionManager.StartTransaction
End Using
End Using guohq 发表于 2019-6-4 09:11
在开始事务处理前,将文档锁一下
试过锁定文档,还是报同样的错误
页:
[1]