Some ported examples in algebra mode

This commit is contained in:
Bernhard 2023-03-20 08:57:47 +01:00
parent fd4a76bce0
commit e9964e465b
15 changed files with 453 additions and 0 deletions

View file

@ -0,0 +1,9 @@
from build123d import *
b = Box(3, 3, 3)
b2 = Rot(0, 0, 45) * Box(1, 2, 0.1)
for plane in [Plane(f) for f in b.faces()]:
b += plane * b2
if "show_object" in locals():
show_object(b, name="box on faces")

View file

@ -0,0 +1,60 @@
from alg123d import *
logo_text = Text("123d", font_size=10, align=(Align.MIN, Align.MIN))
font_height = logo_text.vertices().sort_by(Axis.Y)[-1].Y
build_text = Text("build", font_size=5, align=(Align.CENTER, Align.CENTER))
build_bb = build_text.bounding_box()
build_width = build_bb.max.X - build_bb.min.X
cust_text = Text(
"customizable",
font_size=2.9,
align=(Align.CENTER, Align.CENTER),
font_style=FontStyle.BOLD,
)
cust_bb = cust_text.bounding_box()
cust_width = cust_bb.max.X - cust_bb.min.X
l1 = Line((font_height * 0.3, 0), (font_height * 0.3, font_height))
one = l1 + TangentArc(l1 @ 1, (0, font_height * 0.7), tangent=(l1 % 1) * -1)
two = Pos(font_height * 0.35, 0) * Text("2", font_size=10, align=(Align.MIN, Align.MIN))
three_d = Text("3d", font_size=10, align=(Align.MIN, Align.MIN))
three_d = Pos(font_height * 1.1, 0) * extrude(three_d, amount=font_height * 0.3)
logo_width = three_d.vertices().sort_by(Axis.X)[-1].X
t1 = TangentArc((0, 0), (1, 0.75), tangent=(1, 0))
arrow_left = t1 + mirror(t1, about=Plane.XZ)
ext_line_length = font_height * 0.5
dim_line_length = (logo_width - build_width - 2 * font_height * 0.05) / 2
l1 = Line((0, -font_height * 0.1), (0, -ext_line_length - font_height * 0.1))
l2 = Line(
(logo_width, -font_height * 0.1),
(logo_width, -ext_line_length - font_height * 0.1),
)
extension_lines = Pos(*(l1 @ 0.5)) * arrow_left
extension_lines += (Pos(*(l2 @ 0.5)) * Rot(z=180)) * arrow_left
extension_lines += Line(l1 @ 0.5, l1 @ 0.5 + Vector(dim_line_length, 0))
extension_lines += Line(l2 @ 0.5, l2 @ 0.5 - Vector(dim_line_length, 0))
# Precisely center the build Faces
p1 = Pos((l1 @ 0.5 + l2 @ 0.5) / 2 - Vector((build_bb.max.X + build_bb.min.X) / 2, 0))
build = p1 * build_text
# add the customizable text to the build text sketch
p2 = Pos((l1 @ 1 + l2 @ 1) / 2 - Vector(cust_bb.max.X + cust_bb.min.X, 1.4))
build += p2 * cust_text
cmpd = Compound.make_compound([three_d, two, one, build, extension_lines])
if "show_object" in locals():
show_object(cmpd, name="compound")
# show_object(one.line.wrapped, name="one")
# show_object(two.sketch.wrapped, name="two")
# show_object(three_d.part.wrapped, name="three_d")
# show_object(extension_lines.line.wrapped, name="extension_lines")
# show_object(build.sketch.wrapped, name="build")

View file

@ -0,0 +1,21 @@
from itertools import product
from alg123d import *
pcb = Rectangle(70, 30)
for loc in GridLocations(60, 20, 2, 2):
pcb -= loc * Circle(2)
for i, y in product(range(65 // 5), (-15, -10, 10, 15)):
x = i * 5 - 30
pcb -= Pos(x, y) * Circle(1)
for x, i in product((30, 35), range(30 // 5 - 1)):
y = i * 5 - 10
pcb -= Pos(x, y) * Circle(1)
pcb = extrude(pcb, 3)
if "show_object" in locals():
show(pcb)

37
examples/clock_algebra.py Normal file
View file

@ -0,0 +1,37 @@
from alg123d import *
clock_radius = 10
l1 = CenterArc((0, 0), clock_radius * 0.975, 0.75, 4.5)
l2 = CenterArc((0, 0), clock_radius * 0.925, 0.75, 4.5)
l3 = Line(l1 @ 0, l2 @ 0)
l4 = Line(l1 @ 1, l2 @ 1)
minute_indicator = make_face([l1, l3, l2, l4])
minute_indicator = fillet(
minute_indicator, minute_indicator.vertices(), radius=clock_radius * 0.01
)
clock_face = Circle(clock_radius)
clock_face -= [loc * minute_indicator for loc in PolarLocations(0, 60)]
clock_face -= [
loc * SlotOverall(clock_radius * 0.05, clock_radius * 0.025)
for loc in PolarLocations(clock_radius * 0.875, 12)
]
clock_face -= [
loc
* Text(
str(hour + 1),
font_size=clock_radius * 0.175,
font_style=FontStyle.BOLD,
align=Align.CENTER,
)
for hour, loc in enumerate(
PolarLocations(clock_radius * 0.75, 12, 60, -360, rotate=False)
)
]
if "show_object" in locals():
show_object(clock_face)

View file

@ -0,0 +1,50 @@
from alg123d import *
# 35x7.5mm DIN Rail Dimensions
overall_width, top_width, height, thickness, fillet_radius = 35, 27, 7.5, 1, 0.8
rail_length = 1000
slot_width, slot_length, slot_pitch = 6.2, 15, 25
din = Rectangle(overall_width, thickness, align=(Align.CENTER, Align.MIN))
din += Rectangle(top_width, height, align=(Align.CENTER, Align.MIN))
din -= Rectangle(
top_width - 2 * thickness,
height - thickness,
align=(Align.CENTER, Align.MIN),
)
inside_vertices = (
din.vertices()
.filter_by_position(Axis.Y, 0.0, height, inclusive=(False, False))
.filter_by_position(
Axis.X,
-overall_width / 2,
overall_width / 2,
inclusive=(False, False),
)
)
din = fillet(din, inside_vertices, radius=fillet_radius)
outside_vertices = filter(
lambda v: (v.Y == 0.0 or v.Y == height)
and -overall_width / 2 < v.X < overall_width / 2,
din.vertices(),
)
din = fillet(din, outside_vertices, radius=fillet_radius + thickness)
rail = extrude(din, rail_length)
plane = Plane(rail.faces().max(Axis.Y))
with LazyAlgCompound() as slot_faces:
for loc in GridLocations(0, slot_pitch, 1, rail_length // slot_pitch - 1):
slot_faces += plane * loc * SlotOverall(slot_length, slot_width)
slots = extrude(slot_faces, -height)
rail -= slots
rail = Plane.XZ * rail
if "show_object" in locals():
show_object(rail, name="rail")

View file

@ -0,0 +1,37 @@
from alg123d import *
segment_count = 6
# Create a path for the sweep along the handle - added to pending_edges
handle_center_line = Spline(
((-10, 0, 0), (0, 0, 5), (10, 0, 0)),
tangents=((0, 0, 1), (0, 0, -1)),
tangent_scalars=(1.5, 1.5),
)
# Record the center line for display and workplane creation
handle_path = handle_center_line.edges()[0]
# Create the cross sections - added to pending_faces
with LazyAlgCompound() as sections:
for i in range(segment_count + 1):
plane = Plane(
origin=handle_path @ (i / segment_count),
z_dir=handle_path % (i / segment_count),
)
if i % segment_count == 0:
section = plane * Circle(1)
else:
section = plane * Rectangle(1.25, 3)
section = fillet(section, section.vertices(), radius=0.2)
sections += section
# Create the handle by sweeping along the path
handle = sweep(sections.faces(), path=handle_path, multisection=True)
if "show_object" in locals():
show_object(handle_path.wrapped, name="handle_path")
for i, section in enumerate(sections):
show_object(section.wrapped, name="section" + str(i))
show_object(handle, name="handle", options=dict(alpha=0.6))

29
examples/holes_algebra.py Normal file
View file

@ -0,0 +1,29 @@
from alg123d import *
thru_hole = Cylinder(radius=3, height=2)
thru_hole -= Bore(thru_hole, radius=1)
# Recessed counter bore hole (hole location = (0,0,0))
recessed_counter_bore = Cylinder(radius=3, height=2)
recessed_counter_bore -= CounterBore(
recessed_counter_bore, radius=1, counter_bore_radius=1.5, counter_bore_depth=0.5
)
# Recessed counter sink hole (hole location = (0,0,0))
recessed_counter_sink = Cylinder(radius=3, height=2)
recessed_counter_sink -= CounterSink(
recessed_counter_sink, radius=1, counter_sink_radius=1.5
)
# Flush counter sink hole (hole location = (0,0,2))
flush_counter_sink = Cylinder(radius=3, height=2)
plane = Plane(flush_counter_sink.faces().max())
flush_counter_sink -= plane * CounterSink(
flush_counter_sink, radius=1, counter_sink_radius=1.5
)
if "show_object" in locals():
show_object(thru_hole, name="though hole")
show_object(Pos(10, 0) * recessed_counter_bore, name="recessed counter bore")
show_object(Pos(0, 10) * recessed_counter_sink, name="recessed counter sink")
show_object(Pos(10, 10) * flush_counter_sink, name="flush counter sink")

View file

@ -0,0 +1,15 @@
from alg123d import *
blocks = Pos(-1, -1, 0) * Box(1, 2, 1, align=(Align.CENTER, Align.MIN, Align.MIN))
blocks += Box(1, 1, 2, align=(Align.CENTER, Align.MIN, Align.MIN))
blocks += Pos(1, -1, 0) * Box(1, 2, 1, align=(Align.CENTER, Align.MIN, Align.MIN))
bottom_edges = blocks.edges().filter_by_position(Axis.Z, 0, 1, inclusive=(True, False))
blocks2 = chamfer(blocks, bottom_edges, length=0.1)
top_edges = blocks2.edges().filter_by_position(Axis.Z, 1, 2, inclusive=(False, True))
blocks2 = chamfer(blocks2, top_edges, length=0.1)
if "show_object" in locals():
show_object(blocks2)

View file

@ -0,0 +1,16 @@
from alg123d import *
pipes = Rot(10, 20, 30) * Box(10, 10, 10)
for plane in [Plane(f) for f in pipes.faces()]:
pipe = plane * Circle(4)
pipes -= extrude(pipe, amount=-5)
pipe = plane * Circle(4.5)
pipe -= plane * Circle(4)
last = pipes.edges()
pipes += extrude(pipe, amount=10)
pipes = fillet(pipes, pipes.edges() - last, radius=0.2)
if "show_object" in locals():
show_object(pipes, name="intersecting pipes")

78
examples/lego_algebra.py Normal file
View file

@ -0,0 +1,78 @@
from alg123d 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
# Draw the bottom of the block
# Start with a Rectangle the size of the block
plan = Rectangle(width=block_length, height=block_width)
# Subtract an offset to create the block walls
plan -= offset(
plan,
amount=-wall_thickness,
kind=Kind.INTERSECTION,
)
# Add a grid of lengthwise and widthwise bars
for loc in GridLocations(x_spacing=0, y_spacing=lego_unit_size, x_count=1, y_count=2):
plan += loc * Rectangle(width=block_length, height=ridge_width)
for loc in GridLocations(lego_unit_size, 0, pip_count, 1):
plan += loc * Rectangle(width=ridge_width, height=block_width)
# Substract a rectangle leaving ribs on the block walls
plan -= Rectangle(
block_length - 2 * (wall_thickness + ridge_depth),
block_width - 2 * (wall_thickness + ridge_depth),
)
# Add a row of hollow circles to the center
for loc in GridLocations(
x_spacing=lego_unit_size, y_spacing=0, x_count=pip_count - 1, y_count=1
):
plan += loc * Circle(support_outer_diameter / 2)
plan -= loc * Circle(support_inner_diameter / 2)
# Extrude this base sketch to the height of the walls
lego = extrude(plan, amount=base_height - wall_thickness)
# Create a box on the top of the walls
for loc in Locations((0, 0, lego.vertices().max().Z)):
# Create the top of the block
lego += loc * Box(
length=block_length,
width=block_width,
height=wall_thickness,
align=(Align.CENTER, Align.CENTER, Align.MIN),
)
# Create a workplane on the top of the block
plane = Plane(lego.faces().max())
# Create a grid of pips
for loc in GridLocations(lego_unit_size, lego_unit_size, pip_count, 2):
lego += (
plane
* loc
* Cylinder(
radius=pip_diameter / 2,
height=pip_height,
align=(Align.CENTER, Align.CENTER, Align.MIN),
)
)
if "show_object" in locals():
show_object(lego, name="lego")

17
examples/loft_algebra.py Normal file
View file

@ -0,0 +1,17 @@
from math import pi, sin
from alg123d import *
slice_count = 10
art = AlgCompound()
for i in range(slice_count + 1):
plane = Plane(origin=(0, 0, i * 3), z_dir=(0, 0, 1))
art += plane * Circle(10 * sin(i * pi / slice_count) + 5)
art = loft(art)
top_bottom = art.faces(GeomType.PLANE)
art = shell(art, openings=top_bottom, amount=0.5)
reset_show()
if "show_object" in locals():
show_object(art, name="art")

View file

@ -0,0 +1,8 @@
from alg123d import *
obj = Box(5, 5, 1)
for plane in [Plane(f) for f in obj.faces().filter_by(Axis.Z)]:
obj -= plane * Sphere(1.8)
if "show_object" in locals():
show_object(obj)

View file

@ -0,0 +1,28 @@
from alg123d import *
height, width, thickness, padding = 60, 80, 10, 12
screw_shaft_radius, screw_head_radius, screw_head_height = 1.5, 3, 3
bearing_axle_radius, bearing_radius, bearing_thickness = 4, 11, 7
# Build pillow block as an extruded sketch with counter bore holes
plan = Rectangle(width, height)
plan = fillet(plan, plan.vertices(), 5)
pillow_block = extrude(plan, thickness)
plane = Plane(pillow_block.faces().max())
pillow_block -= plane * CounterBore(
pillow_block, bearing_axle_radius, bearing_radius, bearing_thickness
)
for loc in GridLocations(width - 2 * padding, height - 2 * padding, 2, 2):
pillow_block -= (
plane
* loc
* CounterBore(
pillow_block, screw_shaft_radius, screw_head_radius, screw_head_height
)
)
# Render the part
if "show_object" in locals():
show_object(pillow_block)

View file

@ -0,0 +1,18 @@
from alg123d import *
powerup = Spline(
[(0, 0, 0), (50, 0, 50), (100, 0, 0)],
tangents=((1, 0, 0), (1, 0, 0)),
tangent_scalars=(0.5, 2),
)
corner = RadiusArc(powerup @ 1, (100, 60, 0), -30)
screw = Pos(75, 40, 15) * Helix(75, 150, 15, direction=(-1, 0, 0))
roller_coaster = powerup + corner + screw
roller_coaster += Spline((corner @ 1, screw @ 0), tangents=(corner % 1, screw % 0))
roller_coaster += Spline(
(screw @ 1, (-100, 30, 10), powerup @ 0), tangents=(screw % 1, powerup % 0)
)
if "show_object" in locals():
show_object(roller_coaster, clear=True)

30
examples/vase_algebra.py Normal file
View file

@ -0,0 +1,30 @@
from alg123d import *
l1 = Line((0, 0), (12, 0))
l2 = RadiusArc(l1 @ 1, (15, 20), 50)
l3 = Spline((l2 @ 1, (22, 40), (20, 50)), tangents=(l2 % 1, (-0.75, 1)))
l4 = RadiusArc(l3 @ 1, l3 @ 1 + Vector(0, 5), 5)
l5 = Spline(
(l4 @ 1, l4 @ 1 + Vector(2.5, 2.5), l4 @ 1 + Vector(0, 5)),
tangents=(l4 % 1, (-1, 0)),
)
outline = l1 + l2 + l3 + l4 + l5
outline += Polyline(
(
l5 @ 1,
l5 @ 1 + Vector(0, 1),
(0, (l5 @ 1).Y + 1),
l1 @ 0,
)
)
profile = make_face(outline)
vase = revolve(profile, axis=Axis.Y)
vase = shell(vase, openings=vase.faces().max(Axis.Y), amount=-1)
top_edges = vase.edges(GeomType.CIRCLE).filter_by_position(Axis.Y, 60, 62)
vase = fillet(vase, top_edges, radius=0.25)
vase = fillet(vase, vase.edges().sort_by(Axis.Y)[0], radius=0.5)
if "show_object" in locals():
show_object(vase, name="vase")