Funny you should ask....
Let's trade code --- I'll take yours and see if I can get it to work, and if you could take a moment to look at the following...
First, every so often when loading/working with these files I get the message:
>ERROR: File "<string>", line 1
> //!OpenSCAD
> ^^
>SyntaxError: invalid syntax
(which I'm baffled by)
I have the file gcodereview.py (a much shortened version of the full gcodepreview.py which I've been using for testing/debugging):
import sys
import math
from openscad import *
class gcodereview:
def __init__(self):
self.mc = "Initialized"
self.mpx = float(0)
self.mpy = float(0)
self.mpz = float(0)
self.tpz = float(0)
self.currenttoolnum = 102
def xpos(self):
return self.mpx
def ypos(self):
return self.mpy
def zpos(self):
return self.mpz
def setupstock(self, stockXwidth,
stockYheight,
stockZthickness,
zeroheight,
stockzero,
retractheight):
self.toolpaths = []
stock = cube([stockXwidth, stockYheight, stockZthickness])
self.stock = stock.translate([0,0,-stockZthickness])
def rapid(self, ex, ey, ez):
self.mpx = ex
self.mpy = ey
self.mpz = ez
def xpos(self):
return self.mpx
def ypos(self):
return self.mpy
def zpos(self):
return self.mpz
def setxpos(self, ex):
self.mpx = ex
def setypos(self, ey):
self.mpy = ey
def setzpos(self, ez):
self.mpz = ez
def cutline(self, ex, ey, ez):
tool = cylinder(d = 1.5875, h = 12)
tpb = tool.translate([self.xpos(), self.ypos(), self.zpos()])
tpe = tool.translate([ex, ey, ez])
toolpath = hull(tpb, tpe)
self.mpx = ex
self.mpy = ey
self.mpz = ez
self.toolpaths.append(toolpath)
def cutquarterCCNE(self, ex, ey, ez, radius):
if self.zpos() == ez:
tpzinc = 0
else:
tpzinc = (ez - self.zpos()) / 90
i = 1
while i < 91:
self.cutline(ex + radius * Cos(i), ey - radius + radius * Sin(i), self.zpos()+tpzinc)
i += 1
def cutquarterCCNW(self, ex, ey, ez, radius):
if self.zpos() == ez:
tpzinc = 0
else:
tpzinc = (ez - self.zpos()) / 90
i = 91
while i < 181:
self.cutline(ex + radius + radius * Cos(i), ey + radius * Sin(i), self.zpos()+tpzinc)
i += 1
def cutquarterCCSW(self, ex, ey, ez, radius):
if self.zpos() == ez:
tpzinc = 0
else:
tpzinc = (ez - self.zpos()) / 90
i = 181
while i < 271:
self.cutline(ex + radius * Cos(i), ey + radius + radius * Sin(i), self.zpos()+tpzinc)
i += 1
def cutquarterCCSE(self, ex, ey, ez, radius):
if self.zpos() == ez:
tpzinc = 0
else:
tpzinc = (ez - self.zpos()) / 90
i = 271
while i < 361:
self.cutline(ex - radius + radius * Cos(i), ey + radius * Sin(i), self.zpos()+tpzinc)
i += 1
def stockandtoolpaths(self):
part = self.stock.difference(self.toolpaths)
show(part)
def returnstockandtoolpaths(self):
part = self.stock.difference(self.toolpaths)
return part
def showstock(self):
return self.stock
(attached)
when called by a Python file, it works as expected:
#!/usr/bin/env python
import sys
try:
if 'gcodereview' in sys.modules:
del sys.modules['gcodereview']
except AttributeError:
pass
from gcodereview import *
fa = 2
fs = 0.125
# [Stock] */
stockXwidth = 220
# [Stock] */
stockYheight = 150
# [Stock] */
stockZthickness = 8.35
gcp = gcodereview()
gcp.setupstock(stockXwidth,stockYheight,stockZthickness,"Top","Center",9)
gcp.cutline(stockXwidth, stockYheight, -stockZthickness)
gcp.rapid(stockXwidth/2, stockYheight/2, 0)
gcp.cutquarterCCNE(gcp.xpos() - stockYheight/8, gcp.ypos() + stockYheight/8, -stockZthickness/4, stockYheight/8)
gcp.cutquarterCCNW(gcp.xpos() - stockYheight/8, gcp.ypos() - stockYheight/8, -stockZthickness/2, stockYheight/8)
gcp.cutquarterCCSW(gcp.xpos() + stockYheight/8, gcp.ypos() - stockYheight/8, -stockZthickness * 0.75, stockYheight/8)
gcp.cutquarterCCSE(gcp.xpos() + stockYheight/8, gcp.ypos() + stockYheight/8, -stockZthickness, stockYheight/8)
gcp.stockandtoolpaths()
(attached)
as shown in the exported pixel image:
Wrapping this up in OpenSCAD code is pretty straight-forward:
//!OpenSCAD
use <gcodereview.py>
module setupstock(stockXwidth, stockYheight, stockZthickness, zeroheight, stockzero, retractheight) {
gcp.setupstock(stockXwidth, stockYheight, stockZthickness, zeroheight, stockzero, retractheight);
}
module rapid(ex, ey, ez){
gcp.rapid(ex,ey,ez);
}
function xpos() = gcp.xpos();
function ypos() = gcp.ypos();
function zpos() = gcp.zpos();
module cutline(ex, ey, ez){
gcp.cutline(ex, ey, ez);
}
module cutquarterCCNE(ex, ey, ez, radius){
gcp.cutquarterCCNE(ex, ey, ez, radius);
}
module cutquarterCCNW(ex, ey, ez, radius){
gcp.cutquarterCCNW(ex, ey, ez, radius);
}
module cutquarterCCSW(ex, ey, ez, radius){
gcp.cutquarterCCSW(ex, ey, ez, radius);
}
module cutquarterCCSE(self, ex, ey, ez, radius){
gcp.cutquarterCCSE(ex, ey, ez, radius);
}
module cutquarterCCNEdxf(ex, ey, ez, radius){
gcp.cutquarterCCNEdxf(ex, ey, ez, radius);
}
module stockandtoolpaths(){
gcp.stockandtoolpaths();
}
module showstockandtoolpaths(){
gcp.returnstockandtoolpaths();
}
module showstock(){
gcp.showstock();
}
(also attached)
but when we make a .scad file to recreate the Python template:
//!OpenSCAD
use <gcodereview.py>
include <gcodereview.scad>
$fa = 2;
$fs = 0.125;
/* [Stock] */
stockXwidth = 219;
/* [Stock] */
stockYheight = 150;
/* [Stock] */
stockZthickness = 8.35;
/* [Stock] */
gcp = gcodereview();
setupstock(stockXwidth, stockYheight, stockZthickness, "Top", "Center",0);
cutline(stockXwidth, stockYheight, -stockZthickness);
rapid(stockXwidth/2, stockYheight/2, 0);
cutquarterCCNE(xpos() - stockYheight/8, ypos() + stockYheight/8, -stockZthickness/4, stockYheight/8);
cutquarterCCNW(xpos() - stockYheight/8, ypos() - stockYheight/8, -stockZthickness/2, stockYheight/8);
cutquarterCCSW(xpos() + stockYheight/8, ypos() - stockYheight/8, -stockZthickness * 0.75, stockYheight/8);
cutquarterCCSE(xpos() + stockYheight/8, ypos() + stockYheight/8, -stockZthickness, stockYheight/8);
showstockandtoolpaths();
(also attached)
one instead gets:
>Parsing design (AST generation)...
>Compiling design (CSG Tree generation)...
>WARNING: Python: 'TypeError("unsupported operand type(s) for -: 'float' and 'NoneType'") in line -1' in file ../../OpenSCAD/libraries/gcodereview.scad, line 36
>WARNING: Python: 'SystemError('<built-in method difference of PyOpenSCAD object at 0x0000017447FAD110> returned a result with an exception set') in line -1' in file ../../OpenSCAD/libraries/gcodereview.scad, line 48
>Rendering Polygon Mesh using Manifold...
>WARNING: No top level geometry to render
which I'm kind of mystified by, since my file doesn't have a line # -1.
William