1.4.4 Setting and Getting Data
Numeric Data
Excel VBA Complex Data Type
Origin supports complex columns and matrix objects. To transfer complex data, 2D array is needed.
Option Explicit
Const NUMPTS = 100
Public Sub SetAndGetComplexData()
Dim app As Origin.Application
Dim wbk As Origin.WorksheetPage
Dim Wks As Origin.Worksheet
Dim col As Origin.Column
Dim ii As Integer
Dim cc(1 To NUMPTS, 1 To 2) As Double
Dim data As Variant
For ii = 1 To NUMPTS
cc(ii, 1) = ii + 0.12
cc(ii, 2) = -100 + ii * 0.5
Next
Set app = New Origin.Application
app.NewProject
Set wbk = app.WorksheetPages.Add
Set Wks = wbk.Layers(0)
Set col = Wks.Columns.Add("CC")
col.DataFormat = DF_COMPLEX
col.SetData (cc)
' now we will try to get it back, if using 1D array, will only get the real part
data = col.GetData(ARRAY2D_NUMERIC)
ii = UBound(data)
Worksheets(1).Range("A1").Resize(ii, 2) = data
End Sub
Excel VBA Float Data Type
Option Explicit
Const NUMPTS = 100
'before running this, select col A in Excel and hit Del key to clear the data
'if already run before
' generate some data
Public Sub SetGetFloatData1D2DArray()
Dim app As Origin.Application
Dim wbk As Origin.WorksheetPage
Dim Wks As Origin.Worksheet
Dim col As Origin.Column
Dim ii As Integer
Dim ff(1 To NUMPTS) As Single
Dim data As Variant
For ii = 1 To NUMPTS
ff(ii) = ii + 0.12
Next
Set app = New Origin.Application
app.NewProject
Set wbk = app.WorksheetPages.Add
Set Wks = wbk.Layers(0)
Set col = Wks.Columns.Add("FF")
col.DataFormat = DF_FLOAT
col.SetData (ff)
' now we will try to get it back
data = col.GetData(ARRAY2D_NUMERIC, 90, NUMPTS)
ii = UBound(data)
Worksheets(1).Range("A1").Resize(ii, 1) = data
'we can also get data back as 1D array
data = col.GetData(ARRAY1D_NUMERIC)
For ii = 1 To UBound(data)
Cells(ii, 2) = data(ii)
Next
End Sub
Excel VBA to Transfer A Matrix
In this example we will create two matreces in an Origin MatrixSheet and set and get data in double format for one and complex for the second. Dimension of all matrices in a MatrixSheet must be the same in Origin
Option Explicit
Const NumRows = 50
Const NumCols = 50
Public Sub TrsMtx()
Dim org As Origin.Application
Dim orgMtPg As Origin.MatrixPage
Dim orgMtS As Origin.MatrixSheet
Dim orgMtObjS1 As Origin.MatrixObject
Dim orgMtObjS2 As Origin.MatrixObject
Dim nn As Boolean
Dim ii, jj As Long
Dim s1(1 To NumRows, 1 To NumCols) As Double
Dim s2(1 To NumRows, 1 To NumCols) As Double
'------------------------------------------------------------
Set org = New Origin.Application
org.NewProject
Set orgMtPg = org.MatrixPages.Add
orgMtPg.Layers.Add
Set orgMtS = orgMtPg.Layers(0)
orgMtS.Cols = NumCols
orgMtS.Rows = NumRows
Set orgMtObjS1 = orgMtS.MatrixObjects.Add
Set orgMtS = orgMtPg.Layers(1)
orgMtS.Cols = NumCols
orgMtS.Rows = NumRows
Set orgMtObjS2 = orgMtS.MatrixObjects(0)
For ii = 1 To NumRows
For jj = 1 To NumCols
s1(ii, jj) = jj * 0.2
s2(ii, jj) = ii * 2
Next jj
Next ii
'---------SetData---------------
orgMtObjS1.DataFormat = DF_DOUBLE
nn = orgMtObjS1.SetData(s1, 0, 0)
orgMtObjS2.DataFormat = DF_COMPLEX
nn = orgMtObjS2.SetData(s2, 0, 0)
org.Exit
End Sub
C# to and from a Column in a Worksheet
The using section typically looks like the following:
using System;
using System.Collections.Generic;
using System.Text;
using Origin; // this will allow using Column Worksheet etc without having to write Origin.Column
In this example we shows how to send an array of floats (4 byte floating point) to an Origin column and how to read the values back as 1D and 2D float array, as well as 2D Variant array and an array of strings with current display settings in Origin.
static void Main(string[] args)
{
doTest(100);
}
static void doTest(int npts)
{
object Default = System.Type.Missing;
float[] ff = new float[npts];
for (int ii = 0; ii < npts; ii++)
{
ff[ii]= ii;
}
Origin.ApplicationSI app = new Origin.ApplicationSI(); // to attach to running Origin
//Origin.Application app = new Origin.Application();
app.NewProject();
WorksheetPage wbk = app.WorksheetPages.Add(Default, Default);
Worksheet wks = wbk.Layers[0] as Worksheet;
wks.Name = "My Sheet Name";
Column col = wks.Columns.Add("FF");
col.DataFormat = COLDATAFORMAT.DF_FLOAT;
col.SetData(ff, Default);
Console.Write("Get Data into 1D array");
//read the floats back as 1D float array, must use 0 lower bound for .NET array
object data = col.GetData(ARRAYDATAFORMAT.ARRAY1D_NUMERIC, Default, Default, 0);
float[] dd = data as float[];
Console.WriteLine("[0] = " + dd[0] + ", [last pt] = " + dd[npts-1]);
Console.Write("Get Data into 2D array");
float[,] dd2 = col.GetData(ARRAYDATAFORMAT.ARRAY2D_NUMERIC, Default, Default, 0) as float[,];
Console.WriteLine("[0,0] = " + dd2[0,0] + ", [last pt] = " + dd2[npts - 1, 0]);
Console.Write("Get Data as string array");
data = col.GetData(ARRAYDATAFORMAT.ARRAY1D_STR, Default, Default, 0);
string[] ss1 = data as string[];
Console.WriteLine("[0] = " + ss1[0] + ", [last pt] = " + ss1[npts - 1]);
Console.Write("Press any key to terminate");
Console.ReadKey();
}
C# To and From a Matrix
using Origin;
static void TestMatrix(int NumRows, int NumCols)
{
const int NumRows = 50;
const int NumCols = 50;
object Default = System.Type.Missing;
double[,] s1=new double[NumRows,NumCols];
double[, ,] s2 = new double[NumRows, NumCols, 2];
Origin.ApplicationSI org = new Origin.ApplicationSI();
org.NewProject();
MatrixPage orgMtPg = org.MatrixPages.Add(Default, Default);
MatrixSheet OrgMtS = orgMtPg.Layers[0] as MatrixSheet;
OrgMtS.Cols = NumCols;
OrgMtS.Rows = NumRows;
MatrixObject orgMtObjS1 = OrgMtS.MatrixObjects.Add();
MatrixObject orgMtObjS2 = OrgMtS.MatrixObjects.Add();
for (int ii = 0; ii < NumRows; ii++)
{
for (int jj = 0; jj < NumCols; jj++)
{
s1[ii, jj] = ii;
s2[ii, jj, 0] = ii * 0.1; /// real
s2[ii, jj, 1] = ii + jj * 0.01;//imaginary
}
}
orgMtObjS1.DataFormat = COLDATAFORMAT.DF_DOUBLE;
orgMtObjS1.SetData(s1, 0, 0);
orgMtObjS2.DataFormat = COLDATAFORMAT.DF_COMPLEX;
orgMtObjS2.SetData(s2, 0, 0);
double[,] data1 = orgMtObjS1.GetData(0, 0, -1, -1, ARRAYDATAFORMAT.ARRAY2D_NUMERIC, 0) as double[,];
Console.WriteLine(data1[0, 0] + ", " + data1[NumRows-1, NumCols-1]);
double[,,] data2 = orgMtObjS2.GetData(0, 0, -1, -1, ARRAYDATAFORMAT.ARRAY2D_NUMERIC, 0) as double[,,];
Console.WriteLine(data2[0, 0, 0] + ", " + data2[NumRows-1, NumCols-1, 0] +"+ i"+ data2[NumRows-1, NumCols-1, 1]);
Console.ReadKey(true);
org.Visible = Origin.MAINWND_VISIBLE.MAINWND_SHOW;
}
VC++ to Worksheet
#include "stdafx.h"
#include <afxdisp.h> // MFC classes, as to use COleSafeArray
//make sure Origin8.tlb in the same folder as this program
#import "Origin8.tlb" rename_namespace("Origin")
using namespace Origin;
#define NUM_COLS 4
#define NUM_ROWS 10
#define PAGE_TYPE_WKS 2
#define PAGE_VISIBLE 2
bool sendDataToWks()
{
Origin::IOApplicationPtr pOrigin;
pOrigin.CreateInstance(__uuidof(Application));
pOrigin->PutVisible(Origin::MAINWND_SHOW_BRING_TO_FRONT);
pOrigin->CreatePage(PAGE_TYPE_WKS, "Data", "w", PAGE_VISIBLE); //"w" is template name while "Data" as worksheet page name
Origin::WorksheetPagePtr pPage = pOrigin->GetActivePage();
pPage->Activate();
Origin::WorksheetPtr pwks = pPage->GetLayers()->Add("abc"); //new worksheet name is abc
pwks->Activate();
COleSafeArray csarr;
DWORD dwBound[] = {NUM_ROWS, NUM_COLS}; //declare the array as NUM_ROWS*NUM_COLS
csarr.Create(VT_UI2, 2, dwBound);
long nIndices[2];
USHORT uData;
for ( int ii = 0; ii < NUM_ROWS; ii++ )
{
for ( int jj = 0; jj < NUM_COLS; jj++)
{
nIndices[0] = ii;
nIndices[1] = jj;
uData = (ii + jj)%65536;
csarr.PutElement(nIndices, &uData);
}
}
pwks->SetData(csarr, 0, 0);
System::Console::ReadKey();//press key will exit Origin and this instance
pOrigin->Exit();
return true;
}
VC++ to Matrix
#include "stdafx.h"
#include <afxdisp.h> // MFC classes, as to use COleSafeArray
//make sure Origin8.tlb in the same folder as this program
#import "Origin8.tlb" rename_namespace("Origin")
using namespace Origin;
#define NUM_COLS 4
#define NUM_ROWS 10
#define PAGE_VISIBLE 2
#define PAGE_TYPE_MATRIX 5
bool sendDataToMatrix()
{
Origin::IOApplicationPtr pOrigin;
pOrigin.CreateInstance(__uuidof(Application));
pOrigin->PutVisible(Origin::MAINWND_SHOW_BRING_TO_FRONT);
pOrigin->CreatePage(PAGE_TYPE_MATRIX, "Data", "", PAGE_VISIBLE);
Origin::MatrixPagePtr pPage = pOrigin->GetActivePage();
pPage->Activate(); //bring it to the front
Origin::MatrixSheetPtr pmtx = pPage->GetLayers()->Add("abc"); //new matrixsheet name is abc
pmtx->Activate();
pmtx->PutCols(NUM_COLS);
pmtx->PutRows(NUM_ROWS);
Origin::MatrixObjectPtr pmtxObj = pmtx->GetMatrixObjects()->Add();
pmtxObj->Activate();
pmtxObj->DataFormat = Origin::DF_USHORT;
COleSafeArray csarr;
DWORD dwBound[] = {NUM_ROWS, NUM_COLS}; //declare the array as NUM_ROWS*NUM_COLS
csarr.Create(VT_UI2, 2, dwBound);
long nIndices[2];
USHORT uData;
for ( int ii = 0; ii < NUM_ROWS; ii++ )
{
for ( int jj = 0; jj < NUM_COLS; jj++)
{
nIndices[0] = ii;
nIndices[1] = jj;
uData = (ii + jj)%65536;
csarr.PutElement(nIndices, &uData);
}
}
pmtxObj->SetData(csarr, 0, 0);
System::Console::ReadKey();//press key will exit Origin and this instance
pOrigin->Exit();
return true;
}
|