Adding example of making many holes
Some checks failed
benchmarks / benchmarks (macos-13, 3.12) (push) Has been cancelled
benchmarks / benchmarks (macos-14, 3.12) (push) Has been cancelled
benchmarks / benchmarks (ubuntu-latest, 3.12) (push) Has been cancelled
benchmarks / benchmarks (windows-latest, 3.12) (push) Has been cancelled
Upload coverage reports to Codecov / run (push) Has been cancelled
pylint / lint (3.10) (push) Has been cancelled
Run type checker / typecheck (3.10) (push) Has been cancelled
Run type checker / typecheck (3.13) (push) Has been cancelled
Wheel building and publishing / Build wheel on ubuntu-latest (push) Has been cancelled
tests / tests (macos-13, 3.10) (push) Has been cancelled
tests / tests (macos-13, 3.13) (push) Has been cancelled
tests / tests (macos-14, 3.10) (push) Has been cancelled
tests / tests (macos-14, 3.13) (push) Has been cancelled
tests / tests (ubuntu-latest, 3.10) (push) Has been cancelled
tests / tests (ubuntu-latest, 3.13) (push) Has been cancelled
tests / tests (windows-latest, 3.10) (push) Has been cancelled
tests / tests (windows-latest, 3.13) (push) Has been cancelled
Wheel building and publishing / upload_pypi (push) Has been cancelled

This commit is contained in:
gumyr 2025-05-31 09:41:50 -04:00
parent b74f8023a3
commit 6aaadd12a4
3 changed files with 96 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 53 KiB

View file

@ -49,6 +49,11 @@ Most of the examples show the builder and algebra modes.
:link: clock_face
:link-type: ref
.. grid-item-card:: Fast Grid Holes |Algebra|
:img-top: assets/examples/fast_grid_holes.png
:link: fast_grid_holes
:link-type: ref
.. grid-item-card:: Handle |Builder| |Algebra|
:img-top: assets/examples/handle.png
:link: handle
@ -325,6 +330,32 @@ a detailed and visually appealing clock design.
:class:`~build_common.PolarLocations` are used to position features on the clock face.
.. _fast_grid_holes:
Fast Grid Holes
---------------
.. image:: assets/examples/fast_grid_holes.png
:align: center
.. dropdown:: |Algebra| Reference Implementation (Algebra Mode)
.. literalinclude:: ../examples/fast_grid_holes.py
:start-after: [Code]
:end-before: [End]
This example demonstrates an efficient approach to creating a large number of holes
(625 in this case) in a planar part using build123d.
Instead of modeling and subtracting 3D solids for each hole—which is computationally
expensive—this method constructs a 2D Face from an outer perimeter wire and a list of
hole wires. The entire face is then extruded in a single operation to form the final
3D object. This approach significantly reduces modeling time and complexity.
The hexagonal hole pattern is generated using HexLocations, and each location is
populated with a hexagonal wire. These wires are passed directly to the Face constructor
as holes. On a typical Linux laptop, this script completes in approximately 1.02 seconds,
compared to substantially longer runtimes for boolean subtraction of individual holes in 3D.
.. _handle:

View file

@ -0,0 +1,65 @@
"""
A fast way to make many holes.
name: fast_grid_holes.py
by: Gumyr
date: May 31, 2025
desc:
This example demonstrates an efficient approach to creating a large number of holes
(625 in this case) in a planar part using build123d.
Instead of modeling and subtracting 3D solids for each holewhich is computationally
expensivethis method constructs a 2D Face from an outer perimeter wire and a list of
hole wires. The entire face is then extruded in a single operation to form the final
3D object. This approach significantly reduces modeling time and complexity.
The hexagonal hole pattern is generated using HexLocations, and each location is
populated with a hexagonal wire. These wires are passed directly to the Face constructor
as holes. On a typical Linux laptop, this script completes in approximately 1.02 seconds,
compared to substantially longer runtimes for boolean subtraction of individual holes in 3D.
license:
Copyright 2025 Gumyr
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
# [Code]
import timeit
from build123d import *
from ocp_vscode import show
start_time = timeit.default_timer()
# Calculate the locations of 625 holes
major_r = 10
hole_locs = HexLocations(major_r, 25, 25)
# Create wires for both the perimeter and all the holes
face_perimeter = Rectangle(500, 600).wire()
hex_hole = RegularPolygon(major_r - 1, 6, major_radius=True).wire()
holes = hole_locs * hex_hole
# Create a new Face from the perimeter and hole wires
grid_pattern = Face(face_perimeter, holes)
# Extrude to a 3D part
grid = extrude(grid_pattern, 1)
print(f"Time: {timeit.default_timer() - start_time:0.3f}s")
show(grid)
# [End]