Embedding FreeCAD/ro

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. It's even possible to import the FreeCAD GUI as python module -- with some restrictions, however.

FreeCAD a la capacité incroyable de pouvoir être importé en tant que module Python dans d'autres programmes ou, dans une console Python autonome, avec tous ses modules et ses composants. Il est même possible d'importer l'interface graphique (GUI) de FreeCAD en tant que module python avec toutefois, quelques restrictions. Mais avec quelques restrictions.

FreeCAD are capacitatea uimitoare de a fi importabil ca modul python în alte programe sau într-o consolă python autonomă, împreună cu toate modulele și componentele sale. Este posibil chiar să importați interfața grafică GUI-ul FreeCAD ca modul python - cu some restrictions, dar cu anumite restricții.

Using FreeCAD without GUI
O primă aplicație directă, ușoară și utilă pe care o puteți face este importul documentelor FreeCAD în programul dvs. În următorul exemplu, vom importa geometria pieselor unui document FreeCAD în blender. Iată scenariul complet. Sper că veți fi impresionat de simplitatea sa: Prima parte importantă este să vă asigurați că Python va găsi biblioteca noastră FreeCAD. Odată ce o găsește, toate modulele FreeCAD, cum ar fi Part, pe care o vom folosi, vor fi disponibile automat. Așa că luăm pur și simplu variabila sys.path, care este unde Python caută module și adăugăm calea FreCAD.Lib. Această modificare este doar temporară și se va pierde atunci când închidem interpretorul nostru python. Un alt mod ar putea fi să faceți o legătură cu biblioteca dvs. FreeCAD într-una din căile de căutare python. Am păstrat calea într-o constantă (FREECADPATH) astfel încât un alt utilizator al scriptului îi va fi mai ușor să configureze propriul sistem

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 chose 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.

Using FreeCAD with GUI
From version 4.2 on Qt has the intriguing ability to embed Qt-GUI-dependent plugins into non-Qt host applications and share the host's event loop.

Especially, for FreeCAD this means that it can be imported from within another application with its whole user interface where the host application has full control over FreeCAD, then.

The whole python code to achieve that has only two lines

If the host application is based on Qt then this solution should work on all platforms which Qt supports. However, the host should link the same Qt version as FreeCAD because otherwise you could run into unexpected runtime errors.

For non-Qt applications, however, there are a few limitations you must be aware of. This solution probably doesn't work together with all other toolkits. For Windows it works as long as the host application is directly based on Win32 or any other toolkit that internally uses the Win32 API such as wxWidgets, MFC or WinForms. In order to get it working under X11 the host application must link the "glib" library.

Note, for any console application this solution of course doesn't work because there is no event loop running.

Caveats
Although it is possible to import FreeCAD to an external Python interpreter, this is not a common usage scenario and requires some care. Generally, it is better to use the Python included with FreeCAD, run FreeCAD via command line, or as a subprocess. See Start up and Configuration for more on the last two options.

Since the FreeCAD Python module is compiled from C++ (rather than being a pure Python module), it can only be imported from a compatible Python interpreter. Generally this means that the Python interpreter must be compiled with the same C compiler as was used to build FreeCAD. Information about the compiler used to build a Python interpreter (including the one built with FreeCAD) can be found as follows: