TellmeStpToGlb/main.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

86 lines
3.4 KiB
Python
Raw 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
"""
stp2glb.py 一键 STP → STL → GLB
依赖pythonocc-core + trimesh
用法python main.py model.stp model.glb
"""
import sys
import argparse
from core.converter import StpToGlbConverter
# 质量等级到精度参数的映射
QUALITY_PRESETS = {
'low': (0.1, 0.5),
'medium': (0.01, 0.1),
'high': (0.005, 0.05),
'ultra': (0.001, 0.02)
}
def main():
"""CLI主函数默认使用层级转换并保留原始名称"""
# 兼容原有的简单用法
if len(sys.argv) == 3 and not any(arg.startswith('-') for arg in sys.argv[1:]):
stp_file, glb_file = sys.argv[1], sys.argv[2]
linear_deflection = 0.01
angular_deflection = 0.1
auto_scale = True
auto_center = True
else:
# 使用argparse处理参数
parser = argparse.ArgumentParser(description='STP到GLB转换工具 - 默认使用层级转换并保留原始组件名称')
parser.add_argument('input', help='输入的STP文件路径')
parser.add_argument('output', help='输出的GLB文件路径')
parser.add_argument('--linear-deflection', type=float, default=0.01,
help='线性偏差 (默认: 0.01)')
parser.add_argument('--angular-deflection', type=float, default=0.1,
help='角度偏差 (默认: 0.1)')
parser.add_argument('--quality', choices=['low', 'medium', 'high', 'ultra', 'custom'],
default='medium', help='网格质量等级 (默认: medium)')
parser.add_argument('--no-scale', action='store_true',
help='禁用自动缩放')
parser.add_argument('--no-center', action='store_true',
help='禁用自动居中')
args = parser.parse_args()
stp_file = args.input
glb_file = args.output
auto_scale = not args.no_scale
auto_center = not args.no_center
# 处理精度参数
if args.quality == 'custom':
linear_deflection = args.linear_deflection
angular_deflection = args.angular_deflection
else:
linear_deflection, angular_deflection = QUALITY_PRESETS[args.quality]
# 创建转换器实例
converter = StpToGlbConverter()
# 设置进度回调CLI模式显示简单进度
def show_progress(progress: int, message: str):
# 显示关键进度和调试信息
if progress % 20 == 0 or progress == 100 or "参数:" in message or "提取" in message or "异常" in message or "成功" in message:
print(f"[{progress:3d}%] {message}")
converter.set_progress_callback(show_progress)
try:
# 显示转换参数
print(f"📁 输入文件: {stp_file}")
print(f"📁 输出文件: {glb_file}")
print(f"🎯 精度设置: 线性={linear_deflection}, 角度={angular_deflection}")
# 默认使用层级转换并保留原始名称
print("🔧 使用层级转换模式(保留原始组件名称)...")
converter.convert(stp_file, glb_file, auto_scale, auto_center,
linear_deflection, angular_deflection)
print(f"✅ 完成:{glb_file}")
except Exception as e:
sys.exit(f"❌ 转换失败:{e}")
if __name__ == "__main__":
main()