OpenSCAD User Manual/The OpenSCAD Language


OpenSCAD では C++形式のコメントを用いる:
変数は以下のように簡単に作ることができる。
例:
ダブルクォートとバックスラッシュはエスケープする必要がある (順に\" と \\ )。その他のエスケープ文字は、改行 (\n)、タブ (\t) とCR (\r) である。
NB! この仕様は OpenSCAD-2011.04 からである。古いソースはsed コマンド『sed 's/\\/\\\\/' non-escaped.scad > escaped.scad』で変換できる。
例:
Output: ECHO: "The quick brown fox jumps "over" the lazy dog. The quick brown fox. The \lazy\ dog."
注意: OpenSCAD は変数の値の計算を実行時ではなくコンパイル時に行なうので、 一番最後に代入された値がプログラム中のあらゆる参照で有効となる。 変数というより、再定義可能な定数と考えたほうがよいかもしれない。
例:
出力
このことから、変数は "if" ブロックの中では代入できない:
Example:
出力 Compile Error
この振る舞いのスコープは、root (一番上のレベル) または module の呼び出しごとに限られる。 このことから、変数はスコープの外部に影響されることなくモジュール内で再定義できる。 しかしスコープ内でのすべてのインスタンスは、上述のように最後に定義された値になる。
例:
Output
これは直感に反するようであるが、面白い使い方ができる。 つまり、デフォルト値を持つようなシェアードライブラリを作成した場合、そのファイルをインクルードしたファイルでは、 単純にデフォルト値を『再定義』つまり上書きするだけで新しい値で使うことができる。
変数の値のスコープをより厳密に定義するには、assign ステートメントを参照。
Now we have variables, it would be nice to be able to get input into them instead of setting the values from code. There are a few functions to read data from DXF files, or you can set a variable with the -D switch on the command line.
Getting a point from a drawing
Getting a point is useful for reading an origin point in a 2D view in a technical drawing. The function dxf_cross will read the intersection of two lines on a layer you specify and return the intersection point. This means that the point must be given with two lines in the DXF file, and not a point entity.
Getting a dimension value
You can read dimensions from a technical drawing. This can be useful to read a rotation angle, an extrusion height, or spacing between parts. In the drawing, create a dimension that does not show the dimension value, but an identifier. To read the value, you specify this identifier from your script:
For a nice example of both functions, see Example009 and the image on the homepage of OpenSCAD.
配列要素の値または範囲に基づいて繰り返し実行する。
配列要素の値: for (変数=<配列>) <実行されるもの> - <変数> には配列の値が次々に代入される 範囲: for (変数=<範囲>) <実行されるもの>
範囲: [<初期値>:<終値>] - 初期値から終値まで繰り返す。<終値> は <初期値> より小さくてもよい。 範囲: [<初期値>:<増分>:<終値>] - 初期値から終値まで増分だけ増やして繰り返す。増分は非整数でもよい。 注意: 増分は正の絶対値である必要がある。もし <終値> が <初期値> より小さくとも、増分は絶対値でなくてはならない。 警告: 増分で (<end>-<start>) が割り切れない場合、繰り返しの実際の終値は <end>-(<end>-<start> mod <increment>) となる (訳注: 範囲を超えることはない。初期値1、終値6、差分2の場合、1、3、5が代入される)。
ループのネスト : for (変数1 = <範囲または配列>, 変数2 = <範囲または配列> ) <両方の変数を変化させて実行されるもの> for ループは通常のプログラム同様、ネストできる。ひとつのfor文で両方の繰り返しを実行できる。
Nested loop example
配列または範囲による繰り返しを実行し、描いたものの交わり (intersection) をとる。
注: intersection_for() は、通常のfor() 文と intersection() 文を用いた結果と同じにならない問題があり、対処中である。
パラメータ
サブツリー内を条件によって評価する。
引数
例:
変数にサブツリー内での新しい値を代入する。
引数
例:
Update:
このバージョンから値の代入は好きなところでできるため、assign() は必要ない (assign() は後方互換性のために残されている)。
OpenSCAD User Manual/Mathematical Operators
OpenSCAD User Manual/Mathematical Functions
OpenSCAD User Manual/String Functions
原点の座標に直方体を作る。 centerがtrueの際、原点を中心に作られ、falseの際はx, y, z 共に正の方向に作られる。 引数が下記のパラメータと同じ順ならば、引数の名前を省略し、値だけを入力してもよい。
引数
例:

原点を中心に球を作る。引数名は省略可である。
引数
例:

座標系の原点に円筒を生成する。 1つの半径 (r) を指定すると円筒になり、2つの異なる半径 (r1, r2) を指定すると円錐になる。
引数
デフォルトは false、つまり円筒の底面 (半径 r1) が原点となる。
例:

Create a polyhedron with a list of points and a list of triangles. The point list is all the vertexes of the shape, the triangle list is how the points relates to the surfaces of the polyhedron.
Parameters
Syntax example
Triangle points ordering When looking at the face from the outside inwards, the points must be clockwise. You can rearrange the order of the points or the order they are referenced in each triangle triple. The order of triangles is immaterial. Note that if your polygons are not all oriented the same way OpenSCAD will either print an error or crash completely, so pay attention to the vertex ordering.
Example, a square base pyramid:
Ordering of triangle points An example of a more complex polyhedron, and showing how to fix polyhedrons with badly oriented polygons.
When you select 'Thrown together' from the view menu and compile the design (not compile and render!) you will see a preview with the mis-oriented polygons highlighted. Unfortunately this highlighting is not possible in the OpenCSG preview mode because it would interfere with the way the OpenCSG preview mode is implemented.)
Below you can see the code and the picture of such a problematic polyhedron, the bad polygons (triangles or compositions of triangles) are in pink.
A correct polyhedron would be the following:
Beginner's tip:
If you don't really understand "orientation", try to identify the mis-oriented pink triangles and then permute the references to the points vectors until you get it right. E.g. in the above example, the third triangle ([0,4,5]) was wrong and we fixed it as [4,0,5]. In addition, you may select "Show Edges" from the "View Menu", print a screen capture and number both the points and the triangles. In our example, the points are annotated in black and the triangles in blue. Turn the object around and make a second copy from the back if needed. This way you can keep track.
Clockwise Technique:
Orientation is determined by clockwise indexing. This means that if you're looking at the triangle (in this case [4,0,5]) from the outside you'll see that the path is clockwise around the center of the face. The winding order [4,0,5] is clockwise and therefore good. The winding order [0,4,5] is counter-clockwise and therefore bad. Likewise, any other clockwise order of [4,0,5] works: [5,4,0] & [0,5,4] are good too. If you use the clockwise technique, you'll always have your faces outside (outside of OpenSCAD, other programs do use counter-clockwise as the outside though).
Think of it as a Left Hand Rule:
If you hold the triangle and the fingers of your hand curls is the same order as the points, then your thumb points outwards.
Succinct description of a 'Polyhedron'
Each point, in the point list, is defined with a 3-tuple x,y,z position specification. Points in the point list are automatically given an identifier starting at zero for use in the triangle list (0,1,2,3,... etc).
Each triangle, in the triangle list, is defined by selecting 3 of the points (using the point identifier) out of the point list.
e.g. triangles=[ [0,1,2] ] defines a triangle from the first point (points are zero referenced) to the second point and then to the third point.
When looking at any triangle from the outside, the triangle must list their 3 points in a clockwise order.
OpenSCAD User Manual/Transformations

子ノードの和集合を作る。

2番目以降の子ノードを減算する。

すべての子ノードの共通部分を残す。

常に (even in OpenCSG プレビューモードでも) 子ノードのブーリアンモデルを計算する。
例:


この例は、convexity が4である2次元の図形を示している。赤線で示した光線は、図形と4回交差している。 3次元の場合も同様である。大抵の立体では、10程度にしておけばよい。
OpenSCAD User Manual/Modifier Characters
モジュール(他の言語でのマクロ、関数にあたる)は処理の再利用を容易にする。

この例では、引数 distance、rot、size を設定して呼び出すことにより、このような描画を複数繰り返す場合のプログラムの行数を節約し、 プログラムの可動性を上げる。
呼び出しは、次の例のようにC言語の関数呼び出しに似た方法で、値または数式を渡して行なう。
The child nodes of the module instantiation can be accessed using the children() statement within the module. The number of module children can be accessed using the $children variable.
Parameters
Deprecated child() module
Up to release 2013.06 the now deprecated child() module was used instead. This can be translated to the new children() according to the table:
Examples
Transfer all children to another module:
Use the first child, multiple time:
If you need to make your module iterate over all children you will need to make use of the $children variable, e.g.:
One can specify default values for the arguments:
And then use one of the following ways to supply the arguments
OpenSCAD User Manual/STL Import
OpenSCAD User Manual/Include Statement

All variables starting with a '$' are special variables. The semantic is similar to the special variables in lisp: they have dynamic instead of lexical scoping.
The $fa, $fs and $fn special variables control the number of facets used to generate an arc:
$fa is the minimum angle for a fragment. Even a huge circle does not have more fragments than 360 divided by this number. The default value is 12 (i.e. 30 fragments for a full circle). The minimum allowed value is 0.01. Any attempt to set a lower value will cause a warning.
$fs is the minimum size of a fragment. Because of this variable very small circles have a smaller number of fragments than specified using $fa. The default value is 2. The minimum allowed value is 0.01. Any attempt to set a lower value will cause a warning.
$fn is usually 0. When this variable has a value greater than zero, the other two variables are ignored and full circle is rendered using this number of fragments. The default value is 0.
When $fa and $fs are used to determine the number of fragments for a circle, then OpenSCAD will never use less than 5 fragments.
This is the C code that calculates the number of fragments in a circle:
Spheres are first sliced into as many slices as the number of fragments being used to render a circle of the sphere's radius, and then every slice is rendered into as many fragments as are needed for the slice radius. You might have recognized already that the pole of a sphere is usually a pentagon. This is why.
The number of fragments for a cylinder is determined using the greater of the two radii.
The method is also used when rendering circles and arcs from DXF files.
You can generate high resolution spheres by resetting the $fX values in the instantiating module:
or simply by passing the special variable as parameter:
You can even scale the special variable instead of resetting it:
The $t variable is used for animation. If you enable the animation frame with view->animate and give a value for "FPS" and "Steps", the "Time" field shows the current value of $t. With this information in mind, you can animate your design. The design is recompiled every 1/"FPS" seconds with $t incremented by 1/"Steps" for "Steps" times, ending at either $t=1 or $t=1-1/steps.
If "Dump Pictures" is checked, then images will be created in the same directory as the .scad file, using the following $t values, and saved in the following files:
Or, for other values of Steps, it follows this pattern:
Which pattern it chooses appears to be an unpredictable, but consistent, function of Steps. For example, when Steps=4, it follows the first pattern, and outputs a total of 4 files. When Steps=3, it follows the second pattern, and also outputs 4 files. It will always output either Steps or Steps+1 files, though it may not be predictable which. When finished, it will wrap around and recreate each of the files, looping through and recreating them forever.
These contain the current viewport rotation and translation - at the time of doing the rendering. Moving he viewport does not update them. During an animation they are updated for each frame.
It's not possible to write to them and thus change the viewport parameters (although that could be a decent enough idea).
Example
which makes the cube change size based on the view angle, if an animation loop is active (which does not need to use the $t variable)
You can also make bits of a complex model vanish as you change the view.
The menu command Edit - Paste Viewport Rotation/Translation copies the current value of the viewport, but not the current $vpd or $vpt.
Define a function for code readability and re-use.
Usage examples:
This function prints the contents to the compilation window (aka Console). Useful for debugging code.
The OpenSCAD console supports a subset of HTML markup language. See here for details.
Usage examples:
Shows in the Console as
Forces the generation of a mesh even in preview mode. Useful when the boolean operations become too slow to track.
Needs description.
Usage examples:
Example 1:
Result:

Example 2

Usage Pattern:
The following are some usage examples.
Example 5:
Returns:
Example 6: Return all matches per search vector element.
Returns:
Example 7: Return first match per search vector element; special case return vector.
Returns:
Example 8: Return first two matches per search vector element; vector of vectors.
Returns:
Example 9:
Returns