用于 2D 和 3D CAD 绘图的开源 Python 库

用于创建和管理 2D 和 3D 模型的开源 Python CAD 库。在 Python 应用程序中创建参数化乐高积木并使用选择器。

什么是 Build123d 库?

Build123d 是一个开源 Python 3D CAD 包,可帮助软件开发人员在自己的 Python 应用程序中创建 2D 和 3D 图形。Build123d 库使用标准的 Python 上下文管理器,并提供三个用于处理图形的构建器。BuildLine 构建器可用于一维对象,BuildSketch 构建器可用于平面二维对象,BuildPart 构建器可用于三维对象。构建器使用位置上下文来定位对象或执行操作。

该库支持各种 2D 和 3D 操作,例如圆角、镜像、偏移、缩放、分割、CounterBoreHole、CounterSinkHole、拉伸、孔、放样、旋转、缩放、截面、分割等等。除上述内容外,该库还包含多个选择器、选择器运算符、边和线运算符、平面运算符、向量运算符以及顶点运算符。

Build123d 库是 Build123d API 的一个实用替代方案,它拥有诸多优势,其中最重要的一点是,build123d 在设计对象时能够使用完整的 Python 工具箱。该库操作非常简单,用户可以轻松地在任何地方定位和管理对象。

Previous Next

开始使用 Build123d

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

通过 GitHub 安装 Build123d

python3 -m pip install git+https://github.com/gumyr/build123d.git#egg=build123d 

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

如何通过 Python 库使用选择器

在使用基于 GUI 的 CAD 系统时,用户通常会点击某个特征来选择它执行某些操作。选择器是一种程序,它展示了用户如何使用 Python 过滤器和排序方法将特征从设计中分离出来,这些方法通常以一组自定义 Python 操作来实现。vertices()、edges()、wires()、solids() 和 faces() 就是一些选择器的示例。操作数类型包括:Axis、SortBy 和 GeomType。请记住,像 sorted 或 filtered 这样的标准列表方法有助于生成复杂的选择器。

通过 Python 库创建参数化乐高积木

开源 Python 库 Build123d 允许软件开发人员在自己的 Python 应用程序中创建参数化乐高积木。首先,您需要导入 build123d 环境并定义乐高积木的尺寸。现在,我们可以进入构建器部分,创建积木的内部结构。对于结构,我们需要绘制一个二维草图,然后将其挤压成三维对象。之后,我们可以定义周长矩形,并创建积木矩形的墙面和内部网格。之后,我们需要将内部网格转换为脊线,并移除中心点。现在,创建一组内部空心圆柱体,并将草图挤压成墙面。墙面完成后,需要添加积木的顶部,最后一步是添加点。

如何通过 Python API 创建乐高积木的尺寸和内部网格?

from build123d import *

pip_count = 6

lego_unit_size = 8
pip_height = 1.8
pip_diameter = 4.8
block_length = lego_unit_size * pip_count
block_width = 16
base_height = 9.6
block_height = base_height + pip_height
support_outer_diameter = 6.5
support_inner_diameter = 4.8
ridge_width = 0.6
ridge_depth = 0.3
wall_thickness = 1.2

// instantiate a BuildPart
with BuildPart() as lego:

//create a sketch builder
with BuildPart() as lego:
    # Draw the bottom of the block
    with BuildSketch() as plan:

//create Perimeter Rectangle
with BuildPart() as lego:
    # Draw the bottom of the block
    with BuildSketch() as plan:
        # Start with a Rectangle the size of the block
        perimeter = Rectangle(width=block_length, height=block_width)
 
 //create the walls of the block
ith BuildPart() as lego:
    # Draw the bottom of the block
    with BuildSketch() as plan:
        # Start with a Rectangle the size of the block
        perimeter = Rectangle(width=block_length, height=block_width)
        # Subtract an offset to create the block walls
        Offset(
            perimeter,
            amount=-wall_thickness,
            kind=Kind.INTERSECTION,
            mode=Mode.SUBTRACT,
        )
//Create Internal Grid
with BuildPart() as lego:
    # Draw the bottom of the block
    with BuildSketch() as plan:
        # Start with a Rectangle the size of the block
        perimeter = Rectangle(width=block_length, height=block_width)
        # Subtract an offset to create the block walls
        Offset(
            perimeter,
            amount=-wall_thickness,
            kind=Kind.INTERSECTION,
            mode=Mode.SUBTRACT,
        )
        # Add a grid of lengthwise and widthwise bars
        with GridLocations(x_spacing=0, y_spacing=lego_unit_size, x_count=1, y_count=2):
            Rectangle(width=block_length, height=ridge_width)
        with GridLocations(lego_unit_size, 0, pip_count, 1):
            Rectangle(width=ridge_width, height=block_width)
 
 
 中国人