Saturday, 15 October 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.

No comments:

Post a Comment