As of version 2.2.58, most selection set actions in PyRx have received a powerful overload that allows developers to attach a
When executing a selection command, the callback function automatically intercepts a
Db.ObjectIdArray containing the candidate objects. Your logic then evaluates these objects and returns a list of integers representing the indexes of the IDs that should be discarded from the final selection.Key Benefits of Selection Callbacks
- Advanced Criteria: Filter by complex properties (like geometric limits or dynamic states) that standard DXF group codes cannot check.
- Streamlined Workflow: Eliminate the need to manually iterate over and clean up selection sets after user input.
- Real-time Precision: Ensure your commands only process exactly what they are intended to handle.
The Complete Python Code
Here is how to implement a dynamic block filter using the new callback overload.
import traceback
from pyrx import Ap, Db, Ed, Ge, Rx
def dyn_filter(ids: Db.ObjectIdArray) -> list[int]:
"""
Callback filter that evaluates a list of selected Object IDs.
Returns a list of list indexes pointing to objects that should be removed.
"""
itemsToRemove = []
# Enumerate through the selection array to track both index and object ID
for idx, id in enumerate(ids):
# Open the ID as a BlockReference to inspect its properties
ref = Db.BlockReference(id)
# Check if the block reference is NOT a dynamic block
if not ref.isDynamicBlock():
# Flag the index for removal if it fails the condition
itemsToRemove.append(idx)
return itemsToRemove
@Ap.Command()
def pyselectfcb():
"""
Custom PyRx command to prompt user selection using the custom filter.
"""
try:
# Prompt selection filtering for 'INSERT' (Block References)
# Pass the 'dyn_filter' function as the second argument callback
ps, ss = Ed.Editor.select([(0, "INSERT")], dyn_filter)
# If the selection prompt was successful, print the result status and size
if ps == Ed.PromptStatus.eOk:
print(f"Selection Status: {ps}, Filtered Count: {ss.size()}")
except Exception:
# Catch and print any runtime exceptions for debugging
print(traceback.format_exc())
Code Walkthrough
Ed.Editor.selectOverload: We passdyn_filteras a functional parameter directly into our selection method.- Indexing Strategy: The callback tracks
idx. If a block fails theisDynamicBlock()check, its exact list index is recorded. - Array Cleanup: PyRx reads the returned
itemsToRemovelist and instantly strips those matching positions out of your final selection setss.
Only the Dynamic Blocks are selected, yay!
No comments:
Post a Comment