All CodeSnippets are tested under CatiaV5 Release 19 and english user Interface.

If you use another release or different user interface language, maybe the snippets don’t work correct and/or you get an error message.


Define GeometricalSet as workobject

MyPart.InWorkObject = MyGS

Delete Arrays

General:

Erase Array

You can also this at dynamic arrays:

Redim  Array(0)

Delete Objects

Delete with Selection:

MySelection.Clear
MySelection.Add MyElement
MySelection.Delete

Delete with DeleteObjectForDatum:

Set Ref = MyPart.CreateReferenceFromObject(MyElement)
MyHybShapeFactory.DeleteObjectForDatum MyElement

Get internal name

Get internal name:

Function Internal_Name(MyObjekt As Object) As String
  On Error Resume Next
  Dim MyElement As ModelElement
  Set MyElement = MyObjekt.GetItem("ModelElement")
  Internal_Name = MyElement.InternalName
  If Err.Number <> 0 Then
      Internal_Name = vbNullString
  End If
End Function

inverse boolean variables

BooleanVariable = Not (BooleanVariable)

Replace Elements with Join

Second Method – Replace Elements with Join

Advantage: faster and more stable as the first method (no need for Win32 API-Function)

Disadvantage: elements must be joinable, so you can’t use e.g. points or planes; exklusive parents can’t be deleted automaticly

insert this procedure in a public module
the procedure need  2 parameters:

1. MyJoinToRefresh [Join] – Join, which elements should be replaced
2. MyJoinNewInput [Join] – elements of this join will be replace the elements of MyJoinToRefresh

Sub JoinReplace(MyJoinToRefresh As HybridShapeAssemble, MyJoinNewInput As 
HybridShapeAssemble)
        
    Dim JoinElement As Reference
        
    ' delete all old elements
    For i = 1 To MyJoinToRefresh.GetElementsSize 'To elements count
        MyJoinToRefresh.RemoveElement 1
    Next

    ' read new elemnts and add to join
    For i = 1 To MyJoinNewInput.GetElementsSize
        Set JoinElement = MyJoinNewInput.GetElement(i)
        MyJoinToRefresh.AddElement JoinElement
    Next
    
    MyPart.UpdateObject MyJoinToRefresh 'update
    
    ' MyJoinNewInput delete
    Set ObjektReferenz = MyPart.CreateReferenceFromObject(MyJoinNewInput)
    MyHybShapeFactory.DeleteObjectForDatum MyJoinNewInput

End Sub

Example to insert the procedure into your own code:

JoinReplace MyJoin, MyJoin2

Replace Elements with StartCommand

First Method – Replace with StartCommand

Advantage: native Replace-Command from Catia, all functions are useable

Disadvantage: use Win32 API-Function, because of this it is not so stable and also a little slower as the second method

insert this code infront of a public module:

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (Byval lpClassName As String, Byval lpWindowName As String) As Long

Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (Byval hwnd As Long, Byval lpClassName As String, Byval nMaxCount As Long) As Long

Public Declare Function EnumChildWindows Lib "user32" (Byval hWndParent As Long, Byval lpEnumFunc As Long, Byval lParam As Long) As Long

Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (Byval hwnd As Long, Byval wMsg As Long, Byval wParam As Long, Byval lParam As String) As Long

Public Declare Sub Sleep Lib "kernel32" (Byval dwMilliseconds As Long)

Public Const WM_GETTEXT = &HD

Public Const BM_CLICK = &HF5&

Public O_hwnd As Long

Public C_hwnd As Long

These functions must be also inserted in a public module:

Public Function FindButtonCheck(ByVal hChild As Long, ByVal lParam As Long) As Long

    Dim iClass As String
    Dim iText As String
    Dim j As Integer
   
    'find all subelements of window
    iClass = Space(256)
    j = GetClassName(hChild, iClass, 63)
    iClass = Left(iClass, j)
    iText = Space(256)
    j = SendMessage(hChild, WM_GETTEXT, 255, iText)
    iText = Left(iText, j)

   
    If InStr(1, iText, "Delete") > 0 Then  'test whether Checkbox or not
        O_hwnd = hChild
        SendMessage O_hwnd, BM_CLICK, 0, 0
    End If
   
    FindButtonCheck = 1
   
End Function
Public Function FindButtonOK(ByVal hChild As Long, ByVal lParam As Long) As Long
    
   Dim iClass As String
   Dim iText As String
   Dim j As Integer
   
    'find all subelements of window
    iClass = Space(256)
    j = GetClassName(hChild, iClass, 63)
    iClass = Left(iClass, j)
    iText = Space(256)
    j = SendMessage(hChild, WM_GETTEXT, 255, iText)
    iText = Left(iText, j)

    If iText = "OK" Then 'test whether OK-Button or not 
    O_hwnd = hChild SendMessage O_hwnd, BM_CLICK, 0, 0 'click Button 
    End If 
   
    FindButtonOK = 1 

End Function

Now the procedure to call the function.

The procedure has 3 parameters:
1. ElementToReplace [Object] – Element, which should be replaced
2. ReplaceWith [Object] – Element, which will replace “ElementToReplace”
3. Delete [Boolean] – you can decide whether exclusive parents will be deleted (true) or not (false)

Sub ElementReplace(ElementToReplace As Object, ReplaceWith As Object, Delete As Boolean)

    'ElementToReplace selektieren
    MySelection.Clear
    MySelection.Add ElementToReplace

    'Replacebefehl starten
    CATIA.StartCommand "CATReplaceHdr"
    CATIA.RefreshDisplay = True

    'wait for window open
    Do While FensterObjekt = 0
        Sleep (500)
        FensterObjekt = FindWindow(vbNullString, "Replace")
    Loop

    'select Element "ReplaceWith"
    MySelection.Clear
    MySelection.Add ReplaceWith
    MySelection.Search "Name='" & ReplaceWith.Name & "' ,sel"
    Sleep (200)
    
    ' if exclusive parents should be deleted - select checkbox
    If Delete = True Then
        EnumChildWindows FensterObjekt, AddressOf FindButtonCheck, 0
        Sleep (200)
    End If
    
    z = 0 'avoid endless loop
    Do While FensterObjekt <> 0 And z < 10 'wait for window closed or 5 seconds past
        z = z + 1
        EnumChildWindows FensterObjekt, AddressOf FindButtonOK, 0 'OK-Button druecken
        Sleep (500)
        FensterObjekt = FindWindow(vbNullString, "Replace")
    Loop

End Sub

Example to insert the procedure into your own code:

   'change element "ElementToReplace" into object
    Dim MyObject_01 As Object
    Set MyObject_01 = Element01
    
    'change element "ReplaceWith" into object
    Dim MyObject_02 As Object
    Set MyObject_02 = Element02
    
    'call procedure

    ElementReplace MyObject_01, MyObject_02, True