Extra python modules: Difference between revisions
(add "<syntaxhighlight>" completed documentation in " Usage PySide " and correct links PyQt) |
|||
Line 31: | Line 31: | ||
Once it is installed, you can check that everything is working by typing in FreeCAD python console: |
Once it is installed, you can check that everything is working by typing in FreeCAD python console: |
||
<syntaxhighlight> |
|||
import PySide |
|||
import PySide |
|||
</syntaxhighlight> |
|||
To access the FreeCAD interface, type: |
To access the FreeCAD interface, type : |
||
<syntaxhighlight> |
|||
from PySide import QtCore,QtGui |
|||
from PySide import QtCore,QtGui |
|||
app = QtGui.qApp |
|||
app = QtGui.qApp |
|||
FreeCADWindow = app.activeWindow() |
|||
FreeCADWindow = app.activeWindow() |
|||
</syntaxhighlight> |
|||
if this method does not work, replace with : |
|||
Now you can start to explore the interface with the dir() command. You can add new elements, like a custom widget, with commands like: |
|||
<syntaxhighlight> |
|||
from PySide import QtCore,QtGui |
|||
FreeCADWindow = FreeCADGui.getMainWindow() |
|||
</syntaxhighlight> |
|||
Now you can start to explore the interface with the dir() command. You can add new elements, like a custom widget, with commands like : |
|||
<syntaxhighlight> |
|||
FreeCADWindow.addDockWidget(QtCore.Qt.RghtDockWidgetArea,my_custom_widget) |
|||
</syntaxhighlight> |
|||
Working with Unicode : |
|||
<syntaxhighlight> |
|||
text = text.encode('utf-8') |
|||
</syntaxhighlight> |
|||
Working with QFileDialog and OpenFileName : |
|||
<syntaxhighlight> |
|||
path = FreeCAD.ConfigGet("AppHomePath") |
|||
#path = FreeCAD.ConfigGet("UserAppData") |
|||
OpenName, Filter = PySide.QtGui.QFileDialog.getOpenFileName(None, "Read a txt file", path, "*.txt") |
|||
</syntaxhighlight> |
|||
Working with QFileDialog and SaveFileName : |
|||
<syntaxhighlight> |
|||
path = FreeCAD.ConfigGet("AppHomePath") |
|||
#path = FreeCAD.ConfigGet("UserAppData") |
|||
SaveName, Filter = PySide.QtGui.QFileDialog.getSaveFileName(None, "Save a file txt", path, "*.txt") |
|||
</syntaxhighlight> |
|||
FreeCADWindow.addDockWidget(QtCore.Qt.RghtDockWidgetArea,my_custom_widget) |
|||
=== Additional documentation === |
=== Additional documentation === |
||
Line 47: | Line 83: | ||
Some pyQt4 tutorials (including how to build interfaces with Qt Designer to use with python): |
Some pyQt4 tutorials (including how to build interfaces with Qt Designer to use with python): |
||
* http:// |
* http://pyqt.sourceforge.net/Docs/PyQt4/classes.html - the PyQt4 API Reference on sourceforge |
||
* http://www.rkblog.rk.edu.pl/w/p/introduction-pyqt4/ - a simple introduction |
* http://www.rkblog.rk.edu.pl/w/p/introduction-pyqt4/ - a simple introduction |
||
* http://www.zetcode.com/tutorials/pyqt4/ - very complete in-depth tutorial |
* http://www.zetcode.com/tutorials/pyqt4/ - very complete in-depth tutorial |
||
Line 76: | Line 112: | ||
The best way to compile pivy easily is to grab the debian source package for pivy and make a package with debuild. It is the same source code from the official pivy site, but the debian people made several bug-fixing additions. It also compiles fine on ubuntu karmic: http://packages.debian.org/squeeze/python-pivy (download the .orig.gz and the .diff.gz file, then unzip both, then apply the .diff to the source: go to the unzipped pivy source folder, and apply the .diff patch: |
The best way to compile pivy easily is to grab the debian source package for pivy and make a package with debuild. It is the same source code from the official pivy site, but the debian people made several bug-fixing additions. It also compiles fine on ubuntu karmic: http://packages.debian.org/squeeze/python-pivy (download the .orig.gz and the .diff.gz file, then unzip both, then apply the .diff to the source: go to the unzipped pivy source folder, and apply the .diff patch: |
||
patch -p1 < ../pivy_0.5.0~svn765-2.diff |
|||
<syntaxhighlight> |
|||
patch -p1 < ../pivy_0.5.0~svn765-2.diff |
|||
</syntaxhighlight> |
|||
then |
then |
||
debuild |
|||
<syntaxhighlight> |
|||
debuild |
|||
</syntaxhighlight> |
|||
to have pivy properly built into an official installable package. Then, just install the package with gdebi. |
to have pivy properly built into an official installable package. Then, just install the package with gdebi. |
||
Line 84: | Line 128: | ||
First get the latest sources from the [http://pivy.coin3d.org/mercurial/ project's repository]: |
First get the latest sources from the [http://pivy.coin3d.org/mercurial/ project's repository]: |
||
hg clone http://hg.sim.no/Pivy/default Pivy |
|||
<syntaxhighlight> |
|||
hg clone http://hg.sim.no/Pivy/default Pivy |
|||
</syntaxhighlight> |
|||
As of March 2012, the latest version is Pivy-0.5. |
As of March 2012, the latest version is Pivy-0.5. |
||
Then you need a tool called SWIG to generate the C++ code for the Python bindings. Pivy-0.5 reports that it has only been tested with SWIG 1.3.31, 1.3.33, 1.3.35, and 1.3.40. So you can download a source tarball for one of these old versions from [http://www.swig.org http://www.swig.org]. Then unpack it and from a command line do (as root): |
Then you need a tool called SWIG to generate the C++ code for the Python bindings. Pivy-0.5 reports that it has only been tested with SWIG 1.3.31, 1.3.33, 1.3.35, and 1.3.40. So you can download a source tarball for one of these old versions from [http://www.swig.org http://www.swig.org]. Then unpack it and from a command line do (as root): |
||
./configure |
|||
<syntaxhighlight> |
|||
make |
|||
./configure |
|||
make install (or checkinstall if you use it) |
|||
make |
|||
make install (or checkinstall if you use it) |
|||
</syntaxhighlight> |
|||
It takes just a few seconds to build. |
It takes just a few seconds to build. |
||
Line 96: | Line 148: | ||
After that go to the pivy sources and call |
After that go to the pivy sources and call |
||
python setup.py build |
|||
<syntaxhighlight> |
|||
python setup.py build |
|||
</syntaxhighlight> |
|||
which creates the source files. Note that build can produce thousands of warnings, but hopefully there will be no errors. |
which creates the source files. Note that build can produce thousands of warnings, but hopefully there will be no errors. |
||
Line 102: | Line 158: | ||
After that, install by issuing (as root): |
After that, install by issuing (as root): |
||
python setup.py install (or checkinstall python setup.py install) |
|||
<syntaxhighlight> |
|||
python setup.py install (or checkinstall python setup.py install) |
|||
</syntaxhighlight> |
|||
That's it, pivy is installed. |
That's it, pivy is installed. |
||
Line 110: | Line 170: | ||
As for linux, get the latest source: |
As for linux, get the latest source: |
||
hg clone http://hg.sim.no/Pivy/default Pivy |
|||
<syntaxhighlight> |
|||
hg clone http://hg.sim.no/Pivy/default Pivy |
|||
</syntaxhighlight> |
|||
If you don't have hg, you can get it from MacPorts: |
If you don't have hg, you can get it from MacPorts: |
||
port install mercurial |
|||
<syntaxhighlight> |
|||
port install mercurial |
|||
</syntaxhighlight> |
|||
Then, as above you need SWIG. It should be a matter of: |
Then, as above you need SWIG. It should be a matter of: |
||
port install swig |
|||
<syntaxhighlight> |
|||
port install swig |
|||
</syntaxhighlight> |
|||
I found I needed also: |
I found I needed also: |
||
port install swig-python |
|||
<syntaxhighlight> |
|||
port install swig-python |
|||
</syntaxhighlight> |
|||
As of March 2012, MacPorts SWIG is version 2.0.4. As noted above for linux, you might be better off downloading an older version. SWIG 2.0.4 seems to have a bug that stops Pivy building. See first message in this digest: https://sourceforge.net/mailarchive/message.php?msg_id=28114815 |
As of March 2012, MacPorts SWIG is version 2.0.4. As noted above for linux, you might be better off downloading an older version. SWIG 2.0.4 seems to have a bug that stops Pivy building. See first message in this digest: https://sourceforge.net/mailarchive/message.php?msg_id=28114815 |
||
This can be corrected by editing the 2 source locations to add dereferences: *arg4, *arg5 in place of arg4, arg5. Now Pivy should build: |
This can be corrected by editing the 2 source locations to add dereferences: *arg4, *arg5 in place of arg4, arg5. Now Pivy should build: |
||
python setup.py build |
|||
<syntaxhighlight> |
|||
sudo python setup.py install |
|||
python setup.py build |
|||
sudo python setup.py install |
|||
</syntaxhighlight> |
|||
==== Windows ==== |
==== Windows ==== |
||
Line 130: | Line 206: | ||
Assuming you are using Visual Studio 2005 or later you should open a command prompt with 'Visual Studio 2005 Command prompt' from the Tools menu. |
Assuming you are using Visual Studio 2005 or later you should open a command prompt with 'Visual Studio 2005 Command prompt' from the Tools menu. |
||
If the Python interpreter is not yet in the system path do |
If the Python interpreter is not yet in the system path do |
||
set PATH=path_to_python_2.5;%PATH% |
|||
<syntaxhighlight> |
|||
set PATH=path_to_python_2.5;%PATH% |
|||
</syntaxhighlight> |
|||
To get pivy working you should get the latest sources from the project's repository: |
To get pivy working you should get the latest sources from the project's repository: |
||
svn co https://svn.coin3d.org/repos/Pivy/trunk Pivy |
|||
<syntaxhighlight> |
|||
svn co https://svn.coin3d.org/repos/Pivy/trunk Pivy |
|||
</syntaxhighlight> |
|||
Then you need a tool called SWIG to generate the C++ code for the Python bindings. It is recommended to use version 1.3.25 of SWIG, not the latest version, because at the moment pivy will only function correctly with 1.3.25. Download the binaries for 1.3.25 from [http://www.swig.org http://www.swig.org]. Then unpack it and from the command line add it to the system path |
Then you need a tool called SWIG to generate the C++ code for the Python bindings. It is recommended to use version 1.3.25 of SWIG, not the latest version, because at the moment pivy will only function correctly with 1.3.25. Download the binaries for 1.3.25 from [http://www.swig.org http://www.swig.org]. Then unpack it and from the command line add it to the system path |
||
set PATH=path_to_swig_1.3.25;%PATH% |
|||
<syntaxhighlight> |
|||
set PATH=path_to_swig_1.3.25;%PATH% |
|||
</syntaxhighlight> |
|||
and set COINDIR to the appropriate path |
and set COINDIR to the appropriate path |
||
set COINDIR=path_to_coin |
|||
<syntaxhighlight> |
|||
set COINDIR=path_to_coin |
|||
</syntaxhighlight> |
|||
On Windows the pivy config file expects SoWin instead of SoQt as default. I didn't find an obvious way to build with SoQt, so I modified the file setup.py directly. |
On Windows the pivy config file expects SoWin instead of SoQt as default. I didn't find an obvious way to build with SoQt, so I modified the file setup.py directly. |
||
Line 143: | Line 233: | ||
After that go to the pivy sources and call |
After that go to the pivy sources and call |
||
python setup.py build |
|||
<syntaxhighlight> |
|||
python setup.py build |
|||
</syntaxhighlight> |
|||
which creates the source files. You may run into a compiler error several header files couldn't be found. In this case adjust the INCLUDE variable |
which creates the source files. You may run into a compiler error several header files couldn't be found. In this case adjust the INCLUDE variable |
||
set INCLUDE=%INCLUDE%;path_to_coin_include_dir |
|||
<syntaxhighlight> |
|||
set INCLUDE=%INCLUDE%;path_to_coin_include_dir |
|||
</syntaxhighlight> |
|||
and if the SoQt headers are not in the same place as the Coin headers also |
and if the SoQt headers are not in the same place as the Coin headers also |
||
set INCLUDE=%INCLUDE%;path_to_soqt_include_dir |
|||
<syntaxhighlight> |
|||
set INCLUDE=%INCLUDE%;path_to_soqt_include_dir |
|||
</syntaxhighlight> |
|||
and finally the Qt headers |
and finally the Qt headers |
||
set INCLUDE=%INCLUDE%;path_to_qt4\include\Qt |
|||
<syntaxhighlight> |
|||
set INCLUDE=%INCLUDE%;path_to_qt4\include\Qt |
|||
</syntaxhighlight> |
|||
If you are using the Express Edition of Visual Studio you may get a python keyerror exception. |
If you are using the Express Edition of Visual Studio you may get a python keyerror exception. |
||
Line 155: | Line 260: | ||
Go to line 122 and replace the line |
Go to line 122 and replace the line |
||
vsbase = r"Software\Microsoft\VisualStudio\%0.1f" % version |
|||
<syntaxhighlight> |
|||
vsbase = r"Software\Microsoft\VisualStudio\%0.1f" % version |
|||
</syntaxhighlight> |
|||
with |
with |
||
vsbase = r"Software\Microsoft\VCExpress\%0.1f" % version |
|||
<syntaxhighlight> |
|||
vsbase = r"Software\Microsoft\VCExpress\%0.1f" % version |
|||
</syntaxhighlight> |
|||
Then retry again. |
Then retry again. |
||
If you get a second error like |
If you get a second error like |
||
error: Python was built with Visual Studio 2003;... |
|||
<syntaxhighlight> |
|||
error: Python was built with Visual Studio 2003;... |
|||
</syntaxhighlight> |
|||
you must also replace line 128 |
you must also replace line 128 |
||
self.set_macro("FrameworkSDKDir", net, "sdkinstallrootv1.1") |
|||
<syntaxhighlight> |
|||
self.set_macro("FrameworkSDKDir", net, "sdkinstallrootv1.1") |
|||
</syntaxhighlight> |
|||
with |
with |
||
self.set_macro("FrameworkSDKDir", net, "sdkinstallrootv2.0") |
|||
<syntaxhighlight> |
|||
self.set_macro("FrameworkSDKDir", net, "sdkinstallrootv2.0") |
|||
</syntaxhighlight> |
|||
Retry once again. If you get again an error like |
Retry once again. If you get again an error like |
||
error: Python was built with Visual Studio version 8.0, and extensions need to be built with the same version of the compiler, but it isn't installed. |
|||
<syntaxhighlight> |
|||
error: Python was built with Visual Studio version 8.0, and extensions need to be built with the same version of the compiler, but it isn't installed. |
|||
</syntaxhighlight> |
|||
then you should check the environment variables DISTUTILS_USE_SDK and MSSDK with |
then you should check the environment variables DISTUTILS_USE_SDK and MSSDK with |
||
echo %DISTUTILS_USE_SDK% |
|||
<syntaxhighlight> |
|||
echo %MSSDK% |
|||
echo %DISTUTILS_USE_SDK% |
|||
echo %MSSDK% |
|||
</syntaxhighlight> |
|||
If not yet set then just set it e.g. to 1 |
If not yet set then just set it e.g. to 1 |
||
set DISTUTILS_USE_SDK=1 |
|||
<syntaxhighlight> |
|||
set MSSDK=1 |
|||
set DISTUTILS_USE_SDK=1 |
|||
set MSSDK=1 |
|||
</syntaxhighlight> |
|||
Now, you may run into a compiler error where a 'const char*' cannot be converted in a 'char*'. To fix that you just need to write a 'const' before in the appropriate lines. There are six lines to fix. |
Now, you may run into a compiler error where a 'const char*' cannot be converted in a 'char*'. To fix that you just need to write a 'const' before in the appropriate lines. There are six lines to fix. |
||
Line 181: | Line 317: | ||
To check if Pivy is correctly installed: |
To check if Pivy is correctly installed: |
||
<syntaxhighlight> |
|||
import pivy |
|||
import pivy |
|||
</syntaxhighlight> |
|||
To have Pivy access the FreeCAD scenegraph do the following: |
To have Pivy access the FreeCAD scenegraph do the following: |
||
<syntaxhighlight> |
|||
from pivy import coin |
|||
from pivy import coin |
|||
App.newDocument() # Open a document and a view |
|||
App.newDocument() # Open a document and a view |
|||
view = Gui.ActiveDocument.ActiveView |
|||
view = Gui.ActiveDocument.ActiveView |
|||
FCSceneGraph = view.getSceneGraph() # returns a pivy Python object that holds a SoSeparator, the main "container" of the Coin scenegraph |
|||
FCSceneGraph = view.getSceneGraph() # returns a pivy Python object that holds a SoSeparator, the main "container" of the Coin scenegraph |
|||
FCSceneGraph.addChild(coin.SoCube()) # add a box to scene |
|||
FCSceneGraph.addChild(coin.SoCube()) # add a box to scene |
|||
</syntaxhighlight> |
|||
You can now explore the FCSceneGraph with the dir() command. |
You can now explore the FCSceneGraph with the dir() command. |
||
Line 197: | Line 337: | ||
Unfortunately documentation about pivy is still almost inexistant on the net. But you might find Coin documentation useful, since pivy simply translate Coin functions, nodes and methods in python, everything keeps the same name and properties, keeping in mind the difference of syntax between C and python: |
Unfortunately documentation about pivy is still almost inexistant on the net. But you might find Coin documentation useful, since pivy simply translate Coin functions, nodes and methods in python, everything keeps the same name and properties, keeping in mind the difference of syntax between C and python: |
||
* |
* https://bitbucket.org/Coin3D/coin/wiki/Documentation - Coin3D API Reference |
||
* http://www-evasion.imag.fr/~Francois.Faure/doc/inventorMentor/sgi_html/index.html - The Inventor Mentor - The "bible" of Inventor scene description language. |
* http://www-evasion.imag.fr/~Francois.Faure/doc/inventorMentor/sgi_html/index.html - The Inventor Mentor - The "bible" of Inventor scene description language. |
||
Line 218: | Line 358: | ||
In either case, you'll need the following packages already installed on your system: |
In either case, you'll need the following packages already installed on your system: |
||
<syntaxhighlight> |
|||
python-lxml |
|||
python-lxml |
|||
python-numpy |
|||
python-dateutil |
|||
</syntaxhighlight> |
|||
===== From the git repository ===== |
===== From the git repository ===== |
||
<syntaxhighlight> |
|||
git clone git://github.com/pycollada/pycollada.git pycollada |
|||
git clone git://github.com/pycollada/pycollada.git pycollada |
|||
cd pycollada |
|||
cd pycollada |
|||
sudo python setup.py install |
|||
sudo python setup.py install |
|||
</syntaxhighlight> |
|||
===== With easy_install ===== |
===== With easy_install ===== |
||
Line 232: | Line 377: | ||
Assuming you have a complete python installation already, the easy_install utility should be present already: |
Assuming you have a complete python installation already, the easy_install utility should be present already: |
||
<syntaxhighlight> |
|||
easy_install pycollada |
|||
easy_install pycollada |
|||
</syntaxhighlight> |
|||
You can check if pycollada was correctly installed by issuing in a python console: |
You can check if pycollada was correctly installed by issuing in a python console: |
||
<syntaxhighlight> |
|||
import collada |
|||
import collada |
|||
</syntaxhighlight> |
|||
If it returns nothing (no error message), then all is OK |
If it returns nothing (no error message), then all is OK |
||
Line 266: | Line 415: | ||
You will need a couple of development packages installed on your system in order to compile ifcopenshell: |
You will need a couple of development packages installed on your system in order to compile ifcopenshell: |
||
<syntaxhighlight> |
|||
liboce-*-dev |
|||
liboce-*-dev |
|||
python-dev |
|||
swig |
|||
swig |
|||
</syntaxhighlight> |
|||
but since FreeCAD requires all of them too, if you can compile FreeCAD, you won't need any extra dependency to compile IfcOpenShell. |
but since FreeCAD requires all of them too, if you can compile FreeCAD, you won't need any extra dependency to compile IfcOpenShell. |
||
Line 274: | Line 425: | ||
Grab the latest source code from here: |
Grab the latest source code from here: |
||
<syntaxhighlight> |
|||
svn co https://ifcopenshell.svn.sourceforge.net/svnroot/ifcopenshell ifcopenshell |
|||
svn co https://ifcopenshell.svn.sourceforge.net/svnroot/ifcopenshell ifcopenshell |
|||
</syntaxhighlight> |
|||
The build process is very easy: |
The build process is very easy: |
||
<syntaxhighlight> |
|||
mkdir ifcopenshell-build |
|||
mkdir ifcopenshell-build |
|||
cd ifcopenshell-build |
|||
cmake ../ifcopenshell/cmake |
|||
</syntaxhighlight> |
|||
or, if you are using oce instead of opencascade: |
or, if you are using oce instead of opencascade: |
||
<syntaxhighlight> |
|||
cmake -DOCC_INCLUDE_DIR=/usr/include/oce ../ifcopenshell/cmake |
|||
cmake -DOCC_INCLUDE_DIR=/usr/include/oce ../ifcopenshell/cmake |
|||
</syntaxhighlight> |
|||
Since ifcopenshell is made primarily for Blender, it uses python3 by default. To use it inside FreeCAD, you need to compile it against the same version of python that is used by FreeCAD. So you might need to force the python version with additional cmake parameters (adjust the python version to yours): |
Since ifcopenshell is made primarily for Blender, it uses python3 by default. To use it inside FreeCAD, you need to compile it against the same version of python that is used by FreeCAD. So you might need to force the python version with additional cmake parameters (adjust the python version to yours): |
||
<syntaxhighlight> |
|||
cmake -DOCC_INCLUDE_DIR=/usr/include/oce -DPYTHON_INCLUDE_DIR=/usr/include/python2.7 -DPYTHON_LIBRARY=/usr/lib/python2.7.so ../ifcopenshell/cmake |
|||
cmake -DOCC_INCLUDE_DIR=/usr/include/oce -DPYTHON_INCLUDE_DIR=/usr/include/python2.7 -DPYTHON_LIBRARY=/usr/lib/python2.7.so ../ifcopenshell/cmake |
|||
</syntaxhighlight> |
|||
Then: |
Then: |
||
<syntaxhighlight> |
|||
make |
|||
make |
|||
sudo make install |
|||
</syntaxhighlight> |
|||
You can check that ifcopenshell was correctly installed by issuing in a python console: |
You can check that ifcopenshell was correctly installed by issuing in a python console: |
||
<syntaxhighlight> |
|||
import IfcImport |
|||
import IfcImport |
|||
</syntaxhighlight> |
|||
If it returns nothing (no error message), then all is OK |
If it returns nothing (no error message), then all is OK |
Revision as of 17:58, 22 July 2014
This page lists several additional python modules or other pieces of software that can be downloaded freely from the internet, and add functionality to your FreeCAD installation.
PySide (previously PyQt4)
- homepage (PySide): http://qt-project.org/wiki/PySide
- license: LGPL
- optional, but needed by several modules: Draft, Arch, Ship, Plot, OpenSCAD, Spreadsheet
PySide (previously PyQt) is required by several modules of FreeCAD to access FreeCAD's Qt interface. It is already bundled in the windows verison of FreeCAD, and is usually installed automatically by FreeCAD on Linux, when installing from official repositories. If those modules (Draft, Arch, etc) are enabled after FreeCAD is installed, it means PySide (previously PyQt) is already there, and you don't need to do anything more.
Note:
FreeCAD progressively moved away from PyQt after version 0.13, in favour of PySide, which does exactly the same job but has a license (LGPL) more compatible with FreeCAD.
Installation
Linux
The simplest way to install PySide is through your distribution's package manager. On Debian/Ubuntu systems, the package name is generally python-PySide, while on RPM-based systems it is named pyside. The necessary dependencies (Qt and SIP) will be taken care of automatically.
Windows
The program can be downloaded from http://qt-project.org/wiki/Category:LanguageBindings::PySide::Downloads . You'll need to install the Qt and SIP libraries before installing PySide (to be documented).
MacOSX
PyQt on Mac can be installed via homebrew or port. See CompileOnMac#Install_Dependencies for more information.
Usage
Once it is installed, you can check that everything is working by typing in FreeCAD python console:
import PySide
To access the FreeCAD interface, type :
from PySide import QtCore,QtGui
app = QtGui.qApp
FreeCADWindow = app.activeWindow()
if this method does not work, replace with :
from PySide import QtCore,QtGui
FreeCADWindow = FreeCADGui.getMainWindow()
Now you can start to explore the interface with the dir() command. You can add new elements, like a custom widget, with commands like :
FreeCADWindow.addDockWidget(QtCore.Qt.RghtDockWidgetArea,my_custom_widget)
Working with Unicode :
text = text.encode('utf-8')
Working with QFileDialog and OpenFileName :
path = FreeCAD.ConfigGet("AppHomePath")
#path = FreeCAD.ConfigGet("UserAppData")
OpenName, Filter = PySide.QtGui.QFileDialog.getOpenFileName(None, "Read a txt file", path, "*.txt")
Working with QFileDialog and SaveFileName :
path = FreeCAD.ConfigGet("AppHomePath")
#path = FreeCAD.ConfigGet("UserAppData")
SaveName, Filter = PySide.QtGui.QFileDialog.getSaveFileName(None, "Save a file txt", path, "*.txt")
Additional documentation
Some pyQt4 tutorials (including how to build interfaces with Qt Designer to use with python):
- http://pyqt.sourceforge.net/Docs/PyQt4/classes.html - the PyQt4 API Reference on sourceforge
- http://www.rkblog.rk.edu.pl/w/p/introduction-pyqt4/ - a simple introduction
- http://www.zetcode.com/tutorials/pyqt4/ - very complete in-depth tutorial
Pivy
- homepage: https://bitbucket.org/Coin3D/coin/wiki/Home
- license: BSD
- optional, but needed by several modules of FreeCAD: Draft, Arch
Pivy is a needed by several modules to access the 3D view of FreeCAD. On windows, Pivy is already bundled inside the FreeCAD installer, and on Linux it is usually automatically installed when you install FreeCAD from an official repository. On MacOSX, unfortunately, you will need to compile pivy yourself.
Installation
Prerequisites
I believe before compiling Pivy you will want to have Coin and SoQt installed.
I found for building on Mac it was sufficient to install the Coin3 binary package. Attempting to install coin from MacPorts was problematic: tried to add a lot of X Windows packages and ultimately crashed with a script error.
For Fedora I found an RPM with Coin3.
SoQt compiled from source fine on Mac and Linux.
Debian & Ubuntu
Starting with Debian Squeeze and Ubuntu Lucid, pivy will be available directly from the official repositories, saving us a lot of hassle. In the meantime, you can either download one of the packages we made (for debian and ubuntu karmic) availables on the Download pages, or compile it yourself.
The best way to compile pivy easily is to grab the debian source package for pivy and make a package with debuild. It is the same source code from the official pivy site, but the debian people made several bug-fixing additions. It also compiles fine on ubuntu karmic: http://packages.debian.org/squeeze/python-pivy (download the .orig.gz and the .diff.gz file, then unzip both, then apply the .diff to the source: go to the unzipped pivy source folder, and apply the .diff patch:
patch -p1 < ../pivy_0.5.0~svn765-2.diff
then
debuild
to have pivy properly built into an official installable package. Then, just install the package with gdebi.
Other linux distributions
First get the latest sources from the project's repository:
hg clone http://hg.sim.no/Pivy/default Pivy
As of March 2012, the latest version is Pivy-0.5.
Then you need a tool called SWIG to generate the C++ code for the Python bindings. Pivy-0.5 reports that it has only been tested with SWIG 1.3.31, 1.3.33, 1.3.35, and 1.3.40. So you can download a source tarball for one of these old versions from http://www.swig.org. Then unpack it and from a command line do (as root):
./configure
make
make install (or checkinstall if you use it)
It takes just a few seconds to build.
Alternatively, you can try building with a more recent SWIG. As of March 2012, a typical repository version is 2.0.4. Pivy has a minor compile problem with SWIG 2.0.4 on Mac OS (see below) but seems to build fine on Fedora Core 15.
After that go to the pivy sources and call
python setup.py build
which creates the source files. Note that build can produce thousands of warnings, but hopefully there will be no errors.
This is probably obsolete, but you may run into a compiler error where a 'const char*' cannot be converted in a 'char*'. To fix that you just need to write a 'const' before in the appropriate lines. There are six lines to fix.
After that, install by issuing (as root):
python setup.py install (or checkinstall python setup.py install)
That's it, pivy is installed.
Mac OS
These instructions may not be complete. Something close to this worked for OS 10.7 as of March 2012. I use MacPorts for repositories, but other options should also work.
As for linux, get the latest source:
hg clone http://hg.sim.no/Pivy/default Pivy
If you don't have hg, you can get it from MacPorts:
port install mercurial
Then, as above you need SWIG. It should be a matter of:
port install swig
I found I needed also:
port install swig-python
As of March 2012, MacPorts SWIG is version 2.0.4. As noted above for linux, you might be better off downloading an older version. SWIG 2.0.4 seems to have a bug that stops Pivy building. See first message in this digest: https://sourceforge.net/mailarchive/message.php?msg_id=28114815
This can be corrected by editing the 2 source locations to add dereferences: *arg4, *arg5 in place of arg4, arg5. Now Pivy should build:
python setup.py build
sudo python setup.py install
Windows
Assuming you are using Visual Studio 2005 or later you should open a command prompt with 'Visual Studio 2005 Command prompt' from the Tools menu. If the Python interpreter is not yet in the system path do
set PATH=path_to_python_2.5;%PATH%
To get pivy working you should get the latest sources from the project's repository:
svn co https://svn.coin3d.org/repos/Pivy/trunk Pivy
Then you need a tool called SWIG to generate the C++ code for the Python bindings. It is recommended to use version 1.3.25 of SWIG, not the latest version, because at the moment pivy will only function correctly with 1.3.25. Download the binaries for 1.3.25 from http://www.swig.org. Then unpack it and from the command line add it to the system path
set PATH=path_to_swig_1.3.25;%PATH%
and set COINDIR to the appropriate path
set COINDIR=path_to_coin
On Windows the pivy config file expects SoWin instead of SoQt as default. I didn't find an obvious way to build with SoQt, so I modified the file setup.py directly. In line 200 just remove the part 'sowin' : ('gui._sowin', 'sowin-config', 'pivy.gui.') (do not remove the closing parenthesis).
After that go to the pivy sources and call
python setup.py build
which creates the source files. You may run into a compiler error several header files couldn't be found. In this case adjust the INCLUDE variable
set INCLUDE=%INCLUDE%;path_to_coin_include_dir
and if the SoQt headers are not in the same place as the Coin headers also
set INCLUDE=%INCLUDE%;path_to_soqt_include_dir
and finally the Qt headers
set INCLUDE=%INCLUDE%;path_to_qt4\include\Qt
If you are using the Express Edition of Visual Studio you may get a python keyerror exception. In this case you have to modify a few things in msvccompiler.py located in your python installation.
Go to line 122 and replace the line
vsbase = r"Software\Microsoft\VisualStudio\%0.1f" % version
with
vsbase = r"Software\Microsoft\VCExpress\%0.1f" % version
Then retry again. If you get a second error like
error: Python was built with Visual Studio 2003;...
you must also replace line 128
self.set_macro("FrameworkSDKDir", net, "sdkinstallrootv1.1")
with
self.set_macro("FrameworkSDKDir", net, "sdkinstallrootv2.0")
Retry once again. If you get again an error like
error: Python was built with Visual Studio version 8.0, and extensions need to be built with the same version of the compiler, but it isn't installed.
then you should check the environment variables DISTUTILS_USE_SDK and MSSDK with
echo %DISTUTILS_USE_SDK%
echo %MSSDK%
If not yet set then just set it e.g. to 1
set DISTUTILS_USE_SDK=1
set MSSDK=1
Now, you may run into a compiler error where a 'const char*' cannot be converted in a 'char*'. To fix that you just need to write a 'const' before in the appropriate lines. There are six lines to fix. After that copy the generated pivy directory to a place where the python interpreter in FreeCAD can find it.
Usage
To check if Pivy is correctly installed:
import pivy
To have Pivy access the FreeCAD scenegraph do the following:
from pivy import coin
App.newDocument() # Open a document and a view
view = Gui.ActiveDocument.ActiveView
FCSceneGraph = view.getSceneGraph() # returns a pivy Python object that holds a SoSeparator, the main "container" of the Coin scenegraph
FCSceneGraph.addChild(coin.SoCube()) # add a box to scene
You can now explore the FCSceneGraph with the dir() command.
Additonal Documentation
Unfortunately documentation about pivy is still almost inexistant on the net. But you might find Coin documentation useful, since pivy simply translate Coin functions, nodes and methods in python, everything keeps the same name and properties, keeping in mind the difference of syntax between C and python:
- https://bitbucket.org/Coin3D/coin/wiki/Documentation - Coin3D API Reference
- http://www-evasion.imag.fr/~Francois.Faure/doc/inventorMentor/sgi_html/index.html - The Inventor Mentor - The "bible" of Inventor scene description language.
You can also look at the Draft.py file in the FreeCAD Mod/Draft folder, since it makes big use of pivy.
pyCollada
- homepage: http://pycollada.github.com
- license: BSD
- optional, needed to enable import and export of Collada (.DAE) files
pyCollada is a python library that allow programs to read and write Collada (*.DAE) files. When pyCollada is installed on your system, FreeCAD will be able to handle importing and exporting in the Collada file format.
Installation
Pycollada is usually not yet available in linux distributions repositories, but since it is made only of python files, it doesn't require compilation, and is easy to install. You have 2 ways, or directly from the official pycollada git repository, or with the easy_install tool.
Linux
In either case, you'll need the following packages already installed on your system:
python-lxml
python-numpy
python-dateutil
From the git repository
git clone git://github.com/pycollada/pycollada.git pycollada
cd pycollada
sudo python setup.py install
With easy_install
Assuming you have a complete python installation already, the easy_install utility should be present already:
easy_install pycollada
You can check if pycollada was correctly installed by issuing in a python console:
import collada
If it returns nothing (no error message), then all is OK
Windows
To Be Documented
Reference on how to use easy_install: http://jishus.org/?p=452
Mac OS
To Be Documented
IfcOpenShell
- homepage: http://www.ifcopenshell.org
- license: LGPL
- optional, needed to extend import abilities of IFC files
IFCOpenShell is a library currently in development, that allows to import (and soon export) Industry foundation Classes (*.IFC) files. IFC is an extension to the STEP format, and is becoming the standard in BIM workflows. When ifcopenshell is correctly installed on your system, the FreeCAD Arch Module will detect it and use it to import IFC files, instead of its built-in rudimentary importer. Since ifcopenshell is based on OpenCasCade, like FreeCAD, the quality of the import is very high, producing high-quality solid geometry.
Installation
Since ifcopenshell is pretty new, you'll likely need to compile it yourself.
Linux
You will need a couple of development packages installed on your system in order to compile ifcopenshell:
liboce-*-dev
python-dev
swig
but since FreeCAD requires all of them too, if you can compile FreeCAD, you won't need any extra dependency to compile IfcOpenShell.
Grab the latest source code from here:
svn co https://ifcopenshell.svn.sourceforge.net/svnroot/ifcopenshell ifcopenshell
The build process is very easy:
mkdir ifcopenshell-build
cd ifcopenshell-build
cmake ../ifcopenshell/cmake
or, if you are using oce instead of opencascade:
cmake -DOCC_INCLUDE_DIR=/usr/include/oce ../ifcopenshell/cmake
Since ifcopenshell is made primarily for Blender, it uses python3 by default. To use it inside FreeCAD, you need to compile it against the same version of python that is used by FreeCAD. So you might need to force the python version with additional cmake parameters (adjust the python version to yours):
cmake -DOCC_INCLUDE_DIR=/usr/include/oce -DPYTHON_INCLUDE_DIR=/usr/include/python2.7 -DPYTHON_LIBRARY=/usr/lib/python2.7.so ../ifcopenshell/cmake
Then:
make
sudo make install
You can check that ifcopenshell was correctly installed by issuing in a python console:
import IfcImport
If it returns nothing (no error message), then all is OK
Windows
Copied from the IfcOpenShell README file
Users are advised to use the Visual Studio .sln file in the win/ folder. For Windows users a prebuilt Open CASCADE version is available from the http://opencascade.org website. Download and install this version and provide the paths to the Open CASCADE header and library files to MS Visual Studio C++.
For building the IfcPython wrapper, SWIG needs to be installed. Please download the latest swigwin version from http://www.swig.org/download.html . After extracting the .zip file, please add the extracted folder to the PATH environment variable. Python needs to be installed, please provide the include and library paths to Visual Studio.
Teigha Converter
- homepage: http://www.opendesign.com/guestfiles/TeighaFileConverter
- license: freeware
- optional, used to enable import and export of DWG files
The Teigha Converter is a small freely available utility that allows to convert between several versions of DWG and DXF files. FreeCAD can use it to offer DWG import and export, by converting DWG files to the DXF format under the hood,then using its standard DXF importer to import the file contents. The restrictions of the DXF importer apply.
Installation
On all platforms, only by installing the appropriate package from http://www.opendesign.com/guestfiles/TeighaFileConverter . After installation, if the utility is not found automatically by FreeCAD, you might need to set the path to the converter executable manually, in the menu Edit -> Preferences -> Draft -> Import/Export options.