Sunday, April 15, 2012

Cloud CADCAM via twisted python

I have been working a bit with Julian Todd of FreeSteel . We have started working on a way of serving out toolpaths, using his 'Slicer' routine from a server. The server takes triangle vertices in and returns tool location paths, for waterline type machining. Julian is very familiar with twisted python and plans on using it for his own server. At the moment, I am running his library locally.

Here is an image of a part that I designed in FreeCAD and applied this 'Slicer' scheme to:



The python code that I used for this doesn't post process into actual g-code, but it could.

import Mesh
import Part,FreeCAD
from FreeCAD import Base,Vector
import freesteelpy as kernel

s=Gui.Selection.getSelectionEx()
s1 = s[0]
m1 = s1.Object
m1.Mesh.write("/home/danfalck/mesh1.py") #write the triangle list to a file
f1 = m1.Mesh.Facets #returns out a list of facets

fssurf = kernel.FsSurf.New()
for f in f1:
fssurf.PushTriangle(f.Points[0][0],f.Points[0][1],f.Points[0][2], f.Points[1][0],f.Points[1][1],f.Points[1][2], \
f.Points[2][0],f.Points[2][1],f.Points[2][2])

def doit(fssurf, z , rad):
fssurf.Build(1.0)

fshoriztoolsurf = kernel.FsHorizontalToolSurface.New()
fshoriztoolsurf.AddSurf(fssurf)
fshoriztoolsurf.AddCylinder(rad, z, 10.0, 0) #cylinder radius, lower Z, upper Z, how the cylinder is connected to the rest of the tool -tangential etc.
fsimplicitarea = kernel.FsImplicitArea.New(0)
fsimplicitarea.AddHorizToolSurf(fshoriztoolsurf)
fsimplicitarea.SetContourConditions(0.99, -1.0, 0.002, 2, -1.0, 0.9)
fsweave = kernel.FsWeave.New()
fsweave.SetShape(-25, 25, -25, 25,.1) #(-x, -y, x , y of bounding area, last arg is granularity of path)
fsimplicitarea.GenWeaveZProfile(fsweave)
fsweave.GetNContours()

fspath2x = kernel.FsPath2X.New(0)
fspath2x.RecordContour(fsweave, False, 0, 0.0)

fspath2x.GetNpts()

[ (fspath2x.GetD(i,0), fspath2x.GetD(i,1)) for i in range(fspath2x.GetNpts()) ]

[ (fspath2x.GetD(i,0), fspath2x.GetD(i,1), fspath2x.GetD(i,2)) for i in range(fspath2x.GetNpts()) ]

pt0 = (fspath2x.GetD(0,0), fspath2x.GetD(0,1), fspath2x.GetD(0,2))

#FreeCAD.ActiveDocument.addObject("Part::Feature","rapids)\n")
c = Part.Compound([])

for i in range(fspath2x.GetNpts()-1):
line = ( Part.makeLine( (fspath2x.GetD(i,0), fspath2x.GetD(i,1), fspath2x.GetD(i,2) ) , ( fspath2x.GetD(i+1,0), fspath2x.GetD(i+1,1), fspath2x.GetD(i+1,2) ) ) )
c.add(line)

c.Placement=Base.Placement(Base.Vector(0.00,0.00,z),Base.Rotation(0.00,0.00,0.00,1.00))

Part.show(c)

rad = 1.0
startdepth = 2.5
zdepth = -3.0
stepdown = .25
steps = startdepth

while startdepth >= zdepth:
z = startdepth
doit(fssurf,z,rad)
startdepth =startdepth-stepdown


Sorry for the poor formatting of the source code- I need to figure that out soon.