openscad - 在 openSCAD 中使用 if 与差异
问题描述
我想在 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();
所以代码感觉更像是在说渲染模型和 if 需要切洞?
解决方案
也许这就是您想要的:将孔的参数添加到向量中,并在 for 循环中使用该向量difference()
。如果向量为空,则模型中不减去任何内容,请尝试以下四个示例:
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]);
}