Python UI using Macro Manager

SharpDevelop inside Revit has a really good tool to create windows form. The components can be dragged onto the canvas and then the code can be copied into a Python node in Dynamo with some minor tweaks.

Open Dynamo and create a new Python node. Then start loading all the required namespaces System.Windows.Forms (import all the components required: Button, Label, TextBox…), System.Drawing and EventHandler:

We can output directly the values from the form boxes or they can be stored in some variables.

Let’s then create our Form and give it a meaningful name:

Let’s go back to SharpDevelop and from Form1.Designer.cs copy the code inside the InitializeComponent method:

Remove all the C# keywords (this., new) and all the namespaces that we define before (System.Windows.Forms). Change true to True and false to False. SuspendLayout(); throws an error, so let’s remove that too.

Do the same for all the components definitions removing new System.Drawing. and new System.

The lines AutoScaleDimensions, AutoScaleMode, ResumeLayout and PerformLayout() are throwing another error, so let’s get rid of them.

Now we need to define all the functions associates with the form components:

  • The ok_btn has a Click event associated that will launch the Ok_btnClick function:

When the button is pressed, the textBox1.Text will be written in the distances variable. The same will happen for the textBoxWidth and textBoxHeight. We did not define a variable to store the formPosition value (dependent on which CheckBox is checked) so in order to access their values we will need to call checkBoxStart.Checked, checkBoxEnd.Checked and checkBoxMidPoint.Checked (The formPosition variable does not exists outside the function).

  • The comboBox (aka drop down menu) has a ComboBoxCategorySelectedIndexChanged event which allows to retrieve the item selected by the user:

  • checkBoxStart and checkBoxEnd have their own Click event: CheckBoxStart_Click and CheckBoxEnd_Click while checkBoxMidPoint is set to checked by default. Both checkBoxStart and checkBoxEnd functions will make sure that the checkBoxMidPoint is unchecked if clicked.

Finally we need a function to populate the comboBox:

We can call this function when the form loads using the Load event:

Has we have seen previously we can output the variables we have defined or we can access directly the components on the form:

OUT = distances, formVoidWidth, cboxMid, checkBoxEnd.Checked, checkBoxStart.Checked, 
comboBoxCategory.SelectedItem.ToString(), textBox1.Text, textBoxWidth.Text, textBoxHeight.Text

Written on November 16, 2018