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