Arrays and Vectors

In scripts, data often needs to be managed as an array of items. The CADMATIC script language does not implement arrays as an intrinsic feature, like for example C and C++ do. However, there are externals for several data structures, which implement different kinds of data containers for different purposes.

Name

Type

Description

Vectors

One-dimensional array

Can store a value using any data type, including handles to other structures. Uses dynamic size management.

2D Arrays

Two-dimensional array (row x column table)

Can store a cell value using any data type, including handles to other structures. Uses dynamic size management.

Sort Tables

Two-dimensional array (table)

Implements quick sorting of large arrays. Sorting is based on selected key columns and sorting rules. When sorted, cannot add more rows to the table.

Tag Records

A vector of name–value pairs

All values are managed as string values. CADMATIC uses tag records to implement a simple ASCII markup format with minimal overhead.

Vectors

Vector Functions implement a one-dimensional array. They provide dynamic memory reallocation—the size of the vector is automatically increased when needed.

Note: Also an older version of a single-dimensional array interface exists: A_ALLOC, A_PUT, A_GET. These variables are used in many older scripts, and they are also used as arguments or return values of some script extern functions. They are not compatible with DM_VECTOR variables. As a general rule, always use DM_VECTOR if you can, as DM_VECTOR provides more features and better interfaces.

Example

In this example, we use DM_VECTOR to read the following data from a file as a list of lines:

V2002 225 1234 -250 15
V4450 1100 -440 -2300 17
V1223 -200 400 -500 11

The example script loads the lines from the file, sorts the list, and prints the sorted content.

Running the script outputs the following to the message pane:

Run>Script
 4 string values in the list:
 Sorted content of list:
 Line: 1: <V1001 123 432 122 12>
 Line: 2: <V1223 -200 400 -500 11>
 Line: 3: <V2002 225 1234 -250 15>
 Line: 4: <V4450 1100 -440 -2300 17>

2D Arrays

2D Array Functions implement a two-dimensional table of rows and columns.

Note: Also an older version of a two-dimensional array interface exists: alloc_2darray, realloc_2darray, free_2darray, put_2darray, get_2darray, nrows_2darray, ncols_2darray. These are actually scripts defined in the include file array.mac. This old interface should not be used in new scripts. If using the DM_2D_ARRAY externs, there is no need to worry about memory reallocation, while the old realloc_2darray is known to suffer from inadequate run-time performance.

Example

In this example, instead of reading data file into a vector as in Vectors, we read the data as row and column values into a 2D array:

V2002	225	1234	-250	15
V4450	1100	-440	-2300	17
V1223	-200	400	-500	11

Here, the column values represent the name of the item, COG X, COG Y, COG Z, and mass. In the file, the items are delimited by the space character.

The example script loads the lines from the file, sorts the list by mass, and prints the sorted content.

Running the script outputs the following to the message pane:

Run>Script
 Nr rows: 4, sorted according to mass
 Row: 0: name=V9223, COGx=-1200.0, COGy=4300.0, COGz=-600.0, Mass=5.0
 Row: 1: name=V0451, COGx=2100.0, COGy=-1440.0, COGz=-300.0, Mass=8.0
 Row: 2: name=V0008, COGx=133.0, COGy=442.0, COGz=522.0, Mass=11.0
 Row: 3: name=V0007, COGx=235.0, COGy=4234.0, COGz=-256.0, Mass=18.0

Advanced Example of Using Vectors and 2D Arrays

There are some general rules regarding good programming practices that are also valid in script programming. In shorter scripts following these practices is less important, but any script with several hundreds of lines or more should be made with these rules in mind:

  • Use short functions which encapsulate a single logical action.
  • Generally avoid very long source code files. Encapsulate a logical module into a single file.
  • Avoid using globals.
  • Utilize modularity for clarity and code re-use.

Arrays can help you to apply these rules and make your code more understandable and robust.

Example

In this example, as input we have several data files similar to the one that was processed in the examples in Vectors and 2D Arrays.

We need a script that loads the data lines (item name, COG X, COG Y and COG Z, and mass) from the files into a single table. The table needs to be sorted according to mass and then printed. Additionally, the script should list all files from which data was loaded, and it should allow any number of source files to be used.

Modularity should be supported for clarity and for reusing parts of the code. Global variables should be avoided.

The run-time data structure, utilizing Vector Functions and 2D Array Functions, could look like this:

The root is a file list vector with two elements:

  • Handle of list of file names.
  • Handle of data table.

The size of the file list vector depends on how many files are selected to be loaded by the user at run time (dynamic reallocation).

The data table is a 2D array of five columns:

  • item name
  • COG X
  • COG Y
  • COG Z
  • mass

The number of rows the table has depends on how many rows will be loaded from the selected input files.

Due to clarity and code re-use we want to divide the code into three script files and one header file.

  • Header file defines the data structure
  • Main script implements the workflow and calls the other scripts
  • Script which loads the data from the files and manages the data structures
  • Script which processes the data

The header file, ExaAdvArray.h:

The main script, ExaAdvArrayMain.mac:

The loader script, ExaAdvArrayLoad.mac:

The script which processes the data, ExaAdvArrayProcess.mac:

When the main script ExaAdvArrayMain.mac is run as a script, it writes the following to the message pane:

Run>Script
 
 Files loaded:
 File 1: C:/ScriptExamples/ExaData1.txt
 File 2: C:/ScriptExamples/ExaData2.txt
 
 Nr rows: 8, sorted acc. to mass:
 Row: 1: name=V9223, COGx=-1200.0, COGy=4300.0, COGz=-600.0, Mass=5.0
 Row: 2: name=V0451, COGx=2100.0, COGy=-1440.0, COGz=-300.0, Mass=8.0
 Row: 3: name=V1223, COGx=-200.0, COGy=400.0, COGz=-500.0, Mass=11.0
 Row: 4: name=V0008, COGx=133.0, COGy=442.0, COGz=522.0, Mass=11.0
 Row: 5: name=V1001, COGx=123.0, COGy=432.0, COGz=122.0, Mass=12.0
 Row: 6: name=V2002, COGx=225.0, COGy=1234.0, COGz=-250.0, Mass=15.0
 Row: 7: name=V4450, COGx=1100.0, COGy=-440.0, COGz=-2300.0, Mass=17.0
 Row: 8: name=V0007, COGx=235.0, COGy=4234.0, COGz=-256.0, Mass=18.0

Note: Use PM_CALL_SCRIPT with caution. There is a performance cost involved when using PM_CALL_SCRIPT instead of a local function. You should avoid repeatedly calling PM_CALL_SCRIPT in time-critical for-loops, for example.

Sort Tables

Sort tables were implemented for sorting masses of data records, such as the data table of a Bill of Materials report. The corresponding externs are declared in the include file rpt.h.

The example script below reads from a file lines that list the following data:

item (string), pieces (integer), description (string)

The script loads the data from each line into the sort table structure and then sorts the table according to the "Name" field.

Running the script outputs the following to the message pane:

MenuItem(Command)*Select and Run
6 rows
Item:a-1 Pieces:2 Descr:Elbow
Item:a-1 Pieces:2 Descr:Elbow
Item:a-2 Pieces:8 Descr:Bend
Item:a-2 Pieces:8 Descr:Bend
Item:a-3 Pieces:16 Descr:Pipe
Item:a-3 Pieces:16 Descr:Pipe

Note: You cannot add, modify or delete items in the table after it has been sorted.

Tag Records

A plain-text representation of a tag record is the standard semicolon record that is used in CADMATIC system administration in general:

tag_name1 value1; tag_name2 value2; ... tag_namen valuen;;

A tag file consists of one or more tag records. The format allows new lines inside records. Empty field (double semicolon ;;) marks the end of the record.

The tag record utilities were originally made for filtering Bill of Materials files (m-files) in Plant Modeller and Piping Isometrics & Spools. The external function set includes utilities to read tag records from files and to write them into files. Thus, tag records are a very handy method to implement reading and writing of data tables from and into files.

One benefit of using tag records is that it supports clear formatting of textual data, without having to worry about white space (space and tab characters).

Using tag records, the same example data that was used in Sort Tables is formatted like this:

itm a-1;pcs 2;dsc Elbow 90;;
itm a-2;pcs 8;dsc Bend 45;;
itm a-3;pcs 16;dsc Pipe 60.3 x 5.0;;
itm a-1;pcs 2;dsc Elbow 90;;
itm a-2;pcs 8;dsc Bend 45;;
itm a-3;pcs 16;dsc Pipe 60.3 x 5.0;;

In the script, we can also compute the total number of the "a-1" items and display the resulting value in the output.

Running the script outputs the following to the message pane:

Item		Pieces		Description
a-1		2		Elbow 90
a-2		8		Bend 45
a-3		16		Pipe 60.3 x 5.0
a-1		2		Elbow 90
a-2		8		Bend 45
a-3		16		Pipe 60.3 x 5.0