通过 HTTP API 接收 JSON 请求,自动在 CATIA V5 中创建零件和装配体的 Python 程序。
- ✅ 通过 REST API 接收 JSON 请求生成 CATIA 零件和装配体
- ✅ 支持多种基本零件类型(方块、圆柱、球体、圆锥、L型支架、T型支架)
- ✅ 支持模板方法创建参数化零件
- ✅ 支持装配约束(重合、偏移、接触、角度)
- ✅ 完整的异常处理和日志输出
- ✅ 使用 CATIADocHandler 上下文管理器管理文档生命周期
- CATIA V5 - 已安装并授权
- Python 3.8+
- Windows 操作系统(pycatia 需要 Windows 环境)
pip install pycatiagit clone <repository-url>
cd catia-cad-pythonpip install flask- 打开 CATIA V5
- 进入
工具→选项→常规→参数和测量 - 确保启用了 COM 接口支持
如果需要使用模板方法创建参数化零件,请按以下步骤操作:
-
创建基础零件
- 在 CATIA 中创建一个新的 Part 文档
- 创建基本几何形状(如方块、圆柱)
-
定义参数
- 进入
工具→参数 - 创建参数(如
length,width,height) - 将几何尺寸与参数关联
- 进入
-
创建几何元素
- 创建草图并使用拉伸创建实体
- 确保关键尺寸使用之前定义的参数
-
发布几何元素
- 在零件树中右键点击几何元素
- 选择
发布 - 命名发布的元素(如
Face_Top,Axis_Cylinder)
-
保存模板
- 将零件保存为
.CATPart文件 - 将模板文件放置在
templates/目录下
- 将零件保存为
python app.py默认监听地址:http://0.0.0.0:5000
python app.py --host 127.0.0.1 --port 8080GET /health
响应:
{
"status": "ok",
"message": "CATIA Assembly API is running"
}GET /catia/status
POST /generate
请求体示例:
{
"assembly_name": "TestAssembly",
"components": [
{
"id": "base",
"type": "block",
"parameters": {"length": 100.0, "width": 80.0, "height": 10.0},
"material": "Steel"
},
{
"id": "post",
"type": "cylinder",
"parameters": {"radius": 15.0, "height": 50.0},
"material": "Aluminum"
}
],
"constraints": [
{
"type": "coincidence",
"source": {"component": "post", "element": "Face_Bottom"},
"target": {"component": "base", "element": "Face_Top"}
},
{
"type": "offset",
"source": {"component": "post", "element": "Axis_Cylinder"},
"target": {"component": "base", "element": "Edge_CenterLine"},
"value": 20.0
}
]
}响应:
{
"success": true,
"assembly_name": "TestAssembly",
"assembly_path": "output/assemblies/TestAssembly.CATProduct",
"components": {
"base": "output/parts/base.CATPart",
"post": "output/parts/post.CATPart"
},
"message": "装配体生成成功"
}POST /parts/create
POST /assembly/create
# 健康检查
curl http://localhost:5000/health
# 生成装配体
curl -X POST http://localhost:5000/generance \
-H "Content-Type: application/json" \
-d '{
"assembly_name": "TestAssembly",
"components": [
{
"id": "base",
"type": "block",
"parameters": {"length": 100.0, "width": 80.0, "height": 10.0}
}
],
"constraints": []
}'import requests
import json
url = "http://localhost:5000/generate"
data = {
"assembly_name": "TestAssembly",
"components": [
{
"id": "base",
"type": "block",
"parameters": {"length": 100.0, "width": 80.0, "height": 10.0},
"material": "Steel"
},
{
"id": "post",
"type": "cylinder",
"parameters": {"radius": 15.0, "height": 50.0},
"material": "Aluminum"
}
],
"constraints": [
{
"type": "coincidence",
"source": {"component": "post", "element": "Face_Bottom"},
"target": {"component": "base", "element": "Face_Top"}
}
]
}
response = requests.post(url, json=data)
print(response.json())| 类型 | 必需参数 |
|---|---|
| block | length, width, height |
| cylinder | radius, height |
| sphere | radius |
| cone | radius, height |
| L_shaped | length, width, height, leg_width |
| T_shaped | length, width, height, web_thickness |
| 类型 | 必需参数 |
|---|---|
| coincidence | source, target |
| offset | source, target, value |
| contact | source, target |
| angle | source, target, value |
程序会处理以下异常情况:
- CATIA 未运行
- 文档打开失败
- 参数错误
- 约束应用失败
catia-cad-python/
├── app.py # Flask API 主程序
├── catia_connection.py # CATIA 连接和文档管理
├── part_generator.py # 零件生成器
├── assembly_generator.py # 装配体生成器
├── validator.py # 输入验证器
├── templates/ # 零件模板目录
├── output/ # 输出目录
│ ├── parts/ # 生成的零件
│ └── assemblies/ # 生成的装配体
└── README.md # 本文件
- 脚本运行时 CATIA 应处于打开状态(或通过 pycatia 自动启动)
- 生成的零件文件默认保存在
output/parts/目录 - 生成的装配体文件默认保存在
output/assemblies/目录 - 使用模板方法时,确保模板文件存在且参数名称匹配