使用if与openSCAD有所不同

时间:2018-08-29 12:59:10

标签: openscad

我想在openSCAD中创建模型 然后我想在其中切一个洞(使用差异)

所以我可以做类似的事情

module model_with_hole( hole=false) {
   difference() {
         //the_model()
         if (hole) {
            //the_hole()
         }
   }
}

但这实际上是在说诸如“总是从模型中切出某些东西,只是如果不需要孔,所切出的东西什么都不是,”总是这样。

另一种选择是:

module model_with_hole( hole=false) {
   if (hole) {
       difference() {
         //the_model()
         //the_hole()
       }
   }
   else {
         //the_model()  
   }
}

但这实际上是在说诸如“如果需要孔,则渲染模型并删除孔,否则仅渲染模型”。

是否有一种编码方式,使得调用模型的调用仅存在一次,并且差异操作仅在需要时才会发生?

if (hole) {the_hole()} the_model();

因此,代码更像是说渲染模型,如果需要切孔?

1 个答案:

答案 0 :(得分:0)

也许这就是您想要的: 将孔的参数添加到向量中,并在difference()的for循环中使用此向量。如果向量为空,则不从模型中减去任何东西,请尝试以下四个示例:

module model(l) {
    cube(size = l, center = true);
}

module hole(pos, dim) {
    translate(pos) cylinder(h = dim[0] + 0.1, r = dim[1], center = true);
}

// holes = [];
// holes = [[[0,0,0],[10, 1]]];
holes = [[[-2.5,-2.5,0],[10, 0.5]], [[0,0,0],[10, 1]], [[2.5,2.5,0],[10, 1.5]]];
// holes = [[[-2.5,-2.5,0],[10, 1]], [[-2.5,2.5,0],[10, 1]], [[2.5,2.5,0],[10, 1]], [[2.5,-2.5,0],[10, 1]], [[0,0,0],[10, 1]]];

difference() {
    model(10);
    for (h = holes) hole(h[0], h[1]);
}