You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用CATIA宏获取选中特征子特征,实现停用圆角倒角简化零件

CATIA宏:批量停用圆角/倒角及依赖子特征方案

问题场景

尝试通过宏停用零件中的圆角(edge fillet)和倒角(chamfer)特征以简化模型,但原代码仅直接停用目标特征,若存在后续特征依赖这些圆角/倒角,零件将无法正常更新。需要获取特征的子特征依赖关系,批量停用所有关联特征。

原宏代码

Sub CATMain()

Dim partDocument1 As Document
Set partDocument1 = CATIA.ActiveDocument

Dim part1 As Part
Set part1 = partDocument1.Part

If Err.Number = 0 Then
    Dim selection1 As Selection
    Set selection1 = partDocument1.Selection
    
    selection1.Search "Name=*fillet*,all"
    
    If selection1.Count = 0 Then
        MsgBox "No fillet features"
        
    Else
        For i = 1 To selection1.Count
            part1.Inactivate (selection1.Item2(i).Value)
        Next 'i
        'part1.Update
    End If
    
    Dim selection2 As Selection
    Set selection2 = partDocument1.Selection
    
    selection2.Search "Name=*chamfer*,all"
    
    If selection2.Count = 0 Then
        MsgBox "No chamfer features"
        
    Else
        For j = 1 To selection2.Count
            part1.Inactivate (selection2.Item2(j).Value)
        Next 'j
        'part1.Update
    End If
    
    part1.Update
    
    MsgBox ( tot_f_c_num & " features are inactivated.")

Else
    MsgBox "Not a part document! Open a single part document."
End If

End Sub

解决思路与改进代码

核心逻辑

利用CATIA Feature对象的Children属性,可直接获取该特征的所有子特征(即依赖于它的后续特征)。通过递归遍历子特征集合,实现目标特征及所有依赖特征的批量停用,避免更新冲突。

优化后宏代码

Sub CATMain()
    Dim partDocument1 As Document
    Set partDocument1 = CATIA.ActiveDocument
    
    Dim part1 As Part
    Set part1 = partDocument1.Part
    
    If Err.Number = 0 Then
        Dim totalInactivated As Integer
        totalInactivated = 0
        
        ' 处理圆角特征及依赖子特征
        totalInactivated = totalInactivated + ProcessTargetFeatures(part1, "*fillet*")
        ' 处理倒角特征及依赖子特征
        totalInactivated = totalInactivated + ProcessTargetFeatures(part1, "*chamfer*")
        
        part1.Update
        MsgBox totalInactivated & " features are inactivated."
    Else
        MsgBox "Not a part document! Open a single part document."
    End If
End Sub

' 批量处理指定名称匹配的特征,包含其所有子特征
Function ProcessTargetFeatures(part As Part, namePattern As String) As Integer
    Dim count As Integer
    count = 0
    
    Dim selection As Selection
    Set selection = CATIA.ActiveDocument.Selection
    selection.Clear
    selection.Search "Name=" & namePattern & ",all"
    
    If selection.Count = 0 Then
        MsgBox "No features matching " & namePattern
        ProcessTargetFeatures = 0
        Exit Function
    End If
    
    ' 遍历每个目标特征,处理自身及子特征
    Dim i As Integer
    For i = 1 To selection.Count
        Dim targetFeat As Feature
        Set targetFeat = selection.Item2(i).Value
        
        ' 停用当前目标特征
        If targetFeat.IsActive Then
            part.Inactivate targetFeat
            count = count + 1
        End If
        
        ' 递归停用所有子特征
        count = count + InactivateChildFeatures(part, targetFeat)
    Next i
    
    ProcessTargetFeatures = count
End Function

' 递归停用特征的所有层级子特征
Function InactivateChildFeatures(part As Part, parentFeat As Feature) As Integer
    Dim count As Integer
    count = 0
    
    Dim childFeat As Feature
    ' 遍历父特征的所有直接子特征
    For Each childFeat In parentFeat.Children
        If childFeat.IsActive Then
            part.Inactivate childFeat
            count = count + 1
        End If
        ' 递归处理子特征的下一级子特征
        count = count + InactivateChildFeatures(part, childFeat)
    Next childFeat
    
    InactivateChildFeatures = count
End Function

代码说明

  1. ProcessTargetFeatures函数:统一处理圆角/倒角特征的搜索,调用递归函数处理每个特征及其依赖子特征,避免重复代码。
  2. InactivateChildFeatures递归函数:遍历特征的Children集合,逐层停用所有依赖子特征,确保无遗漏。
  3. 计数修复:新增totalInactivated变量统计总停用特征数,解决原代码中tot_f_c_num未定义的问题。

内容的提问来源于stack exchange,提问作者leeeeee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 05:01:35