Friday, 4 July 2014

FreeCAD: Animated Spring

Not really a spring, but..

... this script creates a helix object and constantly adjust its pitch and height to create a compression-like effect.
As always, the function that calculates and commands the changes is called repetitively by a timer.

Video:


While the animation is running, you can change the values of pitch, length and compression by just typing in, for example:

Pitch = 3
Compression = 0.1   # Relative to the length of the "spring" 


The code:

from __future__ import division 

from PyQt4 import QtCore
import math as mt
import FreeCADGui
App.ActiveDocument.addObject("Part::Helix","Helix")
App.ActiveDocument.Helix.Pitch=5.00
App.ActiveDocument.Helix.Height=20.00
App.ActiveDocument.Helix.Radius=5.00
App.ActiveDocument.Helix.Angle=0.00
App.ActiveDocument.Helix.LocalCoord=0
App.ActiveDocument.Helix.Style=1
App.ActiveDocument.Helix.Label='Helix'
FreeCADGui.ActiveDocument.getObject("Helix").LineColor = (1.00,0.67,0.00)

i = 0
Length = 20
Pitch = 5
Compression = 0.5

def Spring():
  global i, Length, Pitch, Compression
  
  i+=0.01

  R = Pitch / Length
  IH = Compression*Length*mt.cos(i)
  P = Pitch + (R*IH)
  App.ActiveDocument.Helix.Height = Length + IH
  App.ActiveDocument.Helix.Pitch = P
  
  if i == 1000:
    i=0


timer = QtCore.QTimer()
timer.timeout.connect(Spring)
timer.start(5)



Bye!
 :D