TBC Macros and Extensions

 View Only

 WPF.MemberSelection field how to select only specific data into the field?

Jump to  Best Answer
Vitalii Vovk's profile image
Vitalii Vovk posted 08-17-2024 20:40

Hi, I have a question about WPF.MemberSelection. I need to ensure that only specific data can be selected in this field. You can easily achieve this by utilizing IsEntityValidCallback and adding a validation method to it. However, it only works well when the field is focused. If the field is not focused, IsEntityValidCallback does not engage in the filtration process.

You can turn off global selection for the field, but that's not the ideal scenario for me. I had some success working this with the ValueChanged method and some global selection methods, but I'm not fully satisfied.

I would like to ensure that only specific types of data are passed to this field, regardless of whether the field is focused during selection. Does anyone know a good approach to tackle this specific problem?

Vitalii Vovk's profile image
Vitalii Vovk  Best Answer

I believe I've found the solution for using WPF.MemberSelection properties with global selection. To ensure that only certain objects passing validation are added to the fields even the field is not focused, two properties need to be set to True:

  • WPF.MemberSelection.ProcessGlobalSelectionChanges = True: This updates the selection changes from the global selection.
  • WPF.MemberSelection.UseLocalSelection = True: This allows the global selection to go through the validation process.

By setting these properties, the objects will be properly validated before being added to the field

def OnLoad:
    self.objs.IsEntityValidCallback = self.isValid
    self.objs.UseLocalSelection = True
    self.objs.ProcessGlobalSelectionChanges = True
    self.LType = clr.GetClrType(IPolyseg)
    self.PType = clr.GetClrType(IPoint)

def isValid(self, serial):
        obj = self.currentProject.Concordance.Lookup(serial)
        if (self.linesRadioBtnCtl.IsChecked and isinstance(obj, self.LType)):
            return True
        elif (self.pointsRadioBtnCtl.IsChecked and isinstance(obj, self.PType)):
            return True
        return False
Ronny Schneider's profile image
Ronny Schneider

Hello Vitalii,

even though I use the IsEntityValidCallback as you describe above, I always check during runtime if I'm working with the correct object type by adding an

if isinstance()

check in front of those code blocks. Just want to make sure that I don't run into any runtime errors.

Especially with the memberselection, since you can have none matching entities selected before you start the macro.

You can use the isinstance i.e.

self.lType = clr.GetClrType(IPolyseg)
if isinstance(o, self.lType)

# or 

            if isinstance(boundarysite, PlanSetSheetViews) or \
                isinstance(boundarysite, PlanSetSheetView) or \
                isinstance(boundarysite, SheetSet) or \
                isinstance(boundarysite, BasicSheet):

# or
if isinstance(v, clr.GetClrType(Hoops2dView))

I usually use it without the "clr.GetClrType" and it works.

Vitalii Vovk's profile image
Vitalii Vovk

If I understood correctly, I am using the same approach. I want to achieve a scenario where, if there is a selection before the tool is opened, only a specific data type is included in the MemberSelection field once the tool is open. I had no problem implementing this when the MemberSelection field is focused and I do object selection, but I'm having trouble applying it when the field is not focused and I make global  selection  all the selected objects become members of the MemberSelection field. I have seen that was done on some RPS tools Im just wonder how did they achieve that