提供从 Python 安全调用 CATIA V5 COM 接口的通用方案:Python subprocess.run() → cscript → VBS → COM → CATIA。
在 WorkBuddy 沙箱环境下,直接调用 cscript 可能被 LOLBin 策略拦截,但通过 Python 子进程嵌套调用已验证可行(2026-06-07)。
| 问题 | 原因 | 解决方案 |
|---|---|---|
| ------ | ------ | --------- |
Python win32com Position.GetComponents 返回全零 | CATIA V5-6R2024 B34 兼容性问题 | 通过 VBS 原生 COM 调用 |
| cscript 被 WorkBuddy 直接封堵(LOLBin) | 安全策略 | Python subprocess 嵌套调用绕过 |
| 每个 Skill 重复实现 subprocess 调用 | 代码重复 | 统一桥梁模块 |
| 项目 | 路径/值 |
|---|---|
| ------ | --------- |
| CATIA V5 | C:\Program Files\Dassault Systemes\B34\win_b64\code\bin\CNEXT.exe |
| CATIA 版本 | V5-6R2024 B34 (R34.SP03.HF010) |
| Python | C:\Users\vobes23\.workbuddy\binaries\python\envs\default\Scripts\python.exe |
| cscript | Windows 自带(System32) |
Python script
└─ subprocess.run(["cscript", "//nologo", "script.vbs", arg1, arg2...])
└─ cscript.exe (Windows Script Host)
└─ script.vbs
└─ GetObject(, "CATIA.Application") ← COM 连接到运行中的 CATIA
└─ 读取/操作 CATIA 对象模型
└─ WScript.Echo 输出结果 → Python stdout 捕获
import sys
sys.path.insert(0, r"C:\Users\vobes23\.workbuddy\skills\catia-com-bridge\scripts")
from catia_bridge import run_catia_script, check_catia_running
# 检查 CATIA 是否在运行
if not check_catia_running():
print("ERROR: CATIA is not running")
sys.exit(1)
# 执行 VBS 脚本
result = run_catia_script(
vbs_path=r"D:\path\to\script.vbs",
args=["output.txt", "extra_param"],
timeout=300
)
if result.returncode == 0:
for line in result.stdout.splitlines():
print(line)
else:
print(f"ERROR: {result.stderr}")
其他 Skill 声明对本 Skill 的依赖,使用桥梁函数:
# 在 Skill 脚本中
import sys
bridge_dir = r"C:\Users\vobes23\.workbuddy\skills\catia-com-bridge\scripts"
if bridge_dir not in sys.path:
sys.path.insert(0, bridge_dir)
from catia_bridge import run_catia_script
被桥梁调用的 VBS 脚本应遵循以下规范:
WScript.Arguments 获取调用参数Set catia = GetObject(, "CATIA.Application")WScript.Echo 输出结构化数据WScript.Echo "ERROR|CODE|Description" 格式WScript.Quit 0(成功)/ WScript.Quit 1(失败)示例模板:
Option Explicit
Dim catia, outputPath
Set catia = GetObject(, "CATIA.Application")
outputPath = WScript.Arguments(0) ' 第一个参数:输出文件路径
' 执行 CATIA 操作...
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set outFile = fso.CreateTextFile(outputPath, True)
outFile.WriteLine "data..."
outFile.Close
WScript.Quit 0
check_catia_running() -> bool检查 CATIA V5 进程是否在运行(通过进程名 CNEXT.exe)。
run_catia_script(vbs_path, args=None, timeout=300) -> CompletedProcess执行 VBS 脚本并返回结果。
| 参数 | 类型 | 说明 |
|---|---|---|
| ------ | ------ | ------ |
vbs_path | str | VBS 脚本的绝对路径 |
args | List[str] | 传递给 VBS 脚本的参数列表 |
timeout | int | 超时时间(秒),默认 300 |
返回值 subprocess.CompletedProcess:
.returncode — 0 成功.stdout — WScript.Echo 输出.stderr — 错误输出get_catia_app() — 仅 VBS 侧VBS 脚本内部使用的 CATIA 连接模式:
Set catia = GetObject(, "CATIA.Application")
Set topProduct = catia.ActiveDocument.Product
catia-com-bridge/
├── SKILL.md
└── scripts/
└── catia_bridge.py ← 桥梁核心模块(纯 Python,零外部依赖)
| Skill | 用途 |
|---|---|
| ------- | ------ |
| Loch&Bolzen Dual Verify | 提取定位件坐标 |
| catia-conn-supp-extract | 提取 CONNECTOR/SUPPORT 属性 |
| 现象 | 原因 | 解决 | |
|---|---|---|---|
| ------ | ------ | ------ | |
check_catia_running() 返回 False | CATIA 未启动 | 启动 CATIA 并打开目标文档 | |
| `ERROR\ | CATIA_NOT_FOUND` | VBS 无法连接 CATIA | 确认 CATIA 进程存在 |
| `ERROR\ | NO_ACTIVE_PRODUCT` | CATIA 未打开 Product | 打开目标装配体 |
| cscript 超时 | 大装配体遍历耗时 | 增加 timeout 参数 | |
| 返回数据全零 | 非 VBS 环境调用 | 确认使用 VBS(非 Python win32com) |
共 1 个版本