Monday, October 2, 2023

Selecting objects in Python for AutoCAD

 

Selecting objects, PyRx has both ARX style and kind of .NET inspired methods for selecting. The selection methods return values as a tuple, with the first value always being the status (PromptStatus), the second is the selection set class. Selection methods, should always have an overloaded version that excepts a filter, a list of tuples representing a result buffer. Let’s start with just the basic select

 

import traceback
import PyRx as Rx
import PyGe as Ge
import PyGi as Gi
import PyDb as Db
import PyAp as Ap
import PyEd as Ed


def PyRxCmd_ss1():
    try:
        # the filter, note, you can use an integer value instead of
        # the DxfCode, similar to whats in AutoLisp
        filter = [(Db.DxfCode.kDxfStart, "LINE")]

        # sometimes I like to annotate types to preserve intellisense
        # Ed.Editor.select
        ssres: tuple[Ed.PromptStatus,
                     Ed.SelectionSet] = Ed.Editor.select(filter)

        # check the result, note eOk is the same as eNormal (RTNORM in ARX)
        if ssres[0] != Ed.PromptStatus.eOk:
            return

        for id in ssres[1].objectIds():
            ent = Db.Entity(id)
            # should only return lines because of the filter
            print(ent.isA().name())

    except Exception as err:
        traceback.print_exception(err)


 

Also available are various overloads

Ed.Editor.selectAll(filter)

Ed.Editor.selectFence(list[Ge.Point3d],filter)

Ed.Editor.selectWindow(Ge.Point3d,Ge.Point3d,filter)

Etc.

 

Next is the ARX style wrapper of acedSSGet, the arguments are the same as the ARX documentation

 

# this is just in case one of the supplied wrappers doesn't fit, roll your own
def PyRxCmd_pyssget1():
  try:
    # pass points using a list
    ssResult = Ed.Editor.ssget(
        "F", [Ge.Point3d(0, 0, 0), Ge.Point3d(100, 100, 0)], None)

    if ssResult[0] == Ed.PromptStatus.eNormal:
      print(len(ssResult[1].objectIds()))

  except Exception as err:
    print(err)


#add and remove prompts
def PyRxCmd_pyssget2():
  try:
    # note this is a tuple
    ssResult = Ed.Editor.ssget("_:$", ("MYADD", "MYREMOVE"), None)

    if ssResult[0] == Ed.PromptStatus.eNormal:
      print(len(ssResult[1].objectIds()))

  except Exception as err:
    print(err)


# nested
def PyRxCmd_pyssget3():
  try:
    ssResult = Ed.Editor.ssget("_:n", None, None)

    if ssResult[0] != Ed.PromptStatus.eNormal:
      return

    # acedSSNameX
    print(ssResult[1].ssNameX())

  except Exception as err:
    print(err)


 

As a side note acedGetCurrentSelectionSet is also wrapped inside the Editor class

 

No comments:

Post a Comment

TraceBoundary sample in Python for AutoCAD

    import traceback from pyrx_imp import Rx from pyrx_imp import Ge from pyrx_imp import Gi from pyrx_imp import Db from pyrx_imp...