Skip to content

Instantly share code, notes, and snippets.

@5263
Last active August 29, 2015 14:07
Show Gist options
  • Save 5263/1e9c3266574fd9aebf29 to your computer and use it in GitHub Desktop.
Save 5263/1e9c3266574fd9aebf29 to your computer and use it in GitHub Desktop.
Free form turning
import FreeCAD
import Part
def rayshape(phi=0,z=0,r=1e3):
import math
x=r*math.cos(phi)
y=r*math.sin(phi)
l=Part.Line(FreeCAD.Vector(0,0,z),FreeCAD.Vector(x,y,z))
return l.toShape()
def intersectionz(s,phi,r,z=1e3):
import math
x=r*math.cos(phi)
y=r*math.sin(phi)
l=Part.Line(FreeCAD.Vector(x,y,0),FreeCAD.Vector(x,y,z))
e=l.toShape()
dist, pts, geom = e.distToShape(s)
if dist < 1e-4:
maxz = max([p1.z for p1,p2 in pts])
return (r,phi,maxz)
def intersection(s,phi,z,r=1e3):
import math
dist, pts, geom = rayshape(phi,z,r).distToShape(s)
if dist < 1e-4:
radii = [math.sqrt(p1.x**2+p1.y**2) for p1,p2 in pts]
maxr = max(radii)
return (maxr,phi,z)
else:
pass
#print dist
#print pts
#print geom
#print dist, rayshape(phi,z,r).Edges[0].Curve
#print pts,geom
def freeformalonghelixz(s,rmax,rmin=0,steps=10,maxz=None,offsetz=0,pitch=0.1):
for i in range(steps):
frac=i/(steps-1.0)
r=rmin+(rmax-rmin)*frac
phi=(r/pitch)*2*math.pi
inter=intersectionz(s,phi,r)
if inter is not None:
r,phi,z = inter
z += offsetz # multipass roughing
if maxz: # limitation of machine
z = max(z,maxz)
print 'C%1.6fX%1.6fZ%1.6fY%1.4f' % (math.degrees(phi),r,z,0)
def freeformalonghelix(s,zmin=0,zmax=10,pitch=2.0,steps=10,maxr=None,offsetr=0):
for i in range(steps):
frac=i/(steps-1.0)
z=zmax-(zmax-zmin)*frac
phi = (zmax-zmin)* pitch * frac
inter=intersection(s,phi,z)
if inter is not None:
r,phi,z = inter
r += offsetr # multipass roughing
if maxr: # limitation of machine
r = max(r,maxr)
print 'C%1.6fX%1.6fZ%1.6fY%1.4f' % (math.degrees(phi),r,z,0)
def test():
s=Part.makeCylinder(2,2).Shells[0]
s.translate(FreeCAD.Vector(0.1,0.2,0.4))
zmax=s.BoundBox.ZMax
zmin=s.BoundBox.ZMin
freeformalonghelix(s,zmax=zmax,zmin=zmin)
if __name__ == '__main__':
import FreeCADGui
for obj in FreeCADGui.Selection.getSelection():
for i, shell in enumerate(obj.Shape.Shells):
print '; %s Shell %d' % (obj.Name,i)
zmax=shell.BoundBox.ZMax
zmin=shell.BoundBox.ZMin
rmax=shell.BoundBox.XMax #not the best idea
for passnumber in range(4,-1,-1):
offset = passnumber * 0.5 #leave that much material for remeaning passes
print '; pass with offset %f' %offset
print '; todo move to save position'
#freeformalonghelix(shell,zmax=zmax,zmin=zmin,steps=20,pitch=3,offsetr=offset)
shell=shell.toNurbs() #warning: degrades performance, but needed to process spherical segments
freeformalonghelixz(shell,rmax=rmax,steps=50,pitch=3,offsetz=offset)
print '; todo move to save position'
#finishing
#freeformalonghelix(shell,zmax=zmax,zmin=zmin,steps=5,pitch=10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment