明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1635|回复: 6

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

[复制链接]
发表于 2010-12-28 13:24 | 显示全部楼层 |阅读模式
编写了个小程序,将选择的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.移动

 楼主| 发表于 2010-12-28 13:26 | 显示全部楼层
代码如下
  1. struct text
  2.         {
  3.             public double position;
  4.             public String content;
  5.         }

  6.         [CommandMethod("TTT")]
  7.         public void ttt()
  8.         {
  9.             Database db = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
  10.             Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
  11.             Transaction tran=db.TransactionManager.StartTransaction();
  12.             Entity entity = null;
  13.             MText mt=new MText();
  14.             DBText dt = new DBText();
  15.             String cs = "";

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

  19.             TypedValue[] filList = new TypedValue[4];
  20.             filList[0] = new TypedValue((int)DxfCode.Operator, "<or");
  21.             filList[1] = new TypedValue((int)DxfCode.Start, "Mtext");
  22.             filList[2] = new TypedValue((int)DxfCode.Start, "text");
  23.             filList[3] = new TypedValue((int)DxfCode.Operator, "or>");

  24.             SelectionFilter filter = new SelectionFilter(filList);
  25.             PromptSelectionResult ents = ed.GetSelection(selops,filter);
  26.             if (ents.Status == PromptStatus.OK)
  27.             {
  28.                 SelectionSet SS = ents.Value;
  29.                 foreach (ObjectId id in SS.GetObjectIds())
  30.                 {
  31.                     entity = (Entity)tran.GetObject(id, OpenMode.ForRead);
  32.                     if (entity.GetType() == typeof(MText))
  33.                     {
  34.                         mt = (MText)entity;
  35.                         ltext.Add(new text() { position = mt.Location.Y, content = mt.Contents.ToString().Replace("\\P"," ")});
  36.                     }
  37.                     if (entity.GetType() == typeof(DBText))
  38.                     {
  39.                         dt = (DBText)entity;
  40.                         ltext.Add(new text() { position = dt.Position.Y, content = dt.TextString });
  41.                     }
  42.                 }
  43.             }
  44.             ltext.Sort(new Comparison<text>((t1, t2) => t1.position > t2.position ? 1 : 0));
  45.             //ltext.Sort(n =>n.position);
  46.             foreach (text t in ltext)
  47.             {
  48.                 cs=cs+t.content+"\n";
  49.             }
  50.             cs = cs.TrimEnd('\n');
  51.             Clipboard.SetDataObject(cs);
  52.             tran.Commit();
  53.             tran.Dispose();
  54.         }
复制代码
发表于 2010-12-28 13:29 | 显示全部楼层
使用PickFirst,可以实现先选择后命令。
 楼主| 发表于 2010-12-28 13:49 | 显示全部楼层
没用过……
发表于 2010-12-28 14:05 | 显示全部楼层
  1.         [CommandMethod("t2", CommandFlags.UsePickSet)]
  2.         public static void Test2()
  3.         {
  4.             Document doc = Application.DocumentManager.MdiActiveDocument;
  5.             Database db = doc.Database;
  6.             Editor ed = doc.Editor;

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

  13.             }
  14.             else
  15.             {
  16.                 //没有选择就要求用户选择
  17.                 resSel = ed.GetSelection();
  18.             }

  19.         }
复制代码
发表于 2010-12-28 14:16 | 显示全部楼层
排序的部分用Linq写的要简单点
  1.             foreach (text t in ltext.OrderBy(t => t.position))
  2.             {
  3.                 cs = cs + t.content + "\n";
  4.             }
复制代码
 楼主| 发表于 2010-12-28 16:44 | 显示全部楼层
版主不愧是版主啊,搞定了,谢谢!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-5-8 23:45 , Processed in 0.323808 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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