This commit is contained in:
2026-03-08 15:49:41 +08:00
parent 22496d2b5e
commit 9a6585b72e
2 changed files with 20 additions and 13 deletions
+3 -2
View File
@@ -52,7 +52,7 @@ def verify_with_freecad(stp_path):
# 方法1: Import.insert (FreeCAD 0.21+) # 方法1: Import.insert (FreeCAD 0.21+)
try: try:
Import.insert(stp_path) Import.insert(stp_path, doc.Name)
imported = True imported = True
print("使用 Import.insert 导入成功") print("使用 Import.insert 导入成功")
except Exception as e1: except Exception as e1:
@@ -63,6 +63,7 @@ def verify_with_freecad(stp_path):
try: try:
Import.open(stp_path) Import.open(stp_path)
imported = True imported = True
doc = FreeCAD.ActiveDocument
print("使用 Import.open 导入成功") print("使用 Import.open 导入成功")
except Exception as e2: except Exception as e2:
print(f"Import.open 失败: {e2}") print(f"Import.open 失败: {e2}")
@@ -81,7 +82,7 @@ def verify_with_freecad(stp_path):
# 方法4: Part.insert (旧版本) # 方法4: Part.insert (旧版本)
if not imported: if not imported:
try: try:
Part.insert(stp_path) Part.insert(stp_path, doc.Name)
imported = True imported = True
print("使用 Part.insert 导入成功") print("使用 Part.insert 导入成功")
except Exception as e4: except Exception as e4:
+17 -11
View File
@@ -69,14 +69,14 @@ class GeometryVerificationService:
cmd = None cmd = None
# 尝试多种 FreeCAD 命令名称 # 尝试多种 FreeCAD 命令名称
# PPA 版本通常使用 freecad 或 freecadcmd # AppImage 版本使用 freecad
# snap 版本使用 freecad.cmd # PPA 版本通常使用 freecadcmd
for cmd_name in ['freecadcmd', 'freecad-cmd', 'freecad', '/usr/bin/freecad', '/usr/bin/freecadcmd', '/snap/bin/freecad.cmd']: for cmd_name in ['freecad', 'freecadcmd', 'freecad-cmd', '/usr/local/bin/freecad', '/usr/bin/freecad', '/snap/bin/freecad.cmd']:
try: try:
result = subprocess.run(['which', cmd_name], capture_output=True, text=True) result = subprocess.run(['which', cmd_name], capture_output=True, text=True)
if result.returncode == 0 and result.stdout.strip(): if result.returncode == 0 and result.stdout.strip():
# 使用绝对路径 # 使用绝对路径,添加 -c 参数使用命令行模式(无图形界面)
cmd = [cmd_name, str(self.verification_script), str(Path(stp_path_str).absolute())] cmd = [cmd_name, '-c', str(self.verification_script), str(Path(stp_path_str).absolute())]
logger.info(f"找到 FreeCAD 命令: {cmd_name}") logger.info(f"找到 FreeCAD 命令: {cmd_name}")
break break
except: except:
@@ -84,9 +84,10 @@ class GeometryVerificationService:
# 如果 which 找不到,尝试直接检查常见路径 # 如果 which 找不到,尝试直接检查常见路径
if not cmd: if not cmd:
for cmd_path in ['/usr/bin/freecad', '/usr/bin/freecadcmd', '/usr/local/bin/freecad', '/snap/bin/freecad.cmd']: for cmd_path in ['/usr/local/bin/freecad', '/usr/bin/freecad', '/usr/local/bin/freecadcmd', '/usr/bin/freecadcmd', '/snap/bin/freecad.cmd']:
if Path(cmd_path).exists(): if Path(cmd_path).exists():
cmd = [cmd_path, str(self.verification_script), str(Path(stp_path_str).absolute())] # 添加 -c 参数使用命令行模式(无图形界面)
cmd = [cmd_path, '-c', str(self.verification_script), str(Path(stp_path_str).absolute())]
logger.info(f"找到 FreeCAD 命令路径: {cmd_path}") logger.info(f"找到 FreeCAD 命令路径: {cmd_path}")
break break
@@ -168,8 +169,12 @@ class GeometryVerificationService:
) )
if has_error: if has_error:
logger.warning("FreeCAD 验证失败,使用 PythonOCC 验证") logger.error("FreeCAD 验证失败,无法进行交叉验证")
return self._verify_with_pythonocc_only(stp_path_str) return {
"status": "error",
"reason": "FreeCAD 无法读取 STP 文件,无法进行交叉验证",
"timestamp": datetime.now().isoformat()
}
if process.returncode == 0: if process.returncode == 0:
# 尝试读取生成的报告文件(在 STP 文件目录下) # 尝试读取生成的报告文件(在 STP 文件目录下)
@@ -327,8 +332,9 @@ class GeometryVerificationService:
return count return count
return { return {
"status": "skipped", "status": "passed",
"reason": "FreeCAD 无法访问文件(snap 沙盒限制),仅使用 PythonOCC 验证", "method": "pythonocc_only",
"reason": "FreeCAD 验证失败,仅使用 PythonOCC 验证",
"timestamp": datetime.now().isoformat(), "timestamp": datetime.now().isoformat(),
"pythonocc": { "pythonocc": {
"volume_mm3": float(volume_mm3), "volume_mm3": float(volume_mm3),