C#之CAD二次开发(10) 用户交互之选择集
生活随笔
收集整理的這篇文章主要介紹了
C#之CAD二次开发(10) 用户交互之选择集
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
# 0. 前言
?
CAD中通過用戶交互來選擇對象,也可以通過.NET API模擬不同對象選擇選項(xiàng)。
當(dāng)執(zhí)行多個選擇集時,可以創(chuàng)建一個ObjectIdCollection對象來跟蹤已選擇的對象。
可以用如下的函數(shù)進(jìn)行選擇對象:
?
1. GetSelection() 用戶在圖形中選擇實(shí)體2. SelectAll() 選擇所有實(shí)體3. SelectCrossingWindow() 選擇窗口及和窗口四邊相交的實(shí)體4. SelectCrossingPolygon 選擇多邊形中及和多邊形相交的實(shí)體5. SelectFence 欄選6. SelectImplied 選擇當(dāng)前圖形中已經(jīng)選擇的實(shí)體7. SelectLast 選擇圖形中最后一盒繪制的實(shí)體8. SelectPrevious 選擇上一個選擇集9. SelectWindows 選擇窗口中的實(shí)體10. SelectWindowsPolygon 選擇多邊形中的實(shí)體11. SelectCrossingWindow 通過點(diǎn)坐標(biāo)選擇圖形?
# 1. 選擇集過濾
?
如果我們只需要選擇圖形中的部分文件就需要定義過濾規(guī)則
選擇過濾器有一對TypedValue參數(shù)構(gòu)成,TypedValue的第一個參數(shù)是過濾器的類型(例如 對象),第二個參數(shù)是需要過濾的值(例如圓)。
過濾器類型的一個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();}}} }?
原文請關(guān)注公眾號:數(shù)據(jù)智能筆記
?
說明一下:圖片為什么帶水印,我是從我的知乎轉(zhuǎn)載過來的,我要在知乎和公眾號一起發(fā)文,所以沒有多余時間再編輯一個平臺了,可以關(guān)注我的公眾號看原文!
總結(jié)
以上是生活随笔為你收集整理的C#之CAD二次开发(10) 用户交互之选择集的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【电源专题】电源纹波如何产生?测试电源纹
- 下一篇: 骨传导耳机原理,骨传导耳机优缺点