赞
踩
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 通过点坐标选择图形
如果我们只需要选择图形中的部分文件就需要定义过滤规则
选择过滤器有一对TypedValue参数构成,TypedValue的第一个参数是过滤器的类型(例如 对象),第二个参数是需要过滤的值(例如圆)。
过滤器类型的一个DXF组码,用于指定使用何种过滤器
常用过滤器类型列表:
例如只选取图形中的圆形:
- 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();
- }
- }
- }
- }
原文请关注公众号:数据智能笔记
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。