mirror of
https://github.com/gumyr/build123d.git
synced 2025-12-06 02:30:55 -08:00
Rebuilt the Joint Tutorial
This commit is contained in:
parent
c3ead9db8c
commit
538f154c87
25 changed files with 2651 additions and 2549 deletions
|
|
@ -24,12 +24,10 @@ license:
|
|||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
"""
|
||||
# [import]
|
||||
from build123d import *
|
||||
|
||||
# SVG Export options
|
||||
svg_opts = {"pixel_scale": 5, "show_axes": False, "show_hidden": True}
|
||||
|
||||
|
||||
# [Hinge Class]
|
||||
class Hinge(Compound):
|
||||
"""Hinge
|
||||
|
||||
|
|
@ -86,10 +84,10 @@ class Hinge(Compound):
|
|||
height=pin_diameter,
|
||||
align=(Align.CENTER, Align.CENTER, Align.MIN),
|
||||
)
|
||||
Fillet(
|
||||
*pin_head.edges(Select.LAST).filter_by(GeomType.CIRCLE),
|
||||
radius=pin_diameter / 3,
|
||||
)
|
||||
Fillet(
|
||||
*pin_head.edges(Select.LAST).filter_by(GeomType.CIRCLE),
|
||||
radius=pin_diameter / 3,
|
||||
)
|
||||
|
||||
# Either the external and internal leaf with joints
|
||||
with BuildPart(Plane.XY, mode=Mode.PRIVATE) as leaf_builder:
|
||||
|
|
@ -128,8 +126,8 @@ class Hinge(Compound):
|
|||
if not inner:
|
||||
with Locations(pin_center.locations[0]):
|
||||
Add(pin.part)
|
||||
#
|
||||
# Create the Joints
|
||||
|
||||
# [Create the Joints]
|
||||
#
|
||||
# Leaf attachment
|
||||
RigidJoint(
|
||||
|
|
@ -139,7 +137,7 @@ class Hinge(Compound):
|
|||
(width - barrel_diameter, 0, length / 2), (90, 0, 0)
|
||||
),
|
||||
)
|
||||
# Hinge axis (fixed with inner)
|
||||
# [Hinge Axis] (fixed with inner)
|
||||
if inner:
|
||||
RigidJoint(
|
||||
"hinge_axis",
|
||||
|
|
@ -155,21 +153,23 @@ class Hinge(Compound):
|
|||
),
|
||||
angular_range=(90, 270),
|
||||
)
|
||||
# Fastener holes
|
||||
# [Fastener holes]
|
||||
hole_locations = [hole.location for hole in holes]
|
||||
for hole, hole_location in enumerate(hole_locations):
|
||||
CylindricalJoint(
|
||||
label="hole" + str(hole),
|
||||
to_part=leaf_builder.part,
|
||||
axis=hole_location.to_axis(),
|
||||
linear_range=(-2 * CM, 0),
|
||||
linear_range=(-2 * CM, 2 * CM),
|
||||
angular_range=(0, 360),
|
||||
)
|
||||
# [End Fastener holes]
|
||||
|
||||
super().__init__(leaf_builder.part.wrapped, joints=leaf_builder.part.joints)
|
||||
# [Hinge Class]
|
||||
|
||||
|
||||
# Create instances of the two leaves of the hinge
|
||||
# [Create instances of the two leaves of the hinge]
|
||||
hinge_inner = Hinge(
|
||||
width=5 * CM,
|
||||
length=12 * CM,
|
||||
|
|
@ -185,40 +185,17 @@ hinge_outer = Hinge(
|
|||
pin_diameter=4 * MM,
|
||||
inner=False,
|
||||
)
|
||||
Compound.make_compound(
|
||||
[
|
||||
hinge_inner,
|
||||
hinge_inner.joints["leaf"].symbol,
|
||||
hinge_inner.joints["hinge_axis"].symbol,
|
||||
hinge_inner.joints["hole0"].symbol,
|
||||
hinge_inner.joints["hole1"].symbol,
|
||||
hinge_inner.joints["hole2"].symbol,
|
||||
]
|
||||
).export_svg(
|
||||
"tutorial_joint_inner_leaf.svg", (100, 100, -50), (0, 0, 1), svg_opts=svg_opts
|
||||
)
|
||||
Compound.make_compound(
|
||||
[
|
||||
hinge_outer,
|
||||
hinge_outer.joints["leaf"].symbol,
|
||||
hinge_outer.joints["hinge_axis"].symbol,
|
||||
hinge_outer.joints["hole0"].symbol,
|
||||
hinge_outer.joints["hole1"].symbol,
|
||||
hinge_outer.joints["hole2"].symbol,
|
||||
]
|
||||
).export_svg(
|
||||
"tutorial_joint_outer_leaf.svg", (100, 100, -50), (0, 0, 1), svg_opts=svg_opts
|
||||
)
|
||||
|
||||
# Create the box with a RigidJoint to mount the hinge
|
||||
# [Create the box with a RigidJoint to mount the hinge]
|
||||
with BuildPart() as box_builder:
|
||||
Box(30 * CM, 30 * CM, 10 * CM)
|
||||
box = Box(30 * CM, 30 * CM, 10 * CM)
|
||||
Offset(amount=-1 * CM, openings=box_builder.faces().sort_by(Axis.Z)[-1])
|
||||
# Create a notch for the hinge
|
||||
with Locations((-15 * CM, 0, 5 * CM)):
|
||||
Box(2 * CM, 12 * CM, 4 * MM, mode=Mode.SUBTRACT)
|
||||
bbox = box.bounding_box()
|
||||
with Workplanes(
|
||||
box_builder.part.faces().sort_by(Axis.X)[0].located(Location((0, 0, 2 * CM)))
|
||||
Plane(origin=(bbox.min.X, 0, bbox.max.Z - 30 * MM), z_dir=(-1, 0, 0))
|
||||
):
|
||||
with GridLocations(0, 40 * MM, 1, 3):
|
||||
Hole(3 * MM, 1 * CM)
|
||||
|
|
@ -227,13 +204,11 @@ with BuildPart() as box_builder:
|
|||
box_builder.part,
|
||||
Location((-15 * CM, 0, 4 * CM), (180, 90, 0)),
|
||||
)
|
||||
# Demonstrate that objects with Joints can be moved and the joints follow
|
||||
box = box_builder.part.moved(Location((0, 0, 5 * CM)))
|
||||
Compound.make_compound([box, box.joints["hinge_attachment"].symbol]).export_svg(
|
||||
"tutorial_joint_box.svg", (-100, 100, 150), (0, 0, 1), svg_opts=svg_opts
|
||||
)
|
||||
|
||||
# The lid with a RigidJoint for the hinge
|
||||
# [Demonstrate that objects with Joints can be moved and the joints follow]
|
||||
box = box_builder.part.moved(Location((0, 0, 5 * CM)))
|
||||
|
||||
# [The lid with a RigidJoint for the hinge]
|
||||
with BuildPart() as lid_builder:
|
||||
Box(30 * CM, 30 * CM, 1 * CM, align=(Align.MIN, Align.CENTER, Align.MIN))
|
||||
with Locations((2 * CM, 0, 0)):
|
||||
|
|
@ -245,55 +220,110 @@ with BuildPart() as lid_builder:
|
|||
Location((0, 0, 0), (0, 0, 180)),
|
||||
)
|
||||
lid = lid_builder.part
|
||||
Compound.make_compound([lid, lid.joints["hinge_attachment"].symbol]).export_svg(
|
||||
"tutorial_joint_lid.svg", (-100, 100, 150), (0, 0, 1), svg_opts=svg_opts
|
||||
)
|
||||
|
||||
# A screw to attach the hinge to the box
|
||||
# [A screw to attach the hinge to the box]
|
||||
m6_screw = Compound.import_step("M6-1x12-countersunk-screw.step")
|
||||
m6_joint = RigidJoint("head", m6_screw, Location((0, 0, 0), (1, 0, 0), 180))
|
||||
Compound.make_compound([m6_screw, m6_joint.symbol]).export_svg(
|
||||
"tutorial_joint_m6_screw.svg",
|
||||
(-100, 100, 150),
|
||||
(0, 0, 1),
|
||||
svg_opts={"pixel_scale": 20, "show_axes": False, "show_hidden": False},
|
||||
)
|
||||
m6_joint = RigidJoint("head", m6_screw, Location((0, 0, 0), (0, 0, 0)))
|
||||
|
||||
# Connect the parts together
|
||||
# [Connect the parts together]
|
||||
box.joints["hinge_attachment"].connect_to(hinge_outer.joints["leaf"])
|
||||
hinge_outer.joints["hinge_axis"].connect_to(hinge_inner.joints["hinge_axis"], angle=120)
|
||||
hinge_inner.joints["leaf"].connect_to(lid.joints["hinge_attachment"])
|
||||
hinge_outer.joints["hole2"].connect_to(m6_joint, position=-5 * MM, angle=30)
|
||||
hinge_outer.joints["hole2"].connect_to(m6_joint, position=5 * MM, angle=30)
|
||||
|
||||
Compound.make_compound([box, hinge_outer]).export_svg(
|
||||
"tutorial_joint_box_outer.svg", (-100, -100, 50), (0, 0, 1), svg_opts=svg_opts
|
||||
)
|
||||
Compound.make_compound([box, hinge_outer, hinge_inner]).export_svg(
|
||||
"tutorial_joint_box_outer_inner.svg", (-100, -100, 50), (0, 0, 1), svg_opts=svg_opts
|
||||
)
|
||||
Compound.make_compound([box, hinge_outer, hinge_inner, lid]).export_svg(
|
||||
"tutorial_joint_box_outer_inner_lid.svg",
|
||||
(-100, -100, 50),
|
||||
(0, 0, 1),
|
||||
svg_opts=svg_opts,
|
||||
)
|
||||
Compound.make_compound([box, lid, hinge_inner, hinge_outer, m6_screw]).export_svg(
|
||||
"tutorial_joint.svg", (-100, -100, 50), (0, 0, 1), svg_opts=svg_opts
|
||||
)
|
||||
# [Add labels]
|
||||
box.label = "box"
|
||||
lid.label = "lid"
|
||||
hinge_outer.label = "outer hinge"
|
||||
hinge_inner.label = "inner hinge"
|
||||
m6_screw.label = "M6 screw"
|
||||
|
||||
# Create assembly and display it
|
||||
# [Create assembly]
|
||||
box_assembly = Compound(label="assembly", children=[box, lid, hinge_inner, hinge_outer])
|
||||
# [Display assembly]
|
||||
print(box_assembly.show_topology())
|
||||
|
||||
# Add to the assembly by assigning the parent attribute of an object
|
||||
# [Add to the assembly by assigning the parent attribute of an object]
|
||||
m6_screw.parent = box_assembly
|
||||
print(box_assembly.show_topology())
|
||||
|
||||
# [Check that the components in the assembly don't intersect]
|
||||
child_intersect, children, volume = box_assembly.do_children_intersect(
|
||||
include_parent=False
|
||||
)
|
||||
print(f"do children intersect: {child_intersect}")
|
||||
if child_intersect:
|
||||
print(f"{children} by {volume:0.3f} mm^3")
|
||||
|
||||
# [Export SVG files]
|
||||
#
|
||||
# SVG Export options
|
||||
svg_opts = {"pixel_scale": 5, "show_axes": False, "show_hidden": True}
|
||||
Compound.make_compound([box, box.joints["hinge_attachment"].symbol]).export_svg(
|
||||
"assets/tutorial_joint_box.svg", (-100, 100, 150), (0, 0, 1), svg_opts=svg_opts
|
||||
)
|
||||
Compound.make_compound(
|
||||
[
|
||||
hinge_inner,
|
||||
hinge_inner.joints["leaf"].symbol,
|
||||
hinge_inner.joints["hinge_axis"].symbol,
|
||||
hinge_inner.joints["hole0"].symbol,
|
||||
hinge_inner.joints["hole1"].symbol,
|
||||
hinge_inner.joints["hole2"].symbol,
|
||||
]
|
||||
).export_svg(
|
||||
"assets/tutorial_joint_inner_leaf.svg",
|
||||
(100, 100, -50),
|
||||
(0, 0, 1),
|
||||
svg_opts=svg_opts,
|
||||
)
|
||||
Compound.make_compound(
|
||||
[
|
||||
hinge_outer,
|
||||
hinge_outer.joints["leaf"].symbol,
|
||||
hinge_outer.joints["hinge_axis"].symbol,
|
||||
hinge_outer.joints["hole0"].symbol,
|
||||
hinge_outer.joints["hole1"].symbol,
|
||||
hinge_outer.joints["hole2"].symbol,
|
||||
]
|
||||
).export_svg(
|
||||
"assets/tutorial_joint_outer_leaf.svg",
|
||||
(100, 100, -50),
|
||||
(0, 0, 1),
|
||||
svg_opts=svg_opts,
|
||||
)
|
||||
Compound.make_compound([box, hinge_outer]).export_svg(
|
||||
"assets/tutorial_joint_box_outer.svg",
|
||||
(-100, -100, 50),
|
||||
(0, 0, 1),
|
||||
svg_opts=svg_opts,
|
||||
)
|
||||
Compound.make_compound([box, hinge_outer, hinge_inner]).export_svg(
|
||||
"assets/tutorial_joint_box_outer_inner.svg",
|
||||
(-100, -100, 50),
|
||||
(0, 0, 1),
|
||||
svg_opts=svg_opts,
|
||||
)
|
||||
Compound.make_compound([lid, lid.joints["hinge_attachment"].symbol]).export_svg(
|
||||
"assets/tutorial_joint_lid.svg", (-100, 100, 150), (0, 0, 1), svg_opts=svg_opts
|
||||
)
|
||||
Compound.make_compound([box, hinge_outer, hinge_inner, lid]).export_svg(
|
||||
"assets/tutorial_joint_box_outer_inner_lid.svg",
|
||||
(-100, -100, 50),
|
||||
(0, 0, 1),
|
||||
svg_opts=svg_opts,
|
||||
)
|
||||
Compound.make_compound([box, lid, hinge_inner, hinge_outer, m6_screw]).export_svg(
|
||||
"assets/tutorial_joint.svg", (-100, -100, 50), (0, 0, 1), svg_opts=svg_opts
|
||||
)
|
||||
Compound.make_compound([m6_screw, m6_joint.symbol]).export_svg(
|
||||
"assets/tutorial_joint_m6_screw.svg",
|
||||
(-100, 100, 150),
|
||||
(0, 0, 1),
|
||||
svg_opts={"pixel_scale": 20, "show_axes": False, "show_hidden": False},
|
||||
)
|
||||
|
||||
|
||||
if "show_object" in locals():
|
||||
show_object(box, name="box", options={"alpha": 0.8})
|
||||
# show_object(box.joints["hinge_attachment"].symbol, name="box attachment point")
|
||||
|
|
@ -305,15 +335,15 @@ if "show_object" in locals():
|
|||
show_object(hinge_inner, name="hinge_inner")
|
||||
# show_object(hinge_inner.joints["leaf"].symbol, name="hinge_inner leaf joint")
|
||||
# show_object(hinge_inner.joints["hinge_axis"].symbol, name="hinge_inner hinge axis")
|
||||
# for hole in [0, 1, 2]:
|
||||
# show_object(
|
||||
# hinge_inner.joints["hole" + str(hole)].symbol,
|
||||
# name="hinge_inner hole " + str(hole),
|
||||
# )
|
||||
# show_object(
|
||||
# hinge_outer.joints["hole" + str(hole)].symbol,
|
||||
# name="hinge_outer hole " + str(hole),
|
||||
# )
|
||||
for hole in [0, 1, 2]:
|
||||
show_object(
|
||||
hinge_inner.joints["hole" + str(hole)].symbol,
|
||||
name="hinge_inner hole " + str(hole),
|
||||
)
|
||||
show_object(
|
||||
hinge_outer.joints["hole" + str(hole)].symbol,
|
||||
name="hinge_outer hole " + str(hole),
|
||||
)
|
||||
show_object(m6_screw, name="m6 screw")
|
||||
# show_object(m6_joint.symbol, name="m6 screw symbol")
|
||||
show_object(m6_joint.symbol, name="m6 screw symbol")
|
||||
show_object(box_assembly, name="box assembly")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue