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
 Simple calculate and plot script
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

Rhazal

3 Posts

Posted - 12/04/2013 :  06:30:35 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Origin Ver. and Service Release (Select Help-->About Origin): OriginPro 9
Operating System: Windows 7

I'm trying to write a script to complete 2 fairly simple tasks. First it should calculate the values in a new worksheet(named Widerstand) from data in an already existing one(named Spannung) and a value I given by the user.
Then it should plot this new data each seperately as one line-plot for each column of data and one plot with all columns together.
Here are 2 screenshots to clarify what I work with and want to do(for the plotting part)



As I have no experience with scripting, I tried to combine different tutorials I found and came up with this:

#include <GetNbox.h>

void calculate_and_plot()
{
double I;
GETN_BOX(trTemp)
GETN_NUM(I, 10^-3)
if(GetNBox(trTemp, "Verwendete Stromstärke I eingeben"))
{
I = trTemp.I.dVal;
printf("I=%f\n", I);
}
WorksheetPage wksPage("Kontaktwiderstand"); //Access workbook "Kontaktwiderstand"
int index = wksPage.AddLayer("Widerstand"); //Create new sheet "Widerstand"
Worksheet wksNew = wksPage.Layers(index) ; //Access "Widerstand"
set_active_layer(wksNew); //Set "Widerstand" active

Column colA;
colA.Attach(wks, 0);
colA.SetFormula("Spannung!A");//Fill the first column of "Widerstand" with the same values as in "Spannung"
colA.ExecuteFormula();

Column colB;
colB.Attach(wks, 1);
colB.SetFormula("Spannung!B*10^-6/I", AU_AUTO); //Calculate the values for column B with values from "Spannung" and Input I
colB.ExecuteFormula();

Worksheet wks = Project.ActiveLayer(); // get active worksheet
if( !wks )
{
out_str("Blatt mit Werten auswählen");
return;
}
GraphPage gp;
gp.Create("Line"); // create graph with Line template (Standard)
GraphLayer gl = gp.Layers(0); // layer 1 of the graph
Curve crv(wks, 0, 1);
int nPlot = gl.AddPlot(crv, IDM_PLOT_LINE); // add a line plot to the graph, return plot index
if( nPlot >= 0 )
gl.Rescale(); // rescale the graph
}


But there are a few problems and the part about selective plotting is missing too, so it would be good to get some advice on this.

Thanks for your help.

greg

USA
1378 Posts

Posted - 12/05/2013 :  10:45:01 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Just getting your code fixed so that it compiles should be a big help:

GETN_BOX(trTemp)
is a function so it requires a terminating semi-colon ...
GETN_BOX(trTemp);

GETN_NUM is a macro so it does NOT need the semi-colon, but you have to pass the correct arguments ...
GETN_NUM(I, "some text", 10^-3)

Your page name is longer than allowed for a Page Short Name, so ...
WorksheetPage wksPage("Kontaktwiders"); // tand"); //Access workbook "Kontaktwiderstand"

When you attach to columns A and B, the wks object has not been declared. Presumably this should be wksNew ..
Column colA;
colA.Attach(wksNew, 0);
...
Column colB;
colB.Attach(wksNew, 1);

Your two column formulas ("Spannung!A" and "Spannung!B*10^-6/I") will not work. You cannot use range syntax to directly address data; it's only used to declare a range. Try using Set Column Values in Origin to see if you can come up with the syntax that will calculate what you want. If you cannot, maybe adding some Before Formula script will help:
Column colA;
colA.Attach(wksNew, 0);
colA.SetFormula("sA" + STR_COL_FORMULAR_SEPARATOR + "range sA = Spannung!A");
colA.ExecuteFormula();

Column colB;
colB.Attach(wksNew, 1);
string strI = ftoa(I);
string strExpr = "sB * " + strI + STR_COL_FORMULAR_SEPARATOR + "range sB = Spannung!B";
colB.SetFormula(strExpr, AU_AUTO); //Calculate the values for column B with values from "Spannung" and Input I
colB.ExecuteFormula();
(This assumes Spannung is a worksheet in the same Kontaktwiderstand book.)


So the corrected code should all look like this:
#include <GetNbox.h>

void calculate_and_plot()
{
double I;

GETN_BOX(trTemp);
GETN_NUM(I, "some text", 10^-3)

if(GetNBox(trTemp, "Verwendete Stromstärke I eingeben"))
{
I = trTemp.I.dVal;
printf("I=%f\n", I);
}
WorksheetPage wksPage("Kontaktwiders"); // tand"); //Access workbook "Kontaktwiderstand"
int index = wksPage.AddLayer("Widerstand"); //Create new sheet "Widerstand"
Worksheet wksNew = wksPage.Layers(index) ; //Access "Widerstand"
set_active_layer(wksNew); //Set "Widerstand" active

Column colA;
colA.Attach(wksNew, 0);
colA.SetFormula("sA" + STR_COL_FORMULAR_SEPARATOR + "range sA = Spannung!A");
colA.ExecuteFormula();

Column colB;
colB.Attach(wksNew, 1);
string strI = ftoa(I);
string strExpr = "sB * " + strI + STR_COL_FORMULAR_SEPARATOR + "range sB = Spannung!B";
colB.SetFormula(strExpr, AU_AUTO); //Calculate the values for column B with values from "Spannung" and Input I
colB.ExecuteFormula();

Worksheet wks = Project.ActiveLayer(); // get active worksheet
if( !wks )
{
out_str("Blatt mit Werten auswählen");
return;
}
GraphPage gp;
gp.Create("Line"); // create graph with Line template (Standard)
GraphLayer gl = gp.Layers(0); // layer 1 of the graph
Curve crv(wks, 0, 1);
int nPlot = gl.AddPlot(crv, IDM_PLOT_LINE); // add a line plot to the graph, return plot index
if( nPlot >= 0 )
gl.Rescale(); // rescale the graph
}


What graphing you did is working so you just need to add the remaining datasets and create new graphs for each individual datasets.

Edited by - greg on 12/05/2013 10:53:14 AM
Go to Top of Page

Rhazal

3 Posts

Posted - 12/10/2013 :  06:53:47 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Thanks for your help and the explanations, those clarified a lot for me and the code works now.

One last question, as I'm now working on expanding the code for the other columns.
Is it necessary to redo those two parts

Column colB;
colB.Attach(wksNew, 1);
string strI = ftoa(I);
string strExpr = "sB * " + strI + STR_COL_FORMULAR_SEPARATOR + "range sB = Spannung!B";
colB.SetFormula(strExpr, AU_AUTO); //Calculate the values for column B with values from "Spannung" and Input I
colB.ExecuteFormula();

and

gp.Create("Line"); // create graph with Line template (Standard)
GraphLayer gl = gp.Layers(0); // layer 1 of the graph
Curve crv(wks, 0, 1);
int nPlot = gl.AddPlot(crv, IDM_PLOT_LINE); // add a line plot to the graph, return plot index
if( nPlot >= 0 )
gl.Rescale(); // rescale the graph

for each column and graph (and if I unterstood correctly declare a new strI and strExp for each)? Or is there a simple way to make this "autogrow" so that it would be independent from the actual number of columns and could just repeat it for each Y-column?

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