11.2.1 Introduction to Origin C and Code Builder


Video Image.png Video Text Image.png Website blog icon circle.png Blog Image 33x33px.png

Summary

Origin C supports a nearly-complete ANSI C language syntax as well as a subset of C++ features including internal and DLL-extended classes. In addition, Origin C is "Origin aware". This means that Origin objects such as worksheets and graphs are mapped to classes in Origin C, allowing direct manipulation of these objects and their properties from Origin C.

Origin C's integrated development environment (IDE) is called Code Builder. Code Builder provides standard tools for writing, compiling, and debugging your Origin C programs. Once an Origin C function is compiled and linked, the function can be called in various ways from the Origin or Code Builder workspaces.

To open Code Builder, click the Button Code Builder.png button on the Origin Standard toolbar, or press ALT + 4 from the keyboard.

Minimum Origin Version Required: Origin 9

What you will learn

This tutorial will show you how to:

  • Create an Origin C function.
  • Debug and Run the Function.
  • Run the Function Using the Custom Routine Button.

Steps

Create an Origin C Function

  1. In Code Builder, start with a new workspace (File: New Workspace...). On the Code Builder Standard toolbar, click the New Button New.png button to open the New File dialog.
  2. Select C File to create a .c file. Type Tutorial in the File Name text box. In the Location text box, specify the User Files Folder/OriginC folder to save your source file. Make sure that the Add to Workspace box is checked. Click OK. A file named Tutorial.c is created and opened in the Code Builder workspace.
    Introduction to Origin C and Code Builde 1.png
  1. Copy and paste the following code beneath the line that reads "// start your functions here.":
    void new_wks()
    {
    	Worksheet wks;
    	wks.Create("Origin"); // create a Worksheet window with template - Origin
    
    	vector& vecX = wks.Columns(0).GetDataObject();
    	vector& vecY = wks.Columns(1).GetDataObject();
    
    	vecX.Data(1, 10, 1);
    	vecY.Data(0.1, 1, 0.1);
    
    	vector<string> vsLongName = 
    	{
    		"Index",
    		"Data"
    	};
    
    	for (int nCol = 0; nCol < wks.GetNumCols(); ++nCol)
    	{
    		Column col (wks, nCol);
    		col.SetLongName(vsLongName[nCol]);
    	}
    }

    This new_wks function will create a worksheet using the Origin template. It fills the first column with numbers from 1 to 10, incrementing by 1. The second column is filled with numbers from 0.1 to 1, incrementing by 0.1. Lastly, it sets the Long Names of the first and second columns as Index and Data, respectively.

  2. Click the Save button Button Save.png.

Mini-Tutorial: Find more examples of the usage of GetDataObject and use a bookmark to quickly navigate to a marked line.

  1. Click line 33 and click the Toggle Bookmark button Button Toggle Bookmark.png.
  2. Select GetDataObject() in line 37 and click the Find in files button Button Find In Files.png.
  3. In the Find in Files dialog box, set In files/file types as *.c,*.cpp and In folder as <program folder>\OriginC. Check all the boxes in this dialog and click the Find button.
  4. In the Find Results window, double-click any line containing GetDataObject(). This opens the file containing this string and jumps to the related section.
  5. To back to the line marked in step1 activate the Bookmarks window. Double click the bookmark added in step 1 of this mini-tutorial and the cursor jumps to line 33 in the Tutorial.c file.
Origin 2015 provides some useful tools:
  • Quick open files
    Prss SHIFT + ALT + O or select Tools: Quick Open Files to open the Quick Open Files dialog.
  • Switch between .h and .c/.cpp file
    When open a .h file, press CTRL + ALT + O to open the corresponding .c or .cpp file, and vice versa.
  • Find symbols
    Press SHIFT + ALT + S or select Tools: Find Symbols to open the Find Symbols dialog.

Debug and Run the Function

  1. On the Code Builder Standard toolbar, click the Build buttonButton Build.png. This compiles the new_wks function and performs linking (Please make sure the Tutorial.c file is active).
    Debug Code Builder Output.png
  2. Click the space just to the left of lines 34, 35, 37, 50. A red ball icon will be placed at the beginning of each line, indicating that you added breakpoints to these lines. These breakpoints are listed in the Breakpoints window.
  3. In the Breakpoints window, clear the box before tutorial.c, line 35 to disable this breakpoint. Note that the breakpoint icon before line 35 becomes a hollow circle icon.

To delete a breakpoint, you can click on the breakpoint icon before the corresponding line, or right-click on the breakpoint in the Breakpoints window, and select Delete Breakpoint.

  1. Right-click on the breakpoint of line 37. Select When Hit... to open a dialog. Check the box before Print a message. In the text box under Print a message:, append the text test code runs to here. . Click OK to finish. Note that this breakpoint's icon is now a diamond.
    Code Builder When Hit.png
  2. Right click on the breakpoint at line 50. Select Condition... to open a dialog. Enable Condition and type nCol==1 in the text box. Click OK.
    Code Builder Condition.png
  3. To run this code, type the function name new_wks in the upper panel of the LabTalk Console window.
  4. Press ENTER. A yellow arrow will appear upon the first breakpoint icon, indicating which line of code is executed to.
  5. Press F8 to continue debugging the code. You will see the yellow arrow icon moving to the line 50.
  6. In the Command & Results window, the message added in the breakpoint of line 37 is printed in the lower panel.
    Code Builder Print Msg.png
  7. Activate the Variables window. The variable names are listed in the first column and their values are listed in the second column. Check the value of nCol.
    Code Builder nCol.png
  8. Press F5 to run over all codes, the yellow arrow will disappear.
  9. Return to the Origin workspace. You can see that a workbook is created as in the following image.
    Code Builder New Wks.png

Run the Function Using the Custom Routine Button

  1. Hold the CTRL + SHIFT keys and click the Custom Routine buttonButton Custom Routine.png. It will open the Custom.ogs file in Code Builder.
  2. With the cursor at line 11, click the Comment button Button Comment.png to comment out line 11.
  1. Type the following code at lines 12 and 13.
     Run.LoadOC(%YOriginC\Tutorial.c); // %Y is user file folder
     new_wks();

    This code will load the Tutorial.c file we added in the previous section and run the new_wks() function.

  2. Press the Save buttonButton Save.png.
  3. Return to the Origin workspace. Click the Custom Routine buttonButton Custom Routine.png. A new worksheet will be created and filled with data as it was in the previous section.
    Code Builder Cus Routine Result.png