cadquery/notebook_nueva/utils.py

84 lines
2.3 KiB
Python
Raw Normal View History

2023-04-15 21:39:03 +00:00
import shutil
import tempfile
from math import floor
2023-04-15 21:39:03 +00:00
import cadquery as cq
from cadquery import exporters
def extrude_shape(*, model, face, w, h, x_offset, y_offset, element, height):
return (
model.faces(face)
.workplane(centerOption="CenterOfBoundBox")
.center(-w / 2 + x_offset + element["x"], -h / 2 + y_offset + element["y"])
.placeSketch(element["shape"])
.extrude(height)
)
def punch_hole(*, model, face, w, h, x_offset, y_offset, hole, depth):
return (
model.faces(face)
.workplane(centerOption="CenterOfBoundBox")
.center(-w / 2 + x_offset + hole["x"], -h / 2 + y_offset + hole["y"])
.placeSketch(hole["shape"])
.cutBlind(-depth)
)
2023-03-08 20:24:43 +00:00
2023-03-20 16:19:14 +00:00
def extrude_shape2(*, model, face, w, h, x_offset, y_offset, hole, depth):
return (
model.faces(face)
.workplane(centerOption="CenterOfBoundBox")
.center(-w / 2 + x_offset + hole["x"], -h / 2 + y_offset + hole["y"])
.placeSketch(hole["shape"])
.extrude(-depth)
)
2023-04-04 18:37:19 +00:00
def hex_vents(*, size, width, height, density=0.85):
# size is radius of the hexagon
# Information about how this works:
# https://www.redblobgames.com/grids/hexagons/
x_step = size * (3**0.5)
y_step = size * 3 / 2
x_count = floor(width / x_step) - 1
if height > 4 * size:
y_count = floor((height - 2 * size) / (1.5 * size))
else:
y_count = 1
x_size = (x_count + 0.5) * x_step # Assumes at least 2 rows
y_size = 2 * size + 1.5 * size * (y_count - 1)
x_offset = (width - x_size) / 2 + 0.5 * x_step
y_offset = (height - y_size) / 2 + size
2023-03-20 16:19:14 +00:00
vent_positions = []
for x in range(0, x_count):
for y in range(0, y_count):
vent_positions.append(
(
(x + (y % 2) / 2) * x_step + x_offset,
y * y_step + y_offset,
)
)
vents = [
{
"x": 0,
"y": 0,
2023-04-04 18:37:19 +00:00
"shape": cq.Sketch().push(vent_positions).regularPolygon(size * density, 6),
}
]
2023-03-20 16:19:14 +00:00
return vents
2023-04-15 21:39:03 +00:00
def export(model, fname, **kwarg):
tmpfile = tempfile.mktemp(suffix="." + fname.split(".")[-1])
exporters.export(model, tmpfile, **kwarg)
shutil.move(tmpfile, fname)