Embedding FreeCAD

FreeCAD has the amazing ability to be importable as a python module in other programs or in a standalone python console, together with all its modules and components. Keep in mind that the FreeCAD interface won't be available, but most modules are also separated into GUI and non-GUI parts, so almost all functionality that is not directly related to the interface will be available.

One first, direct, easy and useful application you can make of this is to import FreeCAD documents into your program. In the following example, we'll import the Part geometry of a FreeCAD document into blender. Here is the complete script. I hope you'll be impressed by its simplicity:

FREECADPATH = '/opt/FreeCAD/lib' # path to your FreeCAD.so or FreeCAD.dll file import Blender, sys sys.path.append(FREECADPATH) def import_fcstd(filename): try: import FreeCAD except ValueError: Blender.Draw.PupMenu('Error%t|FreeCAD library not found. Please check the FREECADPATH variable in the import script is correct') else: scene = Blender.Scene.GetCurrent import Part doc = FreeCAD.open(filename) objects = doc.Objects for ob in objects: if ob.Type[:4] == 'Part': shape = ob.Shape if shape.Faces: mesh = Blender.Mesh.New rawdata = shape.tessellate(1) for v in rawdata[0]: mesh.verts.append((v.x,v.y,v.z)) for f in rawdata[1]: mesh.faces.append.append(f) scene.objects.new(mesh,ob.Name) Blender.Redraw def main: Blender.Window.FileSelector(import_fcstd, 'IMPORT FCSTD',                        Blender.sys.makename(ext='.fcstd')) if __name__=='__main__': main
 * 1) This lets you import the script without running it

The first, important part is to make sure python will find our FreeCAD library. Once it finds it, all FreeCAD modules such as Part, that we'll use too, will be available automatically. So we simply take the sys.path variable, which is where python searches for modules, and we append the FreeCAD lib path. This modification is only temporary, and will be lost when we'll close our python interpreter. Another way could be making a link to your FreeCAD library in one of the python search paths. I kept the path in a constant (FREECADPATH) so it'll be easier for another user of the script to configure it to his own system.

Once we are sure the library is loaded (the try/except sequence), we can now work with FreeCAD, the same way as we would inside FreeCAD's own python interpreter. We open the FreeCAD document that is passed to us by the main function, and we make a list of its objects. Then, as we choosed only to care about Part geometry, we check if the Type property of each object contains "Part", then we tesselate it.

The tesselation produce a list of vertices and a list of faces defined by vertices indexes. This is perfect, since it is exactly the same way as blender defines meshes. So, our task is ridiculously simple, we just add both lists contents to the verts and faces of a blender mesh. When everything is done, we just redraw the screen, and that's it!

Of course this script is very simple (in fact I made a more advanced here), you might want to extend it, for example importing mesh objects too, or importing Part geometry that has no faces, or import other file formats that FreeCAD can read. You might also want to export geometry to a FreeCAD document, which can be done the same way. You might also want to build a dialog, so the user can choose what to import, etc... The beauty of all this actually lies in the fact that you let FreeCAD do the ground work while presenting its results in the program of your choice.