Outils pour utilisateurs

Outils du site


issue125:freecad

Ceci est une ancienne révision du document !


En cours de traduction, ailleurs ;-)

In this series, we will be examining the world of FreeCAD, an open-source CAD modelling application that it still in Beta, but has been gaining acceptance in recent years. Naturally, it is readily available in the Ubuntu repositories. In the fifth article on using FreeCAD, we worked on an architectural project in two different ways. In the first place, we used the Arch workbench to create a modern architectural project, in which supplementary information is given to the computer, so using FreeCAD to create a Building Integrated Model (BIM). Since this approach is in an early stage of development, and is limited to simple forms, we then used a more traditional approach to create volumes in the same way as in previous projects, but on a larger scale. The sweeping technique allowed us to create an element with the shape of an arch by sweeping one sketch (a profile) around another sketch (the outline of an arch).

In today’s part of this series, we will extend FreeCAD’s possibilities using a little Python programming to create a helicoidal surface in the shape of a mechanical gear wheel.

Programming FreeCAD? And why Python?

As many readers will be aware, the world of program language implementation is divided into two main categories. There are programming languages such as C or Fortran for which the source code needs to be compiled (into our computer’s machine language) in order to be executed. There are also interpreted languages that do not need to be compiled (or “translated”, to give a mental picture of what is happening when using a compiler), but may be interpreted directly by a special program on the user’s computer, called an interpreter. This is the case of many programming languages with a wide acceptance in our days, such as PHP on servers or Python on users’ computers. As a side-note, the Java language tends to pertain mostly to the former, compiled category (though with caveats), while the very similarly termed Javascript is actually quite a different beast and is mostly used interpreted by web browsers.

FreeCAD has been built in Python, thus an interpreted language. This is quite convenient for several reasons. In the first place, it makes the application more easy to transport to other computer architectures and operating systems, as long as a Python interpreter is available for the desired platform - and Python is getting quite ubiquitous, indeed. In the second place, we can open a console view of FreeCAD’s inner workings by simply choosing menu option View > Views > Python console. Each action we carry out through the User interface is actually converted into Python commands the program’s core logic - and we can see it in real time in this console. If, for instance, I create a new project, switch to the Draft workbench, and draw a line, Below is what actually happens.

This is quite neat, since one can learn about the different commands used in an interactive way. Naturally, once one has a grasp of the fundamentals, they can be used to write one’s own scripts, and have them executed by FreeCAD.

To take an example, let us write a simple Python script that will create a simple box shape. Create a new file called “test1.py”, and copy in the code shown top right.

The Part library contains the tools from the Part workbench. We begin by creating a new project, called “Box Model”. We make this the active document (window), and add a new object based on the “Part::Box” prototype, naming it “box1”. We set its dimensions, and have the document recalculate itself. We then tell the user interface (“Gui”) to zoom the view to fit the new object, and select the Axonometric (3D) view.

To execute our script, switch to a terminal window in the same directory we have the .py file, and issue command:

freecad test1.py

We will see FreeCAD start up and execute our script line-by-line, giving the final result shown below.

As a second example, let us build something slightly more complex: the shape of a tin that consists in a flat shape (two arcs connected by straight segments), that will then be extruded to form a volume. Bottom right is the script, in file “test2.py”.

Let us comment on the differences with the previous example. In this case, we begin our new object by creating four vectors V1 to V4, that indicate the positions of the connection points between the arcs and the straight lines. We then create the two straight segments, L1 and L2, and finally the two arcs C1 and C2. We then need to convert these four items into Edge objects, E1 to E4, which are then connected into a Wire object W. This is the outline of our tin’s top. Please ensure lines and arcs are set up in the correct order, otherwise connectivity errors may ensue. Finally, the Wire is converted into a bidimensional Face object F, which is then extruded into a Shape P. A generic volume is derived from “Part::Feature”, and is given P as its shape.

As a final note, the finished project can be saved directly from our script, by calling the doc.saveAs procedure. When executed from the terminal, shown below is the result of our script.

Lets draw some gears

Scripting can come in useful when we need a shape that contains many similar, but different and calculable parts. In such cases, writing a program to iterate over our shapes can be cost-effective in terms of our time. A typical case is when creating a mechanical gear wheel. Such a wheel can be seen as a combination of an exterior shape, formed by a certain number of teeth or cogs, and an inner shape that delimits the axis. This can be either smooth or splined so that the axis can transfer torque to and from the wheel. Both the interior and the exterior wheel forms are formed of a basic shape or motif, that is iterated at a fixed angular offset between each individual tooth.

Let us concentrate on the exterior shape, slightly more complex than the interior splines. Each individual tooth is centered at a certain radius from the wheel center, or pitch surface. In our case, we specify a 100mm radius. Simplifying somewhat, this is the point at which the other gears connected to this one will transfer their force. Points 3 and 6 in our schematic are on this surface. Going outwards, teeth extend to an outer limit, the top flat. Points 4 and 5 are on this radius, in our case taken at 110 mm. Finally, we need to “make some space” inwards, to accommodate the other wheel’s teeth. So we go inwards to the bottom flat, at radius 90 mm in our case, and holding points 1 and 2. We will iterate this basic shape over the 16 teeth of our wheel.

Our script (right) will simply draw a series of lines from vertices 1 through 6 of each cog, and then on to vertex 1 of the next cog. Since Python has a mathematical library “math”, sine and cosine functions can be used to calculate a pair of X and Y-axis coordinates for each vertex. The script itself is quite straightforward. As a note, in our example we are using only straight segments. However, in a real gear wheel, the bottom and top flats, and the contact faces, would more usually be drawn with arcs.

Once we have the cog shape in FreeCAD, we can continue using it “by hand” within the normal user interface, as a basis for an extrusion or any other operation we wish. With a similar script to draw the central, crenellated shape for the splines, we can extrude both the external volume of the gear wheel and the volume of the inner axle, and then use a boolean operation to cut one out of the other. We can thus obtain the shape of a traditional straight gear that would be found in many traditional applications such as a four-by-four vehicle’s transfer box.

However, straight teeth do have the disadvantage of creating quite a bit of noise during operation, since, during each rotation, each tooth engages immediately with the other gear wheel’s corresponding tooth along the complete width of the tooth. This produces the typical whining noise that can be heard from some mechanical setups. In most modern applications where smoothness of operation and low noise emissions are valued, such as vehicle gear boxes, helicoidal gears can be preferred.

To draw such a gear wheel, the same tooth pattern can be used, but swept along a helicoidal path instead of using the simple linear extrusion tool.

Start, in the Part workbench, by selecting the Part > Create Primitive menu option. Here, we can select the Helix shape. Since I wished to create a gear wheel 50 mm in width, with teeth sloped at approximately 1:20 across the width of the wheel, I chose a helix height of 50 mm, but a pitch of 1000 mm between helix spires. The external radius of the helix should correspond to the point at which it will be created. Both right-handed and left-handed helices can be used, as needed.

Once the helicoidal line is drawn, it can be used as a path along which to sweep the cog’s external face, using the same tools as when creating a Gothic arch in the previous part of this series. The internal surface will be created using a linear extrusion as before, since even a helicoidal gear wheel’s internal splines tend to be straight so as to facilitate assembly of the wheel on its supporting axle. The resulting wheel is actually a fair approximation of an actual gear. Some aspects would be made better, though, such as bevelling external edges to make them less aggressive, or cutting out part of the gear wheel’s material to make it lighter and use less material in fabrication. These operations can also be done in FreeCAD, and are left as an exercise to the reader (hint: use a revolution surface to create cutouts for each face).

What next?

In this article on using FreeCAD, we extended FreeCAD’s possibilities using a little Python programming to create a helicoidal surface in the shape of a gear wheel. Once the basic shape has been drawn using a script, it can be used in combination with any other of the techniques available from the graphical interface to create the final object. Python is a fully-developed programming suite, containing many different libraries both mathematical and for other purposes, that can be used in combination with FreeCAD to create objects that do not exist within the initial library of basic shapes.

In the next part of this series, we will concentrate on a more complex primitive object that allows us to create forms and volumes with less regularity, the mesh.

issue125/freecad.1506877260.txt.gz · Dernière modification : 2017/10/01 19:01 de auntiee