【CATIA的二次开发29】抽象对象Document涉及文档标识的属性
在CATIA VBA开发中,Document对象是最核心、最基础的对象之一。它代表了当前在CATIA会话中打开的一个文档(文件)。
几乎所有与文件操作、模型访问相关的操作都始于获取一个Document对象。Document对象包含多种方法和属性,以下介绍Document对象方法和属性
一、Document对象方法
1、方法和属性列表

2、属性分类
- 应用程序连接属性
属性 | 返回值类型 | 功能描述 | 使用场景示例 |
---|---|---|---|
Application | Application | 返回当前文档所属的 CATIA 应用实例 | 访问全局功能: Set catia = doc.Application |
Parent | AnyObject | 返回文档的父对象 (通常是 Application 或 Documents 集合) | 导航对象层级: Set parentObj = doc.Parent |
- 文档标识属性
属性 | 返回值类型 | 功能描述 | 平台差异说明 |
---|---|---|---|
Name | String | 获取文档名称 (不含路径) | 示例:“Part1.CATPart” V5/V6 通用 |
FullName | String | 获取完整路径+文件名 | 示例:“C:\Parts\Assembly1.CATProduct” V5:本地路径 3DX:PLM 数据库路径 |
Path | String | 获取文档所在目录路径 (不含文件名) | 示例:“C:\Parts” V5:本地路径 3DX:返回空或虚拟路径 |
- 文档状态属性
属性 | 返回值类型 | 功能描述 | 使用技巧 |
---|---|---|---|
ReadOnly | Boolean | 判断文档是否只读 True = 只读模式 | 修改前检查: If Not doc.ReadOnly Then doc.Part.Update() |
Saved | Boolean | 判断文档是否已保存 True = 无未保存修改 | 关闭前验证: If Not doc.Saved Then MsgBox “保存更改!” |
SeeHiddenElements | Boolean | 控制隐藏元素可见性 True = 显示隐藏元素 | 审查隐藏对象: doc.SeeHiddenElements = True |
- 图形视图属性
属性 | 返回值类型 | 功能描述 | 使用示例 |
---|---|---|---|
Cameras | Cameras 集合 | 访问文档相机集合(视图视角配置) | 保存/恢复视图: Set view1 = doc.Cameras.Item(“Camera.1”) |
ActiveView | Viewer / Window | 获取当前激活视图窗口(V5 特有) | 视图操作: doc.ActiveView.FitAllIn() |
- 选择与过滤属性
属性 | 返回值类型 | 功能描述 | 平台差异说明 |
---|---|---|---|
Selection | Selection | 获取文档选择集合对象(V5 核心功能) | V5:直接访问 3DX:改用 SelectionManager 服务 |
CurrentFilter | Filter | 获取/设置当前选择过滤器(V5 特有) | 已淘汰: 3DX 中使用 PLMTypeFilter 替代 |
CurrentLayer | Layer | 访问当前工作图层(需启用层功能) | 图层管理: Set curLayer = doc.CurrentLayer |
- 关键属性功能对比表
属性分类 | 属性名称 | 核心功能 | 是否可写 | V5 支持 | 3DX 支持 |
---|---|---|---|---|---|
应用连接 | Application | 获取 CATIA 实例 | ❌ | ✅ | ✅ |
Parent | 获取父对象 | ❌ | ✅ | ✅ | |
文档标识 | Name | 获取文档名称 | ❌ | ✅ | ✅ |
FullName | 获取完整路径 | ❌ | ✅ | ⚠️ (PLM路径) | |
Path | 获取目录路径 | ❌ | ✅ | ⚠️ (常为空) | |
文档状态 | ReadOnly | 判断只读状态 | ❌ | ✅ | ✅ |
Saved | 判断保存状态 | ❌ | ✅ | ✅ | |
SeeHiddenElements | 控制隐藏元素可见性 | ✅ | ✅ | ✅ | |
图形视图 | Cameras | 访问视图相机集合 | ❌ | ✅ | ✅ |
ActiveView | 获取活动视图(V5特有) | ❌ | ✅ | ❌ | |
选择与过滤 | Selection | 访问选择集(V5核心) | ❌ | ✅ | ⚠️ (服务替代) |
CurrentFilter | 获取/设置过滤器(已淘汰) | ✅ | ✅ | ❌ | |
CurrentLayer | 访问当前图层 | ✅ | ✅ | ✅ |
二、属性~文档标识属性(Name、FullName和Path)
1、Name属性
在 CATIA VBA 开发中,Document.Name 属性 是文档标识的核心属性,用于获取文档的文件名(包含扩展名)。
这个只读属性在文档管理、自动化流程和用户交互中扮演着关键角色,是访问文档基本信息的基础。
-
属性的核心作用
用于获取文档的文件名(包含扩展名)。 -
基本用法
ReadOnly Property Name As String
-
返回类型:String
-
内容:文档的文件名(例如:“EngineBlock.CATPart”)
-
核心特性与行为
- 命名规则与格式
文档类型 典型名称格式 说明 零件文档 “Part1.CATPart” 默认命名或用户定义名称 装配文档 “Assembly1.CATProduct” 自动递增的数字后缀 工程图文档 “Drawing1.CATDrawing” 基于关联零件/装配的命名 新建未保存文档 “Part1.CATPart” 使用默认模板名称 - 关键行为特点
- 只读属性:无法直接修改(需使用 SaveAs 重命名)
- 包含扩展名:始终返回完整文件名(名称 + 扩展名)
- 唯一性不保证:多个文档可能同名(不同路径)
- 未保存文档:返回默认名称(如 “Part1.CATPart”)
- 相关属性对比
属性 返回值示例 区别 Name “Bracket.CATPart” 文件名+扩展名 FullName “C:\Projects\Bracket.CATPart” 完整路径+文件名 Path “C:\Projects” 仅目录路径
-
-
典型应用场景
场景一:文档标识与日志记录
Sub LogDocumentOperation()Dim doc As DocumentSet doc = CATIA.ActiveDocumentDim logEntry As StringlogEntry = Format(Now, "yyyy-mm-dd hh:nn:ss") & " | " & _"文档: " & doc.Name & " | " & _"操作: 特征修改"AppendToLogFile "C:\Audit.log", logEntry
End Sub
场景二:窗口标题管理
Sub UpdateWindowTitle()Dim doc As DocumentSet doc = CATIA.ActiveDocumentDim win As WindowFor Each win In CATIA.WindowsIf win.Document Is doc Then' 添加修改状态标记Dim status As Stringstatus = IIf(doc.Saved, "", "*") ' 未保存显示星号win.Caption = doc.Name & status & " - CATIA V5"End IfNext
End Sub
场景三:文档快速筛选
Sub FindDocumentByName(partialName As String)Dim docs As DocumentsSet docs = CATIA.DocumentsDim doc As DocumentDim found As BooleanFor Each doc In docsIf InStr(1, doc.Name, partialName, vbTextCompare) > 0 Thendoc.Activatefound = TrueExit ForEnd IfNextIf Not found ThenMsgBox "未找到包含 '" & partialName & "' 的文档", vbInformationEnd If
End Sub
场景四:智能名称解析
Function ExtractBaseName(fullName As String) As String' 移除扩展名Dim dotPosition As IntegerdotPosition = InStrRev(fullName, ".")If dotPosition > 0 ThenExtractBaseName = Left(fullName, dotPosition - 1)ElseExtractBaseName = fullNameEnd If
End FunctionFunction ExtractExtension(fullName As String) As String' 获取扩展名Dim dotPosition As IntegerdotPosition = InStrRev(fullName, ".")If dotPosition > 0 ThenExtractExtension = Mid(fullName, dotPosition + 1)ElseExtractExtension = ""End If
End Function
场景五:自动重命名系统
Sub AutoRenameDocument()Dim doc As DocumentSet doc = CATIA.ActiveDocument' 基于参数生成新名称Dim newName As StringnewName = GenerateDocumentName(doc)' 仅当名称不同时重命名If newName <> doc.Name ThenDim newPath As StringnewPath = doc.Path & "\" & newNamedoc.SaveAs newPathMsgBox "文档已重命名为: " & newNameEnd If
End SubFunction GenerateDocumentName(doc As Document) As StringSelect Case doc.TypeCase "Part"Dim partNumber As StringpartNumber = doc.Part.Parameters.Item("PartNumber").ValueGenerateDocumentName = partNumber & ".CATPart"Case "Product"Dim projectCode As