hact 发表于 2010-12-28 13:24:28

【求助】关于命令执行与实体选择的先后顺序

编写了个小程序,将选择的Mtext内容复制到粘贴板上,这样可以直接复制到excel中
由于这个操作在日常工作中很多,因此想尽可能减少操作的步骤(按回车,点鼠标等等)

实际操作了一遍,觉得比较麻烦
1.运行命令
2.选择Mtext
3.点右键确认
4.切换到excel,进行Ctrl+V

关于前3点,如何做改进为
1.选择Mtext
2.运行命令/或快捷方式

这样前后2中方式都能使用了。

就像移动命令一下
1.选择直线
2.输入m,回车/点右键
3.移动
或者
1.输入m,回车
2.选择直线,点右键
3.移动

另:发现一个现在,同样的移动命令若是点工具栏上的按钮时,只要
1.点按钮
2。选直线,确认
3.移动

1.选直线
2.点按钮
3.移动

hact 发表于 2010-12-28 13:26:20

代码如下struct text
      {
            public double position;
            public String content;
      }

      
      public void ttt()
      {
            Database db = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
            Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
            Transaction tran=db.TransactionManager.StartTransaction();
            Entity entity = null;
            MText mt=new MText();
            DBText dt = new DBText();
            String cs = "";

            List<text> ltext = new List<text>();
            
            PromptSelectionOptions selops = new PromptSelectionOptions(); // 建立选择的过滤器内容

            TypedValue[] filList = new TypedValue;
            filList = new TypedValue((int)DxfCode.Operator, "<or");
            filList = new TypedValue((int)DxfCode.Start, "Mtext");
            filList = new TypedValue((int)DxfCode.Start, "text");
            filList = new TypedValue((int)DxfCode.Operator, "or>");

            SelectionFilter filter = new SelectionFilter(filList);
            PromptSelectionResult ents = ed.GetSelection(selops,filter);
            if (ents.Status == PromptStatus.OK)
            {
                SelectionSet SS = ents.Value;
                foreach (ObjectId id in SS.GetObjectIds())
                {
                  entity = (Entity)tran.GetObject(id, OpenMode.ForRead);
                  if (entity.GetType() == typeof(MText))
                  {
                        mt = (MText)entity;
                        ltext.Add(new text() { position = mt.Location.Y, content = mt.Contents.ToString().Replace("\\P"," ")});
                  }
                  if (entity.GetType() == typeof(DBText))
                  {
                        dt = (DBText)entity;
                        ltext.Add(new text() { position = dt.Position.Y, content = dt.TextString });
                  }
                }
            }
            ltext.Sort(new Comparison<text>((t1, t2) => t1.position > t2.position ? 1 : 0));
            //ltext.Sort(n =>n.position);
            foreach (text t in ltext)
            {
                cs=cs+t.content+"\n";
            }
            cs = cs.TrimEnd('\n');
            Clipboard.SetDataObject(cs);
            tran.Commit();
            tran.Dispose();
      }

hmxmylove 发表于 2010-12-28 13:29:58

使用PickFirst,可以实现先选择后命令。

hact 发表于 2010-12-28 13:49:37

没用过……

雪山飞狐_lzh 发表于 2010-12-28 14:05:16

      
      public static void Test2()
      {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            //获取用户选择
            var resSel = ed.SelectImplied();
            //判断是否有选择
            if (resSel.Status == PromptStatus.OK)
            {
                //查找是否有文本,有则继续

            }
            else
            {
                //没有选择就要求用户选择
                resSel = ed.GetSelection();
            }

      }

雪山飞狐_lzh 发表于 2010-12-28 14:16:45

排序的部分用Linq写的要简单点            foreach (text t in ltext.OrderBy(t => t.position))
            {
                cs = cs + t.content + "\n";
            }

hact 发表于 2010-12-28 16:44:35

版主不愧是版主啊,搞定了,谢谢!
页: [1]
查看完整版本: 【求助】关于命令执行与实体选择的先后顺序