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])) raise ValueError(f"Unsupported isolated edit operation: {operation}") 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() request_path = Path(parsed.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 if __name__ == "__main__": raise SystemExit(main())