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