Salome HOME
PR: merge from branch BR_UnitTests tag mergeto_trunk_17oct05
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_Client.cxx
1 //  SALOME SALOMEDS : data structure of SALOME and sources of Salome data server 
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SALOMEDS_Client.cxx
25 //  Author : Yves FRICAUD
26 //  Module : SALOME
27 //  $Header$
28
29 #include <SALOMEconfig.h>
30 #include CORBA_SERVER_HEADER(SALOMEDS)
31 #include "SALOMEDS_StudyManager_i.hxx"
32 #include "utilities.h"
33 #include "SALOMEDS_AttributeName_i.hxx"
34 #include "HDFOI.hxx"
35 using namespace std;
36
37 //============================================================================
38 /*! Function :
39  *  Purpose  : 
40  */
41 //============================================================================
42 static void DumpComponent(SALOMEDS::Study_ptr Study,SALOMEDS::SObject_ptr SO,Standard_Integer offset) {
43   SALOMEDS::SObject_var RefSO;
44   SALOMEDS::ChildIterator_var it = Study->NewChildIterator(SO);
45   for (; it->More();it->Next()){
46     SALOMEDS::SObject_var CSO= it->Value();
47     SALOMEDS::GenericAttribute_var anAttr;
48     if (CSO->FindAttribute(anAttr,"AttributeName")) 
49     {
50       SALOMEDS::AttributeName_var Name = SALOMEDS::AttributeName::_narrow(anAttr);
51       CORBA::String_var Val = Name->Value();
52       for (Standard_Integer i = 1; i <= offset ; i++) 
53         MESSAGE("--");
54       MESSAGE(">"<<CSO->GetID()<<Val);
55     }
56     if (CSO->ReferencedObject(RefSO)) {
57       for (Standard_Integer i = 1; i <= offset ; i++) 
58         MESSAGE(" ");
59       MESSAGE("*Reference"<<RefSO->GetID());
60     }
61     DumpComponent(Study,CSO,offset+2);
62   }
63 }
64
65 //============================================================================
66 /*! Function :
67  *  Purpose  : 
68  */
69 //============================================================================
70 static void DumpStudy (SALOMEDS::Study_ptr Study) {
71   MESSAGE("Explore Study and Write name of each object if it exists");
72   
73   Standard_CString name;
74   SALOMEDS::SComponentIterator_var itcomp = Study->NewComponentIterator();
75   Standard_Integer offset = 1;
76   for (; itcomp->More(); itcomp->Next()) {
77     SALOMEDS::SComponent_var SC = itcomp->Value();
78     name = SC->ComponentDataType();
79     MESSAGE("-> ComponentDataType is "<<name);  
80     DumpComponent(Study,SC,offset);
81   }
82 }
83
84 //============================================================================
85 /*! Function :
86  *  Purpose  : 
87  */
88 //============================================================================
89 static void Test(SALOMEDS::StudyManager_ptr myStudyMgr )
90 {
91   try {
92   Standard_CString name;
93   MESSAGE("Create New Study Study1");
94   SALOMEDS::Study_var myStudy = myStudyMgr->NewStudy("Study1");
95  
96   MESSAGE("Create Builder ");
97   SALOMEDS::StudyBuilder_var StudyBuild = myStudy->NewBuilder();
98
99
100   // Create new components
101   SALOMEDS::GenericAttribute_var anAttr;
102   SALOMEDS::AttributeName_var Name;
103   SALOMEDS::AttributeComment_var Comment;
104   SALOMEDS::AttributePersistentRef_var PRef;
105
106   // GEOM
107   MESSAGE("Add Component GEOM"); 
108   SALOMEDS::SComponent_var Geom = StudyBuild->NewComponent("GEOM");
109   MESSAGE("Add attribute name GEOM to component Geom");
110   anAttr = StudyBuild->FindOrCreateAttribute(Geom, "AttributeName");
111   Name = SALOMEDS::AttributeName::_narrow(anAttr);
112   Name->SetValue("Geometry");
113
114   // MESH
115   MESSAGE("Add Component MESH");
116   SALOMEDS::SComponent_var Mesh = StudyBuild->NewComponent("MESH");
117   anAttr = StudyBuild->FindOrCreateAttribute(Mesh, "AttributeName");
118   Name = SALOMEDS::AttributeName::_narrow(anAttr);
119   Name->SetValue("Mesh");
120
121   //Create objects
122
123   //Value
124   //box
125   MESSAGE("Add Object  box under GEOM");
126   SALOMEDS::SObject_var box = StudyBuild->NewObject(Geom);
127
128   MESSAGE("Add Attribute Name in object box");
129   anAttr = StudyBuild->FindOrCreateAttribute(box, "AttributeName");
130   Name = SALOMEDS::AttributeName::_narrow(anAttr);
131   Name->SetValue("box_0");
132
133   MESSAGE("Add Attribute Comment in object box");
134   anAttr = StudyBuild->FindOrCreateAttribute(box, "AttributeComment");
135   Comment = SALOMEDS::AttributeComment::_narrow(anAttr);
136   Comment->SetValue("this is a box");
137
138   //mesh_box
139   MESSAGE("Add Object  mesh_box under MESH");
140   SALOMEDS::SObject_var mesh_box = StudyBuild->NewObject(Mesh);
141
142   MESSAGE("Add Attribute Name in object mesh_box");
143   anAttr = StudyBuild->FindOrCreateAttribute(mesh_box, "AttributeName");
144   Name = SALOMEDS::AttributeName::_narrow(anAttr);
145   Name->SetValue("mesh_box_0");
146
147   // Test attribute delete
148   //MESSAGE("Delete Attribute Name in object mesh_box");
149   //StudyBuild->RemoveAttribute (mesh_box,SALOMEDS::Name);
150
151   //mesh_box_face
152   MESSAGE("Add Object mesh_box_face under mesh_box");
153   SALOMEDS::SObject_var mesh_box_face = StudyBuild->NewObject(mesh_box);
154
155   MESSAGE("Add Attribute Name in object mesh_box_face");
156   anAttr = StudyBuild->FindOrCreateAttribute(mesh_box_face, "AttributeName");
157   Name = SALOMEDS::AttributeName::_narrow(anAttr);
158   Name->SetValue("mesh_box_face_0");
159
160   MESSAGE("Add Reference mesh_box_face to box");
161   StudyBuild->Addreference (mesh_box_face,box);
162
163   //cylinder
164   MESSAGE("Add Object  cylinder under GEOM");
165   SALOMEDS::SObject_var cylinder = StudyBuild->NewObject(Geom);
166
167   MESSAGE("Add Attribute Name in object cylinder");
168   anAttr = StudyBuild->FindOrCreateAttribute(cylinder, "AttributeName");
169   Name = SALOMEDS::AttributeName::_narrow(anAttr);
170   Name->SetValue("cylinder_0");
171
172   MESSAGE("Add Attribute Persistent Reference in object cylinder");
173   anAttr = StudyBuild->FindOrCreateAttribute(cylinder, "AttributePersistentRef");
174   PRef = SALOMEDS::AttributePersistentRef::_narrow(anAttr);
175   PRef->SetValue("/HDFROOT/GEOM/cylinder");
176
177   // Test Undo
178   MESSAGE("Test Undo");
179
180   StudyBuild->UndoLimit(3);
181   StudyBuild->NewCommand();
182   MESSAGE("Add Object  mesh_cylinder under MESH");
183   SALOMEDS::SObject_var mesh_cylinder = StudyBuild->NewObject(Mesh);
184   MESSAGE("Add Attribute Name in object mesh_cylinder");
185   anAttr = StudyBuild->FindOrCreateAttribute(mesh_cylinder, "AttributeName");
186   Name = SALOMEDS::AttributeName::_narrow(anAttr);
187   Name->SetValue("mesh_cylinder_0");
188   StudyBuild->CommitCommand();
189
190   MESSAGE("Test GetStudy");
191   SALOMEDS::Study_var stu = mesh_cylinder->GetStudy();
192   MESSAGE ("-> Study Name is "<<stu->Name());
193
194   DumpStudy(myStudy);
195
196   StudyBuild->Undo();
197   // Study should have no trace of object mesh_cylinder
198   DumpStudy(myStudy);
199
200  
201   //myStudyMgr->Open ((char*)name);
202   //MESSAGE("Name " << name);
203
204   // GetOpenStudies
205   MESSAGE("GetOpenStudies list");
206   SALOMEDS::ListOfOpenStudies_var _list_open_studies =  myStudyMgr->GetOpenStudies();
207
208   for (unsigned int ind = 0; ind < _list_open_studies->length();ind++)
209     {
210       MESSAGE("Open studies list : " << _list_open_studies[ind]);  
211     }
212
213
214   // GetStudyByName
215   SALOMEDS::Study_var myStudy1 =myStudyMgr->GetStudyByName(_list_open_studies[0]);
216   MESSAGE("GetStudyByName done");
217   
218   // Save as
219   myStudyMgr->SaveAs("/home/edeville/Study1.hdf",myStudy1, false);
220
221   // Get Persistent Reference of the study test
222   name = myStudy1->GetPersistentReference();
223   MESSAGE("Persitent Reference of the study " << name);
224
225   // Get Transient Reference of the study test
226   name = myStudy1->GetTransientReference();
227   MESSAGE("Transient Reference of the study " << name);
228
229   // FindComponent Test
230   SALOMEDS::SComponent_var compo = myStudy1->FindComponent("GEOM");
231   // Get ComponentDataType test
232   MESSAGE("Find ComponentDataType of compo");
233   name = compo->ComponentDataType();
234   MESSAGE("-> ComponentDataType is "<<name);  
235
236   
237   // FindComponentID Test
238   SALOMEDS::SComponent_var compo1 = myStudy1->FindComponentID("0:1:2");
239   // Get ComponentDataType test
240   MESSAGE("Find ComponentDataType of compo1");
241   name = compo1->ComponentDataType();
242   MESSAGE("-> ComponentDataType is "<<name);  
243
244   // FindObject Test
245   SALOMEDS::SObject_var objn = myStudy1->FindObject("cylinder_0");
246  // Test FindAttribute function : get AttributeName attribute
247   MESSAGE("Find Name in object objn");
248   if (objn->FindAttribute(anAttr, "AttributeName")) {
249     Name = SALOMEDS::AttributeName::_narrow(anAttr);
250     CORBA::String_var Val = Name->Value();
251     MESSAGE("-> Name is "<<Val);
252   }
253   else {
254     MESSAGE("-> Name is not found");
255   }
256
257   // FindObjectID Test
258   SALOMEDS::SObject_var obj = myStudy1->FindObjectID("0:1:2:1:1");
259  // Test FindAttribute function : get AttributeName attribute
260   MESSAGE("Find Name in object obj");
261   if (obj->FindAttribute(anAttr, "AttributeName")) {
262     Name = SALOMEDS::AttributeName::_narrow(anAttr);
263     CORBA::String_var Val = Name->Value();
264     MESSAGE("-> Name is "<<Val);
265   }
266   else {
267     MESSAGE("-> Name is not found");
268   }
269   //DumpStudy(myStudy1);
270   }
271   catch(HDFexception)
272     {
273       MESSAGE( "HDFexception ! " )
274     } 
275 }
276
277 ///////////////////////////// MAIN ///////////////////////////////////////
278 //////////////////////////////////////////////////////////////////////////
279 int main(int argc, char** argv)
280
281   try {
282     // Initialise the ORB.
283 #if OMNIORB_VERSION >= 4
284     const char* options[][2] = { { "giopMaxMsgSize", "104857600" }, { 0, 0 } };
285     CORBA::ORB_var orb = CORBA::ORB_init( argc , argv , "omniORB4", options) ;
286 #else
287     CORBA::ORB_var orb = CORBA::ORB_init(argc, argv, "omniORB3");
288     omniORB::MaxMessageSize(100 * 1024 * 1024);
289 #endif
290     
291     // Obtain a reference to the root POA.
292     CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
293     PortableServer::POA_var poa = PortableServer::POA::_narrow(obj);
294
295     SALOME_NamingService * salomens = new SALOME_NamingService(orb);
296
297     MESSAGE("Find StudyManager ");
298     CORBA::Object_ptr obj2 = salomens->Resolve("myStudyManager");
299     SALOMEDS::StudyManager_var myStudyMgr = SALOMEDS::StudyManager::_narrow(obj2);
300
301     // Obtain a POAManager, and tell the POA to start accepting
302     // requests on its objects.
303     PortableServer::POAManager_var pman = poa->the_POAManager();
304     pman->activate();
305
306     // Test basic services
307     Test(myStudyMgr);
308
309     orb->run();
310     orb->destroy();
311   }
312   catch(CORBA::SystemException&) {
313     MESSAGE("Caught CORBA::SystemException." )
314   }
315   catch(CORBA::Exception&) {
316     MESSAGE( "Caught CORBA::Exception." )
317   }
318   catch(omniORB::fatalException& fe) {
319     MESSAGE( "Caught omniORB::fatalException:" )
320     MESSAGE( "  file: " << fe.file() )
321     MESSAGE( "  line: " << fe.line() )
322     MESSAGE( "  mesg: " << fe.errmsg() )
323   }
324   catch(...) {
325     MESSAGE( "Caught unknown exception." )
326   }
327   return 0;
328 }