Skip to content

ning876/catia-cad-python

Repository files navigation

CATIA V5 Assembly Generation API

通过 HTTP API 接收 JSON 请求,自动在 CATIA V5 中创建零件和装配体的 Python 程序。

功能特性

  • ✅ 通过 REST API 接收 JSON 请求生成 CATIA 零件和装配体
  • ✅ 支持多种基本零件类型(方块、圆柱、球体、圆锥、L型支架、T型支架)
  • ✅ 支持模板方法创建参数化零件
  • ✅ 支持装配约束(重合、偏移、接触、角度)
  • ✅ 完整的异常处理和日志输出
  • ✅ 使用 CATIADocHandler 上下文管理器管理文档生命周期

环境要求

  1. CATIA V5 - 已安装并授权
  2. Python 3.8+
  3. Windows 操作系统(pycatia 需要 Windows 环境)

安装步骤

1. 安装 pycatia

pip install pycatia

2. 克隆或下载本项目

git clone <repository-url>
cd catia-cad-python

3. 安装依赖

pip install flask

配置 CATIA

设置 COM 接口

  1. 打开 CATIA V5
  2. 进入 工具选项常规参数和测量
  3. 确保启用了 COM 接口支持

创建零件模板(可选)

如果需要使用模板方法创建参数化零件,请按以下步骤操作:

  1. 创建基础零件

    • 在 CATIA 中创建一个新的 Part 文档
    • 创建基本几何形状(如方块、圆柱)
  2. 定义参数

    • 进入 工具参数
    • 创建参数(如 length, width, height
    • 将几何尺寸与参数关联
  3. 创建几何元素

    • 创建草图并使用拉伸创建实体
    • 确保关键尺寸使用之前定义的参数
  4. 发布几何元素

    • 在零件树中右键点击几何元素
    • 选择 发布
    • 命名发布的元素(如 Face_Top, Axis_Cylinder
  5. 保存模板

    • 将零件保存为 .CATPart 文件
    • 将模板文件放置在 templates/ 目录下

运行服务

启动 Flask API

python app.py

默认监听地址:http://0.0.0.0:5000

自定义端口

python app.py --host 127.0.0.1 --port 8080

API 端点

1. 健康检查

GET /health

响应:

{
    "status": "ok",
    "message": "CATIA Assembly API is running"
}

2. CATIA 连接状态

GET /catia/status

3. 生成完整装配体

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": "装配体生成成功"
}

4. 创建单个零件

POST /parts/create

5. 创建装配体(已有零件)

POST /assembly/create

测试示例

使用 curl 测试

# 健康检查
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": []
  }'

使用 Python requests

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            # 本文件

注意事项

  1. 脚本运行时 CATIA 应处于打开状态(或通过 pycatia 自动启动)
  2. 生成的零件文件默认保存在 output/parts/ 目录
  3. 生成的装配体文件默认保存在 output/assemblies/ 目录
  4. 使用模板方法时,确保模板文件存在且参数名称匹配

About

通过python驱动catia进行3D建模

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors