C#之CAD二次开发(10) 用户交互之选择集
生活随笔
收集整理的这篇文章主要介绍了
C#之CAD二次开发(10) 用户交互之选择集
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
# 0. 前言
CAD中通过用户交互来选择对象,也可以通过.NET API模拟不同对象选择选项。
当执行多个选择集时,可以创建一个ObjectIdCollection对象来跟踪已选择的对象。
可以用如下的函数进行选择对象:
1. GetSelection() 用户在图形中选择实体2. SelectAll() 选择所有实体3. SelectCrossingWindow() 选择窗口及和窗口四边相交的实体4. SelectCrossingPolygon 选择多边形中及和多边形相交的实体5. SelectFence 栏选6. SelectImplied 选择当前图形中已经选择的实体7. SelectLast 选择图形中最后一盒绘制的实体8. SelectPrevious 选择上一个选择集9. SelectWindows 选择窗口中的实体10. SelectWindowsPolygon 选择多边形中的实体11. SelectCrossingWindow 通过点坐标选择图形
# 1. 选择集过滤
如果我们只需要选择图形中的部分文件就需要定义过滤规则
选择过滤器有一对TypedValue参数构成,TypedValue的第一个参数是过滤器的类型(例如 对象),第二个参数是需要过滤的值(例如圆)。
过滤器类型的一个DXF组码,用于指定使用何种过滤器
常用过滤器类型列表:
例如只选取图形中的圆形:
# 2. 示例代码
using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.Runtime; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace _05_选择集 {public class Class1{[CommandMethod("SeleDemo")]public void SeleDemo(){Database db = HostApplicationServices.WorkingDatabase;Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;//PromptSelectionResult psr = ed.SelectAll(); // 选择窗口中所有图形// 只选择窗口中的圆形TypedValue[] values = new TypedValue[]{new TypedValue((int)DxfCode.Start,"Circle")};SelectionFilter filter = new SelectionFilter(values);// 过滤器//PromptSelectionResult psr = ed.GetSelection(filter); // 选取图形对象//if(psr.Status == PromptStatus.OK)//{// SelectionSet sSet = psr.Value;// this.ChangeColor(sSet);//}PromptSelectionResult psr = ed.GetSelection(filter);List<ObjectId> ids = new List<ObjectId>();if (psr.Status == PromptStatus.OK){SelectionSet sSet = psr.Value;List<Point3d> points = new List<Point3d>();points = this.GetPoint(sSet);for (int i = 0; i < points.Count; i++){PromptSelectionResult ss1 = ed.SelectCrossingWindow(points.ElementAt(i), points.ElementAt(i));ids.AddRange(ss1.Value.GetObjectIds());}}this.ChangeColor(ids);}private List<Point3d> GetPoint(SelectionSet sSet){List<Point3d> points = new List<Point3d>();ObjectId[] ids = sSet.GetObjectIds();Database db = HostApplicationServices.WorkingDatabase;using (Transaction trans = db.TransactionManager.StartTransaction()){for (int i = 0; i < ids.Length; i++){Entity ent = (Entity)ids[i].GetObject(OpenMode.ForRead);Point3d center = ((Circle)ent).Center;double radius = ((Circle)ent).Radius;points.Add(new Point3d(center.X + radius, center.Y, center.Z));}trans.Commit();}return points;}/// <summary>/// 改变颜色/// </summary>/// <param name="sSet">选取对象</param>private void ChangeColor(SelectionSet sSet){ObjectId[] ids = sSet.GetObjectIds();Database db = HostApplicationServices.WorkingDatabase;using (Transaction trans = db.TransactionManager.StartTransaction()){for (int i = 0; i < ids.Length; i++){Entity ent = (Entity)ids[i].GetObject(OpenMode.ForWrite);ent.ColorIndex = 1; // 红色}trans.Commit();}}private void ChangeColor(List<ObjectId> ids){Database db = HostApplicationServices.WorkingDatabase;using (Transaction trans = db.TransactionManager.StartTransaction()){for (int i = 0; i < ids.Count; i++){Entity ent = (Entity)ids[i].GetObject(OpenMode.ForWrite);ent.ColorIndex = 3; // 红色}trans.Commit();}}} }
原文请关注公众号:数据智能笔记
说明一下:图片为什么带水印,我是从我的知乎转载过来的,我要在知乎和公众号一起发文,所以没有多余时间再编辑一个平台了,可以关注我的公众号看原文!
总结
以上是生活随笔为你收集整理的C#之CAD二次开发(10) 用户交互之选择集的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 【电源专题】电源纹波如何产生?测试电源纹
- 下一篇: c#实现上位机数据采集的项目总结