Sunday, October 23, 2011

Engine: 5v2 - camshaft gear, parametric copy

In the post Engine: 5 - camshaft gear I used simple Python script to generate a radial copy. Unfortunately copies were independent from base object. Today I'll show how use more advanced radial script.

In directory:
you can find script.

Open earlier camshaft gear model. Delete Chamfer, Cut, Fusion and Fillet001 to Fillet036. Leave only he Revolution and the Fillet. Save model as a new file.

Import script by typing in the python console:
from PartDesign.Scripts import RadialCopy as rcopy
Select the Fillet and make a radial copy:
and eventually refresh the document:
At the end change values in Property view-data tab: Radius 0, Angle 10.

Open the Part workbench and use the boolean difference to cut the Radial Copy from the Revolution.

Try change something, eg. first sketch.

After a while whole model will be regenerated. This is the difference between previous and actual script.

Download file.

Download RAW Video.

FreeCAD 0.12 5052 SVN

Saturday, October 15, 2011

Engine: 6 - piston ring, macro tutorial

FreeCAD has good macros support. Let's me show you macro recording example: simple piston ring creation.

Macro creation

Open a new file in Part Design workbench. You should see four icons:

  • "Open a dialog to record a macro" - red circle 
  • "Stop the macro recording session" - (inactive) square
  • "Opens a dialog to let you execute a recorded macro" - notepad icon
  • "Execute the macro in the editor" - (inactive) triangle

Click on the "Open a dialog to record a macro" and fill "Macro name" field. Click on the Record button, macro recording will start.

Sketch a rectangle as above image on the plane  XY. It would be ring section.

Revolve section around Y axis with angle 359.5 deg (ring needs a small gap). Important: set base in the Property view to [0,0,0]. Now you should see finished piston ring model. You can stop macro recording - click on the "Stop the macro recording session" icon.

Click on the "Opens a dialog to let you execute a recorded macro", select your macro in the dialog window, and click Edit button. Now you can see your macro listing. You can do some clean up (I recommend replace "FreeCAD.getDocument.("Unnamed")"  with more universal "App.activeDocument()"). Save it with "Save the active document" icon. Listing should be similar to this:

# Macro Begin: /home/adi/.FreeCAD/piston-ring1.FCMacro +++++++++++++++++++++++++++++++++++++++++++++++++
import FreeCAD
import Sketcher
import PartDesign

App.activeDocument().Sketch.Placement = App.Placement(App.Vector(0.000000,0.000000,0.000000),App.Rotation(0.000000,0.000000,0.000000,1.000000))
#Gui.activeDocument().activeView().setCamera('#Inventor V2.1 ascii \n OrthographicCamera { \n viewportMapping ADJUST_CAMERA \n position 0 0 87 \n orientation 0 0 1 0 \n nearDistance 37 \n farDistance 137 \n aspectRatio 1 \n focalDistance 87 \n height 119 }')
App.activeDocument().Revolution.Sketch = App.activeDocument().Sketch
App.activeDocument().Revolution.Base = App.Vector(37.600000,1.850000,0.100000)
App.activeDocument().Revolution.Axis = App.Vector(0.000000,1.000000,0.000000)
App.activeDocument().Revolution.Angle = 360.0
App.activeDocument().getObject("Revolution").Angle = 359.50
App.activeDocument().getObject("Revolution").Base = (0.00, 0.00, 0.00)

# Macro End: /home/adi/.FreeCAD/piston-ring1.FCMacro +++++++++++++++++++++++++++++++++++++++++++++++++

Your icon, your toolbar

If you created an amazing macro you need your custom shortcut:) Open dialog window from the "Tools-Customize" menu.

Select "Macros" tab, fill "Menu text" and select your favorite "Pixmap". Then click "Add".

Now you can add icon to a toolbar. Select tab "Toolbars" and "Macros" from combo view. From the right combo select Workbench (eg. Part Design). Create new toolbar by clicking "New..." and then add your macro icon (eg. "Ring creator") using right arrow.

At the end you can see your icon (football in the example) in one of the FreeCAD toolbars. Click it to execute macro.


You can try:

  • modify created macros,
  • put scripts fro older posts as macros,
  • use PyQT to extend graphical interface,
  • use external modules in macros, eg. oosheet, pyODE
  • and much more!

Download file.

Download RAW Video.

FreeCAD 0.12 5017 SVN

This is probably the last "FreeCAD: engine tutorial" post before FreeCAD 0.12 stable release.

Friday, October 7, 2011

OOSheet as FreeCAD and LibreOffice bridge

Access to LibreOffice ( docs is able from Python. You can do it with PyUNO. When you need Calc spreadsheet, situation is even simpler. In this case OOSheet Python module is useful. 


As example I will show script which gets LibreOffice spreadsheet data, modifies FreeCAD wrench model and additionally returns model volume to the spreadsheet.

OOSheet installation under Ubuntu

I think that all readers have LibreOffice and Python installed. Fastest OOSheet installation method, type in terminal:

sudo apt-get install python-pip
sudo pip install oosheet

Actual FreeCAD version installation

I recommend add PPA repository:

sudo add-apt-repository ppa:freecad-maintainers/freecad-daily
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install freecad freecad-doc

What do you need to proceed

  1. File klucz_en.ods with our data.
  2. Model file klucz.fcstd. Everything should be saved in the fctest directory.


Our solid is based on a single sketch pad. Script for rows 2-12 will:
  • change pad (wrench thickness),
  • change sketch constraints: 45 (wrench gap), 51 (wrench length), 54 (wrench connector width),
  • recompute model,
  • export solid to STEP - file number equal to row number,
  • export model views to SVG file,
  • compute solid volume and return it to last spreadsheet column.
Open LibreOffice spreadsheet with 2002 port listening by typing in terminal:
oocalc fctest/klucz_en.ods -accept="socket,host=localhost,port=2002;urp;StarOffice.ServiceManager"

Then open in FreeCAD klucz.fcstd model file. Put below script in FreeCAD Python console. You should see new STEP and SVG files in fctest directory. Last sheet column should be filled by computed volume values.

Script listing:

from oosheet import OOSheet as S # for connection with z OO Calc
import ImportGui # for STEP export
for keynumber in range (2,13):
     skey = str(keynumber)
     App.ActiveDocument.Sketch.setDatum(54,S('b'+skey).value) #wrench connector width from sheet
     App.ActiveDocument.Sketch.setDatum(51,S('a'+skey).value) #wrench length
     App.ActiveDocument.Sketch.setDatum(45,S('d'+skey).value) #wrench gap
     App.ActiveDocument.getObject("Pad").Length = S('c'+skey).value #wrench thickness
     App.ActiveDocument.recompute() #model recompute
     __objs__=[] #STEP export
     del __objs__
     PageFile = open(App.activeDocument().Page.PageResult,'r') #SVG export
     OutFile = open('fctest/klucz'+skey+'.svg','w')
     del OutFile,PageFile
     S('e'+skey).value = App.ActiveDocument.getObject("Pad").Shape.Volume #save volume in the sheet

Download Video.
Source post.

Saturday, October 1, 2011

Engine: 5 - camshaft gear

Timing wheel (camshaft gear) which works with toothed belt is quite simple part. However it needs radial pattern tool - I will use short python script.

Open a new sketch in Part Design workbench. First of all sketch a trapezoid on the plane XY. Use dimensions as in the picture. Trapezoid should be connected with point (0,0) by two construction (blue, length = 47) lines.

Pad the sketch. The pad should be 30. You can add fillets to edges.

Make a new sketch on the plane YZ. Draw a wheel section.

Dimension and constraints are shown above.

Revolve the sketch around Z axis (Data tab in Property view). Next hide the revolution by selecting it and pressing [Space].

Use a python script

FreeCAD has a python console (at the bottom). Select Pad in Tree view and copy this script to python console:

teeth= 36
sel = FreeCADGui.Selection.getSelection()
sel = sel[0]
name = sel.Name
shape = sel.Shape
for i in range(1,
     newshape = sel.Shape.copy()
     newshape.Placement=App.Placement(App.Vector(0,0,0),App.Vector(0,0,1),       (i*(float(360)/float(
     newobject = FreeCAD.ActiveDocument.addObject("Part::Feature",name)
     newobject.Shape = newshape

35 new pads should appear as in the image above. You made radial pattern!

Short explanation:
teeth= 36 number of pads

sel = FreeCADGui.Selection.getSelection()
sel = sel[0]
name = sel.Name
shape = sel.Shape
get information from part selected in Tree view

for i in range(1,teeth): loop from 1 to 35 (python count from 0)

newshape = sel.Shape.copy() make a copy

newshape.Placement=App.Placement(App.Vector(0,0,0),App.Vector(0,0,1),       (i*(float(360)/float(teeth)))) move the copy, syntax App.Placement(Base, Axis, Angle in deg)

Open Part workbench and select all pads (or fillets if made they) in Tree View - use [Shift] and click first and last. You can deselect revolve with [Ctrl]. Now make a fusion (two blue balls icon) of the all pads. Unhide [Space] the revolution.
Select with [Ctrl] revolution and fusion and do cut of them (icon with blue and white ball).

Now we have a crankshaft gear.

You can also open Part Design and add some fillets and chamfers.

More info:
Download file.

Download RAW Video.

FreeCAD 0.12 4983 SVN