跳轉到內容

OpenSCAD 使用者手冊/在命令列環境中使用 OpenSCAD

來自華夏公益教科書

命令列使用

[編輯 | 編輯原始碼]

OpenSCAD 不僅可以用作 GUI,還可以處理命令列引數。

OpenSCAD 2021.01 具有以下選項

Usage: openscad [options] file.scad
Allowed options:
 --export-format arg          overrides format of exported scad file when
                              using option '-o', arg can be any of its
                              supported file extensions.  For ascii stl
                              export, specify 'asciistl', and for binary stl
                              export, specify 'binstl'.  Ascii export is the
                              current stl default, but binary stl is planned
                              as the future default so asciistl should be
                              explicitly specified in scripts when needed.
 
 -o [ --o ] arg               output specified file instead of running the
                              GUI, the file extension specifies the type: stl,
                              off, wrl, amf, 3mf, csg, dxf, svg, pdf, png,
                              echo, ast, term, nef3, nefdbg (May be used
                              multiple time for different exports). Use '-'
                              for stdout
 
 -D [ --D ] arg               var=val -pre-define variables
 -p [ --p ] arg               customizer parameter file
 -P [ --P ] arg               customizer parameter set
 --enable arg                 enable experimental features (specify 'all' for
                              enabling all available features): roof |
                              input-driver-dbus | lazy-union |
                              vertex-object-renderers |
                              vertex-object-renderers-indexing |
                              vertex-object-renderers-direct |
                              vertex-object-renderers-prealloc | textmetrics
 
 -h [ --help ]                print this help message and exit
 -v [ --version ]             print the version
 --info                       print information about the build process
 
 --camera arg                 camera parameters when exporting png:
                              =translate_x,y,z,rot_x,y,z,dist or
                              =eye_x,y,z,center_x,y,z
 --autocenter                 adjust camera to look at object's center
 --viewall                    adjust camera to fit object
 --imgsize arg                =width,height of exported png
 --render arg                 for full geometry evaluation when exporting png
 --preview arg                [=throwntogether] -for ThrownTogether preview
                              png
 --animate arg                export N animated frames
 --view arg                   =view options: axes | crosshairs | edges |
                              scales | wireframe
 --projection arg             =(o)rtho or (p)erspective when exporting png
 --csglimit arg               =n -stop rendering at n CSG elements when
                              exporting png
 --summary arg                enable additional render summary and statistics:
                              all | cache | time | camera | geometry |
                              bounding-box | area
 --summary-file arg           output summary information in JSON format to the
                              given file, using '-' outputs to stdout
 --colorscheme arg            =colorscheme: *Cornfield | Metallic | Sunset |
                              Starnight | BeforeDawn | Nature | DeepOcean |
                              Solarized | Tomorrow | Tomorrow Night | Monotone
 
 -d [ --d ] arg               deps_file -generate a dependency file for make
 -m [ --m ] arg               make_cmd -runs make_cmd file if file is missing
 -q [ --quiet ]               quiet mode (don't print anything *except*
                              errors)
 --hardwarnings               Stop on the first warning
 --check-parameters arg       =true/false, configure the parameter check for
                              user modules and functions
 --check-parameter-ranges arg =true/false, configure the parameter range check
                              for builtin modules
 --debug arg                  special debug info - specify 'all' or a set of
                              source file names
 -s [ --s ] arg               stl_file deprecated, use -o
 -x [ --x ] arg               dxf_file deprecated, use -o

OpenSCAD 2019.05 具有以下選項

 Usage: openscad [options] file.scad
 Allowed options:
   -o [ --o ] arg               output specified file instead of running the 
                                GUI, the file extension specifies the type: stl,
                                off, amf, 3mf, csg, dxf, svg, png, echo, ast, 
                                term, nef3, nefdbg
                                
   -D [ --D ] arg               var=val -pre-define variables
   -p [ --p ] arg               customizer parameter file
   -P [ --P ] arg               customizer parameter set
   -h [ --help ]                print this help message and exit
   -v [ --version ]             print the version
   --info                       print information about the build process
                                
   --camera arg                 camera parameters when exporting png: 
                                =translate_x,y,z,rot_x,y,z,dist or 
                                =eye_x,y,z,center_x,y,z
   --autocenter                 adjust camera to look at object's center
   --viewall                    adjust camera to fit object
   --imgsize arg                =width,height of exported png
   --render arg                 for full geometry evaluation when exporting png
   --preview arg                [=throwntogether] -for ThrownTogether preview 
                                png
   --view arg                   =view options: axes | crosshairs | edges | 
                                scales | wireframe
   --projection arg             =(o)rtho or (p)erspective when exporting png
   --csglimit arg               =n -stop rendering at n CSG elements when 
                                exporting png
   --colorscheme arg            =colorscheme: *Cornfield | Metallic | Sunset | 
                                Starnight | BeforeDawn | Nature | DeepOcean | 
                                Solarized | Tomorrow | Tomorrow 2 | Tomorrow 
                                Night | Monotone
                                
   -d [ --d ] arg               deps_file -generate a dependency file for make
   -m [ --m ] arg               make_cmd -runs make_cmd file if file is missing
   -q [ --quiet ]               quiet mode (don't print anything *except* 
                                errors)
   --hardwarnings               Stop on the first warning
   --check-parameters arg       =true/false, configure the parameter check for 
                                user modules and functions
   --check-parameter-ranges arg =true/false, configure the parameter range check
                                for builtin modules
   --debug arg                  special debug info
   -s [ --s ] arg               stl_file deprecated, use -o
   -x [ --x ] arg               dxf_file deprecated, use -o

OpenSCAD 2015.03-1 具有以下選項

openscad     [ -o output_file [ -d deps_file ] ]\
             [ -m make_command ] [ -D var=val [..] ] \
             [ --help ] print this help message and exit \
             [ --version ] [ --info ] \
             [ --camera=translatex,y,z,rotx,y,z,dist | \
               --camera=eyex,y,z,centerx,y,z ] \
             [ --autocenter ] \
             [ --viewall ] \
             [ --imgsize=width,height ] [ --projection=(o)rtho|(p)ersp] \
             [ --render | --preview[=throwntogether] ] \
             [ --colorscheme=[Cornfield|Sunset|Metallic|Starnight|BeforeDawn|Nature|DeepOcean] ] \
             [ --csglimit=num ]\
             filename

OpenSCAD 2014.03+ 具有以下選項

openscad     [ -o output_file [ -d deps_file ] ]\
             [ -m make_command ] [ -D var=val [..] ] \
             [ --version ] [ --info ] \
             [ --camera=translatex,y,z,rotx,y,z,dist | \
               --camera=eyex,y,z,centerx,y,z ] \
             [ --imgsize=width,height ] [ --projection=(o)rtho|(p)ersp] \
             [ --render | --preview[=throwntogether] ] \
             [ --csglimit=num ] \
             filename

Openscad 2013.05 具有以下選項

openscad     [ -o output_file [ -d deps_file ] ]\
             [ -m make_command ] [ -D var=val [..] ] [ --render ] \
             [ --camera=translatex,y,z,rotx,y,z,dist | \
               --camera=eyex,y,z,centerx,y,z ] \
             [ --imgsize=width,height ] [ --projection=(o)rtho|(p)ersp] \
             filename

早期版本僅具有以下選項

openscad [ -o output_file [ -d deps_file ] ] \
         [ -m make_command ] [ -D var=val [..] ] filename

OpenSCAD 版本 2011.09.30(現已棄用)上的用法為

openscad [ { -s stl_file | -o off_file | -x dxf_file } [ -d deps_file ] ]\
         [ -m make_command ] [ -D var=val [..] ] filename

匯出選項

[編輯 | 編輯原始碼]

當使用 -o 選項呼叫時,OpenSCAD 不會啟動 GUI,而是執行給定的檔案並根據副檔名(.stl / .off / .dxf.csg)將其匯出到 output_file

一些版本使用 -s/-d/-o 來確定輸出檔案格式;請使用“openscad --help”進行檢查。

如果除了匯出命令之外還給出了選項 -d,則在構建網格時訪問的所有檔案都將以 Makefile 語法寫入 -d 的引數中。

對於至少 2015.03-2+,指定副檔名 .echo 會使 openscad 生成一個文字檔案,其中包含錯誤訊息以及 filename 中所有 echo() 呼叫的輸出,就像它們在 GUI 中可見的控制檯視窗中出現一樣。不支援多個輸出檔案,因此使用此選項,您也不能獲取本來應該生成的模型。

相機和影像輸出

[編輯 | 編輯原始碼]

對於 2013.05+,添加了輸出 .png 影像的選項。有兩種型別的相機可用於生成影像。

第一種相機型別是“萬向節”相機,它使用尤拉角、平移和相機距離,就像 OpenSCAD 視窗底部的 OpenSCAD GUI 視窗顯示一樣。

第二種相機型別是“向量”相機,具有“眼睛”相機位置向量和“看向”中心向量。

--imgsize x,y 選擇 .png 尺寸,--projection 選擇正交或透視,就像在 GUI 中一樣。

預設情況下,cmdline .png 輸出使用帶有 OpenCSG 的預覽模式(f5)。在某些情況下,可能需要輸出完整的渲染,使用 CGAL。這可以透過新增“--render”作為選項來完成。

為了預定義變數,請使用 -D 選項。它可以重複給出。每次出現 -D 後面必須跟一個賦值。與正常的 OpenSCAD 賦值不同,這些賦值不會定義變數,而是定義常量,這些常量不能在程式內部更改,因此可以用來覆蓋程式在匯出時定義的值。

如果要將 -D 變數分配給另一個變數,則 -D 變數必須在主 .scad 程式中初始化

param1=17;       // must be initialized
val=param1;      // param1 passed via -D on cmd-line
echo(val,param1); // outputs 17,17

如果沒有第一行,val 將未定義。

右側可以是任意的 OpenSCAD 表示式,包括數學運算和字串。

請注意,您的 shell(bash、cmd 等)會在將引數傳遞給 openscad 之前解析這些引數,因此您需要適當地引用或轉義包含空格或引號等特殊字元的引數。例如,要將字串 production 分配給 quality 引數,您必須確保 shell 不會剝離 openscad 所期望的 " 字元。在 bash 中,您可以這樣寫

openscad -o my_model_production.stl -D 'quality="production"' my_model.scad

或者從 Windows 提示符中

openscad.com -o my_model_production.stl -D "quality=""production""" my_model.scad

或者您可能需要轉義內部引號而不是外部引號

openscad -o my_model_production.stl -D "quality=\"production\"" my_model.scad

請注意,當從不使用 shell 的另一個程序執行 OpenSCAD 時,這種雙重轉義是不必要的,因為每個引數都是單獨傳遞的。例如,一個 Java 應用程式可能會像這樣啟動一個程序

   pb = new ProcessBuilder("/usr/bin/openscad",
     "-o", "my_model_production.stl",
     "-D", "quality=\"production\"",
     "my_model.scad");

構建所需檔案的命令

[編輯 | 編輯原始碼]

在複雜的構建過程中,如果在 Makefile 中定義了一些 OpenSCAD 檔案所需的缺失檔案,則可以生成這些檔案。如果 OpenSCAD 給出了選項 -m make,它會在第一次嘗試訪問缺失的 file 時啟動 make file

處理資料夾中的所有 .scad 檔案

[編輯 | 編輯原始碼]

將資料夾中的所有 .scad 轉換為 .stl 的示例

在包含 .scad 檔案的資料夾中,建立一個包含以下文字的 .bat 檔案

   FOR %%f in (*.scad)  DO openscad -o "%%~nf.stl" "%%f" 

如果它在未處理的情況下關閉,請檢查透過將 openscad 目錄新增到以下位置來設定 PATH

     Start - Settings - Control Panel - System - Advanced tab - Environment Variables - System Variables, select Path, then click Edit.

將 openscad 目錄新增到列表中


Makefile 示例

[編輯 | 編輯原始碼]

-d-m 選項只有在組合使用時才有意義。(-m 而不使用 -d 將不會在構建匯出時考慮修改的依賴項,-d 而不使用 -m 將要求檔案在生成依賴項的第一次執行之前就已經構建好了。)

以下是一個基本 Makefile 的示例,它從同名 .scad 檔案建立一個 .stl 檔案

# explicit wildcard expansion suppresses errors when no files are found
include $(wildcard *.deps)

%.stl: %.scad
	openscad -m make -o $@ -d $@.deps $<

當第一次執行 make my_example.stl 時,它找不到任何 .deps 檔案,並且必須依賴於 my_example.scad。由於 my_example.stl 尚未預置,因此它將無條件地建立。如果 OpenSCAD 發現缺失檔案,它會呼叫 make 來構建它們,並將所有用到的檔案列在 my_example.stl.deps 中。

當隨後呼叫 make my_example.stl 時,它會找到幷包含 my_example.stl.deps,並檢查其中列出的所有檔案(包括 my_example.scad)自 my_example.stl 構建以來是否發生了更改,這取決於它們的時間戳。只有在這種情況下,它才會再次構建 my_example.stl

自動目標

[編輯 | 編輯原始碼]

當從單個 .scad 檔案構建類似的 .stl 檔案時,也有一種方法可以實現自動化

# match "module foobar() { // `make` me"
TARGETS=$(shell sed '/^module [a-z0-9_-]*().*make..\?me.*$$/!d;s/module //;s/().*/.stl/' base.scad)

all: ${TARGETS}

# auto-generated .scad files with .deps make make re-build always. keeping the
# scad files solves this problem. (explanations are welcome.)
.SECONDARY: $(shell echo "${TARGETS}" | sed 's/\.stl/.scad/g')

# explicit wildcard expansion suppresses errors when no files are found
include $(wildcard *.deps)

%.scad:
	echo -ne 'use <base.scad>\n$*();' > $@

%.stl: %.scad
	openscad -m make -o $@ -d $@.deps $<

所有應該自動匯出的物件都必須在 base.scad 中使用它們未來的檔名(不帶“.stl”)定義在一個單獨的模組中,並且在模組定義行中有一個註釋,例如“// make me”。“TARGETS=”行從基本檔案選出這些檔名,並建立檔名。在呼叫 make all(或 make,簡而言之)時構建這些檔案。

由於上一個示例的約定是從同名的 .scad 檔案建立 .stl 檔案,因此必須為每個這些檔案生成一個 .scad 檔案。這在“%.scad:”段落中完成;my_example.scad 是一個簡單的 OpenSCAD 檔案

use <base.scad>
my_example();

“.SECONDARY”行是為了防止 make 刪除生成的 .scad 檔案。它的存在有助於確定哪些檔案不再需要重建;請在討論頁面上釋出有關究竟是什麼出錯(或者如何更好地修復它)的想法!

Windows 注意事項

[編輯 | 編輯原始碼]

在 Windows 上,openscad.com 應該從命令列呼叫,作為 openscad.exe 的包裝器。這是因為 Openscad 使用“devenv”解決方案來解決命令列/GUI 輸出問題。在 cmd.exe 提示符下鍵入“openscad”會預設呼叫 .com 程式包裝器。

MacOS 注意事項

[編輯 | 編輯原始碼]

在 MacOS 上,二進位制檔案通常隱藏在 App 資料夾中。如果 OpenSCAD 安裝在全域性應用程式資料夾中,它可以像以下示例一樣從命令列呼叫,該示例只顯示 OpenSCAD 版本

macbook:/$ /Applications/OpenSCAD.app/Contents/MacOS/OpenSCAD -v
OpenSCAD version 2013.06

或者,您可以建立一個指向二進位制檔案的符號連結,以便更容易地從命令列呼叫

 macbook:/$ sudo ln -sf /Applications/OpenSCAD.app/Contents/MacOS/OpenSCAD /usr/local/bin/openscad

現在您可以直接呼叫 openscad,而無需輸入完整的路徑。

 macbook:/$ openscad -v
 OpenSCAD version 2015.03-3

在某些版本的 MacOS 上,嘗試透過該連結執行 openscad 時,您可能會收到以下錯誤

 This application failed to start because it could not find or load the Qt platform plugin "cocoa".
 Reinstalling the application may fix this problem.
 Abort trap: 6

您可以透過建立一個包裝指令碼直接呼叫可執行檔案來解決此問題

 sudo rm -f /usr/local/bin/openscad
 echo '#!/bin/sh' > test
 echo '/Applications/OpenSCAD.app/Contents/MacOS/OpenSCAD "$@"' >> test
 chmod +x test ; sudo mv test /usr/local/bin/openscad
華夏公益教科書