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 with Macro creator. Users can create Ironpython scripts with the SharpDevelop IDE attached with Revit for performing automation. Although, the barrier is high and it probably going to be retired.

Macro Manager in Manage Tab

Then few months ago, I came across a post written by Iris Inokhosa presenting a standalone SharpDevelop 4 allows users to build a winform UI and able to generate the Ironpython source code automatically. Then, users can just copy the script and run it in the python node of Dynamo.

https://www.dhirubhai.net/in/iris-inokhosa-ab2174164/

However, the attached SharpDevelop is a modified version, and it removed the ability to create new files of winform with Ironpython. Also. the .msi file requires administrative privileges to complete the installation and I am not able to find the portable version of SharpDevelop 4 ( ! people should not run portable software in company computer), so it is blocking the possibility of using it in a strict working environment.

Unable to create new solution / file with Macro Manager

Thus, I was hoping to find a way to break the restrictions imposed on the built-in IDE. After a few days of try and error, I finally found a workaround to unleash the power of designing winform in the IDE.

I found out a magic script is needed for a Python script file to unlock the functionality of the attached SharpDevelop.

#The magic script
import System.Drawing
import System.Windows.Forms

from System.Drawing import *
from System.Windows.Forms import *

class Form1(Form):
    def __init__(self):
        self.InitializeComponent()
    
    def InitializeComponent(self):
        self.Name = "Form1"
        self.Text = self.Name        

It is generated by the portable version of SharpDevelop deployed on a less restrictive environment. It basically just imported winform libraries and created an object of Form.

I simply copy the source code and paste it in Notepad then save it with .py extension.

Save as a Python file

After that, I open the attached SharpDevelop directly instead going to Macro Manager and create a new module.

C:\Program Files\Autodesk\Revit XXXX\SDA\bin

Then, a simple drag and drop can open the Python file in the IDE.

Drag and Drop

Comparing the template created by Macro Manager and the file opened with Magic script, we can notice there is a tab named "Design" on the right.

Template from Macro Manager<----> Drag and Drop file with magic script

Now, we can see there is a Form object in the screen and start putting more element into the area to construct a new winform UI.

Designing UI

Each element has its own properties. We can click on the Properties tab to modify the appearance and behavior as we want.

Properties

The SharpDevelop will generate the source code in Ironpython for UI while we playing with all the components.

Ironpython source code

We need to add a few more line in the header and footer in order to run the UI in Dynamo.

#Header
import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")        
#Footer
Application.Run(Form1())        

Personally, I like to use IronPython2 Engine because CPython3 is not supporting ISelectionFilter which I need to use all the time. It would be great if Dynamo Team can fix it or implementing Ironpython3 and I wish it would make things easier.

Python node in Dynamo

Finally, we can just click run and we have the UI created.


Pandurang Bhor

Associate Senior Software Developer at Burns & McDonnell

1 年

it was informative, but Revit don't want to continue with ironpython2, it will be going through cpython3 or c# later on it will be difficult to maintain old libraries and nodes

回复

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

KaChun Cho的更多文章

社区洞察

其他会员也浏览了