Xaml return values

Hi everyone,

i’m trying out the xaml coding within pyrevit.
i have a simple window that has 2 buttons: selection button and confirm button.

i want the value of the selection to be passed outside of the window when the confirm button is pressed. The problem i’m having is that the the printout and dictionary already get triggerd when the window shows back up after the selection happend ( so no confirm yet).

Is it correct to take the values outside of the window proces?
Or should i just put that all under the confirm?

That just seems tricky when u start having a bigger script. seems that the confirm would trigger something outside of the window to be the most logical but i can’t really figure this step out.

below my current code:

# -*- coding: utf-8 -*-

import datetime
starttime = datetime.datetime.today()

import clr

import wpf
from System import Windows

from Autodesk.Revit.DB import *
from pyrevit import forms, revit, script, UI

# doc
# ===================================================
doc = revit.doc
uidoc = revit.uidoc

# definitions
# ===================================================
class MyWindow(Windows.Window):
    def __init__(self, xaml_filename):
        wpf.LoadComponent(self, script.get_bundle_file(xaml_filename))

    def element(self):
        return self.element
    def Confirm(self, sender, args):
        self.element = self.selection
        self.Close()  # Close the window after Confirm button is pressed
        return self.element
    def Select_Wall(self, sender, args):
        self.selection = revit.pick_element_by_category(BuiltInCategory.OST_Walls)
        if self.selection:
            self.Show()  # Show the window after an element is selected

# Main
# ===================================================
window = MyWindow('ui.xaml')

if window.element:

is it also normal that the code doesn’t find its import anymore after using a the selection button a second time? this makes the script return an execption whereby it doesn’t find the revit module anymore.

    def Select_Wall(self, sender, args):
            self.selection = revit.pick_element_by_category(DB.BuiltInCategory.OST_Walls)
            if self.selection:
        except Exception as ex:
            print("Exception occurred during element selection: {}".format(ex))

I found the ‘mistake’:
It was in the difference between window.Show() vs window.ShowDialog().
The first opens the window and continues the code the second does not, hence it can repeat itself and can find the needed modules and code.

More info on this:
How to open a window - WPF .NET | Microsoft Learn

1 Like