明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1420|回复: 2

[基础] C# 选择集基础实例

[复制链接]
发表于 2013-7-31 17:31 | 显示全部楼层 |阅读模式
是一个简单的选择集基础实例,希望对新手有帮助,高手勿喷
然后还有一个问题:怎样才能做出 框选 的效果? (像AutoCAD的框选效果)

            
  1. Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
  2.             Database db = HostApplicationServices.WorkingDatabase;
  3.             DocumentLock dlock = Application.DocumentManager.MdiActiveDocument.LockDocument();

  4.             using (Transaction tran = db.TransactionManager.StartTransaction())
  5.             {
  6.                 SelectionSet selSet;
  7.                 //输入一种选择集过滤的实体类型   如  circle   line text
  8.                 PromptStringOptions stringOpts = new PromptStringOptions("\n请输入实体类型:");
  9.                 PromptResult stringResult = ed.GetString(stringOpts);
  10.                 if (stringResult.Status != PromptStatus.OK) return;

  11.                 //输入结束后,将其加入选择集筛选条件
  12.                 TypedValue[] typeSet = new TypedValue[1];
  13.                 typeSet[0] = new TypedValue((int)DxfCode.Start, stringResult.StringResult);
  14.                 SelectionFilter sFilter = new SelectionFilter(typeSet);
  15.                 //①用户选择其实体   (选择集方式一)
  16.                 //PromptSelectionResult selectResult = ed.GetSelection(sFilter);

  17.                 //②用户选择区域        (选择集方式二)
  18.                 Point3d onePnt,otherPnt;
  19.                 PromptPointOptions pntOpts = new PromptPointOptions("\n请选择起始点:");
  20.                 PromptPointResult pntResult = ed.GetPoint(pntOpts);
  21.                 if (pntResult.Status != PromptStatus.OK) return;
  22.                 onePnt = pntResult.Value;

  23.                 pntOpts = new PromptPointOptions("\n请选择结束点:");
  24.                 pntOpts.BasePoint = onePnt;
  25.                 //是否虚线显示
  26.                 //pntOpts.UseDashedLine = true;
  27.                 pntOpts.UseBasePoint = true;

  28.                 pntResult = ed.GetPoint(pntOpts);
  29.                 if (pntResult.Status != PromptStatus.OK) return;
  30.                 otherPnt = pntResult.Value;
  31.                
  32.                 //多种选择方法
  33.                 //PromptSelectionResult selectResult = ed.SelectCrossingWindow(onePnt, otherPnt, sFilter);
  34.                 PromptSelectionResult selectResult = ed.SelectWindow(onePnt, otherPnt, sFilter);

  35.                 if (selectResult.Status != PromptStatus.OK) return;
  36.                 selSet = selectResult.Value;
  37.                 ed.WriteMessage(selSet.Count.ToString());
  38.             }

  39.             dlock.Dispose();


 楼主| 发表于 2013-8-1 08:39 | 显示全部楼层
找到了另外一种方法来实现了。

站内相关示例帖子:
http://bbs.mjtd.com/forum.php?mod=viewthread&tid=84859
http://www.objectarx.net/forum.php?mod=viewthread&tid=5811

  1. Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
  2. Database db = HostApplicationServices.WorkingDatabase;
  3. DocumentLock dlock = Application.DocumentManager.MdiActiveDocument.LockDocument();

  4. using (Transaction tran = db.TransactionManager.StartTransaction())
  5. {
  6.     SelectionSet selSet;
  7.     //输入一种选择集过滤的实体类型   如  circle   line text
  8.     PromptStringOptions stringOpts = new PromptStringOptions("\n请输入实体类型:");
  9.     PromptResult stringResult = ed.GetString(stringOpts);
  10.     if (stringResult.Status != PromptStatus.OK) return;

  11.     //输入结束后,将其加入选择集筛选条件
  12.     TypedValue[] typeSet = new TypedValue[1];
  13.     typeSet[0] = new TypedValue((int)DxfCode.Start, stringResult.StringResult);
  14.     SelectionFilter sFilter = new SelectionFilter(typeSet);
  15.     //用户选择区域
  16.     PromptSelectionOptions selectOpts = new PromptSelectionOptions();
  17.     PromptSelectionResult selectResult = ed.GetSelection(selectOpts,sFilter);

  18.     if (selectResult.Status != PromptStatus.OK) return;
  19.     selSet = selectResult.Value;

  20.     //得到该用户选择区域的左下角 和 右上角
  21.     CrossingOrWindowSelectedObject o = (CrossingOrWindowSelectedObject)selSet[0];
  22.     PickPointDescriptor[] p = o.GetPickPoints();
  23.     Point2d minPoint = new Point2d(p[3].PointOnLine.X, p[3].PointOnLine.Y);
  24.     Point2d maxPoint = new Point2d(p[1].PointOnLine.X, p[1].PointOnLine.Y);

  25.     ed.WriteMessage("\n" + minPoint.ToString());
  26.     ed.WriteMessage("\n" + maxPoint.ToString());
  27. }

  28. dlock.Dispose();

发表于 2013-8-23 09:53 | 显示全部楼层
呵呵,比较基础。。。。。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-5-17 10:21 , Processed in 0.146039 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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