Salome HOME
PR: merge from branch BR_auto_V310 tag mergefrom_OCC_development_for_3_2_0a2_10mar06
[modules/kernel.git] / src / SALOMEDSImpl / testDS.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/
19 //
20 //File:    testDS.cxx
21 //Author:  Sergey RUIN
22
23 #include <stdio.h>
24 #include <iostream> 
25 #include <vector>
26
27 #include <TColStd_HSequenceOfTransient.hxx>
28 #include <TCollection_AsciiString.hxx>
29 #include <TDocStd_Document.hxx>
30 #include <TDF_Attribute.hxx>
31 #include <TDF_Label.hxx>
32 #include <TDF_Data.hxx>
33 #include <TDF_Tool.hxx>
34
35 #include "SALOMEDSImpl_Attributes.hxx"
36 #include "SALOMEDSImpl_StudyManager.hxx"
37 #include "SALOMEDSImpl_Study.hxx"
38 #include "SALOMEDSImpl_StudyBuilder.hxx"
39 #include "SALOMEDSImpl_SObject.hxx"
40 #include "SALOMEDSImpl_SComponent.hxx"
41 #include "SALOMEDSImpl_AttributeParameter.hxx"
42
43 //#include "SALOMEDSImpl_.hxx"
44
45 int main (int argc, char * argv[])
46 {
47   cout << "Test started " << endl;
48
49   Handle(SALOMEDSImpl_StudyManager) aSM = new SALOMEDSImpl_StudyManager();
50   cout << "Manager is created " << endl;
51   Handle(SALOMEDSImpl_Study) aStudy = aSM->NewStudy("SRN");
52   cout << "Study with id = " << aStudy->StudyId() << " is created " << endl; 
53   Handle(SALOMEDSImpl_StudyBuilder) aBuilder = aStudy->NewBuilder();
54   cout << "StudyBuilder is created " << endl;
55   Handle(SALOMEDSImpl_SComponent) aSC = aBuilder->NewComponent("TEST");
56   cout << "New component with type " << aSC->ComponentDataType() << " is created " << endl;
57   Handle(SALOMEDSImpl_SObject) aSO = aBuilder->NewObject(aSC);
58   cout << "New SObject with  ID = " << aSO->GetID() << " is created"  << endl;
59   TCollection_AsciiString anEntry;
60   TDF_Tool::Entry(aSO->GetLabel(), anEntry);
61   cout << "An entry of newly created SO is "  << anEntry << endl;
62   Handle(SALOMEDSImpl_AttributeIOR) aIORA = SALOMEDSImpl_AttributeIOR::Set(aSO->GetLabel(), "ior1234");
63   cout << "New AttributeIOR is created, it contains " << aIORA->Value() << endl;
64   Handle(SALOMEDSImpl_GenericAttribute) ga = Handle(SALOMEDSImpl_GenericAttribute)::DownCast(aIORA);
65   cout << "Attribute has type: " << ga->Type() << " and value: " << ga->Save() << endl; 
66   cout << "Just another way to create an attribute: official one :) " << endl;
67   Handle(TDF_Attribute) aTDFAttr =  aBuilder->FindOrCreateAttribute(aSO, "AttributeName");  
68   Handle(SALOMEDSImpl_AttributeName) aRN = Handle(SALOMEDSImpl_AttributeName)::DownCast(aTDFAttr);
69   aRN->SetValue("name_attribute");
70   cout << " The type = " << aRN->Type() << endl;
71   ga = Handle(SALOMEDSImpl_GenericAttribute)::DownCast(aRN);
72   cout << "Attribute has type: " << ga->Type() << " and value: " << ga->Save() << endl;   
73   cout << "Check GetObjectPath: " << aStudy->GetObjectPath(aSO) << endl;
74   
75   Handle(SALOMEDSImpl_SObject) aSubSO = aBuilder->NewObject(aSO);
76   aTDFAttr =  aBuilder->FindOrCreateAttribute(aSubSO, "AttributeIOR");  
77   Handle(SALOMEDSImpl_AttributeIOR) aIOR2 = Handle(SALOMEDSImpl_AttributeIOR)::DownCast(aTDFAttr);
78   aIOR2->SetValue("some ior");
79   aBuilder->Addreference(aSubSO, aSO);
80   Handle(SALOMEDSImpl_SObject) aRefObject;
81   aSubSO->ReferencedObject(aRefObject);
82   cout << "Check reference : ReferencedObject is " << aRefObject->GetID() << endl;
83   cout << "Check : Remove object: " << endl;
84   aBuilder->RemoveObject(aSubSO);
85   cout << "Remove: done" << endl;
86
87   cout << "Check AttributeTreeNode " << endl;
88   aTDFAttr =  aBuilder->FindOrCreateAttribute(aSO, "AttributeTreeNode");  
89   cout << Handle(SALOMEDSImpl_GenericAttribute)::DownCast(aTDFAttr)->Type() << endl;
90   cout << "Check AttributeTreeNode : done " << endl;
91
92   aTDFAttr =  aBuilder->FindOrCreateAttribute(aSO, "AttributeParameter");  
93   cout << Handle(SALOMEDSImpl_GenericAttribute)::DownCast(aTDFAttr)->Type() << endl;
94
95   cout << "Check the attributes on SObject" << endl;
96   Handle(TColStd_HSequenceOfTransient) aSeq = aSO->GetAllAttributes();
97   for(int i = 1; i <= aSeq->Length(); i++) 
98     cout << "Found: " << Handle(SALOMEDSImpl_GenericAttribute)::DownCast(aSeq->Value(i))->Type() << endl;
99
100
101
102   cout << "Check AttributeParameter "   << endl;
103
104   Handle(SALOMEDSImpl_AttributeParameter) AP = Handle(SALOMEDSImpl_AttributeParameter)::DownCast(aTDFAttr);
105
106   cout << "AttributeParameter with type : " << AP->Type() << endl;
107   
108   AP->SetInt("1", 123);
109   cout << "IsSet for int: " << AP->IsSet("1", PT_INTEGER) << " value : " << AP->GetInt("1") << endl;
110   //for(int i = 2; i < 5; i++) AP->SetInt(i, i*i);
111
112   AP->SetReal("1", 123.123);
113   cout << "IsSet for real: " << AP->IsSet("1", PT_REAL) << " value : " << AP->GetReal("1") << endl;
114   //for(int i = 2; i < 5; i++) AP->SetReal(i, 0.1);
115
116   AP->SetString("1", "value is 123.123!");
117   cout << "IsSet for string: " << AP->IsSet("1", PT_STRING) << " value : " << AP->GetString("1") << endl; 
118   /*
119   for(int i = 2; i < 5; i++) {
120     TCollection_AsciiString s((double)(1.0/i));
121     cout << "Setting for " << i << " value : " << s  << endl;
122     AP->SetString(i, s); 
123   }
124   */
125
126   AP->SetBool("1", true);
127   cout << "IsSet for bool: " << AP->IsSet("1", PT_BOOLEAN) << " value : " << AP->GetBool("1") << endl;
128   //for(int i = 2; i < 5; i++) AP->SetBool(i, 0);
129   
130   vector<double> v;
131   v.push_back(111.111);
132   v.push_back(222.22222);
133   v.push_back(333.3333333);
134   AP->SetRealArray("1", v);
135   cout << "IsSet for array: " << AP->IsSet("1", PT_REALARRAY);
136   vector<double> v2 = AP->GetRealArray("1");
137   cout.precision(10);
138   cout << " values :  "; 
139   for(int i = 0; i<v2.size(); i++) cout << v2[i] << " ";
140   cout << endl;
141   
142   v[0] = 211.111;
143   v[1] = 422.22222;
144   v[2] = 633.3333333;
145   AP->SetRealArray("2", v);
146
147   vector<int> vi;
148   vi.push_back(1);
149   vi.push_back(2);
150   AP->SetIntArray("2", vi);
151   
152   vector<string> vs;
153   vs.push_back("hello, ");
154   vs.push_back("world!");
155   AP->SetStrArray("3", vs);        
156
157   TCollection_AsciiString as = AP->Save();
158   cout << "AS = " << as << endl;
159   AP->Load(as);
160   
161   cout << "Restored string with id = 1 is: " << AP->GetString("1") << endl;
162   cout << "Restored int with id = 2 is: " << AP->GetInt("1")  << endl;
163   cout << "Restored real with id = 3 is: " << AP->GetReal("1")  << endl;
164   cout << "Restored bool with id = 1 is: " << AP->GetBool("1")  << endl;
165   
166   v2 = AP->GetRealArray("2");
167   cout << "Restored real array with id = 2 is: ";
168   for(int i = 0; i<v2.size(); i++) cout << v2[i] << " ";
169   cout << endl;
170
171   vi = AP->GetIntArray("2");
172   cout << "Restored int array with id = 2 is: ";
173   for(int i = 0; i<vi.size(); i++) cout << vi[i] << " ";
174   cout << endl;
175   
176   vs = AP->GetStrArray("3");
177   cout << "Restored string array with id = 2 is: ";
178   for(int i = 0; i<vs.size(); i++) cout << vs[i] << " ";
179   cout << endl;
180
181   cout << "Check RemoveID 1 with type PT_INTEGER" << endl;
182   AP->RemoveID("1", PT_INTEGER);
183   cout << "IsSet with id = 1, type = PT_INTEGER : " << AP->IsSet("1", PT_INTEGER)  << endl;
184   cout << "Check RemoveID is done" << endl;
185   
186   cout << "Check AttributeParameter : done"   << endl;
187
188   cout << "Test finished " << endl;    
189   return 0;
190 }
191