继续solidPython,折腾openscad的螺纹建模,关于for的折腾。我理解openscad中的for 语句更应该叫unionfor语句。看看这个例子:
for (i=[0:3])
translate([i*10,0,0])
cube(i+1);
用Py实现一下, 先用枚举法
from solid import *
from solid.utils import *
r = viewscad.Renderer(openscad_exec='/Applications/OpenSCAD.app/Contents/MacOS/OpenSCAD')
def lizi(i = 0):
c = Tx(i * 10)(Cu(1 + i))
return(c)
c = lizi()
c += lizi(1)
c += lizi(2)
c += lizi(3)
c
再用for语句:
for i in range(0, 3):
c = Tx(i * 10)(Cu(1 + i))
c
这是结果:
不对呀!?用下面这个语句试一试
c = 0
for i in range(0, 3):
c += Tx(i * 10)(Cu(1 + i))
c
这是结果:
成功!!完活。。。
再验证一下这个例子:
example 1 for() loop vector of vectors (rotation)
example 1 - iteration over a vector of vectors (rotation) for(i = [ [ 0, 0, 0], [ 10, 20, 300], [200, 40, 57], [ 20, 88, 57] ]) { rotate(i) cube([100, 20, 20], center = true); }
这是Py代码:
s = [[ 0, 0, 0],[ 10, 20, 300],[200, 40, 57],[ 20, 88, 57]]
for i in s:
c += rotate(i)(Cu([100, 20, 20], True))
c
结果不错。