build123d

repository·dev·Indexed 23 days ago

https://github.com/gumyr/build123d

A Python-based parametric boundary representation (BREP) modeling framework for 2D and 3D CAD. Built on the Open Cascade geometric kernel, it is designed for creating precise models for manufacturing processes such as 3D printing and CNC machining. It features two primary paradigms: a stateless Algebra Mode using algebraic operators and a stateful Builder Mode using nested context managers. The library supports importing/exporting SVG and STEP files, as well as advanced geometry filtering, sorting, and assembly management via Compound objects.

Tokens
46.2K
Snippets
86
Records
332
Agent score
84%

What's inside build123d

  1. Overview of build123d

    dev

    build123d is a Python-based, parametric (BREP) modeling framework for 2D and 3D CAD. It is built on the Open Cascade geometric kernel and is designed for creating precise models for 3D printing, CNC machining, and laser cutting.

    Key features include:

    • Algebraic Modeling: Uses operator-driven design (e.g., obj += sub_obj) for readable and composable logic.
    • Explicit Geometry Classes: Well-defined 1D, 2D, and 3D geometry classes.
    • Pythonic Integration: Selectors behave like lists, locations are iterables, and supports natural conversions (e.g., Solid(shell), tuple(Vector)).
    • Extensibility: Supports subclassing and functional composition without monkey patching.
    • Interoperability: Models can be exported to CAD tools like FreeCAD and SolidWorks.
  2. Overview of Joints in build123d

    dev

    Joints allow Solid and Compound objects to be arranged relative to each other with degrees of motion similar to physical joints. Joints always work in pairs and must be connected to a compatible joint type using the connect_to method.

    Joint Compatibility Matrix:

    Joint TypeCan connect to...
    BallJointRigidJoint
    CylindricalJointRigidJoint
    LinearJointRigidJoint or RevoluteJoint
    RevoluteJointRigidJoint
    RigidJointRigidJoint

    Key Concepts:

    • Labels: Each joint on an object must have a unique label within that part.
    • symbol property: All joints have a symbol property used for visualization (supported by ocp-vscode).
    • BuildPart Scope: If joints are created inside a BuildPart builder, the to_part parameter is optional; the builder automatically transfers joints to the resulting part upon exit.
    • Connection Behavior: The connect_to method performs a one-time repositioning. To maintain relative locations, place the parts in an assembly, combine them with boolean operations, or use a BuildPart context.
  3. Overview of Surface Modeling in build123d

    dev

    Surface modeling in build123d involves the direct creation and manipulation of the skin (bounding faces) of a 3D object. Unlike volumetric modeling (extruding or revolving), surface modeling focuses on building individual curved or planar faces.

    In the BREP (Boundary Representation) system used by build123d, solids are defined by a hierarchy of faces, edges, and vertices. To create a solid, you must ensure that adjacent faces share edges consistently to form a continuous, manifold topology.Shell. A shell that is properly oriented and encloses a finite region of space becomes a solid. For robust results, it is recommended to reuse the same topology.Edge objects across adjacent faces to ensure the final shell is watertight.

  4. Available Stateful Contexts

    dev

    build123d provides several stateful contexts for managing geometric construction workflows:

    • 1D Construction: BuildLine
    • 2D Sketching: BuildSketch
    • 3D Part Building: BuildPart
    • Location Helpers: GridLocations, HexLocations, Locations, and PolarLocations
  5. Leverage Parameterized Modeling for CAD

    dev

    build123d supports parameterized modeling, which allows you to define designs using variables and constraints rather than fixed geometry.

    Benefits of using parameterized models:

    • Reusability: Modify designs by changing parameters (e.g., length, width) instead of manual geometry editing.
    • Design Exploration: Quickly visualize different design options by adjusting parameters.
    • Constraints and Relationships: Define relationships between parameters to ensure the model remains valid during changes.
    • Automation: Automate repetitive tasks like generating parts lists or detailed drawings.
    • Collaboration: Maintain consistency across different development stages and team members.
  6. Understand Boundary Representation (BREP) Modeling in build123d

    dev

    build123d uses Boundary Representation (BREP) for 3D modeling. Unlike mesh-based systems (like Blender or OpenSCAD) that use triangles, BREP uses mathematical representations to define shapes.

    Key advantages of the BREP approach used in build123d:

    • Precision: Mathematical definitions allow for highly accurate modeling of complex shapes.
    • Topology: Maintains explicit information about edges, faces, and vertices, enabling robust Boolean operations.
    • Analytical Modeling: Supports operations like collision detection, mass property calculations, and finite element analysis via topological data.
    • Feature-based Modeling: Models are built by creating and modifying individual features (holes, fillets, chamfers), which facilitates parametric design.
    • Efficient Storage: Uses a compact mathematical representation rather than large triangle datasets.
  7. Understand build123d Topology

    dev

    Topology in build123d refers to the hierarchical structure of geometric elements and their relationships. All topological objects inherit from the Shape base class. The hierarchy typically follows this structure:

    • Shape: The base class for all topological elements.
    • Compound: A container for grouping multiple geometric shapes (vertices, edges, faces, etc.).
    • Solid: A bounded, watertight volume suitable for Boolean operations.
    • Shell: A collection of Face objects defining a connected volume.
    • Face: A 2D surface.
    • Wire: A connected sequence of Edge objects (loops).
    • Edge: A 1D curve.
    • Vertex: A 0D point.

    You can inspect this hierarchy using the show_topology() method on any shape.

  8. Use a Face as a Plane for BuildSketch

    dev
    The BuildSketch class accepts either a Plane or a Face. When using a Face, ensure it is planar to avoid unpredictable behavior. You can use GridLocations to create a grid of points for placing multiple objects simultaneously. To cut shapes from a parent part, use extrude with a negative amount and Mode.SUBTRACT (in Builder mode) or the - operator (in Algebra mode).
  9. Emboss and Deboss text

    dev

    To place text on a face:

    1. Use BuildSketch on a target face.
    2. Use build_enums.Align to control text placement.
    3. To Emboss (raise): Place the sketch on the top face.
    4. To Deboss (indent): Place the sketch on the same face but use a negative extrusion or a different operation.

    Tip: When performing multiple operations on the same face, avoid re-selecting the face using a generic selector like faces().sort_by(Axis.Z)[-1] if the first operation changed the face topology; instead, store the face in a variable.

  10. Leverage topological references and selectors

    dev

    Instead of manually calculating positions for fillets or chamfers (as required in OpenSCAD), use build123d's selector system to query topological features. You can filter edges, vertices, or faces of an existing object to apply operations precisely.

    For example, to fillet only the interior edges of a part, use .edges().filter_by(lambda e: e.is_interior).

  11. Use CylindricalJoint for screw-like motion

    dev

    A CylindricalJoint allows for motion both around and along an axis, mimicking the behavior of a screw.

    • Axis of Motion: Defines the direction of movement.
    • Position: Controls the linear movement along the axis. For example, a position of 0 might align a screw head with a surface, while positive values move it further along the axis.
    • Angular Range: You can set an angular range (e.g., (0, 360)) to allow full rotation.