knowledge:openscad:reference:general
差别
这里会显示出您选择的修订版和当前版本之间的差别。
| 后一修订版 | 前一修订版 | ||
| knowledge:openscad:reference:general [2022/05/13 03:12] – 创建 弘毅 | knowledge:openscad:reference:general [2025/10/11 02:55] (当前版本) – 外部编辑 127.0.0.1 | ||
|---|---|---|---|
| 行 1: | 行 1: | ||
| + | [[knowledge: | ||
| ====== OpenSCAD User Manual/ | ====== OpenSCAD User Manual/ | ||
| + | |||
| ===== Introduction ===== | ===== Introduction ===== | ||
| + | OpenSCAD is a 2D/3D and solid modeling program which is based on a Functional programming language used to create models that are previewed on the screen, and rendered into 3D mesh which allows the model to be exported in a variety of 2D/3D file formats. | ||
| + | A script in the OpenSCAD language is used to create 2D or 3D models. This script is a free format list of action statements. | ||
| + | |||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | operator() { action(); action(); } } | ||
| + | Objects | ||
| + | Objects are the building blocks for models, created by 2D and 3D primitives. Objects end in a semicolon ';' | ||
| + | |||
| + | Actions | ||
| + | Action statements include creating objects using primitives and assigning values to variables. Action statements also end in a semicolon ';' | ||
| + | |||
| + | Operators | ||
| + | Operators, or transformations, | ||
| + | |||
| + | | ||
| + | | ||
| + | | ||
| + | x = 4+y; | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| ===== Comments ===== | ===== Comments ===== | ||
| + | Comments are a way of leaving notes within the script, or code, (either to yourself or to future programmers) describing how the code works, or what it does. Comments are not evaluated by the compiler, and should not be used to describe self-evident code. | ||
| + | |||
| + | OpenSCAD uses C++-style comments: | ||
| + | |||
| + | // This is a comment | ||
| + | | ||
| + | myvar = 10; // The rest of the line is a comment | ||
| + | | ||
| + | /* | ||
| + | | ||
| + | can span multiple lines. | ||
| + | */ | ||
| + | |||
| + | ===== Variables ===== | ||
| + | OpenSCAD variables are created by a statement with a name or identifier, assignment via an expression and a semicolon. The role of arrays, found in many imperative languages, is handled in OpenSCAD via vectors. Currently valid identifiers can only be composed of simple characters and underscores [a-zA-Z0-9_] and do not allow high-ascii or unicode characters. | ||
| + | |||
| + | var = 25; | ||
| + | xx = 1.25 * cos(50); | ||
| + | y = 2*xx+var; | ||
| + | logic = true; | ||
| + | MyString = "This is a string"; | ||
| + | a_vector = [1,2,3]; | ||
| + | rr = a_vector[2]; | ||
| + | range1 = [-1.5: | ||
| + | xx = [0: | ||
| + | OpenSCAD is a Functional programming language, as such variables are bound to expressions and keep a single value during their entire lifetime due to the requirements of referential transparency. In imperative languages, such as C, the same behavior is seen as constants, which are typically contrasted with normal variables. | ||
| + | |||
| + | In other words OpenSCAD variables are more like constants, but with an important difference. If variables are assigned a value multiple times, only the last assigned value is used in all places in the code. See further discussion at Variables are set at compile-time, | ||
| + | |||
| + | Values cannot be modified during run time; all variables are effectively constants that do not change. Each variable retains its last assigned value at compile time, in line with Functional programming languages. Unlike Imperative languages, such as C, OpenSCAD is not an iterative language, and as such the concept of x = x + 1 is not valid. Understanding this concept leads to understanding the beauty of OpenSCAD. | ||
| + | |||
| + | Before version 2015.03 | ||
| + | It was not possible to do assignments at any place except the file top-level and module top-level. Inside an if/ | ||
| + | |||
| + | Since version 2015.03 | ||
| + | Variables can now be assigned in any scope. Note that assignments are only valid within the scope in which they are defined - you are still not allowed to leak values to an outer scope. See Scope of variables for more details. | ||
| + | |||
| + | a=0; | ||
| + | if (a==0) | ||
| + | { | ||
| + | a=1; // before 2015.03 this line would generate a Compile Error | ||
| + | // since 2015.03 | ||
| + | } | ||
| + | Undefined variable | ||
| + | A non assigned variable has the special value undef. It could be tested in conditional expression, and returned by a function. | ||
| + | |||
| + | | ||
| + | | ||
| + | | ||
| + | if (a==undef) { | ||
| + | | ||
| + | } | ||
| + | Scope of variables | ||
| + | When operators such as translate() and color() need to encompass more than one action ( actions end in ;), braces {} are needed to group the actions, creating a new, inner scope. When there is only one semicolon, braces are usually optional. | ||
| + | |||
| + | Each pair of braces creates a new scope inside the scope where they were used. Since 2015.03, new variables can be created within this new scope. New values can be given to variables which were created in an outer scope . These variables and their values are also available to further inner scopes created within this scope, but are not available to any thing outside this scope. Variables still have only the last value assigned within a scope. | ||
| + | |||
| + | // scope 1 | ||
| + | a = 6; // create a | ||
| + | | ||
| + | | ||
| + | a= 10; | ||
| + | b= 16; // create b | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | b=20; | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | //In this example, scopes 1 and 1.1 are outer scopes to 1.1.1 but 1.2 is not. | ||
| + | Anonymous scopes are not considered scopes: | ||
| + | { | ||
| + | angle = 45; | ||
| + | } | ||
| + | | ||
| + | For() loops are not an exception to the rule about variables having only one value within a scope. A copy of loop contents is created for each pass. Each pass is given its own scope, allowing any variables to have unique values for that pass. No, you still can't do a=a+1; | ||
| + | |||
| + | Variables are set at compile-time, | ||
| + | Because OpenSCAD calculates its variable values at compile-time, | ||
| + | |||
| + | // The value of ' | ||
| + | a = 0; | ||
| + | | ||
| + | a = 3; | ||
| + | | ||
| + | a = 5; | ||
| + | While this appears to be counter-intuitive, | ||
| + | |||
| + | Special Variables | ||
| + | Special variables provide an alternate means of passing arguments to modules and functions. All variables starting with a ' | ||
knowledge/openscad/reference/general.1652411544.txt · 最后更改: 2025/10/11 02:55 (外部编辑)
