use freecad python module of Windows dev version

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
User avatar
bernd
Veteran
Posts: 12849
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

use freecad python module of Windows dev version

Post by bernd »

At work I have a CAD software which has a Pythoninterpreter (Nemetschek Allplan). I would like to import freecad moules in this Python environment. But I use the FreeCAD windwos dev version which is just an extracted 7zip archive. How can I use all the Pythonmodules from this FreeCAD?

bernd
User avatar
sgrogan
Veteran
Posts: 6499
Joined: Wed Oct 22, 2014 5:02 pm

Re: use freecad python module of Windows dev version

Post by sgrogan »

bernd wrote: Fri Aug 17, 2018 12:36 pm How can I use all the Pythonmodules from this FreeCAD?
Hey bernd.
I think making sure the path to the modules are in your interpreters sys.path should work for .py files.
For the compiled extensions .pyd we need to determine what python version your interpreter is using.
"fight the good fight"
User avatar
bernd
Veteran
Posts: 12849
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: use freecad python module of Windows dev version

Post by bernd »

which one do I need to import Part?
wmayer
Founder
Posts: 20241
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: use freecad python module of Windows dev version

Post by wmayer »

You only have to make sure that Nemetschek Allplan finds the FreeCAD.pyd file. Then you must first import FreeCAD in order to initialize the internals of FreeCAD. If your directory structure is OK FreeCAD sets up everything so that it finds its extension modules later. Afterwards you can import Part.

Trying to import Part without loading FreeCAD first is not supposed to work and leads to a crash.
User avatar
bernd
Veteran
Posts: 12849
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: use freecad python module of Windows dev version

Post by bernd »

Thanks guys for the informations. Werner this is what I need. I will gibe it a try but it may take some time.

Since Allplan uses Python 3.x I'm lucky we gone provide Pyton 3 builds for windows. Sgrogan great :D

bernd
User avatar
bernd
Veteran
Posts: 12849
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: use freecad python module of Windows dev version

Post by bernd »

Code: Select all

pathToFreeCAD = "C:\\0_BHA_privat\\progr\\FreeCAD_0.18.xxxxx_x64_dev_Py3_Qt5_win\\bin"
import sys
sys.path.append(pathToFreeCAD)
for p in sys.path:
    print(p)
works great but if I add an

Code: Select all

import FreeCAD
Allplan crashes ... :shock:

screen.jpg
screen.jpg (230.51 KiB) Viewed 1575 times

Mhh since it crashes it does recognize something, means I seam on the right track ... :)

something to test the import if it works ...

Code: Select all

ver = FreeCAD.Version()
print("FreeCAD {0}.{1}.{2}".format(ver[0],ver[1],ver[2]))
wmayer
Founder
Posts: 20241
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: use freecad python module of Windows dev version

Post by wmayer »

Next step is to compare the 3rd party libraries used by both applications. It might be possible that two different versions of a library is used by the applications which in most cases won't work.

An alternative is to not import FreeCAD directly but spawn a Python process and load the FreeCAD module this way (you can also spawn FreeCADCmd.exe). In this case it doesn't matter which 3rd party libraries both applications use, especially Python2 vs. Python3.

The problem then will be how to interchange data between the processes: https://stackoverflow.com/questions/392 ... plications
User avatar
bernd
Veteran
Posts: 12849
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: use freecad python module of Windows dev version

Post by bernd »

wmayer wrote: Tue Aug 21, 2018 9:02 am Next step is to compare the 3rd party libraries used by both applications. It might be possible that two different versions of a library is used by the applications which in most cases won't work.
I would like to try tackling this down. Werner do you have an idea how to get more errormessages from what is happening after import FreeCAD in Allplan, by the use of Python. Or how would you processeed ?

bernd
wmayer
Founder
Posts: 20241
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: use freecad python module of Windows dev version

Post by wmayer »

When exactly does it crash? Does the terminal window return when entering "import FreeCAD"?
If you have installed Visual Studio on this computer (it doesn't matter which version) you can attach the debugger to the Allplan executable after it has crashed (don't click on "Programm schließen"). The debugger may give some further information about where the crash happens.

If you don't have VS here you can also use WinDbg which is part of the Debugging Tools for Windows. If none of them is available you can use the tool ProcessExplorer (www.sysinternals.com), select the crashing executable and create a dump file. You can choose between mini dump (small file size) and full dump (can become huge). Then you can analyse the dump file on another computer where a debugger is available.

Btw, with ProcessExplorer you see all dlls an executable has loaded and you may find conflicting versions used by Allplan and FreeCAD.
User avatar
bernd
Veteran
Posts: 12849
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: use freecad python module of Windows dev version

Post by bernd »

Wow cool a lot of informations. I will go and give them a try.


wmayer wrote: Tue Aug 21, 2018 10:14 am When exactly does it crash? Does the terminal window return when entering "import FreeCAD"?
The problem with Allplan Python is, you do not have your own terminal to interact with the Python interpreter like in FreeCAD. One creates a Python file with all the code inside. In Allplan GUI you can drop your PythonPart in the Allplan drawing window. You can not interact. The only thing which is available is the Allplan trace window which prints all kind of informations including Python outputs. See earlier post wha is printed ... nothing. If import FreeCAD is omited everything runs smooth.

bernd
Post Reply