Skip to content

Commit 67e697a

Browse files
committed
Adding threads example
1 parent 5eb2db0 commit 67e697a

File tree

1 file changed

+153
-0
lines changed

1 file changed

+153
-0
lines changed

examples/threads.py

+153
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
"""
2+
3+
Parametric Threads Examples
4+
5+
name: threads.py
6+
by: Gumyr
7+
date: June 30th 2023
8+
9+
desc: A collection of different thread types.
10+
11+
license:
12+
13+
Copyright 2023 Gumyr
14+
15+
Licensed under the Apache License, Version 2.0 (the "License");
16+
you may not use this file except in compliance with the License.
17+
You may obtain a copy of the License at
18+
19+
http://www.apache.org/licenses/LICENSE-2.0
20+
21+
Unless required by applicable law or agreed to in writing, software
22+
distributed under the License is distributed on an "AS IS" BASIS,
23+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24+
See the License for the specific language governing permissions and
25+
limitations under the License.
26+
27+
"""
28+
29+
import timeit
30+
from build123d import *
31+
from bd_warehouse.thread import (
32+
Thread,
33+
AcmeThread,
34+
IsoThread,
35+
MetricTrapezoidalThread,
36+
PlasticBottleThread,
37+
)
38+
39+
# from ocp_vscode import show_object
40+
41+
42+
# Raw Thread internal
43+
starttime = timeit.default_timer()
44+
internal = Thread(
45+
apex_radius=5,
46+
apex_width=0.1,
47+
root_radius=6,
48+
root_width=0.8,
49+
pitch=1,
50+
length=12.001,
51+
apex_offset=0.2,
52+
end_finishes=["raw", "square"],
53+
)
54+
elapsed_time = timeit.default_timer() - starttime
55+
print(f"Internal Thread time: {elapsed_time:.3f}s")
56+
show_object(internal.locate(Location((-10, -10, 0))))
57+
58+
# Raw Thread external
59+
starttime = timeit.default_timer()
60+
external = Thread(
61+
apex_radius=6,
62+
apex_width=0.1,
63+
root_radius=5,
64+
root_width=0.8,
65+
pitch=1,
66+
length=8.0,
67+
apex_offset=0.2,
68+
end_finishes=["fade", "chamfer"],
69+
)
70+
elapsed_time = timeit.default_timer() - starttime
71+
print(f"External Thread time: {elapsed_time:.3f}s")
72+
show_object(external.locate(Location((10, -10, 0))))
73+
74+
# IsoThread internal in the form of a nut
75+
starttime = timeit.default_timer()
76+
iso_internal = IsoThread(
77+
major_diameter=6 * MM,
78+
pitch=1 * MM,
79+
length=8 * MM,
80+
external=False,
81+
end_finishes=("chamfer", "fade"),
82+
hand="right",
83+
)
84+
with BuildPart() as iso_internal_nut:
85+
with BuildSketch():
86+
RegularPolygon(iso_internal.major_diameter * 0.75, 6)
87+
Circle(iso_internal.major_diameter / 2, mode=Mode.SUBTRACT)
88+
extrude(amount=iso_internal.length)
89+
90+
nut = iso_internal.fuse(iso_internal_nut.part)
91+
elapsed_time = timeit.default_timer() - starttime
92+
print(f"Nut elapsed time: {elapsed_time:.3f}s")
93+
print(f"{nut.is_valid()=}")
94+
show_object(nut.locate(Location((-10, 10, 0))))
95+
96+
# IsoThread external in the form of a screw
97+
starttime = timeit.default_timer()
98+
iso_external = IsoThread(
99+
major_diameter=6 * MM,
100+
pitch=1 * MM,
101+
length=4.5 * MM,
102+
external=True,
103+
end_finishes=("square", "square"),
104+
hand="right",
105+
)
106+
external_core = Cylinder(
107+
iso_external.root_radius,
108+
iso_external.length,
109+
align=(Align.CENTER, Align.CENTER, Align.MIN),
110+
)
111+
iso_external_screw = iso_external.fuse(external_core)
112+
elapsed_time = timeit.default_timer() - starttime
113+
print(f"Iso External Screw elapsed time: {elapsed_time:.3f}s")
114+
show_object(iso_external_screw.locate(Location((10, 10, 0))))
115+
116+
# Acme Screw
117+
starttime = timeit.default_timer()
118+
acme = AcmeThread(size="1/4", length=1 * IN)
119+
show_object(acme.thread_profile)
120+
121+
acme_screw = acme + Cylinder(
122+
acme.root_radius, acme.length, align=(Align.CENTER, Align.CENTER, Align.MIN)
123+
)
124+
elapsed_time = timeit.default_timer() - starttime
125+
print(f"Acme external elapsed time: {elapsed_time:.3f}s")
126+
print(f"{acme_screw.is_valid()=}")
127+
show_object(acme_screw.locate(Location((20, 0, 0))))
128+
129+
# Metric Trapezoidal Thread
130+
starttime = timeit.default_timer()
131+
metric = MetricTrapezoidalThread(size="8x1.5", length=20 * MM)
132+
metric_screw = metric + Cylinder(
133+
metric.root_radius, metric.length, align=(Align.CENTER, Align.CENTER, Align.MIN)
134+
)
135+
136+
elapsed_time = timeit.default_timer() - starttime
137+
print(f"Metric external elapsed time: {elapsed_time:.3f}s")
138+
print(f"{metric_screw.is_valid()=}")
139+
show_object(metric_screw.locate(Location((-20, 0, 0))))
140+
141+
# Plastic Thread
142+
starttime = timeit.default_timer()
143+
plastic_external = PlasticBottleThread("M40SP444", external=True)
144+
plastic_internal = PlasticBottleThread("M38SP444", external=False)
145+
146+
elapsed_time = timeit.default_timer() - starttime
147+
print(f"Plastic elapsed time: {elapsed_time:.3f}s")
148+
print(f"{plastic_external.is_valid()=}")
149+
print(f"{plastic_internal.is_valid()=}")
150+
print(f"Plastic external & internal elapsed time: {elapsed_time:.3f}s")
151+
152+
show_object(plastic_internal.locate(Location((0, -40))))
153+
show_object(plastic_external.locate(Location((0, 40))))

0 commit comments

Comments
 (0)