CATScript - The Beauty of Code
CATScript is a scripting language specific to CATIA, a widely used computer-aided design (CAD) software.
In this article, I’d like to put down some more reusable code snippets to make a programme’s live easier.
Text Concatenation with Array
The following coding example is likely the most efficient way
of managing large amounts of text information.
Note when using the Join function, vbNewLine needs to be declared only once.
Option Explicit
Language="VBSCRIPT"
Sub CATMain()
Dim i As Integer
Dim msg As Array
msg = Array ( _
"entia non sunt",_
"multiplicanda praeter",_
"necessitatem",_
"",_
"Entities should not be",_
"multiplied beyond necessity."_
)
' loop through each individual array item
' For i = 0 To UBound(msg) MsgBox msg(i) Next
' show the array's content
MsgBox Join (msg, vbNewLine), vbOkOnly + vbInformation, "Message:"
End Sub
If you are short in time, don’t read any further in this chapter, use the above code, modify it as required and you are done.
Dictionaries
While I typically use the commands built into the CATScript language, there is also
additional functionality available via CreateObject.
This way, dictionary objects are provided and can be employed to address problems
requiring more complex data structures.
MsgBox Goodies
The array data type can also be used to implement a clean and lean MsgBox call.
I personally like the “Select Case” statement together with predefined enumerations.
This way, there is no need to hard code return values.
Sub UserSelectionCmd()
Dim msg As Array
msg = Array (_
"This function allows to ....",_
"<your text description here>",_
"<some more explanations...>",_
"","",_
"YES = " + vbTab + "Do you like to continue?",_
"No = " + vbTab + "<Do semething else instead...>")
Do While True
Select Case MsgBox ( _
Join (msg, vbNewLine),_
vbQuestion + vbYesNoCancel, "User Selection:")
Case vbCancel
Exit Sub
Case vbNo
Call DoSomethingElseCmd ()
Case vbYes
Exit Do
End Select
Loop
End Sub
More about Dynamic Arrays
I prefer dynamic array allocation because it is the most flexible approach. At the same time, I try to avoid writing excessive code, so I looked for a clean and concise solution to meet this requirement:
Standard way to set up some more array variables manually.
Sub CATMain ()
Dim i As Integer
Dim item, var() As Variant
ReDim var(0) : var(0) = 1
ReDim Preserve var(1) : var(1) = "Hello"
ReDim Preserve var(2) : var(2) = True
For i = LBound(var) To UBound(var)
MsgBox TypeName (var(i)) + " = " + CStr(var(i))
Next
' also possible:
For Each item In var
MsgBox TypeName (item) + " = " + CStr(item)
Next
End Sub
Accessing every element in an array can also be achieved using a For Each loop,
which is often the most readable and concise option.
The following code mimics a dynamic array. The size of the array is handled on the fly at the time when adding a new array item:
Sub Add (ByRef arr() As String, ByVal new_item As String)
ReDim Preserve arr (UBound(arr) + 1)
arr (UBound(arr)) = new_item
End Sub
Sub CATMain()
Dim arr() As String
ReDim arr(-1)
' Add items dynamically
Add arr, "First"
Add arr, "Second"
Add arr, "Third"
MsgBox Join(arr, vbNewLine)
End Sub
This is likely the most comprehensive version for a dynamically allocated array with the fewest lines of code.
Notes:
The array index starts at 0 which is the default behavior for array variables.
If the array would be declared as
Dim arr() As Variantthe array is capable to handle any kind of variable.Hint: If an object is assigned as array item, you probably would need to declare the
Setkeyword as usual.To loop through each item of the array, one can do this as usaully like toe following:
Dim arr() As String ' MsgBox CStr(LBound(arr)) + "/" + CStr(UBound(arr)) Dim i As Integer For i = LBound(arr) To UBound(arr) MsgBox "Index " & i & ": " & arr(i) Nextand as an alternative:
MsgBox Join(arr, vbNewLine)
