catia宏编写助手
下载地址
https://www.zhengdianxue.com/wp-content/uploads/skills/user_cf90b6c5--catia-marco.zip将提示词发送给你的 AI 安装该 skills
请根据 https://skillhub.cn/install/skillhub.md,安装 @user_cf90b6c5/catia-marco。CATIA 宏开发助手 是一个专门用于编写、调试和修改 CATIA V5/V6 自动化宏的工具包。 它能做什么: 帮你写 CATIA 宏(CATScript/VBScript/VBA),覆盖图纸标注、零件设计、装配等场景 提供 214 个 CATIA API 接口的完整文档,随时查阅方法和属性 内置 10 个官方宏示例,可直接参考或修改 列出了 CATScript 的 11 条语法限制(比如不能用 For Each、不能用 Format() 等),避免写出来的代码运行报错 记录了 12 条实际踩过的坑(比如 GetRealColor 读不了颜色、GetPaperDimensions 方法不存在等),不用重复踩坑 一句话总结: 写 CATIA 宏时加载这个 Skill,就像带了一个熟悉 CATIA API 和各种坑点的专家在旁边指导。
阅读全文 (展开查看完整说明)
概述
CATIA 宏开发助手
编写、调试和优化 CATIA V5/V6 自动化宏(CATScript/VBScript/VBA)。
Core Principles
CATScript vs VBScript vs VBA
- CATScript — Dassault's legacy scripting language, processed by stripping type info then running through VBScript engine. Has the MOST restrictions.
- VBScript — Standard Windows scripting, runs inside CATIA. Fewer restrictions than CATScript.
- VBA — Full Visual Basic for Applications, most capable. Use when complex type declarations are needed.
Default choice: Write as CATScript (.CATScript files) unless the user specifies otherwise. CATScript is the most widely compatible format but has critical limitations.
Critical CATScript Limitations (MUST follow)
These are hard limitations of the CATIA VBScript engine. Violating ANY of these will cause runtime errors. Every single one below has been verified to cause real failures in production. Do NOT assume "maybe it works".
- NO
GoToacross procedure boundaries or nested blocksGoToCANNOT jump into or out ofFor/If/DoblocksGoTowithin the same block level is OK (e.g., skip to end of a Sub)- Workaround: Use
If/ElseIf/Elseor boolean flag variables instead of GoTo for flow control
- NO
For Each...Nexton CATIA collections- CATIA's collection objects do not support
For Eachenumeration - Workaround: Always use
For i = 1 To Collection.Count ... Set obj = Collection.Item(i) ... Next
- CATIA's collection objects do not support
Nextmust NOT have a variable nameNext i→ RUNTIME ERROR- Use bare
Nextonly
- NO
Format()functionFormat(value, "0.00")→ Type mismatch errorCStr(value)is also unreliable for formatting- Workaround: Use string concatenation with
&, or build formatting manually
- NO
As TypeinDimDim x As Integer→ Syntax error in CATScript- Use
Dim x(all variables are Variant)
- NO
On Error GoTo labelOn Error GoTo ErrorHandler→ Not supported- Only
On Error Resume NextandOn Error GoTo 0
Diminside blocks- CATScript processes
Dimas if all are hoisted to procedure top - Inline
Dimworks but without types; safer to declare all at top
- CATScript processes
- NO
CreateObject("Scripting.FileSystemObject")- May fail or be restricted. Use
CATIA.FileSysteminstead for file operations.
- May fail or be restricted. Use
- NO
Type…End Type(user-defined types)- Not supported in CATScript. Use Classes or parallel arrays instead.
- Array function limitations
Array()works but returns a Variant arrayReDim Preserveworks- No multi-dimensional array literals
- String functions are VBScript subset
InStr,Left,Right,Mid,Len,Replace,Split,Join— all workStrConv— NOT supportedLikeoperator — NOT supported
Variable Naming Conventions
| Prefix | Type | Example |
|---|---|---|
o | Object | oSheet, oView, oComp |
s | String | sName, sPath |
i | Integer | iCount, iIndex |
d | Double | dLength, dHeight |
b | Boolean | bFound, bIsVisible |
Standard Macro Structure
' ============================================
' Purpose: Brief description
' File: MacroName.CATScript
' ============================================
Sub CATMain()
On Error Resume Next
' Get CATIA application
Dim oCATIA
Set oCATIA = CATIA
' Get active document
Dim oDoc
Set oDoc = oCATIA.ActiveDocument
' ... macro logic ...
On Error GoTo 0
End SubError Handling Pattern
' Disable auto error handling only when needed
On Error Resume Next
Dim result
result = someObject.SomeMethod()
If Err.Number <> 0 Then
' Handle error
On Error GoTo 0
MsgBox "Error: " & Err.Description
Exit Sub
End If
On Error GoTo 0String Formatting (NO Format())
' WRONG - causes type mismatch in CATScript:
MsgBox Format(value, "0.00")
' CORRECT - use concatenation:
MsgBox "Value: " & valueCollection Iteration Pattern
' WRONG - For Each doesn't work in CATScript:
For Each oView In oViews
...
Next
' CORRECT - use index-based loop:
Dim i
For i = 1 To oViews.Count
Set oView = oViews.Item(i)
...
NextAPI Reference Structure
The references/interfaces/ directory contains the complete CATIA V5 Automation IDL documentation, merged into plain text files by framework. Each file contains all interface definitions for that framework:
| 文件 | 框架 | 大小 | 内容 |
|---|---|---|---|
DraftingInterfaces.txt | 图纸/工程图 | ~270KB | Sheet、View、Component、Dimension、Text、Table 等 |
Drafting2DLInterfaces.txt | 2D Layout | ~88KB | Layout2DRoot、Layout2DSheet、Layout2DView |
InfInterfaces.txt | 基础设施 | ~353KB | Application、Document、Selection、Window 等 |
PartInterfaces.txt | 零件设计 | ~206KB | Part、Body、Sketch、HybridShape 等 |
ProductStructureInterfaces.txt | 装配 | ~36KB | Product、Products、Constraints 等 |
MecModInterfaces.txt | 机械建模 | ~244KB | Sketcher、Factory2D、GeometricElements 等 |
Each interface is separated by === interface_<Name> === markers. Search for interface_<InterfaceName> to find a specific API.
DraftingInterfaces (Drawing/Drafting)
Core object hierarchy:
CATIA.Application
└── DrawingDocument
└── DrawingRoot
├── Sheets (DrawingSheets)
│ └── DrawingSheet (ActiveSheet)
│ ├── Views (DrawingViews)
│ │ └── DrawingView
│ │ ├── Components (DrawingComponents)
│ │ │ └── DrawingComponent
│ │ ├── Dimensions (DrawingDimensions)
│ │ │ └── DrawingDimension
│ │ ├── Texts (DrawingTexts)
│ │ │ └── DrawingText
│ │ ├── Tables (DrawingTables)
│ │ │ └── DrawingTable
│ │ ├── Leaders (DrawingLeaders)
│ │ ├── Arrows (DrawingArrows)
│ │ ├── GDTs (DrawingGDTs)
│ │ ├── Weldings (DrawingWeldings)
│ │ ├── Threads (DrawingThreads)
│ │ ├── Pictures (DrawingPictures)
│ │ └── GenerativeBehavior
│ └── DrawingPageSetup
└── Parameters / RelationsKey interfaces to search in references/interfaces/DraftingInterfaces.txt:
interface_DrawingSheet— GetPaperHeight(), GetPaperWidth(), Scale, etc.interface_DrawingView— x, y coordinates, GenerativeBehavior, lock statusinterface_DrawingComponent— Name, x, y, referenced viewinterface_DrawingComponents— Count, Item()interface_DrawingText— Text content, position, propertiesinterface_DrawingTable— Table manipulationinterface_DrawingDimension— Dimension propertiesinterface_DrawingRoot— ActiveSheet, Sheets, Update, Isolateinterface_DrawingDocument— DrawingRoot accessinterface_DraftingSettingAtt— Drafting settings
InfInterfaces (Infrastructure)
在 references/interfaces/InfInterfaces.txt 中搜索:
interface_Application— CATIA.Application rootinterface_Document— Document base classinterface_Documents— Open, Add, Iteminterface_Selection— User selection managementinterface_Window,interface_Windows— Window managementinterface_VisPropertySet— Color, line width, visibility
PartInterfaces (Part Design)
- Part, Bodies, Sketches, HybridShapes, etc.
ProductStructureInterfaces (Assembly)
- Product, Products, Constraints, etc.
MecModInterfaces (Mechanical Modeler)
- Sketcher, Factory2D, GeometricElements, etc.
Drafting2DLInterfaces (2D Layout for 3D Design)
- Layout2DRoot, Layout2DSheet, Layout2DView, Layout2DFactory
Searching the API Reference
To find a specific API method or property:
- 在对应框架的 txt 文件中搜索 — 用
grep或文本搜索工具在references/interfaces/目录下搜索 - 按接口名搜索 — 搜索
=== interface_<InterfaceName>定位到具体接口 - 按方法名搜索 — 搜索方法名(如
GetPaperHeight、Add、Item)
搜索示例:
# 在 Drawing 文档中查找 GetPaperHeight
search: GetPaperHeight in references/interfaces/DraftingInterfaces.txt
# 查找所有 Component 相关接口
search: interface_DrawingComponent in references/interfaces/DraftingInterfaces.txt
# 查找枚举值
search: CatDrawingViewType in references/interfaces/DraftingInterfaces.txtMacro Examples
所有宏示例合并保存在 references/macros/_all_examples.txt,每个宏用 === 文件名 === 分隔。
包含 11 个官方示例(Drafting + 2D Layout):
Drawing/Drafting 示例:
CAADriCreateView.CATScript— 在新图纸中创建生成视图CAADriSheet.CATScript— 创建和切换图纸页CAADriDetailSheet.CATScript— 创建细节图纸页CAADriDimension.CATScript— 创建尺寸标注CAADriDrawingTable.CATScript— 操作图纸表格CAADriInstantiateOuterDitto.CATScript— 实例化 2D 组件(外部 ditto)CAADriInstantiateInnerDitto.CATScript— 实例化 2D 组件(内部 ditto)CAADriDuplicateAView.CATScript— 复制视图CAADriSetPrintArea.CATScript— 设置打印区域CAADriUpdateSheets.CATScript— 更新图纸页
2D Layout 示例:
CAA2dlIPrint2DLayout.CATScript— 打印 2D Layout
Known Pitfalls from Real-World Usage
These are NOT theoretical — every item below caused actual failures during macro development and debugging.
API Limitations
- DrawingComponent.GetRealColor() does NOT work — Color reading fails silently. Returns wrong values or errors. Workaround: match by name prefix instead of color filtering.
- DrawingSheet.GetPaperDimensions() does NOT exist — This method is not available on DrawingSheet. Use
GetPaperHeight()andGetPaperWidth()separately. - DrawingSheet.GetPaperHeight() / GetPaperWidth() — These DO work — They return the paper size in millimeters as Double. Note: the "height" is the actual sheet height regardless of landscape/portrait orientation. For example, A3 landscape has height=297 (the short side).
- View coordinates are relative to the Sheet origin — To get absolute sheet position:
SheetX = View.x + Component.x,SheetY = View.y + Component.y. - 2D Component naming after Instantiate — Base name
2D Component.abecomes2D Component.a.1,2D Component.a.2, etc. Use prefix matching:Left(name, Len(prefix)) = prefix. - PaperSize enum values don't always match orientation — Always call
GetPaperHeight()at runtime instead of relying on enum assumptions.
Language / Engine Pitfalls
- MsgBox/InputBox only accept Strings — Always use
CStr()to convert numbers before passing to MsgBox/InputBox. Passing a Double directly may cause type mismatch. - VBScript
Select Caseis case-insensitive — But CATIA object names (e.g.,2D Component.a) ARE case-sensitive. Do not rely on Select Case for name matching. - Dictionary object —
CreateObject("Scripting.Dictionary")works in CATScript and is the recommended way to do key-value lookups, deduplication, and sorted grouping. - Chr() function —
Chr(65)returns "A", works fine. But for Unicode characters beyond 255, useChrW().
Coordinate System Notes
- CATIA Drawing coordinate system — Origin (0,0) is at bottom-left of the sheet. X increases rightward, Y increases upward. This is standard mathematical orientation, NOT screen coordinates.
- Custom coordinate overlay — When building custom zone grids (e.g., A1-Z99), the origin offset and grid spacing depend on paper size. Always read paper height at runtime with
GetPaperHeight()rather than hardcoding.