Monday, September 9, 2013

Autocreate Minitab Scripts in C++

The following code will read in a CSV file containing row and reference line data. The program will then create a Minitab script as the output file. This file can then be used directly in Minitab to auto-generate hundreds of plots. This approach can work for auto-generating / automating any scripts in Minitab.

// main_histo-limits.cpp
// James Eastham
// Version 1.0
// Purpose: The purpose of this program is to read in a set of test names and
// limits. Produce minitab script code which can be used to generate a large
// number of histograms with correct reference limit lines.
// Input: The input to this program will be a CSV file containing all the test
// names and LSL/USL for which histograms will be created.
// The input file is hard coded as limits.csv
// Output: The output will be a text file (histolimits.txt) which can be pasted
// directly into minitab and ran as a script
#include
#include
#include ;
#include ;The

using namespace std;

// int argc, char *argv[] are only needed for processing command line
// arguments. This program does not use command line arguments, so
// main() would be fine here.

int main()
{
    string testname$;           // Variable for test name read from CSV
    string lsl$;                // Varialbe for lower spec limit from CSV
    string usl$;                // Variable for upper spec limit from CSV
    // Input file name is hardcoded below as testnames.txt
    ifstream testnames ("limits.csv");
    ofstream myfile;
    // Output script file is hardcoded below as histoscript.txt
    myfile.open ("histolimits.txt");
    if (testnames.is_open())
    {
       while (testnames.good() )
             {
             getline (testnames,testname$,',');  // Read test name from file
             getline (testnames,lsl$,',');       // Read lsl from file
             getline (testnames,usl$);           // Read usl from file
             // The following cout lines simply print the info read from CSV
             // to the screen for debug purposes
             cout << testname$;  
             cout << ',';
             cout << lsl$;
             cout << ',';
             cout << usl$;
             cout << endl;
             if (testname$.length()>30)
                {
                cout << "***WARNING LONG TEST NAME, Exceeds 30 Charcaters***\n";
                }
    // The following 'myfile' lines output text to the output file
    // 'histolimits.txt'. This txt file is then used in Minitab as
    // a script. The testname$, lsl$, usl$ are used in this Minitab script
    // for naming each histogram/plot and reference/limit lines.
    // This text file can be pasted directly into Minitab.
   
    myfile << "Histogram '";
    myfile << testname$;
    myfile << "';\n";
    myfile << "Scale 1;\n";
    myfile << "TFont \"Arial Narrow\";\n";
    myfile << "PSize 8;\n";
    myfile << "Scale 2;\n";
    myfile << "TFont \"Arial Narrow\";\n";
    myfile << "PSize 8;\n";
    myfile << "AxLabel 1;\n";
    myfile << "TFont \"Arial Narrow\";\n";
    myfile << "AxLabel 2;\n";
    myfile << "TFont \"Arial Narrow\";\n";
    myfile << "PSize 8;\n";
    myfile << "NoBold;\n";
    myfile << "Table;\n";
    myfile << "TFont \"Arial Narrow\";\n";
    myfile << "Section 1;\n";
    myfile << "NoLegend;\n";
    myfile << "Density;\n";
    myfile << "Bar 'part_id';\n";
    myfile << "Color 3;\n";
    myfile << "Distribution 'part_id';\n";
    myfile << "Normal;\n";
    myfile << "Grid 1;\n";
    myfile << "Reference 1 ";
    myfile << lsl$;
    myfile << ";\n";
    myfile << "Type 3;\n";
    myfile << "Color 2;\n";
    myfile << "Size 1;\n";
    myfile << "MODEL 1;\n";
    myfile << "TFont \"Arial Narrow\";\n";
    myfile << "PSize 8;\n";
    myfile << "Reference 1 ";
    myfile << usl$;
    myfile << ";\n";
    myfile << "Type 3;\n";
    myfile << "Color 114;\n";
    myfile << "Size 1;\n";
    myfile << "MODEL 1;\n";
    myfile << "Title \"TM6PP6924 : ";
    // The following two remarks are not needed
    //myfile << "Title &\n";
    //myfile << "\"";
    myfile << testname$;
    myfile << "\";\n";
    myfile << "TFont \"Arial Narrow\";\n";
    myfile << "PSize 12;\n";
    myfile << "NoBold;\n";
    myfile << "SubTitle;\n";
    myfile << "TFont \"Arial Narrow\";\n";
    myfile << "PSize 8;\n";
    myfile << "StDist;\n";
    myfile << "Footnote;\n";
    myfile << "FPanel;\n";
    myfile << "NoDTitle;\n";
    myfile << "NoDSubtitle.\n";
     
}
testnames.close();   // close the input file
}
else cout << "Unable to open file";
   
    myfile.close();  // close the output file
    system("PAUSE");
    return EXIT_SUCCESS;
}