【CATIA的二次开发30】抽象对象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:PartsAssembly1.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 访问当前图层

二、属性~文档状态属性(ReadOnly、Saved和SeeHiddenElements)

1、ReadOnly属性

在 CATIA VBA 开发中,Document.ReadOnly 属性 是文档访问权限的关键指示器,用于判断当前文档是否以只读模式打开。
这个只读属性在自动化工作流中至关重要,帮助开发者识别文档状态、防止无效操作并优化用户交互。

属性的核心作用
用于判断当前文档是否以只读模式打开。 这个只读属性在自动化工作流中至关重要,

基本用法

ReadOnly Property ReadOnly As Boolean

返回类型:Boolean
True:文档为只读模式
False:文档可编辑

核心功能与行为特点

状态判定规则

文档来源 ReadOnly 状态 说明
打开已存在文件 取决于文件属性 继承文件系统权限
新建文档 False 新文档默认可编辑
从PDM系统检出 False 检出状态可编辑
从PDM系统查看 True 查看状态只读
网络共享文件 取决于共享权限 可能为只读

影响的操作

操作 只读状态影响 解决方案
Save() 失败 (错误 -2147467259) 使用 SaveAs()
修改参数/关系式 失败 克隆文档
添加/删除特征 失败 请求编辑权限
更改属性 可能失败 检查具体属性可编辑性

特殊场景行为
* 内存文档:从未保存的文档始终返回 False
* PDM集成:状态可能随签入/签出动态变化
* 多用户环境:可能因其他用户锁定变为只读

典型应用场景

场景一:智能保存处理

Sub SmartSave()
    Dim doc As Document
    Set doc = CATIA.ActiveDocument
    
    If doc.ReadOnly Then
        Dim newPath As String
        newPath = InputBox("文档只读,请输入保存路径", "另存为", "C:Modified_" & doc.Name)
        If newPath <> "" Then doc.SaveAs newPath
    Else
        doc.Save
    End If
End Sub

场景二:只读文档警告

Sub CheckEditableBeforeModify()
    Dim doc As Document
    Set doc = CATIA.ActiveDocument
    
    If doc.ReadOnly Then
        If MsgBox("文档为只读,是否创建可编辑副本?", vbYesNo) = vbYes Then
            CreateEditableCopy doc
        Else
            Exit Sub
        End If
    End If
    
    ' 执行修改操作
    ModifyDocument
End Sub

场景三:批量处理过滤

Sub ProcessEditableDocuments()
    Dim docs As Documents
    Set docs = CATIA.Documents
    Dim doc As Document
    
    For Each doc In docs
        If Not doc.ReadOnly And Not doc.Saved Then
            ProcessDocument doc
            doc.Save
        End If
    Next
End Sub

场景四:PDM状态集成

Function IsCheckoutRequired(doc As Document) As Boolean
    If doc.ReadOnly Then
        ' 检查PDM集成
        If IsPDMIntegrated(doc) Then
            Dim pdmStatus As String
            pdmStatus = GetPDMStatus(doc)
            IsCheckoutRequired = (pdmStatus = "Checked-in")
        Else
            ' 文件系统只读
            IsCheckoutRequired = True
        End If
    Else
        IsCheckoutRequired = False
    End If
End Function

Sub CheckoutIfRequired()
    Dim doc As Document
    Set doc = CATIA.ActiveDocument
    
    If IsCheckoutRequired(doc) Then
        If PDMCheckout(doc) Then
            MsgBox "文档已检出,可编辑"
        Else
            MsgBox "检出失败,文档保持只读", vbExclamation
        End If
    End If
End Sub

场景五:只读模式分析器

Sub AnalyzeReadOnlyCause()
    Dim doc As Document
    Set doc = CATIA.ActiveDocument
    
    If doc.ReadOnly Then
        Dim cause As String
        
        ' 判断只读原因
        If IsFileLocked(doc.FullName) Then
            cause = "文件被其他用户锁定"
        ElseIf IsReadOnlyFile(doc.FullName) Then
            cause = "文件系统权限限制"
        ElseIf IsPDMViewOnly(doc) Then
            cause = "PDM查看模式"
        Else
            cause = "未知原因"
        End If
        
        MsgBox "只读原因: " & cause, vbInformation
    Else
        MsgBox "文档可编辑", vbInformation
    End If
End Sub

场景六:自动克隆系统

Sub AutoCloneReadOnlyDocument()
    Dim doc As Document
    Set doc = CATIA.ActiveDocument
    
    If doc.ReadOnly Then
        ' 生成克隆路径
        Dim clonePath As String
        clonePath = "C:TempClones" & Format(Now, "yyyymmdd_hhmm") & "_" & doc.Name
        
        ' 创建可编辑克隆
        doc.SaveAs clonePath
        doc.Close
        CATIA.Documents.Open clonePath
    End If
End Sub

企业级应用方案

场景一:PDM集成工作流

Sub PDMIntegratedSave()
    Dim doc As Document
    Set doc = CATIA.ActiveDocument
    
    If doc.ReadOnly Then
        ' 尝试检出文档
        If Not PDMCheckout(doc) Then
            ' 检出失败时创建副本
            Dim newPath As String
            newPath = GeneratePDMTempPath(doc)
            doc.SaveAs newPath
            PDMCheckout newPath  # 检出副本
        End If
    End If
    
    ' 执行保存
    doc.Save
    
    ' 签入文档
    If IsTempDocument(doc) Then
        PDMMergeChanges doc  # 合并更改到主文档
    Else
        PDMCheckin doc
    End If
End Sub

场景二: 协作设计控制

Sub CollaborativeDesignLock()
    Dim doc As Document
    Set doc = CATIA.ActiveDocument
    
    ' 检查协作状态
    If doc.ReadOnly And IsCollaborativeDesign(doc) Then
        ' 请求编辑权限
        If RequestDesignLock(doc) Then
            ' 刷新文档状态
            doc.Reload
            If Not doc.ReadOnly Then
                MsgBox "已获得编辑权限", vbInformation
            End If
        End If
    End If
End Sub

场景三:自动归档系统

Sub ArchiveReadOnlyVersions()
    Dim fso As Object, folder As Object, file As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set folder = fso.GetFolder("X:DesignArchive")
    
    For Each file In folder.Files
        If IsCatiaFile(file.Name) Then
            ' 以只读方式打开
            Dim doc As Document
            Set doc = CATIA.Documents.Open(file.Path, True) ' True = 只读
            
            ' 验证只读状态
            If doc.ReadOnly Then
                ' 添加到归档数据库
                AddToArchiveDB doc
            Else
                ' 重置为只读防止误改
                SetFileReadOnly file.Path
            End If
            
            doc.Close
        End If
    Next
End Sub

错误处理与最佳实践

场景一:安全修改只读文档

Sub SafeModify()
    On Error GoTo ErrorHandler
    Dim doc As Document
    Set doc = CATIA.ActiveDocument
    
    If doc.ReadOnly Then
        Err.Raise vbObjectError + 1000, , "文档只读"
    End If
    
    ' 执行修改
    ModifyGeometry doc
    doc.Save
    Exit Sub
    
ErrorHandler:
    If Err.Number = vbObjectError + 1000 Then
        If MsgBox(Err.Description & ",是否另存为可编辑副本?", vbYesNo) = vbYes Then
            CreateEditableCopy doc
        End If
    Else
        MsgBox "错误 " & Err.Number & ": " & Err.Description, vbCritical
    End If
End Sub

场景二:只读状态缓存

Dim docStateCache As New Dictionary

Sub CacheDocumentState()
    Dim doc As Document
    For Each doc In CATIA.Documents
        docStateCache(doc.FullName) = doc.ReadOnly
    Next
End Sub

Function WasReadOnlyWhenOpened(fullPath As String) As Boolean
    On Error Resume Next
    WasReadOnlyWhenOpened = docStateCache(fullPath)
    If Err.Number <> 0 Then WasReadOnlyWhenOpened = False
End Function

场景三:多用户冲突解决

Sub HandleMultiUserConflict()
    Dim doc As Document
    Set doc = CATIA.ActiveDocument
    
    ' 检测运行时变为只读
    If Not WasReadOnlyWhenOpened(doc.FullName) And doc.ReadOnly Then
        ' 尝试重新获取控制权
        If TryRegainControl(doc) Then
            Exit Sub
        End If
        
        ' 创建冲突解决副本
        Dim conflictPath As String
        conflictPath = GenerateConflictPath(doc)
        doc.SaveAs conflictPath
        
        ' 提示用户
        ShowConflictResolutionDialog doc.FullName, conflictPath
    End If
End Sub

性能优化指南

场景一:减少状态检查频率

Sub OptimizedStateCheck()
    Static lastCheckTime As Date
    Dim doc As Document
    Set doc = CATIA.ActiveDocument
    
    ' 每5分钟检查一次
    If DateDiff("n", lastCheckTime, Now) >= 5 Then
        If doc.ReadOnly Then
            UpdateUIForReadOnlyState
        Else
            UpdateUIForEditableState
        End If
        lastCheckTime = Now
    End If
End Sub

场景二:批量只读处理

Sub BatchMakeReadOnly()
    Application.RefreshDisplay = False ' 禁用刷新
    
    Dim doc As Document
    For Each doc In CATIA.Documents
        If Not doc.ReadOnly And doc.Saved Then
            ' 关闭可编辑文档
            doc.Close
            
            ' 重新以只读打开
            CATIA.Documents.Open doc.FullName, True ' True = 只读
        End If
    Next
    
    Application.RefreshDisplay = True
End Sub

场景三:状态变更事件驱动

Sub RegisterReadOnlyListener()
    Dim doc As Document
    Set doc = CATIA.ActiveDocument
    
    ' 创建事件监听器
    Dim listener As ReadOnlyListener
    Set listener = CreateReadOnlyListener(doc)
    
    ' 注册状态变更处理
    listener.OnStateChange = GetRef("HandleReadOnlyChange")
End Sub

Sub HandleReadOnlyChange(isReadOnly As Boolean)
    If isReadOnly Then
        ShowReadOnlyWarning
        DisableEditingTools
    Else
        HideReadOnlyWarning
        EnableEditingTools
    End If
End Sub

特殊场景处理

场景一:

Sub ProcessWithTempFile()
    Dim doc As Document
    Set doc = CATIA.ActiveDocument
    
    If doc.ReadOnly Then
        ' 创建临时副本
        Dim tempPath As String
        tempPath = Environ("TEMP") & "" & doc.Name
        doc.SaveAs tempPath
        doc.Close
        
        ' 处理可编辑副本
        Set doc = CATIA.Documents.Open(tempPath)
    End If
    
    ' 执行操作
    HeavyProcessing doc
    
    ' 清理临时文件
    If IsTempDocument(doc) Then
        doc.Close
        Kill tempPath
    End If
End Sub

场景二:工程图只读处理

Sub ProcessReadOnlyDrawing()
    Dim drawing As DrawingDocument
    Set drawing = CATIA.ActiveDocument
    
    If drawing.ReadOnly Then
        ' 创建可编辑副本
        Dim editableDrawing As DrawingDocument
        Set editableDrawing = CreateEditableCopy(drawing)
        
        ' 在副本上操作
        AddRevisionNotes editableDrawing
        
        ' 保存并替换原文件
        ReplaceOriginalDrawing drawing, editableDrawing
    Else
        AddRevisionNotes drawing
        drawing.Save
    End If
End Sub

场景三:装配环境权限管理

Sub CheckAssemblyEditRights()
    Dim product As Product
    Set product = CATIA.ActiveDocument.Product
    
    ' 检查根产品权限
    If product.ReferenceProduct.Parent.ReadOnly Then
        MsgBox "顶层装配只读,无法修改", vbExclamation
        Exit Sub
    End If
    
    ' 检查选中组件权限
    Dim sel As Selection
    Set sel = CATIA.ActiveDocument.Selection
    If sel.Count > 0 Then
        Dim comp As Product
        Set comp = sel.Item(1).Value
        If comp.ReferenceProduct.Parent.ReadOnly Then
            MsgBox "选定组件为只读,无法修改", vbExclamation
            Exit Sub
        End If
    End If
    
    ' 执行修改
    ModifySelectedComponent
End Sub

总结
Document.ReadOnly 属性是 CATIA VBA 中权限管理的核心工具,主要应用于: 1、权限感知操作:根据状态调整工作流; 2、数据保护:防止修改受保护文档; 3、协作控制:管理多用户编辑冲突; 4、自动化归档:安全处理历史版本; 5、企业集成:与 PDM/PLM 系统协同。
使用时应遵循以下最佳实践: 1、关键操作前检查:修改前验证可编辑状态; 2、智能错误处理:提供友好的只读解决方案; 3、状态变更监控:动态响应权限变化; 4、资源优化:最小化只读文档的处理开销; 5、用户沟通:清晰解释只读状态原因。
通过合理应用此属性,可以构建健壮的企业级自动化系统,确保设计流程的安全性和合规性。

2、Saved属性

在 CATIA VBA 开发中,Document.Saved 属性 是文档修改状态的关键指示器,用于判断文档自上次保存以来是否发生过更改。
这个只读属性在自动化工作流中至关重要,帮助开发者优化保存操作、防止数据丢失并提升用户体验。

属性的核心作用
用于判断文档自上次保存以来是否发生过更改。
基本用法

ReadOnly Property Saved As Boolean

返回类型:Boolean
True:文档无未保存修改
False:文档有未保存修改

核心功能与行为特点

修改状态检测

Dim doc As Document
Set doc = CATIA.ActiveDocument

If doc.Saved Then
    MsgBox "文档无修改"
Else
    MsgBox "文档有未保存修改"
End If

状态变更规则

操作 Saved 状态 说明
打开文档 True 初始状态
修改几何特征 False 任何设计变更
修改参数/关系式 False 知识工程变更
文档保存 (Save) True 重置为已保存状态
文档另存为 (SaveAs) True 新文档视为已保存
撤销操作 (Undo) 可能变为 True 当撤销到原始状态时

特殊场景行为

只读文档:修改后仍标记为未保存(但需另存为新文件)
新文档:创建后立即标记为 False(即使未做修改)
外部修改:不会自动更新状态(需重新打开文档)

典型应用场景

场景一:智能保存控制

Sub SmartSave()
    Dim doc As Document
    Set doc = CATIA.ActiveDocument
    
    If Not doc.Saved Then
        If doc.ReadOnly Then
            doc.SaveAs "C:Modified_" & doc.Name
        Else
            doc.Save
        End If
        MsgBox "文档已保存"
    Else
        MsgBox "无修改,无需保存"
    End If
End Sub

场景二:关闭前保存检查

Sub SafeCloseDocument()
    Dim doc As Document
    Set doc = CATIA.ActiveDocument
    
    If Not doc.Saved Then
        Select Case MsgBox("文档有未保存修改,是否保存?", vbYesNoCancel)
            Case vbYes: doc.Save
            Case vbNo: ' 不保存直接关闭
            Case vbCancel: Exit Sub
        End Select
    End If
    
    doc.Close
End Sub

场景三:批量文档处理

Sub ProcessAllModifiedDocs()
    Dim docs As Documents
    Set docs = CATIA.Documents
    Dim doc As Document
    
    For Each doc In docs
        If Not doc.Saved And Not doc.ReadOnly Then
            ProcessDocument doc
            doc.Save
        End If
    Next
End Sub

场景四:修改跟踪系统

Dim lastSavedState As Boolean

Sub StartModificationTracking()
    Dim doc As Document
    Set doc = CATIA.ActiveDocument
    lastSavedState = doc.Saved
    Application.AddTimer 5000, "CheckModificationStatus"  ' 每5秒检查
End Sub

Sub CheckModificationStatus()
    Dim doc As Document
    Set doc = CATIA.ActiveDocument
    
    If doc.Saved <> lastSavedState Then
        If doc.Saved Then
            LogEvent "文档已保存: " & doc.Name
        Else
            LogEvent "文档修改: " & doc.Name
        End If
        lastSavedState = doc.Saved
    End
    
    ' 继续监控
    Application.AddTimer 5000, "CheckModificationStatus"
End Sub

场景五:自动备份机制

Sub AutoBackupOnModification()
    Static lastBackupTime As Date
    Dim doc As Document
    Set doc = CATIA.ActiveDocument
    
    If Not doc.Saved And DateDiff("n", lastBackupTime, Now) >= 10 Then
        Dim backupPath As String
        backupPath = "X:Backups" & Format(Now, "yyyymmdd_hhmm") & "_" & doc.Name
        
        doc.SaveAs backupPath
        lastBackupTime = Now
        MsgBox "自动备份已创建: " & backupPath, vbInformation
    End If
End Sub

场景六:修改状态可视化

Sub UpdateStatusIndicator()
    Dim doc As Document
    Set doc = CATIA.ActiveDocument
    Dim indicator As Shape
    
    ' 创建/获取状态指示器
    On Error Resume Next
    Set indicator = doc.Part.MainBody.Shapes.Item("ModStatusIndicator")
    If Err.Number <> 0 Then
        Set indicator = CreateStatusIndicator(doc)
    End If
    
    ' 更新颜色
    If doc.Saved Then
        indicator.Color = RGB(0, 255, 0)  ' 绿色:已保存
    Else
        indicator.Color = RGB(255, 0, 0)  ' 红色:未保存
    End If
    
    doc.Part.Update
End Sub

Function CreateStatusIndicator(doc As Document) As Shape
    ' 创建状态指示几何体
    Dim factory As Factory
    Set factory = doc.Part.ShapeFactory
    
    Dim indicator As Pad
    Set indicator = factory.AddNewPad(..., 5)  ' 创建小凸台
    indicator.Name = "ModStatusIndicator"
    
    Set CreateStatusIndicator = indicator
End Function

企业级应用方案

场景一:PDM 集成保存工作流

Sub SaveToPDMWithCheck()
    Dim doc As Document
    Set doc = CATIA.ActiveDocument
    
    If doc.Saved Then
        MsgBox "无修改,无需保存到PDM"
        Exit Sub
    End If
    
    ' 生成PDM路径
    Dim pdmPath As String
    pdmPath = GeneratePDMPath(doc)
    
    ' 执行保存
    doc.SaveAs pdmPath
    
    ' 添加PDM元数据
    AddPDMMetadata doc
    
    ' 触发工作流
    StartPDMWorkflow pdmPath
End Sub

场景二:设计审批系统

Sub SubmitForApproval()
    Dim doc As Document
    Set doc = CATIA.ActiveDocument
    
    ' 检查未保存修改
    If Not doc.Saved Then
        If MsgBox("存在未保存修改,是否先保存?", vbYesNo) = vbYes Then
            doc.Save
        Else
            Exit Sub
        End If
    End If
    
    ' 运行设计检查
    If Not RunDesignChecks(doc) Then
        MsgBox "设计检查未通过,无法提交审批"
        Exit Sub
    End If
    
    ' 提交审批系统
    SubmitToApprovalSystem doc.FullName
End Sub

场景三:自动文档清理

Sub CleanUnmodifiedDocuments()
    Dim docs As Documents
    Set docs = CATIA.Documents
    Dim doc As Document
    
    For Each doc In docs
        ' 关闭未修改且未激活的文档
        If doc.Saved And Not doc.IsActive Then
            doc.Close
        End If
    Next
End Sub

错误处理与最佳实践

场景一:状态缓存异常处理

Function IsDocumentSaved(doc As Document) As Boolean
    On Error Resume Next
    IsDocumentSaved = doc.Saved
    If Err.Number <> 0 Then
        ' 处理损坏文档状态
        RepairDocument doc
        IsDocumentSaved = True  ' 保守假设
    End If
End Function

场景二:保存状态验证

Sub VerifySaveOperation()
    Dim doc As Document
    Set doc = CATIA.ActiveDocument
    
    Dim preSaveState As Boolean
    preSaveState = doc.Saved
    
    doc.Save
    
    If preSaveState = doc.Saved Then
        ' 未正确更新保存状态
        LogError "保存状态异常: " & doc.Name
        ForceStateUpdate doc
    End If
End Sub

场景三:多用户协作处理

Sub CollaborativeSave()
    Dim doc As Document
    Set doc = CATIA.ActiveDocument
    
    If Not doc.Saved Then
        ' 检查文件锁定状态
        If IsFileLocked(doc.FullName) Then
            Dim newPath As String
            newPath = GenerateCollaborativePath(doc)
            doc.SaveAs newPath
            NotifyTeam "文档已另存为: " & newPath
        Else
            doc.Save
        End If
    End If
End Sub

性能优化指南

场景一:批量操作优化

Sub BulkProcessing()
    Dim doc As Document
    Set doc = CATIA.ActiveDocument
    
    ' 暂停状态检查
    Dim origState As Boolean
    origState = doc.Saved
    Application.EnableStateTracking = False
    
    ' 执行批量操作
    For i = 1 To 1000
        CreateFeature i
    Next
    
    ' 恢复并更新状态
    Application.EnableStateTracking = True
    doc.SetModified  ' 手动标记为修改状态
End Sub

场景二:大型装配优化

Sub ProcessLargeAssembly()
    Dim doc As Document
    Set doc = CATIA.ActiveDocument
    
    ' 保存原始状态
    Dim origSavedState As Boolean
    origSavedState = doc.Saved
    
    ' 最小化状态更新
    doc.Product.UpdateMode = catManualUpdate
    
    ' 批量修改
    ModifyComponents doc.Product
    
    ' 恢复状态
    doc.Product.UpdateMode = catAutomaticUpdate
    doc.Product.Update
    
    ' 手动更新保存状态
    If origSavedState Then
        doc.Saved = True  ' 仅当原始状态为已保存时
    End If
End Sub

场景三:状态轮询优化

Sub EfficientModificationCheck()
    ' 使用事件驱动而非轮询
    Dim listener As ModificationListener
    Set listener = CreateModificationListener(CATIA.ActiveDocument)
    
    ' 注册修改事件处理
    listener.OnModification = GetRef("HandleDocumentModification")
End Sub

Sub HandleDocumentModification()
    UpdateStatusIndicator
    StartAutoSaveTimer
End Sub

特殊场景处理

场景一:恢复崩溃后的文档

Sub RecoverAfterCrash()
    For Each doc In CATIA.Documents
        If Not doc.Saved Then
            If MsgBox("发现未保存文档: " & doc.Name & ",是否恢复?", vbYesNo) = vbYes Then
                doc.SaveAs "C:Recovered" & doc.Name
            End If
        End If
    Next
End Sub

场景二:只读文档处理

Sub HandleReadOnlyDocument()
    Dim doc As Document
    Set doc = CATIA.ActiveDocument
    
    If doc.ReadOnly And Not doc.Saved Then
        Dim newPath As String
        newPath = "C:EditableCopies" & Format(Now, "yyyymmdd_hhnn") & "_" & doc.Name
        
        doc.SaveAs newPath
        doc.Close
        CATIA.Documents.Open newPath
    End If
End Sub

场景三:外部修改检测

Sub DetectExternalChanges()
    Dim doc As Document
    Set doc = CATIA.ActiveDocument
    
    ' 检查文件系统修改时间
    Dim lastModified As Date
    lastModified = GetFileModificationTime(doc.FullName)
    
    If lastModified > doc.LastSaveTime And doc.Saved Then
        If MsgBox("文件已被外部修改,重新加载?", vbYesNo) = vbYes Then
            doc.Reload
        Else
            doc.Saved = False  ' 手动标记为未保存
        End If
    End If
End Sub

总结
Document.Saved 属性是 CATIA VBA 自动化中文档状态管理的基石,关键应用包括: 1、智能保存:仅在必要时执行保存操作; 2、数据保护:防止意外关闭导致数据丢失; 3、工作流控制:基于状态触发不同操作; 4、资源优化:避免处理未修改文档; 5、协作管理:处理多用户编辑冲突。
最佳实践建议: 1、关键操作前检查:关闭、切换文档前验证状态; 2、结合用户提示:重要修改请求用户确认; 3、状态缓存优化:减少频繁访问提升性能; 4、异常处理:健壮处理文档损坏情况; 5、企业集成:与 PDM/PLM 系统深度整合。
通过合理应用此属性,可以构建高效可靠的自动化系统,显著提升设计流程的稳定性和用户体验。

3、SeeHiddenElements属性

在 CATIA VBA 开发中,Document.SeeHiddenElements 属性 是控制隐藏元素可见性的关键设置。这个布尔属性决定了当前文档中是否显示被用户或系统隐藏的元素(如被其他几何体遮挡的零件、隐藏的参考平面等)。
正确使用此属性对于需要访问或操作隐藏元素的自动化任务至关重要。

属性的核心作用
这个布尔属性决定了当前文档中是否显示被用户或系统隐藏的元素(如被其他几何体遮挡的零件、隐藏的参考平面等)。
基本用法

Property SeeHiddenElements As Boolean

可读写:是
默认值:False(不显示隐藏元素)
功能:设置或获取隐藏元素的显示状态

核心功能与应用场景

场景一:临时显示隐藏元素

Sub ShowHiddenElementsTemporarily()
    Dim doc As Document
    Set doc = CATIA.ActiveDocument
    
    ' 保存原始设置
    Dim originalState As Boolean
    originalState = doc.SeeHiddenElements
    
    ' 显示隐藏元素
    doc.SeeHiddenElements = True
    doc.Part.Update  ' 刷新视图
    
    ' 执行需要操作隐藏元素的任务
    ProcessHiddenElements
    
    ' 恢复原始设置
    doc.SeeHiddenElements = originalState
    doc.Part.Update
End Sub

Sub ProcessHiddenElements()
    ' 在此方法中操作隐藏元素
End Sub

场景二:检查元素隐藏状态

Function IsElementHidden(elem As AnyObject) As Boolean
    Dim doc As Document
    Set doc = CATIA.ActiveDocument
    
    ' 保存原始设置
    Dim originalState As Boolean
    originalState = doc.SeeHiddenElements
    
    ' 确保隐藏元素可见
    doc.SeeHiddenElements = True
    doc.Part.Update
    
    ' 检查元素可见性
    IsElementHidden = (elem.Visibility = catVisModeHide)
    
    ' 恢复原始设置
    doc.SeeHiddenElements = originalState
    doc.Part.Update
End Function

场景三:批量处理隐藏元素

Sub ProcessAllHiddenElements()
    Dim doc As Document
    Set doc = CATIA.ActiveDocument
    
    ' 启用隐藏元素可见
    doc.SeeHiddenElements = True
    doc.Part.Update
    
    ' 获取所有几何元素
    Dim allElements As Collection
    Set allElements = GetAllGeometricElements(doc)
    
    ' 处理隐藏元素
    Dim elem As AnyObject
    For Each elem In allElements
        If elem.Visibility = catVisModeHide Then
            ProcessHiddenElement elem
        End If
    Next
    
    ' 恢复设置
    doc.SeeHiddenElements = False
    doc.Part.Update
End Sub

关键技术细节

场景一:

' 手动隐藏元素
elem.Visibility = catVisModeHide

' 即使 SeeHiddenElements=True,以下元素仍不可见:
' - 被图层隐藏的元素
' - 被父组件隐藏的子元素
' - 被显示模式过滤的元素

场景二:刷新要求 修改属性后必须刷新视图才能生效:

' 零件文档
doc.SeeHiddenElements = True
doc.Part.Update

' 装配文档
doc.SeeHiddenElements = True
doc.Product.Update

' 工程图文档
doc.SeeHiddenElements = True
doc.Sheets.ActiveSheet.Update

场景三:作用域范围 1、仅影响当前文档窗口; 2、新窗口默认继承当前设置; 3、不影响其他已打开的文档。
场景四:性能影响 在大型装配中启用可能降低性能:

' 优化大型装配性能
Sub OptimizedHiddenView()
    Dim doc As Document
    Set doc = CATIA.ActiveDocument
    
    ' 保存原始设置
    Dim origMode As Long
    origMode = CATIA.ActiveWindow.ActiveViewer.DisplayMode
    
    ' 最小化性能影响
    CATIA.ActiveWindow.ActiveViewer.DisplayMode = catWireFrameMode
    Application.RefreshDisplay = False
    
    ' 显示隐藏元素
    doc.SeeHiddenElements = True
    If doc.Type = "Product" Then doc.Product.Update
    
    ' 执行操作...
    ProcessCriticalHiddenElements
    
    ' 恢复设置
    Application.RefreshDisplay = True
    CATIA.ActiveWindow.ActiveViewer.DisplayMode = origMode
    doc.SeeHiddenElements = False
    If doc.Type = "Product" Then doc.Product.Update
End Sub

高级应用示例

场景一:隐藏元素选择器

Function SelectHiddenElement(prompt As String) As AnyObject
    Dim doc As Document
    Set doc = CATIA.ActiveDocument
    
    ' 保存原始设置
    Dim originalState As Boolean
    originalState = doc.SeeHiddenElements
    
    ' 显示隐藏元素
    doc.SeeHiddenElements = True
    doc.Part.Update
    
    ' 提示用户选择
    Dim sel As Selection
    Set sel = doc.Selection
    sel.Clear
    sel.SelectElement2 prompt, "", False
    
    ' 获取选择结果
    If sel.Count > 0 Then
        Set SelectHiddenElement = sel.Item(1).Value
    Else
        Set SelectHiddenElement = Nothing
    End If
    
    ' 恢复原始设置
    doc.SeeHiddenElements = originalState
    doc.Part.Update
End Function

场景二:隐藏元素报告生成

Sub GenerateHiddenElementsReport()
    Dim doc As Document
    Set doc = CATIA.ActiveDocument
    
    ' 启用隐藏元素可见
    doc.SeeHiddenElements = True
    doc.Part.Update
    
    ' 创建报告
    Dim report As String
    report = "隐藏元素报告: " & doc.Name & vbCrLf & vbCrLf
    report = report & "名称 | 类型 | 隐藏原因" & vbCrLf
    report = report & "------------------------------" & vbCrLf
    
    ' 遍历所有元素
    Dim elem As AnyObject
    For Each elem In GetAllElements(doc)
        If elem.Visibility = catVisModeHide Then
            report = report & elem.Name & " | " & TypeName(elem) & " | " & _
                     GetHideReason(elem) & vbCrLf
        End If
    Next
    
    ' 保存报告
    SaveTextToFile report, "C:Reports" & doc.Name & "_HiddenElements.txt"
    
    ' 恢复设置
    doc.SeeHiddenElements = False
    doc.Part.Update
End Sub

场景三:自动修复隐藏依赖

Sub FixHiddenDependencies()
    Dim doc As Document
    Set doc = CATIA.ActiveDocument
    
    ' 显示隐藏元素
    doc.SeeHiddenElements = True
    doc.Part.Update
    
    ' 查找被隐藏的参考元素
    Dim brokenRefs As Collection
    Set brokenRefs = FindBrokenReferences(doc)
    
    ' 修复每个依赖
    Dim refElem As Reference
    For Each refElem In brokenRefs
        FixReference refElem
    Next
    
    ' 恢复设置
    doc.SeeHiddenElements = False
    doc.Part.Update
End Sub

企业级应用场景

场景一:设计审查准备

Sub PrepareForDesignReview()
    Dim doc As Document
    Set doc = CATIA.ActiveDocument
    
    ' 显示所有元素
    doc.SeeHiddenElements = True
    
    ' 显示所有图层
    Dim layer As Layer
    For Each layer In doc.Layers
        layer.Visible = True
    Next
    
    ' 更新视图
    If doc.Type = "Part" Then
        doc.Part.Update
    ElseIf doc.Type = "Product" Then
        doc.Product.Update
    End If
    
    ' 缩放到全部可见
    CATIA.ActiveWindow.ActiveViewer.FitAllIn
    
    ' 保存审查快照
    CaptureViewToFile "C:Reviews" & doc.Name & "_FullView.jpg"
End Sub

场景二:隐藏元素清理工具

Sub CleanUnusedHiddenElements()
    Dim doc As Document
    Set doc = CATIA.ActiveDocument
    
    ' 显示隐藏元素
    doc.SeeHiddenElements = True
    doc.Part.Update
    
    ' 查找未使用的隐藏元素
    Dim unusedElements As Collection
    Set unusedElements = FindUnusedElements(doc)
    
    ' 删除确认
    If unusedElements.Count > 0 Then
        If MsgBox("发现 " & unusedElements.Count & " 个未使用的隐藏元素,是否删除?", _
                  vbYesNo) = vbYes Then
            DeleteElements unusedElements
        End If
    End If
    
    ' 恢复设置
    doc.SeeHiddenElements = False
    doc.Part.Update
End Sub

场景三:质量控制检查

Sub QualityCheckHiddenElements()
    Dim doc As Document
    Set doc = CATIA.ActiveDocument
    
    ' 启用隐藏元素可见
    doc.SeeHiddenElements = True
    doc.Part.Update
    
    Dim errorCount As Integer
    errorCount = 0
    
    ' 检查隐藏的制造特征
    If CheckHiddenManufacturingFeatures(doc) Then
        LogError "发现隐藏的制造特征"
        errorCount = errorCount + 1
    End If
    
    ' 检查隐藏的参考几何
    If CheckHiddenReferenceGeometry(doc) Then
        LogError "发现隐藏的关键参考几何"
        errorCount = errorCount + 1
    End If
    
    ' 生成报告
    GenerateQCReport errorCount
    
    ' 恢复设置
    doc.SeeHiddenElements = False
    doc.Part.Update
End Sub

最佳实践与错误处理

场景一:状态安全恢复

Sub SafeHiddenElementsOperation()
    Dim doc As Document
    Set doc = CATIA.ActiveDocument
    
    ' 保存原始设置
    Dim originalState As Boolean
    originalState = doc.SeeHiddenElements
    
    On Error GoTo ErrorHandler
    
    ' 启用隐藏元素
    doc.SeeHiddenElements = True
    If doc.Type = "Part" Then doc.Part.Update
    
    ' 执行操作...
    CriticalOperation
    
ErrorHandler:
    ' 确保状态恢复
    doc.SeeHiddenElements = originalState
    If doc.Type = "Part" Then doc.Part.Update
    If Err.Number <> 0 Then Err.Raise Err.Number
End Sub

场景二:性能优化模式

Sub HighPerformanceHiddenAccess()
    Dim doc As Document
    Set doc = CATIA.ActiveDocument
    
    ' 保存原始设置
    Dim origMode As Long
    origMode = CATIA.ActiveWindow.ActiveViewer.DisplayMode
    
    ' 优化设置
    CATIA.ActiveWindow.ActiveViewer.DisplayMode = catWireFrameMode
    Application.RefreshDisplay = False
    
    ' 显示隐藏元素
    doc.SeeHiddenElements = True
    doc.Part.Update
    
    ' 快速操作
    FastScanHiddenElements
    
    ' 恢复设置
    Application.RefreshDisplay = True
    CATIA.ActiveWindow.ActiveViewer.DisplayMode = origMode
    doc.SeeHiddenElements = False
    doc.Part.Update
End Sub

场景三:用户确认机制

Sub UserControlledHiddenView()
    Dim doc As Document
    Set doc = CATIA.ActiveDocument
    
    ' 检查是否有隐藏元素
    If HasHiddenElements(doc) Then
        If MsgBox("文档包含隐藏元素,是否显示?", vbYesNo) = vbYes Then
            doc.SeeHiddenElements = True
            If doc.Type = "Part" Then doc.Part.Update
        End If
    End If
End Sub

Function HasHiddenElements(doc As Document) As Boolean
    ' 此函数需要实现实际检查逻辑
    ' 简化的实现示例:
    On Error Resume Next
    HasHiddenElements = (doc.Part.HybridBodies.Count > 0)
End Function

特殊场景处理

场景一:跨文档隐藏元素访问

Sub AccessHiddenInAssembly()
    Dim asmDoc As Document
    Set asmDoc = CATIA.ActiveDocument ' 当前为装配文档
    
    ' 获取子零件文档
    Dim partDoc As Document
    Set partDoc = asmDoc.Product.Products.Item(1).ReferenceProduct.Parent
    
    ' 在子零件中显示隐藏元素
    partDoc.SeeHiddenElements = True
    partDoc.Part.Update
    
    ' 操作子零件的隐藏元素
    ProcessPartHiddenElements partDoc
    
    ' 恢复设置
    partDoc.SeeHiddenElements = False
    partDoc.Part.Update
End Sub

场景二:工程图隐藏元素处理

Sub ProcessHiddenDrawingElements()
    Dim drawingDoc As DrawingDocument
    Set drawingDoc = CATIA.ActiveDocument
    
    ' 显示隐藏元素
    drawingDoc.SeeHiddenElements = True
    
    ' 处理所有图纸
    Dim sheet As DrawingSheet
    For Each sheet In drawingDoc.Sheets
        sheet.Activate
        
        ' 更新当前图纸
        sheet.Update
        
        ' 处理隐藏元素
        ProcessHiddenViews sheet
    Next
    
    ' 恢复设置
    drawingDoc.SeeHiddenElements = False
    drawingDoc.Sheets.Item(1).Activate ' 返回第一张图纸
End Sub

场景三:与选择过滤器的结合使用

Sub SelectHiddenWithFilter()
    Dim doc As Document
    Set doc = CATIA.ActiveDocument
    
    ' 显示隐藏元素
    doc.SeeHiddenElements = True
    doc.Part.Update
    
    ' 创建选择过滤器
    Dim sel As Selection
    Set sel = doc.Selection
    sel.Clear
    
    Dim filter As Filter
    Set filter = sel.CreateFilter()
    filter.FilterType = catHiddenElementFilter
    filter.Value = catHiddenElement
    
    ' 选择所有隐藏元素
    sel.Search filter
    
    ' 处理选中的隐藏元素
    If sel.Count > 0 Then
        ProcessSelectedHiddenElements sel
    End If
    
    ' 恢复设置
    doc.SeeHiddenElements = False
    doc.Part.Update
End Sub

总结
Document.SeeHiddenElements 属性是 CATIA VBA 中管理元素可见性的核心工具,主要应用于: 1、设计审查:全面查看模型所有元素; 2、:检查隐藏的制造特征; 3、依赖修复:解决因元素隐藏导致的参考丢失; 4、文档清理:识别并删除未使用的隐藏元素; 5、自动化检查:验证模型完整性。
使用时应遵循以下最佳实践: 1、状态保存与恢复:始终保留并恢复原始设置; 2、性能优化:大型模型中使用简化显示模式; 3、错误处理:确保异常时正确清理; 4、用户沟通:重要变更通知用户; 5、选择性启用:仅在必要时显示隐藏元素。
通过合理应用此属性,可以构建强大的自动化工具,提升设计质量和效率。

三、属性~文档状态属性(ReadOnly、Saved和SeeHiddenElements)对比

1、核心定义与功能对比

属性 数据类型 读写性 核心功能
ReadOnly Boolean 只读 判断文档是否以只读模式打开
Saved Boolean 只读 判断文档是否有未保存的修改
SeeHiddenElements Boolean 读写 控制是否显示文档中被隐藏的元素(几何图形、标注等)

2、属性特性与优缺点分析

属性 优点 缺点 风险等级
ReadOnly ✅ 快速判断编辑权限
✅ 避免保存冲突
✅ 跨版本稳定
❌ 无法直接修改状态
❌ 不反映PLM权限(3DX)
Saved ✅ 准确检测未保存修改
✅ 防止数据丢失
✅ 关闭前验证
⚠️ 延迟更新(复杂操作后)
⚠️ 不自动检测外部修改
SeeHiddenElements ✅ 全局控制隐藏元素
✅ 简化隐藏对象访问
✅ 无需遍历
⚠️ 可能暴露敏感数据
⚠️ 影响性能(大装配)
⚠️ 不持久化

3、版本演变与兼容性

版本平台 ReadOnly Saved SeeHiddenElements 关键变化
CATIA V5 (R8-R30) 检测操作系统文件权限 即时跟踪修改状态 完美支持隐藏几何/标注 无显著变化
3DEX R2014-2016x 增加PLM只读状态检测 增强PLM协作状态感知 支持PLM特有隐藏对象 增加PLM集成
R2018x+ 新增PLM权限服务集成 优化外部修改检测 性能优化(大装配) 解决延迟更新问题
R2022x+ 支持细粒度权限控制 新增AutoSave状态跟踪 支持选择性显示(图层/类型) 增强企业级功能

4、使用场合指南

使用场景 推荐属性 代码示例 注意事项
编辑前权限检查 ReadOnly If doc.ReadOnly Then Exit Sub 3DX需结合PLM权限服务
关闭前保存提示 Saved If Not doc.Saved Then doc.Save 复杂操作后手动刷新状态
访问隐藏基准元素 SeeHiddenElements doc.SeeHiddenElements = True 操作后恢复原始状态
批量导出所有几何 SeeHiddenElements ToggleHiddenElements(True)
ExportAllGeometry()
考虑性能影响
PLM协作环境权限验证 ReadOnly + PLM服务 CheckPLMEditPermission(doc) 跨平台兼容实现
自动保存触发器 Saved AutoSaveOnModify(doc) 避免频繁保存中断操作

5、版本迁移解决方案

场景一:跨平台只读检测

Function IsDocumentEditable(doc As Document) As Boolean
    ' 基础只读检测
    If doc.ReadOnly Then Exit Function
    
    ' 3DX环境PLM权限检查
    If Is3DXEnvironment() Then
        On Error Resume Next
        Dim plmService As Object
        Set plmService = doc.Application.GetService("PLMAccessService")
        If Not plmService Is Nothing Then
            IsDocumentEditable = plmService.HasWriteAccess(doc)
        End If
    Else
        IsDocumentEditable = True
    End If
End Function

场景二:增强型保存状态监控

Sub MonitorDocumentChanges(doc As Document)
    Static lastState As Boolean
    
    ' 强制刷新保存状态
    doc.Saved = doc.Saved
    
    ' 检测状态变化
    If lastState <> doc.Saved Then
        If doc.Saved Then
            LogAction "文档保存: " & doc.Name
        Else
            LogAction "文档修改: " & doc.Name
        End If
        lastState = doc.Saved
    End If
    
    ' R2022x+自动保存扩展
    If CATIA.Version >= "R2022" And Not doc.Saved Then
        If Timer - lastSaveTime > AUTO_SAVE_INTERVAL Then
            doc.SaveAs tempBackupPath & GetSafeFileName(doc)
        End If
    End If
End Sub

场景三:安全隐藏元素访问模板

Sub AccessHiddenElements(doc As Document, action As String)
    ' 保存原始状态
    Dim originalState As Boolean
    originalState = doc.SeeHiddenElements
    
    ' 应用临时可见性
    On Error Resume Next
    doc.SeeHiddenElements = True
    
    ' 执行操作
    Select Case action
        Case "Measure"
            MeasureHiddenGeometry doc
        Case "Export"
            ExportHiddenElements doc
        Case "Select"
            SelectCriticalHiddenFeatures doc
    End Select
    
    ' 恢复原始状态 (即使出错)
    doc.SeeHiddenElements = originalState
    If Err.Number <> 0 Then HandleError "隐藏元素访问失败"
End Sub

6、属性使用决策树

7、最佳实践与专家建议

场景一:ReadOnly 高级用法

' 企业级权限检查模板
Function CheckEditPermission(doc As Document) As Boolean
    ' 基础只读检查
    If doc.ReadOnly Then Exit Function
    
    ' 3DX PLM权限验证
    If Is3DXEnvironment() Then
        Dim plmAccess As Object
        Set plmAccess = doc.Application.GetService('PLMAccessService')
        CheckEditPermission = plmAccess.CheckRight(doc, 'MODIFY')
    Else
        ' V5扩展:网络文件锁定检查
        CheckEditPermission = Not IsFileLocked(doc.FullName)
    End If
End Function

场景二:Saved 状态可靠检测

' 强制刷新保存状态的方法
Sub RefreshSavedState(doc As Document)
    ' R2022x+ 官方方法
    If CATIA.Version >= "R2022" Then
        doc.UpdateSavedStatus
    Else
        ' 传统刷新技巧
        Dim temp As Boolean
        temp = doc.Saved
        doc.Saved = temp
    End If
End Sub

场景三:SeeHiddenElements 性能优化

' 大装配场景优化方案
Sub SafeToggleHiddenElements(doc As Document, state As Boolean)
    ' 仅当状态变化时执行
    If doc.SeeHiddenElements <> state Then
        ' 禁用屏幕刷新
        CATIA.RefreshDisplay = False
        
        ' 执行状态变更
        doc.SeeHiddenElements = state
        
        ' 分批更新视图 (大装配)
        If doc.Products.Count > 1000 Then
            PartialViewUpdate doc
        End If
        
        ' 恢复刷新
        CATIA.RefreshDisplay = True
    End If
End Sub

场景四:三属性联合使用模式

' 安全文档修改模板
Sub SafeDocumentModification(doc As Document, operation As String)
    ' 步骤1:权限检查
    If Not CheckEditPermission(doc) Then
        MsgBox "无编辑权限", vbExclamation
        Exit Sub
    End If
    
    ' 步骤2:保存原始状态
    Dim origSaved As Boolean
    origSaved = doc.Saved
    
    ' 步骤3:执行操作
    On Error GoTo ErrorHandler
    Select Case operation
        Case "AddFeatures": AddCustomFeatures doc
        Case "UpdateParams": UpdateDesignParameters doc
        Case "HideElements": HideCriticalElements doc
    End Select
    
    ' 步骤4:状态恢复
    If origSaved Then doc.Saved = True
    Exit Sub
    
ErrorHandler:
    ' 错误时恢复保存标记
    doc.Saved = origSaved
    ReportError Err.Description
End Sub

8、版本兼容性解决方案

兼容性问题 V5 解决方案 3DX 解决方案 跨平台适配器
PLM只读状态 文件属性检测 PLMAccessService.CheckRight() CheckEditPermission() 函数
外部修改检测 定时器+文件时间戳 PLMChangeService.GetModifications() EnhancedSavedMonitor() 模块
选择性显示隐藏元素 图层控制+逐个元素可见性 PLMVisibilityFilter 服务 SmartHideElements() 封装
自动保存 自定义定时保存 PLMAutoSaveService CrossPlatformAutoSave() 实现

9、企业级开发推荐

场景一:权限管理黄金法则

' 所有修改操作前必须检查
If Not CheckEditPermission(doc) Then
    ThrowError "DOC_READONLY", "Document is read-only"
End If

场景二:状态保存安全协议

' 修改文档时的状态处理
Sub BeginDocumentEdit(doc As Document)
    g_originalSavedState = doc.Saved
    g_docEditTimestamp = Now
End Sub

Sub EndDocumentEdit(doc As Document)
    If g_originalSavedState Then
        doc.Save
        doc.Saved = True
    Else
        doc.Saved = False ' 确保修改标记可见
    End If
End Sub

场景三:隐藏元素访问安全规范

' 企业级隐藏元素访问模板
Sub EnterpriseAccessHidden(doc As Document, operationType As String)
    ' 权限验证
    If Not UserHasRight("VIEW_HIDDEN") Then Exit Sub
    
    ' 审计日志
    LogSecurityEvent "ACCESS_HIDDEN", doc.Name, operationType
    
    ' 执行安全访问
    With New HiddenElementAccessor
        .Initialize doc
        .ExecuteOperation operationType
        .RestoreState
    End With
End Sub

终极建议:

在关键业务流程中始终组合使用这三个属性 – 先通过ReadOnly验证权限,再通过Saved管理修改状态,使用SeeHiddenElements时遵循”最小权限+即时恢复”原则。
在3DX环境中集成PLM服务增强功能,对V5保留传统实现路径。

上述部分代码有的未经各个版本测试,在参考使用时应经过再次调试。


推荐阅读:阅读本文请参考以下文章

001、【CATIA的二次开发01】技术与原理

002、【CATIA的二次开发02】CATIA对象结构图

003、【CATIA的二次开发03】零件设计工作台对象结构及应用

004、【CATIA的二次开发04】错误处理技巧

005、【CATIA的二次开发05】装配设计对象结构及应用

006、【CATIA的二次开发06】创成式曲面设计对象结构及应用

007、【CATIA的二次开发07】草图编辑器对象结构及应用

008、【CATIA的二次开发08】工程制图对象结构及应用

009、【CATIA的二次开发09】Collection、Abstract Object和Object区别

010、【CATIA的二次开发10】CATIA版本发展历程及其在VBA开发中相关背景

011、【CATIA的二次开发11】CATIA V5对象层次结构

012、【CATIA的二次开发12】根对象Application的Documents集合概述

013、【CATIA的二次开发13】根对象Application的Documents集合方法

014、【CATIA的二次开发14】根对象Application的Documents集合属性

015、【CATIA的二次开发15】根对象Application的Documents集合方法

016、【CATIA的二次开发16】根对象Application涉及撤销和重做事务管理相关方法

017、【CATIA的二次开发17】根对象Application涉及文档与文件操作相关方法

018、【CATIA的二次开发18】根对象Application涉及用户交互相关方法

019、【CATIA的二次开发19】根对象Application涉及对象与集合及邮件相关方法

020、【CATIA的二次开发20】根对象Application常用属性(ActiveDocument属性Caption属性Name属性)

021、【CATIA的二次开发21】根对象Application常用属性(StatusBar属性RefreshDisplay属性Application属性)

022、【CATIA的二次开发22】关于抽象对象Document概念详细总结

023、【CATIA的二次开发23】抽象对象Document涉及文档激活控制的方法

024、【CATIA的二次开发24】抽象对象Document涉及文档生命周期的方法

025、【CATIA的二次开发25】抽象对象Document涉及交互选择的方法

026、【CATIA的二次开发26】抽象对象Document涉及选择过滤、工作环境控制和数据交换的方法

027、【CATIA的二次开发27】抽象对象Document涉及对象引用的方法

028、【CATIA的二次开发28】抽象对象Document涉及应用程序连接的属性

029、【CATIA的二次开发29】抽象对象Document涉及文档标识的属性


感谢已关注! 🙏 很高兴您的阅读、已关注、收藏与支持!我会继续努力学习,持续分享我在学习编程过程中的一点经验,希望能为大家带来帮助。 如果有任何问题或建议,欢迎随时留言交流!一起学习,共同进步!💻🚀

© 版权声明
THE END
如果内容对您有所帮助,就支持一下吧!
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容