From acbf6f2db065f96f5c3f1df68a0cdb862e8f8ff9 Mon Sep 17 00:00:00 2001 From: wangruiguo Date: Thu, 3 Sep 2026 19:08:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E:=20STEP=E6=89=B9=E9=87=8F?= =?UTF-8?q?=E8=BD=ACGLB=E8=84=9A=E6=9C=AC(=E7=BD=91=E9=A1=B53D=E8=BD=BB?= =?UTF-8?q?=E9=87=8F=E5=8C=96=E6=9E=84=E5=BB=BA=E6=9C=9F=E5=B7=A5=E5=85=B7?= =?UTF-8?q?,=20=E5=BE=85=E7=9C=9F=E5=AE=9E=E6=95=B0=E6=8D=AE=E5=B0=B1?= =?UTF-8?q?=E4=BD=8D=E5=90=8E=E5=90=AF=E7=94=A8)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- occt-service/batch-convert-glb.py | 82 +++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 occt-service/batch-convert-glb.py diff --git a/occt-service/batch-convert-glb.py b/occt-service/batch-convert-glb.py new file mode 100644 index 0000000..72d437d --- /dev/null +++ b/occt-service/batch-convert-glb.py @@ -0,0 +1,82 @@ +# -*- coding: utf-8 -*- +""" +批量转换:把 catalog\step\ 下所有 STEP 转成 glTF/GLB(网页 3D 轻量化用)。 + +用法: + python batch-convert-glb.py [deflection] + +说明: + - 走本地 OCCT 服务的 /convert 接口(需先启动 occt_service.py) + - 同名 .step → .glb,断点续传(已存在且非空的 .glb 跳过) + - 单线程、逐文件,避免给服务造成压力 + - 默认 deflection=0.5(屏幕预览够用) +""" +import os +import sys +import time +import urllib.request + +OCCT_URL = os.environ.get("OCCT_URL", "http://localhost:8090") + + +def convert_one(step_path, glb_path, deflection): + with open(step_path, "rb") as f: + data = f.read() + req = urllib.request.Request( + "%s/convert?deflection=%s" % (OCCT_URL, deflection), + data=data, method="POST", + headers={"Content-Type": "application/octet-stream"}, + ) + resp = urllib.request.urlopen(req, timeout=120) + out = resp.read() + if not out or out[:4] != b"glTF": + raise RuntimeError("转换失败(非法 glb): %s" % step_path) + # 原子写:先写临时再改名 + tmp = glb_path + ".tmp" + with open(tmp, "wb") as f: + f.write(out) + os.replace(tmp, glb_path) + + +def main(): + if len(sys.argv) < 3: + print("用法: python batch-convert-glb.py [deflection]") + sys.exit(1) + step_dir = sys.argv[1] + out_dir = sys.argv[2] + deflection = float(sys.argv[3]) if len(sys.argv) > 3 else 0.5 + os.makedirs(out_dir, exist_ok=True) + + steps = [] + for root, _, files in os.walk(step_dir): + for fn in files: + if fn.lower().endswith((".step", ".stp")): + steps.append(os.path.join(root, fn)) + steps.sort() + + ok = 0 + skip = 0 + fail = 0 + t0 = time.time() + for i, sp in enumerate(steps): + base = os.path.splitext(os.path.basename(sp))[0] + gp = os.path.join(out_dir, base + ".glb") + if os.path.exists(gp) and os.path.getsize(gp) > 0: + skip += 1 + continue + try: + convert_one(sp, gp, deflection) + ok += 1 + if (ok + skip) % 50 == 0: + print("[%d/%d] ok=%d skip=%d fail=%d %.1fs" % (i + 1, len(steps), ok, skip, fail, time.time() - t0)) + except Exception as e: + fail += 1 + print("[FAIL] %s: %s" % (os.path.basename(sp), e)) + + print("完成: 总 %d, 转换 %d, 跳过 %d, 失败 %d, 耗时 %.1fs" % (len(steps), ok, skip, fail, time.time() - t0)) + if fail: + sys.exit(2) + + +if __name__ == "__main__": + main()