TBC Macros and Extensions

 View Only

 Capturing UI selection for macro

Eero Isola's profile image
Eero Isola posted 08-25-2025 07:37

Hello,

I am trying to create a macro that would:

  • Capture my UI selection (That has to be done before executing)
  • Select all items that share a layer with that item (And update UI selection accordingly)

This would be used mainly with CAD utilities, but I see no reason why it would not work with something else.

I have tried multiple solutions, but none of them can capture UI selection. Can someone help with this?

I am using TBC 5.9.

Ronny Schneider's profile image
Ronny Schneider

I'm still on vacation, without access to a dongle, so I can't test the following.

But in general it should work the following way.

In the UI XAML you probably would use a

    <Wpf:MemberSelection x:Name="objs" Height="30" Margin="0"/>

in the macro in OnLoad you need to define which routine is called when the change event is triggered, i.e.

        self.objs.ValueChanged += self.selectionChanged

and then you'll put the code for selecting the items off a layer in there, i.e.

    def selectionChanged(self, ctrl, e):

        # first disable the event trigger, otherwise you'll end up with a loop that is calling itself all the time
        self.objs.ValueChanged -= self.SelectionChanged
        
        layermemberserials = []
        # go through the selected objects
        for o in self.objs: # should in theory just be one object, but maybe the user selected multiple objects
            # get the layer of the of the selected object, as object
            l = self.currentProject.Concordance[o.Layer]
            # add the serials of all objects on that layer to the list
            for sn in l.Members:
                layermemberserials.Add(sn)

            # potentially you could shorten the above as follows
            #layermemberserials += self.currentProject.Concordance[o.Layer].Members

        # clear the global selection
        GlobalSelection.Clear()

        # our list potentially includes duplicate serial numbers, but I believe the following is ok with that
        # now set the global selection to our list of objects we've just compiled
        GlobalSelection.Items(self.currentProject).Set(layermemberserials)

        # reenable the event trigger
        self.objs.ValueChanged += self.SelectionChanged