明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 704|回复: 2

[命令] 调用cad2015命令的问题

[复制链接]
发表于 2019-6-4 08:49 | 显示全部楼层 |阅读模式
本帖最后由 xgr 于 2019-6-4 08:51 编辑

NET可以直接调用CAD2015的命令,但是在按钮的点击事件里调用cad2015命令却出现错误。
成功调用代码:
  1. [CommandMethod("Test")]
  2.         public static void Test()
  3.         {
  4.             Document doc = Application.DocumentManager.MdiActiveDocument;
  5.             Database db = doc.Database;
  6.             Editor ed = doc.Editor;
  7.             PromptEntityOptions peo = new PromptEntityOptions("\nSelect a curve: ");
  8.             peo.SetRejectMessage("Only a curve.");
  9.             peo.AddAllowedClass(typeof(Curve), false);
  10.             PromptEntityResult per = ed.GetEntity(peo);
  11.             if (per.Status != PromptStatus.OK) return;
  12.             ObjectId curveId = per.ObjectId;
  13.             PromptPointResult ppr = ed.GetPoint("\nBreak point on curve:");
  14.             if (ppr.Status != PromptStatus.OK) return;
  15.             Point3d breakPoint = ppr.Value;
  16.             ed.Command("_.break", curveId, "_first", breakPoint, breakPoint);
  17.             
  18.             using (Transaction tr = db.TransactionManager.StartTransaction())
  19.             {
  20.                 ObjectId id = Autodesk.AutoCAD.Internal.Utils.EntLast();
  21.                 Entity ent = (Entity) tr.GetObject(id, OpenMode.ForWrite);
  22.                 ent.Color = Color.FromColorIndex(ColorMethod.ByAci, 1);
  23.                 tr.Commit();
  24.             }
  25.         }

窗口一按钮点击事件,一样的代码
  1. private void button2_Click(object sender, EventArgs e)
  2.         {
  3.             Document doc = Application.DocumentManager.MdiActiveDocument;
  4.             Database db = doc.Database;
  5.             Editor ed = doc.Editor;
  6.             PromptEntityOptions peo = new PromptEntityOptions("\nSelect a curve: ");
  7.             peo.SetRejectMessage("Only a curve.");
  8.             peo.AddAllowedClass(typeof(Curve), false);
  9.             PromptEntityResult per = ed.GetEntity(peo);
  10.             if (per.Status != PromptStatus.OK) return;
  11.             ObjectId curveId = per.ObjectId;
  12.             PromptPointResult ppr = ed.GetPoint("\nBreak point on curve:");
  13.             if (ppr.Status != PromptStatus.OK) return;
  14.             Point3d breakPoint = ppr.Value;
  15.             ed.Command("_.break", curveId, "_first", breakPoint, breakPoint);

  16.             using (Transaction tr = db.TransactionManager.StartTransaction())
  17.             {
  18.                 ObjectId id = Autodesk.AutoCAD.Internal.Utils.EntLast();
  19.                 Entity ent = (Entity)tr.GetObject(id, OpenMode.ForWrite);
  20.                 ent.Color = Color.FromColorIndex(ColorMethod.ByAci, 1);
  21.                 tr.Commit();
  22.             }
  23.         }

结果报错:
************* 异常文本 **************
Autodesk.AutoCAD.Runtime.Exception: eInvalidInput
在 Autodesk.AutoCAD.EditorInput.Editor.Command(Object[] parameter)

发表于 2019-6-4 09:11 | 显示全部楼层
在开始事务处理前,将文档锁一下
  1. Using Lock As DocumentLock = Doc.LockDocument
  2.             Using Trans As Transaction = Doc.TransactionManager.StartTransaction

  3.             End Using
  4.         End Using
复制代码
 楼主| 发表于 2019-6-4 09:45 | 显示全部楼层
guohq 发表于 2019-6-4 09:11
在开始事务处理前,将文档锁一下

试过锁定文档,还是报同样的错误
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-5-7 05:29 , Processed in 0.380428 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表