1.1.1 Creating a Simple OPJIn this example, we will create a simple OPJ that consists of a single workbook with a single sheet.
We will add only two columns, X and Y, with default Text & Numeric data type.
We will also setup the long name and units for each column.
Finally, we will put in some numeric values into these two columns and then save it as an OPJ file.
Excel VBA
Option Explicit
Const NUMPTS = 100
Public Sub CreateOPJ()
Dim org As Origin.Application
Dim orgWkBk As Origin.WorksheetPage
Dim orgWks As Origin.Worksheet
Dim orgColumn As Origin.Column
Dim ii As Long
Dim strPathName As Variant
Dim s1(1 To NUMPTS) As Double
Dim s2(1 To NUMPTS) As Double
'------------------------------------------------------------
On Error GoTo error
' Create the Origin COM object:
Set org = New Origin.Application
' Initialize new project:
org.NewProject
' Add a workbook
Set orgWkBk = org.WorksheetPages.Add
' The sheet
Set orgWks = orgWkBk.Layers(0)
' Add two Columns
orgWks.Columns.Add
orgWks.Columns.Add
' Set Long Names and Units to the two columns:
orgWks.Columns(0).LongName = "Temperature"
orgWks.Columns(0).Units = "(\+(o)C)"
orgWks.Columns(1).LongName = "Presure"
orgWks.Columns(1).Units = "(lb/in\+(2))"
' Set column types:
orgWks.Columns(0).Type = COLTYPE_X 'Set ColType as X, COLTYPE_X=3
orgWks.Columns(1).Type = COLTYPE_Y 'Set ColType as Y, COLTYPE_Y=0
' Set LongName and Units as visible
orgWks.LabelVisible(LT_LONG_NAME) = True
orgWks.LabelVisible(LT_UNIT) = True
' Set data array s1 and s2
For ii = 1 To NUMPTS
s1(ii) = ii * 0.1
s2(ii) = ii / 13.4
Next
' Send data to the Columns
orgWks.Columns(0).SetData (s1) ' col (1)
orgWks.Columns(1).SetData (s2) ' col (2)
'--------------------Save to an OPJ File------------------------------
' Get the pathname for saving the OPJ:
strPathName = Application.GetSaveAsFilename("My Project Name", "Project files (*.OPJ),*.OPJ", 0)
If strPathName = "False" Then
Exit Sub
End If
' Save:
If org.Save(strPathName) = False Then
MsgBox "Failed to save the project into " & strPathName
Else
MsgBox "Saved into " & strPathName
End If
Exit Sub
error:
MsgBox ("ERROR")
Resume Next
End Sub
VB6
Same as Excel VBA
VB.NET
Dim NUMPTS = 100
Public Sub CreateOPJ()
Dim org As Origin.Application
Dim orgWkBk As Origin.WorksheetPage
Dim orgWks As Origin.Worksheet
Dim ii As Long
Dim s1(NUMPTS) As Double
Dim s2(NUMPTS) As Double
Dim a
Dim strPathName As String
'------------------------------------------------------------
Try
' Create the Origin COM object:
org = New Origin.Application
' Initialize new project:
a = org.NewProject()
' Add a workbook
orgWkBk = org.WorksheetPages.Add
' The sheet:
orgWks = orgWkBk.Layers(0)
' Set Sheet name:
orgWks.Name = "RawData"
' Add two Columns
orgWks.Columns.Add()
orgWks.Columns.Add()
' Set Long Names and Units to the two columns:
orgWks.Columns(0).LongName = "Temperature"
orgWks.Columns(0).Units = "(\+(o)C)"
orgWks.Columns(1).LongName = "Presure"
orgWks.Columns(1).Units = "(lb/in\+(2))"
' Set column types:
orgWks.Columns(0).Type = Origin.COLTYPES.COLTYPE_X 'Set ColType as X, COLTYPE_X=3
orgWks.Columns(1).Type = Origin.COLTYPES.COLTYPE_Y 'Set ColType as Y, COLTYPE_Y=0
' Set LongName and Units as visible
orgWks.LabelVisible(Origin.LABELTYPEVALS.LT_LONG_NAME) = True
orgWks.LabelVisible(Origin.LABELTYPEVALS.LT_UNIT) = True
' Set data array s1 and s2
For ii = 0 To NUMPTS-1
s1(ii) = ii * 0.1
s2(ii) = ii / 13.4
Next
'Create a single column data range in the workSheet
orgWks.Columns(0).SetData (s1) ' col (1)
orgWks.Columns(1).SetData (s2) ' col (2)
'------------------------------------------------------------
' Get the pathname for saving the OPJ:
strPathName = Nothing
If (SaveFileDialog.ShowDialog = Windows.Forms.DialogResult.OK) Then
strPathName = SaveFileDialog.FileName
End If
If strPathName = Nothing Then
Exit Sub
End If
' Save:
If org.Save(strPathName) = False Then
MsgBox("Failed to save the project into " & strPathName)
Else
MsgBox("Saved into " & strPathName)
End If
Catch exception As Exception
End Try
End Sub
C#
public void CreateOPJ()
{
int NUMPTS = 100;
Origin.Worksheet orgWks;
Double[] s1 =new double [NUMPTS];
Double[] s2 =new double [NUMPTS];
//------------------------------------------------------------
try
{
//Create the Origin COM object:
Origin.Application org = new Origin.Application();
if (org == null)
{
Console.WriteLine("Origin could not be started. Check that your installation and project references are correct.");
return;
}
//Initialize new project:
org.NewProject();
//Add a workbook
Origin.WorksheetPage orgWkBk = org.WorksheetPages.Add(System.Type.Missing, System.Type.Missing);
//The sheet:
orgWks = (Origin.Worksheet)orgWkBk.Layers[0];
//Set Sheet name:
orgWks.Name = "RawData";
//Add two Columns
orgWks.Columns.Add(System.Type.Missing);
orgWks.Columns.Add(System.Type.Missing);
//Set Long Names, Units, and Comment to the two columns:
orgWks.Columns[0].LongName = "Temperature";
orgWks.Columns[0].Units = @"(\+(o)C)";
orgWks.Columns[1].LongName = "Presure";
orgWks.Columns[1].Units = @"(lb/in\+(2))";
//Set column types:
orgWks.Columns[0].Type = Origin.COLTYPES.COLTYPE_X;
orgWks.Columns[1].Type = Origin.COLTYPES.COLTYPE_Y;
//Set LongName and Units as visible
orgWks.set_LabelVisible(Origin.LABELTYPEVALS.LT_LONG_NAME, true);
orgWks.set_LabelVisible(Origin.LABELTYPEVALS.LT_UNIT, true);
//Set data array s1 and s2
for (int ii = 0; ii <= NUMPTS-1; ii++)
{
s1[ii] = ii * 0.1;
s2[ii] = ii / 13.4;
}
//Create a single column data range in the workSheet
orgWks.Columns[0].SetData(s1, System.Type.Missing); //col (1)
orgWks.Columns[1].SetData(s2, System.Type.Missing); //col (2)
//------------------------------------------------------------
String PathName = "C:\\ProjectName.opj";
// Save:
if (org.Save(PathName) == false )
{
Console.WriteLine("Failed to save the project into " + PathName);
}
else
{
Console.WriteLine("Saved into " + PathName);
}
}
catch
{
Console.WriteLine("ERROR");
}
}
|