x
This commit is contained in:
@@ -4,6 +4,7 @@ import os
|
||||
from fastapi import APIRouter, Depends, HTTPException, Request
|
||||
|
||||
from services.auth_service import get_current_active_user
|
||||
from services.redis_task_manager import redis_task_manager
|
||||
from models.database import User
|
||||
from utils.logger import get_logger
|
||||
from api.routes import (
|
||||
@@ -24,6 +25,16 @@ logger = get_logger(__name__)
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
async def _get_task_data(task_id: str) -> dict:
|
||||
"""从 Redis 优先查找任务,回退到旧版内存字典"""
|
||||
task = await redis_task_manager.get_task(task_id)
|
||||
if task:
|
||||
return task
|
||||
if task_id in tasks:
|
||||
return tasks[task_id]
|
||||
return None
|
||||
|
||||
|
||||
@router.post("/optimize-layout")
|
||||
async def optimize_cavity_layout(
|
||||
request: Request,
|
||||
@@ -140,10 +151,13 @@ async def ai_parting_surface_detect(
|
||||
body = await request.json()
|
||||
task_id = body.get("task_id")
|
||||
|
||||
if not task_id or task_id not in tasks:
|
||||
if not task_id:
|
||||
raise HTTPException(404, "缺少 task_id")
|
||||
|
||||
task_data = await _get_task_data(task_id)
|
||||
if not task_data:
|
||||
raise HTTPException(404, "任务不存在")
|
||||
|
||||
task_data = tasks[task_id]
|
||||
geometry_data = task_data.get("geometry_data")
|
||||
if not geometry_data:
|
||||
raise HTTPException(400, "该任务尚未完成几何分析")
|
||||
@@ -166,10 +180,13 @@ async def detect_undercuts(
|
||||
parting_direction = body.get("parting_direction", [0, 0, 1])
|
||||
mold_size = body.get("mold_size", {"length": 300, "width": 300, "height": 200})
|
||||
|
||||
if not task_id or task_id not in tasks:
|
||||
if not task_id:
|
||||
raise HTTPException(404, "缺少 task_id")
|
||||
|
||||
task_data = await _get_task_data(task_id)
|
||||
if not task_data:
|
||||
raise HTTPException(404, "任务不存在")
|
||||
|
||||
task_data = tasks[task_id]
|
||||
geometry_data = task_data.get("geometry_data")
|
||||
if not geometry_data:
|
||||
raise HTTPException(400, "该任务尚未完成几何分析")
|
||||
@@ -288,15 +305,25 @@ async def export_mold_results(
|
||||
formats = body.get("formats", ["step", "stl"])
|
||||
components = body.get("components", ["cavity", "core"])
|
||||
|
||||
if not task_id or task_id not in tasks:
|
||||
if not task_id:
|
||||
raise HTTPException(404, "缺少 task_id")
|
||||
|
||||
task_data = await _get_task_data(task_id)
|
||||
if not task_data:
|
||||
raise HTTPException(404, "任务不存在")
|
||||
|
||||
task_data = tasks[task_id]
|
||||
cavity_shapes = task_data.get("cavity_shapes")
|
||||
if not cavity_shapes:
|
||||
raise HTTPException(400, "该任务尚未完成模具生成或形状数据不可用")
|
||||
filename = task_data.get("filename", f"mold_{task_id}")
|
||||
|
||||
base_filename = Path(task_data.get("filename", f"mold_{task_id}")).stem
|
||||
if not cavity_shapes:
|
||||
file_path = task_data.get("file_path")
|
||||
if file_path and os.path.exists(str(file_path)):
|
||||
cavity_shapes = await _reparse_stp_for_export(str(file_path), task_data.get("material", "ABS"))
|
||||
|
||||
if not cavity_shapes:
|
||||
raise HTTPException(400, "该任务尚未完成模具生成或形状数据不可用,请等待处理完成后再导出")
|
||||
|
||||
base_filename = Path(filename).stem
|
||||
result = cad_exporter.export_mold_results(
|
||||
cavity_data=cavity_shapes,
|
||||
base_filename=base_filename,
|
||||
@@ -350,3 +377,21 @@ async def get_export_recommendations(
|
||||
"""获取导出格式建议(UG/FreeCAD/SolidWorks)"""
|
||||
result = cad_exporter.get_export_recommendations(target)
|
||||
return {"status": "success", "data": result}
|
||||
|
||||
|
||||
async def _reparse_stp_for_export(file_path: str, material: str = "ABS") -> dict:
|
||||
"""从 STP 文件重新生成模具型腔数据用于导出"""
|
||||
try:
|
||||
from core.stp_parser import STPParser
|
||||
from core.mold_generator import MoldCavityGenerator
|
||||
|
||||
stp_parser = STPParser()
|
||||
shape = stp_parser.load_step_file(Path(file_path))
|
||||
mold_gen = MoldCavityGenerator(shrinkage_rate=0.005)
|
||||
mold_gen.set_material(material)
|
||||
cavity_result = mold_gen.generate_mold_cavities(shape)
|
||||
logger.info(f"重新解析 STP 用于导出: {file_path}")
|
||||
return cavity_result
|
||||
except Exception as e:
|
||||
logger.warning(f"重新解析 STP 导出失败: {e}")
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user