FreeCAD tutorial

FreeCAD is an open source app to build models that you can e.g. 3d print.

As a beginner in 2024, you will always want the "Part Design" [doc] [tutorial] workbench, which is quite trickily hidden in the toolbars.

Finding the Part Design workbench
Finding the Part Design workbench

Navigating the 3D view [docs] is semi-intuitive:

You can only have one big body that you cut out parts from, so think as a mason, not as a Lego builder. The usual way to start is to draw something in 2d as on paper ("sketch"), then extrude it to 3d ("pad"), then build further shapes that you will subtract from that.

Also: Cool for devs, everything is a Python command [docs], use View→Panels→Python Console to see them in the lower right corner.

1 Sketch in 2D

So we start with a new sketch in 2d:

Starting a new 2D sketch
Starting a new 2D sketch

In the 3d view, choose a plane; take YZ plane when in doubt, that will mean you extrude the drawing left-to-right (along x-axis). Now we can choose from a lot of shapes, let's start with a polyline.

Drawing a polyline
Drawing a polyline

LMB multiple times into the 3d view to create an approximate shape. Also note (a) that FreeCAD tries to guess some vertical (v) and horizontal (h) constraints from the way I clicked, shown in green, and (b) that FreeCAD switched the left pane from "Model" to "Tasks", here the sketching task.

So how do we get to the final 2d shape? Constraints. Found under Sketch→Sketcher constraints they define size/length and orientation of each line in the polyline. Start with the 3 constraints you have.

Horizontal and vertical distance constraints
Horizontal and vertical distance constraints

Select the bottom line and press l to edit the horizontal distance e.g. to 120mm, then i to select the left/right lines and edit the vertical distance e.g. to 30 and 70mm. You can select the line either before or after pressing the key (or clicking the appropriate toolbar button).

You can also use l and i to define distances between points, here e.g. between the center point of the coordinate system and the top middle point of the polyline. Notice how in the left pane, the number of "DoF" (degree of freedoms) steadily decreases as we add more constraints. To find out which contraints are missing, wiggle some points and see what moves, then react to that.

Angle and general distance constraints
Angle and general distance constraints

If some angle moves, use k,a on two lines to constrain the angle between these lines; if some point can still wiggle, use k,d to constrain the distance between them, this time not vertically or horizontally but in total.

Once we have 0 DoF left, all the lines turn green, and in the left pane, you can click "Close" to exit the sketch task.

2 Pad in 3D

Time to extrude ("pad") our sketch along the x-axis, remembering we did the 2d sketch on the yz-plane.

Press Pad to go to Pad task
Press "Pad" to go to Pad task

Once again, the pertinent Pad button is hiding in plain sight among the many toolbar buttons. Also note that in the left pane, the body has our sketch as direct child. Now press the "Pad" button (no keyboard shortcut) to get to the task.

Define the pad length
Define the pad length

In the (task) left pane, you can define the extrusion as e.g. 130mm, and press "Ok" to exit the task and get to the main view, where the (model) left-pane location of the sketch has changed.

Admire the extruded (padded) sketch in 3d
Admire the extruded (padded) sketch in 3d

Again in the left pane, the body now has the pad as direct child, which in turn has the sketch as direct child. This is the FreeCAD way of saying "parent depends on child". Also, I pressed Space on the Sketch in the left pane because it was not visible before.

Now lets make some holes.

3 Pockets in 2D, then 3D

Making holes or cutting away from our main pad follows essentially the same rules as sketch and pad, but with a slight twist:

Our main body was positioned relative to the origin point (0, 0, 0). This time, we want the holes relative to the surface they are in. So select a surface in the 3D view and then press "Create sketch" in the toolbar again.

Select surface to punch holes into
Select surface to punch holes into

FreeCAD switches to task mode again, and the toolbars change again, which we use to define our reference frame aka coordinate system.

Define external geometry as local reference frame
Define external geometry as local reference frame

To the right, as one of the last geometries, we have g,x for external geometry. Click it and after that, some lines or points in the 3D view, which should turn purple. Then construct e.g. a circle, and constrain diameter and horizontal and vertical distances relative to the reference frame.

Create circle to extrude to a pocket later
Create circle to extrude to a pocket later

Roughly click the circle and its diameter. Then, start with the diameter constraint e.g. to 20mm on the circular line, and then horizontal i and vertical l distances of e.g. 15mm and 20mm respectively, defined relative to the top left point of the external geometry, and starting at the circle center. Once this is done and the circle fully constrained, click "Close" to end the task and return to the model.

Our latest sketch arrives as "Sketch001" in the left pane, and now we can click "Pocket" in the toolbar.

Create a pocket from the circular sketch
Create a pocket from the circular sketch

Just like "pad", choose how deep the pocket goes, then "Ok" the task to return to the model view.

Admire the newly created pocket
Admire the newly created pocket

The pocket is visible in 3d, however the left pane is more wonky. Now the pocket resides on the same level ("beside") the pad, while the sketches are children of their respective bodies. This strikes me as somewhat inconsistent, but maybe I am the only one.

4 More Holes in 3D

You might want to make a lot of similar holes, and that is where the various clone tools come on. One of the simpler ones is the linear pattern, aka repeat along line.

Repeat holes along a line
Repeat holes along a line

In the left pane, select the pocket you want to repeat and press the "linear pattern" toolbar button, which opens the task.

Choose repetition axis, total area, and repeats
Choose repetition axis, total area, and repeats

Finding the axis you want can be a bit fiddly, and you need to remember that "length" refers to total area covered, not length between repeats. Together with the number of repetitions which includes the original pocket, we are done here, so press "Ok".

Admire the linear pattern (repeats along line) of pockets
Admire the linear pattern (repeats along line) of pockets

In the left pane, the LinearPattern is on the same level ("beside") the Pad, as is the original Pocket, and only the Sketches are children. So showing dependencies in the tree view is different for 2d sketches and 3d modifiers, apparently.

5 Skewed Holes in 3D

So far we defined sketches on rectangular surfaces. When they are not, we need a so-called "datum plane", or local coordinate system, to start sketching.

Create a datum plane on some irregular surface
Create a datum plane on some irregular surface

Select your irregular surface or line or point, and create a datum plane, once again hidden somewhere in the toolbar.

Define vertexes to span a coordinate system on the plane
Define vertexes to span a coordinate system on the plane

Once again, the FreeCAD default choice is not optimal, here for "attachment mode" which is "Plane"; in particular, what becomes the x/y axis next for the sketch is undefined. So select attachment mode "o-x-y", i.e. origin, x-axis and y-axis, and press each of the "Vertex" buttons in turn to click the appropriate vertex in the 3D view. Notice how the plane in the 3D view turns to support our axes. After clicking "Close" and starting the sketch, you may have to go back and activate checkbox "Flip sides" at the bottom if the sketch turns out to be on the wrong side of the datum plane.

Start sketch on a well-defined datum plane
Start sketch on a well-defined datum plane

The x-axis is prioritized when x and y vertexes were not orthogonal, looking from the o point. So notice here how the x-axis is exactly as we defined it, while the y-axis is a bit off. Now we can e.g. define a rectangle that is axis-aligned to the plane, which is a lot easier than constraining a slanted rectangle.

Create pocket from plane axis-aligned rectangle
Create pocket from plane axis-aligned rectangle

Pocketing such a plane axis-aligned rectangle results in a hole that goes parallel to the top surface. So in such scenarios, it pays to construct a datum plane whose x-axis is parallel to the desired top surface.

6 Slanted Bodies

Another good use for datum planes is when we want irregular surfaces instead of linear extrusion.

Create an additive loft between two sketches
Create an additive loft between two sketches

For this we need two sketches. In the current project, we delete everything but the first sketch, planning to copy that sketch and put the second one onto another datum plane. First create the target datum plane. Then use Edit→Duplicate selected object because copy and paste for some reason only puts new objects outside the body, and in the dialog deselect the original plane. Now RMB on the copied sketch in the left pane, and click Attachment editor where you can switch the plane to the target datum plane.

Editing the second sketch to make it somewhat different would make sense here. The number of points can even be different to the first sketch! Then we select the two sketches in the left pane, go to the toolbar and two icons to the right of "Pad", we find "Additive loft" to create an interpolated body, useful for e.g. slants. And finally, we can start making holes again, as usual.

7 Macros

Soon enough, things will start to repeat, and that is where macros [docs] come into play. First, you will want to use global variables instead of typing "14.05mm" a hundred times, and one way to define them is in spreadsheets [docs] [forum].

Create global variables in a spreadsheet
Create global variables in a spreadsheet

In the toolbar at the top, switch the workbench, create a spreadsheet and you are ready to define some values and their "aliases" aka variable names. Now whenever you type e.g. a constraint somewhere, there is a small "f(x)" button that allows you to use such a global variable, e.g. for a spreadsheet named "L_var" and a variable "strip_screw_fillet" the expression "<<L_var>>.strip_screw_fillet" would substitute the variable value, and update automatically when you change the value in the spreadsheet.

Now change back to the "Part Design" workbench, and we will create a macro inside a sketch, which is the most useful scenario. For better repeatability, I recommend always sketching onto an O-Y-X datum plane, so the distance to (0, 0) is always the same. Now before you start sketching, press the red "Macro recording" button.

Record a macro inside a sketch
Record a macro inside a sketch

We always have to choose a file to write to, usually saved under ~/.local/share/FreeCAD/Macro/ as .FCMacro Python files. Alternatively, you can enable View→Panels→Python Console and watch your actions scroll by one-by-one as Python commands. When sketching, avoid referencing the datum plane or external geometry, because this will be different every time the macro is executed later. Also, for some strange reason, "symmetry constraints" [wiki] [forum] are not replayable without causing an invalid state in FreeCAD 0.20.2 (Debian 12 bookworm), so create them at the end, because manual wiggling in the UI solves the problem.

Once you are done, press the "Stop macro recording" button to the right of the red "Macro recording" button, and then the "Macros" menu button one further right, which will open a dialog. In this dialog, we can Edit the recorded macro, where you will soon see that every line uses the same hardcoded absolute sketch reference which makes the macro non-transferable to other sketches. As a remedy, we replace the absolute sketch with a sk variable retrieved from the current sketch:

sketch_name = Gui.ActiveDocument.getInEdit().Object.Name
sk = App.ActiveDocument.getObject(sketch_name)

... where hardcoded sketch references come in two flavors, the second one appearing whenever a variable is used.

App.getDocument('docname').getObject('Sketch107') # == sk
App.getDocument('docname').Sketch107              # == sk

Unfortunately the macro editor does not have a "search and replace" function, so copy everything to an editor of your choice, substitute sk everywhere, and copy everything back. And now we can replay the macro in any sketch, with constraints against datum plane or external geometry the only remaining manual task. Use Ctrl+Shift+1 or a higher number for replay, see Macro→Recent Macros but beware that the order changes when you usage a non-1 macro a lot.

Every addConstraint returns the index of the new constraint [docs], which you can use further e.g. for distances involving spreadsheet variables:

i = sk.addConstraint(Sketcher.Constraint('Angle',2,2,3,1,2.737224)) 
sk.setExpression('Constraints[%d]' % (i,), u'<<L_var>>.keyhole_lock_angle')

Also, switching through document tabs with Ctrl+Tab as hotkey [docs] [guide] might be preferable to mouse clicking.

8 Sketching Virtual Lines

Advanced sketching includes symmetries (s), for which we need virtual lines aka "toggle construction geometry" (g,n) which again is hidden helpfully in the toolbar.

Sketch with construction geometry for invisible lines
Sketch with construction geometry for "invisible lines"

Notice that once you toggle construction geometry, in the toolbar all 2d geometry buttons turn blue. This means whatever you draw now will not be part of the "real" sketch, and instead be considered "helper lines". Here we make a helper line (g,L) in the middle of the block, and first select two corner points left and right before selecting the virtual line; the order is important. Then, using s for "Constraint symmetrical" will keep the virtual line in the middle.

And now that the virtual line is centered, we can select the circle center points, then the virtual line, and press s again: The circles keep the same horizontal distance from the virtual line. For good measure, select both circle outlines to add e for "Constraint equal", and the two circles have the same size too. Also, remember to g,n again to return to normal mode, or you will have a lot more virtual geometry than you wanted.

9 Clone a Sketch

You will probably want to use a sketch in multiple locations, just attached to different datum planes or with different offsets from that datum plane. Here, c,l for "clone sketch" is your friend. Alternatively, switch to the "Draft" workbench, then in the menu bar Modification→Clone or the dark blue sheep, then back to "Part Design"; and yes, this workflow is indeed very odd.

In the left pane, for some reason the cloned sketch lands outside all of your bodies, and must be mouse-dragged into the destination body. Once inside, look at the Properties for "Map Mode".

Clone a sketch, then map to any datum plane
Clone a sketch, then map to any datum plane

And if you did your homework and have nice O-Y-X datum planes waiting at all target locations, then choose one and you are set: we have a parametric clone except for positioning, done with the usual Attachment Offset→Position which now appears in the Properties.

Conclusion

So that was FreeCAD in one little tour! Once you are done with the model, select "Body" in the left pane, go File→Export or Ctrl+E, and then export the CAD to triangles e.g. as STL. And if you are not done yet, there is always the FreeCAD wiki, where you can learn so much more... And that was it! Hope you got a little bit of starter knowledge today. Good luck on whatever you are constructing!

EOF (Jun:2024)