6基于C#的CAD二次开发-对象选择(通过范围选择对象)
1 程序界面

2 主要程序段
所有程序段都经过调试,能正常使用。如有疑问,请自行调试后再使用。
2.1 选择所有实体
/// <summary>
/// 选择所有实体
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
dataGridView1.Rows.Clear();
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
PromptSelectionResult psr = ed.SelectAll();
if (psr.Status != PromptStatus.OK)
{
ed.WriteMessage("\n未选择任何对象。\n");
return;
}
SelectionSet sSet = psr.Value;
ObjectId[] selectedIds = sSet.GetObjectIds(); // 获取选择对象的ObjectiID数组.
using (Transaction tr = doc.Database.TransactionManager.StartTransaction())
{
foreach (ObjectId objectId1 in selectedIds)
{
//将获取的ObjectId转换为具体的实体Entity
Entity entity1 = tr.GetObject(objectId1, OpenMode.ForRead) as Entity;
//将实体Entity的有关信息显示在交互界面的表格
int index = dataGridView1.Rows.Add();
dataGridView1.Rows[index].Cells["对象类型"].Value = entity1.GetType().Name;
dataGridView1.Rows[index].Cells["ObjectId"].Value = entity1.ObjectId.ToString();
ObjectId a11 = tr.GetObject(objectId1, OpenMode.ForRead).OwnerId;
dataGridView1.Rows[index].Cells["ObjectId"].Value = tr.GetObject(a11, OpenMode.ForRead).GetType().Name;
}
ObjectId objectId6= ed.CurrentViewportObjectId;
Viewport entity6 = tr.GetObject(objectId6, OpenMode.ForRead) as Viewport;
MessageBox.Show(entity6.Width.ToString());
}
}
2.2 在图形中选择实体
/// <summary>
/// 在图形中选择实体
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
dataGridView1.Rows.Clear();
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
ed.WriteMessage("\n请框选对象\n");
PromptSelectionResult psr = ed.GetSelection();
if (psr.Status != PromptStatus.OK)
{
ed.WriteMessage("\n未选择任何对象。\n");
return;
}
SelectionSet sSet = psr.Value;
ObjectId[] selectedIds = sSet.GetObjectIds(); // 获取选择对象的ObjectiID数组.
using (Transaction tr = doc.Database.TransactionManager.StartTransaction())
{
foreach (ObjectId objectId1 in selectedIds)
{
//将获取的ObjectId转换为具体的实体Entity
Entity entity1 = tr.GetObject(objectId1, OpenMode.ForRead) as Entity;
//将实体Entity的有关信息显示在交互界面的表格
int index = dataGridView1.Rows.Add();
dataGridView1.Rows[index].Cells["对象类型"].Value = entity1.GetType().Name;
dataGridView1.Rows[index].Cells["ObjectId"].Value = entity1.ObjectId.ToString();
}
}
}
2.3 由选择多点组成一个多边形区域,选择多边形区域中及和多边形相交的实体
/// <summary>
/// 由选择多点组成一个多边形区域,选择多边形区域中及和多边形相交的实体
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
dataGridView1.Rows.Clear();
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
//通过选择点创建多边形,选择的点不能封闭,连成的线不能自相交
Point3dCollection pt3dCol = new Point3dCollection();
for (int i = 0; i < 5; i++)
{
PromptPointOptions pPtOpts = new PromptPointOptions("");
// 提示起点
pPtOpts.Message = "\n选择多边形第"+(i+1).ToString()+"点\n";
PromptPointResult psrP = ed.GetPoint(pPtOpts);
pt3dCol.Add(psrP.Value);
}
//通过多边形选择,选择范围为和该多边形相交以及该多边形内部的对象
PromptSelectionResult psr = ed.SelectCrossingPolygon(pt3dCol);
if (psr.Status != PromptStatus.OK)
{
ed.WriteMessage("\n未选择任何对象。\n");
return;
}
SelectionSet sSet = psr.Value;
ObjectId[] selectedIds = sSet.GetObjectIds(); // 获取选择对象的ObjectiID数组.
using (Transaction tr = doc.Database.TransactionManager.StartTransaction())
{
foreach (ObjectId objectId1 in selectedIds)
{
//将获取的ObjectId转换为具体的实体Entity
Entity entity1 = tr.GetObject(objectId1, OpenMode.ForRead) as Entity;
//将实体Entity的有关信息显示在交互界面的表格
int index = dataGridView1.Rows.Add();
dataGridView1.Rows[index].Cells["对象类型"].Value = entity1.GetType().Name;
dataGridView1.Rows[index].Cells["ObjectId"].Value = entity1.ObjectId.ToString();
}
}
}
2.4 通过两点连线为对角线构建一个四边形,选择该四边形的边界和四边形内的对象
/// <summary>
/// 通过两点连线为对角线构建一个四边形,选择该四边形的边界和四边形内的对象
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{
dataGridView1.Rows.Clear();
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
PromptPointOptions pPtOpts = new PromptPointOptions("");
// 提示起点
pPtOpts.Message = "\n选择第1点";
PromptPointResult psrP = ed.GetPoint(pPtOpts);
Point3d point3D1 = psrP.Value;
pPtOpts.Message = "\n选择第2点";
psrP = ed.GetPoint(pPtOpts);
Point3d point3D2 = psrP.Value;
PromptSelectionResult psr = ed.SelectCrossingWindow(point3D1, point3D2);
if (psr.Status != PromptStatus.OK)
{
ed.WriteMessage("\n未选择任何对象。\n");
return;
}
SelectionSet sSet = psr.Value;
ObjectId[] selectedIds = sSet.GetObjectIds(); // 获取选择对象的ObjectiID数组.
using (Transaction tr = doc.Database.TransactionManager.StartTransaction())
{
foreach (ObjectId objectId1 in selectedIds)
{
//将获取的ObjectId转换为具体的实体Entity
Entity entity1 = tr.GetObject(objectId1, OpenMode.ForRead) as Entity;
//将实体Entity的有关信息显示在交互界面的表格
int index = dataGridView1.Rows.Add();
dataGridView1.Rows[index].Cells["对象类型"].Value = entity1.GetType().Name;
dataGridView1.Rows[index].Cells["ObjectId"].Value = entity1.ObjectId.ToString();
}
}
}
2.5 栏选
/// <summary>
/// 栏选。和SelectCrossingPolygon不同。该方法由多点组成一条曲线,只选择和该曲线相交的对象。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button5_Click(object sender, EventArgs e)
{
dataGridView1.Rows.Clear();
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
//通过选择点创建多边形,选择的点不能封闭,连成的线不能自相交
Point3dCollection pt3dCol = new Point3dCollection();
for (int i = 0; i < 5; i++)
{
PromptPointOptions pPtOpts = new PromptPointOptions("");
// 提示起点
pPtOpts.Message = "\n选择多边形第" + (i + 1).ToString() + "点\n";
PromptPointResult psrP = ed.GetPoint(pPtOpts);
pt3dCol.Add(psrP.Value);
}
//通过多边形选择,选择范围为和该多边形相交以及该多边形内部的对象
PromptSelectionResult psr = ed.SelectFence(pt3dCol);
if (psr.Status != PromptStatus.OK)
{
ed.WriteMessage("\n未选择任何对象。\n");
return;
}
SelectionSet sSet = psr.Value;
ObjectId[] selectedIds = sSet.GetObjectIds(); // 获取选择对象的ObjectiID数组.
using (Transaction tr = doc.Database.TransactionManager.StartTransaction())
{
foreach (ObjectId objectId1 in selectedIds)
{
//将获取的ObjectId转换为具体的实体Entity
Entity entity1 = tr.GetObject(objectId1, OpenMode.ForRead) as Entity;
//将实体Entity的有关信息显示在交互界面的表格
int index = dataGridView1.Rows.Add();
dataGridView1.Rows[index].Cells["对象类型"].Value = entity1.GetType().Name;
dataGridView1.Rows[index].Cells["ObjectId"].Value = entity1.ObjectId.ToString();
}
}
}
2.6 选择当前图形中已经选择的实体
/// <summary>
/// 选择当前图形中已经选择的实体。意思是指手动在CAD界面中选择实体后,
/// 用该命令可以将手动选择的实体添加到程序的ObjectId数组中,以便程序对手动选择的实体进行后续操作。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button6_Click(object sender, EventArgs e)
{
dataGridView1.Rows.Clear();
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
PromptSelectionResult psr = ed.SelectImplied();
if (psr.Status != PromptStatus.OK)
{
ed.WriteMessage("\n未选择任何对象。\n");
return;
}
SelectionSet sSet = psr.Value;
ObjectId[] selectedIds = sSet.GetObjectIds(); // 获取选择对象的ObjectiID数组.
using (Transaction tr = doc.Database.TransactionManager.StartTransaction())
{
foreach (ObjectId objectId1 in selectedIds)
{
//将获取的ObjectId转换为具体的实体Entity
Entity entity1 = tr.GetObject(objectId1, OpenMode.ForRead) as Entity;
//将实体Entity的有关信息显示在交互界面的表格
int index = dataGridView1.Rows.Add();
dataGridView1.Rows[index].Cells["对象类型"].Value = entity1.GetType().Name;
dataGridView1.Rows[index].Cells["ObjectId"].Value = entity1.ObjectId.ToString();
}
}
}
2.7 选择图形中最后绘制的实体
/// <summary>
/// 选择图形中最后绘制的实体
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button7_Click(object sender, EventArgs e)
{
dataGridView1.Rows.Clear();
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
PromptSelectionResult psr = ed.SelectLast();
if (psr.Status != PromptStatus.OK)
{
ed.WriteMessage("\n未选择任何对象。\n");
return;
}
SelectionSet sSet = psr.Value;
ObjectId[] selectedIds = sSet.GetObjectIds(); // 获取选择对象的ObjectiID数组.
using (Transaction tr = doc.Database.TransactionManager.StartTransaction())
{
foreach (ObjectId objectId1 in selectedIds)
{
//将获取的ObjectId转换为具体的实体Entity
Entity entity1 = tr.GetObject(objectId1, OpenMode.ForRead) as Entity;
//将实体Entity的有关信息显示在交互界面的表格
int index = dataGridView1.Rows.Add();
dataGridView1.Rows[index].Cells["对象类型"].Value = entity1.GetType().Name;
dataGridView1.Rows[index].Cells["ObjectId"].Value = entity1.ObjectId.ToString();
}
}
}
2.8 选择上一个选择集
/// <summary>
/// 选择上一个选择集
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button8_Click(object sender, EventArgs e)
{
dataGridView1.Rows.Clear();
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
PromptSelectionResult psr = ed.SelectPrevious();
if (psr.Status != PromptStatus.OK)
{
ed.WriteMessage("\n未选择任何对象。\n");
return;
}
SelectionSet sSet = psr.Value;
ObjectId[] selectedIds = sSet.GetObjectIds(); // 获取选择对象的ObjectiID数组.
using (Transaction tr = doc.Database.TransactionManager.StartTransaction())
{
foreach (ObjectId objectId1 in selectedIds)
{
//将获取的ObjectId转换为具体的实体Entity
Entity entity1 = tr.GetObject(objectId1, OpenMode.ForRead) as Entity;
//将实体Entity的有关信息显示在交互界面的表格
int index = dataGridView1.Rows.Add();
dataGridView1.Rows[index].Cells["对象类型"].Value = entity1.GetType().Name;
dataGridView1.Rows[index].Cells["ObjectId"].Value = entity1.ObjectId.ToString();
}
}
}
2.9 在图形中选择实体
private void button9_Click(object sender, EventArgs e)
{
dataGridView1.Rows.Clear();
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
PromptPointOptions pPtOpts = new PromptPointOptions("");
// 提示起点
pPtOpts.Message = "\n选择第1点";
PromptPointResult psrP = ed.GetPoint(pPtOpts);
Point3d point3D1 = psrP.Value;
pPtOpts.Message = "\n选择第2点";
psrP = ed.GetPoint(pPtOpts);
Point3d point3D2 = psrP.Value;
PromptSelectionResult psr = ed.SelectWindow(point3D1, point3D2);
if (psr.Status != PromptStatus.OK)
{
ed.WriteMessage("\n未选择任何对象。\n");
return;
}
SelectionSet sSet = psr.Value;
ObjectId[] selectedIds = sSet.GetObjectIds(); // 获取选择对象的ObjectiID数组.
using (Transaction tr = doc.Database.TransactionManager.StartTransaction())
{
foreach (ObjectId objectId1 in selectedIds)
{
//将获取的ObjectId转换为具体的实体Entity
Entity entity1 = tr.GetObject(objectId1, OpenMode.ForRead) as Entity;
//将实体Entity的有关信息显示在交互界面的表格
int index = dataGridView1.Rows.Add();
dataGridView1.Rows[index].Cells["对象类型"].Value = entity1.GetType().Name;
dataGridView1.Rows[index].Cells["ObjectId"].Value = entity1.ObjectId.ToString();
}
}
}
3 编辑器类Editor
实体的选择主要在编辑器类Editor中进行。该类和文档相关,如用在内存中打开文件,且文件不显示的方式打开文档(即后台打开CAD文件),则该类不能在,须通过其他的方式进行对象的选择。
3.1 Editor类库基本信息
基本信息 | 基本信息内容 |
---|
类名称: | Editor |
类全称: | Autodesk.AutoCAD.EditorInput.Editor |
命名空间: | Autodesk.AutoCAD.EditorInput |
所属文件: | C:\Program Files\Autodesk\AutoCAD 2019\accoremgd.dll |
基类: | System.MarshalByRefObject |
3.2 Editor类库方法
方法名称 | 返回类型 | 是否公共函数 | 是否静态函数 |
---|
ApplyCurDwgLayerTableChanges() | Void | True | False |
Command(Object[] parameter) | Void | True | False |
CommandAsync(Object[] parameter) | CommandResult | True | False |
CreateObjRef(Type requestedType) | ObjRef | True | False |
DoPrompt(PromptOptions opt) | PromptResult | True | False |
Drag(PromptDragOptions options) | PromptPointResult | True | False |
Drag(SelectionSet selection,String message,DragCallback callback) | PromptPointResult | True | False |
Drag(Jig jig) | PromptResult | True | False |
DrawVector(Point3d from,Point3d to,Int32 color,Boolean drawHighlighted) | Void | True | False |
DrawVectors(ResultBuffer rb,Matrix3d transform) | Void | True | False |
Equals(Object obj) | Boolean | True | False |
GetAngle(PromptAngleOptions options) | PromptDoubleResult | True | False |
GetAngle(String message) | PromptDoubleResult | True | False |
GetCommandVersion() | Int32 | True | False |
GetCorner(PromptCornerOptions options) | PromptPointResult | True | False |
GetCorner(String message,Point3d basePoint) | PromptPointResult | True | False |
GetCurrentView() | ViewTableRecord | True | False |
GetDistance(PromptDistanceOptions options) | PromptDoubleResult | True | False |
GetDistance(String message) | PromptDoubleResult | True | False |
GetDouble(PromptDoubleOptions options) | PromptDoubleResult | True | False |
GetDouble(String message) | PromptDoubleResult | True | False |
GetEntity(PromptEntityOptions options) | PromptEntityResult | True | False |
GetEntity(String message) | PromptEntityResult | True | False |
GetFileNameForOpen(PromptOpenFileOptions options) | PromptFileNameResult | True | False |
GetFileNameForOpen(String message) | PromptFileNameResult | True | False |
GetFileNameForSave(PromptSaveFileOptions options) | PromptFileNameResult | True | False |
GetFileNameForSave(String message) | PromptFileNameResult | True | False |
GetHashCode() | Int32 | True | False |
GetInteger(PromptIntegerOptions options) | PromptIntegerResult | True | False |
GetInteger(String message) | PromptIntegerResult | True | False |
GetKeywords(PromptKeywordOptions options) | PromptResult | True | False |
GetKeywords(String message,String[] globalKeywords) | PromptResult | True | False |
GetLifetimeService() | Object | True | False |
GetNestedEntity(PromptNestedEntityOptions options) | PromptNestedEntityResult | True | False |
GetNestedEntity(String message) | PromptNestedEntityResult | True | False |
GetPoint(PromptPointOptions options) | PromptPointResult | True | False |
GetPoint(String message) | PromptPointResult | True | False |
GetSelection(PromptSelectionOptions options,SelectionFilter filter) | PromptSelectionResult | True | False |
GetSelection(PromptSelectionOptions options) | PromptSelectionResult | True | False |
GetSelection(SelectionFilter filter) | PromptSelectionResult | True | False |
GetSelection() | PromptSelectionResult | True | False |
GetString(PromptStringOptions options) | PromptResult | True | False |
GetString(String message) | PromptResult | True | False |
GetType() | Type | True | False |
GetViewportNumber(Point point) | Int32 | True | False |
InitCommandVersion(Int32 nVersion) | Int32 | True | False |
InitializeLifetimeService() | Object | True | False |
PointToScreen(Point3d pt,Int32 viewportNumber) | Point | True | False |
PointToWorld(Point pt,Int32 viewportNumber) | Point3d | True | False |
PointToWorld(Point pt) | Point3d | True | False |
PostCommandPrompt() | Void | True | False |
Regen() | Void | True | False |
SelectAll(SelectionFilter filter) | PromptSelectionResult | True | False |
SelectAll() | PromptSelectionResult | True | False |
SelectCrossingPolygon(Point3dCollection polygon,SelectionFilter filter) | PromptSelectionResult | True | False |
SelectCrossingPolygon(Point3dCollection polygon) | PromptSelectionResult | True | False |
SelectCrossingWindow(Point3d pt1,Point3d pt2,SelectionFilter filter,Boolean forceSubEntitySelection) | PromptSelectionResult | True | False |
SelectCrossingWindow(Point3d pt1,Point3d pt2,SelectionFilter filter) | PromptSelectionResult | True | False |
SelectCrossingWindow(Point3d pt1,Point3d pt2) | PromptSelectionResult | True | False |
SelectFence(Point3dCollection fence,SelectionFilter filter) | PromptSelectionResult | True | False |
SelectFence(Point3dCollection fence) | PromptSelectionResult | True | False |
SelectImplied() | PromptSelectionResult | True | False |
SelectLast() | PromptSelectionResult | True | False |
SelectPrevious() | PromptSelectionResult | True | False |
SelectWindow(Point3d pt1,Point3d pt2,SelectionFilter filter) | PromptSelectionResult | True | False |
SelectWindow(Point3d pt1,Point3d pt2) | PromptSelectionResult | True | False |
SelectWindowPolygon(Point3dCollection polygon,SelectionFilter filter) | PromptSelectionResult | True | False |
SelectWindowPolygon(Point3dCollection polygon) | PromptSelectionResult | True | False |
SetCurrentView(ViewTableRecord value) | Void | True | False |
SetImpliedSelection(SelectionSet selectionSet) | Void | True | False |
SetImpliedSelection(ObjectId[] selectedObjects) | Void | True | False |
Snap(String snapMode,Point3d input) | Point3d | True | False |
StartUserInteraction(IntPtr hwnd) | EditorUserInteraction | True | False |
StartUserInteraction(Window window) | EditorUserInteraction | True | False |
SwitchToModelSpace() | Void | True | False |
SwitchToPaperSpace() | Void | True | False |
ToString() | String | True | False |
TraceBoundary(Point3d seedPoint,Boolean detectIslands) | DBObjectCollection | True | False |
TurnForcedPickOff() | Int32 | True | False |
TurnForcedPickOn() | Int32 | True | False |
TurnSubentityWindowSelectionOff() | Void | True | False |
TurnSubentityWindowSelectionOn() | Void | True | False |
UpdateScreen() | Void | True | False |
UpdateTiledViewportsFromDatabase() | Void | True | False |
UpdateTiledViewportsInDatabase() | Void | True | False |
ViewportIdFromNumber(Int32 viewportNumber) | ObjectId | True | False |
WriteMessage(String message,Object[] parameter) | Void | True | False |
WriteMessage(String message) | Void | True | False |
3.3 Editor类库属性
属性名称 | 属性类型 | 是否可写 |
---|
ActiveViewportId | Autodesk.AutoCAD.DatabaseServices.ObjectId | False |
CurrentUserCoordinateSystem | Autodesk.AutoCAD.Geometry.Matrix3d | True |
CurrentViewportObjectId | Autodesk.AutoCAD.DatabaseServices.ObjectId | False |
Document | Autodesk.AutoCAD.ApplicationServices.Document | False |
IsDragging | System.Boolean | False |
IsQuiescent | System.Boolean | False |
IsQuiescentForTransparentCommand | System.Boolean | False |
MouseHasMoved | System.Boolean | False |
UseCommandLineInterface | System.Boolean | False |