Read Xaml file in dynamo by Python

Hi Everybody
I’m trying to read file xaml or create wpf form in dynamo like this video ( https://www.youtube.com/watch?v=UO_yYrWcxUM&t=446s ) the actually thing i want is use wpf by visual studio and then read file xaml in dynamo by python instead of create form by python. But when i follow above video i got a problem when read the file xaml in dynamo like below picture


i was find the file wpf.py from address link above picture

the all file i use:

Python file:
270124-wpf.py (1.2 KB)

dynamo file:
TestWPF.dyn (5.0 KB)

xaml file like this picture:


Really hope have a solution for this problem, i was lost a lot of time to find solution :sob: :sob:.
Thanks all to read

Try this

clr.AddReference('IronPython.Wpf')
from IronPython.Modules import Wpf as wpf

From pyRevit issue on github How to embed wpf with IronPython engine? · Issue #832 · eirannejad/pyRevit · GitHub

2 Likes

I think should import as a path if missing in Dynamo, but not sure it can resolve problem exaclly

import clt
clr.AddReferenceToFileAndPath("<the place to your reference IronPython.Wpf.dll>") 
import wpf 

it can be facing from two problem :

  • Missing Library DLLs shipped together in the time install Revit software
  • Conflict library IronPython with another third party library or extension.
4 Likes

Thanks for answer me

in 2022 it worked but in revit 2020, i lookup in source in DynamoForRevit i see it dont have some Dlls, and then i copy but got other problem like this:
Warning: IronPythonEvaluator.EvaluateIronPythonScript operation failed.
Traceback (most recent call last):
File “”, line 42, in
File “”, line 37, in init
TypeError: LoadComponent() takes exactly 3 arguments (2 given)
i will try to fix it,
Thank you for your answer

Hi @binhngo197

Normally for Revit 2020+ the ‘IronPython.Wpf’ module is integrated either into DynamoForRevit ( or into the DSIronPython package) for lower versions it is necessary to specify the paths to add like this

import clr
import sys
import System
clr.AddReference('ProtoGeometry')
clr.AddReference('RevitAPIUI')
clr.AddReference('RevitServices')

import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument
view = doc.ActiveView
uiapp = DocumentManager.Instance.CurrentUIApplication
uidoc = uiapp.ActiveUIDocument
app = uiapp.Application
sdkNumber = int(app.VersionNumber)

from Autodesk.DesignScript.Geometry import *
from Autodesk.Revit.UI import *
from Autodesk.Revit.UI.Selection import *

clr.AddReference('WindowsBase')

if sdkNumber < 2020:
    pf_path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFilesX86)
    sys.path.append(pf_path + '\\IronPython 2.7\\Lib')
    sys.path.append(pf_path + '\\IronPython 2.7\\DLLs')
try:
    clr.AddReference('IronPython.Wpf')
    clr.AddReference('PresentationCore')
    clr.AddReference('PresentationFramework')

except IOError:
    raise

from System.Windows.Markup import XamlReader, XamlWriter
from System.Windows import Window, Application
from System.IO import StreamReader, StringReader

doc = DocumentManager.Instance.CurrentDBDocument
view = doc.ActiveView
uidoc = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

import wpf
from System.Windows import Application, Window, Thickness
from System.Windows.Controls import Button

class MyWindow(Window):
    def __init__(self):
        self.winLoad = wpf.LoadComponent(self, r"C:\Users\sneep\Downloads\gui.xaml")

    def button1_Click(self, sender, e):
        print("Button clicked!")
        self.Close()


myWindow = MyWindow()
myWindow.ShowDialog() 
        
3 Likes

I was using wpf too at the beginning. However it cant run on cpython so I switched all to use window form.

1 Like

To enhance your development process, consider utilizing C# with WPF for creating add-ins. While Dynamo may seem appealing, transitioning to it would entail a significant learning curve and additional time for scripting before your end users can benefit from your work.

3 Likes

oh, Thank you, it is work now :smiling_face_with_three_hearts: :smiling_face_with_three_hearts:

2 Likes