1.9.4 Accessing Metadata


Metadata is information which refers to other data. Examples include the time at which data was originally collected, the operator of the instrument collecting the data and the temperature of a sample being investigated. Metadata can be stored in Projects, Pages, Layers and Columns.

Access DataRange

The Origin C Project class provides methods to add, get, and remove an Origin C DataRange object to and from the current project.

Worksheet wks = Project.ActiveLayer();

DataRange dr;                   // Construct the range object
dr.Add("X", wks, 0, 0, -1, -1); // Add whole worksheet to range
dr.SetName("Range1");           // Set range name
int UID = dr.GetUID(TRUE);      // Get Unique ID for the range object

int nn = Project.AddDataRange(dr); // Add range to project

In the Command Window or Script Window you can use the LabTalk command list r to list all the DataRange objects in the current project.

Access Tree

Access a Tree in a Project

Add Tree

This code declares a variable of type tree, assigns some data to nodes of the tree, and adds the tree to the current project.

Tree tr;
tr.FileInfo.name.strVal = "Test.XML";
tr.FileInfo.size.nVal = 255;

// add tree variable to project
int nNumTrees = Project.AddTree("Test", tr); 
out_int("The number of trees in project: ", nNumTrees);


Get Tree

Likewise, a similar code extracts data stored in an existing tree variable named Test and puts it into a new tree variable named trTest:

// get tree from project by name
Tree trTest;
if( Project.GetTree("Test", trTest) )
	out_tree(trTest);

Get the Names of All LabTalk Trees

The Project::GetTreeNames method gets the names of all LabTalk tree variables in the project. Here, the names are assigned to a string vector; the number of strings assigned is returned as an integer.

vector<string> vsTreeNames;
int nNumTrees = Project.GetTreeNames(vsTreeNames);

Access Tree in a Worksheet

OriginObject::PutBinaryStorage is used to put a tree into many types of Origin object, for example, a WorksheetPage, Worksheet, Column, GraphPage, or MatrixPage.

Add Tree

Keep an active worksheet window in the current project, to run the example code below. After running the code to add a user tree, right click on the title of the worksheet window, choose Show Organizer, and you will see the added user tree show up in the panel on the right.

Worksheet wks = Project.ActiveLayer();
if( wks )
{
	Tree tr;
	tr.name.strVal = "Jacky";
	tr.id.nVal = 7856;
	
	// put tree with name wksTree to worksheet object
	string strStorageName = "wksTree";
	wks.PutBinaryStorage(strStorageName, tr);	
}

Get Tree

The OriginObject::GetBinaryStorage method is used to get a tree from an Origin object by name.

Worksheet wks = Project.ActiveLayer();
if( wks )
{	
	Tree tr;
	string strStorageName = "wksTree";
	
	// if the tree named wksTree is existed, return true.
	if( wks.GetBinaryStorage(strStorageName, tr) )
		out_tree(tr); // output tree 
}

Get the Names of All Trees

The OriginObject::GetStorageNames method gets the names of everything in storage in an Origin object. There are two storage types: INI and binary. Trees belong to binary storage, and the following example code shows how to get binary storage from a Worksheet.

Worksheet wks = Project.ActiveLayer();
if( wks )
{
	// get the names of all binary type storage 
	vector<string> vsNames;
	wks.GetStorageNames(vsNames, STORAGE_TYPE_BINARY); 
	
	for(int nn = 0; nn < vsNames.GetSize(); nn++)
		out_str(vsNames[nn]);
}

Access Tree in a Worksheet Column

For setting and getting a tree in a Worksheet Column, use the same methods for setting and getting a tree in a Worksheet, as described above.

Add Tree

Worksheet wks = Project.ActiveLayer();
Column col(wks, 0);

Tree tr;
tr.test.strVal = "This is a column";
tr.value.dVal = 0.15;

col.PutBinaryStorage("colTree", tr);

Get Tree

Worksheet wks = Project.ActiveLayer();
Column col(wks, 0);

Tree tr;	
if( col.GetBinaryStorage("colTree", tr) )
	out_tree(tr);

Get the Names of All Trees

Worksheet wks = Project.ActiveLayer();
Column col(wks, 0);

// get the names of all binary type storage 
vector<string> vsNames;
col.GetStorageNames(vsNames, STORAGE_TYPE_BINARY); 

for(int nn = 0; nn < vsNames.GetSize(); nn++)
	out_str(vsNames[nn]);

Access Import File Tree Nodes

After importing data into a worksheet, Origin stores metadata in a special tree-like structure at the page level. Basic information about the file can be retrieved and put into a tree.

Worksheet wks = Project.ActiveLayer();
WorksheetPage wksPage = wks.GetPage();

storage st;
st = wksPage.GetStorage("system");   

Tree    tr;
tr = st;

double dDate = tr.Import.FileDate.dVal;    
printf("File Date: %s\n", get_date_str(dDate, LDF_SHORT_AND_HHMMSS_SEPARCOLON));
printf("File Name: %s\n", tr.Import.FileName.strVal);
printf("File Path: %s\n", tr.Import.FilePath.strVal);

Access Report Sheet Tree

Analysis Report sheets are specially formatted Worksheets based on a tree structure. You can get the report tree from a report sheet as below.

Worksheet wks = Project.ActiveLayer();

Tree trReport;
uint uid; // to receive the UID of the report range
// true to translate the escaped operation strings(ex. ?$OP:A=1) 
// to real dataset name in the returned tree 
bool bTranslate = true; 
if( wks.GetReportTree(trReport, &uid, 0, GRT_TYPE_RESULTS, true) )
{
	out_tree(trReport);
}