Hi Maersch,
This can be done with a combination of LabTalk script programming and Origin C code.
Try the following steps and please report back if this does not work.
Easwar
OriginLab
1> Create a new OC file in Code Builder and copy-paste the code pasted below, and save this file in the following location:
<your user files folder>\OriginC\OriginLab\WksImportUsingFilter.c
It is important to name it this way and save in this location to have the rest of the steps work.
Here is the OC code. Make sure you get the #include statements:
#include <Origin.h>
#include <..\OriginLab\fileimport.h>
#include <..\OriginLab\Filter_Utils.h>
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
// This function imports specified data file using specified import filter into
// the currently active worksheet.
// It is assumed that the Code Builder workspace already has the necessary files
// needed to call import functions related to the import wizard prior to compiling
// this function.
// In order to set up the workspace one can issue the following command from script:
// %a = system.path.program$;
// iret = run.loadoc(%aOriginC\OriginLab\ImportWiz.ocw);
// if( 0 != iret )
// {
// type -b "Failed to load workspace for importing";
// return;
// }
//
// Parameters:
// strFileName: Name of data file to be imported
// strFilterName: Name of filter file to use for importing.
// If blank, then the function looks for a filter that
// is saved in the worksheet
// Return:
// 0: Success
// -1: Active layer is not a worksheet
// -2: Filter name was blank, and could not find a filter in worksheet
// other: Error values returned by the import function
//
int wks_import_using_filter(string strFileName, string strFilterName = "")
{
Worksheet wks = Project.ActiveLayer();
if( !wks ) return -1;
WorksheetPage wpg = wks.GetPage();
// If filter file name is null...
Tree trFilter;
if( 0 == strFilterName.GetLength() )
{
// Check if there is a filter stored in wks
if( !fuLoadFilterFromPage(trFilter, wpg, FILTER_TYPE_ASCII) )
return -2;
// Import file using filter stored in wks
return ImportFile(wpg, trFilter, strFileName);
}
// If filter file name is not null, import using specified file name
else
return ImportFile(wpg, strFilterName, strFileName);
}
2> Go to your OPJ and import one of your files into the worksheet using the Import Wizard, and in the process save the import settings to a filter file. You can either save the filter right into the worksheet, or if you save to disk, then make sure to save it to your user files folder area.
3> In the worksheet add a new text label. You can give it any text string, such as "oncreate". Then right-click on the text label to bring up Label Control dialog using the context menu
4> In the label control dialog, change the "Script, Run After:" drop-down to "Window Create"
5> In the edit box below the drop-down, enter the following script code:
// First load and compile workspace for Import Wizard
%a = system.path.program$;
iret = run.loadoc(%aOriginC\OriginLab\ImportWiz.ocw);
if( 0 != iret )
{
type -b "Failed to load workspace for importing";
return;
}
// Now load and compile OC file with function to import
iret = run.loadoc(%YOriginC\OriginLab\WksImportUsingFilter.c);
if( 0 != iret )
{
type -b "Failed to load and compile OC file"
return;
}
// Now call OC function to import specific file
iret = wks_import_using_filter("C:\my_data.txt", %YFilters\myfilter.oif);
if( 0 != iret )
{
type -b "Failed to import file";
return;
}
6> In the above script, change the path/name of the data file and filter file as applicable to you, in the line:
iret = wks_import_using_filter("C:\my_data.txt", %YFilters\myfilter.oif);
If you are using a filter saved in the worksheet, then you can leave out the second argument.
7> Clear the data in your wks and save the OPJ
Now when you open the OPJ (manually or programmatically), the worksheet is "created" each time, and at the time of creation the script under step 5 will be executed. This script will load all necessary files into the Code Builder workspace and then will call the custom function in the new OC file to do the import and fill the wks with the new data from your text file.
Edited by - easwar on 09/01/2005 12:24:15 PM