我想编写一个模块,可以选择将其子项组合为联合或差异。
module u_or_d(option="d") {
if (option == "d") {
difference() children();
} else {
union() children();
}
}
module thing(option) {
u_or_d(option) {
cube(10, center=true);
cylinder(h=20, d=5, center=true);
}
}
translate([-15, 0, 0]) thing("d");
translate([ 15, 0, 0]) thing("u");
我很惊讶这不起作用。事物的两个实例似乎都创建了立方体和圆柱体的联合。
CSG 树转储显示了问题。以下是相关摘录:
difference() {
group() {
cube(size = [10, 10, 10], center = true);
cylinder($fn = 0, $fa = 12, $fs = 2, h = 20, r1 = 2.5, r2 = 2.5, center = true);
}
}
孩子们被包裹在一个组中,所以difference()
实际上只有一个孩子,这恰好是被包裹孩子的隐式联合。
有没有一种方法children()
可以避免这种不需要的分组?如果没有,模块是否有另一种方法允许调用者选择模块如何组合其子模块?