Tuesday, 11 March 2014

FreeCAD: Simple Engine Animation.


A very simple animation to show the basics:


I do not have the time needed to go on detail plus this information will be obsolete once FreeCAD has an assembly module. So I'm posting just what I have.

Steps to animate something inside FreeCAD:


-Create parts

-Export them to STEP

-Create new document and import parts

-Determine how FreeCAD names the parts (in my script you can see "Part_Feature003", or "Part_Feature")

-Create a function that defines mathematically the mechanism (crank-slider type in mine, very simple)

-Create a timer that calls the function to make it move (look at the end of the script)


This is the python script, you can download parts here:


from PyQt4 import QtCore, QtGui

import Part
from FreeCAD import Base
import math as mt

i=0
def engine():
  global i
  
  i+=0.1
  
  cig=mt.degrees(i)
  
  
  B=mt.asin((mt.cos(i)*40.0)/140.0)
  AB=mt.asin((mt.sin(i)*(40.0/140.0)))
  PP=mt.cos(i)*40.0+mt.cos(AB)*140.0
  alphaB=mt.radians(90.0)-B
  
  br=FreeCAD.Rotation(FreeCAD.Vector(0,0,1),alphaB)
  bd=App.Vector(mt.sin(i)*40,mt.cos(i)*40.0,20.0)
  bdgc=FreeCAD.Vector(mt.sin(i)*40,mt.cos(i)*40.0,20.0)
  
  App.getDocument("montadoanimacion").Part__Feature002.Placement=App.Placement(App.Vector(0,0,0),App.Rotation(FreeCAD.Vector(0,0,1),-cig))
  
  #biela
  
  App.getDocument("montadoanimacion").Part__Feature003.Placement=App.Placement(bd,App.Rotation(FreeCAD.Vector(0,0,1),(mt.degrees(AB))))
  
  #piston
  
  App.getDocument("montadoanimacion").Part__Feature.Placement=App.Placement(App.Vector(0,PP-35,30),App.Rotation(-0.5,0.5,0.5,0.5))

timer=QtCore.QTimer()
timer.timeout.connect(engine)
timer.start(10)



Open the file and then copy-paste this script. It should move. 

That's all.

Now I hope to see how people creates new and more complex animations :)


Bye!

4 comments:

  1. I don't know your name, but this really helped. Thanks.
    The only thing I changed was from PyQt4 import QtGui, QtCore
    Thanks again

    ReplyDelete
    Replies
    1. Hi, my name is Javier ;)
      Thanks for your words and the library annotation, I think I should change that.

      Delete
  2. Just for you to notice, your tutorial was usefull, but i wasn't able to do ani animation, if you can make a short video for this, it would be great!
    But you helped me with the part stuff for RayTracing, thanks a lot!

    ReplyDelete
    Replies
    1. Hi Francisco, thanks for your words.
      Where did you had the problem exactly? Because the hardest thing is to mathematically define the mechanism, and that's something you can't learn easily with a video.
      Bye!

      Delete