跳轉到內容

OpenSCAD 使用者手冊/WIP/遷移指南

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

OpenSCAD 2019 包含了一些破壞舊設計或對之前沒有錯誤和警告的程式碼產生錯誤和警告的更改。

重複賦值警告

[編輯 | 編輯原始碼]

在以下情況下,OpenSCAD 2019 會在重複賦值的情況下產生警告

  • 在同一個檔案和範圍內覆蓋變數
  • 主檔案中的賦值被包含檔案覆蓋

在以下情況下,變數會由最後一個賦值靜默覆蓋

  • 透過命令列賦值
  • 包含檔案中的變數被主檔案覆蓋

未初始化的特殊變數

[編輯 | 編輯原始碼]

這段程式碼現在會導致警告,因為 $test 沒有被初始化

test = $test==undef ? 1 : $test ;

解決方法是使用新的 is_undef() 函式

test = is_undef($test) ? 1 : $test ;

模組和函式引數

[編輯 | 編輯原始碼]

引數型別

[編輯 | 編輯原始碼]

內建函式和模組現在會在引數型別不匹配時發出警告。

由於型別檢查以前是透過使用內建函式返回 undef 來完成的,因此現在建議/要求使用 is_list()、is_num()、is_bool() 和 is_string() 來進行型別檢查。

重複引數

[編輯 | 編輯原始碼]

OpenSCAD 2019 會在多次提供命名引數時發出警告。

cube(size=2,size=1);

未指定引數

[編輯 | 編輯原始碼]

OpenSCAD 現在會在模組或函式引數未宣告時發出警告。

使用者模組

[編輯 | 編輯原始碼]

這意味著現在會導致警告

a=1;

module test(){
    cube(a);
}

test();

translate([2,0,0])
test(a=2);

如果您打算這樣做 - 比如在 Write.scad 庫中 - 您可以按如下方式更新程式碼

a=1;

module test(a){
    cube(a);
}

test();

translate([2,0,0])
test(a=2);

對於 Write.scad,具體來說:像這樣的定義

module writecircle(text,where,radius){

需要更改為

module writecircle(text, where, radius, rotate, font, h, t, spac, east, west, middle, ccw){
</syntaxhighlight lang="unknown">
==== builtin modules ====
Same goes for builtin modules, so this now creates a warning:
<syntaxhighlight lang="text">
sphere(center=true);

以及

circle(center=true);

circle 和 sphere 都不支援 center 引數 - circle 和 sphere 始終居中。

另一個例子是

circle(radius=5);

因為 circle 接受 r,而不是 radius。

引數被文字覆蓋

[編輯 | 編輯原始碼]
module test(a){
    a=2;
    sphere(a);
}

test();

translate([4,0,0])
test(a=1);

請注意,這個非常類似的示例不會觸發警告,因為實現中有缺陷

module test(a){
    a=1+1;
    sphere(a);
}

test();

translate([4,0,0])
test(a=2);

內建模組的引數範圍檢查

[編輯 | 編輯原始碼]

像這樣的

cube(-1);
cube([1,1,-1]);
cube(0);
cube([1,1,0]);

內建模組的引數衝突檢查

[編輯 | 編輯原始碼]
cylinder(h = 1, r1 = 0, r2 = 1, d = 2, center = true);

內建函式的引數計數

[編輯 | 編輯原始碼]

如果內建函式的引數計數不匹配,OpenSCAD 2019 會顯示警告

echo(abs(123,456));

內建 Assert 不能被使用者函式或模組覆蓋。如果您有一個名為 assert 的函式或模組,則必須重新命名它。

華夏公益教科書