Select model element from UI upon button click

I want user to select a pipe from the UI , by clicking a button. Following code I am using to achieve this task. But I am able to select pipe only after closing the UI window. I want the user to select the pipe while my UI window is active.

class CustomISelectionFilter(ISelectionFilter):
    def __init__(self, category_name):
        self.category_name = category_name
    def AllowElement(self, e):
        if e.Category.Name == self.category_name:
            return True
            return False
    def AllowReference(self, ref, point):
        return true
class MyWindow(Windows.Window):
    def __init__(self):
        wpf.LoadComponent(self,xamlfile) #self is the instance of the class and we are loading xaml file into that instance
    def btn_Click_SelectPipe(self,sender,args):
            ductsel = uidoc.Selection.PickObject(ObjectType.Element,CustomISelectionFilter("Pipes"),"Select a Pipe")
        except Exceptions.OperationCanceledException:
            TaskDialog.Show("Operation canceled","Canceled by the user")

Here is my UI

Depending on the context and function of the script, the easiest solution would be to have the element selected before you run the script and build in some code to check that what you have selected is the correct thing.

Little more challenging would be to close the dialog when you click the button, which would allow the user to interact with Revit environment, and then write some code to open the dialog again after you have selected.

Most challenging, in my opinion, would be to create a non-modal window which would allow you to interact with the Revit environment even with the dialog open.


Can you show all the code?

1 Like

I tried by hiding the window and it is working

def btn_Click_SelectPipe(self,sender,args):
            Pipe_Sel = uidoc.Selection.PickObject(ObjectType.Element,CustomISelectionFilter("Pipes"),"Select a Pipe")
            UI.TaskDialog.Show("Operation canceled","Canceled by the user")
1 Like