05. Selection


Introduction

Today we will learn how to interact with AutoCAD.

Select Objects in the Drawing Area

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:

  • 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.

Add selected objects to a selection set

This example prompts the user to select objects twice and then merges the two selection sets created into a single selection set.

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;

[CommandMethod("MergeSelectionSets")]
public static void MergeSelectionSets()
{
  // Get the current document editor
  Editor acDocEd = Application.DocumentManager.MdiActiveDocument.Editor;

  // Request for objects to be selected in the drawing area
  PromptSelectionResult acSSPrompt;
  acSSPrompt = acDocEd.GetSelection();

  SelectionSet acSSet1;
  ObjectIdCollection acObjIdColl = new ObjectIdCollection();

  // If the prompt status is OK, objects were selected
  if (acSSPrompt.Status == PromptStatus.OK)
  {
      // Get the selected objects
      acSSet1 = acSSPrompt.Value;

      // Append the selected objects to the ObjectIdCollection
      acObjIdColl = new ObjectIdCollection(acSSet1.GetObjectIds());
  }

  // Request for objects to be selected in the drawing area
  acSSPrompt = acDocEd.GetSelection();

  SelectionSet acSSet2;

  // If the prompt status is OK, objects were selected
  if (acSSPrompt.Status == PromptStatus.OK)
  {
      acSSet2 = acSSPrompt.Value;

      // Check the size of the ObjectIdCollection, if zero, then initialize it
      if (acObjIdColl.Count == 0)
      {
          acObjIdColl = new ObjectIdCollection(acSSet2.GetObjectIds());
      }
      else
      {
          // Step through the second selection set
          foreach (ObjectId acObjId in acSSet2.GetObjectIds())
          {
              // Add each object id to the ObjectIdCollection
              acObjIdColl.Add(acObjId);
          }
      }
  }

  Application.ShowAlertDialog("Number of objects selected: " +
                              acObjIdColl.Count.ToString());
}

Define Rules for selection Filters

DXF CODES Filter Type
0 (or DxfCode.Start) Object Type (String)Such as “Line,” “Circle,” “Arc,” and so forth.
2 (or DxfCode.BlockName) Block Name (String)The block name of an insert reference.
8 or (DxfCode.LayerName) Layer Name (String)Such as “Layer 0.”
60 (DxfCode.Visibility) Object Visibility (Integer)Use 0 = visible, 1 = invisible.
62 (or DxfCode.Color) Color Number (Integer)Numeric index values ranging from 0 to 256.Zero indicates BYBLOCK. 256 indicates BYLAYER. A negative value indicates that the layer is turned off.
67 Model/paper space indicator (Integer)Use 0 or omitted = model space, 1 = paper space.

Relational operators for selection set filter lists

Operator Description
“”"*""" Anything goes (always true)
“”"=""" Equals
“”"!=""" Not equal to
“”"/=""" Not equal to
“”"<>""" Not equal to
“”"<""" Less than
“”"<=""" Less than or equal to
“”">""" Greater than
“”">=""" Greater than or equal to
“”"&""" Bitwise AND (integer groups only)
“”"&=""" Bitwise masked equals (integer groups only)

PromptStatus

namespace Autodesk.AutoCAD.EditorInput
{
    public enum PromptStatus
    {
        Keyword = -5005,
        Cancel = -5002,
        Error = -5001,
        None = 5000,
        Modeless = 5027,
        Other = 5028,
        OK = 5100
    }
}

SelectionDemo Code

using Autodesk.AutoCAD.ApplicationServices;
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;
using AcDoNetTools;
using Autodesk.AutoCAD.DatabaseServices;

namespace SelectionDemo
{
    public class Class1
    {

        [CommandMethod("PromptDemo")]
        public void PromptDemo()
{
            Database db = HostApplicationServices.WorkingDatabase;
            // Get the current editor
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            //PromptPointResult ppr = ed.GetPoint("\n Please select the first point:");
            //if (ppr.Status == PromptStatus.OK)
            //{
            //    Point3d p1 = ppr.Value;
            //    ppr = ed.GetPoint("\n Please select the second point:");
            //    if (ppr.Status == PromptStatus.OK)
            //    {
            //        Point3d p2 = ppr.Value;
            //        db.AddLineToModeSpace(p1, p2);
            //    }
            //}

            Point3d p1 = new Point3d(0, 0, 0);
            Point3d p2 = new Point3d();

            PromptPointOptions ppo = new PromptPointOptions("Please select the first point:");
            ppo.AllowNone = true;
            PromptPointResult ppr = GetPoint(ppo);

            if (ppr.Status == PromptStatus.Cancel) return;

            if (ppr.Status == PromptStatus.OK) p1 = ppr.Value;
            ppo.Message = "Please select the second point";
            ppo.BasePoint = p1;
            ppo.UseBasePoint = true;
            ppr = GetPoint(ppo);
            if (ppr.Status == PromptStatus.Cancel) return;
            if (ppr.Status == PromptStatus.None) return;
            if (ppr.Status == PromptStatus.OK) p2 = ppr.Value;
            db.AddLineToModeSpace(p1, p2);  
        }

Reference

http://docs.autodesk.com/ACD/2010/ENU/AutoCAD .NET Developer’s Guide/index.html?url=WS1a9193826455f5ff2566ffd511ff6f8c7ca-3e30.htm,topicNumber=d0e26245

http://docs.autodesk.com/ACD/2011/CHS/filesDXF/WSfacf1429558a55de185c428100849a0ab7-5f35.htm


Author: Ford Yang
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint polocy. If reproduced, please indicate source Ford Yang !
  TOC