CATScript Delete Empty Part Bodies

A utility CATScript macro for CatiaV5 to clean up a all empty PartBodies in a CATPart.

Usage:

Assuming that one needs to clean e.g. a CATAllPart from all unnecessary geometry. After the clean operation (by selecting geometry visually on the screen rather than in the object tree) a lot of empty PartBodies might remain.

In such a case, executing the macro is much faster than going through the object tree to manually select and delete those empty PartBodies.

What is an empty PartBody

  • a 1st level PartBody,
  • which is not build together by a boolean operation,
  • and has no geometry behind the object.

Once the macro is executed, it traverses through all PartBodies available in the object model, trying to find empty PartBodies (as declared above).

If successful a dialog pops up, which gives some more information about the object search. If the object names are quite long, the information text is truncated.

Note: Depending on the actual model structure, most often the macro needs to be executed repetitively, so that no empty PartBody remains.

MainBody:

The MainBody has a special meaning in CatiaV5 and in most companies it is common practice to keep the MainBody left empty.

Hint: There are some exclusions of the rule, e.g. to be able to use a CATPart for a FEA analysis…

  • all geometry must be behind the MainBody (as the FEA logic only takes this geometry as an input).

  • Also make sure to have a material associated to the MainBody (otherwise the material needs to be specified in the FEA workbench, which is even more complicated).

As a consequence, the MainBody hereby is not processed or taken into account by the macro at all.

' ------------------------------------------------------------------------
' DeleteEmptyPartBodies.CATScript  ---
' ------------------------------------------------------------------------
' (c) 2010, Johann Oberdorfer - Engineering Support | CAD | Software
'     johann.oberdorfer [at] googlemail.com
'     www.johann-oberdorfer.eu
' ------------------------------------------------------------------------
'   This source file is distributed under the BSD license.
'   This program is distributed in the hope that it will be useful,
'   but WITHOUT ANY WARRANTY; without even the implied warranty of
'   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
'   See the BSD License for more details.
' ------------------------------------------------------------------------
' Purpose:
'    Utiltiy function to clean-up empty part bodies.
'
'    Proceed part bodies which meet the following criteria:
'      - appear on the 1st level of the specification tree,
'      - and which do not have any "Shapes" attached,
'      - the MainBody is not taken into account.
'
' ------------------------------------------------------------------------
' Revision History:
' June 2012: J.Oberdorfer, V0.1 - Initial Release
' ------------------------------------------------------------------------

Option Explicit
Language="VBSCRIPT"

' // utility function:
' //
Sub StrAppend ( ByRef obj_list() As String, ByVal str As String )
    ReDim Preserve obj_list (UBound(obj_list) + 1)
    obj_list(UBound(obj_list)) = str
End Sub


' // get all empty part bodies
' //
Function GetEmptyPartBodies ( _
            ByVal active_doc As Document, _
            ByRef obj_list() As String _
    ) As Boolean

    DIM i As Integer
    Dim b As Body
    Dim bodies As PartBodies
    Set bodies = active_doc.Part.Bodies
    GetEmptyPartBodies = False

    For i = 1 to bodies.count
        Set b = bodies.Item(i)

        If ( (b.InBooleanOperation = False) AND _
             (b.Shapes.Count = 0) And _
             (b.Name <> active_doc.Part.MainBody.Name) ) Then

            StrAppend obj_list, b.Name
        End If
    Next

    If ( UBound(obj_list) > 0 ) Then
        GetEmptyPartBodies = True
    End If
End Function


Sub CATMain()

    Dim i,j As Integer
    Dim msg As Sctring
    Dim obj_list() As String

    Dim sel As Collection
    Dim active_doc As Document
    ReDim obj_list(0)
    Set active_doc = CATIA.ActiveDocument
    Set sel = active_doc.Selection

    Select Case TypeName(active_doc)

    ' -----------------
    Case "PartDocument"
    ' -----------------

        If (GetEmptyPartBodies (active_doc, obj_list) = True ) Then

            If (Ubound(obj_list) = 1) Then
                msg = "There is 1 empty PartBody in the model:" + vbNewline
            Else
                msg = "There are " + CStr( UBound(obj_list) ) + " PartBodies in the model:"
            End If

            msg = msg + vbNewline + vbNewline
            For i = 1 to UBound(obj_list)
                msg = msg + obj_list(i) + " "
            Next
            msg = msg + vbNewline

            j = MsgBox ( _
                    msg + vbNewLine _
                    + "Do you want to clean up empty PartBodies ?", _
                    + vBYesNo + vBQuestion, "Question: Clean up of empty PartBodies..." )

            Select Case j
            Case 6
                ' YES:
                sel.Clear()

                For i = 1 to UBound(obj_list)
                    Dim obj As AnyObject
                    Set obj = active_doc.Part.Bodies.GetItem (obj_list(i))
                    sel.Add (obj)
                Next

                ' delete selected objects...
                ' --------------------------
                sel.Cut()
                ' --------------------------
            Case 7
                ' NO:
            End Select
        Else
            MsgBox "No empty Part Boidies in the model!"
        End If

    ' -----------------
    Case Else
        MsgBox "The active document must be a CATPart.", _
        vBCancelOnly + vBExclamation, "Warning:"
    End Select

End Sub