Code:
'vbaOption Explicit
Private Const NEAR_LENGTH = 0.001
Sub CATMain()
Dim msg As String
msg = "Please select a Face : ESC key Exit"
Dim targetFace As SelectedElement
Set targetFace = select_element(msg, Array("Face"))
If targetFace Is Nothing Then Exit Sub
Dim faces As Variant
faces = get_body_faces(targetFace)
Call remove_select_faces( _
get_far_items_index( _
targetFace, _
faces, _
NEAR_LENGTH _
) _
)
MsgBox "Done"
End Sub
Private Function get_body_faces( _
ByRef selElm As SelectedElement _
)
Dim parentBody As AnyObject
Set parentBody = get_parent_of_T( _
selElm.value, _
"Body" _
)
Dim sel As Selection
Set sel = CATIA.ActiveDocument.Selection
CATIA.HSOSynchronized = False
sel.Clear
sel.Add parentBody
sel.Search "Topology.CGMFace,sel"
Dim items() As Variant
ReDim items(sel.Count2)
Dim i As Long
For i = 1 To sel.Count2
Set items(i) = sel.Item2(i)
Next
CATIA.HSOSynchronized = True
get_body_faces = items
End Function
Private Function get_far_items_index( _
ByRef selElm As SelectedElement, _
ByRef aryElms As Variant, _
ByVal lng As Double _
) As Collection
Dim ref As Reference
Set ref = selElm.Reference
Dim searchRef() As Variant
ReDim searchRef(UBound(aryElms))
Dim i As Long
For i = 1 To UBound(aryElms)
Set searchRef(i) = aryElms(i).Reference
Next
Dim pt As part
Set pt = selElm.Document.part
Dim mes As Measurable
Set mes = pt.Parent.GetWorkbench("SPAWorkbench") _
.getMeasurable(ref)
Dim farItems As Collection
Set farItems = New Collection
For i = UBound(aryElms) To 1 Step -1
If mes.GetMinimumDistance(searchRef(i)) > lng Then
Call farItems.Add(i)
End If
Next
Set get_far_items_index = farItems
End Function
Private Sub remove_select_faces( _
ByVal lst As Collection)
Dim sel As Selection
Set sel = CATIA.ActiveDocument.Selection
CATIA.HSOSynchronized = False
Dim idx
For Each idx In lst
sel.Remove2 idx
Next
CATIA.HSOSynchronized = True
End Sub
Private Function select_element( _
ByVal msg As String, _
ByVal filter As Variant _
) As SelectedElement
Dim sel As Variant
Set sel = CATIA.ActiveDocument.Selection
sel.Clear
Select Case sel.SelectElement2(filter, msg, False)
Case "Cancel", "Undo", "Redo"
Exit Function
End Select
Set select_element = sel.Item(1)
sel.Clear
End Function
Private Function get_parent_of_T( _
ByVal aoj As AnyObject, _
ByVal t As String) _
As AnyObject
Dim aojName As String
Dim parentName As String
On Error Resume Next
aojName = aoj.name
parentName = aoj.Parent.name
On Error GoTo 0
If TypeName(aoj) = TypeName(aoj.Parent) And _
aojName = parentName Then
Set get_parent_of_T = Nothing
Exit Function
End If
If TypeName(aoj) = t Then
Set get_parent_of_T = aoj
Else
Set get_parent_of_T = get_parent_of_T(aoj.Parent, t)
End If
End Function