跳转到内容

linkcad.geom

几何模块提供用于坐标计算和形状操作的 2D 基元。

from linkcad.v1.geom import Point, Vector, Bounds, Transformation, Angle, Resolution

坐标是以数据库单位表示的整数。PointVector 是纯值类型——它们不是数据库对象,可以自由创建。

Point

二维坐标。

from linkcad.v1.geom import Point
p = Point(1000, 2000)
print(p.x, p.y)
print(p[0], p[1]) # indexable, so a Point unpacks like a tuple
属性 / 方法说明
Point() / Point(x, y)构造一个点
xy坐标分量;可赋值
distance_to(other)欧氏距离
equals(other, tolerance=1e-12)容差范围内的相等判断
+-点运算
*/按整数缩放
==!=精确比较
p[0] / p[1]按索引访问 xy

凡是 LinkCAD 接受顶点的地方,普通的 (x, y) 元组同样可用:

cell.add_polygon(layer, [(0, 0), (1000, 0), (1000, 1000)])
cell.add_polygon(layer, [Point(0, 0), Point(1000, 0), Point(1000, 1000)])

Vector

二维方向或偏移。

from linkcad.v1.geom import Vector
v = Vector(100, 200)
print(v.length)
属性 / 方法说明
Vector() / Vector(x, y)构造一个向量
xy向量分量;可赋值
length向量长度
normalize()就地归一化为单位长度
+-向量运算
*/标量乘除

Transformation

二维变换:等比缩放、旋转和平移。先构造,再就地组合——变更类方法返回变换自身,因此可以链式调用。

from linkcad.v1.geom import Transformation
xform = Transformation()
xform.rotate(45.0).translate(1000, 2000)
print(xform.rotation.degrees, xform.translation.x, xform.translation.y)

顺序很重要。rotate() 也会旋转已经设置的偏移,因此若希望偏移就是最终位置,请先旋转再平移。

方法 / 属性说明
Transformation()恒等变换
Transformation(scale, rotation, offset)由缩放系数、AngleVector 构造
translate(dx, dy)添加平移。坐标会舍入为数据库整数
rotate(degrees)添加旋转,单位为度
scale(factor)添加等比缩放
scaling缩放系数;可赋值
rotation旋转 Angle(只读)
translation平移 Vector(只读)
inverse逆变换
is_identity变换不产生任何效果时为 True
transform_point(point)对一个 Point 应用变换,返回一个 Point

transform_point() 接受并返回数据库 Point,因此结果可以直接传回 cell.add_polygon()

from linkcad.v1.geom import Point, Transformation
xform = Transformation()
xform.rotate(90.0).translate(1000, 2000)
moved = [xform.transform_point(Point(x, y)) for x, y in [(0, 0), (100, 0), (100, 100)]]
cell.add_polygon(layer, moved)

Bounds

轴对齐边界框。cell.boundscell_object.bounds 会返回它。

b = cell.bounds
if not b.empty:
print(b.min_x, b.min_y, b.width, b.height)
属性 / 方法说明
Bounds() / Bounds(min_pt, max_pt)构造空盒,或由两个 Point 构造
min_xy / max_xy角点
min_x / max_x / min_y / max_y各条边
width / height尺寸
center中心点
empty盒内无内容时为 True
contains(point, tolerance=0)点是否在边界内
overlaps(other)重叠判断

Bounds 上的每个度量都是名词,因而都是属性。这里曾是 API 中唯一泄漏 C++ 拼写的地方——minXY()maxX()——现在不再如此。

Angle

角度值,用于 LinkCAD 接受或返回角度量的所有位置。

from linkcad.v1.geom import Angle
a = Angle.from_degrees(45.0)
b = Angle.from_radians(0.785398)
print(a.degrees, a.radians)
工厂方法 / 属性 / 方法说明
Angle()零角度
Angle.from_degrees(degrees)由度创建
Angle.from_radians(radians)由弧度创建
angle.degrees以度表示的值
angle.radians以弧度表示的值
angle.equals(other, tolerance_radians=1e-12)容差范围内的比较
Angle.zero / Angle.pi_half / Angle.pi / Angle.two_pi0°、90°、180° 和 360° 的常量

Resolution

控制在合并或分解等处理操作中,曲线形状(圆、圆弧、椭圆、NURBS)如何细分为多边形顶点。

from linkcad.v1.geom import Resolution
res = Resolution()
res.minimum_facets = 32 # at least 32 segments per full circle
res.maximum_error = 1 # max deviation in database units
print(res.segment_count(10_000))
属性 / 方法说明
Resolution() / Resolution(min_facets=0, error_max=...)构造
minimum_facets每个整圆的最少多边形段数;可赋值
maximum_error相对真实曲线的最大允许偏差,以数据库单位表示;可赋值
segment_count(diameter)该直径的圆会被分成多少段