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