openswx 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- openswx/__init__.py +41 -0
- openswx/__main__.py +160 -0
- openswx/advanced.py +708 -0
- openswx/analysis_more.py +296 -0
- openswx/api.py +221 -0
- openswx/applog.py +77 -0
- openswx/assembly.py +1172 -0
- openswx/cache.py +168 -0
- openswx/cam.py +754 -0
- openswx/cfd.py +455 -0
- openswx/cli.py +124 -0
- openswx/commands_app.py +235 -0
- openswx/commands_asm.py +42 -0
- openswx/commands_assembly.py +476 -0
- openswx/commands_blocks.py +198 -0
- openswx/commands_document.py +758 -0
- openswx/commands_drawing.py +927 -0
- openswx/commands_features.py +875 -0
- openswx/commands_mfg.py +586 -0
- openswx/commands_more.py +1585 -0
- openswx/commands_review.py +655 -0
- openswx/commands_settings.py +44 -0
- openswx/commands_simreport.py +178 -0
- openswx/commands_simulation.py +414 -0
- openswx/commands_sketch.py +476 -0
- openswx/commands_std.py +123 -0
- openswx/commands_tol.py +127 -0
- openswx/desktop.py +243 -0
- openswx/document.py +957 -0
- openswx/drawing.py +535 -0
- openswx/drawing_export.py +274 -0
- openswx/drawing_more.py +314 -0
- openswx/drawing_sheets.py +1398 -0
- openswx/dxf_io.py +187 -0
- openswx/ecosystem.py +389 -0
- openswx/electrical.py +255 -0
- openswx/equations.py +162 -0
- openswx/evaluate.py +214 -0
- openswx/fcstd.py +103 -0
- openswx/fea.py +809 -0
- openswx/fea_more.py +272 -0
- openswx/fea_tet.py +909 -0
- openswx/features.py +2975 -0
- openswx/fileassoc.py +117 -0
- openswx/geom.py +222 -0
- openswx/hardware.py +174 -0
- openswx/inspection.py +291 -0
- openswx/isolated.py +103 -0
- openswx/jobs.py +101 -0
- openswx/materials.py +172 -0
- openswx/mcp_bridge.py +122 -0
- openswx/mcp_server.py +214 -0
- openswx/migrate.py +52 -0
- openswx/naming.py +272 -0
- openswx/parasolid.py +1264 -0
- openswx/parasolid_write.py +467 -0
- openswx/piping.py +230 -0
- openswx/plastics.py +318 -0
- openswx/plugins.py +54 -0
- openswx/recovery.py +205 -0
- openswx/render.py +165 -0
- openswx/security.py +177 -0
- openswx/session.py +1277 -0
- openswx/settings.py +95 -0
- openswx/sketch.py +1497 -0
- openswx/sldprt.py +510 -0
- openswx/snapshots.py +81 -0
- openswx/sw_drawing.py +261 -0
- openswx/sw_rebuild.py +2168 -0
- openswx/sw_sketch.py +436 -0
- openswx/sw_tree.py +147 -0
- openswx/tolerance.py +169 -0
- openswx/updates.py +52 -0
- openswx/web/app.js +2818 -0
- openswx/web/drawing.js +621 -0
- openswx/web/icons.js +214 -0
- openswx/web/index.html +91 -0
- openswx/web/mfg.js +231 -0
- openswx/web/more.js +1072 -0
- openswx/web/propman.js +990 -0
- openswx/web/sketcher.js +794 -0
- openswx/web/style.css +353 -0
- openswx/web/vendor/three-Pass.js +95 -0
- openswx/web/vendor/three-gpu-pathtracer.LICENSE +21 -0
- openswx/web/vendor/three-gpu-pathtracer.module.js +9361 -0
- openswx/web/vendor/three-mesh-bvh.LICENSE +21 -0
- openswx/web/vendor/three-mesh-bvh.module.js +8373 -0
- openswx/web/vendor/three.LICENSE +21 -0
- openswx/web/vendor/three.module.min.js +6 -0
- openswx/web/viewport.js +802 -0
- openswx/web_server.py +179 -0
- openswx-0.2.0.dist-info/METADATA +331 -0
- openswx-0.2.0.dist-info/RECORD +98 -0
- openswx-0.2.0.dist-info/WHEEL +5 -0
- openswx-0.2.0.dist-info/entry_points.txt +2 -0
- openswx-0.2.0.dist-info/licenses/LICENSE +202 -0
- openswx-0.2.0.dist-info/licenses/NOTICE +38 -0
- openswx-0.2.0.dist-info/top_level.txt +1 -0
openswx/advanced.py
ADDED
|
@@ -0,0 +1,708 @@
|
|
|
1
|
+
"""Advanced part features: Flex / Deform / Freeform, conic and curvature-continuous fillet profiles, mold tools
|
|
2
|
+
(parting line, parting surface, shut-off surfaces, tooling split), Insert Part / Mirror Part, cosmetic threads and
|
|
3
|
+
the remaining surface tools (ruled, radiate, extend, untrim, flatten). The builders follow features.py: each takes
|
|
4
|
+
(ctx, params, feature name) and changes the rebuild context; BUILDERS / NAME_PREFIX there pick them up."""
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
import math
|
|
8
|
+
import os
|
|
9
|
+
|
|
10
|
+
from build123d import Compound, Edge, Face, Location, Plane, Shape, Solid, Vector, Wire
|
|
11
|
+
|
|
12
|
+
from .features import Ctx, FeatureError, _add_surface, _compound, _material, _valid, apply_op, resolve_plane
|
|
13
|
+
from .geom import face_normal, geom_name, resolve_edge, resolve_face, vec
|
|
14
|
+
|
|
15
|
+
AXES = {"X": Vector(1, 0, 0), "Y": Vector(0, 1, 0), "Z": Vector(0, 0, 1)}
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _dir(spec) -> Vector:
|
|
19
|
+
if isinstance(spec, str):
|
|
20
|
+
key = spec.strip().upper().lstrip("+")
|
|
21
|
+
neg = key.startswith("-")
|
|
22
|
+
v = AXES.get(key.lstrip("-"))
|
|
23
|
+
if v is None:
|
|
24
|
+
raise FeatureError(f"unknown direction {spec!r}: X, Y, Z or a vector")
|
|
25
|
+
return -v if neg else v
|
|
26
|
+
v = vec(spec)
|
|
27
|
+
if v.length < 1e-12:
|
|
28
|
+
raise FeatureError("the direction is a zero vector")
|
|
29
|
+
return v.normalized()
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
# ====================================================================== Flex / Deform / Freeform
|
|
33
|
+
def map_shape(shape: Shape, fn, n: int = 20) -> Shape:
|
|
34
|
+
"""Deform a shape by moving the control points of its NURBS geometry through fn(x, y, z) -> (x, y, z).
|
|
35
|
+
The geometry is converted to NURBS, refined (degree >= 3, at least n poles per direction) so a nonlinear
|
|
36
|
+
map is followed closely, then edited in place: the parameter curves of the edges stay attached to their
|
|
37
|
+
faces (a copied surface would detach them). Affine maps are exact; others converge with n."""
|
|
38
|
+
from OCP.BRep import BRep_Builder, BRep_Tool
|
|
39
|
+
from OCP.BRepBuilderAPI import BRepBuilderAPI_NurbsConvert
|
|
40
|
+
from OCP.BRepLib import BRepLib
|
|
41
|
+
from OCP.gp import gp_Pnt
|
|
42
|
+
from OCP.ShapeFix import ShapeFix_Shape
|
|
43
|
+
from OCP.TopLoc import TopLoc_Location
|
|
44
|
+
from OCP.TopoDS import TopoDS
|
|
45
|
+
|
|
46
|
+
whole = Shape.cast(BRepBuilderAPI_NurbsConvert(shape.wrapped, True).Shape())
|
|
47
|
+
|
|
48
|
+
def moved(p, tr):
|
|
49
|
+
w = p.Transformed(tr)
|
|
50
|
+
return gp_Pnt(*fn(w.X(), w.Y(), w.Z())).Transformed(tr.Inverted())
|
|
51
|
+
|
|
52
|
+
for f in whole.faces():
|
|
53
|
+
loc = TopLoc_Location()
|
|
54
|
+
s = BRep_Tool.Surface_s(TopoDS.Face(f.wrapped), loc)
|
|
55
|
+
if not hasattr(s, "SetPole"):
|
|
56
|
+
raise FeatureError("a face could not be converted to NURBS")
|
|
57
|
+
if s.UDegree() < 3 or s.VDegree() < 3:
|
|
58
|
+
s.IncreaseDegree(max(3, s.UDegree()), max(3, s.VDegree()))
|
|
59
|
+
for along_u in (True, False):
|
|
60
|
+
k = s.NbUKnots() if along_u else s.NbVKnots()
|
|
61
|
+
lo, hi = (s.UKnot(1), s.UKnot(k)) if along_u else (s.VKnot(1), s.VKnot(k))
|
|
62
|
+
if (s.NbUPoles() if along_u else s.NbVPoles()) < n:
|
|
63
|
+
for i in range(1, n):
|
|
64
|
+
(s.InsertUKnot if along_u else s.InsertVKnot)(lo + (hi - lo) * i / n, 1, 1e-9, True)
|
|
65
|
+
tr = loc.Transformation()
|
|
66
|
+
for i in range(1, s.NbUPoles() + 1):
|
|
67
|
+
for j in range(1, s.NbVPoles() + 1):
|
|
68
|
+
s.SetPole(i, j, moved(s.Pole(i, j), tr))
|
|
69
|
+
for e in whole.edges():
|
|
70
|
+
loc = TopLoc_Location()
|
|
71
|
+
c = BRep_Tool.Curve_s(TopoDS.Edge(e.wrapped), loc, 0.0, 0.0)
|
|
72
|
+
if c is None or not hasattr(c, "SetPole"):
|
|
73
|
+
continue
|
|
74
|
+
if c.Degree() < 3:
|
|
75
|
+
c.IncreaseDegree(3)
|
|
76
|
+
lo, hi = c.FirstParameter(), c.LastParameter()
|
|
77
|
+
if c.NbPoles() < n:
|
|
78
|
+
for i in range(1, n):
|
|
79
|
+
c.InsertKnot(lo + (hi - lo) * i / n, 1, 1e-9, True)
|
|
80
|
+
tr = loc.Transformation()
|
|
81
|
+
for i in range(1, c.NbPoles() + 1):
|
|
82
|
+
c.SetPole(i, moved(c.Pole(i), tr))
|
|
83
|
+
b = BRep_Builder()
|
|
84
|
+
for v in whole.vertices():
|
|
85
|
+
vt = TopoDS.Vertex(v.wrapped)
|
|
86
|
+
p = BRep_Tool.Pnt_s(vt)
|
|
87
|
+
b.UpdateVertex(vt, gp_Pnt(*fn(p.X(), p.Y(), p.Z())), BRep_Tool.Tolerance_s(vt))
|
|
88
|
+
BRepLib.SameParameter_s(whole.wrapped, 1e-4, True)
|
|
89
|
+
fix = ShapeFix_Shape(whole.wrapped)
|
|
90
|
+
fix.Perform()
|
|
91
|
+
return Shape.cast(fix.Shape())
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def _set_body(ctx: Ctx, shape: Shape, what: str) -> None:
|
|
95
|
+
solids = [s for s in shape.solids() if s.is_valid]
|
|
96
|
+
if not solids:
|
|
97
|
+
raise FeatureError(f"{what} gave no valid solid (try smaller values)")
|
|
98
|
+
ctx.body = _compound(_material(solids))
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def build_flex(ctx: Ctx, p: dict, name: str) -> None:
|
|
102
|
+
"""Flex: bend, twist, taper or stretch the body along an axis (X / Y / Z or a vector) between two trim
|
|
103
|
+
positions measured along it (default: the whole body). Bend: angle (deg) toward bend_direction; twist:
|
|
104
|
+
angle (deg); taper: factor at the far end (1 = none); stretch: distance (mm)."""
|
|
105
|
+
if ctx.body is None:
|
|
106
|
+
raise FeatureError("no body to flex")
|
|
107
|
+
kind = p.get("type", "bend")
|
|
108
|
+
a = _dir(p.get("axis", "X"))
|
|
109
|
+
bdir = p.get("bend_direction")
|
|
110
|
+
b = _dir(bdir) if bdir is not None else (Vector(0, 1, 0) if abs(a.Y) < 0.9 else Vector(0, 0, 1))
|
|
111
|
+
b = (b - a * b.dot(a)).normalized()
|
|
112
|
+
c = a.cross(b)
|
|
113
|
+
bb = ctx.body.bounding_box()
|
|
114
|
+
corners = [Vector(x, y, z) for x in (bb.min.X, bb.max.X) for y in (bb.min.Y, bb.max.Y) for z in (bb.min.Z, bb.max.Z)]
|
|
115
|
+
ts = [q.dot(a) for q in corners]
|
|
116
|
+
t0 = float(p["start"]) if p.get("start") is not None else min(ts)
|
|
117
|
+
t1 = float(p["end"]) if p.get("end") is not None else max(ts)
|
|
118
|
+
if t1 - t0 < 1e-6:
|
|
119
|
+
raise FeatureError("the trim positions must enclose part of the body")
|
|
120
|
+
L, mid = t1 - t0, (bb.min + bb.max) * 0.5
|
|
121
|
+
hb, hc = mid.dot(b), mid.dot(c)
|
|
122
|
+
|
|
123
|
+
def split(x, y, z):
|
|
124
|
+
q = Vector(x, y, z)
|
|
125
|
+
return q.dot(a), q.dot(b) - hb, q.dot(c) - hc
|
|
126
|
+
|
|
127
|
+
def join(t, h, w):
|
|
128
|
+
q = a * t + b * (h + hb) + c * (w + hc)
|
|
129
|
+
return (q.X, q.Y, q.Z)
|
|
130
|
+
|
|
131
|
+
if kind == "twist":
|
|
132
|
+
ang = math.radians(float(p.get("angle", 90.0)))
|
|
133
|
+
|
|
134
|
+
def fn(x, y, z):
|
|
135
|
+
t, h, w = split(x, y, z)
|
|
136
|
+
th = ang * min(max((t - t0) / L, 0.0), 1.0)
|
|
137
|
+
return join(t, h * math.cos(th) - w * math.sin(th), h * math.sin(th) + w * math.cos(th))
|
|
138
|
+
elif kind == "taper":
|
|
139
|
+
k = float(p.get("factor", 0.5))
|
|
140
|
+
if k <= 0:
|
|
141
|
+
raise FeatureError("the taper factor must be positive")
|
|
142
|
+
|
|
143
|
+
def fn(x, y, z):
|
|
144
|
+
t, h, w = split(x, y, z)
|
|
145
|
+
s = 1 + (k - 1) * min(max((t - t0) / L, 0.0), 1.0)
|
|
146
|
+
return join(t, h * s, w * s)
|
|
147
|
+
elif kind == "stretch":
|
|
148
|
+
dist = float(p.get("distance", 10.0))
|
|
149
|
+
|
|
150
|
+
def fn(x, y, z):
|
|
151
|
+
t, h, w = split(x, y, z)
|
|
152
|
+
return join(t + dist * min(max((t - t0) / L, 0.0), 1.0), h, w)
|
|
153
|
+
elif kind == "bend":
|
|
154
|
+
phi = math.radians(float(p.get("angle", 90.0)))
|
|
155
|
+
if abs(phi) < 1e-9:
|
|
156
|
+
raise FeatureError("give a bend angle")
|
|
157
|
+
R = L / phi # the neutral axis (through the body centre) keeps its length
|
|
158
|
+
|
|
159
|
+
def fn(x, y, z):
|
|
160
|
+
t, h, w = split(x, y, z)
|
|
161
|
+
if t <= t0:
|
|
162
|
+
return join(t, h, w)
|
|
163
|
+
s = min(t - t0, L)
|
|
164
|
+
ang = s / R
|
|
165
|
+
nt, nh = t0 + (R - h) * math.sin(ang), R - (R - h) * math.cos(ang)
|
|
166
|
+
extra = t - t1
|
|
167
|
+
if extra > 0: # beyond the bend: straight on along the bent direction
|
|
168
|
+
nt, nh = nt + extra * math.cos(phi), nh + extra * math.sin(phi)
|
|
169
|
+
return join(nt, nh, w)
|
|
170
|
+
else:
|
|
171
|
+
raise FeatureError(f"unknown flex type {kind!r}: bend, twist, taper or stretch")
|
|
172
|
+
_set_body(ctx, map_shape(ctx.body, fn, int(p.get("resolution", 20))), "flex")
|
|
173
|
+
ctx.info[name] = {"type": kind, "start": t0, "end": t1}
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def _falloff(points: list[dict], radius: float):
|
|
177
|
+
pts = [(vec(q["point"]), vec(q["move"])) for q in points]
|
|
178
|
+
r2 = radius * radius
|
|
179
|
+
|
|
180
|
+
def fn(x, y, z):
|
|
181
|
+
q = Vector(x, y, z)
|
|
182
|
+
out = q
|
|
183
|
+
for c, m in pts:
|
|
184
|
+
d2 = (q - c).length ** 2
|
|
185
|
+
if d2 < r2:
|
|
186
|
+
w = (1 - d2 / r2) ** 2 # smooth: zero slope at the edge of the region
|
|
187
|
+
out = out + m * w
|
|
188
|
+
return (out.X, out.Y, out.Z)
|
|
189
|
+
return fn
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
def build_deform(ctx: Ctx, p: dict, name: str) -> None:
|
|
193
|
+
"""Deform (point): push the body at points ([{"point": [x, y, z], "move": [dx, dy, dz]}]) with a smooth
|
|
194
|
+
fall-off to zero at `radius` from each point."""
|
|
195
|
+
if ctx.body is None:
|
|
196
|
+
raise FeatureError("no body to deform")
|
|
197
|
+
pts = p.get("points") or []
|
|
198
|
+
if not pts:
|
|
199
|
+
raise FeatureError("give at least one point and its move")
|
|
200
|
+
r = float(p.get("radius", 20.0))
|
|
201
|
+
if r <= 0:
|
|
202
|
+
raise FeatureError("the deform radius must be positive")
|
|
203
|
+
_set_body(ctx, map_shape(ctx.body, _falloff(pts, r), int(p.get("resolution", 20))), "deform")
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def build_freeform(ctx: Ctx, p: dict, name: str) -> None:
|
|
207
|
+
"""Freeform: pull a face's interior along its normal by `distance` (positive outward); the push falls off
|
|
208
|
+
smoothly to the face's surroundings (radius: half the face's size unless given)."""
|
|
209
|
+
if ctx.body is None:
|
|
210
|
+
raise FeatureError("no body to edit")
|
|
211
|
+
face = resolve_face(ctx.body, p["face"])
|
|
212
|
+
from .geom import interior_point
|
|
213
|
+
c = interior_point(face)
|
|
214
|
+
n = face_normal(face, c)
|
|
215
|
+
bb = face.bounding_box()
|
|
216
|
+
r = float(p.get("radius") or max(bb.size.X, bb.size.Y, bb.size.Z) * 0.5)
|
|
217
|
+
pts = [{"point": list(c), "move": list(n * float(p.get("distance", 2.0)))}]
|
|
218
|
+
_set_body(ctx, map_shape(ctx.body, _falloff(pts, r), int(p.get("resolution", 20))), "freeform")
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
# ====================================================================== fillet profiles
|
|
222
|
+
def _faces_of(body: Shape, edge: Edge) -> list[Face]:
|
|
223
|
+
return [f for f in body.faces() if any(edge.is_same(e) for e in f.edges())]
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
def build_profile_fillet(ctx: Ctx, p: dict, name: str) -> None:
|
|
227
|
+
"""Fillet with a conic (rho) or curvature-continuous profile on straight edges between planar faces.
|
|
228
|
+
The profile meets each face `radius` from the edge; a conic with rho 0.5 is a parabola (circular is about
|
|
229
|
+
0.414), curvature continuous is a quintic that leaves both faces with zero curvature (G2)."""
|
|
230
|
+
if ctx.body is None:
|
|
231
|
+
raise FeatureError("no body to fillet")
|
|
232
|
+
r = float(p.get("radius", 5.0))
|
|
233
|
+
profile = p.get("profile", "conic")
|
|
234
|
+
rho = float(p.get("rho", 0.5))
|
|
235
|
+
if not 0.05 <= rho <= 0.95:
|
|
236
|
+
raise FeatureError("rho must be between 0.05 and 0.95")
|
|
237
|
+
tools_cut, tools_add = [], []
|
|
238
|
+
for ref in p.get("edges", []):
|
|
239
|
+
e = resolve_edge(ctx.body, ref)
|
|
240
|
+
if geom_name(e) != "line":
|
|
241
|
+
raise FeatureError("conic / curvature-continuous fillets need straight edges")
|
|
242
|
+
fs = _faces_of(ctx.body, e)
|
|
243
|
+
if len(fs) != 2 or any(geom_name(f) != "plane" for f in fs):
|
|
244
|
+
raise FeatureError("conic / curvature-continuous fillets need an edge between two planar faces")
|
|
245
|
+
A, B = e.position_at(0), e.position_at(1)
|
|
246
|
+
d = (B - A).normalized()
|
|
247
|
+
legs = []
|
|
248
|
+
for f in fs:
|
|
249
|
+
t = d.cross(f.normal_at()).normalized()
|
|
250
|
+
mid = A + (B - A) * 0.5
|
|
251
|
+
if f.distance_to(mid + t * min(r, 1e-2)) > 1e-6: # the leg must run into the face
|
|
252
|
+
t = -t
|
|
253
|
+
legs.append(t)
|
|
254
|
+
P1, P2, C = A + legs[0] * r, A + legs[1] * r, A
|
|
255
|
+
if profile == "conic": # a rational quadratic: weight rho / (1 - rho) on the corner
|
|
256
|
+
curve = Edge.make_bezier(P1, C, P2, weights=[1.0, rho / (1 - rho), 1.0])
|
|
257
|
+
elif profile in ("curvature_continuous", "g2"): # three collinear poles at each end: zero end curvature
|
|
258
|
+
curve = Edge.make_bezier(P1, P1 + (C - P1) * 0.4, P1 + (C - P1) * 0.75, P2 + (C - P2) * 0.75,
|
|
259
|
+
P2 + (C - P2) * 0.4, P2)
|
|
260
|
+
else:
|
|
261
|
+
raise FeatureError(f"unknown fillet profile {profile!r}: conic or curvature_continuous")
|
|
262
|
+
wire = Wire([curve, Edge.make_line(P2, C), Edge.make_line(C, P1)])
|
|
263
|
+
prism = Solid.extrude(Face(wire), B - A)
|
|
264
|
+
probe = C + (legs[0] + legs[1]) * (r * 0.05)
|
|
265
|
+
(tools_cut if ctx.body.is_inside(probe) else tools_add).append(prism)
|
|
266
|
+
if not tools_cut and not tools_add:
|
|
267
|
+
raise FeatureError("select straight edges to fillet")
|
|
268
|
+
for t in tools_cut:
|
|
269
|
+
apply_op(ctx, "cut", t)
|
|
270
|
+
for t in tools_add:
|
|
271
|
+
apply_op(ctx, "add", t)
|
|
272
|
+
ctx.tools[name] = [("cut", t) for t in tools_cut] + [("add", t) for t in tools_add]
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
# ====================================================================== mold tools
|
|
276
|
+
def _parting_edges(body: Shape, d: Vector) -> list[Edge]:
|
|
277
|
+
"""Edges where the part turns from faces that see the pull direction (and walls along it) to faces
|
|
278
|
+
that face away: the parting line for a two-plate mold."""
|
|
279
|
+
faces = body.faces()
|
|
280
|
+
side = [1 if face_normal(f, f.center()).dot(d) > -1e-6 else -1 for f in faces]
|
|
281
|
+
out = []
|
|
282
|
+
for e in body.edges():
|
|
283
|
+
adj = [i for i, f in enumerate(faces) if any(e.is_same(x) for x in f.edges())]
|
|
284
|
+
if len(adj) == 2 and side[adj[0]] != side[adj[1]]:
|
|
285
|
+
out.append(e)
|
|
286
|
+
return out
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
def _parting(ctx: Ctx, p: dict) -> tuple[Vector, float, list[Edge]]:
|
|
290
|
+
if ctx.body is None:
|
|
291
|
+
raise FeatureError("no part body for mold tools")
|
|
292
|
+
d = _dir(p.get("direction", "Y"))
|
|
293
|
+
edges = _parting_edges(ctx.body, d)
|
|
294
|
+
if p.get("level") is not None:
|
|
295
|
+
return d, float(p["level"]), edges
|
|
296
|
+
if not edges:
|
|
297
|
+
raise FeatureError("no parting line found for that pull direction")
|
|
298
|
+
hs = [e.position_at(t).dot(d) for e in edges for t in (0.0, 0.5, 1.0)]
|
|
299
|
+
if max(hs) - min(hs) > 1e-4:
|
|
300
|
+
raise FeatureError("the parting line is not planar for this pull direction: give the parting `level`")
|
|
301
|
+
return d, sum(hs) / len(hs), edges
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
def build_parting_line(ctx: Ctx, p: dict, name: str) -> None:
|
|
305
|
+
"""Mold Tools > Parting Line: the edges where the part divides between the two mold halves for the pull
|
|
306
|
+
direction (shown as a curve)."""
|
|
307
|
+
d, level, edges = _parting(ctx, p)
|
|
308
|
+
if not edges:
|
|
309
|
+
raise FeatureError("no parting line found for that pull direction")
|
|
310
|
+
ctx.curves[name] = edges
|
|
311
|
+
ctx.info[name] = {"direction": list(d), "level": level, "edges": len(edges), "length": sum(e.length for e in edges)}
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
def _section_faces(body: Shape, d: Vector, level: float) -> list[Face]:
|
|
315
|
+
"""The part's cross-section at the parting level as faces (largest first)."""
|
|
316
|
+
from build123d import Keep
|
|
317
|
+
try:
|
|
318
|
+
half = body.split(Plane(d * level, z_dir=d), Keep.TOP)
|
|
319
|
+
except Exception:
|
|
320
|
+
return []
|
|
321
|
+
half = Compound(half) if isinstance(half, (list, tuple)) else half
|
|
322
|
+
faces = []
|
|
323
|
+
for f in half.faces():
|
|
324
|
+
if geom_name(f) == "plane" and abs(abs(f.normal_at().dot(d)) - 1) < 1e-9 and abs(f.center().dot(d) - level) < 1e-6:
|
|
325
|
+
faces.append(f)
|
|
326
|
+
return sorted(faces, key=lambda f: -f.area)
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
def build_parting_surface(ctx: Ctx, p: dict, name: str) -> None:
|
|
330
|
+
"""Mold Tools > Parting Surface: a flat surface on the parting plane from the part's outline out to
|
|
331
|
+
`margin` beyond it."""
|
|
332
|
+
d, level, _ = _parting(ctx, p)
|
|
333
|
+
margin = float(p.get("margin", 10.0))
|
|
334
|
+
bb = ctx.body.bounding_box()
|
|
335
|
+
size = max(bb.size.X, bb.size.Y, bb.size.Z) + 2 * margin
|
|
336
|
+
c = (bb.min + bb.max) * 0.5
|
|
337
|
+
c = c - d * (c.dot(d) - level)
|
|
338
|
+
outer = Face.make_rect(size, size, Plane(c, z_dir=d))
|
|
339
|
+
sec = _section_faces(ctx.body, d, level)
|
|
340
|
+
surf = outer
|
|
341
|
+
for f in sec:
|
|
342
|
+
surf = surf.cut(Face(f.outer_wire()))
|
|
343
|
+
_add_surface(ctx, name, surf)
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
def build_shut_off_surfaces(ctx: Ctx, p: dict, name: str) -> None:
|
|
347
|
+
"""Mold Tools > Shut-off Surfaces: patches closing the openings that run through the part along the pull,
|
|
348
|
+
so core and cavity seal against each other (the holes in the part's section at the parting level)."""
|
|
349
|
+
d, level, _ = _parting(ctx, p)
|
|
350
|
+
holes = [Face(w) for f in _section_faces(ctx.body, d, level) for w in f.inner_wires()]
|
|
351
|
+
if not holes:
|
|
352
|
+
raise FeatureError("the part has no openings to shut off at the parting level")
|
|
353
|
+
_add_surface(ctx, name, holes[0] if len(holes) == 1 else Compound(holes))
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
def build_tooling_split(ctx: Ctx, p: dict, name: str) -> None:
|
|
357
|
+
"""Mold Tools > Tooling Split: a mold block `margin` around the part, split at the parting level into the
|
|
358
|
+
cavity (the side the pull points to) and the core, each minus the part. Bodies: part (unless keep_part is
|
|
359
|
+
false), core, cavity. Openings through the part are shut off at the parting plane."""
|
|
360
|
+
from build123d import Box, Keep
|
|
361
|
+
d, level, _ = _parting(ctx, p)
|
|
362
|
+
margin = float(p.get("margin", 10.0))
|
|
363
|
+
part = ctx.body
|
|
364
|
+
bb = part.bounding_box()
|
|
365
|
+
lo, hi = bb.min - Vector(margin, margin, margin), bb.max + Vector(margin, margin, margin)
|
|
366
|
+
size = hi - lo
|
|
367
|
+
block = Box(size.X, size.Y, size.Z).moved(Location((lo + hi) * 0.5))
|
|
368
|
+
pl = Plane(d * level, z_dir=d)
|
|
369
|
+
one = lambda x: Compound(list(x)) if isinstance(x, (list, tuple)) else x # noqa: E731
|
|
370
|
+
cavity = _valid(one(block.split(pl, Keep.TOP)).cut(part))
|
|
371
|
+
core = _valid(one(block.split(pl, Keep.BOTTOM)).cut(part))
|
|
372
|
+
if not cavity.solids() or not core.solids():
|
|
373
|
+
raise FeatureError("the parting plane does not cut the mold block")
|
|
374
|
+
bodies = (list(part.solids()) if p.get("keep_part", True) else []) + core.solids() + cavity.solids()
|
|
375
|
+
ctx.body = _compound(bodies)
|
|
376
|
+
ctx.info[name] = {"level": level, "direction": list(d), "core_volume": core.volume, "cavity_volume": cavity.volume}
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
# ====================================================================== Insert Part / Mirror Part
|
|
380
|
+
_PART_CACHE: dict = {}
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
def _load_part_body(path: str):
|
|
384
|
+
if not os.path.exists(path):
|
|
385
|
+
raise FeatureError(f"part file not found: {path}")
|
|
386
|
+
key = (os.path.abspath(path), os.path.getmtime(path))
|
|
387
|
+
if key in _PART_CACHE:
|
|
388
|
+
return _PART_CACHE[key]
|
|
389
|
+
ext = os.path.splitext(path)[1].lower()
|
|
390
|
+
if ext == ".oswprt":
|
|
391
|
+
from .document import PartDocument
|
|
392
|
+
body = PartDocument.open(path).body
|
|
393
|
+
elif ext in (".step", ".stp"):
|
|
394
|
+
from build123d import import_step
|
|
395
|
+
body = import_step(path)
|
|
396
|
+
elif ext in (".x_t", ".x_b"):
|
|
397
|
+
from .parasolid import read_xt_shape
|
|
398
|
+
body = read_xt_shape(path)
|
|
399
|
+
elif ext == ".sldprt":
|
|
400
|
+
from .sldprt import read_sldprt
|
|
401
|
+
body = read_sldprt(path)
|
|
402
|
+
else:
|
|
403
|
+
raise FeatureError(f"cannot insert a {ext} file as a part")
|
|
404
|
+
if body is None or not body.solids():
|
|
405
|
+
raise FeatureError(f"{os.path.basename(path)} has no solid body")
|
|
406
|
+
_PART_CACHE[key] = body
|
|
407
|
+
return body
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
def build_insert_part(ctx: Ctx, p: dict, name: str) -> None:
|
|
411
|
+
"""Insert Part (a derived part): another part file's body, placed by position / rotation (degrees about
|
|
412
|
+
X, Y, Z) and optionally mirrored about a plane (Mirror Part); it follows changes to that file on rebuild."""
|
|
413
|
+
shape = _load_part_body(p["path"])
|
|
414
|
+
if p.get("mirror"):
|
|
415
|
+
shape = shape.mirror(resolve_plane(ctx, p["mirror"]))
|
|
416
|
+
rx, ry, rz = (float(v) for v in (p.get("rotation") or [0, 0, 0]))
|
|
417
|
+
shape = shape.moved(Location(tuple(float(v) for v in (p.get("position") or [0, 0, 0])), (rx, ry, rz)))
|
|
418
|
+
apply_op(ctx, "cut" if p.get("op") == "cut" else "add", Compound(shape.solids()), p.get("merge", True))
|
|
419
|
+
ctx.info[name] = {"source": p["path"], "volume": shape.volume}
|
|
420
|
+
|
|
421
|
+
|
|
422
|
+
# ====================================================================== cosmetic thread
|
|
423
|
+
def build_cosmetic_thread(ctx: Ctx, p: dict, name: str) -> None:
|
|
424
|
+
"""Cosmetic Thread on a cylindrical face (hole or shaft): records the thread (label, diameters, length) for
|
|
425
|
+
drawings and shows the thread diameter as circles at its ends; the geometry is not changed."""
|
|
426
|
+
if ctx.body is None:
|
|
427
|
+
raise FeatureError("no body for a cosmetic thread")
|
|
428
|
+
face = resolve_face(ctx.body, p["face"])
|
|
429
|
+
if geom_name(face) != "cylinder":
|
|
430
|
+
raise FeatureError("a cosmetic thread goes on a cylindrical face")
|
|
431
|
+
from OCP.BRepAdaptor import BRepAdaptor_Surface
|
|
432
|
+
cyl = BRepAdaptor_Surface(face.wrapped).Cylinder()
|
|
433
|
+
ax = cyl.Axis()
|
|
434
|
+
o = Vector(ax.Location().X(), ax.Location().Y(), ax.Location().Z())
|
|
435
|
+
a = Vector(ax.Direction().X(), ax.Direction().Y(), ax.Direction().Z())
|
|
436
|
+
r = cyl.Radius()
|
|
437
|
+
from .geom import interior_point
|
|
438
|
+
q = interior_point(face)
|
|
439
|
+
radial = (q - (o + a * (q - o).dot(a))).normalized()
|
|
440
|
+
internal = not ctx.body.is_inside(q - radial * min(r * 0.5, 0.5)) # (a hole: just inside the face is air)
|
|
441
|
+
ts = [(v - o).dot(a) for e in face.edges() for v in (e.position_at(0), e.position_at(1))]
|
|
442
|
+
t0, t1 = min(ts), max(ts)
|
|
443
|
+
length = float(p.get("length") or (t1 - t0))
|
|
444
|
+
label = p.get("label") or (f"M{round(2 * r / 0.85):g}" if internal else f"M{2 * r:g}")
|
|
445
|
+
thread_r = r / 0.85 if internal else r * 0.85 # internal: the major diameter; external: the minor
|
|
446
|
+
ctx.curves[name] = [Edge.make_circle(thread_r, Plane(o + a * t, z_dir=a)) for t in (t0, t0 + length)]
|
|
447
|
+
ctx.info[name] = {"cosmetic_thread": {"label": label, "internal": internal, "diameter": 2 * r,
|
|
448
|
+
"thread_diameter": 2 * thread_r, "length": length,
|
|
449
|
+
"axis_origin": list(o + a * t0), "axis": list(a)}}
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
# ====================================================================== surface tools
|
|
453
|
+
def _ruled_from(edges, dir_fn, distance):
|
|
454
|
+
from OCP.BRepFill import BRepFill
|
|
455
|
+
faces = []
|
|
456
|
+
for e in edges:
|
|
457
|
+
n = 2 if geom_name(e) == "line" else 24
|
|
458
|
+
pts = [e.position_at(i / (n - 1)) for i in range(n)]
|
|
459
|
+
moved = [q + dir_fn(e, i / (n - 1)) * distance for i, q in enumerate(pts)]
|
|
460
|
+
e2 = Edge.make_line(moved[0], moved[-1]) if n == 2 else Edge.make_spline(moved)
|
|
461
|
+
faces.append(Face(BRepFill.Face_s(e.wrapped, e2.wrapped)))
|
|
462
|
+
return faces[0] if len(faces) == 1 else Compound(faces)
|
|
463
|
+
|
|
464
|
+
|
|
465
|
+
def _model_edges(ctx: Ctx, refs):
|
|
466
|
+
if ctx.body is None:
|
|
467
|
+
raise FeatureError("no body to take edges from")
|
|
468
|
+
es = [resolve_edge(ctx.body, r) for r in refs or []]
|
|
469
|
+
if not es:
|
|
470
|
+
raise FeatureError("select edges")
|
|
471
|
+
return es
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
def build_ruled_surface(ctx: Ctx, p: dict, name: str) -> None:
|
|
475
|
+
"""Surfaces > Ruled Surface from model edges: `type` direction (along a vector), normal (along the
|
|
476
|
+
adjacent face's normal) or tangent (continuing the adjacent face outward), `distance` mm."""
|
|
477
|
+
es = _model_edges(ctx, p.get("edges"))
|
|
478
|
+
kind = p.get("type", "normal")
|
|
479
|
+
body = ctx.body
|
|
480
|
+
if kind == "direction":
|
|
481
|
+
dv = _dir(p.get("direction", "Y"))
|
|
482
|
+
fn = lambda e, t: dv # noqa: E731
|
|
483
|
+
else:
|
|
484
|
+
def fn(e, t):
|
|
485
|
+
f = _faces_of(body, e)[0]
|
|
486
|
+
q = e.position_at(t)
|
|
487
|
+
n = face_normal(f, q)
|
|
488
|
+
if kind == "normal":
|
|
489
|
+
return n
|
|
490
|
+
out = e.tangent_at(t).cross(n).normalized()
|
|
491
|
+
return out if f.distance_to(q - out * 1e-3) < 1e-6 else -out # away from the face
|
|
492
|
+
_add_surface(ctx, name, _ruled_from(es, fn, float(p.get("distance", 10.0))))
|
|
493
|
+
|
|
494
|
+
|
|
495
|
+
def build_radiate_surface(ctx: Ctx, p: dict, name: str) -> None:
|
|
496
|
+
"""Surfaces > Radiate Surface: from model edges outward in a plane parallel to the reference `plane`,
|
|
497
|
+
perpendicular to the edges, `distance` mm."""
|
|
498
|
+
es = _model_edges(ctx, p.get("edges"))
|
|
499
|
+
n = resolve_plane(ctx, p.get("plane", "Top")).z_dir
|
|
500
|
+
body = ctx.body
|
|
501
|
+
|
|
502
|
+
def fn(e, t):
|
|
503
|
+
q = e.position_at(t)
|
|
504
|
+
out = e.tangent_at(t).cross(n).normalized()
|
|
505
|
+
return out if not body.is_inside(q + out * 1e-2) else -out
|
|
506
|
+
_add_surface(ctx, name, _ruled_from(es, fn, float(p.get("distance", 10.0))))
|
|
507
|
+
|
|
508
|
+
|
|
509
|
+
def _surface_source(ctx: Ctx, p: dict):
|
|
510
|
+
if p.get("surface"):
|
|
511
|
+
s = ctx.surfaces.get(p["surface"])
|
|
512
|
+
if s is None:
|
|
513
|
+
raise FeatureError(f"no surface body named {p['surface']!r}")
|
|
514
|
+
return s.faces()
|
|
515
|
+
if ctx.body is None:
|
|
516
|
+
raise FeatureError("no faces to work on")
|
|
517
|
+
fs = [resolve_face(ctx.body, f) for f in p.get("faces", [])]
|
|
518
|
+
if not fs:
|
|
519
|
+
raise FeatureError("give a surface body or faces")
|
|
520
|
+
return fs
|
|
521
|
+
|
|
522
|
+
|
|
523
|
+
def _uv_bounds(face):
|
|
524
|
+
from OCP.BRepTools import BRepTools
|
|
525
|
+
return BRepTools.UVBounds_s(face.wrapped)
|
|
526
|
+
|
|
527
|
+
|
|
528
|
+
def _make_face(surf, u0, u1, v0, v1):
|
|
529
|
+
from OCP.BRepBuilderAPI import BRepBuilderAPI_MakeFace
|
|
530
|
+
mk = BRepBuilderAPI_MakeFace(surf, u0, u1, v0, v1, 1e-6)
|
|
531
|
+
if not mk.IsDone():
|
|
532
|
+
raise FeatureError("the face could not be rebuilt on its surface")
|
|
533
|
+
return Face(mk.Face())
|
|
534
|
+
|
|
535
|
+
|
|
536
|
+
def build_extend_surface(ctx: Ctx, p: dict, name: str) -> None:
|
|
537
|
+
"""Surfaces > Extend Surface: grow faces by `distance` mm past their edges on the same underlying surface
|
|
538
|
+
(B-spline surfaces are extended smoothly). The result is a new surface body."""
|
|
539
|
+
from OCP.BRep import BRep_Tool
|
|
540
|
+
from OCP.GeomConvert import GeomConvert
|
|
541
|
+
from OCP.GeomLib import GeomLib
|
|
542
|
+
dist = float(p.get("distance", 10.0))
|
|
543
|
+
out = []
|
|
544
|
+
for f in _surface_source(ctx, p):
|
|
545
|
+
surf = BRep_Tool.Surface_s(f.wrapped)
|
|
546
|
+
u0, u1, v0, v1 = _uv_bounds(f)
|
|
547
|
+
kind = geom_name(f)
|
|
548
|
+
if kind in ("plane", "cylinder", "cone"):
|
|
549
|
+
um, vm = (u0 + u1) / 2, (v0 + v1) / 2
|
|
550
|
+
lu = surf.Value(u0, vm).Distance(surf.Value(u1, vm)) or 1.0
|
|
551
|
+
lv = surf.Value(um, v0).Distance(surf.Value(um, v1)) or 1.0
|
|
552
|
+
du, dv = (u1 - u0) * dist / lu, (v1 - v0) * dist / lv
|
|
553
|
+
if kind != "plane": # u is an angle there: never wrap past a full turn
|
|
554
|
+
du = 0.0 if u1 - u0 > 2 * math.pi - 1e-6 else min(du, (2 * math.pi - (u1 - u0)) / 2)
|
|
555
|
+
out.append(_make_face(surf, u0 - du, u1 + du, v0 - dv, v1 + dv))
|
|
556
|
+
else:
|
|
557
|
+
from OCP.Geom import Geom_RectangularTrimmedSurface
|
|
558
|
+
bs = GeomConvert.SurfaceToBSplineSurface_s(Geom_RectangularTrimmedSurface(surf, u0, u1, v0, v1))
|
|
559
|
+
for in_u in (True, False):
|
|
560
|
+
for after in (True, False):
|
|
561
|
+
GeomLib.ExtendSurfByLength_s(bs, dist, 1, in_u, after)
|
|
562
|
+
ub0, ub1, vb0, vb1 = bs.Bounds()
|
|
563
|
+
out.append(_make_face(bs, ub0, ub1, vb0, vb1))
|
|
564
|
+
_add_surface(ctx, name, out[0] if len(out) == 1 else Compound(out))
|
|
565
|
+
|
|
566
|
+
|
|
567
|
+
def build_untrim_surface(ctx: Ctx, p: dict, name: str) -> None:
|
|
568
|
+
"""Surfaces > Untrim Surface: `mode` internal fills the holes of each face; natural returns the face to the
|
|
569
|
+
natural bounds of its surface (its parameter range; infinite surfaces keep the face's own range)."""
|
|
570
|
+
from OCP.BRep import BRep_Tool
|
|
571
|
+
mode = p.get("mode", "internal")
|
|
572
|
+
out = []
|
|
573
|
+
for f in _surface_source(ctx, p):
|
|
574
|
+
if mode == "internal":
|
|
575
|
+
out.append(Face(f.outer_wire()))
|
|
576
|
+
continue
|
|
577
|
+
surf = BRep_Tool.Surface_s(f.wrapped)
|
|
578
|
+
u0, u1, v0, v1 = surf.Bounds()
|
|
579
|
+
fu0, fu1, fv0, fv1 = _uv_bounds(f)
|
|
580
|
+
big = 1e50
|
|
581
|
+
out.append(_make_face(surf, u0 if abs(u0) < big else fu0, u1 if abs(u1) < big else fu1,
|
|
582
|
+
v0 if abs(v0) < big else fv0, v1 if abs(v1) < big else fv1))
|
|
583
|
+
_add_surface(ctx, name, out[0] if len(out) == 1 else Compound(out))
|
|
584
|
+
|
|
585
|
+
|
|
586
|
+
def build_flatten_surface(ctx: Ctx, p: dict, name: str) -> None:
|
|
587
|
+
"""Surfaces > Flatten: unroll developable faces (planar, cylindrical, conical) into the plane `plane`
|
|
588
|
+
(default Top) at true size: the flat face keeps the face's area."""
|
|
589
|
+
from OCP.BRepAdaptor import BRepAdaptor_Surface
|
|
590
|
+
pl = resolve_plane(ctx, p.get("plane", "Top"))
|
|
591
|
+
out = []
|
|
592
|
+
for f in _surface_source(ctx, p):
|
|
593
|
+
kind = geom_name(f)
|
|
594
|
+
ad = BRepAdaptor_Surface(f.wrapped)
|
|
595
|
+
if kind == "plane":
|
|
596
|
+
flat = lambda u, v: (u, v) # noqa: E731
|
|
597
|
+
elif kind == "cylinder":
|
|
598
|
+
R = ad.Cylinder().Radius()
|
|
599
|
+
flat = lambda u, v, R=R: (u * R, v) # noqa: E731
|
|
600
|
+
elif kind == "cone":
|
|
601
|
+
cone = ad.Cone()
|
|
602
|
+
sa = cone.SemiAngle()
|
|
603
|
+
Lr = cone.RefRadius() / math.sin(sa) if abs(math.sin(sa)) > 1e-12 else 1e9 # slant length to the apex
|
|
604
|
+
|
|
605
|
+
def flat(u, v, Lr=Lr, sa=sa):
|
|
606
|
+
rad, ang = Lr + v, u * math.sin(sa)
|
|
607
|
+
return (rad * math.cos(ang), rad * math.sin(ang))
|
|
608
|
+
else:
|
|
609
|
+
raise FeatureError(f"{kind} faces are not developable: flatten works on planar, cylindrical and conical faces")
|
|
610
|
+
wires = []
|
|
611
|
+
for w in [f.outer_wire(), *f.inner_wires()]:
|
|
612
|
+
pts = [pl.from_local_coords((*flat(u, v), 0)) for u, v in _wire_uv(f, w)]
|
|
613
|
+
wires.append(Wire.make_polygon(_dedupe(pts), close=True))
|
|
614
|
+
out.append(Face(wires[0], wires[1:]))
|
|
615
|
+
_add_surface(ctx, name, out[0] if len(out) == 1 else Compound(out))
|
|
616
|
+
|
|
617
|
+
|
|
618
|
+
def _wire_uv(face, wire, k: int = 32):
|
|
619
|
+
"""The wire's points in the face's (u, v) parameters, in order, from the edges' own parameter curves (so
|
|
620
|
+
seams of closed surfaces come out right, which projecting points cannot do)."""
|
|
621
|
+
from OCP.BRepAdaptor import BRepAdaptor_Curve2d
|
|
622
|
+
from OCP.BRepTools import BRepTools_WireExplorer
|
|
623
|
+
from OCP.TopAbs import TopAbs_REVERSED
|
|
624
|
+
out = []
|
|
625
|
+
ex = BRepTools_WireExplorer(wire.wrapped, face.wrapped)
|
|
626
|
+
while ex.More():
|
|
627
|
+
e = ex.Current()
|
|
628
|
+
c2 = BRepAdaptor_Curve2d(e, face.wrapped)
|
|
629
|
+
a, b = c2.FirstParameter(), c2.LastParameter()
|
|
630
|
+
n = 2 if c2.GetType().name == "GeomAbs_Line" else k
|
|
631
|
+
ts = [a + (b - a) * i / (n - 1) for i in range(n)]
|
|
632
|
+
if e.Orientation() == TopAbs_REVERSED:
|
|
633
|
+
ts.reverse()
|
|
634
|
+
for t in ts:
|
|
635
|
+
q = c2.Value(t)
|
|
636
|
+
out.append((q.X(), q.Y()))
|
|
637
|
+
ex.Next()
|
|
638
|
+
return out
|
|
639
|
+
|
|
640
|
+
|
|
641
|
+
def _dedupe(pts):
|
|
642
|
+
out = []
|
|
643
|
+
for q in pts:
|
|
644
|
+
if not out or (q - out[-1]).length > 1e-6:
|
|
645
|
+
out.append(q)
|
|
646
|
+
if len(out) > 1 and (out[0] - out[-1]).length < 1e-6:
|
|
647
|
+
out.pop()
|
|
648
|
+
return out
|
|
649
|
+
|
|
650
|
+
|
|
651
|
+
def build_filled_surface_edges(ctx: Ctx, p: dict, name: str) -> None:
|
|
652
|
+
"""Filled Surface from model edges (a hole or gap in the body): `continuity` contact or tangent (G1 to
|
|
653
|
+
the faces next to each edge), optionally pulled through constraint points."""
|
|
654
|
+
from OCP.BRepFill import BRepFill_Filling
|
|
655
|
+
from OCP.GeomAbs import GeomAbs_C0, GeomAbs_G1
|
|
656
|
+
from OCP.gp import gp_Pnt
|
|
657
|
+
es = _model_edges(ctx, p.get("edges"))
|
|
658
|
+
tangent = p.get("continuity", "tangent") == "tangent"
|
|
659
|
+
fill = BRepFill_Filling()
|
|
660
|
+
for e in es:
|
|
661
|
+
fs = _faces_of(ctx.body, e)
|
|
662
|
+
if tangent and fs:
|
|
663
|
+
fill.Add(e.wrapped, fs[0].wrapped, GeomAbs_G1, True)
|
|
664
|
+
else:
|
|
665
|
+
fill.Add(e.wrapped, GeomAbs_C0, True)
|
|
666
|
+
for q in p.get("points") or []:
|
|
667
|
+
fill.Add(gp_Pnt(*[float(v) for v in q]))
|
|
668
|
+
fill.Build()
|
|
669
|
+
if not fill.IsDone():
|
|
670
|
+
raise FeatureError("the fill failed: the edges must form one closed loop")
|
|
671
|
+
_add_surface(ctx, name, Face(fill.Face()))
|
|
672
|
+
|
|
673
|
+
|
|
674
|
+
def build_connection_point(ctx: Ctx, p: dict, name: str) -> None:
|
|
675
|
+
"""Connection point (for magnetic mates): a point and the direction a mating part comes in from."""
|
|
676
|
+
pt, d = vec(p["point"]), _dir(p.get("direction", "Y"))
|
|
677
|
+
ctx.points[name] = pt
|
|
678
|
+
ctx.info[name] = {"connection_point": {"point": list(pt), "normal": list(d)}}
|
|
679
|
+
|
|
680
|
+
|
|
681
|
+
def build_mate_reference(ctx: Ctx, p: dict, name: str) -> None:
|
|
682
|
+
"""Mate Reference: a named face of this part and the mate it takes; inserted into an assembly next to a
|
|
683
|
+
part with a mate reference of the same name, the two are mated automatically."""
|
|
684
|
+
if ctx.body is None:
|
|
685
|
+
raise FeatureError("no body for a mate reference")
|
|
686
|
+
face = resolve_face(ctx.body, p["face"])
|
|
687
|
+
ctx.info[name] = {"mate_reference": {"name": p.get("reference") or name, "type": p.get("type", "coincident"),
|
|
688
|
+
"face": p["face"], "aligned": bool(p.get("aligned")), "kind": geom_name(face)}}
|
|
689
|
+
|
|
690
|
+
|
|
691
|
+
BUILDERS = {
|
|
692
|
+
"connection_point": build_connection_point, "mate_reference": build_mate_reference,
|
|
693
|
+
"flex": build_flex, "deform": build_deform, "freeform": build_freeform, "profile_fillet": build_profile_fillet,
|
|
694
|
+
"parting_line": build_parting_line, "parting_surface": build_parting_surface,
|
|
695
|
+
"shut_off_surfaces": build_shut_off_surfaces, "tooling_split": build_tooling_split,
|
|
696
|
+
"insert_part": build_insert_part, "cosmetic_thread": build_cosmetic_thread,
|
|
697
|
+
"ruled_surface": build_ruled_surface, "radiate_surface": build_radiate_surface,
|
|
698
|
+
"extend_surface": build_extend_surface, "untrim_surface": build_untrim_surface,
|
|
699
|
+
"flatten_surface": build_flatten_surface,
|
|
700
|
+
}
|
|
701
|
+
NAME_PREFIX = {
|
|
702
|
+
"connection_point": "Connection Point", "mate_reference": "MateReference",
|
|
703
|
+
"flex": "Flex", "deform": "Deform", "freeform": "Freeform", "profile_fillet": "Fillet",
|
|
704
|
+
"parting_line": "Parting Line", "parting_surface": "Parting Surface", "shut_off_surfaces": "Shut-Off Surface",
|
|
705
|
+
"tooling_split": "Tooling Split", "insert_part": "Insert Part", "cosmetic_thread": "Cosmetic Thread",
|
|
706
|
+
"ruled_surface": "Ruled Surface", "radiate_surface": "Radiate Surface", "extend_surface": "Surface-Extend",
|
|
707
|
+
"untrim_surface": "Surface-Untrim", "flatten_surface": "Surface-Flatten",
|
|
708
|
+
}
|