OpenSCAD用戶手冊/CSG 建模

維基教科書,自由的教學讀本


布爾操作概述[編輯]

2D示例[編輯]
 union()       {square(10);circle(10);} // 正方形 或   圆形
 difference()  {square(10);circle(10);} // 正方形 与非 圆形
 difference()  {circle(10);square(10);} // 圆形   与非 正方形
 intersection(){square(10);circle(10);} // 正方形 与   圆形
3D示例[編輯]
 union()       {cube(12, center=true); sphere(8);} // 立方体 或   球体
 difference()  {cube(12, center=true); sphere(8);} // 立方体 与非 球体
 difference()  {sphere(8); cube(12, center=true);} // 球体   与非 立方体
 intersection(){cube(12, center=true); sphere(8);} // 立方体 与   球体

union[編輯]

創建所有子節點的併集。即,所有子節點之(邏輯).
可用於2D對象或3D對象, 但不能兩者混用。

併集

 //举个栗子:
 union() {
 	cylinder (h = 4, r=1, center = true, $fn=100);
 	rotate ([90,0,0]) cylinder (h = 4, r=0.9, center = true, $fn=100);
 }

備註: union is implicit when not used. But it is mandatory, for example, in difference to group first child nodes into one.

difference[編輯]

從第一個子節點集中減去第二個(以及指定的其他)子節點集(邏輯與非).
可用於2D對象或3D對象, 但不能兩者混用。

差集

用例:
difference() {
	cylinder (h = 4, r=1, center = true, $fn=100);
	rotate ([90,0,0]) cylinder (h = 4, r=0.9, center = true, $fn=100);
}
減去多個子對象[編輯]

請注意,在第二個實例中,加入了第一個子對象與第二個子對象的併集操作。

// 减去多个子对象的用例:
$fn=90;
difference(){
                                            cylinder(r=5,h=20,center=true);
    rotate([00,140,-45]) color("LightBlue") cylinder(r=2,h=25,center=true);
    rotate([00,40,-50])                     cylinder(r=2,h=30,center=true);
    translate([0,0,-10])rotate([00,40,-50]) cylinder(r=1.4,h=30,center=true);
}
   
// 第二个实例在第一个实例的基础上添加了一个并集操作
translate([10,10,0]){
    difference(){
      union(){        // 将第一个子对象与第二个子对象组合起来
                                                cylinder(r=5,h=20,center=true);
        rotate([00,140,-45]) color("LightBlue") cylinder(r=2,h=25,center=true);
      }
      rotate([00,40,-50])                       cylinder(r=2,h=30,center=true);
      translate([0,0,-10])rotate([00,40,-50])   cylinder(r=1.4,h=30,center=true);
    }
}

intersection[編輯]

創建所有子節點的交集。這將保留集合間的重合部分(邏輯).
只有所有子對象共用或共享的部分才會被保留下來。
可用於2D對象或3D對象, 但不能兩者混用。

交集

//用例:
intersection() {
	cylinder (h = 4, r=1, center = true, $fn=100);
	rotate ([90,0,0]) cylinder (h = 4, r=0.9, center = true, $fn=100);
}


render[編輯]

警告:使用渲染函數時,總是針對子對象樹計算其CSG模型(在OpenCSG預覽模式下也是如此)。這將導致預覽變得極慢,並發生OpenSCAD短暫掛起並處於凍結狀態。

用例:
render(convexity = 1) { ... }
convexity 此參數為整數。此凸性參數用於指定:一射線貫穿物體的前後側時,兩者相交可達到的最多次數。此參數僅用於在OpenCSG預覽模式下正確地顯示對象,而對於多面體的渲染並無影響。


此圖像展示的是凸性參數為4的一個2D圖形,這是因為紅色射線穿過該圖形時與之最多相交4次。利用同樣的方法亦可確定3D圖形的凸性。在大多數情況下,將其設置為10就能夠順利地完成渲染工作。