FreeCAD
Spring.py
Go to the documentation of this file.
1 #! python
2 # -*- coding: utf-8 -*-
3 # (c) 2011 Adrian Przekwas LGPL
4 
5 from __future__ import division # allows floating point division from integers
6 import FreeCAD, Part
7 from FreeCAD import Base
8 
9 class MySpring:
10  def __init__(self, obj):
11  ''' Add the properties: Pitch, Diameter, Height, BarDiameter '''
12  obj.addProperty("App::PropertyLength","Pitch","MySpring","Pitch of the helix").Pitch=5.0
13  obj.addProperty("App::PropertyLength","Diameter","MySpring","Diameter of the helix").Diameter=6.0
14  obj.addProperty("App::PropertyLength","Height","MySpring","Height of the helix").Height=30.0
15  obj.addProperty("App::PropertyLength","BarDiameter","MySpring","Diameter of the bar").BarDiameter=3.0
16  obj.Proxy = self
17 
18  def onChanged(self, fp, prop):
19  if prop == "Pitch" or prop == "Diameter" or prop == "Height" or prop == "BarDiameter":
20  self.execute(fp)
21 
22  def execute(self, fp):
23  pitch = fp.Pitch
24  radius = fp.Diameter/2
25  height = fp.Height
26  barradius = fp.BarDiameter/2
27  myhelix=Part.makeHelix(pitch,height,radius)
28  g=myhelix.Edges[0].Curve
29  c=Part.Circle()
30  c.Center=g.value(0) # start point of the helix
31  c.Axis=(0,1,0)
32  c.Radius=barradius
33  p=c.toShape()
34  section = Part.Wire([p])
35  makeSolid=1 #change to 1 to make a solid
36  isFrenet=1
37  myspring=Part.Wire(myhelix).makePipeShell([section],makeSolid,isFrenet)
38  fp.Shape = myspring
39 
41  doc = FreeCAD.activeDocument()
42  if doc == None:
43  doc = FreeCAD.newDocument()
44  spring=doc.addObject("Part::FeaturePython","My_Spring")
45  spring.Label = "My Spring"
46  MySpring(spring)
47  spring.ViewObject.Proxy=0
48  doc.recompute()
49 
50 if __name__ == "__main__":
51  makeMySpring()