How to refer to a selected object in a macro?

I’m trying to set up a macro to select all block instances of a given name

'_-SelBlockInstanceNamed _SelectBlockToMatch

This is fine if I want run the macro THEN select a block but I would like to select a block THEN run the macro and have the macro refer to the selected block for the _SelectBlockToMatch portion of the macro

Is this possible?

Hi Andy, it seems this is not working in V7, in V8 preselection will work.

Ah nice, no worries!

Cheers Gijs

There you have a simple script to do that.
You can make it into a command and macro it, or use it as is

import rhinoscriptsyntax as rs

def select_block_instances(block_name):
    all_blocks = rs.AllObjects()
    for block in all_blocks:
        if rs.IsBlockInstance(block) and rs.BlockInstanceName(block) == block_name:
            rs.SelectObject(block)


block_name = rs.GetString("Enter the block name")
if block_name:
    select_block_instances(block_name)

Hope that helps,
If you would like to select an object not by writing It’s name, but rather by selecting an object that has the same name as the one we want to select I can change it. Let me know.
Farouk

Hi Farouk,

Thanks for making this - yes could you make it where I select an object?

All my block names are part numbers, hard to remember them!

Thanks

Hey Andy,
There you go :

import rhinoscriptsyntax as rs

def select_block_instances(block_name):
    all_blocks = rs.AllObjects()

    # Loop through all blocks
    for block in all_blocks:
        # Check if the block is an instance of the given block name
        if rs.IsBlockInstance(block) and rs.BlockInstanceName(block) == block_name:
            rs.SelectObject(block)

block_id = rs.GetObject("Select a block")
if block_id:
    # Get the block name
    block_name = rs.BlockInstanceName(block_id)
    if block_name:
        select_block_instances(block_name)

Hope that helps.
Farouk