Accessing Project Variables

Get Project Variables

Minimum Origin Version Required: Origin 8.1 SR0

This example is used to get the variables in the project, including system variable, project variable and section variable.

Before running this example, it will be better to restart Origin and then run the following script to generate some variables.

doc -n;  // create a new project
fname$ = system.path.program$ + "Samples\Curve Fitting\Gaussian.dat";  // data path, project variable
impASC;  // import data
int nCols = wks.nCols;  // number of columns, section variable
nRows = wks.nRows;  // number of rows, project variable
int nWidth = wks.colWidth;  // column's width, section variable
string strName = wks.Name$;  // worksheet name, section variable
strLongName$ = col(a)[L]$;  // long name of column A, project variable
double dValue = col(c)[1];  // value in first cell of column C, section variable
dCell2 = col(c)[2];  // value in second cell of column C, project variable
const cPI = pi;  // const, PI, section variable
void get_project_variables()
{
        vector<string> vsProStrVarNames;  // project string variable name
        vector<string> vsProStrVarValues;  // value of project string variable
        
        // get project string variables
        int nProStrVarCount = Project.GetLTStrVars(vsProStrVarNames, vsProStrVarValues, true);
        
        // print total number of project string variables
        printf("There are %d project string variables in this project. They are:\n", nProStrVarCount);
        
        //  print all project string variables 
        for(int iProStrVar=0; iProStrVar<nProStrVarCount; iProStrVar++)
        {
                printf("\tName: %s\tValue: %s\n", vsProStrVarNames[iProStrVar], vsProStrVarValues[iProStrVar]);
        }
        
        // system & project numeric variables
        vector<string> vsProVarNames;  // name
        vector vsProVarValues;  // value
        vector<int> vsProVarTypes;  // type
        
        // get system & project numeric variables
        int nProVarCount = Project.GetLTVars(vsProVarNames, vsProVarValues, false, vsProVarTypes);
        
        // print the total number of system & project numeric variables
        printf("There are %d system & project numeric variables in this project. They are:\n", nProVarCount);
        
        // print all system & project numeric variables
        for(int iProVar=0; iProVar<nProVarCount; iProVar++)
        {
                printf("\tName: %s\tValue: %f\tType: %d\n", vsProVarNames[iProVar],
                        vsProVarValues[iProVar], vsProVarTypes[iProVar]);
        }
        
        // section variables
        vector<string> vsSecVarNames;  // name
        vector<string> vsSecVarValues;  // value
        
        // get all section variables
        int nSecVarCount = Project.GetSessionLTVars(vsSecVarNames, vsSecVarValues, LTVAR_TYPE_VAR_ALL);
        
        // print total number of section variable
        printf("There are %d section variables in this project. They are:\n", nSecVarCount);
        
        // print all section variables
        for(int iSecVar=0; iSecVar<nSecVarCount; iSecVar++)
        {
                printf("\tName: %s\tValue: %s\n", vsSecVarNames[iSecVar], vsSecVarValues[iSecVar]);
        }
        
                // section integer variables
        vector<string> vsSecIntVarNames;  // name
        vector<string> vsSecIntVarValues;  // value
        
        // get all section integer variables
        int nSecIntVarCount = Project.GetSessionLTVars(vsSecIntVarNames, vsSecIntVarValues, LTVAR_TYPE_VAR_INT);
        
        // print total number of section integer variable
        printf("There are %d section integer variables in this project. They are:\n", nSecIntVarCount);
        
        // print all section integer variables
        for(int iSecIntVar=0; iSecIntVar<nSecIntVarCount; iSecIntVar++)
        {
                printf("\tName: %s\tValue: %s\n", vsSecIntVarNames[iSecIntVar], vsSecIntVarValues[iSecIntVar]);
        }
}

Accessing Tree Variable

Minimum Origin Version Required: Origin 8 SR1

This example is used to add and get tree variables in the project. Firstly, loop over all workbooks in the project and create a tree node for each workbook. Then add this tree variable to the project. Finally, get all the tree variables in the project and print the tree information.

void access_tree_variable()
{
        string strTreeName = "FilesInfo";  // tree name
        
        Tree trFiles;
        trFiles.SetAttribute(STR_LABEL_ATTRIB, strTreeName);
        
        // loop all worksheet pages in the project for creating tree
        foreach(WorksheetPage wp in Project.WorksheetPages)
        {     
                string strFileName;
                strFileName = wp.GetName();  // get name
                
                TreeNode tnFileInfo;
                // each file will be a tree node
                tnFileInfo = trFiles.AddTextNode(strFileName, strFileName); 
                
                PageSystemInfo psi;  // page information
                
                if(wp.GetPageSystemInfo(& psi))  // get page information
                {
                        // set page information to tree node
                        tnFileInfo.AddTextNode(get_date_str(psi.dCreated), "CreateTime");  
                        tnFileInfo.AddTextNode(get_date_str(psi.dModified), "ModifiedTime");
                        tnFileInfo.AddNumericNode((int)psi.nSize, "Size");
                        tnFileInfo.AddNumericNode(psi.nDependents, "Dependents");
                }
        }
        
        // add tree variable to project
        int nTree = Project.AddTree(strTreeName, trFiles);
        
        vector<string> vsTreeNames;
        
        // get all tree names in the project
        int nElement = Project.GetTreeNames(vsTreeNames);
        
        // loop to get all tree variables in the project
        for(int iTree=0; iTree<vsTreeNames.GetSize(); iTree++)
        {
                TreeNode tnGet;
                
                // get the tree variable
                BOOL bRet = Project.GetTree(vsTreeNames[iTree], tnGet);
                if((bRet!=0) && (tnGet.IsValid()))
                        out_tree(tnGet);  // output the tree
                else
                        out_str("Error!\n");
        }
}