3.6.3 Importing With Filter


Version Info

Minimum Origin Version Required: Origin 8 SR6

Prior to Calling Import Functions

Functions for importing files are declared in OriginC\Originlab\FileImport.h file. These functions are also documented in the Origin C Language Reference help file.

Prior to calling the import file functions, you need to first programmatically load and compile FileImport.c. This can be done from script using the command:

run.LoadOC(Originlab\FileImport.c, 16);
// Option 16 ensures that all dependent Origin C files are loaded, by scanning for the corresponding .h in FileImport.c

Note: The above run.LoadOC command needs to be run any time a new Origin session is launched, and also any time you start a new workspace in Code Builder.

Once the above has been executed from script, then you can compile the examples below or your own function that calls the import functions. Note that if you have a .c file with your function (or have copied the examples below to a .c file), you could load and compile that file as well from script, using the command:

run.LoadOC(MyFileImport.c); // where MyFileImport is your file name

Examples

Import Single File With Specified Filter

This example shows how to import a single data file using a specific import filter file. Run import_file_with_specify_filter_ex in Command Window to see result.

#include <..\Originlab\FileImport.h>

// import file with filter to active layer
void import_file_with_specify_filter_ex()
{    
    Page pg = Project.Pages(); // Active Page
    
    // Get page book name
    string strPageName = pg.GetName();
    
    // Get page active layer index
    int nIndexLayer = pg.Layers().GetIndex();
    
    // Get Origin sample folder
    string strPath = GetAppPath(TRUE) + "Samples\\Signal Processing\\";
    
    // specify .oif filter name
    string strFilterName = "TR Data Files";
    
    int nRet = import_file(strPageName, nIndexLayer, strPath + "TR2MM.dat", strFilterName);
    
    
    if ( 0 == nRet )
        out_str("Import successful!");
    else
        out_str("Failed to import!");
}

Import Multiple Files With Specified Filter

This example shows how to import multiple PCLAMP files refer to system pCLAMP filter. Run import_multiple_files_with_specify_filter_ex in Command Window to see result.

#include <..\Originlab\FileImport.h>
void import_multiple_files_with_specify_filter_ex()
{    
    Page pg = Project.Pages(); // Active Page
    
    // Get page book name
    string strPageName = pg.GetName();
    
    // Get page active layer index
    int nIndexLayer = pg.Layers().GetIndex();
    
    // Get Origin sample folder
    string strPath = GetAppPath(TRUE) + "Samples\\Import and Export\\";
    
    // List all file names need to imported
    StringArray saFileNames;
    saFileNames.Add(strPath + "pCLAMP\\93310C08.DAT");
    saFileNames.Add(strPath + "pCLAMP\\93310C10.DAT");
    saFileNames.Add(strPath + "pCLAMP\\93311C01.DAT");
    
    // specify .oif filter name, location see the setting of nLocation below.
    string strFilterName = "pCLAMP";
 
    // import filter tree
    // about filter settings, refer to imppClamp xfunction tree variable "options"
    Tree trFilter;    

    int nLocation = 1; // Origin exe folder, more options see the document of load_import_filter function.
    
    int nRet;
    nRet = load_import_filter(strFilterName, saFileNames[0], strPageName, nLocation, trFilter);
    
    if ( 0 == nRet )
        out_str("Loaded import filter successfully!");
    else
        out_str("Failed to load import filter!\nPlease check strFilterName and nLocation if assigned correctly");
 
    if ( 0 == nRet )
    {
        nRet = import_files(saFileNames, strPageName, nIndexLayer, trFilter);
        
        if ( 0 == nRet )
            out_str("Imported successfully!");
        else
            out_str("Failed to import!");
    }
}

Configure ASCII Filter

This example to show how to load ASCII filter to a tree, then convert the tree to ASCII struct, you can update some import settings in the struct, and then import the data file using the DataSheet::ImportASCII function.

For additional examples on how to update import settings with ASCII struct, please see ASCII Import

#include <..\Originlab\FileImport.h>

void config_filter_tree_ex()
{
	string strFile = GetAppPath(1) + "Samples\\Curve Fitting\\Step01.dat";
	if( !strFile.IsFile() )
		return;	
		
	Worksheet wks;
	wks.Create("origin");
	
	// load filter to tree
    Tree trFilter;    
    string strFilterName = "ASCII";
    int nLocation = 1; // build-in Filters folder
    int nRet = load_import_filter(strFilterName, strFile, wks.GetPage().GetName(), nLocation, trFilter);
    if( 0 != nRet )
    	out_str("Failed to load import filter");

	ASCIMP ascimp;
	ascimp = trFilter.ASCIMP;// convert filter tree to ASCIMP struct
	
	ascimp.iRenameCols = 0; // 0 to keep default column name, 1 to rename column	
	
	nRet =  wks.ImportASCII(strFile, ascimp);
    if(0 == nRet)
    	out_str("Import successful");	
}

Load and Configure Filter with OC Code

The following examples show how to load filter file to tree and config import settings by tree.

Configure Filter to Add More Variables

This example shows how to get information from data file as user variable and add variables to Page Info. Run import_ascii_file_with_filter in Command Window to import file. After import, right click worksheet title and choose Show Organizer, in left list box, click page.info -> USER.VARIABLES, will see POSITION and DATASUBFOLDER variables in right panel.

#include <..\Originlab\FileImport.h>
void import_ascii_file_with_filter()
{
	string strDataFile = GetAppPath(1) + "Samples\\Import and Export\\S15-125-03.dat";
	if( !strDataFile.IsFile() )
	{
		out_str("Cannot find this data file");
		return;	
	}		
	
	string strFilterFile = GetAppPath(1) + "Samples\\Import and Export\\VarsFromFileNameAndHeader.oif";
	if( !strFilterFile.IsFile() )
	{
		out_str("Cannot find this filter file");
		return;
	}
		
	Worksheet wks;
	wks.Create("origin");
	string strPageName = wks.GetPage().GetName();
	
  	// 1. load filter to tree
  	Tree 	trFilter;      
    int 	nLocation = 4; // Full path of filter file
    int 	nRet = load_import_filter(strFilterFile, strDataFile, strPageName, nLocation, trFilter);
    if( 0 != nRet )
    {
    	out_str("Failed to load import filter");	
    	return;
    }
    
    // 2. Config filter tree to add more user variables to page info
    trFilter.Variables.PageInfo.nVal = 1;
    trFilter.Variables.ExtractByDelimiter.nVal = 1;
    
    // 2.1 Extract variable name and value from file
    // here use H008 line last item as variable name and H009 line last item as value.
    int nLastColumnIndex = 3; // there is 4 columns
	vector<int> vn(1);
	vn[0] = '\t';
    
    Tree trVar;
    trVar.Name.Line.nVal = 7; // H008, 7 = header line number(8) - 1 
    trVar.Name.Token.nVal = nLastColumnIndex;
    trVar.Name.Delimiters.nVals = vn;    
    
    trVar.Value.Line.nVal = 8; // H009, 8 = header line number(9) - 1 
    trVar.Value.Token.nVal = nLastColumnIndex;
    trVar.Value.Delimiters.nVals = vn;    
    
    trFilter.Variables.Vars.AddNode(trVar); 
    
    
    // 2.2 Extract value from file and define variable name by yourself
    trVar.Reset();
    
    trVar.Name.Constant.strVal = "DataSubFolder";
    
    trVar.Value.Line.nVal = -2; //F002, -2 = file line number(2) * -1
    trVar.Value.Token.nVal = 3; // 3th (offset 0) token is the last sub folder in file path string    
	vn[0] = '\\';
    trVar.Value.Delimiters.nVals = vn; // '\' is file path delimiter   
    
	trFilter.Variables.Vars.AddNode(trVar); 
   

    // 3. Do import with filter tree   
    StringArray saDataFiles;
    saDataFiles.Add( strDataFile );
    
    nRet = import_files(saDataFiles, strPageName, wks.GetIndex(), trFilter);
    if( 0 != nRet )
	{
		out_str("Failed to import data file");
		return;
	}
}

Configure Filter to Set Import Mode

This example shows how to config filter tree to set import mode to import multiple ASCII files to separate new sheets. Run config_filter_to_set_import_mode in Command Window to see result.

#include <..\Originlab\FileImport.h>
void config_filter_to_set_import_mode()
{
	string strFileFolder = GetAppPath(1) + "Samples\\Import and Export\\";	
	string strFile1 = strFileFolder + "S15-125-03.dat";
	string strFile2 = strFileFolder + "S21-235-07.dat";
	string strFile3 = strFileFolder + "S32-014-04.dat";
	StringArray saDataFiles;
	saDataFiles.Add(strFile1);
	saDataFiles.Add(strFile2);
	saDataFiles.Add(strFile3);
	
	Worksheet wks;
	wks.Create("origin");
	string strPageName = wks.GetPage().GetName();
	
  	// 1. load filter to tree
  	Tree 	trFilter;      
    int 	nLocation = 1; // 1 - Origin working filter folder
	string 	strFilterFile = "ASCII";
    int 	nRet = load_import_filter(strFilterFile, strFile1, strPageName, nLocation, trFilter);
    if( 0 != nRet )
    {
    	out_str("Fail to load import filter");	
    	return;
    }   

    
    // 2. Modify filter tree to import multiple files to separate new sheets
    /*
    Import Mode:
    0 - Replace Data
	1 - Append Columns
	2 - Append Rows
	3 - New Books
	4 - New Sheets
	5 - Auto
    */
    trFilter.Display.i1stMode.nVal = 4; 
    trFilter.ASCIMP.iMode.nVal = 4;
    
    // 3. Do import with filter tree     
    nRet = import_files(saDataFiles, strPageName, wks.GetIndex(), trFilter);
    if( 0 != nRet )
	{
		out_str("Fail to import data file");   
		return;
	}  
	
	out_str("Data file import to " + strPageName);		
}