We are trying to have a button that once clicked will show a picker to select an element.
The following code works (once). Once clicked again an exception is thrown. Strangely it is printed in a different window (hint?).
This is the resulted of printing the exception:
This name ‘uidoc’ is not defined
Any suggestion on how to fix this would be appreciated?
gdict = globals()
uiapp = __revit__
uidoc = uiapp.ActiveUIDocument
class MyWindow(Windows.Window):
def __init__(self):
xaml_file_path = script.get_bundle_file('dialog/dialog.xaml')
with open(xaml_file_path, 'r') as f:
xaml_content = f.read()
self.dialog = XamlReader.Parse(StringReader(xaml_content).ReadToEnd())
self.select_button = self.dialog.FindName('selectButton')
self.select_button.Click += self.handle_select_click
def show(self):
self.dialog.ShowDialog()
def handle_select_click(self, sender, e):
self.dialog.Dispatcher.Invoke(lambda: self.dialog.Hide())
try:
choices = uidoc.Selection
hasPickOne = choices.PickObject(ObjectType.Element)
if hasPickOne != None:
print("One element selected.")
except Exception as e:
print(e)
self.dialog.Dispatcher.Invoke(lambda: self.dialog.Show())
MyWindow().show()