用户工具

站点工具


knowledge:openscad:relativity.scad

Relativity SCAD

Relativity SCAD不是独立语言,只是OpenSCAD的library,但是它提供了一套全新的API替换掉OpenSCAD原有的API的大部分功能,使用起来比OpenSCAD API简单很多。

特别之处:

  • 通过描述对象之间的相对位置,大大简化了OpenSCAD建模中繁琐的transformation
  • 通过selector对部分对象进行操作,减少了boolean operation的繁琐组合

安装

下载文件后,将其放在计算机上的 OpenSCAD 文件夹中。此文件夹的位置因操作系统而异。启动 OpenSCAD 并键入以下内容:

include <relativity.scad>

使用“include”关键字导入依赖很重要。relativity.scad 需要在其初始化期间声明变量。如果您使用“use”关键字导入 relativity.scad,OpenSCAD 将不会加载这些变量,并且会导致奇怪的行为!

原始实体

这个 OpenSCAD 库增加了相对于其他几何基元的大小、位置和方向对象的功能。

为此,该库引入了一组新模块来替换 OpenSCAD 中的默认几何图元:

实体 新语法 老语法
box cube
rod cylinder
ball phere

如果您需要,所有原始几何图元仍然存在,您可以决定使用多少或多少使用它们。新的原语几乎具有旧版本的所有行为,但有两个重要区别。

Centering

First, in place of center is a new attribute, anchor. This attribute specifies where the axis of rotation is to be placed relative to the object. The anchor attribute is given as a vector whose elements range from -1 (e.g. bottom of axis) to 1 (e.g. top of axis). A value of 0 indicates the anchor is to be centered along an axis.
首先,代替中心的是一个新属性,锚点。此属性指定旋转轴相对于对象的放置位置。锚属性作为一个向量给出,其元素范围从 -1(例如轴的底部)到 1(例如轴的顶部)。值 0 表示锚点将沿轴居中。

+1 0 -1
X
锚=[1,0,0]

锚=[0,0,0]

锚=[-1,0,0]
Y
锚=[0,1,0]

锚=[0,0,0]

锚=[0,-1,0]
Z
锚=[0,0,1]

锚=[0,0,0]

锚=[0,0,-1]

Alignment

The second distinction among these new primitives allows for the creation of child objects. By default, a child object is positioned with its origin at the center of its parent:
这些新原语之间的第二个区别允许创建子对象。默认情况下,子对象的原点位于其父对象的中心:

box(50, anchor=[0,0,-1])
    sphere(d=55);
    

this however can be changed using of a second operation, align:

然而,这可以使用第二个操作来改变,对齐:

box(50, anchor=[0,0,-1])
    align([0,0,1])
    sphere(d=60);
    

You can align objects along any axis. The value passed to align() is a vector whose elements range from -1 (e.g. bottom of axis) to 1 (e.g. top of axis). A value of 0 indicates an object is to be placed at the center of its parent along that axis.

您可以沿任何轴对齐对象。传递给 align() 的值是一个向量,其元素范围从 -1(例如轴的底部)到 1(例如轴的顶部)。值 0 表示对象将沿该轴放置在其父对象的中心。

+10-1
X
对齐([1,0,0])

对齐([0,0,0]

对齐([-1,0,0]
Y
对齐([0,1,0]

对齐([0,0,0]

对齐([0,-1,0]
Z
对齐([0,0,1]

对齐([0,0,0]

对齐([0,0,-1]

You can also pass a list of vectors. When you pass a list of vectors, align() will create duplicates of its children in the locations indicated by your vectors.

您还可以传递向量列表。当您传递向量列表时, align() 将在向量指示的位置创建其子项的副本。

box(50, anchor=[0,0,-1])
    align([[0,0,1], [0,1,0]])
    sphere(d=60);
    

The examples above use sphere() as their child object. However, ball() can also be be used as a child. Using ball() will allow us to combine align() and anchor, making it easy to stack objects end-to-end:

上面的示例使用 sphere() 作为它们的子对象。但是,ball() 也可以用作子级。使用 ball() 将允许我们结合 align() 和锚点,使端到端堆叠对象变得容易:

box(50, anchor=[0,0,-1])
align([0,0,1])
ball(50, anchor=[0,0,-1])
align([0,0,1])
rod(50, anchor=[0,0,-1]);

To allow for readable code, relativity.scad comes with a few constants representing commonly used values for the align operator:

为了允许代码可读,relativity.scad 带有一些常量,表示对齐运算符的常用值:

top = [0,0,1];
center=[0,0,0];
bottom = [0,0,-1];
x = [1,0,0];
y = [0,1,0];
z = [0,0,1];

It is also possible to combine this constant with + or - operators, since OpenSCAD does (simple) vector calculations.

也可以将此常数与 + 或 - 运算符结合使用,因为 OpenSCAD 进行(简单)矢量计算。

There are also two special variables that are exposed by the align() module, $inward and $outward. $inward points towards the center of a parent object, while $outward points away from it.

align()对齐模块还公开了两个特殊变量,$inward 和 $outward。 $inward 指向父对象的中心,而 $outward 指向远离它的对象。

The anchor parameter defaults to $inward. This is meant to facilitate stacking objects. All told, the code above can be rewritten:

锚参数默认为 $inward。这是为了便于堆叠对象。总而言之,上面的代码可以重写:

box(50)
align(top)
ball(50)
align(top)
rod(50);

Orientation

Child objects by default will maintain the same z-axis orientation as their parent objects

默认情况下,子对象将保持与其父对象相同的 z 轴方向

box(50, anchor=[0,0,-1])
align([0,1,0])
rod(d=50, h=50, anchor=[0,-1,0]);

This, however, can be changed with the use of another module, orient. The orient module will redirect the top of a child object to face a given direction. Sometimes rotation using orient can be more intuitive than using rotate

然而,这可以通过使用另一个模块 orient 来改变。 orient 模块将重定向子对象的顶部以面向给定方向。有时使用 orient 进行旋转比使用 rotate 更直观

box(50, anchor=[0,0,-1])
align([0,1,0])
orient([0,1,1])
rod(d=50, h=50, anchor=[0,0,-1]);

You can also pass a list of vectors to orient. In this case, orient will create duplicates of its children with their tops facing the directions you specify in the list.

您还可以将向量列表传递给 orient。在这种情况下, orient 将创建其子项的副本,其顶部朝向您在列表中指定的方向。

//OpenSCAD logo:  
differed("hole", "not(hole)")  
ball(50)  
orient([x,y,z])  
rod(d=25, h=50, $class="hole");  

Sizing

relativity.scad exposes a dynamic variable, $parent_size, in case there are any unforeseen circumstances where something needs to be done relative to a parent object. This can also be used when setting the size of a child, say, when creating a fractal:

relativity.scad 公开了一个动态变量 $parent_size,以防有任何不可预见的情况需要相对于父对象执行某些操作。这也可以在设置孩子的大小时使用,例如,在创建分形时:

box([50,50,50])
align(top)
box(0.8*$parent_size)
align(top)
box(0.8*$parent_size);

The tree demo provides a good example for the parent() and $parent_size functionality.

树演示为 parent() 和 $parent_size 功能提供了一个很好的示例。

To ease the use of $parent_size, geometric primitives for rod and ball accept size as their first parameter argument, much like cube and box.

为了简化 $parent_size 的使用,棒和球的几何基元接受大小作为它们的第一个参数参数,就像立方体和盒子一样。

rod([50,50,50])
align(top)
rod(0.8*$parent_size)
align(top)
rod(0.8*$parent_size);

An interesting consequence to this means you can pass rod and ball different values for x,y, and z:

一个有趣的结果意味着您可以为 x、y 和 z 传递不同的值:

rod([50,50,50])
align(top)
rod([$parent_size.x*.5, $parent_size.y*.7, $parent_size.z*.9])
align(top)
rod([$parent_size.x*.5, $parent_size.y*.7, $parent_size.z*.9]);

Another, more unfortunate side effect means rod does not currently support multiple diameters/radii through the d1 and d2 parameters. This may change as need arises. You can still use the original cylinder primitive when circumstances call for it, but a better idea might be to perform the hull between two rods, one of infinitesimal height.

另一个更不幸的副作用意味着杆目前不支持通过 d1 和 d2 参数的多个直径/半径。这可能会随着需要而改变。当情况需要时,您仍然可以使用原始圆柱体原语,但更好的想法可能是在两根杆之间执行船体,其中一根杆的高度非常小。

hulled() rod(d=30, h=infinitesimal) rod(d=10, h=20);

Styling (AKA "CSG")

Relativity.scad transforms the way you work with objects in OpenSCAD. You'll find yourself working a lot more with “tree-like” structures, where geometric primitives nest inside one another. This is similar to the way you write using nested tags in html. Just like html, there is a system for performing operations on the tree-like structure. In html, there's css. In relativity, there's the CSG operations

Relativity.scad 改变了您在 OpenSCAD 中处理对象的方式。您会发现自己更多地使用“树状”结构,其中几何基元相互嵌套。这类似于您在 html 中使用嵌套标签编写的方式。就像 html 一样,有一个系统可以对树状结构进行操作。在html中,有css。在相对论中,有 CSG 操作:

ModuleDemoCode
differed
differed("hole","not(hole)") 
 ball(50)  
 orient([x,y,z])  
 rod(d=25, h=50, $class="hole");
hulled
hulled("hole")
 ball(50)  
 orient([x,y,z])  
 rod(d=25, h=50, $class="hole");
intersected
intersected("hole", "not(hole)")
 ball(50)  
 orient([x,y,z])  
 rod(d=25, h=50, $class="hole");
show
show("hole")
 ball(50)  
 orient([x,y,z])  
 rod(d=25, h=50, $class="hole");  
hide
hide("hole")
 ball(50)  
 orient([x,y,z])  
 rod(d=25, h=50, $class="hole");  
colored
colored("red", "hole") 
 ball(50) 
 orient([x,y,z]) 
 rod(d=25, h=50, $class="hole"); 
scaled
scaled(0.95, "hole") 
 ball(50) 
 orient([x,y,z]) 
 rod(d=25, h=50, $class="hole"); 
resized
resized([47,47,47], "hole") 
 ball(50) 
 orient([x,y,z]) 
 rod(d=25, h=50, $class="hole"); 
mirrored
 mirrored(y) 
 rotated(22*z, class="hole") 
 ball(50) 
 orient([x,y,z]) 
 rod(d=25, h=50, $class="hole"); 
rotated
rotated(22*z, class="hole") 
 ball(50) 
 orient([x,y,z]) 
 rod(d=25, h=50, $class="hole"); 
translated
translated(10*z, class="hole") 
 ball(50) 
 orient([x,y,z]) 
 rod(d=25, h=50, $class="hole"); 

Selectors

Selectors are the means through which CSG operations specify class. Selectors are strings whose format is meant to resemble a subset of CSS selector syntax.

选择器是 CSG 操作指定类的方法。选择器是字符串,其格式类似于 CSS 选择器语法的子集。

Illustrations below uses the following code:

下面的插图使用以下代码:

colored("blue", ...)
ball(50, $class="parent")
{
    align(x)
    ball(40, $class="child A")
    align(x)
    ball(30, $class="grandchild");
    
    align(z)
    ball(40, $class="child B")
    align(z)
    ball(30, $class="grandchild");
    
    align(-x)
    ball(40, $class="child C")
    align(-x)
    ball(30, $class="grandchild");
}

As of version 2015.02.14, selectors support the following syntax:

从 2015.02.14 版本开始,选择器支持以下语法:

SelectorRenderDescription
TreeA fractal tree rendered using parent() and mirrored()
使用 parent() 和 mirrored() 渲染的分形树
Stepper MotorA high fidelity NEMA-17 stepper motor written in 50 lines of code
用 50 行代码编写的高保真 NEMA-17 步进电机
Motor MountA stepper motor mount made easy with attach()
使用 attach() 可以轻松安装步进电机
Human BodyA human body designed using techniques in classical figure drawing. Work in progress. Not for the faint-hearted.
使用古典人物绘画技术设计的人体。工作正在进行中。不适合胆小的人。
Flower
Centipede

https://github.com/davidson16807/relativity.scad/wiki

knowledge/openscad/relativity.scad.txt · 最后更改: 2023/06/07 04:23 由 127.0.0.1