Author |
Topic |
|
TreeNode
64 Posts |
Posted - 02/17/2010 : 7:49:34 PM
|
Origin Ver. 8.0 and Service Release (Select Help-->About Origin): SR6 Operating System: Winows XP
Hi everybody!
I want to start working on my buisness more object-oriented. So I have to build some user defined classes.
I started to inherit a class from Origin-class: GraphLayer. The only thing I wanted to do till now was to add a element variable, to hold information about the type of this Layer.
I want to create Layers of different types, and dependent on these types some functions should handle them in a different way.
So up to now a tried the following:
#ifndef MY_GRAPHLAYER_H
#define MY_GRAPHLAYER_H MY_GRAPHLAYER_H
enum LayerTypes
{
TYPE_ONE,
TYPE_TWO,
}
class My_GraphLayer : public GraphLayer
{
public:
My_GraphLayer( int nType, LPCSTR lpcszWinName, int nLayerNum = -1 ) : GraphLayer( lpcszWinName, nLayerNum )
{
m_nType = nType;
}
My_GraphLayer( int nType, Layer & layer ) : GraphLayer( layer )
{
m_nType = nType;
}
int GetLayerType() { return m_nType; }
int nType;
} // class My_GraphLayer : public GraphLayer
#endif
I setted up a class like shown above. After that I tried to add such inherited GraphLayers to a GraphPage:
void test_userdef_graphlayer()
{
GraphPage gp;
gp.Create("origin");
My_GraphLayer myGL( TYPE_ONE, gp.GetName() );
gp.AddLayer( myGL );
int nType = myGL.GetLayerType();
switch( nType )
{
case TYPE_ONE:
out_str("This Layers type is TYPE_ONE. \n");
break;
case TYPE_TWO:
out_str("This Layers type is TYPE_TWO. \n");
break;
default:
out_str("Type could not be detected. \n");
}
}
Up to here everything worked fine. The user defined GraphLayer could be added to the GraphPage. The type was stored in the member variable m_nType, and I could get this value by calling member-function GetLayerType().
But this works only at the point where I create that stuff. Later I get problems to cast my user defined GraphLayers. I tried something like that:
void cast_userdef_graphlayer()
{
GraphPage gp;
gp = Project.Pages();
My_GraphLayer myGL;
int nType;
foreach( GraphLayer gl in gp.Layers )
{
myGL = gl;
nType = myGL.GetLayerType();
}
}
In this example nType always has value 0. I dont know how to cast the My_GraphLayer objects.
foreach( My_GraphLayer myGL in gp.Layers ) doesnt work...
I am not surprised about that. But I dont know how to handle that. Does anybody have an idea what I can do to realize my intention?
I thought of using something like: dynamic_cast - Operator
But OriginC doesnt know this. Or do I just have to include a special library? Please help, if anybody knows in which way I could handle this problem
<!-- helping each other is a good thing...like TreeNode holding the branches and leaves of a Tree --> |
|
cpyang
USA
1406 Posts |
Posted - 02/18/2010 : 6:54:30 PM
|
Well, you just need to put your class inside the loop. I tested this with a graph having two layers and having different X1 and it works fine.
class My_GraphLayer : public GraphLayer
{
public:
My_GraphLayer( int nType, LPCSTR lpcszWinName, int nLayerNum = -1 ) : GraphLayer( lpcszWinName, nLayerNum )
{
m_nType = nType;
}
My_GraphLayer( int nType, Layer & layer ) : GraphLayer( layer )
{
m_nType = nType;
}
int GetLayerType() { return m_nType; }
private:
int m_nType;
};// class My_GraphLayer : public GraphLayer
void cast_userdef_graphlayer()
{
GraphPage gp;
gp = Project.Pages();
int nType = 1;;
foreach( GraphLayer gl in gp.Layers )
{
My_GraphLayer myGL(nType++, gp.GetName(), gl.GetIndex());
printf("Type =%d, X1=%g\n", myGL.GetLayerType(), myGL.X.From);
}
}
CP
|
|
|
TreeNode
64 Posts |
Posted - 02/18/2010 : 11:05:11 PM
|
Hi CP,
thank you for your quick answer!
But I am not sure if you understood my problem exactly. Because when I execute your function the value of the Type starts from 1, end ends with the number of the last Layer.
You are setting the value of m_nType in the foreach loop. And then get this value with the GetLayerType() method. In fact, till this point everything works fine.
But what I want to do is:
- at first creating a few of My_GraphLayer objects, where all of these could have the same or maybe different types: ( for example: nType of first Layer is 2, second Layer nType = 4, third Layer nType = 3, fourth layer nType = 3 ...)
- then add these My_GraphLayer layers to a GraphPage
- after that data will be imported, doing several operations, etc.
- and then there comes a time, where I have to get to know of which type a layer is, because dependent on the type it will be treated in different ways
I did not have success on achieving that. But there must be a way, I know it ... but dont know how ;D
Please help again !!
<!-- helping each other is a good thing...like TreeNode holding the branches and leaves of a Tree --> |
|
|
cpyang
USA
1406 Posts |
Posted - 02/20/2010 : 11:37:56 AM
|
I see, your class has to be a wrapper class, can not have any data as it is not persistent, so any data must be saved into the layer itself. If just one number, can save into the name, which as the added benifit of visible from Layer Management dialog. Here is my updated example:
class My_GraphLayer : public GraphLayer
{
public:
My_GraphLayer( Layer & layer ) : GraphLayer( layer )
{
}
int GetLayerType()
{
string str = GetName();
if(str.GetLength() < 2 || str[0] != 'T') return -1;
string strType = str.Mid(1);
return atoi(strType);
}
//same to layer name, but cannot use 1,2,3 as those are not allowed
//we have to prefix it with a letter
void SetLayerType(int nType)
{
string str;str.Format("T%d", nType);
SetName(str);
}
};// class My_GraphLayer : public GraphLayer
void cast_userdef_graphlayer()
{
GraphPage gp;
gp = Project.Pages();
// first set the type
int nType = 1;
foreach( GraphLayer gl in gp.Layers )
{
My_GraphLayer myGL(gl);
myGL.SetLayerType(nType++);
}
//now get them back
foreach( GraphLayer ll in gp.Layers )
{
My_GraphLayer myGL(ll);
printf("Type =%d, X1=%g\n", myGL.GetLayerType(), myGL.X.From);
}
}
|
|
|
TreeNode
64 Posts |
Posted - 03/13/2010 : 1:24:27 PM
|
Hi CP,
thank you very much! Thats exactly what I wanted to do. Really great, it works fine.
So what I have to remember for the future is, that when I want to inherit a class from internal Origin Objects to add some extra functionality I cant add new data elements but only new methods. Is that correct??
And let my ask you another question. The class My_GraphLayer in your new example now is a wrapper class?
|-- TreeNode ...|-- a?? ...|-- ha!! |
|
|
cpyang
USA
1406 Posts |
Posted - 03/14/2010 : 8:29:20 PM
|
Yes, My_GraphLayer is a wrapper class for the internal Origin GraphLayer object.
You cannot have persistent data in such a class as you can see the usage of it is on the stack. It just wrap the real Origin object during its life time. Any persistent data must be saved into the internal Origin object.
CP
|
|
|
TreeNode
64 Posts |
Posted - 03/15/2010 : 03:25:10 AM
|
Ok, think I understand. It is really helpful for me to get a better understanding of things like that. Thank you a lot for that!
|-- TreeNode ...|-- a?? ...|-- ha!! |
|
|
|
Topic |
|
|
|