x
This commit is contained in:
@@ -26,6 +26,7 @@ FreeCAD 导入建议:
|
||||
"""
|
||||
|
||||
import os
|
||||
import re
|
||||
from typing import Dict, List, Any, Optional, Tuple
|
||||
from pathlib import Path
|
||||
from utils.logger import get_logger
|
||||
@@ -40,6 +41,35 @@ class CADExporter:
|
||||
self.output_dir = output_dir
|
||||
os.makedirs(output_dir, exist_ok=True)
|
||||
|
||||
@staticmethod
|
||||
def _safe_segment(value: Optional[str], fallback: str) -> str:
|
||||
text = str(value or "").strip()
|
||||
if not text:
|
||||
text = fallback
|
||||
text = re.sub(r"[^A-Za-z0-9._-]+", "_", text)
|
||||
return text[:80] or fallback
|
||||
|
||||
def build_export_dir(
|
||||
self,
|
||||
base_filename: str,
|
||||
task_id: Optional[str] = None,
|
||||
scheme_id: Optional[str] = None,
|
||||
) -> str:
|
||||
if task_id:
|
||||
task_segment = self._safe_segment(task_id, "task")
|
||||
scheme_segment = self._safe_segment(scheme_id, "default")
|
||||
return os.path.join(self.output_dir, task_segment, scheme_segment)
|
||||
return os.path.join(self.output_dir, self._safe_segment(base_filename, "mold"))
|
||||
|
||||
def get_relative_path(self, filepath: str) -> str:
|
||||
full_path = Path(filepath).resolve()
|
||||
output_root = Path(self.output_dir).resolve()
|
||||
try:
|
||||
relative = full_path.relative_to(output_root)
|
||||
except ValueError:
|
||||
relative = Path(os.path.basename(filepath))
|
||||
return relative.as_posix()
|
||||
|
||||
def export_step(self, shape: Any, filepath: str,
|
||||
schema: str = "AP214") -> bool:
|
||||
"""
|
||||
@@ -207,7 +237,9 @@ class CADExporter:
|
||||
def export_mold_results(self, cavity_data: Dict,
|
||||
base_filename: str,
|
||||
formats: List[str] = None,
|
||||
components: List[str] = None) -> Dict[str, Any]:
|
||||
components: List[str] = None,
|
||||
task_id: Optional[str] = None,
|
||||
scheme_id: Optional[str] = None) -> Dict[str, Any]:
|
||||
"""
|
||||
批量导出模具设计结果
|
||||
|
||||
@@ -225,11 +257,17 @@ class CADExporter:
|
||||
if components is None:
|
||||
components = ["cavity", "core"]
|
||||
|
||||
export_dir = os.path.join(self.output_dir, base_filename)
|
||||
export_dir = self.build_export_dir(
|
||||
base_filename=base_filename,
|
||||
task_id=task_id,
|
||||
scheme_id=scheme_id,
|
||||
)
|
||||
os.makedirs(export_dir, exist_ok=True)
|
||||
|
||||
results = {
|
||||
"base_filename": base_filename,
|
||||
"task_id": task_id,
|
||||
"scheme_id": scheme_id,
|
||||
"export_dir": export_dir,
|
||||
"files": [],
|
||||
"errors": [],
|
||||
@@ -277,11 +315,13 @@ class CADExporter:
|
||||
|
||||
if success:
|
||||
file_size = os.path.getsize(filepath)
|
||||
relative_path = self.get_relative_path(filepath)
|
||||
results["files"].append({
|
||||
"component": comp_name,
|
||||
"component_label": label,
|
||||
"format": fmt,
|
||||
"filepath": filepath,
|
||||
"relative_path": relative_path,
|
||||
"filename": os.path.basename(filepath),
|
||||
"size_bytes": file_size,
|
||||
"size_readable": self._format_file_size(file_size),
|
||||
|
||||
Reference in New Issue
Block a user