

In this chapter, you will be able to use both methods, but it is highly recommended to use the Python Console, since it will immediately inform you of any typing error. In the console, you write Python commands one by one, which are executed when you press return, while the macros can contain a more complex script made of several lines, which is executed only when the macro is launched from the same Macros window.
#Freecad python scripting examples code
There are two easy ways to write Python code in FreeCAD: From the Python console (menu View -> Panels -> Python Console), or from the Macro editor (menu Tools -> Macros -> New). If you are interested in learning more, the FreeCAD documentation wiki has an extensive section related to Python programming. In this chapter, we will discover very generally the Python language. This system also uses the Python console, by simply recording everything that is done in it. By leaving the Python console open, you can literally see the Python code unfold as you work, and in no time, almost without knowing it, you will be learning some Python language.įreeCAD also has a macros system, which allows you to record actions to be replayed later. It is often useful to perform operations for which there is no toolbar button yet, or to check shapes for problems, or to perform repetitive tasks:īut the Python console also has another very important use: Every time you press a toolbar button, or perform other operations in FreeCAD, some Python code is printed in the console and executed. FreeCAD has an advanced Python console, available from menu View->Panels->Python console. Some workbenches of FreeCAD and most of the addon workbenches are fully programmed in Python. You can, for example, create new objects, modify their geometry, analyze their contents, or even create new interface controls, tools and panels. With it, you can access and control almost any feature of FreeCAD. You will be able to use it in many other applications, such as Blender, Inkscape or GRASS.įreeCAD makes extensive use of Python. It is embedded in many other applications, which makes it a very valuable tool to learn.
#Freecad python scripting examples series
It also has a series of features that makes it specially interesting for us FreeCAD users: It is very easy to learn, specially for people who have never programmed before. Look at the Plank Positioning Details chapter to get more explaination on rotation, orientation and flip transformations.Python is a widely popular, open-source programming language, very often used as a scripting language, embedded in applications, as is the case with FreeCAD. Knowing that, choose the most convenient definitions for your design. The choice of the definition influences the behavior of the flip. Those sizes are virtual and you can play with them for your convenience.Ī physical object can be defined in several ways respecting our main and second axis conventions. To realize flip and orientation, the place_plank() function needs to know the sizes along X, Y and Z of the object. The place_plank() function uses this approach to place a object in an cuboid assembly. After defining the orientation of the main axis and second axis, there are still four possibilities called flip: ‘identity’, ‘x-flip’, ‘y-flip’ and ‘z-flip’. For example, ‘yx’ means that the main axis of the object is parallel to the Y-axis of the reference frame and the second axis of the object is parallel to the X-axis. There are just six possibilities: ‘xy’, ‘xz’, ‘yx’, ‘yz’, ‘zx’ and ‘zy’. the orientation of the main and second axis.To place an object, construed with the above conventions, in a cuboid assembly, you can define the rotation of the object with two natural parameters: During the object construction, we choose the X axis as main axis and the Y axis as second axis.Ī cuboid assembly is a construction where most of the objects have their main axis parallel to the X, Y or Z-axis.The second largest size of an object defines the second axis of the object.


Even if those methods are mathematically straight forward, they might require many tries and errors to find out the correct rotation to apply to an object to place it correctly in an assembly. FreeCAD provides the usual rotate and translate methods to place an object in a construction-assembly.
