Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
UI.MultipleInputForm Updated
#Copyright (c) mostafa el ayoubi , 2016
#Data-Shapes www.data-shapes.net , elayoubi.mostafa@gmail.com
import clr
clr.AddReference('System.Windows.Forms')
clr.AddReference('System.Drawing')
from System.Drawing import Point
from System.Windows.Forms import Application, Button, Form, Label, TextBox, CheckBox, FolderBrowserDialog, OpenFileDialog, DialogResult, ComboBox, FormBorderStyle
from System.Collections.Generic import *
clr.AddReference('RevitAPIUI')
from Autodesk.Revit.UI import Selection
clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
clr.AddReference('RevitServices')
from RevitServices.Persistence import DocumentManager
doc = DocumentManager.Instance.CurrentDBDocument
uidoc = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument
class MultiTextBoxForm(Form):
def __init__(self):
self.Text = 'Data-Shapes | Multi Input UI'
self.output = []
self.values = []
def setclose(self, sender, event):
cbindexread = 0
for f in self.output:
if f.GetType() == TextBox:
self.values.append(f.Text)
if f.GetType() == CheckBox:
self.values.append(f.Checked)
if f.GetType() == Button:
if f.Tag == None :
self.values.append(f.Text)
else:
self.values.append(f.Tag)
if f.GetType() == ComboBox:
self.values.append(globals() ['dict%d'%(cbindexread)][f.Text])
cbindexread += 1
self.Close()
def reset(self, sender, event):
pass
def openfile(self, sender, event):
ofd = OpenFileDialog()
dr = ofd.ShowDialog()
if dr == DialogResult.OK:
sender.Text = ofd.FileName
def opendirectory(self, sender, event):
fbd = FolderBrowserDialog()
dr = fbd.ShowDialog()
if dr == DialogResult.OK:
sender.Text = fbd.SelectedPath
def pickobjects(self, sender, event):
sel = uidoc.Selection.PickObjects(Selection.ObjectType.Element,'')
selelem = [doc.GetElement(s.ElementId) for s in sel]
sender.Tag = (selelem)
def pickfaces(self, sender, event):
selface = uidoc.Selection.PickObjects(Selection.ObjectType.Face,'')
faces = [uidoc.Document.GetElement(s).GetGeometryObjectFromReference(s).ToProtoType(True) for s in selface]
sender.Tag = [i for f in faces for i in f]
def pickedges(self, sender, event):
seledge = uidoc.Selection.PickObjects(Selection.ObjectType.Edge,'')
edges = [uidoc.Document.GetElement(s).GetGeometryObjectFromReference(s).AsCurve().ToProtoType(True) for s in seledge]
sender.Tag = edges
def topmost(self):
self.TopMost = True
form = MultiTextBoxForm()
form.topmost()
xlabel = 25
xinput = 125
y = 10
fields = []
error = 0
cbindex = 0
#Input form
if isinstance(IN[0],list):
inputnames = IN[0]
else:
inputnames = [IN[0]]
if isinstance(IN[1],list):
inputtypes = IN[1]
else:
inputtypes = [IN[1]]
for i,j in zip(inputnames,inputtypes):
label = Label()
label.Location = Point(xlabel,y+4)
label.Height = 20
label.Width = 80
label.Text = str(i)
form.Controls.Add(label)
if isinstance(j,list):
cb = ComboBox()
cb.Location = Point(xinput,y)
cb.Width = 150
globals()['dict%d'%(cbindex)] = {}
try :
for k in j:
globals()['dict%d'%(cbindex)][k.Name] = k
cb.Items.Add(k.Name)
except :
for k in j:
try:
globals()['dict%d'%(cbindex)][str(k)] = k
except:
globals()['dict%d'%(cbindex)][k.encode('utf-8').decode('utf-8')] = k
cb.Items.Add(k)
form.Controls.Add(cb)
form.output.append(cb)
cbindex += 1
elif j == 's':
tb = TextBox()
tb.Text = 'Default'
tb.Width = 150
tb.Location = Point(xinput,y)
form.Controls.Add(tb)
form.Controls.Add(label)
form.output.append(tb)
elif j == 'bool':
yn = CheckBox()
yn.Location = Point(xinput,y)
yn.Text = 'Yes/No'
form.Controls.Add(yn)
form.output.append(yn)
elif j == 'fp':
fp = Button()
fp.Width = 150
fp.Text = 'FilePath'
fp.Location = Point(xinput,y)
form.Controls.Add(fp)
fp.Click += form.openfile
form.output.append(fp)
elif j == 'dp':
dp = Button()
dp.Width = 150
dp.Text = 'DirectoryPath'
dp.Location = Point(xinput,y)
form.Controls.Add(dp)
dp.Click += form.opendirectory
form.output.append(dp)
elif j == 'se':
se = Button()
se.Width = 150
se.Text = 'Select model element(s)'
se.Location = Point(xinput,y)
form.Controls.Add(se)
se.Click += form.pickobjects
form.output.append(se)
elif j == 'sf':
sf = Button()
sf.Width = 150
sf.Text = 'Select face(s)'
sf.Location = Point(xinput,y)
form.Controls.Add(sf)
sf.Click += form.pickfaces
form.output.append(sf)
elif j == 'sed':
sed = Button()
sed.Width = 150
sed.Text = 'Select Edge(s)'
sed.Location = Point(xinput,y)
form.Controls.Add(sed)
sed.Click += form.pickedges
form.output.append(sed)
else :
error = 'One or more input types are invalid, visit www.data-shapes.net for more informations'
y+= 30
button = Button()
button.Text = 'Set values'
button.Width = 150
button.Location = Point (100,y+30)
button.Click += form.setclose
form.Controls.Add(button)
form.MaximizeBox = False
form.MinimizeBox = False
form.FormBorderStyle = FormBorderStyle.FixedSingle
form.Height = y + 120
if error == 0 and IN[2] == True:
Application.Run(form)
result = form.values
OUT = result,True
elif IN[2] == False:
result = "Set toggle to true!"
OUT = result,False
@Ralmus

This comment has been minimized.

Copy link

@Ralmus Ralmus commented Dec 1, 2016

hi good node. i have update dynamo to 1.2.1
code error
UnicodeDecodeError: ('unknown', u'\xb0', 0, 1, '')
what happen?

@MostafaElAyoubi

This comment has been minimized.

Copy link
Owner Author

@MostafaElAyoubi MostafaElAyoubi commented Jan 4, 2017

Ralmus, I have fixed this Unicode error. It is due to python not handling well some characters (like accents etc.) in Revit 2016 and previous. You can download latest version of the package and try . I'd like to know if it works! thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment