[SOLVED] Embedding FreeCAD in ipython

Need help, or want to share a macro? Post here!
iari
Posts: 42
Joined: Mon Nov 18, 2013 8:52 pm

[SOLVED] Embedding FreeCAD in ipython

Postby iari » Wed Dec 23, 2015 3:49 pm

Hi,
I can import FreeCAD module from the external (Anaconda) python console, while I've errors when trying from the ipython console.

In ipython I do:

Code: Select all

In [116]: sys.path.append(r'C:\Program Files\FreeCAD 0.15\bin')
In [117]: import FreeCAD
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-117-3f6c136746bd> in <module>()
----> 1 import FreeCAD

ImportError: DLL load failed: Impossibile trovare la procedura specificata.
My FreeCAD module path is:
C:\Program Files\FreeCAD 0.15\bin

There I find:
FreeCAD.pyd
FreeCADApp.dll
...

My ipython version:
'2.7.8 |Anaconda 2.1.0 (64-bit)| (default, Jul 2 2014, 15:12:11) [MSC v.1500 64 bit (AMD64)]'

FreeCAD python console version:
'2.7.8 (default, Nov 17 2014, 20:37:05) [MSC v.1800 64 bit (AMD64)]'

FreeCAD version:
OS: Windows 8
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.15.4671 (Git)
Branch: releases/FreeCAD-0-15
Hash: 244b3aef360841646cbfe80a1b225c8b39c8380c
Python version: 2.7.8
Qt version: 4.8.6
Coin version: 4.0.0a
OCC version: 6.8.0.oce-0.17

Can you help?
Thank you!
igmar
Last edited by iari on Sat Dec 26, 2015 8:31 am, edited 1 time in total.
wmayer
Site Admin
Posts: 14893
Joined: Thu Feb 19, 2009 10:32 am

Re: Embedding FreeCAD in ipython

Postby wmayer » Wed Dec 23, 2015 5:06 pm

In [116]: sys.path.append(r'C:\Program Files\FreeCAD 0.15\bin')
Here you must write

Code: Select all

In [116]: sys.path.append(r'C:\\Program Files\\FreeCAD 0.15\\bin')
because "\b" is an escape sequence and will be interpreted as "\x08".
iari
Posts: 42
Joined: Mon Nov 18, 2013 8:52 pm

Re: Embedding FreeCAD in ipython

Postby iari » Wed Dec 23, 2015 5:22 pm

wmayer wrote:
In [116]: sys.path.append(r'C:\Program Files\FreeCAD 0.15\bin')
Here you must write

Code: Select all

In [116]: sys.path.append(r'C:\\Program Files\\FreeCAD 0.15\\bin')
because "\b" is an escape sequence and will be interpreted as "\x08".
You are right, but the snippet was only one of the trials.. Unfortunately also the right string doesn't resolve.
wmayer
Site Admin
Posts: 14893
Joined: Thu Feb 19, 2009 10:32 am

Re: Embedding FreeCAD in ipython

Postby wmayer » Wed Dec 23, 2015 5:34 pm

2.7.8 |Anaconda 2.1.0 (64-bit)| (default, Jul 2 2014, 15:12:11) [MSC v.1500 64 bit (AMD64)]'
'2.7.8 (default, Nov 17 2014, 20:37:05) [MSC v.1800 64 bit (AMD64)]
The different compiler versions MSC 1800 (=> VS 2013) and MSC 1500 (=> VS 2008) can also be a problem.

So, you should try to find an ipython version built with VS 2013 or you should find a FreeCAD version built with VS 2008.
wmayer
Site Admin
Posts: 14893
Joined: Thu Feb 19, 2009 10:32 am

Re: Embedding FreeCAD in ipython

Postby wmayer » Thu Dec 24, 2015 10:42 am

Yesterday I downloaded the package winpython which also includes ipython
IPython QtConsole 3.2.1
Python 2.7.10 (default, May 23 2015, 09:44:00) [MSC v.1500 64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.
which I used to test FreeCAD 0.15 with:
Python 2.7.8 (default, Nov 17 2014, 20:37:05) [MSC v.1800 64 bit (AMD64)] on win32
Type 'help', 'copyright', 'credits' or 'license' for more information.
For me it perfectly worked after setting the sys.path to import the FreeCAD module.
iari
Posts: 42
Joined: Mon Nov 18, 2013 8:52 pm

Re: Embedding FreeCAD in ipython

Postby iari » Sat Dec 26, 2015 8:31 am

Using ipython from winpython i's a reasonable solution, and it works!
Thanks
igmar
iari
Posts: 42
Joined: Mon Nov 18, 2013 8:52 pm

Re: [SOLVED] Embedding FreeCAD in ipython

Postby iari » Mon Dec 28, 2015 3:23 pm

Hi,
I successfully import FreeCAD modules in ipython (coming with WinPython-64bit-2.7.10.3). But when I try to use makeBSpline I obtain the following error:

Code: Select all

In [3]: run testFreeCAD
FreeCAD 0.15, Libs: 0.15R4671 (Git)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
C:\****\testFreeCAD.py in <module>()
     47 # Creazione delle facce
     48 for j in range(0,Nc,1):
---> 49         BSpline = Draft.makeBSpline(points[j*Nr:j*Nr+Nr],closed=False,face=False,support=None)
     50         points2=[points[j*Nr+Nr-1],points[j*Nr+Nr-1]-FreeCAD.Vector(0,0,thick),FreeCAD.Vector(0.0,0.0,0.0)-FreeCAD.Vector(0,0,thick),points[j*Nr]-FreeCAD.Vector(0,0,thick),points[j*Nr]]
     51         Wire1 = Draft.makeWire(points2,closed=False,face=True,support=None)

C:\Program Files\FreeCAD 0.15\Mod\Draft\Draft.pyc in makeBSpline(pointslist, closed, placement, face, support)
    841     true (and wire is closed), the wire will appear filled. Instead of
    842     a pointslist, you can also pass a Part Wire.'''
--> 843     from DraftTools import msg,translate
    844     if not isinstance(pointslist,list):
    845         nlist = []

C:\Program Files\FreeCAD 0.15\Mod\Draft\DraftTools.py in <module>()
     34 import os, FreeCAD, FreeCADGui, WorkingPlane, math, re, Draft, Draft_rc, DraftVecUtils
     35 from FreeCAD import Vector
---> 36 from DraftGui import todo,QtCore,QtGui
     37 from DraftSnap import *
     38 from DraftTrackers import *

C:\Program Files\FreeCAD 0.15\Mod\Draft\DraftGui.py in <module>()
   1791
   1792 if not hasattr(FreeCADGui,"draftToolBar"):
-> 1793     FreeCADGui.draftToolBar = DraftToolBar()
   1794 #----End of Python Features Definitions----#
   1795

C:\Program Files\FreeCAD 0.15\Mod\Draft\DraftGui.py in __init__(self)
    235         self.FORMAT = makeFormatSpec(self.DECIMALS,'Length')
    236         self.AFORMAT = makeFormatSpec(self.DECIMALS,'Angle')
--> 237         self.uiloader = FreeCADGui.UiLoader()
    238
    239         if self.taskmode:

AttributeError: 'module' object has no attribute 'UiLoader'
Indeed, I cannot call FreeCADGui.UiLoader() as UiLoader is not present in FreeCADGui functions..

Any idea?
Thanks
igmar

ipython version:
'2.7.10 (default, May 23 2015, 09:44:00) [MSC v.1500 64 bit (AMD64)]

FreeCAD version:
OS: Windows 8
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.15.4671 (Git)
Branch: releases/FreeCAD-0-15
Hash: 244b3aef360841646cbfe80a1b225c8b39c8380c
Python version: 2.7.8
Qt version: 4.8.6
Coin version: 4.0.0a
OCC version: 6.8.0.oce-0.17
wmayer
Site Admin
Posts: 14893
Joined: Thu Feb 19, 2009 10:32 am

Re: [SOLVED] Embedding FreeCAD in ipython

Postby wmayer » Mon Dec 28, 2015 8:38 pm

You have to also import FreeCADGui and then call its showMainWindow(). This way it gets fully initialized.

Code: Select all

import sys
import FreeCAD, FreeCADGui
from PyQt4 import QtGui

app = QtGui.QApplication(sys.argv)
FreeCADGui.showMainWindow()