Pages

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:
yourfreecadinstallation/Mod/PartDesign/Scripts
you can find RadialCopy.py 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:
rcopy.makeRadialCopy()
and eventually refresh the document:
App.ActiveDocument.recompute()
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().addObject('Sketcher::SketchObject','Sketch')
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 }')
#Gui.activeDocument().setEdit('Sketch')
App.ActiveDocument.Sketch.addGeometry(Part.Line(App.Vector(-90.364075,7.014562,0),App.Vector(-76.830093,7.014562,0)))
App.ActiveDocument.Sketch.addGeometry(Part.Line(App.Vector(-76.830093,7.014562,0),App.Vector(-76.830093,0.082527,0)))
App.ActiveDocument.Sketch.addGeometry(Part.Line(App.Vector(-76.830093,0.082527,0),App.Vector(-90.364075,0.082527,0)))
App.ActiveDocument.Sketch.addGeometry(Part.Line(App.Vector(-90.364075,0.082527,0),App.Vector(-90.364075,7.014562,0)))
App.ActiveDocument.Sketch.addConstraint(Sketcher.Constraint('Coincident',0,2,1,1))
App.ActiveDocument.Sketch.addConstraint(Sketcher.Constraint('Coincident',1,2,2,1))
App.ActiveDocument.Sketch.addConstraint(Sketcher.Constraint('Coincident',2,2,3,1))
App.ActiveDocument.Sketch.addConstraint(Sketcher.Constraint('Coincident',3,2,0,1))
App.ActiveDocument.Sketch.addConstraint(Sketcher.Constraint('Horizontal',0))
App.ActiveDocument.Sketch.addConstraint(Sketcher.Constraint('Horizontal',2))
App.ActiveDocument.Sketch.addConstraint(Sketcher.Constraint('Vertical',1))
App.ActiveDocument.Sketch.addConstraint(Sketcher.Constraint('Vertical',3))
#App.ActiveDocument.recompute()
App.ActiveDocument.Sketch.addConstraint(Sketcher.Constraint('DistanceX',1,2,-76.830093))
App.ActiveDocument.Sketch.addConstraint(Sketcher.Constraint('DistanceY',1,2,0.082527))
#App.ActiveDocument.recompute()
App.ActiveDocument.Sketch.setDatum(9,0.000000)
App.ActiveDocument.Sketch.setDatum(8,34.800000)
App.ActiveDocument.Sketch.addConstraint(Sketcher.Constraint('DistanceX',2,2,-90.364075))
App.ActiveDocument.Sketch.setDatum(10,37.500000)
App.ActiveDocument.Sketch.addConstraint(Sketcher.Constraint('DistanceY',2,2,0,1,7.014562))
App.ActiveDocument.Sketch.setDatum(11,1.750000)
#Gui.activeDocument().resetEdit()
App.ActiveDocument.recompute()
App.activeDocument().addObject("PartDesign::Revolution","Revolution")
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.recompute()
#Gui.activeDocument().hide("Sketch")
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.

More?

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 (OpenOffice.org) 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. 

Examples

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.

Example

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
     __objs__.append(FreeCAD.getDocument("klucz").getObject("Pad"))
     ImportGui.export(__objs__,'fctest/klucz'+skey+'.stp')
     del __objs__
     PageFile = open(App.activeDocument().Page.PageResult,'r') #SVG export
     OutFile = open('fctest/klucz'+skey+'.svg','w')
     OutFile.write(PageFile.read())
     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,
teeth):
     newshape = sel.Shape.copy()
     newshape.Placement=App.Placement(App.Vector(0,0,0),App.Vector(0,0,1),       (i*(float(360)/float(
teeth))))
     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:
  1. http://sourceforge.net/apps/mediawiki/free-cad/index.php?title=Macros_recipes
  2. http://sourceforge.net/apps/phpbb/free-cad/viewtopic.php?f=3&t=576&p=4026&hilit=quaternion#p4026
Download file.

Download RAW Video.


FreeCAD 0.12 4983 SVN