NSDT工具推荐Three.js AI纹理开发包 - YOLO合成数据生成器 - GLTF/GLB在线编辑 - 3D模型格式在线转换 - 可编程3D场景编辑器 - REVIT导出3D模型插件 - 3D模型语义搜索引擎 - Three.js虚拟轴心开发包 - 3D模型在线减面 - STL模型在线切割

Build123d 是一个基于 Python 的参数化边界表示 (BREP) 建模框架,适用于 2D 和 3D CAD。它基于 Open Cascade 几何内核构建,允许使用简单直观的 Python 语法创建复杂模型。 Build123d 可用于创建用于 3D 打印、CNC 加工、激光切割和其他制造工艺的模型。模型可以导出到各种流行的 CAD 工具,例如 FreeCAD 和 SolidWorks。

Build123d 可以被视为 CadQuery 的演变,其中有些限制性的 Fluent API(方法链)被有状态上下文管理器(即块)取代,从而启用完整的 python 工具箱:for 循环、对象引用、对象排序和过滤等。

Build123d 使用标准的 python 上下文管理器 - 即处理文件时经常使用的 with 语句 - 作为正在构建的对象的构建器。对象完成后,可以从构建器中提取它并以其他方式使用:例如导出为 STEP 文件或在装配中使用。

Build123d有三个可用的构建器:

  • BuildLine:一维对象的构建器 - 具有长度属性但不具有面积或体积属性的对象 - 通常用于创建草图或路径中使用的复杂线条。
  • BuildSketch:平面二维对象的构建器 - 具有面积属性但不具有体积属性的对象 - 通常用于创建挤压成 3D 零件的 2D 绘图。
  • BuildPart:三维对象(具有体积属性的对象)的构建器,用于创建单独的零件。

这三个构建器在层次结构中一起工作,如下所示:

with BuildPart() as my_part:
    ...
    with BuildSketch() as my_sketch:
        ...
        with BuildLine() as my_line:
            ...
        ...
    ...

其中,线条完成后, my_line将被添加到 my_sketch;草图完成后, my_sketch 将被添加到 my_part

以茶杯的设计为例:

from build123d import *
from ocp_vscode import show

wall_thickness = 3 * MM
fillet_radius = wall_thickness * 0.49

with BuildPart() as tea_cup:
    # Create the bowl of the cup as a revolved cross section
    with BuildSketch(Plane.XZ) as bowl_section:
        with BuildLine():
            # Start & end points with control tangents
            s = Spline(
                (30 * MM, 10 * MM),
                (69 * MM, 105 * MM),
                tangents=((1, 0.5), (0.7, 1)),
                tangent_scalars=(1.75, 1),
            )
            # Lines to finish creating ½ the bowl shape
            Polyline(s @ 0, s @ 0 + (10 * MM, -10 * MM), (0, 0), (0, (s @ 1).Y), s @ 1)
        make_face()  # Create a filled 2D shape
    revolve(axis=Axis.Z)
    # Hollow out the bowl with openings on the top and bottom
    offset(amount=-wall_thickness, openings=tea_cup.faces().filter_by(GeomType.PLANE))
    # Add a bottom to the bowl
    with Locations((0, 0, (s @ 0).Y)):
        Cylinder(radius=(s @ 0).X, height=wall_thickness)
    # Smooth out all the edges
    fillet(tea_cup.edges(), radius=fillet_radius)

    # Determine where the handle contacts the bowl
    handle_intersections = [
        tea_cup.part.find_intersection(
            Axis(origin=(0, 0, vertical_offset), direction=(1, 0, 0))
        )[-1][0]
        for vertical_offset in [35 * MM, 80 * MM]
    ]
    # Create a path for handle creation
    with BuildLine(Plane.XZ) as handle_path:
        path_spline = Spline(
            handle_intersections[0] - (wall_thickness / 2, 0),
            handle_intersections[0] + (35 * MM, 30 * MM),
            handle_intersections[0] + (40 * MM, 60 * MM),
            handle_intersections[1] - (wall_thickness / 2, 0),
            tangents=((1, 1.25), (-0.2, -1)),
        )
    # Align the cross section to the beginning of the path
    with BuildSketch(
        Plane(origin=path_spline @ 0, z_dir=path_spline % 0)
    ) as handle_cross_section:
        RectangleRounded(wall_thickness, 8 * MM, fillet_radius)
    sweep()  # Sweep handle cross section along path

assert abs(tea_cup.part.volume - 130326) < 1

show(tea_cup, names=["tea cup"])

结果如下所示:


原文链接:Build123d - A python CAD programming library

BimAnt翻译整理,转载请标明出处