用于 3D CAD 文件格式的开源 Python 库

用于创建参数化 3D CAD 模型的免费 Python CAD 库。除了传统的 STL 之外,它还可以创建高质量的 CAD 格式,如 STEP、AMF 和 3MF

开源 Python 库 CadQuery 是一个易于使用的 Python 库,用于构建参数化 3D CAD 模型。该库非常强大,通过编写简短的脚本可以在 Python 应用程序中生成高质量的 CAD 模型。还可以自定义脚本并使用单个脚本制作许多不同的对象。 CadQuery 脚本速度非常快,可以比其他可用库更快地构建 STL、STEP、AMF 和 3MF。

该库提供高级建模功能,例如圆角、曲线拉伸、参数曲线和放样。它是一个基于脚本的库,可以创建最终用户可以轻松定制的参数化模型。它使用尽可能接近您向人类描述对象的方式的脚本创建 3D 模型。 CadQuery 根据 Apache 公共许可证 2.0 版的条款获得许可。

该 CadQuery 库被特意设计为无 GUI,因此它可以在工程和科学应用程序中使用,以编程方式创建 3D 模型。如果需要使用 GUI,可以考虑基于 Qt 的 GUI CQ-editor 或 Jupyter 扩展 jupyter-cadquery。也可以将该库与其他 Python 库一起使用。

Previous Next

CadQuery 入门

安装 CadQuery 稳定版的最简单方法是使用 pip。请使用以下命令顺利安装。

通过 pip 安装 CadQuery

pip install cadquery 

您可以从 Github 存储库下载已编译的共享库。

通过 Python 3D 库使用多边形

多边形是具有直边的二维形状。多边形建模是一种通过使用多边形网格表示或近似其表面来建模对象的方法。开源 Python 库 CadQuery 使软件开发人员只需几行 Python 代码即可为每个堆栈点创建多边形。这对于固件无法针对小孔尺寸进行校正的 3d 打印机非常有用。

通过 Python 3D 库在面上创建工作平面

开源 Python 库 CadQuery 为在 Python 应用程序中的面上创建工作平面提供了完整的支持。以这种方式使用工作平面是 CadQuery 的一个关键特性。它使用户无需搜索各种特征在变量中的位置,并使模型能够删除冗余维度。用户可以通过调用 Workplane.faces() 方法来选择生成的实体的面。请记住,默认情况下,新工作平面的原点是通过从选定的面创建一个平面并将先前的原点投影到该平面上来计算的。

通过 Python 在面上创建工作平面

result = cq.Workplane("front").box(2, 3, 0.5)  # make a basic prism
result = result.faces(">Z").workplane().hole(0.5)  # find the top-most face and make a hole
 

通过 Python 使用 3D 乐高积木

CadQuery 库为使用 Python 命令创建代表不同大小的乐高积木的 3D 动画提供了完整的支持。也可以通过将多个立方体和圆柱体连接在一起来重新制作砖块。我们可以创建一个复合对象,将这些 3D 形状连接到一个对象(砖块)中。以下示例演示了如何生成任意大小的常规矩形 Lego(TM) 积木。这只是因为关于砖块底部的逻辑而变得棘手。

通过 Python 创建 3D 乐高积木

#####
# Inputs
######
lbumps = 6       # number of bumps long
wbumps = 2       # number of bumps wide
thin = True      # True for thin, False for thick
#
# Lego Brick Constants-- these make a lego brick a lego :)
#
pitch = 8.0
clearance = 0.1
bumpDiam = 4.8
bumpHeight = 1.8
if thin:
    height = 3.2
else:
    height = 9.6
t = (pitch - (2 * clearance) - bumpDiam) / 2.0
postDiam = pitch - t  # works out to 6.5
total_length = lbumps*pitch - 2.0*clearance
total_width = wbumps*pitch - 2.0*clearance
# make the base
s = cq.Workplane("XY").box(total_length, total_width, height)
# shell inwards not outwards
s = s.faces("Z").workplane(). \
    rarray(pitch, pitch, lbumps, wbumps, True).circle(bumpDiam / 2.0) \
    .extrude(bumpHeight)
# add posts on the bottom. posts are different diameter depending on geometry
# solid studs for 1 bump, tubes for multiple, none for 1x1
tmp = s.faces(" 1 and wbumps > 1:
    tmp = tmp.rarray(pitch, pitch, lbumps - 1, wbumps - 1, center=True). \
        circle(postDiam / 2.0).circle(bumpDiam / 2.0).extrude(height - t)
elif lbumps > 1:
    tmp = tmp.rarray(pitch, pitch, lbumps - 1, 1, center=True). \
        circle(t).extrude(height - t)
elif wbumps > 1:
    tmp = tmp.rarray(pitch, pitch, 1, wbumps - 1, center=True). \
        circle(t).extrude(height - t)
else:
    tmp = s
# Render the solid
build_object(tmp)
 中国人