Epitrochoid.py

Go to the documentation of this file.
00001 
00002 from __future__ import division # allows floating point division from integers
00003 import FreeCAD, Part, math
00004 from FreeCAD import Base
00005 
00006 class Epitrochoid:
00007     def __init__(self, obj):
00008         ''' Add the properties: Radius1, Radius2, Distance, Segments '''
00009         obj.addProperty("App::PropertyLength","Radius1","Epitrochoid","Interior radius").Radius1=60.0
00010         obj.addProperty("App::PropertyLength","Radius2","Epitrochoid","Exterior radius").Radius2=20.0
00011         obj.addProperty("App::PropertyLength","Distance","Epitrochoid","Distance").Distance=10.0
00012         obj.addProperty("App::PropertyInteger","Segments","Epitrochoid","Number of the line segments").Segments=72
00013         obj.Proxy = self
00014 
00015     def onChanged(self, fp, prop):
00016         if prop == "Radius1" or prop == "Radius2" or prop == "Distance" or prop == "Segments": #if one of these is changed
00017             self.execute(fp)
00018 
00019     def execute(self, fp): #main part of script
00020         steps=fp.Segments  #get value from property
00021         dang=math.radians(360/steps)
00022         r2=fp.Radius2
00023         r1=fp.Radius1
00024         f1 = r1 + r2
00025         f2 = f1 / r2
00026         d=fp.Distance
00027         ang=0
00028         z=0
00029         
00030         if r2 == 0:
00031             raise Exception("Exterior radius must not be zero")
00032 
00033         for i in range(steps):
00034             if i==0:
00035                 x1=f1*math.cos(ang)-d*math.cos(f2*ang) #coords for line startpoint
00036                 y1=f1*math.sin(ang)-d*math.sin(f2*ang)
00037                 ang=dang
00038                 x2=f1*math.cos(ang)-d*math.cos(f2*ang) #coords for line endpoint
00039                 y2=f1*math.sin(ang)-d*math.sin(f2*ang)
00040                 seg=Part.makeLine((x1,y1,z),(x2,y2,z))
00041                 wire=Part.Wire([seg])
00042                 x1=x2
00043                 y1=y2
00044             else:
00045                 x2=f1*math.cos(ang)-d*math.cos(f2*ang)
00046                 y2=f1*math.sin(ang)-d*math.sin(f2*ang)
00047                 seg=Part.makeLine((x1,y1,z),(x2,y2,z))
00048                 wire=Part.Wire([wire,seg])
00049                 x1=x2
00050                 y1=y2
00051             ang=ang+dang #increment angle
00052         fp.Shape = wire  #result shape
00053 
00054 def makeEpitrochoid():
00055     doc = FreeCAD.activeDocument()
00056     if doc == None:
00057         doc = FreeCAD.newDocument()
00058     epitrochoid=doc.addObject("Part::FeaturePython","Epitrochoid") #add object to document
00059     epitrochoid.Label = "Epitrochoid"
00060     Epitrochoid(epitrochoid)
00061     epitrochoid.ViewObject.Proxy=0
00062     doc.recompute()
00063 
00064 if __name__ == "__main__": #feature will be generated after macro execution
00065     makeEpitrochoid()

Generated on Wed Nov 23 19:00:10 2011 for FreeCAD by  doxygen 1.6.1