from Autodesk.Revit.DB import *
from pyrevit import forms
import wpf, os, clr
clr.AddReference("System")
from System.Collections.Generic import List
from System.Windows import Window, FontWeights, Thickness, WindowStartupLocation, SizeToContent
from System.Windows.Controls import StackPanel, DockPanel, TextBox, TextBlock, Dock, Button, Separator
PATH_SCRIPT = os.path.dirname(__file__)
doc = __revit__.ActiveUIDocument.Document
uidoc = __revit__.ActiveUIDocument
app = __revit__.Application
class ef_TextDock():
"""Custom Class for generating a DockPanel that has TextBlock and TextBox with defined values."""
def __init__(self,key, label, default_value=""):
self.key = key
self.label = label
self.default_value = default_value
@property
def control(self):
dock = DockPanel()
dock.Margin = Thickness(0, 0, 0, 10)
text_block = TextBlock()
text_block.Text = self.label
text_block.FontWeight = FontWeights.Bold
text_block.Margin = Thickness(0, 0, 5, 0)
self.textbox = TextBox()
self.textbox.Text = self.default_value
dock.Children.Add(text_block)
dock.Children.Add(self.textbox)
return dock
@property
def value(self):
return self.textbox.Text
class ef_FlexTextForm(Window):
""" Flexible Text Form to take any numbers of text user inputs.
e.g.
from ef_forms import ef_TextDock, ef_FlexTextForm
components = [ef_TextDock(key='username', label = 'User Name:', default_value='Erik'),
ef_TextDock(key='lastname', label = 'User LastName'),
ef_TextDock(key = 'H', label = 'El Height'),
ef_TextDock(key = 'W', label = 'El Width')]
UI = ef_FlexTextForm(components)
dict_values = UI.values
username = dict_values['username']
lastname = dict_values['lastname']
height = dict_values['H']
width = dict_values['W']"""
text_controls = {}
values = {}
def __init__(self, components):
self.components = components
self.set_window_properties()
self.populate_wpf_controls()
self.ShowDialog()
def set_window_properties(self):
self.Title = 'EF-FlexTextForm'
self.Width = 300
self.MinHeight = 125
self.WindowStartupLocation = WindowStartupLocation.CenterScreen
self.ResizeMode = 0
self.SizeToContent = SizeToContent.Height
def populate_wpf_controls(self):
self.stack = StackPanel(Margin=Thickness(10))
for item in self.components:
self.text_controls[item.key] = item
self.stack.Children.Add(item.control)
sep = Separator()
sep.Margin = Thickness(0, 0, 0, 10)
button = Button()
button.Content = 'Submit!'
button.Click += self.UIe_btn_run
self.stack.Children.Add(sep)
self.stack.Children.Add(button)
self.Content = self.stack
def UIe_btn_run(self, sender, e):
"""Button action: Rename view with given """
for key, text_control in self.text_controls.items():
self.values[key] = text_control.value
self.Close()