12.2.4 Organizing and Accessing Origin C Functions


Techniques for using your Origin C functions.

Minimum Origin Version Required: Origin 8.0 SR0

What you will learn

This tutorial will show you how to:

  • Save your Origin C Function with your Project
  • Associate your Programs with Visual Objects
  • Load and Compile your Origin C Function from script

Saving your Origin C Function with your Project

One way to load and compile your Origin C function is to save the Origin C file as an attachment to your Origin Project (*.OPJ) file. When a project file is opened, all files attached to it are separated out and stored in a temporary folder. In addition, any Origin C file that was attached is also automatically loaded into the Code Builder workspace, and compiled. The function is then ready to be called upon opening the Origin Project file.

  1. Start a new Origin project file by clicking on the New Project button Button New.png on the Standard Toolbar.
  2. On the Standard Toolbar, click the Code Builder button Button Code Builder.png.
  3. From the Code Builder menu, select File: New. This opens the New File dialog box.
  4. In the top list-box, select C File.
  5. In the File Name text box, type: Test. Keep the Add to Workspace check box selected. Click OK. The file Test.c is added to the workspace.
  6. Select and copy the following function, and paste it into the Test.c file. Be sure to paste the text below the line that reads "//start your functions here."
void Plot_Data(string strTemplate, string strData) 
	// Create a graph window from a Template
	GraphPage gp;			
	BOOL bOK = gp.Create(strTemplate, CREATE_VISIBLE);
	if( !bOK )
	// Attach the first layer (0) to a GraphLayer object	
	GraphLayer gl = gp.Layers(0); 
	//Attach a dataset to a Curve object	
	Curve crv(strData); 
	// Add the Curve to the graph layer	
	int nPlot = gl.AddPlot(crv);	
	if(nPlot >= 0)	
		// Set plot color to Green(2)
		gl.DataPlots(nPlot).SetColor(2, TRUE);
		// Rescale this graph layer

The Plot_Data function takes two arguments: (1) the template name and (2) the name of a Y dataset to include (plot) in the layer.

  1. Click the Build button Button Build.png to compile and link the file.
  1. Drag-and-drop the file Test.c from the User subfolder branch of the Workspace tree, to the Project subfolder. (Hint: You may need to first expand the User subfolder branch to display the Test.c entry prior to dragging the file).
  1. Go back to the Origin interface and save the project by clicking the Save button Button Save Project.png on the Standard Toolbar. Give the project the name Test.OPJ, and save it in a location of your choosing.
  1. The Origin C file, Test.c, is now saved with the Project. To verify this, close the project, and go back to Code Builder. You will see that there are no entries under the Project subfolder of the Workspace tree. Now go back to Origin interface and reopen the project. Go to Code builder and verify that Test.c is now listed under the Project subfolder (Hint: you may need to expand the Project subfolder branch to see the Test.c entry).

Associating your Programs with Visual Objects

You will now learn how to create a button on a worksheet and program the button to call the Origin C function in the Test.c file that you saved with the project.

  1. Open the Test.OPJ project that you saved under step 9 (previous section).
  2. Highlight the A(X) and B(Y) columns, right-click and select Fill Columns With: Row Numbers.
  3. From the menu, select Format: Worksheet to open the Worksheet Properties dialog box.
  4. In the Size tab, Worksheet Measurement branch, set the Gap from Top to 40 and click OK to close the dialog.
Organizing and Accessing Origin C Functions 1.png

The worksheet now has sufficient space above the column headings to add a text label.

Right-click in the area directly above the two columns and choose Add Text.

  1. At the cursor, type: Plot Data.
  1. Click once outside the text label to deselect it.
  1. Right-click on the text label and choose Programming Control to open the Programming Control dialog box. (Hint: Please choose Label Control in Origin 7.5)
  1. From the Script, Run After drop-down list, choose Button Up.
  1. Type the following script in the text box at the bottom of the dialog box:
  1. Click OK.

Your text label will now look like a button.

Organizing and Accessing Origin C Functions 2.png
  1. Click the Plot Data button on your worksheet.
  1. The Plot_Data function in your Test.c file is called, and a scatter plot is created.
Notes: The script behind the button assumes that you have data in column B(Y) of the Data1 worksheet and that there is an associated X data set.

Loading and Compiling your Origin C Function from Script

In this tutorial we learned how to save an Origin C function along with the project file and then access the function from the Origin interface. Saving an Origin C file with a project limits access to functions within that file to only that project. When a new project is opened, the functions are not available any more.

To access functions in an Origin C file that is saved on disk, the file can be programmatically loaded and compiled using LabTalk script. The script command for performing the programmatic load and compile is run.LoadOC. Refer to the LabTalk Help files (Help: Programming: Labtalk) for more information on using this command.

This concludes the Origin C Functions tutorial.