在图形区域中选择对象
 
 

You can select objects through by having the user interactively select objects, or you can simulate many of the various object selection options through the .NET API. If your routine performs multiple selection sets, you will need to either track each selection set returned or create an ObjectIdCollection object to keep track of all the selected objects. The following functions allow you to select objects from the drawing:

用户选择对象可以通过与用户交互式选择对象来完成,或通过 .NET API 模拟多种选择选项来完成。如果你的程序完成了多个选择集,就需要跟踪每个返回的选择集或创建一个 ObjectIdCollection 对象以跟踪所有选择的对象。下面的函数允许用户从图形中选择对象:

GetSelection

Prompts the user to pick objects from the screen.

提示用户从屏幕中拾取对象。

SelectAll

Selects all objects in the current space in which are not locked or frozen.

在当前空间中选择所有对象,但不包括锁定或冻结的。

SelectCrossingPolygon

Selects objects within and crossing a polygon defined by specifying points. The polygon can be any shape but cannot cross or touch itself.

选择与通过指定点定义的多边形内部和相交的对象。多边形可以是任何形状但不能与它自己相交或接触。

SelectCrossingWindow

Selects objects within and crossing an area defined by two points.

选择与通过两个点定义的区域内部和相交的对象。

SelectFence

Selects all objects crossing a selection fence. Fence selection is similar to crossing polygon selection except that the fence is not closed, and a fence can cross itself.

选择与选择栅格相交的所有对象。栅格选择与多边形相交选择是相似的,不同的是栅格不能封闭,而且栅格可以与自己相交。

SelectLast

Selects the last object created in the current space.

选择当前空间中最后创建的对象。

SelectPrevious

Selects all objects selected during the previous Select objects: prompt.

选择在前一个选择对象提示时选择的所有对象。

SelectWindow

Selects all objects completely inside a rectangle defined by two points.

选择完全包含在通过两个点定义的矩形内部的所有对象。

SelectWindowPolygon

Selects objects completely inside a polygon defined by points. The polygon can be any shape but cannot cross or touch itself.

选择完全包含在通过点定义的多边形里面的对象。多边形可以是任何形状但不能与它自己相交或接触。

SelectAtPoint

Selects objects passing through a given point and places them into the active selection set.

选择经过给定点的对象并将它们放到活动选择集中去。

SelectByPolygon

Selects objects within a fence and adds them to the active selection set.

选择栅格内部的对象并将它们放到活动选择集中去。

在屏幕上提示选择对象并遍历选择集

This example prompts the user to select objects, then changes the color of each object selected to Green or the AutoCAD Color Index of 3.

本例提示用户选择对象,然后修改每个选中对象的颜色为绿色或 AutoCAD 颜色索引为 3。

VB.NET

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
 
<CommandMethod("SelectObjectsOnscreen")> _
Public Sub SelectObjectsOnscreen()
  '' 获得当前文档和数据库   Get the current document and database
  Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
  Dim acCurDb As Database = acDoc.Database
 
  ''启动一个事务   Start a transaction
  Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
 
      '' 要求在图形区域中选择对象    Request for objects to be selected in the drawing area
      Dim acSSPrompt As PromptSelectionResult = acDoc.Editor.GetSelection()
 
      '' 如果提示状态是 OK,对象就被选择了    If the prompt status is OK, objects were selected
      If acSSPrompt.Status = PromptStatus.OK Then
          Dim acSSet As SelectionSet = acSSPrompt.Value
 
          '' 遍历选择集中的对象   Step through the objects in the selection set
          For Each acSSObj As SelectedObject In acSSet
              '' 检查以确定返回的 SelectedObject 对象是有效的     Check to make sure a valid SelectedObject object was returned
              If Not IsDBNull(acSSObj) Then
                  '' 以写的方式打开选择的对象   Open the selected object for write
                  Dim acEnt As Entity = acTrans.GetObject(acSSObj.ObjectId, _
                                                          OpenMode.ForWrite)
 
                  If Not IsDBNull(acEnt) Then
                      '' 修改对象的颜色为绿色   Change the object's color to Green
                      acEnt.ColorIndex = 3
                  End If
              End If
          Next
 
          '' 保存新对象到数据库中   Save the new object to the database
          acTrans.Commit()
      End If
 
      '' 销毁事务  Dispose of the transaction
  End Using
End Sub

C#

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
 
[CommandMethod("SelectObjectsOnscreen")]
public static void SelectObjectsOnscreen()
{
  // 获得当前文档和数据库   Get the current document and database
  Document acDoc = Application.DocumentManager.MdiActiveDocument;
  Database acCurDb = acDoc.Database;
 
  // 启动一个事务  Start a transaction
  using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
  {
      // 要求在图形区域中选择对象    Request for objects to be selected in the drawing area
      PromptSelectionResult acSSPrompt = acDoc.Editor.GetSelection();
 
      // 如果提示状态是 OK,对象就被选择了    If the prompt status is OK, objects were selected
      if (acSSPrompt.Status == PromptStatus.OK)
      {
          SelectionSet acSSet = acSSPrompt.Value;
 
          // 遍历选择集中的对象   Step through the objects in the selection set
          foreach (SelectedObject acSSObj in acSSet)
          {
              // 检查以确定返回的 SelectedObject 对象是有效的     Check to make sure a valid SelectedObject object was returned
              if (acSSObj != null)
              {
                  // 以写的方式打开选择的对象   Open the selected object for write
                  Entity acEnt = acTrans.GetObject(acSSObj.ObjectId,
                                                   OpenMode.ForWrite) as Entity;
 
                  if (acEnt != null)
                  {
                      // 修改对象的颜色为绿色   Change the object's color to Green
                      acEnt.ColorIndex = 3;
                  }
              }
          }
 
          // 保存新对象到数据库中   Save the new object to the database
          acTrans.Commit();
      }
 
      // Dispose of the transaction
  }
}
VBA/ActiveX 代码参考

用窗口相交方式选择对象

This example selects the objects within and that intersect a crossing window.

本例选择相交窗口内部的和与窗口相交的对象。

VB.NET

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.EditorInput
 
<CommandMethod("SelectObjectsByCrossingWindow")> _
Public Sub SelectObjectsByCrossingWindow()
  '' 获得当前文档的编辑器     Get the current document editor
  Dim acDocEd As Editor = Application.DocumentManager.MdiActiveDocument.Editor
 
  '' 从(2,2,0) 到 (10,8,0)创建交叉窗口    Create a crossing window from (2,2,0) to (10,8,0)
  Dim acSSPrompt As PromptSelectionResult
  acSSPrompt = acDocEd.SelectCrossingWindow(New Point3d(2, 2, 0), _
                                            New Point3d(10, 8, 0))
 
  '' 如果提示状态是 OK,对象就被选择了    If the prompt status is OK, objects were selected
  If acSSPrompt.Status = PromptStatus.OK Then
      Dim acSSet As SelectionSet = acSSPrompt.Value
 
      Application.ShowAlertDialog("Number of objects selected: " & _
                                  acSSet.Count.ToString())
  Else
      Application.ShowAlertDialog("Number of objects selected: 0")
  End If
End Sub

C#

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
 
[CommandMethod("SelectObjectsByCrossingWindow")]
public static void SelectObjectsByCrossingWindow()
{
  // 获得当前文档的编辑器     Get the current document editor
  Editor acDocEd = Application.DocumentManager.MdiActiveDocument.Editor;
 
  // 从(2,2,0) 到 (10,8,0)创建交叉窗口    Create a crossing window from (2,2,0) to (10,8,0)
  PromptSelectionResult acSSPrompt;
  acSSPrompt = acDocEd.SelectCrossingWindow(new Point3d(2, 2, 0),
                                            new Point3d(10, 8, 0));
 
  // 如果提示状态是 OK,对象就被选择了    If the prompt status is OK, objects were selected
  if (acSSPrompt.Status == PromptStatus.OK)
  {
      SelectionSet acSSet = acSSPrompt.Value;
 
      Application.ShowAlertDialog("Number of objects selected: " +
                                  acSSet.Count.ToString());
  }
  else
  {
      Application.ShowAlertDialog("Number of objects selected: 0");
  }
}
VBA/ActiveX 代码参考