Pyrevit: How to embed wpf with IronPython engine?

Created on 21 Feb 2020  Â·  8Comments  Â·  Source: eirannejad/pyRevit

Hi @eirannejad,
I'm trying to create an IronPython engine in AutoCAD, using the simplest method like here: https://github.com/Autodesk/civilconnection/blob/master/Src/CivilPython/CivilPython/CivilPython.cs#L46-L47
I added embedded python stdlib like you did here: https://github.com/eirannejad/pyRevit/blob/develop/dev/pyRevitLoader/Source/ScriptExecutor.cs#L136-L146
when I add these lines into my script:

import wpf

it said

File "wpf.py", line 11, in <module>
ImportError: No module named _wpf

I tried appending my path with

import clr
import sys
sys.path.append(r'C:\Program Files (x86)\IronPython 2.7\Platforms\Net40')
clr.AddReference('IronPython.Wpf')
import wpf

but I still had that Exception.
Could you please share some tricks you did to be able to import wpf in pyRevit?
Thanks a lot!

Not pyRevit Issue Question

All 8 comments

There is a couple of DLLs that are shipped with IronPython. You need to include and load all of the m. The IronPython.Wpf dll includes the wpf module code.

Screen Shot 2020-02-28 at 07 55 05

I did it by clr.AddReference("IronPython.Wpf"), but still got the same Exception. I also double checked the source C# project that create IronPython engine. It referenced IronPython.Wpf.dll
Am I missing anything?

Is the DLL shipped with the add-on as well?

No. I'll try shipping it. Thanks.

On Sat, 29 Feb 2020 at 15:05, Ehsan Iran-Nejad notifications@github.com
wrote:

Is the DLL shipped with the add-on as well?

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/eirannejad/pyRevit/issues/832?email_source=notifications&email_token=AAHDQVB3IGLB5MN53OT23BLRFDATBA5CNFSM4KY74252YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOENLTKRA#issuecomment-592917828,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AAHDQVDXFHNB4DZONYSLM5DRFDATBANCNFSM4KY7425Q
.

I added these lines in my .cs file:

ipy.LoadAssembly(Assembly.LoadFrom(string.Format(@"{0}\IronPython.dll", GetAssemblyPath())));
ipy.LoadAssembly(Assembly.LoadFrom(string.Format(@"{0}\IronPython.Modules.dll", GetAssemblyPath())));
ipy.LoadAssembly(Assembly.LoadFrom(string.Format(@"{0}\IronPython.SQLite.dll", GetAssemblyPath())));
ipy.LoadAssembly(Assembly.LoadFrom(string.Format(@"{0}\IronPython.Wpf.dll", GetAssemblyPath())));
ipy.LoadAssembly(Assembly.LoadFrom(string.Format(@"{0}\Microsoft.Dynamic.dll", GetAssemblyPath())));
ipy.LoadAssembly(Assembly.LoadFrom(string.Format(@"{0}\Microsoft.Scripting.dll", GetAssemblyPath())));

Here the script to test importing wpf. lib.pprint is my tool to print to AutoCAD command line:

# -*- coding: utf-8 -*-
import sys
import os
sys.path.append(os.path.dirname(__file__))
from lib import pprint, caddoc, editor
pprint(sys.path)
import clr
for r in clr.References:
    pprint(r)
import wpf
pprint('ok')

Here's the output:

mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Acmgd, Version=22.0.0.0, Culture=neutral, PublicKeyToken=null
Acdbmgd, Version=22.0.0.0, Culture=neutral, PublicKeyToken=null
accoremgd, Version=22.0.0.0, Culture=neutral, PublicKeyToken=null
acdbmgdbrep, Version=22.0.0.0, Culture=neutral, PublicKeyToken=null
IronPython, Version=2.7.0.40, Culture=neutral, PublicKeyToken=7f709c5b713576e1
IronPython.Modules, Version=2.7.0.40, Culture=neutral, PublicKeyToken=7f709c5b713576e1
IronPython.SQLite, Version=2.7.0.40, Culture=neutral, PublicKeyToken=7f709c5b713576e1
IronPython.Wpf, Version=2.7.0.40, Culture=neutral, PublicKeyToken=7f709c5b713576e1
Microsoft.Dynamic, Version=1.1.0.20, Culture=neutral, PublicKeyToken=7f709c5b713576e1
Microsoft.Scripting, Version=1.1.0.20, Culture=neutral, PublicKeyToken=7f709c5b713576e1
PresentationCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

I assume the dlls were loaded into current engine, but the exception still thrown:
image

The only thing I did additionally was to embed python_279_lib.zip into current engine.

😆 You're awesome. I didn't even remember this. It's from a long time ago

TIL it's always good to comment your code.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Arsany123 picture Arsany123  Â·  4Comments

thazell picture thazell  Â·  5Comments

thumDer picture thumDer  Â·  4Comments

alitehami picture alitehami  Â·  4Comments

TheBIMsider picture TheBIMsider  Â·  5Comments