Creating a chart in a MaterialsScript (Warning: Uses Perl!)

There have been several requests for the capability to create charts from within a MaterialsScript. It is not possible to do this at the moment using a simplified set of MaterialsScript commands but it can be achieved using some fairly simple Perl. You can do this by creating a .tbl file outside the project and then importing it in using standard MaterialsScript. On import, the .tbl file is automatically converted to an .xcd and hence gives you a chart.

I have pasted some example Perl to help you to do this below. Note that in this example I use a hash of arrays to store multiple Y values associated with an X value but you can use simpler data structures like a straight hash list if you just want to store single X and Y data.

# The following script fragment shows how to use a .tbl file to create a chart

# in MaterialsScript. The .tbl file is a text format and it is documented in the

# Materials Studio online help. This script uses a unique property of a .tbl file -
# when it is imported into Materials Visualizer, a chart is created from the .tbl.

# This script should be run on the server only.

# Create some values to populate the chart - in this case, I use a hash of arraysmy %chartValues = ();

for (my \\\$counter = 0; \\\$counter < 10; ++ \\\$counter) {

    push (@{\\\$chartValues{\\\$counter}}, \\\$counter);     # Store X and Y1

    push (@{\\\$chartValues{\\\$counter}}, \\\$counter+1);     # Store X and Y2

}

# Open a new text file outside of the project called achart.tbl
open (TBLDOC, ">achart.tbl");# Print all the header information - see online help for .tbl format
print TBLDOC "#\\n";

print TBLDOC "TITLE: X\\n";

print TBLDOC "FUNCTION: X\\n";

print TBLDOC "UNITS OF MEASUREMENT: ps\\n";

print TBLDOC "#\\n";

print TBLDOC "TITLE: Y1\\n";

print TBLDOC "FUNCTION: Y1\\n";
#print TBLDOC "UNITS OF MEASUREMENT: ps\\n";
print TBLDOC "#\\n";
print TBLDOC "TITLE: Y2\\n";
print TBLDOC "FUNCTION: Y2\\n";
#print TBLDOC "UNITS OF MEASUREMENT: ps\\n";
print TBLDOC "#\\n";
print TBLDOC "#\\n";
# Then print all the values from the chart

foreach my \\\$key ( sort {\\\$a <=> \\\$b} keys %chartValues) {

    print TBLDOC "\\\$key \\\$chartValues{\\\$key}[0] \\\$chartValues{\\\$key}[1]\\n";

}

# Close down the doc - this must be done otherwise the chart will be empty!
close TBLDOC;

# Then import the tbl back into the project. On importing, it is converted into an xcd

my \\\$chart = Documents->Import("achart.tbl");

NOTE: You should run this ONLY on the server and not in debug mode.

I hope this helps,

Cheers

Stephen