Code snippets/fr

Cette page contient, des exemples, des extraits de code en Python FreeCAD, recueillis auprès d'utilisateurs expérimentés et de produits de discussions sur les forums.

Lisez les et utilisez les comme point de départ pour vos propres scripts. .

Un fichier typique InitGui.py
En plus de votre module principal, chaque module doit contenir, un fichier InitGui.py, responsable de l'insertion du module dans l'interface principale.

Ceci est un simple exemple.

Un fichier module typique
Ceci est l'exemple d'un fichier module principal, il contient tout ce que fait votre module. C'est le fichier Scripts.py invoqué dans l'exemple précédent. Vous avez ici toutes vos commandes personnalisées.

Importer un nouveau type de fichier
Importer un nouveau type de fichier dans FreeCAD est facile. FreeCAD ne prends pas en considération l'importation de n'importe quelle données dans un document ouvert, parce que, vous ne pouvez pas ouvrir directement un nouveau type de fichier.

Donc, ce que vous devez faire, c'est ajouter la nouvelle extension de fichier à la liste des extensions connues de FreeCAD, et, d'écrire le code qui va lire le fichier et créer les objets FreeCAD que vous voulez.

Cette ligne doit être ajoutée au fichier InitGui.py pour ajouter la nouvelle extension de fichier à la liste: Puis, dans le fichier Import_Ext.py, faites: Pour exporter votre document avec une nouvelle extension, le fonctionnement est le même, mais vous devrez faire:

FreeCAD.addExportType("Your new File Type (*.ext)","Export_Ext")

Ajouter une ligne
Une ligne, à uniquement deux points.

Adding a polygon
A polygon is simply a set of connected line segments (a polyline in AutoCAD). It doesn't need to be closed.

Adding and removing an object to a group
Note: You can even add other groups to a group...

Accessing and changing representation of an object
Each object in a FreeCAD document has an associated view representation object that stores all the parameters that define how the object appear, like color, linewidth, etc...

Observing mouse events in the 3D viewer via Python
The Inventor framework allows to add one or more callback nodes to the scenegraph of the viewer. By default in FreeCAD one callback node is installed per viewer which allows to add global or static C++ functions. In the appropriate Python binding some methods are provided to make use of this technique from within Python code. Now, pick somewhere on the area in the 3D viewer and observe the messages in the output window. To finish the observation just call The following event types are supported
 * SoEvent -- all kind of events
 * SoButtonEvent -- all mouse button and key events
 * SoLocation2Event -- 2D movement events (normally mouse movements)
 * SoMotion3Event -- 3D movement events (normally spaceball)
 * SoKeyboardEvent -- key down and up events
 * SoMouseButtonEvent -- mouse button down and up events
 * SoSpaceballButtonEvent -- spaceball button down and up events

The Python function that can be registered with addEventCallback expects a dictionary. Depending on the watched event the dictionary can contain different keys. For all events it has the keys: For all button events, i.e. keyboard, mouse or spaceball events For keyboard events: For mouse button event For spaceball events: And finally motion events:
 * Type -- the name of the event type i.e. SoMouseEvent, SoLocation2Event, ...
 * Time -- the current time as string
 * Position -- a tuple of two integers, mouse position
 * ShiftDown -- a boolean, true if Shift was pressed otherwise false
 * CtrlDown -- a boolean, true if Ctrl was pressed otherwise false
 * AltDown -- a boolean, true if Alt was pressed otherwise false
 * State -- A string 'UP' if the button was up, 'DOWN' if it was down or 'UNKNOWN' for all other cases
 * Key -- a character of the pressed key
 * Button -- The pressed button, could be BUTTON1, ..., BUTTON5 or ANY
 * Button -- The pressed button, could be BUTTON1, ..., BUTTON7 or ANY
 * Translation -- a tuple of three floats
 * Rotation -- a quaternion for the rotation, i.e. a tuple of four floats

Manipulate the scenegraph in Python
It is also possible to get and change the scenegraph in Python, with the 'pivy' module -- a Python binding for Coin. The Python API of pivy is created by using the tool SWIG. As we use in FreeCAD some self-written nodes you cannot create them directly in Python. However, it is possible to create a node by its internal name. An instance of the type 'SoFCSelection' can be created with

Adding and removing objects to/from the scenegraph
Adding new nodes to the scenegraph can be done this way. Take care of always adding a SoSeparator to contain the geometry, coordinates and material info of a same object. The following example adds a red line from (0,0,0) to (10,0,0): To remove it, simply issue:

Adding custom widgets to the interface
You can create custom widgets with Qt designer, transform them into a python script, and then load them into the FreeCAD interface with PyQt4.

The python code produced by the Ui python compiler (the tool that converts qt-designer .ui files into python code) generally looks like this (it is simple, you can also code it directly in python): Then, all you need to do is to create a reference to the FreeCAD Qt window, insert a custom widget into it, and "transform" this widget into yours with the Ui code we just made:

Adding a Tab to the Combo View
The following code allows you to add a tab to the FreeCAD ComboView, besides the "Project" and "Tasks" tabs. It also uses the uic module to load an ui file directly in that tab.