Salome HOME
To avoid compilation pb on RedHat 8.0.
[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/ or email : webmaster.salome@opencascade.com
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
54   cout << "Check the study lock, locking"   << endl;
55   aStudy->SetStudyLock("SRN");
56   cout << "Is study locked = " << aStudy->IsStudyLocked() << endl;
57   cout << "Get study locker : " << aStudy->GetLockerID() << endl;
58   aStudy->UnLockStudy("SRN");
59   cout << "Is study locked = " << aStudy->IsStudyLocked()  << endl;
60
61   Handle(SALOMEDSImpl_StudyBuilder) aBuilder = aStudy->NewBuilder();
62   cout << "StudyBuilder is created " << endl;
63   Handle(SALOMEDSImpl_SComponent) aSC = aBuilder->NewComponent("TEST");
64   cout << "New component with type " << aSC->ComponentDataType() << " is created " << endl;
65   Handle(SALOMEDSImpl_SObject) aSO = aBuilder->NewObject(aSC);
66   cout << "New SObject with  ID = " << aSO->GetID() << " is created"  << endl;
67   TCollection_AsciiString anEntry;
68   TDF_Tool::Entry(aSO->GetLabel(), anEntry);
69   cout << "An entry of newly created SO is "  << anEntry << endl;
70   Handle(SALOMEDSImpl_AttributeIOR) aIORA = SALOMEDSImpl_AttributeIOR::Set(aSO->GetLabel(), "ior1234");
71   cout << "New AttributeIOR is created, it contains " << aIORA->Value() << endl;
72   Handle(SALOMEDSImpl_GenericAttribute) ga = Handle(SALOMEDSImpl_GenericAttribute)::DownCast(aIORA);
73   cout << "Attribute has type: " << ga->Type() << " and value: " << ga->Save() << endl; 
74   cout << "Just another way to create an attribute: official one :) " << endl;
75   Handle(TDF_Attribute) aTDFAttr =  aBuilder->FindOrCreateAttribute(aSO, "AttributeName");  
76   Handle(SALOMEDSImpl_AttributeName) aRN = Handle(SALOMEDSImpl_AttributeName)::DownCast(aTDFAttr);
77   aRN->SetValue("name_attribute");
78   cout << " The type = " << aRN->Type() << endl;
79   ga = Handle(SALOMEDSImpl_GenericAttribute)::DownCast(aRN);
80   cout << "Attribute has type: " << ga->Type() << " and value: " << ga->Save() << endl;   
81   cout << "Check GetObjectPath: " << aStudy->GetObjectPath(aSO) << endl;
82   
83   Handle(SALOMEDSImpl_SObject) aSubSO = aBuilder->NewObject(aSO);
84   aTDFAttr =  aBuilder->FindOrCreateAttribute(aSubSO, "AttributeIOR");  
85   Handle(SALOMEDSImpl_AttributeIOR) aIOR2 = Handle(SALOMEDSImpl_AttributeIOR)::DownCast(aTDFAttr);
86   aIOR2->SetValue("some ior");
87   aBuilder->Addreference(aSubSO, aSO);
88   Handle(SALOMEDSImpl_SObject) aRefObject;
89   aSubSO->ReferencedObject(aRefObject);
90   cout << "Check reference : ReferencedObject is " << aRefObject->GetID() << endl;
91   cout << "Check : Remove object: " << endl;
92   aBuilder->RemoveObject(aSubSO);
93   cout << "Remove: done" << endl;
94
95   cout << "Check AttributeTreeNode " << endl;
96   aTDFAttr =  aBuilder->FindOrCreateAttribute(aSO, "AttributeTreeNode");  
97   cout << Handle(SALOMEDSImpl_GenericAttribute)::DownCast(aTDFAttr)->Type() << endl;
98   cout << "Check AttributeTreeNode : done " << endl;
99
100   aTDFAttr =  aBuilder->FindOrCreateAttribute(aSO, "AttributeParameter");  
101   cout << Handle(SALOMEDSImpl_GenericAttribute)::DownCast(aTDFAttr)->Type() << endl;
102
103   cout << "Check the attributes on SObject" << endl;
104   Handle(TColStd_HSequenceOfTransient) aSeq = aSO->GetAllAttributes();
105   for(int i = 1; i <= aSeq->Length(); i++) 
106     cout << "Found: " << Handle(SALOMEDSImpl_GenericAttribute)::DownCast(aSeq->Value(i))->Type() << endl;
107
108
109
110   cout << "Check AttributeParameter "   << endl;
111
112   Handle(SALOMEDSImpl_AttributeParameter) AP = Handle(SALOMEDSImpl_AttributeParameter)::DownCast(aTDFAttr);
113
114   cout << "AttributeParameter with type : " << AP->Type() << endl;
115   
116   AP->SetInt("1", 123);
117   cout << "IsSet for int: " << AP->IsSet("1", PT_INTEGER) << " value : " << AP->GetInt("1") << endl;
118   //for(int i = 2; i < 5; i++) AP->SetInt(i, i*i);
119
120   AP->SetReal("1", 123.123);
121   cout << "IsSet for real: " << AP->IsSet("1", PT_REAL) << " value : " << AP->GetReal("1") << endl;
122   //for(int i = 2; i < 5; i++) AP->SetReal(i, 0.1);
123
124   AP->SetString("1", "value is 123.123!");
125   cout << "IsSet for string: " << AP->IsSet("1", PT_STRING) << " value : " << AP->GetString("1") << endl; 
126   /*
127   for(int i = 2; i < 5; i++) {
128     TCollection_AsciiString s((double)(1.0/i));
129     cout << "Setting for " << i << " value : " << s  << endl;
130     AP->SetString(i, s); 
131   }
132   */
133
134   AP->SetBool("1", true);
135   cout << "IsSet for bool: " << AP->IsSet("1", PT_BOOLEAN) << " value : " << AP->GetBool("1") << endl;
136   //for(int i = 2; i < 5; i++) AP->SetBool(i, 0);
137   
138   vector<double> v;
139   v.push_back(111.111);
140   v.push_back(222.22222);
141   v.push_back(333.3333333);
142   AP->SetRealArray("1", v);
143   cout << "IsSet for array: " << AP->IsSet("1", PT_REALARRAY);
144   vector<double> v2 = AP->GetRealArray("1");
145   cout.precision(10);
146   cout << " values :  "; 
147   for(int i = 0; i<v2.size(); i++) cout << v2[i] << " ";
148   cout << endl;
149   
150   v[0] = 211.111;
151   v[1] = 422.22222;
152   v[2] = 633.3333333;
153   AP->SetRealArray("2", v);
154
155   vector<int> vi;
156   vi.push_back(1);
157   vi.push_back(2);
158   AP->SetIntArray("2", vi);
159   
160   vector<string> vs;
161   vs.push_back("hello, ");
162   vs.push_back("world!");
163   AP->SetStrArray("3", vs);        
164
165   TCollection_AsciiString as = AP->Save();
166   cout << "AS = " << as << endl;
167   AP->Load(as);
168   
169   cout << "Restored string with id = 1 is: " << AP->GetString("1") << endl;
170   cout << "Restored int with id = 2 is: " << AP->GetInt("1")  << endl;
171   cout << "Restored real with id = 3 is: " << AP->GetReal("1")  << endl;
172   cout << "Restored bool with id = 1 is: " << AP->GetBool("1")  << endl;
173   
174   v2 = AP->GetRealArray("2");
175   cout << "Restored real array with id = 2 is: ";
176   for(int i = 0; i<v2.size(); i++) cout << v2[i] << " ";
177   cout << endl;
178
179   vi = AP->GetIntArray("2");
180   cout << "Restored int array with id = 2 is: ";
181   for(int i = 0; i<vi.size(); i++) cout << vi[i] << " ";
182   cout << endl;
183   
184   vs = AP->GetStrArray("3");
185   cout << "Restored string array with id = 2 is: ";
186   for(int i = 0; i<vs.size(); i++) cout << vs[i] << " ";
187   cout << endl;
188
189   cout << "Check RemoveID 1 with type PT_INTEGER" << endl;
190   AP->RemoveID("1", PT_INTEGER);
191   cout << "IsSet with id = 1, type = PT_INTEGER : " << AP->IsSet("1", PT_INTEGER)  << endl;
192   cout << "Check RemoveID is done" << endl;
193   
194   cout << "Check AttributeParameter : done"   << endl;
195
196   cout << "Test finished " << endl;    
197   return 0;
198 }
199