The Origin Forum
File Exchange
Try Origin for Free
The Origin Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ | Send File to Tech support
Username:
Password:
Save Password
Forgot your Password? | Admin Options

 All Forums
 Origin Forum for Programming
 Forum for Origin C
 How can 3rd-party apps communicate with Origin?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

nilaysoft

China
Posts

Posted - 02/23/2005 :  05:12:44 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Origin Version (Select Help-->About Origin): 7.5
Operating System: WinXP Home

Hi, buddies,

Here I propose an approach that a third-party application can communicate with Origin, and Origin runs partially behind the scene. The original idea is due to a post of Mike Buess. The forum thread: http://www.originlab.com/forum/topic.asp?TOPIC_ID=3671

1. Create "CommandLine.ini" by excuting the following scripts in the Script Window.

file -c "%YOrigin.ini" "%YCommandLine.ini";
ini.file$=%YCommandLine.ini;
ini.Options.ScriptWindow=1;

Then copy 'CommandLine.ini' to your working directory, e.g. D:\Origin
2. Write an Origin C program that can accomplish your task. Here I will take single peak Gaussian fitting as the example.


/**
* Import data from an ASCII file
* PARAMETERS
* strFile - The file name of the source ASCII file.
* strSheet - The Worksheet name for import.
*/
bool read_from(String strFile, String strSheet)
{
// Create a Worksheet object
Worksheet wks;
wks.Attach(strSheet);
if (!wks.IsValid())
wks.Create();

// Rename the Worksheet, so we can operate it.
// Assume it is the active window now.
PageBase pb = Project.Pages();
pb.Rename(strSheet);

// Import
// Do not rename the worksheet page to the file name
// after import
if (wks.ImportASCII(strFile))
return true;
else
return false;
}

/**
* Export Worksheet data to an ASCII file
* PARAMETERS:
* strFile2 - The file name of the destination ASCII file.
* strSheet - The Workshhet name for export.
*/
bool write_to(String strFile2, String strSheet)
{
// Declare a Worksheet Object
Worksheet wks;
wks.Attach(strSheet);

// If the Worksheet doesn't exist, quit.
if (!wks.IsValid())
return false;

// If exists, export to ASCII.
if (-1 != wks.ExportASCII(strFile2, WKS_EXPORT_ALL))
return true;
else
return false;
}

/**
* Peform single-peak Gaussian fitting.
* PARAMETERS:
* strFile - The file name of the source ASCII file.
* strFile2 - The file name of the destination ASCII file.
* strSheet - The Worksheet name that will store source data.
*/
bool gaussian_fit(String strFile, String strFile2, String strSheet)
{
// Import
if (!read_from(strFile, strSheet))
return false;

// Source data Y column
String strCurve = strSheet;
strCurve += "_B";

// Begin fitting
using NLSF = LabTalk.NLSF; // Point to the NLSF object
NLSF.Init(); // Initialize the fitter
NLSF.Func$ = "gaussamp"; // Assign fitting function
NLSF.cleanupfitdata();
NLSF.FitData$ = strCurve; // Assign dataset name
NLSF.Execute("parainit"); // Perform automatic parameter initialization
NLSF.Fit(100); // Perform fit - up to 100 iterations


// The Worksheet name that contains fitting results.
String strSheet2 = NLSF.funcCol$;
strSheet2 = strSheet2.Left(strSheet2.Find("_", 0));

// Export
// out_str(strSheet2);
return write_to(strFile2, strSheet2);
}

nilaysoft

China
Posts

Posted - 02/23/2005 :  05:19:18 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
(continued)

3. Write the controlling codes in your own application. Here I use Window SDK.


// hwndOrigin is a global variable. It refers to the child window
// of the Script Window (i.e. Edit).

HWND hwndOrigin;

/**
* Initializations:
* 1. Start Origin, but show the script window only.
* 2. Find the handle of the script window.
* 3. Find the handle of the child of the script window (Edit),
* and save it as a global variable.
*/
BOOL initOrigin()
{
// The /h switch hides everything but the script window.
// The -i CommandLine switch insures that the script window will be open.
// Here we assume that CommandLine.ini is stored in D:\Origin
LPCTSTR lpstrPath = "Origin Path Here\\Origin75 /h -i D:\\Origin\\CommandLine";

// The handle already exit.
if (hwndOrigin != NULL)
return true;
// Start Origin from the command line, and
// Find the script window and its child by using FindWindowEx API.
else {
WinExec(lpstrPath, SW_SHOW);

HWND hwndOriginSW = FindWindowEx(NULL, NULL, "ORIGIN_SW", NULL);
if (hwndOriginSW != NULL)
{
hwndOrigin = FindWindowEx(hwndOriginSW, NULL, "Edit", NULL);
if (hwndOrigin != NULL)
{
return true;
}
}
}
// Failure
return false;
}

/**
* Exit Origin by sending 'exit' command to the script window.
*/
void exitOrigin()
{
// doc -s suppresses the "save or not" dialog box.
LPCTSTR lpstrCommand = "doc -s; exit";
// Send the command to the script window.
// WM_KEYDOWN excutes the command.
if (hwndOrigin != NULL)
{
SendMessage(hwndOrigin, WM_SETTEXT, 0, (LPARAM) lpstrCommand);
SendMessage(hwndOrigin, WM_KEYDOWN, VK_RETURN, 0L);
}
}


4. Perform fitting


if (!initOrigin())
{
AfxMessageBox("Origin initialization fails");
return 0;
}

// The file name that Origin will read data from
LPSTR lpstrFile = "D:\\Origin\\temp.txt";

// The file name that Origin will save resulting data to
LPSTR lpstrFile2 = "D:\\Origin\\temp2.txt";

// TODO
// Here are codes that can save the data to be fitted to 'lpstrFile'
//

// Open a new window by a user-created template 'Gauss'.
// The template includes the Origin C codes in step 2.
// Thus Origin C codes are automatically compiled when open
// the new data window
SendMessage(hwndOrigin, WM_SETTEXT, 0, (LPARAM) "win -t data D:\\Origin\\Gauss Data1");
SendMessage(hwndOrigin, WM_KEYDOWN, VK_RETURN, 0L);

// Send the fitting command to the script window.
char buffer[256];
sprintf(buffer, "gaussian_fit %s %s", lpstrFile, "Data1");
SendMessage(hwndOrigin, WM_SETTEXT, 0, (LPARAM) buffer);
SendMessage(hwndOrigin, WM_KEYDOWN, VK_RETURN, 0L);

// TODO
// Here are code that can read data stored in 'lpstrFile2';
//
exitOrigin();

return 0;


5. Reminders:
(1) This is just a prototype. There is much space to improve by combining 'Dialog builder' of Origin C.
(2) If the user's application supports COM, the above codes become the second choice.
(3) Origin runs partially behind the scene, only the Script Window is shown.
(4) The user's application manages its own process of Origin. Other running Origin windows will not be affected.

Edited by - nilaysoft on 02/23/2005 05:29:57 AM
Go to Top of Page
  Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000