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
from pyrx import Ap, Ax, Db, Ed, Ge
from shapely import LineString


@Ap.Command()
def 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")]
       
        ps, ss = Ed.Editor.select(filter)

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

        for id in ss:
            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
@Ap.Command()
def pyssget1():
    try:
        # pass points using a list
        ps, ss = Ed.Editor.ssget(
            "F", [Ge.Point3d(0, 0, 0), Ge.Point3d(100, 100, 0)], None
        )

        if ps == Ed.PromptStatus.eNormal:
            print(len(ss.objectIds()))

    except Exception as err:
        print(err)


# add and remove prompts
@Ap.Command()
def pyssget2():
    try:
        # note this is a tuple
        ps, ss = Ed.Editor.ssget("_:$", ("MYADD", "MYREMOVE"), None)

        if ps == Ed.PromptStatus.eNormal:
            print(len(ss.objectIds()))

    except Exception as err:
        print(err)


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

        if ps != Ed.PromptStatus.eNormal:
            return

        # acedSSNameX
        print(ss.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 import Ap , Db , Ed , Ge , Gi @ Ap . Command () def TB () -> None :     try :         db = Db . curD...