我应该如何在命令行中将值传递给 openSCAD?

How should I pass values to openSCAD in command line?

我想对一堆stl文件进行某种修改。所以我写了一个 openSCAD 程序,想从命令行编写脚本,但它不起作用,我也不知道为什么。

根据手册页,我认为以下命令会起作用,但它不起作用(啊,我也使用 openSCAD 的 appimage 版本(以防它很重要):

OpenSCAD-2021.01-x86_64.AppImage -o [OUTPUT FILE NAME] 'myscript.scad' -D filename=[INPUT FILE NAME] 

但是,-D 选项似乎没有达到我的预期。所以我做了一个小测试脚本:

echo ("START");

filename1="cube.stl";

echo(filename1=filename1, filename2=filename2);

minkowski() {
  import(filename1);
  sphere(r=1, $fn=24);
}

translate([30, 0, 0])
  minkowski() {
    import(filename2);
    sphere(r=1, $fn=24);
  }

如果我用 openSCAD 的 gui 版本测试 myscript.scad,它会给出以下结果(这是预期的):

Compiling design (CSG Tree generation)...
ECHO: "START"
WARNING: Ignoring unknown variable 'filename2' in file test.scad, line 5 
ECHO: filename1 = "cube.stl", filename2 = undef
WARNING: Ignoring unknown variable 'filename2' in file test.scad, line 14 
Compiling design (CSG Products generation)...
ERROR: Unsupported file format while trying to import file '""', import() at line 14 
Geometries in cache: 5
Geometry cache size in bytes: 128056
CGAL Polyhedrons in cache: 0
CGAL cache size in bytes: 0
Compiling design (CSG Products normalization)...
Normalized tree has 2 elements!
Compile and preview finished.
Total rendering time: 0:00:00.177

但是,如果我在命令行中测试它,没有初始化任何值,顺便说一下,我不知道第 19 行和第 20 行的警告是什么,而我的 scad 文件只有 16 行:

MyPrompt> ./OpenSCAD-2021.01-x86_64.AppImage -o 'test.stl' 'test.scad' -D filename1=cube.stl -D filename2=cube.stl 
WARNING: Ignoring unknown variable 'cube' in file test.scad, line 19
WARNING: Ignoring unknown variable 'cube' in file test.scad, line 20
ECHO: "START"
ECHO: filename1 = undef, filename2 = undef
ERROR: Unsupported file format while trying to import file '""', import() at line 8
Geometries in cache: 4
Geometry cache size in bytes: 118112
CGAL Polyhedrons in cache: 1
CGAL cache size in bytes: 1362752
Total rendering time: 0:00:00.854
   Top level object is a 3D object:
   Simple:        yes
   Vertices:      576
   Halfedges:    3168
   Edges:        1584
   Halffacets:   2024
   Facets:       1012
   Volumes:         3

作为参考,cube.stl 是使用 cube(); 命令创建的,包含:

00000000  4f 70 65 6e 53 43 41 44  20 4d 6f 64 65 6c 0a 00  |OpenSCAD Model..|
00000010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000050  0c 00 00 00 00 00 00 80  00 00 00 00 00 00 80 3f  |...............?|
00000060  00 00 00 00 00 00 80 3f  00 00 80 3f 00 00 80 3f  |.......?...?...?|
*
00000080  00 00 80 3f 00 00 00 00  00 00 00 00 00 00 00 00  |...?............|
00000090  80 3f 00 00 80 3f 00 00  00 00 00 00 80 3f 00 00  |.?...?.......?..|
000000a0  00 00 00 00 80 3f 00 00  80 3f 00 00 00 00 00 00  |.....?...?......|
000000b0  00 00 00 00 80 3f 00 00  00 00 00 00 00 00 00 00  |.....?..........|
000000c0  00 00 80 bf 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000000d0  00 00 80 3f 00 00 80 3f  00 00 00 00 00 00 80 3f  |...?...?.......?|
000000e0  00 00 00 00 00 00 00 00  00 00 00 00 00 80 00 00  |................|
000000f0  00 00 00 00 80 bf 00 00  80 3f 00 00 80 3f 00 00  |.........?...?..|
00000100  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000110  00 00 00 00 80 3f 00 00  00 00 00 00 00 00 00 00  |.....?..........|
00000120  00 00 80 bf 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000130  00 00 00 00 00 00 80 3f  00 00 00 00 00 00 80 3f  |.......?.......?|
00000140  00 00 00 00 00 00 00 00  00 00 80 3f 00 00 00 00  |...........?....|
00000150  00 00 00 00 80 bf 00 00  00 80 00 00 80 3f 00 00  |.............?..|
00000160  00 00 00 00 80 3f 00 00  00 00 00 00 00 00 00 00  |.....?..........|
*
00000180  00 00 80 3f 00 00 00 80  00 00 00 00 00 00 80 3f  |...?...........?|
00000190  00 00 00 00 00 00 80 3f  00 00 80 3f 00 00 80 3f  |.......?...?...?|
*
000001b0  00 00 00 00 80 3f 00 00  00 00 00 00 00 00 00 00  |.....?..........|
000001c0  80 3f 00 00 80 3f 00 00  00 00 00 00 80 3f 00 00  |.?...?.......?..|
000001d0  00 00 00 00 80 3f 00 00  80 3f 00 00 00 00 00 00  |.....?...?......|
000001e0  00 00 00 00 00 00 00 00  00 00 80 3f 00 00 00 80  |...........?....|
000001f0  00 00 80 3f 00 00 80 3f  00 00 00 00 00 00 00 00  |...?...?........|
00000200  00 00 80 3f 00 00 80 3f  00 00 80 3f 00 00 80 3f  |...?...?...?...?|
00000210  00 00 80 3f 00 00 00 00  00 00 00 00 80 3f 00 00  |...?.........?..|
00000220  00 00 00 00 00 00 00 00  80 3f 00 00 80 3f 00 00  |.........?...?..|
00000230  80 3f 00 00 80 3f 00 00  00 00 00 00 00 00 00 00  |.?...?..........|
00000240  80 3f 00 00 00 00 00 00  00 00 80 bf 00 00 00 00  |.?..............|
00000250  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000260  00 00 00 00 00 00 80 3f  00 00 80 3f 00 00 00 00  |.......?...?....|
00000270  00 00 80 3f 00 00 00 00  00 00 00 00 80 bf 00 00  |...?............|
00000280  00 80 00 00 00 00 00 00  00 00 00 00 80 3f 00 00  |.............?..|
00000290  80 3f 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |.?..............|
000002a0  00 00 00 00 00 00 00 00  80 3f 00 00              |.........?..|

好的,知道了:

问题出在openSCAD想要接收带引号的字符串。但是,shell 会尝试吃掉它们。所以,正确的调用语法是:

MyPrompt> ./Téléchargements/OpenSCAD-2021.01-x86_64.AppImage -o 'test.stl' 'test.scad' -D filename1='"cube.stl"' -D filename2='"cube.stl"'
ECHO: "START"
ECHO: filename1 = "cube.stl", filename2 = "cube.stl"
Geometries in cache: 4
Geometry cache size in bytes: 136832
CGAL Polyhedrons in cache: 1
CGAL cache size in bytes: 1524416
Total rendering time: 0:00:01.021
   Top level object is a 3D object:
   Simple:        yes
   Vertices:      672
   Halfedges:    3536
   Edges:        1768
   Halffacets:   2200
   Facets:       1100
   Volumes:         3

这也解释了警告,因为它不是试图将 cube.stl 解释为字符串,而是作为点符号中的变量名(因此主要变量名确实是 cube) .