7.兮♂贝 发表于 2013-7-26 16:46:18

C# 添加右键菜单实例

本帖最后由 7.兮♂贝 于 2013-7-26 17:17 编辑

遇到一个问题,估计以后也会经常遇到,所以想问问各位大虾,这个问题出在哪里。详细代码我会贴到2楼。

整个功能是通过自定义CAD操作命令,完成鼠标右键的菜单添加,同时点击该菜单,将执行画一条直线的动作。

菜单添加成功,执行画一条直线的代码也成功。但是将两者联合起来,就会报 ”eLockViolation”。

拜托大侠


知识点:

加入到应用程序级的右键菜单中
Application.AddDefaultContextMenuExtension
加入到某一种对象的右键菜单中,比如圆、直线等
Application.AddObjectContextMenuExtension

7.兮♂贝 发表于 2013-7-26 16:46:49

本帖最后由 7.兮♂贝 于 2013-7-26 17:20 编辑

//右键菜单
      
      public void AddContextMenu()
      {
                //右键菜单 对象
                ContextMenuExtension m_ContextMenu = new ContextMenuExtension();
                m_ContextMenu.Title = "ContextMenu Sample";
                //右键菜单项及其事件
                MenuItem MenuItem_1 = new MenuItem("Create Line");
                MenuItem_1.Click += new EventHandler(MenuItem_1_Click);

                //菜单项添加到右键菜单
                m_ContextMenu.MenuItems.Add(MenuItem_1);

                //加入到应用程序级的右键菜单中
                Application.AddDefaultContextMenuExtension(m_ContextMenu);

                //加入到某一种对象的右键菜单中
                //Application.AddObjectContextMenuExtension(RXClass.GetClass(System.Type.GetType("Circle")), m_ContextMenu);
      }
      private void MenuItem_1_Click(object sender, System.EventArgs e)
      {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = HostApplicationServices.WorkingDatabase;
            Editor ed = doc.Editor;

            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                PromptPointOptions pointOpts,otherOpts;
                PromptPointResult pointResult,otherResult;
                Point3d firstPnt = new Point3d();
                Point3d OtherPnt = new Point3d();
                Line tLine;

                pointOpts = new PromptPointOptions("please select a point: ");
                pointResult = ed.GetPoint(pointOpts);
                if (pointResult.Status == PromptStatus.OK)
                  firstPnt = pointResult.Value;

                otherOpts = new PromptPointOptions("\n select other point: ");
                otherResult = ed.GetPoint(otherOpts);
                if (otherResult.Status == PromptStatus.OK)
                  OtherPnt = otherResult.Value;

                try
                {
                  //得到当前数据库块表
                  BlockTable bt = (BlockTable)(trans.GetObject(db.BlockTableId, OpenMode.ForRead));
                  BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt, OpenMode.ForWrite);
                  tLine = new Line(firstPnt, OtherPnt);
                  //往数据库中添加实体
                  btr.AppendEntity(tLine);
                  trans.AddNewlyCreatedDBObject(tLine, true);
                  trans.Commit();
                }
                catch(Autodesk.AutoCAD.Runtime.Exception g)
                {
                  Application.ShowAlertDialog(g.Message);
                  trans.Abort();
                }
            }
      }

sieben 发表于 2013-7-26 18:57:11

      DocumentLock dlock = Application.DocumentManager.MdiActiveDocument.LockDocument();
      try
      {
;;把你画直线的代码放在这里,或者说需要访问Database的代码放在这里
      }
      catch (System.Exception ex)
      {
      }
      finally
      {
      dlock.Dispose();
      }

xingang1005 发表于 2013-7-29 16:54:11

操作文档时要对文档进行锁定 ,DocumentLock dlock = Application.DocumentManager.MdiActiveDocument.LockDocument();
然后操作完了记得释放dlock.Dispose();

7.兮♂贝 发表于 2013-7-30 10:06:11

xingang1005 发表于 2013-7-29 16:54 static/image/common/back.gif
操作文档时要对文档进行锁定 ,DocumentLock dlock = Application.DocumentManager.MdiActiveDocument.Lock ...

应该是这个了... 感谢,赶紧去试试

7.兮♂贝 发表于 2013-7-30 10:21:38

sieben 发表于 2013-7-26 18:57 static/image/common/back.gif
DocumentLock dlock = Application.DocumentManager.MdiActiveDocument.LockDocument();
      try
...

问题解决了... 对数据库的操作都需要这样吗?为什么在不添加右键菜单的情况下,直接执行画直线的方法是可以实现的呢?


非常感谢!!!

sieben 发表于 2013-7-30 13:05:56

菜单的操作或对话框的操作会认为是在AutoCAD的外面,而一般情况下认为是在AutoCAD内部,两者有差异

xgr 发表于 2019-1-15 13:15:28

CAD2012
//加入到某一种对象的右键菜单中
Application.AddObjectContextMenuExtension(RXObject.GetClass(typeof(Circle)), m_ContextMenu);

箭头_Row 发表于 2024-9-12 14:13:38

問題來了,如何給右鍵添加一個快捷鍵,就像默認的那樣點右鍵后再按A就是平移,那麼自己添加的按鈕如何執行這個操作?看官方API此處好像麼開放。
页: [1]
查看完整版本: C# 添加右键菜单实例