Notes of creating winform GUI in Dynamo Revit using Python [0]
Revit + Dynamo + Python

Notes of creating winform GUI in Dynamo Revit using Python [0]

As the online resources about this topic is getting less and less, I would like to share what I have collected and learned incase of all the links and tutorial are vanished in the internet.


At the time of making this notes, I am using Revit 2023 and Dynamo Revit 2.16.1.6510. Winform API run smoothly on Ironpython2 engine, so don't need to be worry if you are using older versions. But I would like to test the compatibility on Cpython3 engine.

First, we need to create a python script node in Dynamo.

Python Script Node

Inside the node, we can use the following code to create a simple form.

import clr
clr.AddReference('System.Windows.Forms')
from System.Windows.Forms import *

class MainForm(Form):
    def __init__(self):
        self.Text = "Topic"
        self.Width = 400
        self.Height = 400
        self.MaximizeBox = True
        self.MinimizeBox = True
        self.ControlBox = True
        self.Opacity = 1
        self.SizeGripStyle = SizeGripStyle.Show
        self.FormBorderStyle = FormBorderStyle.FixedSingle
        self.StartPosition = FormStartPosition.CenterScreen
        self.TopMost = True

Application.Run(MainForm())        

Expected result will be like this

A simple form

Now, lets explain the code line by line

import clr
clr.AddReference("System.Windows.Forms")
from System.Windows.Forms import *        

This is for importing necessary module.

For Ironpython2.7, clr is required to be imported and add the reference of "System.Windows.Forms"

class MainForm(Form):        

Setup a new Form class (object)

     def __init__(self):        

define the first function will be run in the Form

        self.Text = "Topic"        

it is for changing the name of the topic on top.

        self.Width = 400
        self.Height = 400        

This is for defining the size of the form and there are another way to define it with Size class imported from System.Drawing.

clr.AddReference('System.Drawing')
from System.Drawing import Size
class MainForm(Form):
    def __init__(self):
        self.Text = "Topic"
        self.Size = Size(200,600)        

It has the same effect as defining the size separately. the variables in the Size class are Size(Width,Height).

        self.MaximizeBox = True
        self.MinimizeBox = True
        self.ControlBox = True        

These 3 lines representing the buttons on top right corner.

Replacing True with False can take out the button from the Form.

        self.Opacity = 1        

As the name suggested, it controls the Opacity of the form

Opacity
self.SizeGripStyle = SizeGripStyle.Show #to show the indictor
self.SizeGripStyle = SizeGripStyle.Hide #to hide the indictor        

It is for controlling the visibility of the sizable indictor at the bottom right corner.

self.FormBorderStyle = FormBorderStyle.FixedSingle        

This line is to set the Form Style. Some styles will override the setting above.

My personal favourite is FixedSingle, but there are 6 more options in the following API document.

https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.formborderstyle?view=windowsdesktop-7.0

self.StartPosition = FormStartPosition.CenterScreen        

After setting up the style of the form, we need to decide where it should be started. There are 5 options in total and they can be found in the following link.

https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.formstartposition?view=windowsdesktop-7.0

self.TopMost = True        

Another setting I would normally set the TopMost parameter as True to make sure it is on top of all other programs.

Application.Run(MainForm()) #Non-Blocking
MainForm().ShowDialog() #Blocking        

The last line is to kick off the form with the setting we defined.

But there is 2 ways to do it, one is Non-blocking, another one is blocking.

I like to use the non-blocking method because allows me to create function that need to interact with Revit.


I know this is an old topic and now there are more fancy way to do GUI. But I am currently getting sick so I want to do something easier.

要查看或添加评论,请登录

KaChun Cho的更多文章

  • Bonsai 0.8.0 - A Peek at Clash Detection

    Bonsai 0.8.0 - A Peek at Clash Detection

    As most of the paperwork are settled and BlenderBIM is having significate changes, it will be a good time to check on…

    9 条评论
  • Notes of creating winform GUI in Dynamo Revit using Python [1]

    Notes of creating winform GUI in Dynamo Revit using Python [1]

    A couple of years ago, when Dynamo was a beta addon requiring users to install it separately, I noticed Revit comes…

    2 条评论
  • 4 Months after switching to Linux

    4 Months after switching to Linux

    Config Distro: Kubuntu 20.04 LTS Desktop Environment: Plasma 5 CPU: i7-6700 GPU: RTX 2060 RAM: 16GB Reason First of…

社区洞察

其他会员也浏览了