Files
pythonocc-step-editor/step_editor/isolated_edit_worker.py
T

105 lines
4.1 KiB
Python
Raw Normal View History

from __future__ import annotations
import argparse
import json
import traceback
from pathlib import Path
from .model import StepModel
def _execute(model: StepModel, operation: str, args: list[object]) -> str:
if operation == "push_pull_face":
return model.push_pull_face(int(args[0]), float(args[1]))
if operation == "move_face_plane_offset_local":
return model.move_face_plane_offset_local(int(args[0]), float(args[1]))
if operation == "resize_face_area_local":
return model.resize_face_area_local(int(args[0]), float(args[1]))
if operation == "resize_face_area":
return model.resize_face_area(int(args[0]), float(args[1]))
if operation == "resize_face_size_local":
return model.resize_face_size_local(int(args[0]), float(args[1]), str(args[2]))
if operation == "resize_face_size_owning_scale":
return model.resize_face_size_owning_scale(int(args[0]), float(args[1]), str(args[2]))
if operation == "move_face_center_local":
center = list(args[1])
if len(center) != 3:
raise ValueError("move_face_center_local requires a 3D target center.")
return model.move_face_center_local(
int(args[0]),
(float(center[0]), float(center[1]), float(center[2])),
)
if operation == "resize_shell_thickness":
return model.resize_shell_thickness(int(args[0]), float(args[1]))
if operation == "resize_shell_thickness_owning_scale":
return model.resize_shell_thickness_owning_scale(int(args[0]), float(args[1]))
if operation == "resize_cylindrical_height":
return model.resize_cylindrical_height(int(args[0]), float(args[1]))
if operation == "resize_cylindrical_boss_height":
return model.resize_cylindrical_boss_height(int(args[0]), float(args[1]))
if operation == "resize_cylindrical_height_owning_scale":
return model.resize_cylindrical_height_owning_scale(int(args[0]), float(args[1]))
if operation == "resize_cone_reference_radius":
return model.resize_conical_reference_radius(int(args[0]), float(args[1]))
if operation == "resize_cone_semi_angle":
return model.resize_conical_semi_angle(int(args[0]), float(args[1]))
if operation == "resize_sphere_radius":
return model.resize_spherical_radius(int(args[0]), float(args[1]))
if operation == "resize_torus_radius":
return model.resize_toroidal_radius(int(args[0]), float(args[1]), str(args[2]))
raise ValueError(f"Unsupported isolated edit operation: {operation}")
def run_request(request: str | Path) -> int:
request_path = Path(request)
response_path = request_path.with_suffix(".response.json")
try:
request = json.loads(request_path.read_text(encoding="utf-8-sig"))
input_path = Path(str(request["input_path"]))
output_path = Path(str(request["output_path"]))
operation = str(request["operation"])
args = list(request.get("args") or [])
model = StepModel.load(input_path)
message = _execute(model, operation, args)
model.export_all(output_path)
response_path.write_text(
json.dumps(
{
"ok": True,
"message": message,
"stats": model.stats().__dict__,
"output_path": str(output_path),
},
ensure_ascii=False,
indent=2,
),
encoding="utf-8",
)
return 0
except Exception as exc:
response_path.write_text(
json.dumps(
{
"ok": False,
"error": str(exc),
"traceback": traceback.format_exc(),
},
ensure_ascii=False,
indent=2,
),
encoding="utf-8",
)
return 2
def main() -> int:
parser = argparse.ArgumentParser(description="Run one high-risk geometry edit in an isolated process.")
parser.add_argument("request", help="JSON request file.")
parsed = parser.parse_args()
return run_request(parsed.request)
if __name__ == "__main__":
raise SystemExit(main())