跳到內容

OpenSCAD 使用者手冊/2D 圖元

來自華夏公益教科書,開放的書籍,開放的世界

所有 2D 圖元都可以使用 3D 變換進行變換。它們通常用作 3D 擠出的部分。雖然它們無限薄,但它們以 1 個單位的厚度渲染。

注意:嘗試使用difference()從 3D 物件中減去,會導致最終渲染出現意外結果。

正方形

[編輯 | 編輯原始碼]

在第一象限建立一個正方形或矩形。當center為真時,正方形將以原點為中心。如果按這裡顯示的順序給出,引數名稱是可選的。

square(size = [x, y], center = true/false);
square(size =  x    , center = true/false);
引數:
大小
單個值,兩邊長度都為該值的正方形
2 值陣列 [x,y],尺寸為 x 和 y 的矩形
中心
false(預設值),第 1 象限(正),一個角在 (0,0)
true,正方形以 (0,0) 為中心
default values:  square();   yields:  square(size = [1, 1], center = false);
示例:

10x10 square

equivalent scripts for this example
 square(size = 10);
 square(10);
 square([10,10]);
 .
 square(10,false);
 square([10,10],false);
 square([10,10],center=false);
 square(size = [10, 10], center = false);
 square(center = false,size = [10, 10] );

OpenScad square 20x10

equivalent scripts for this example
 square([20,10],true);
 a=[20,10];square(a,true);

在原點建立一個圓形。除了 r 之外,所有引數必須命名。

circle(r=radius | d=diameter);
引數
r : 圓形半徑。r 名稱是圓形中唯一可選的名稱。
圓形解析度基於大小,使用 $fa 或 $fs。
對於一個小而高解析度的圓形,可以建立一個大圓形,然後將其縮小,或者可以設定 $fn 或其他特殊變數。注意:這些示例超出了 3D 印表機以及顯示屏的解析度。
scale([1/100, 1/100, 1/100]) circle(200); // create a high resolution circle with a radius of 2.
circle(2, $fn=50);                        // Another way.
d  : 圓形直徑(僅在 2014.03 之後的版本中可用)。
$fa : 每個片段的最小角度(以度為單位)。
$fs : 每個片段的最小圓周長度。
$fn : 360 度中固定的片段數。值大於或等於 3 將覆蓋 $fa 和 $fs。
如果使用,$fa、$fs 和 $fn 必須是命名引數。點選這裡瞭解更多詳情
defaults:  circle(); yields:  circle($fn = 0, $fa = 12, $fs = 2, r = 1);

circle for user manual description

此示例的等效指令碼

 circle(10);
 circle(r=10);
 circle(d=20);
 circle(d=2+9*2);

可以使用scale()resize()使 x 和 y 尺寸不相等,從圓形建立橢圓。請參閱OpenSCAD 使用者手冊/變換

Ellipse from circle Ellipse from circle top view

equivalent scripts for this example
 resize([30,10])circle(d=20);
 scale([1.5,.5])circle(d=20);

正多邊形

[編輯 | 編輯原始碼]

可以使用circle()並將 $fn 設定為邊數來建立 3 個或更多邊的正多邊形。以下兩段程式碼是等效的。

 circle(r=1, $fn=4);
 module regular_polygon(order = 4, r=1){
     angles=[ for (i = [0:order-1]) i*(360/order) ];
     coords=[ for (th=angles) [r*cos(th), r*sin(th)] ];
     polygon(coords);
 }
 regular_polygon();

這些會產生以下形狀,其中多邊形內接於圓形,所有邊(和角)都相等。一個角指向正 x 方向。對於不規則形狀,請參見下面的多邊形圖元。

script for these examples
 translate([-42,  0]){circle(20,$fn=3);%circle(20,$fn=90);}
 translate([  0,  0]) circle(20,$fn=4);
 translate([ 42,  0]) circle(20,$fn=5);
 translate([-42,-42]) circle(20,$fn=6);
 translate([  0,-42]) circle(20,$fn=8);
 translate([ 42,-42]) circle(20,$fn=12);
 
 color("black"){
     translate([-42,  0,1])text("3",7,,center);
     translate([  0,  0,1])text("4",7,,center);
     translate([ 42,  0,1])text("5",7,,center);
     translate([-42,-42,1])text("6",7,,center);
     translate([  0,-42,1])text("8",7,,center);
     translate([ 42,-42,1])text("12",7,,center);
 }

多邊形

[編輯 | 編輯原始碼]

函式 polygon() 從 x,y 座標列表建立一個多邊形。多邊形是最強大的 2D 物件。它可以建立圓形和正方形可以建立的任何東西,以及更多。這包括具有凹邊和凸邊的不規則形狀。此外,它可以在該形狀內放置孔。

polygon(points = [ [x, y], ... ], paths = [ [p1, p2, p3..], ...], convexity = N);
引數
多邊形的 x,y 點列表。 : 2 元素向量組成的向量。
注意:點從 0 到 n-1 索引。
路徑
預設值
如果沒有指定路徑,將按列表中的順序使用所有點。
單個向量
遍歷點的順序。使用從 0 到 n-1 的索引。可能按不同的順序,並使用所有或部分列出的點。
多個向量
建立主要形狀和次要形狀。次要形狀從主要形狀中減去(就像difference())。次要形狀可能完全或部分位於主要形狀內。
閉合形狀是透過從指定的最後一點返回到第一點來建立的。
凸性
"向內"曲線的整數,即穿過多邊形的任意直線的預期路徑交叉。見下文。
defaults:   polygon();  yields:  polygon(points = undef, paths = undef, convexity = 1);

equivalent scripts for this example
 polygon(points=[[0,0],[100,0],[130,50],[30,50]]);
 polygon([[0,0],[100,0],[130,50],[30,50]], paths=[[0,1,2,3]]);
 polygon([[0,0],[100,0],[130,50],[30,50]],[[3,2,1,0]]);
 polygon([[0,0],[100,0],[130,50],[30,50]],[[1,0,3,2]]);
    
 a=[[0,0],[100,0],[130,50],[30,50]];
 b=[[3,0,1,2]];
 polygon(a);
 polygon(a,b);
 polygon(a,[[2,3,0,1,2]]);

一個孔

[編輯 | 編輯原始碼]

equivalent scripts for this example
 polygon(points=[[0,0],[100,0],[0,100],[10,10],[80,10],[10,80]], paths=[[0,1,2],[3,4,5]],convexity=10);

 triangle_points =[[0,0],[100,0],[0,100],[10,10],[80,10],[10,80]];
 triangle_paths =[[0,1,2],[3,4,5]];
 polygon(triangle_points,triangle_paths,10);

第一個路徑向量 [0,1,2] 選擇點 [0,0]、[100,0]、[0,100] 作為主要形狀。第二個路徑向量 [3,4,5] 選擇點 [10,10]、[80,10]、[10,80] 作為次要形狀。次要形狀從主要形狀中減去(想想difference())。由於次要形狀完全位於主要形狀內,它會留下一個有孔的形狀。

[注意: 需要 2015.03 版本](用於使用concat()


      //example polygon with multiple holes
a0 = [[0,0],[100,0],[130,50],[30,50]];     // main
b0 = [1,0,3,2];
a1 = [[20,20],[40,20],[30,30]];            // hole 1
b1 = [4,5,6];
a2 = [[50,20],[60,20],[40,30]];            // hole 2
b2 = [7,8,9];
a3 = [[65,10],[80,10],[80,40],[65,40]];    // hole 3
b3 = [10,11,12,13];
a4 = [[98,10],[115,40],[85,40],[85,10]];   // hole 4
b4 = [14,15,16,17];
a  = concat (a0,a1,a2,a3,a4);
b  = [b0,b1,b2,b3,b4];
polygon(a,b);
      //alternate 
polygon(a,[b0,b1,b2,b3,b4]);

從多邊形擠出 3D 形狀

[編輯 | 編輯原始碼]

   translate([0,-20,10]) {
       rotate([90,180,90]) {
           linear_extrude(50) {
               polygon(
                   points = [
                      //x,y
                       /*
                                  O  .
                       */
                       [-2.8,0],
                       /*
                                O__X  .
                       */
                       [-7.8,0],
                       /*
                              O
                               \
                                X__X  .
                       */
                       [-15.3633,10.30],
                       /*
                              X_______._____O
                               \         
                                X__X  .
                       */
                       [15.3633,10.30],
                       /*
                              X_______._______X
                               \             /
                                X__X  .     O
                       */
                       [7.8,0],
                       /*
                              X_______._______X
                               \             /
                                X__X  .  O__X
                       */
                       [2.8,0],
                       /*
                           X__________.__________X
                            \                   /
                             \              O  /
                              \            /  /
                               \          /  /
                                X__X  .  X__X
                       */
                       [5.48858,5.3],
                       /*
                           X__________.__________X
                            \                   /
                             \   O__________X  /
                              \            /  /
                               \          /  /
                                X__X  .  X__X
                       */
                       [-5.48858,5.3],
                                   ]
                               );
                           }
       }
   }

凸性引數指定與物件相交的射線可能穿透的最大正面(背面)數。此引數僅在 OpenCSG 預覽模式中正確顯示物件時需要,對多面體渲染沒有影響。

此影像顯示了一個凸性為 2 的 2D 形狀,因為以紅色指示的射線交叉了 2D 形狀,外部⇒內部(或內部⇒外部)最多 2 次。3D 形狀的凸性將以類似的方式確定。將其設定為 10 應該適用於大多數情況。

import_dxf

[編輯 | 編輯原始碼]

[已棄用: import_dxf() 將在將來的版本中刪除。 請改用import()]

讀取 DXF 檔案並建立一個 2D 形狀。

示例

linear_extrude(height = 5, center = true, convexity = 10)
		import_dxf(file = "example009.dxf", layer = "plate");
華夏公益教科書