f3d2solidcraft 0.3.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.
Files changed (74) hide show
  1. f3d2solidcraft/__init__.py +3 -0
  2. f3d2solidcraft/__main__.py +3 -0
  3. f3d2solidcraft/_vendor/__init__.py +0 -0
  4. f3d2solidcraft/_vendor/ezf3d/LICENSE +21 -0
  5. f3d2solidcraft/_vendor/ezf3d/__init__.py +1 -0
  6. f3d2solidcraft/_vendor/ezf3d/container/__init__.py +1 -0
  7. f3d2solidcraft/_vendor/ezf3d/container/archive.py +204 -0
  8. f3d2solidcraft/_vendor/ezf3d/model/__init__.py +1 -0
  9. f3d2solidcraft/_vendor/ezf3d/model/parameters.py +365 -0
  10. f3d2solidcraft/_vendor/ezf3d/model/sketch.py +645 -0
  11. f3d2solidcraft/_vendor/ezf3d/model/timeline.py +493 -0
  12. f3d2solidcraft/_vendor/ezf3d/streams/__init__.py +1 -0
  13. f3d2solidcraft/_vendor/ezf3d/streams/manifest.py +262 -0
  14. f3d2solidcraft/_vendor/ezf3d/streams/primitives.py +191 -0
  15. f3d2solidcraft/_vendor/ezf3d/streams/segment.py +521 -0
  16. f3d2solidcraft/cli.py +574 -0
  17. f3d2solidcraft/engine.py +458 -0
  18. f3d2solidcraft/fallback.py +250 -0
  19. f3d2solidcraft/geometry.py +397 -0
  20. f3d2solidcraft/mappers/__init__.py +141 -0
  21. f3d2solidcraft/mappers/bodies.py +99 -0
  22. f3d2solidcraft/mappers/combine.py +73 -0
  23. f3d2solidcraft/mappers/components.py +542 -0
  24. f3d2solidcraft/mappers/edges.py +449 -0
  25. f3d2solidcraft/mappers/extrude.py +1501 -0
  26. f3d2solidcraft/mappers/faces.py +50 -0
  27. f3d2solidcraft/mappers/loft.py +149 -0
  28. f3d2solidcraft/mappers/outputs.py +73 -0
  29. f3d2solidcraft/mappers/params.py +134 -0
  30. f3d2solidcraft/mappers/pattern.py +68 -0
  31. f3d2solidcraft/mappers/planes.py +222 -0
  32. f3d2solidcraft/mappers/primitives.py +144 -0
  33. f3d2solidcraft/mappers/records.py +51 -0
  34. f3d2solidcraft/mappers/revolve.py +134 -0
  35. f3d2solidcraft/mappers/sheet.py +95 -0
  36. f3d2solidcraft/mappers/shell.py +180 -0
  37. f3d2solidcraft/mappers/sketch.py +455 -0
  38. f3d2solidcraft/mcp.py +218 -0
  39. f3d2solidcraft/model.py +503 -0
  40. f3d2solidcraft/oracle.py +905 -0
  41. f3d2solidcraft/pairing.py +105 -0
  42. f3d2solidcraft/reader/__init__.py +77 -0
  43. f3d2solidcraft/reader/cadir.py +832 -0
  44. f3d2solidcraft/reader/fusion.py +218 -0
  45. f3d2solidcraft/reader/locale.py +115 -0
  46. f3d2solidcraft/replay.py +322 -0
  47. f3d2solidcraft/report.py +107 -0
  48. f3d2solidcraft/tools.py +219 -0
  49. f3d2solidcraft/verify.py +186 -0
  50. f3d2solidcraft/visibility.py +95 -0
  51. f3d2solidcraft-0.3.0.dist-info/METADATA +189 -0
  52. f3d2solidcraft-0.3.0.dist-info/RECORD +74 -0
  53. f3d2solidcraft-0.3.0.dist-info/WHEEL +4 -0
  54. f3d2solidcraft-0.3.0.dist-info/entry_points.txt +5 -0
  55. f3d2solidcraft-0.3.0.dist-info/licenses/LICENSE +21 -0
  56. f3d2solidcraft-0.3.0.dist-info/licenses/src/f3d2solidcraft/_vendor/ezf3d/LICENSE +21 -0
  57. f3d2solidcraft_app/__init__.py +5 -0
  58. f3d2solidcraft_app/__main__.py +40 -0
  59. f3d2solidcraft_app/engines.py +158 -0
  60. f3d2solidcraft_app/events.py +35 -0
  61. f3d2solidcraft_app/opener.py +22 -0
  62. f3d2solidcraft_app/outcome.py +54 -0
  63. f3d2solidcraft_app/resources/icon-128.png +0 -0
  64. f3d2solidcraft_app/resources/icon-16.png +0 -0
  65. f3d2solidcraft_app/resources/icon-256.png +0 -0
  66. f3d2solidcraft_app/resources/icon-32.png +0 -0
  67. f3d2solidcraft_app/resources/icon-512.png +0 -0
  68. f3d2solidcraft_app/resources/icon-64.png +0 -0
  69. f3d2solidcraft_app/resources/icon.icns +0 -0
  70. f3d2solidcraft_app/resources/icon.ico +0 -0
  71. f3d2solidcraft_app/resources/icon.svg +26 -0
  72. f3d2solidcraft_app/runner.py +91 -0
  73. f3d2solidcraft_app/texts.py +98 -0
  74. f3d2solidcraft_app/window.py +677 -0
f3d2solidcraft/cli.py ADDED
@@ -0,0 +1,574 @@
1
+ """f3d2solidcraft command line."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import argparse
6
+ import contextlib
7
+ import json
8
+ import os
9
+ import subprocess
10
+ import sys
11
+ import tempfile
12
+ import time
13
+ from collections import Counter
14
+ from collections.abc import Callable
15
+ from pathlib import Path
16
+
17
+ import zstandard
18
+
19
+ from f3d2solidcraft.engine import BodyUnmeasurable, CommandRefused, Engine, EngineError
20
+ from f3d2solidcraft.fallback import apply_fallback, name_bodies, original_step
21
+ from f3d2solidcraft.mappers import MAPPERS
22
+ from f3d2solidcraft.mappers.components import reconcile_components
23
+ from f3d2solidcraft.oracle import Oracle, OracleUnavailable, PlacementsUnmeasurable
24
+ from f3d2solidcraft.reader import read_design
25
+ from f3d2solidcraft.replay import replay
26
+ from f3d2solidcraft.report import build_report, summary
27
+ from f3d2solidcraft.tools import ToolFailed, ToolMissing, Tools, find_tools, long_path, tool_version
28
+ from f3d2solidcraft.verify import (
29
+ Verification,
30
+ check_rebuild,
31
+ compare,
32
+ driving_parameter,
33
+ original_bodies,
34
+ )
35
+ from f3d2solidcraft.visibility import hide_what_fusion_hides
36
+
37
+
38
+ def _reference_reading(oracle: Oracle | None) -> list[dict] | None:
39
+ """The oracle's reading of the original, or None when there is none to be had."""
40
+ if oracle is None:
41
+ return None
42
+ try:
43
+ return oracle.measured()
44
+ except OracleUnavailable:
45
+ return None
46
+
47
+
48
+ # How many times a conversion may be redone after learning something only the engine could tell it.
49
+ _RETRIES = 6
50
+
51
+
52
+ def _in_engine(
53
+ design: object,
54
+ step: Path,
55
+ out: Path,
56
+ out_partial: Path,
57
+ tmp: Path,
58
+ tools: Tools,
59
+ strict: bool,
60
+ step_warnings: list[str],
61
+ unmeasurable: set[str],
62
+ progress: Callable[[int, int, str], None] | None = None,
63
+ ) -> tuple:
64
+ """The whole engine half of a conversion, in the one engine process of the run.
65
+
66
+ `strict` asks for the reading that enumerates placements where the design has repeats. It is
67
+ tried first and raises `_PlacementsUnmeasurable` when this engine cannot answer it, which is the
68
+ caller's cue to redo the run per definition.
69
+ """
70
+ with Engine(tools.solidcraft_cli, [tmp, out.parent]) as eng:
71
+ eng.unmeasurable = set(unmeasurable)
72
+ eng.call("new_design", {"name": out.stem})
73
+ # Without the original to ask, the conversion still runs: every feature that
74
+ # needs inference simply falls back, as it did before M1.
75
+ oracle_warnings: list[str] = []
76
+ try:
77
+ oracle = Oracle.open(eng, step, strict=strict)
78
+ except OracleUnavailable as exc:
79
+ oracle = None
80
+ oracle_warnings.append(f"no reference solid to infer from: {exc}")
81
+ # The oracle closes as a context manager, not in a bare `finally`: a replay that
82
+ # dies keeps its own error (`Oracle.__exit__` only swallows a failed delete then),
83
+ # while on success a failed delete still aborts instead of saving the reference.
84
+ with oracle if oracle is not None else contextlib.nullcontext():
85
+ result, ctx = replay(design, eng, oracle, progress=progress)
86
+ # The reference the oracle already read *is* the original, so the verification
87
+ # takes its view of it from there instead of opening the STEP again at the end.
88
+ original = _reference_reading(oracle)
89
+ fallback_warnings = step_warnings + oracle_warnings + apply_fallback(ctx, result, step)
90
+ # Right after the import, so everything below reads the names the design gives its
91
+ # bodies rather than the STEP export's: the rebuild check and the measurement the
92
+ # verification reports, the component reconciliation and the visibility replay.
93
+ fallback_warnings += name_bodies(ctx, result)
94
+ rebuild_parameter = driving_parameter(ctx, result)
95
+ rebuild_error = check_rebuild(ctx, result)
96
+ # The placements the measurement below stands for, counted off the component graph: one
97
+ # `component.list`, nothing measured.
98
+ measured_placements = eng.placements_by_body()
99
+ converted = eng.body_readings()
100
+ # Last, because it is the only step after the measurement: it moves bodies between
101
+ # components without touching their geometry, and the engine drops its cached
102
+ # measurements on any change — so a measurement taken after it pays for the whole
103
+ # design again (24 s of `mango para lima`'s 108) and reads the same geometry.
104
+ fallback_warnings += reconcile_components(ctx, result.imported_bodies)
105
+ # After the moves, so every body is where it will be saved, and after the
106
+ # measurement above, which hiding does not change but which has no reason to run
107
+ # against a half-arranged design.
108
+ fallback_warnings += hide_what_fusion_hides(ctx, result)
109
+ # "Reads the same" held for each body's geometry and not for how many times it is placed:
110
+ # moving the bodies of a part placed N times into a component placed once collapsed the N
111
+ # copies to one, and the verification, taken above, said every one matched. Measured on a
112
+ # board of 302 placements of 43 definitions: verified, and 43 bodies in the saved file.
113
+ # So the placements are counted again right before the save, from the same free listing,
114
+ # and anything lost is the verification's to report rather than the file's to hide.
115
+ placements_lost = dict(measured_placements - eng.placements_by_body())
116
+ eng.call("save", {"path": str(out_partial)})
117
+ if original is None:
118
+ # No oracle, or it stopped answering: read the original the long way. Opening
119
+ # the STEP replaces the design in the session, so it happens after the save.
120
+ try:
121
+ original = original_bodies(eng, step)
122
+ except CommandRefused as exc:
123
+ # The third and last place the original is load-bearing, and the last one
124
+ # that was unprotected. The design is already saved above, so what is lost
125
+ # here is only the verification — say so instead of ending the run, which
126
+ # is what solidcraft 0.40.0-rc.3 made it do on
127
+ # `corpus-07` (spec section 124).
128
+ fallback_warnings.append(f"the original could not be read to verify against: {exc.message}")
129
+ # Read before the oracle's own numbers go out of scope: what the conversion spent of it
130
+ # belongs in the report, so the next question about the budget is answerable from a run
131
+ # that already happened instead of from a patch that counts by hand (spec section 131).
132
+ budget = oracle.budget() if oracle is not None else None
133
+ if budget is not None and budget["clock_exceeded"]:
134
+ fallback_warnings.append(
135
+ f"the oracle's questions took {budget['seconds']:.1f} s, over the "
136
+ f"{budget['clock_ceiling']:.0f} s ceiling: it stopped asking with "
137
+ f"{budget['budget'] - budget['asked']} of its {budget['budget']} questions unspent, "
138
+ f"so what this run counted is this machine's and not the budget's"
139
+ )
140
+ return (
141
+ result,
142
+ ctx,
143
+ original,
144
+ converted,
145
+ fallback_warnings,
146
+ rebuild_parameter,
147
+ rebuild_error,
148
+ budget,
149
+ placements_lost,
150
+ )
151
+
152
+
153
+ def convert(
154
+ f3d: Path,
155
+ out: Path,
156
+ report_path: Path | None,
157
+ force: bool,
158
+ tools: Tools,
159
+ progress: Callable[[int, int, str], None] | None = None,
160
+ ) -> dict:
161
+ # Every path the engine sees is spelt out in full: on Windows it refuses an 8.3 short name, which a
162
+ # user's own path can be as much as the temp dir below (spec of `long_path`).
163
+ f3d = long_path(f3d)
164
+ out = long_path(out)
165
+ report_path = long_path(report_path) if report_path is not None else None
166
+ for target in (out, report_path):
167
+ if target is not None and target.exists() and not force:
168
+ raise FileExistsError(f"{target} exists; use --force to replace it")
169
+ out.parent.mkdir(parents=True, exist_ok=True)
170
+ if report_path:
171
+ report_path.parent.mkdir(parents=True, exist_ok=True)
172
+ # Outputs land as siblings first and replace the targets only once the whole run succeeded.
173
+ out_partial = out.with_name(out.name + ".partial")
174
+ report_partial = report_path.with_name(report_path.name + ".partial") if report_path else None
175
+ try:
176
+ with tempfile.TemporaryDirectory(prefix="f3d2sc-") as tmp_name:
177
+ tmp = long_path(tmp_name)
178
+ design = read_design(f3d, tools.cadmpeg, tmp)
179
+ # left by read_design; ~25x the .f3d, so it goes as soon as possible
180
+ cadir = tmp / "design.cadir.json"
181
+ step, step_warnings = original_step(tools.cadmpeg, cadir, tmp / "original.step")
182
+ cadir.unlink()
183
+ (tmp / "design.cadir.fidelity.json").unlink(missing_ok=True) # cadmpeg dump writes it alongside
184
+ out_partial.unlink(missing_ok=True) # a stale one would make the engine leave a .bak
185
+ # Two things can only be learnt by trying, and each one costs the engine: whether the
186
+ # design's placements can be measured at all, and whether some one body cannot. A
187
+ # call that times out leaves the session out of step, so the engine is gone either way
188
+ # and the run is redone knowing one more thing — the import is what gets paid again.
189
+ # Bounded by `_RETRIES` so a design that keeps producing new unmeasurable bodies ends
190
+ # with a refusal rather than a loop (spec section 98).
191
+ mode_warnings: list[str] = []
192
+ strict = True
193
+ unmeasurable: set[str] = set()
194
+ for _attempt in range(_RETRIES):
195
+ try:
196
+ (
197
+ result,
198
+ _ctx,
199
+ original,
200
+ converted,
201
+ fallback_warnings,
202
+ rebuild_parameter,
203
+ rebuild_error,
204
+ budget,
205
+ placements_lost,
206
+ ) = _in_engine(
207
+ design,
208
+ step,
209
+ out,
210
+ out_partial,
211
+ tmp,
212
+ tools,
213
+ strict,
214
+ step_warnings,
215
+ unmeasurable,
216
+ progress=progress,
217
+ )
218
+ break
219
+ except PlacementsUnmeasurable as exc:
220
+ mode_warnings.append(
221
+ f"{exc}, so every reading counts one body per definition and the "
222
+ f"verification compares them that way"
223
+ )
224
+ strict = False
225
+ except BodyUnmeasurable as exc:
226
+ mode_warnings.append(
227
+ f"{exc}, so it is left out of every reading and out of the verification: "
228
+ f"nothing is claimed about it either way"
229
+ )
230
+ unmeasurable.add(exc.body)
231
+ else:
232
+ raise EngineError(
233
+ f"gave up after {_RETRIES} attempts; the last reason was: {mode_warnings[-1]}"
234
+ )
235
+ fallback_warnings += mode_warnings
236
+ verification = (
237
+ compare(original, converted)
238
+ if original is not None
239
+ else Verification(unverified="the original could not be read")
240
+ )
241
+ verification.rebuild_error = rebuild_error
242
+ verification.placements_lost = placements_lost
243
+ verification.rebuild_parameter = rebuild_parameter
244
+ # Said out loud, because the verification passing while a body was never looked at is
245
+ # exactly the thing a reader has to be able to tell apart (spec section 133).
246
+ if verification.unmeasured_original or verification.unmeasured_converted:
247
+ fallback_warnings.append(
248
+ f"{len(verification.unmeasured_original)} body(ies) of the original and "
249
+ f"{len(verification.unmeasured_converted)} of the conversion could not be "
250
+ f"measured, so they are left out of the verification and nothing is claimed "
251
+ f"about them either way: "
252
+ f"{', '.join(sorted(set(verification.unmeasured_original + verification.unmeasured_converted)))}"
253
+ )
254
+ report = build_report(design, result, fallback_warnings, verification, budget)
255
+ if report_partial:
256
+ report_partial.write_text(json.dumps(report, indent=2), encoding="utf-8")
257
+ os.replace(out_partial, out)
258
+ if report_partial and report_path:
259
+ os.replace(report_partial, report_path)
260
+ finally:
261
+ out_partial.unlink(missing_ok=True)
262
+ if report_partial:
263
+ report_partial.unlink(missing_ok=True)
264
+ return report
265
+
266
+
267
+ def inspect(f3d: Path, tools: Tools) -> str:
268
+ with tempfile.TemporaryDirectory(prefix="f3d2sc-") as tmp:
269
+ design = read_design(f3d, tools.cadmpeg, long_path(tmp))
270
+ tags = Counter(f.tag for f in design.features)
271
+ lines = [f"{f3d}: {len(design.parameters)} parameters, {len(design.sketches)} sketches"]
272
+ lines += [
273
+ f" {tag:<24} {n:>3} {'mapped' if tag in MAPPERS else 'fallback'}" for tag, n in tags.most_common()
274
+ ]
275
+ for occ in design.occurrences:
276
+ component = design.components.get(occ.component or "")
277
+ if component is None or component.name is None:
278
+ continue
279
+ depth = 0
280
+ parent = occ.parent
281
+ while parent:
282
+ depth += 1
283
+ parent = next((o.parent for o in design.occurrences if o.id == parent), None)
284
+ lines.append(
285
+ f" {' ' * depth}component {component.name:<20} "
286
+ f"{len(component.bodies):>2} bodies, {len(component.sketches):>2} sketches"
287
+ f"{'' if occ.identity else ' (moved: not an identity placement)'}"
288
+ )
289
+ lines += [f" ! {w}" for w in design.warnings]
290
+ return "\n".join(lines)
291
+
292
+
293
+ def inspect_counts(f3d: Path, tools: Tools) -> dict:
294
+ """What the file holds — parameters, sketches, features — and nothing predicted about it."""
295
+ with tempfile.TemporaryDirectory(prefix="f3d2sc-") as tmp:
296
+ design = read_design(f3d, tools.cadmpeg, long_path(tmp))
297
+ return {
298
+ "parameters": len(design.parameters),
299
+ "sketches": len(design.sketches),
300
+ "features": len(design.features),
301
+ }
302
+
303
+
304
+ # A file regresses when it gets this much slower or heavier, beyond small absolute noise.
305
+ _SLOWER, _SLOWER_ABS_S = 1.5, 1.0
306
+ _HEAVIER, _HEAVIER_ABS_MB = 1.5, 50.0
307
+
308
+
309
+ def _peak_working_set(pid: int) -> int:
310
+ """Windows' answer to `ru_maxrss`: the peak working set of a process that has exited, in bytes.
311
+
312
+ Asked of the process object, which outlives the process while the `Popen` that started it still
313
+ holds its handle — so `pid` cannot have been reused yet. It counts the converter's own process,
314
+ not the engines it starts, which are processes of their own; `wait4` on Linux also folds in the
315
+ children the converter waited for. Windows has no `wait4`: without this `bench` crashed there.
316
+ """
317
+ import ctypes
318
+ from ctypes import wintypes
319
+
320
+ class _Counters(ctypes.Structure):
321
+ _fields_ = [
322
+ ("cb", wintypes.DWORD),
323
+ ("PageFaultCount", wintypes.DWORD),
324
+ ("PeakWorkingSetSize", ctypes.c_size_t),
325
+ ("WorkingSetSize", ctypes.c_size_t),
326
+ ("QuotaPeakPagedPoolUsage", ctypes.c_size_t),
327
+ ("QuotaPagedPoolUsage", ctypes.c_size_t),
328
+ ("QuotaPeakNonPagedPoolUsage", ctypes.c_size_t),
329
+ ("QuotaNonPagedPoolUsage", ctypes.c_size_t),
330
+ ("PagefileUsage", ctypes.c_size_t),
331
+ ("PeakPagefileUsage", ctypes.c_size_t),
332
+ ]
333
+
334
+ kernel32 = ctypes.WinDLL("kernel32", use_last_error=True)
335
+ kernel32.OpenProcess.argtypes = [wintypes.DWORD, wintypes.BOOL, wintypes.DWORD]
336
+ kernel32.OpenProcess.restype = wintypes.HANDLE
337
+ kernel32.K32GetProcessMemoryInfo.argtypes = [wintypes.HANDLE, ctypes.POINTER(_Counters), wintypes.DWORD]
338
+ kernel32.K32GetProcessMemoryInfo.restype = wintypes.BOOL
339
+ kernel32.CloseHandle.argtypes = [wintypes.HANDLE]
340
+ process_query_limited_information = 0x1000
341
+ handle = kernel32.OpenProcess(process_query_limited_information, False, pid)
342
+ if not handle:
343
+ raise OSError(ctypes.get_last_error(), f"cannot open process {pid} to read its memory")
344
+ try:
345
+ counters = _Counters()
346
+ counters.cb = ctypes.sizeof(_Counters)
347
+ if not kernel32.K32GetProcessMemoryInfo(handle, ctypes.byref(counters), counters.cb):
348
+ raise OSError(ctypes.get_last_error(), f"cannot read the memory of process {pid}")
349
+ return int(counters.PeakWorkingSetSize)
350
+ finally:
351
+ kernel32.CloseHandle(handle)
352
+
353
+
354
+ def _measured_convert(f3d: Path, out_dir: Path) -> dict:
355
+ """Convert in a child process: wall time and that process's peak memory."""
356
+ report_path = out_dir / f"{f3d.stem}.report.json"
357
+ log = out_dir / f"{f3d.stem}.log"
358
+ args = [
359
+ sys.executable,
360
+ "-m",
361
+ "f3d2solidcraft",
362
+ "convert",
363
+ str(f3d),
364
+ "-o",
365
+ str(out_dir / f"{f3d.stem}.solidcraft"),
366
+ "--report",
367
+ str(report_path),
368
+ "--force",
369
+ ]
370
+ report_path.unlink(missing_ok=True) # a stale report from an earlier run must never be read
371
+ start = time.monotonic()
372
+ with log.open("w") as sink:
373
+ proc = subprocess.Popen(args, stdout=sink, stderr=subprocess.STDOUT)
374
+ if hasattr(os, "wait4"):
375
+ _, status, usage = os.wait4(proc.pid, 0)
376
+ # ru_maxrss is bytes on macOS and kilobytes on Linux.
377
+ peak_bytes = usage.ru_maxrss * (1 if sys.platform == "darwin" else 1024)
378
+ code = os.waitstatus_to_exitcode(status)
379
+ else:
380
+ code = proc.wait()
381
+ peak_bytes = _peak_working_set(proc.pid)
382
+ seconds = round(time.monotonic() - start, 3)
383
+ peak_mb = round(peak_bytes / (1024 * 1024), 1)
384
+ if code not in (0, 2) or not report_path.is_file():
385
+ error = log.read_text(errors="replace")[-500:]
386
+ return {"ok": False, "error": error, "seconds": seconds, "peak_mb": peak_mb}
387
+ report = json.loads(report_path.read_text(encoding="utf-8"))
388
+ v = report["verification"]
389
+ bodies = report.get("bodies") or {}
390
+ return {
391
+ "ok": v["ok"],
392
+ **report["counts"],
393
+ "native": bodies.get("native", 0),
394
+ "imported": bodies.get("imported", 0),
395
+ "missing": len(v["missing"]),
396
+ "extra": len(v["extra"]),
397
+ "placements_lost": sum((v.get("placements_lost") or {}).values()),
398
+ "seconds": seconds,
399
+ "peak_mb": peak_mb,
400
+ }
401
+
402
+
403
+ def _regressions(name: str, was: dict, now: dict) -> list[str]:
404
+ if "exact" not in was:
405
+ return [] # only an error row has no counts, and a file that never converted cannot regress
406
+ found = []
407
+ if was.get("ok") and not now.get("ok"):
408
+ found.append(f"{name}: no longer verified")
409
+ exact_before, exact_now = was.get("exact", 0), now.get("exact", 0)
410
+ inferred_before, inferred_now = was.get("inferred", 0), now.get("inferred", 0)
411
+ rebuilt_before = exact_before + inferred_before
412
+ rebuilt_now = exact_now + inferred_now
413
+ if rebuilt_now < rebuilt_before:
414
+ found.append(f"{name}: rebuilt features {rebuilt_before} -> {rebuilt_now}")
415
+ # The total can hold while the file stops being read and starts being guessed: say so.
416
+ if exact_now < exact_before and inferred_now > inferred_before:
417
+ found.append(
418
+ f"{name}: exact {exact_before} -> {exact_now} with inferred {inferred_before} -> {inferred_now}"
419
+ )
420
+ if now.get("native", 0) < was.get("native", 0):
421
+ found.append(f"{name}: native bodies {was.get('native')} -> {now.get('native', 0)}")
422
+ for key in ("fallback", "failed", "missing", "extra", "placements_lost"):
423
+ if now.get(key, 0) > was.get(key, 0):
424
+ found.append(f"{name}: {key} {was.get(key)} -> {now.get(key, 0)}")
425
+ for key, ratio, floor in (("seconds", _SLOWER, _SLOWER_ABS_S), ("peak_mb", _HEAVIER, _HEAVIER_ABS_MB)):
426
+ a, b = was.get(key), now.get(key)
427
+ if a and b and b > a * ratio and b - a > floor:
428
+ found.append(f"{name}: {key} {a} -> {b}")
429
+ return found
430
+
431
+
432
+ def _load_baseline(path: Path) -> tuple[dict, dict]:
433
+ """(the tool versions it was measured with, the record per file).
434
+
435
+ A baseline written before this recorded them is still read, with empty versions: those files
436
+ are the project's own working baselines and there was no reason to throw them away. What is not
437
+ allowed is a silent comparison across engines, which is what `_tool_notes` is for.
438
+ """
439
+ try:
440
+ before = json.loads(path.read_text(encoding="utf-8"))
441
+ except json.JSONDecodeError as exc:
442
+ raise ValueError(f"baseline {path} is not valid JSON: {exc}") from None
443
+ if not isinstance(before, dict):
444
+ raise ValueError(f"baseline {path} is not a bench.json (expected an object per file)") # noqa: TRY004
445
+ if isinstance(before.get("files"), dict):
446
+ return before.get("tools") or {}, before["files"]
447
+ return {}, before
448
+
449
+
450
+ def _tool_notes(was: dict, now: dict) -> list[str]:
451
+ """What to say out loud when the baseline was not measured on the same tools.
452
+
453
+ A comparison across engine versions is exactly what the solidcore lane is for, so this is not a
454
+ regression and does not fail the run. It is printed first, and printed every time, because the
455
+ numbers below it mean something different then — and because an engine can move on its own: the
456
+ `solidcraft-cli` on this machine is a symlink into an app that updates itself, which is how four
457
+ spec sections came to be measured on 0.30.1 while a note said 0.30.0 (spec sections 123 to 125).
458
+ """
459
+ notes = []
460
+ for tool in ("solidcraft_cli", "cadmpeg"):
461
+ a, b = was.get(tool), now.get(tool)
462
+ if not a:
463
+ notes.append(f"the baseline does not record which {tool} measured it; this run used {b}")
464
+ elif a != b:
465
+ notes.append(f"measured on {tool} {b}, the baseline on {a}: the comparison crosses versions")
466
+ return notes
467
+
468
+
469
+ def bench(
470
+ corpus: Path, out_dir: Path, baseline: Path | None, tools: Tools
471
+ ) -> tuple[dict, list[str], list[str]]:
472
+ # A bad baseline fails before any work.
473
+ was_tools, before = _load_baseline(baseline) if baseline else ({}, {})
474
+ out_dir.mkdir(parents=True, exist_ok=True)
475
+ now_tools = {
476
+ "solidcraft_cli": tool_version(tools.solidcraft_cli),
477
+ "cadmpeg": tool_version(tools.cadmpeg),
478
+ }
479
+ files = sorted([*corpus.rglob("*.f3d"), *corpus.rglob("*.f3z")])
480
+ results = {f.name: _measured_convert(f, out_dir) for f in files}
481
+ # Recorded with the numbers, not beside them: a baseline that does not carry the versions it was
482
+ # measured on cannot be compared honestly, and a file is the only thing that outlives the run.
483
+ (out_dir / "bench.json").write_text(
484
+ json.dumps({"tools": now_tools, "files": results}, indent=2), encoding="utf-8"
485
+ )
486
+ regressions = [
487
+ r for name, now in results.items() if name in before for r in _regressions(name, before[name], now)
488
+ ]
489
+ notes = _tool_notes(was_tools, now_tools) if baseline else []
490
+ return results, regressions, notes
491
+
492
+
493
+ def _event(payload: dict) -> None:
494
+ """One machine-readable progress line; flushed, because a reader is waiting on it."""
495
+ print(json.dumps(payload), flush=True)
496
+
497
+
498
+ def main(argv: list[str] | None = None) -> int:
499
+ parser = argparse.ArgumentParser(prog="f3d2solidcraft")
500
+ sub = parser.add_subparsers(dest="cmd", required=True)
501
+ c = sub.add_parser("convert", help="convert a .f3d or .f3z into a .solidcraft design")
502
+ c.add_argument("f3d", type=Path)
503
+ c.add_argument("-o", "--output", type=Path, required=True)
504
+ c.add_argument("--report", type=Path)
505
+ c.add_argument("--force", action="store_true")
506
+ c.add_argument(
507
+ "--progress",
508
+ action="store_true",
509
+ help="write one JSON line per feature, and one with the exit code at the end (for the desktop app)",
510
+ )
511
+ i = sub.add_parser("inspect", help="show what a .f3d or .f3z holds and what would be mapped")
512
+ i.add_argument("f3d", type=Path)
513
+ i.add_argument("--json", action="store_true", help="print the counts as one JSON line")
514
+ b = sub.add_parser("bench", help="convert a whole corpus and compare with a previous run")
515
+ b.add_argument("corpus", type=Path)
516
+ b.add_argument("--out", type=Path, required=True)
517
+ b.add_argument("--baseline", type=Path)
518
+ sub.add_parser("mcp", help="serve the converter over MCP on stdio")
519
+ args = parser.parse_args(argv)
520
+ if args.cmd == "mcp":
521
+ # Imported here so the other commands do not pay for it, and so a client that only speaks
522
+ # MCP is the only caller that can turn this process into a protocol stream.
523
+ from f3d2solidcraft.mcp import serve
524
+
525
+ return serve()
526
+ # The desktop app waits for `done` to know the run is over, so it is sent from one place on
527
+ # every way out of a conversion: a clean end, an error turned into exit 1, and an exception
528
+ # that escapes `main` (then reported as exit 1, and the exception still propagates).
529
+ announce = args.cmd == "convert" and args.progress
530
+ code = 1
531
+ try:
532
+ try:
533
+ tools = find_tools()
534
+ if args.cmd == "bench":
535
+ results, regressions, notes = bench(args.corpus, args.out, args.baseline, tools)
536
+ ok = sum(1 for r in results.values() if r.get("ok"))
537
+ slowest = max((r["seconds"] for r in results.values()), default=0)
538
+ heaviest = max((r["peak_mb"] for r in results.values()), default=0)
539
+ print(f"{ok}/{len(results)} verified, slowest {slowest} s, peak {heaviest} MB")
540
+ for line in notes:
541
+ print(f" NOTE {line}")
542
+ for line in regressions:
543
+ print(f" REGRESSION {line}")
544
+ return 1 if regressions else 0
545
+ if args.cmd == "inspect":
546
+ if args.json:
547
+ print(json.dumps(inspect_counts(args.f3d, tools)))
548
+ else:
549
+ print(inspect(args.f3d, tools))
550
+ return 0
551
+ progress = None
552
+ if announce:
553
+
554
+ def progress(index: int, total: int, name: str) -> None:
555
+ _event({"event": "feature", "index": index, "total": total, "name": name})
556
+
557
+ report = convert(args.f3d, args.output, args.report, args.force, tools, progress=progress)
558
+ except (
559
+ ToolMissing,
560
+ ToolFailed,
561
+ FileExistsError,
562
+ FileNotFoundError,
563
+ EngineError,
564
+ ValueError, # bad input data (json.JSONDecodeError is one) or a corrupt baseline
565
+ zstandard.ZstdError, # a damaged .f3d stream
566
+ ) as exc:
567
+ print(f"error: {exc}", file=sys.stderr)
568
+ return 1
569
+ print(summary(report))
570
+ code = 0 if report["verification"]["ok"] else 2
571
+ return code
572
+ finally:
573
+ if announce:
574
+ _event({"event": "done", "exit": code})