CATIA V5 Automation

Checking data types at runtime


In this article, I will show a simple way to check any data type using the Visual Basic language.  If you don’t already know how to check data types, I think this article will prove to be very useful for you.  I use this technique very often when I write code and I suspect you will as well.

The TypeName Function

The TypeName function is very simple to use.  You pass any variable to the function and it returns the type of data assigned to that variable.  It is most often used with objects but it can be used with value types as well.  Value types are data types that hold values like string, integer, double and so on rather than references to objects.  The general syntax goes like this,

strType = TypeName(variable)

An example

Let’s look at it in action with a very common example.  Often times you might record a macro with a CATPart active and open in its own window – all goes smoothly and the macro replays fine.  Then the next day you replay it again but this time you may have some other document type open or maybe a part is open but it is in a product assembly.  Usually the macro will fail because when the code was recorded, a part was the active document, but now it is not.  This is a great opportunity to use TypeName to check the type of the active document so you can take additional actions to either have the user select a part or end the program smoothly.  Here is an example code snippet:

'Declare variables
Dim strType As String
Dim strMsg As String
Dim objPart As Part
Dim strReturn As String
Dim objSel As Object
Dim varFilter(0) As Variant

'Get the type of the active document
strType = TypeName(CATIA.ActiveDocument)

'Decide what to do based on the active doc type
Select Case strType

Case “PartDocument”
   'If a part is the active document then get the part object
   'from the active document
   Set objPart = CATIA.ActiveDocument.Part

Case “ProductDocument”
   'If a product document is active, assume there is a part in
   'the assembly that the user wants to run the program on.

   'Give the user a warning to let them know they
   'will need to select a part.
   strMsg = “This program requires a part!” &vbcrlf &vbcrlf
   strMsg = strMsg &“After clicking OK, please select a part in the assembly…”
   Msgbox strMsg, 64, “Instructions”

   'Begin an interactive selection to get the part.  Use a filter
   'to only allow the user to either select a part or they may cancel.
   varFilter(0) = “Part”
   Set objSel = CATIA.ActiveDocument.Selection
   strReturn = objSel.SelectElement2(varFilter, “Select a part…”, false)

   'Check the result of the interactive selection
   If strReturn = “Normal” Then
      'The selection was successful, so get the selected part
      Set objPart = objSel.Item2(1).Value
      'The selection failed or was canceled by the user,
      'so end the program early.
      Msgbox “Selection failed so the program will end.”, 64, “Selection failed”
      Exit Sub
   End If

Case Else
   'Some other type of document is active (maybe a drawing or other)
   'so just warn the user and end the program early
   strMsg = “This program only works with a part.  Please try again…”
   Msgbox strMsg, 64, “Part required”
   Exit Sub

End Select

'If the code execution gets to here without exiting early, then objPart must
'be a valid part object and we can carry on with the rest of the script

'I will simply display the name of the part.
Msgbox objPart.Name


In the example above, you can see that the TypeName function is very easy to use.  It is just one line of code.  However, the rest of the example is much longer.  This is because you need to think ahead and form a plan of what you intend to do if the data type you checked is not the one you expect.  The minimum action taken should be to immediately end the program because if you continue an error is sure to result and the user will get an ugly error message.  Not good.

If you have the time and the desire to write quality code, by all means do it!  The users of your programs will appreciate it, believe me.  This is the path I took in the example above.  Sure, it took a few more minutes to write the extra lines of code but you have to admit it really improves the usability of the program.

If you like this article, please rate it!

Add to:, StumbleUpon, Digg, Google