@forgent3d/cad-runtime 0.1.2 → 0.1.3
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.
- package/dist/index.cjs +1 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +0 -1
- package/dist/index.d.ts +0 -1
- package/dist/index.js +1 -2
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/python/__pycache__/export_runner.cpython-313.pyc +0 -0
- package/python/export_runner.py +203 -5
package/dist/index.cjs
CHANGED
|
@@ -31,8 +31,7 @@ module.exports = __toCommonJS(index_exports);
|
|
|
31
31
|
var SANDBOX_AUTH_SCHEME = "Bearer";
|
|
32
32
|
var SANDBOX_API_PATHS = {
|
|
33
33
|
resolve: "/v1/sandboxes/resolve",
|
|
34
|
-
runTool: "/v1/tools/run"
|
|
35
|
-
export: "/v1/export"
|
|
34
|
+
runTool: "/v1/tools/run"
|
|
36
35
|
};
|
|
37
36
|
function normalizeSandboxSecret(secret) {
|
|
38
37
|
const normalized = secret?.trim();
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/sandbox-protocol.ts"],"sourcesContent":["export {\n SANDBOX_API_PATHS,\n SANDBOX_AUTH_SCHEME,\n normalizeSandboxSecret,\n sandboxAuthorizationHeader,\n type SandboxApiPath,\n} from \"./sandbox-protocol.js\";\n","export const SANDBOX_AUTH_SCHEME = \"Bearer\";\n\nexport const SANDBOX_API_PATHS = {\n resolve: \"/v1/sandboxes/resolve\",\n runTool: \"/v1/tools/run\",\n
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/sandbox-protocol.ts"],"sourcesContent":["export {\n SANDBOX_API_PATHS,\n SANDBOX_AUTH_SCHEME,\n normalizeSandboxSecret,\n sandboxAuthorizationHeader,\n type SandboxApiPath,\n} from \"./sandbox-protocol.js\";\n","export const SANDBOX_AUTH_SCHEME = \"Bearer\";\n\nexport const SANDBOX_API_PATHS = {\n resolve: \"/v1/sandboxes/resolve\",\n runTool: \"/v1/tools/run\",\n} as const;\n\nexport type SandboxApiPath = (typeof SANDBOX_API_PATHS)[keyof typeof SANDBOX_API_PATHS];\n\nexport function normalizeSandboxSecret(secret: string | undefined): string | undefined {\n const normalized = secret?.trim();\n return normalized || undefined;\n}\n\nexport function sandboxAuthorizationHeader(secret: string | undefined): string | undefined {\n const normalized = normalizeSandboxSecret(secret);\n return normalized ? `${SANDBOX_AUTH_SCHEME} ${normalized}` : undefined;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAO,IAAM,sBAAsB;AAE5B,IAAM,oBAAoB;AAAA,EAC/B,SAAS;AAAA,EACT,SAAS;AACX;AAIO,SAAS,uBAAuB,QAAgD;AACrF,QAAM,aAAa,QAAQ,KAAK;AAChC,SAAO,cAAc;AACvB;AAEO,SAAS,2BAA2B,QAAgD;AACzF,QAAM,aAAa,uBAAuB,MAAM;AAChD,SAAO,aAAa,GAAG,mBAAmB,IAAI,UAAU,KAAK;AAC/D;","names":[]}
|
package/dist/index.d.cts
CHANGED
|
@@ -2,7 +2,6 @@ declare const SANDBOX_AUTH_SCHEME = "Bearer";
|
|
|
2
2
|
declare const SANDBOX_API_PATHS: {
|
|
3
3
|
readonly resolve: "/v1/sandboxes/resolve";
|
|
4
4
|
readonly runTool: "/v1/tools/run";
|
|
5
|
-
readonly export: "/v1/export";
|
|
6
5
|
};
|
|
7
6
|
type SandboxApiPath = (typeof SANDBOX_API_PATHS)[keyof typeof SANDBOX_API_PATHS];
|
|
8
7
|
declare function normalizeSandboxSecret(secret: string | undefined): string | undefined;
|
package/dist/index.d.ts
CHANGED
|
@@ -2,7 +2,6 @@ declare const SANDBOX_AUTH_SCHEME = "Bearer";
|
|
|
2
2
|
declare const SANDBOX_API_PATHS: {
|
|
3
3
|
readonly resolve: "/v1/sandboxes/resolve";
|
|
4
4
|
readonly runTool: "/v1/tools/run";
|
|
5
|
-
readonly export: "/v1/export";
|
|
6
5
|
};
|
|
7
6
|
type SandboxApiPath = (typeof SANDBOX_API_PATHS)[keyof typeof SANDBOX_API_PATHS];
|
|
8
7
|
declare function normalizeSandboxSecret(secret: string | undefined): string | undefined;
|
package/dist/index.js
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/sandbox-protocol.ts"],"sourcesContent":["export const SANDBOX_AUTH_SCHEME = \"Bearer\";\n\nexport const SANDBOX_API_PATHS = {\n resolve: \"/v1/sandboxes/resolve\",\n runTool: \"/v1/tools/run\",\n
|
|
1
|
+
{"version":3,"sources":["../src/sandbox-protocol.ts"],"sourcesContent":["export const SANDBOX_AUTH_SCHEME = \"Bearer\";\n\nexport const SANDBOX_API_PATHS = {\n resolve: \"/v1/sandboxes/resolve\",\n runTool: \"/v1/tools/run\",\n} as const;\n\nexport type SandboxApiPath = (typeof SANDBOX_API_PATHS)[keyof typeof SANDBOX_API_PATHS];\n\nexport function normalizeSandboxSecret(secret: string | undefined): string | undefined {\n const normalized = secret?.trim();\n return normalized || undefined;\n}\n\nexport function sandboxAuthorizationHeader(secret: string | undefined): string | undefined {\n const normalized = normalizeSandboxSecret(secret);\n return normalized ? `${SANDBOX_AUTH_SCHEME} ${normalized}` : undefined;\n}\n"],"mappings":";AAAO,IAAM,sBAAsB;AAE5B,IAAM,oBAAoB;AAAA,EAC/B,SAAS;AAAA,EACT,SAAS;AACX;AAIO,SAAS,uBAAuB,QAAgD;AACrF,QAAM,aAAa,QAAQ,KAAK;AAChC,SAAO,cAAc;AACvB;AAEO,SAAS,2BAA2B,QAAgD;AACzF,QAAM,aAAa,uBAAuB,MAAM;AAChD,SAAO,aAAa,GAAG,mBAAmB,IAAI,UAAU,KAAK;AAC/D;","names":[]}
|
package/package.json
CHANGED
|
Binary file
|
package/python/export_runner.py
CHANGED
|
@@ -3,9 +3,9 @@ export_runner.py - Auto-managed by AI CAD Companion Viewer (do not edit manually
|
|
|
3
3
|
----------------------------------------------------------------------------
|
|
4
4
|
Responsibilities:
|
|
5
5
|
* Accept --model <name> (and legacy --part <name>);
|
|
6
|
-
* Optionally support on-demand exports via --export-format/--output (step/stl/brep);
|
|
7
|
-
* Load models/<model>/parts/<part-name>/part.py and read
|
|
8
|
-
* Export build123d geometry to .cache/<name>.brep
|
|
6
|
+
* Optionally support on-demand exports via --export-format/--output (step/stl/obj/brep/glb/3mf);
|
|
7
|
+
* Load models/<model>/assembly.py, part.py, or parts/<part-name>/part.py and read result;
|
|
8
|
+
* Export build123d geometry to .cache/<name>.brep and requested exchange/mesh formats;
|
|
9
9
|
* Let the frontend parse BREP via occt-import-js for geometry inspection.
|
|
10
10
|
"""
|
|
11
11
|
import os
|
|
@@ -18,8 +18,11 @@ if sys.platform == "win32":
|
|
|
18
18
|
|
|
19
19
|
import argparse
|
|
20
20
|
import json
|
|
21
|
+
import struct
|
|
22
|
+
import tempfile
|
|
21
23
|
import time
|
|
22
24
|
import traceback
|
|
25
|
+
import zipfile
|
|
23
26
|
|
|
24
27
|
HERE = os.path.dirname(os.path.abspath(__file__))
|
|
25
28
|
PROJECT_ROOT = HERE
|
|
@@ -94,6 +97,159 @@ def _write_stl(shape, path_out):
|
|
|
94
97
|
raise RuntimeError(f"Unable to export STL: {exc}")
|
|
95
98
|
|
|
96
99
|
|
|
100
|
+
def _xml_escape_text(value):
|
|
101
|
+
return (
|
|
102
|
+
str(value if value is not None else "")
|
|
103
|
+
.replace("&", "&")
|
|
104
|
+
.replace("<", "<")
|
|
105
|
+
.replace(">", ">")
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def _xml_escape_attr(value):
|
|
110
|
+
return _xml_escape_text(value).replace('"', """).replace("'", "'")
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def _format_3mf_number(value):
|
|
114
|
+
num = float(value)
|
|
115
|
+
if abs(num) < 1e-9:
|
|
116
|
+
num = 0.0
|
|
117
|
+
if num.is_integer():
|
|
118
|
+
return str(int(num))
|
|
119
|
+
return f"{num:.6f}".rstrip("0").rstrip(".") or "0"
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def _rgba_to_3mf_color(rgba):
|
|
123
|
+
if not rgba:
|
|
124
|
+
rgba = (0xb0 / 255, 0xb0 / 255, 0xb0 / 255, 1.0)
|
|
125
|
+
values = []
|
|
126
|
+
for i, fallback in enumerate((0.69, 0.69, 0.69, 1.0)):
|
|
127
|
+
try:
|
|
128
|
+
v = float(rgba[i])
|
|
129
|
+
except Exception:
|
|
130
|
+
v = fallback
|
|
131
|
+
values.append(max(0, min(255, round(max(0.0, min(1.0, v)) * 255))))
|
|
132
|
+
return "#" + "".join(f"{v:02X}" for v in values)
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def _read_stl_triangles(path_in):
|
|
136
|
+
with open(path_in, "rb") as f:
|
|
137
|
+
data = f.read()
|
|
138
|
+
if len(data) >= 84:
|
|
139
|
+
tri_count = struct.unpack("<I", data[80:84])[0]
|
|
140
|
+
expected = 84 + tri_count * 50
|
|
141
|
+
if expected == len(data):
|
|
142
|
+
triangles = []
|
|
143
|
+
offset = 84
|
|
144
|
+
for _ in range(tri_count):
|
|
145
|
+
values = struct.unpack("<12fH", data[offset:offset + 50])
|
|
146
|
+
triangles.append((
|
|
147
|
+
(values[3], values[4], values[5]),
|
|
148
|
+
(values[6], values[7], values[8]),
|
|
149
|
+
(values[9], values[10], values[11]),
|
|
150
|
+
))
|
|
151
|
+
offset += 50
|
|
152
|
+
return triangles
|
|
153
|
+
|
|
154
|
+
text = data.decode("utf-8", errors="ignore")
|
|
155
|
+
vertices = []
|
|
156
|
+
for line in text.splitlines():
|
|
157
|
+
parts = line.strip().split()
|
|
158
|
+
if len(parts) >= 4 and parts[0].lower() == "vertex":
|
|
159
|
+
try:
|
|
160
|
+
vertices.append((float(parts[1]), float(parts[2]), float(parts[3])))
|
|
161
|
+
except ValueError:
|
|
162
|
+
pass
|
|
163
|
+
if len(vertices) < 3 or len(vertices) % 3 != 0:
|
|
164
|
+
raise RuntimeError("Unable to parse STL triangles for mesh export")
|
|
165
|
+
return [(vertices[i], vertices[i + 1], vertices[i + 2]) for i in range(0, len(vertices), 3)]
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def _write_obj_mesh(triangles, path_out, title="Forgent3D model"):
|
|
169
|
+
if not triangles:
|
|
170
|
+
raise RuntimeError("OBJ export found no triangles")
|
|
171
|
+
with open(path_out, "w", encoding="utf-8", newline="\n") as f:
|
|
172
|
+
f.write("# Forgent3D OBJ export\n")
|
|
173
|
+
f.write(f"o {_xml_escape_text(title or 'model')}\n")
|
|
174
|
+
for tri in triangles:
|
|
175
|
+
for vertex in tri:
|
|
176
|
+
f.write(
|
|
177
|
+
f"v {_format_3mf_number(vertex[0])} "
|
|
178
|
+
f"{_format_3mf_number(vertex[1])} "
|
|
179
|
+
f"{_format_3mf_number(vertex[2])}\n"
|
|
180
|
+
)
|
|
181
|
+
for i in range(0, len(triangles) * 3, 3):
|
|
182
|
+
f.write(f"f {i + 1} {i + 2} {i + 3}\n")
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def _write_3mf_archive(triangles, path_out, title="Forgent3D model", color=None):
|
|
186
|
+
if not triangles:
|
|
187
|
+
raise RuntimeError("3MF export found no triangles")
|
|
188
|
+
display_color = _rgba_to_3mf_color(color)
|
|
189
|
+
vertices_xml = []
|
|
190
|
+
triangles_xml = []
|
|
191
|
+
vertex_index = 0
|
|
192
|
+
for tri in triangles:
|
|
193
|
+
indices = []
|
|
194
|
+
for vertex in tri:
|
|
195
|
+
indices.append(vertex_index)
|
|
196
|
+
vertices_xml.append(
|
|
197
|
+
f' <vertex x="{_format_3mf_number(vertex[0])}" '
|
|
198
|
+
f'y="{_format_3mf_number(vertex[1])}" '
|
|
199
|
+
f'z="{_format_3mf_number(vertex[2])}" />'
|
|
200
|
+
)
|
|
201
|
+
vertex_index += 1
|
|
202
|
+
triangles_xml.append(
|
|
203
|
+
f' <triangle v1="{indices[0]}" v2="{indices[1]}" v3="{indices[2]}" />'
|
|
204
|
+
)
|
|
205
|
+
|
|
206
|
+
safe_title = _xml_escape_text(str(title or "Forgent3D model")[:128])
|
|
207
|
+
model_xml = "\n".join([
|
|
208
|
+
'<?xml version="1.0" encoding="UTF-8"?>',
|
|
209
|
+
'<model unit="millimeter" xml:lang="en-US" xmlns="http://schemas.microsoft.com/3dmanufacturing/core/2015/02">',
|
|
210
|
+
f' <metadata name="Title">{safe_title}</metadata>',
|
|
211
|
+
' <resources>',
|
|
212
|
+
' <basematerials id="1">',
|
|
213
|
+
f' <base name="Default" displaycolor="{display_color}" />',
|
|
214
|
+
' </basematerials>',
|
|
215
|
+
f' <object id="2" type="model" name="{_xml_escape_attr(title or "model")}" pid="1" pindex="0">',
|
|
216
|
+
' <mesh>',
|
|
217
|
+
' <vertices>',
|
|
218
|
+
*vertices_xml,
|
|
219
|
+
' </vertices>',
|
|
220
|
+
' <triangles>',
|
|
221
|
+
*triangles_xml,
|
|
222
|
+
' </triangles>',
|
|
223
|
+
' </mesh>',
|
|
224
|
+
' </object>',
|
|
225
|
+
' </resources>',
|
|
226
|
+
' <build>',
|
|
227
|
+
' <item objectid="2" />',
|
|
228
|
+
' </build>',
|
|
229
|
+
'</model>',
|
|
230
|
+
'',
|
|
231
|
+
])
|
|
232
|
+
content_types_xml = "\n".join([
|
|
233
|
+
'<?xml version="1.0" encoding="UTF-8"?>',
|
|
234
|
+
'<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">',
|
|
235
|
+
' <Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml" />',
|
|
236
|
+
' <Override PartName="/3D/3dmodel.model" ContentType="application/vnd.ms-package.3dmanufacturing-3dmodel+xml" />',
|
|
237
|
+
'</Types>',
|
|
238
|
+
'',
|
|
239
|
+
])
|
|
240
|
+
rels_xml = "\n".join([
|
|
241
|
+
'<?xml version="1.0" encoding="UTF-8"?>',
|
|
242
|
+
'<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">',
|
|
243
|
+
' <Relationship Target="/3D/3dmodel.model" Id="rel0" Type="http://schemas.microsoft.com/3dmanufacturing/2013/01/3dmodel" />',
|
|
244
|
+
'</Relationships>',
|
|
245
|
+
'',
|
|
246
|
+
])
|
|
247
|
+
with zipfile.ZipFile(path_out, "w", compression=zipfile.ZIP_DEFLATED) as z:
|
|
248
|
+
z.writestr("[Content_Types].xml", content_types_xml)
|
|
249
|
+
z.writestr("_rels/.rels", rels_xml)
|
|
250
|
+
z.writestr("3D/3dmodel.model", model_xml)
|
|
251
|
+
|
|
252
|
+
|
|
97
253
|
# Keep in sync with packages/electron/src/viewer-materials.ts MATERIAL_PRESETS.
|
|
98
254
|
_VIEWER_PRESET_COLORS = {
|
|
99
255
|
"cad_clay": (0xc8 / 255, 0xd0 / 255, 0xdc / 255, 1.0),
|
|
@@ -218,6 +374,44 @@ def _write_glb(shape, path_out, params=None):
|
|
|
218
374
|
raise RuntimeError(f"Unable to export GLB: {exc}")
|
|
219
375
|
|
|
220
376
|
|
|
377
|
+
def _write_3mf(shape, path_out, params=None, title="Forgent3D model"):
|
|
378
|
+
fd, tmp_stl = tempfile.mkstemp(suffix=".stl")
|
|
379
|
+
os.close(fd)
|
|
380
|
+
try:
|
|
381
|
+
_write_stl(shape, tmp_stl)
|
|
382
|
+
triangles = _read_stl_triangles(tmp_stl)
|
|
383
|
+
finally:
|
|
384
|
+
try:
|
|
385
|
+
os.unlink(tmp_stl)
|
|
386
|
+
except OSError:
|
|
387
|
+
pass
|
|
388
|
+
|
|
389
|
+
color = None
|
|
390
|
+
if isinstance(params, dict):
|
|
391
|
+
viewer = params.get("__viewer") or params.get("viewer") or {}
|
|
392
|
+
materials = viewer.get("materials") if isinstance(viewer, dict) else None
|
|
393
|
+
if isinstance(materials, dict):
|
|
394
|
+
color = _resolve_material_color(materials.get("default"))
|
|
395
|
+
_write_3mf_archive(triangles, path_out, title=title, color=color)
|
|
396
|
+
return "export_runner.3mf_from_stl_mesh"
|
|
397
|
+
|
|
398
|
+
|
|
399
|
+
def _write_obj(shape, path_out, title="Forgent3D model"):
|
|
400
|
+
fd, tmp_stl = tempfile.mkstemp(suffix=".stl")
|
|
401
|
+
os.close(fd)
|
|
402
|
+
try:
|
|
403
|
+
_write_stl(shape, tmp_stl)
|
|
404
|
+
triangles = _read_stl_triangles(tmp_stl)
|
|
405
|
+
finally:
|
|
406
|
+
try:
|
|
407
|
+
os.unlink(tmp_stl)
|
|
408
|
+
except OSError:
|
|
409
|
+
pass
|
|
410
|
+
|
|
411
|
+
_write_obj_mesh(triangles, path_out, title=title)
|
|
412
|
+
return "export_runner.obj_from_stl_mesh"
|
|
413
|
+
|
|
414
|
+
|
|
221
415
|
def _resolve_model_source(model_name: str, part_name: str, source_override: str = None):
|
|
222
416
|
if source_override:
|
|
223
417
|
candidate = source_override
|
|
@@ -383,7 +577,7 @@ def build_one(model_name: str, part_name: str = None, export_format: str = "brep
|
|
|
383
577
|
return 8
|
|
384
578
|
|
|
385
579
|
fmt = (export_format or "brep").strip().lower()
|
|
386
|
-
if fmt not in ("brep", "step", "stl", "glb"):
|
|
580
|
+
if fmt not in ("brep", "step", "stl", "obj", "glb", "3mf"):
|
|
387
581
|
print(f"[export_runner] Unsupported export format: {fmt}", file=sys.stderr)
|
|
388
582
|
return 7
|
|
389
583
|
|
|
@@ -402,6 +596,10 @@ def build_one(model_name: str, part_name: str = None, export_format: str = "brep
|
|
|
402
596
|
method = _write_step(result, out)
|
|
403
597
|
elif fmt == "glb":
|
|
404
598
|
method = _write_glb(result, out, _load_model_params(model_name))
|
|
599
|
+
elif fmt == "obj":
|
|
600
|
+
method = _write_obj(result, out, model_name)
|
|
601
|
+
elif fmt == "3mf":
|
|
602
|
+
method = _write_3mf(result, out, _load_model_params(model_name), model_name)
|
|
405
603
|
else:
|
|
406
604
|
method = _write_stl(result, out)
|
|
407
605
|
export_elapsed = time.perf_counter() - export_started
|
|
@@ -425,7 +623,7 @@ def main() -> int:
|
|
|
425
623
|
parser.add_argument("--part", default=None, help="Legacy alias for --model")
|
|
426
624
|
parser.add_argument("--part-name", default=None, help="Part directory name inside models/<model>/parts/")
|
|
427
625
|
parser.add_argument("--source", default=None, help="Optional project-relative or absolute source file path (overrides default lookup)")
|
|
428
|
-
parser.add_argument("--export-format", default="brep", choices=["brep", "step", "stl", "glb"])
|
|
626
|
+
parser.add_argument("--export-format", default="brep", choices=["brep", "step", "stl", "obj", "glb", "3mf"])
|
|
429
627
|
parser.add_argument("--output", default=None, help="Optional absolute path for exported file")
|
|
430
628
|
args = parser.parse_args()
|
|
431
629
|
global PROJECT_ROOT, MODELS_DIR, CACHE_DIR
|