TellmeStpToGlb/app.py
sladro b5e18c5c7a feat: 重构转换系统,默认启用层级转换和组件名称保留
## 主要变更
- 创建新的STP文件文本解析器,直接读取文件内容提取组件名称
- 简化CLI和API接口,移除层级和名称保留参数
- 所有转换默认使用层级模式并保留原始组件名称
- 重构转换器类,清理冗余代码

## 技术改进
- 新增 core/stp_parser.py:直接解析PRODUCT和PRODUCT_DEFINITION实体
- 优化 converter.py:统一转换流程,默认层级转换
- 更新 main.py:简化命令行参数,移除 --hierarchy 和 --preserve-names
- 修复 API 错误处理:全局异常处理器返回正确的JSON响应
- 完善 API 精度参数传递:支持自定义精度选项

## 接口变更
- CLI:`python main.py input.stp output.glb` 即可获得层级转换结果
- API:移除 preserve_hierarchy 和 preserve_original_names 参数
- 保持向后兼容:原有的基本用法不变

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-15 10:25:14 +08:00

92 lines
2.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""
FastAPI应用入口
STP到GLB转换服务
"""
import asyncio
from contextlib import asynccontextmanager
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from api.routes import router
from services.task_manager import task_manager
@asynccontextmanager
async def lifespan(app: FastAPI):
"""应用生命周期管理"""
# 启动时的初始化
print("🚀 STP到GLB转换服务启动中...")
# 启动定期清理任务
cleanup_task = asyncio.create_task(periodic_cleanup())
yield
# 关闭时的清理
print("🛑 STP到GLB转换服务关闭中...")
cleanup_task.cancel()
try:
await cleanup_task
except asyncio.CancelledError:
pass
async def periodic_cleanup():
"""定期清理已完成的任务"""
while True:
try:
await asyncio.sleep(300) # 每5分钟清理一次
task_manager.cleanup_completed_tasks(max_tasks=100)
except asyncio.CancelledError:
break
except Exception as e:
print(f"定期清理任务出错: {e}")
# 创建FastAPI应用
app = FastAPI(
title="STP到GLB转换服务",
description="提供STP格式到GLB格式的3D模型转换服务",
version="1.0.0",
lifespan=lifespan
)
# 配置CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # 在生产环境中应该限制具体的域名
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# 注册路由
app.include_router(router)
# 全局异常处理
@app.exception_handler(Exception)
async def global_exception_handler(request, exc):
"""全局异常处理器"""
print(f"未处理的异常: {exc}")
return JSONResponse(
status_code=500,
content={"detail": "服务器内部错误"}
)
if __name__ == "__main__":
import uvicorn
print("启动STP到GLB转换服务...")
print("API文档地址: http://localhost:8000/docs")
print("健康检查: http://localhost:8000/health")
uvicorn.run(
"app:app",
host="0.0.0.0",
port=8000,
reload=True, # 开发模式生产环境应设为False
log_level="info"
)