OpenSCAD支持變量和循環,從而能夠快速複製出大量的幾何對象而且按照遞歸的方式進行佈局。shell
循環的變量能夠是枚舉、區間和矢量對象,循環體支持幾何對象構建、座標平移與旋轉、交併差等操做。數組
Vector(矢量):
oop
for (variable=<vector>) { <do_something> - <variable> is assigned to each successive value in the vector }
Range(區間、範圍):
佈局
for (variable=<range>) { <do_something> }
Nested(嵌套) :
this
for ( variable1 = <range or vector>, variable2 = <range or vector> ) { <do something, using both variables> }
for 循環能夠進行嵌套,就像普通程序同樣。
spa
Usage example 1 - 經過vector的循環調用 | |
for (z = [-1, 1]) // 兩個變量, z = -1, z = 1 { translate([0, 0, z]) cube(size = 1, center = false); } |
使用範圍型變量的循環 |
Usage example 2a - 經過範圍實現循環 | |
for ( i = [0 : 5] ) { rotate( i * 360 / 6, [1, 0, 0]) translate([0, 10, 0]) sphere(r = 1); } |
使用範圍型給定步長的循環 |
Usage example 2b -指定步長和範圍的循環 |
// Note: The middle parameter in the range designation // ('0.2' in this case) is the 'increment-by' value // Warning: Depending on the 'increment-by' value, the // real end value may be smaller than the given one. //相似於c語言的for(i=0;i<5;i+=0.2)... for ( i = [0 : 0.2 : 5] ) { rotate( i * 360 / 6, [1, 0, 0]) translate([0, 10, 0]) sphere(r = 1); } |
Usage example 3 - 經過矢量的循環 (旋轉) | |
for(i = [ [ 0, 0, 0], [ 10, 20, 300], [200, 40, 57], [ 20, 88, 57] ]) { rotate(i) cube([100, 20, 20], center = true); } |
經過矢量數組的枚舉型循環 |
Usage example 4 -矢量數組的循環(位移): | |
for(i = [ [ 0, 0, 0], [10, 12, 10], [20, 24, 20], [30, 36, 30], [20, 48, 40], [10, 60, 50] ]) { translate(i) cube([50, 15, 10], center = true); } |
循環的嵌套和多變量 |
嵌套循環的例程.net
for (xpos=[0:3], ypos = [2,4,6]) // do twelve iterations, using each xpos with each ypos translate([xpos*ypos, ypos, 0]) cube([0.5, 0.5, 0.5]);
全部的循環(包括枚舉值、範圍、矢量、矢量數組)都支持幾何實體的 intersection 操做。code
注意: intersection_for()
is a work around because of an issue that you cannot get the expected results using a combination of the standard for()
and intersection()
statements. The reason is that for()
do a implicit union()
of the contents.對象
參數遞歸
<loop variable name>
Name of the variable to use within the for loop.
Usage example 1 - 範圍循環: | |
intersection_for(n = [1 : 6]) { rotate([0, 0, n * 60]) { translate([5,0,0]) sphere(r=12); } } |
Intersection for使用示例: |
Usage example 2 - rotation : | |
intersection_for(i = [ [ 0, 0, 0], [ 10, 20, 300], [200, 40, 57], [ 20, 88, 57] ]) { rotate(i) cube([100, 20, 20], center = true); } |