FEAT - BIM CONVERTER: поддержка IGES через Ops
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
import worker
|
||||
|
||||
|
||||
class CadFormatTests(unittest.TestCase):
|
||||
def test_source_format_variants(self) -> None:
|
||||
self.assertEqual(worker.source_format_for_path(Path("model.STEP")), "step")
|
||||
self.assertEqual(worker.source_format_for_path(Path("model.stp")), "step")
|
||||
self.assertEqual(worker.source_format_for_path(Path("model.IGES")), "iges")
|
||||
self.assertEqual(worker.source_format_for_path(Path("model.igs")), "iges")
|
||||
self.assertIsNone(worker.source_format_for_path(Path("model.glb")))
|
||||
|
||||
def test_process_one_dispatches_iges_converter(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
worker.UPLOADS_DIR = Path(directory).resolve()
|
||||
source_path = worker.UPLOADS_DIR / "model.igs"
|
||||
source_path.write_bytes(b"IGES")
|
||||
|
||||
metadata = {
|
||||
"artifact": "uploads/model.xkt",
|
||||
"artifactType": "xkt",
|
||||
"componentCount": 1,
|
||||
}
|
||||
with (
|
||||
patch.object(worker, "find_ready_conversion_with_same_hash", return_value=None),
|
||||
patch.object(worker, "convert_iges_assets", return_value=metadata) as convert_iges,
|
||||
):
|
||||
self.assertTrue(worker.process_one(source_path))
|
||||
|
||||
convert_iges.assert_called_once()
|
||||
manifest = worker.read_json(worker.manifest_path(source_path))
|
||||
self.assertEqual(manifest["sourceFormat"], "iges")
|
||||
self.assertEqual(manifest["status"], "ready")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
+82
-20
@@ -13,6 +13,7 @@ from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from OCP.BRepMesh import BRepMesh_IncrementalMesh
|
||||
from OCP.IGESCAFControl import IGESCAFControl_Reader
|
||||
from OCP.Message import Message_ProgressRange
|
||||
from OCP.RWGltf import RWGltf_CafWriter
|
||||
from OCP.STEPCAFControl import STEPCAFControl_Reader
|
||||
@@ -25,7 +26,7 @@ from OCP.XCAFDoc import XCAFDoc_DocumentTool, XCAFDoc_ShapeTool
|
||||
|
||||
|
||||
CONVERTER_NAME = "NodeDcBimConverter"
|
||||
CONVERTER_VERSION = "0.4.9"
|
||||
CONVERTER_VERSION = "0.5.0"
|
||||
UPLOADS_DIR = Path(os.environ.get("NODEDC_BIM_CONVERTER_UPLOADS_DIR", "/beam/uploads")).resolve()
|
||||
POLL_INTERVAL_SECONDS = float(os.environ.get("NODEDC_BIM_CONVERTER_INTERVAL_SECONDS", "10"))
|
||||
PROCESS_ONCE = os.environ.get("NODEDC_BIM_CONVERTER_ONCE", "").lower() in {"1", "true", "yes"}
|
||||
@@ -73,6 +74,11 @@ CADQUERY_TIMEOUT_SECONDS = float(
|
||||
os.environ.get("NODEDC_BIM_CONVERTER_CADQUERY_TIMEOUT_SECONDS", str(CONVERSION_TIMEOUT_SECONDS))
|
||||
)
|
||||
STEP_EXTENSIONS = {".step", ".stp"}
|
||||
IGES_EXTENSIONS = {".iges", ".igs"}
|
||||
CAD_SOURCE_FORMAT_BY_EXTENSION = {
|
||||
**{extension: "step" for extension in STEP_EXTENSIONS},
|
||||
**{extension: "iges" for extension in IGES_EXTENSIONS},
|
||||
}
|
||||
GLB_EXTENSIONS = {".glb"}
|
||||
GLB_LOD_PROFILES = [
|
||||
{
|
||||
@@ -351,6 +357,9 @@ def find_ready_conversion_with_same_hash(source_path: Path, manifest: dict[str,
|
||||
sha256 = manifest.get("sha256") or calculate_file_sha256(source_path)
|
||||
if not sha256:
|
||||
return None
|
||||
source_format = source_format_for_path(source_path)
|
||||
if not source_format:
|
||||
return None
|
||||
version_id = manifest.get("versionId")
|
||||
version_number = manifest.get("version")
|
||||
source_manifest_path = manifest_path(source_path).resolve()
|
||||
@@ -373,7 +382,7 @@ def find_ready_conversion_with_same_hash(source_path: Path, manifest: dict[str,
|
||||
update_asset_manifest_version(candidate_source_path, candidate)
|
||||
if candidate_sha256 != sha256:
|
||||
continue
|
||||
if candidate.get("sourceFormat") != "step":
|
||||
if candidate.get("sourceFormat") != source_format:
|
||||
continue
|
||||
if candidate.get("artifactType") != "xkt" and candidate.get("targetFormat") != "xkt":
|
||||
continue
|
||||
@@ -659,27 +668,43 @@ def gltf_component_tree(gltf_json: dict[str, Any], fallback_name: str) -> dict[s
|
||||
}
|
||||
|
||||
|
||||
def convert_step_xcaf_to_glb(source_path: Path, glb_path: Path) -> tuple[dict[str, Any], dict[str, Any]]:
|
||||
def source_format_for_path(source_path: Path) -> str | None:
|
||||
return CAD_SOURCE_FORMAT_BY_EXTENSION.get(source_path.suffix.lower())
|
||||
|
||||
|
||||
def convert_cad_xcaf_to_glb(
|
||||
source_path: Path,
|
||||
glb_path: Path,
|
||||
source_format: str,
|
||||
) -> tuple[dict[str, Any], dict[str, Any]]:
|
||||
app = XCAFApp_Application.GetApplication_s()
|
||||
doc = TDocStd_Document(TCollection_ExtendedString("MDTV-XCAF"))
|
||||
app.NewDocument(TCollection_ExtendedString("MDTV-XCAF"), doc)
|
||||
|
||||
print(f"[{CONVERTER_NAME}] STEPCAF read {source_path.name}", flush=True)
|
||||
reader = STEPCAFControl_Reader()
|
||||
if source_format == "step":
|
||||
reader = STEPCAFControl_Reader()
|
||||
reader_name = "STEPCAF"
|
||||
elif source_format == "iges":
|
||||
reader = IGESCAFControl_Reader()
|
||||
reader_name = "IGESCAF"
|
||||
else:
|
||||
raise ValueError(f"Unsupported CAD source format: {source_format}")
|
||||
|
||||
print(f"[{CONVERTER_NAME}] {reader_name} read {source_path.name}", flush=True)
|
||||
reader.SetNameMode(True)
|
||||
reader.SetColorMode(True)
|
||||
reader.SetLayerMode(True)
|
||||
read_status = reader.ReadFile(str(source_path))
|
||||
if "RetDone" not in str(read_status):
|
||||
raise ValueError(f"STEPCAF read failed: {read_status}")
|
||||
raise ValueError(f"{reader_name} read failed: {read_status}")
|
||||
if not reader.Transfer(doc):
|
||||
raise ValueError("STEPCAF transfer failed.")
|
||||
raise ValueError(f"{reader_name} transfer failed.")
|
||||
|
||||
shape_tool = XCAFDoc_DocumentTool.ShapeTool_s(doc.Main())
|
||||
labels = TDF_LabelSequence()
|
||||
shape_tool.GetFreeShapes(labels)
|
||||
if labels.Length() == 0:
|
||||
raise ValueError("STEPCAF document has no free shapes.")
|
||||
raise ValueError(f"{reader_name} document has no free shapes.")
|
||||
|
||||
print(
|
||||
f"[{CONVERTER_NAME}] meshing {source_path.name}: "
|
||||
@@ -710,6 +735,7 @@ def convert_step_xcaf_to_glb(source_path: Path, glb_path: Path) -> tuple[dict[st
|
||||
gltf_json, renamed_nodes = normalize_glb_node_names(glb_path)
|
||||
tree = gltf_component_tree(gltf_json, source_path.stem)
|
||||
stats = {
|
||||
"sourceFormat": source_format,
|
||||
"freeShapeCount": labels.Length(),
|
||||
"gltfNodeCount": len(gltf_json.get("nodes", [])),
|
||||
"gltfMeshCount": len(gltf_json.get("meshes", [])),
|
||||
@@ -841,7 +867,7 @@ def convert_glb_to_xkt(glb_path: Path, xkt_path: Path) -> dict[str, Any]:
|
||||
}
|
||||
|
||||
|
||||
def build_step_metadata(
|
||||
def build_cad_metadata(
|
||||
source_path: Path,
|
||||
glb_path: Path,
|
||||
xkt_path: Path,
|
||||
@@ -850,6 +876,7 @@ def build_step_metadata(
|
||||
tree: dict[str, Any],
|
||||
stats: dict[str, Any],
|
||||
import_strategy: str,
|
||||
source_format: str,
|
||||
) -> dict[str, Any]:
|
||||
xkt_stats = convert_glb_to_xkt(glb_path, xkt_path)
|
||||
has_xkt = bool(xkt_stats.get("enabled") and xkt_path.exists())
|
||||
@@ -862,7 +889,7 @@ def build_step_metadata(
|
||||
"artifactType": preferred_artifact_type,
|
||||
"glbArtifact": src_from_path(glb_path),
|
||||
"xktArtifact": src_from_path(xkt_path) if has_xkt else None,
|
||||
"format": "step",
|
||||
"format": source_format,
|
||||
"targetFormat": "xkt" if has_xkt else "glb",
|
||||
"importStrategy": import_strategy,
|
||||
"componentTree": tree,
|
||||
@@ -881,9 +908,14 @@ def build_step_metadata(
|
||||
return metadata
|
||||
|
||||
|
||||
def convert_step_xcaf_to_glb_process(source_path: str, glb_path: str, result_path: str) -> None:
|
||||
def convert_cad_xcaf_to_glb_process(
|
||||
source_path: str,
|
||||
glb_path: str,
|
||||
source_format: str,
|
||||
result_path: str,
|
||||
) -> None:
|
||||
try:
|
||||
tree, stats = convert_step_xcaf_to_glb(Path(source_path), Path(glb_path))
|
||||
tree, stats = convert_cad_xcaf_to_glb(Path(source_path), Path(glb_path), source_format)
|
||||
write_json_atomic(Path(result_path), {"ok": True, "tree": tree, "stats": stats})
|
||||
except Exception as exc:
|
||||
write_json_atomic(Path(result_path), {"ok": False, "error": str(exc)})
|
||||
@@ -960,7 +992,7 @@ def run_cadquery_stage_with_timeout(result_path: Path, source_path: Path, glb_pa
|
||||
return result
|
||||
|
||||
|
||||
def run_step_glb_stage_with_timeout(
|
||||
def run_cad_glb_stage_with_timeout(
|
||||
stage_name: str,
|
||||
timeout_seconds: float,
|
||||
target: Any,
|
||||
@@ -1031,7 +1063,7 @@ def convert_step_assets(source_path: Path, glb_path: Path, xkt_path: Path, metad
|
||||
"renamedNodeCount": (previous_stats.get("renamedNodeCount") or 0) + renamed_nodes,
|
||||
"reusedExistingGlb": True,
|
||||
}
|
||||
return build_step_metadata(
|
||||
return build_cad_metadata(
|
||||
source_path,
|
||||
glb_path,
|
||||
xkt_path,
|
||||
@@ -1039,18 +1071,20 @@ def convert_step_assets(source_path: Path, glb_path: Path, xkt_path: Path, metad
|
||||
tree=previous_metadata["componentTree"],
|
||||
stats=stats,
|
||||
import_strategy=previous_metadata.get("importStrategy") or "existing-glb",
|
||||
source_format="step",
|
||||
)
|
||||
|
||||
import_strategy = "occt-xcaf-gltf"
|
||||
result_path = metadata_path.with_name(f"{metadata_path.name}.stage-result.json")
|
||||
try:
|
||||
result = run_step_glb_stage_with_timeout(
|
||||
result = run_cad_glb_stage_with_timeout(
|
||||
"OCCT/XCAF",
|
||||
XCAF_TIMEOUT_SECONDS,
|
||||
convert_step_xcaf_to_glb_process,
|
||||
convert_cad_xcaf_to_glb_process,
|
||||
result_path,
|
||||
str(source_path),
|
||||
str(glb_path),
|
||||
"step",
|
||||
)
|
||||
tree = result["tree"]
|
||||
stats = result["stats"]
|
||||
@@ -1072,7 +1106,7 @@ def convert_step_assets(source_path: Path, glb_path: Path, xkt_path: Path, metad
|
||||
"fallbackReason": str(exc),
|
||||
}
|
||||
|
||||
return build_step_metadata(
|
||||
return build_cad_metadata(
|
||||
source_path,
|
||||
glb_path,
|
||||
xkt_path,
|
||||
@@ -1080,6 +1114,30 @@ def convert_step_assets(source_path: Path, glb_path: Path, xkt_path: Path, metad
|
||||
tree=tree,
|
||||
stats=stats,
|
||||
import_strategy=import_strategy,
|
||||
source_format="step",
|
||||
)
|
||||
|
||||
|
||||
def convert_iges_assets(source_path: Path, glb_path: Path, xkt_path: Path, metadata_path: Path) -> dict[str, Any]:
|
||||
result_path = metadata_path.with_name(f"{metadata_path.name}.stage-result.json")
|
||||
result = run_cad_glb_stage_with_timeout(
|
||||
"IGES OCCT/XCAF",
|
||||
XCAF_TIMEOUT_SECONDS,
|
||||
convert_cad_xcaf_to_glb_process,
|
||||
result_path,
|
||||
str(source_path),
|
||||
str(glb_path),
|
||||
"iges",
|
||||
)
|
||||
return build_cad_metadata(
|
||||
source_path,
|
||||
glb_path,
|
||||
xkt_path,
|
||||
metadata_path,
|
||||
tree=result["tree"],
|
||||
stats=result["stats"],
|
||||
import_strategy="occt-xcaf-gltf",
|
||||
source_format="iges",
|
||||
)
|
||||
|
||||
|
||||
@@ -1113,6 +1171,9 @@ def process_one(source_path: Path) -> bool:
|
||||
xkt_path = source_path.with_name(f"{stem}.xkt")
|
||||
metadata_path = source_path.with_name(f"{stem}.metadata.json")
|
||||
manifest = read_json(manifest_file)
|
||||
source_format = source_format_for_path(source_path)
|
||||
if not source_format:
|
||||
return False
|
||||
|
||||
if not should_process(source_path, manifest, glb_path, xkt_path):
|
||||
return False
|
||||
@@ -1140,7 +1201,7 @@ def process_one(source_path: Path) -> bool:
|
||||
"componentTreeRequired": True,
|
||||
"converterName": CONVERTER_NAME,
|
||||
"converterVersion": CONVERTER_VERSION,
|
||||
"sourceFormat": "step",
|
||||
"sourceFormat": source_format,
|
||||
"sourceSrc": src_from_path(source_path),
|
||||
"targetFormat": "xkt" if XKT_ENABLED else "glb",
|
||||
}
|
||||
@@ -1215,7 +1276,8 @@ def process_one(source_path: Path) -> bool:
|
||||
print(f"[{CONVERTER_NAME}] converting {source_path}", flush=True)
|
||||
|
||||
try:
|
||||
metadata = convert_step_assets(source_path, glb_path, xkt_path, metadata_path)
|
||||
converter = convert_step_assets if source_format == "step" else convert_iges_assets
|
||||
metadata = converter(source_path, glb_path, xkt_path, metadata_path)
|
||||
artifact_src = metadata.get("artifact") or src_from_path(xkt_path if xkt_path.exists() else glb_path)
|
||||
artifact_type = metadata.get("artifactType") or ("xkt" if xkt_path.exists() else "gltf")
|
||||
ready_manifest = {
|
||||
@@ -1943,7 +2005,7 @@ def scan_once() -> int:
|
||||
continue
|
||||
suffix = source_path.suffix.lower()
|
||||
did_process = False
|
||||
if suffix in STEP_EXTENSIONS:
|
||||
if suffix in CAD_SOURCE_FORMAT_BY_EXTENSION:
|
||||
did_process = process_one(source_path)
|
||||
elif suffix in GLB_EXTENSIONS:
|
||||
did_process = convert_glb_to_xkt_one(source_path)
|
||||
|
||||
Reference in New Issue
Block a user