Salome HOME
fd365b9109e65a4748a81fa88bb681e0fc90d017
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_Client.cxx
1 // Copyright (C) 2007-2016  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_AttributeName_i.hxx"
32 #include "SALOME_NamingService.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::Study_ptr myStudy)
89 {
90   try {
91   char* name;
92
93   MESSAGE("Create Builder ");
94   SALOMEDS::StudyBuilder_var StudyBuild = myStudy->NewBuilder();
95
96   // Create new components
97   SALOMEDS::GenericAttribute_var anAttr;
98   SALOMEDS::AttributeName_var Name;
99   SALOMEDS::AttributeComment_var Comment;
100   SALOMEDS::AttributePersistentRef_var PRef;
101
102   // GEOM
103   MESSAGE("Add Component GEOM"); 
104   SALOMEDS::SComponent_var Geom = StudyBuild->NewComponent("GEOM");
105   MESSAGE("Add attribute name GEOM to component Geom");
106   anAttr = StudyBuild->FindOrCreateAttribute(Geom, "AttributeName");
107   Name = SALOMEDS::AttributeName::_narrow(anAttr);
108   Name->SetValue("Geometry");
109
110   // MESH
111   MESSAGE("Add Component MESH");
112   SALOMEDS::SComponent_var Mesh = StudyBuild->NewComponent("MESH");
113   anAttr = StudyBuild->FindOrCreateAttribute(Mesh, "AttributeName");
114   Name = SALOMEDS::AttributeName::_narrow(anAttr);
115   Name->SetValue("Mesh");
116
117   //Create objects
118
119   //Value
120   //box
121   MESSAGE("Add Object  box under GEOM");
122   SALOMEDS::SObject_var box = StudyBuild->NewObject(Geom);
123
124   MESSAGE("Add Attribute Name in object box");
125   anAttr = StudyBuild->FindOrCreateAttribute(box, "AttributeName");
126   Name = SALOMEDS::AttributeName::_narrow(anAttr);
127   Name->SetValue("box_0");
128
129   MESSAGE("Add Attribute Comment in object box");
130   anAttr = StudyBuild->FindOrCreateAttribute(box, "AttributeComment");
131   Comment = SALOMEDS::AttributeComment::_narrow(anAttr);
132   Comment->SetValue("this is a box");
133
134   //mesh_box
135   MESSAGE("Add Object  mesh_box under MESH");
136   SALOMEDS::SObject_var mesh_box = StudyBuild->NewObject(Mesh);
137
138   MESSAGE("Add Attribute Name in object mesh_box");
139   anAttr = StudyBuild->FindOrCreateAttribute(mesh_box, "AttributeName");
140   Name = SALOMEDS::AttributeName::_narrow(anAttr);
141   Name->SetValue("mesh_box_0");
142
143   // Test attribute delete
144   //MESSAGE("Delete Attribute Name in object mesh_box");
145   //StudyBuild->RemoveAttribute (mesh_box,SALOMEDS::Name);
146
147   //mesh_box_face
148   MESSAGE("Add Object mesh_box_face under mesh_box");
149   SALOMEDS::SObject_var mesh_box_face = StudyBuild->NewObject(mesh_box);
150
151   MESSAGE("Add Attribute Name in object mesh_box_face");
152   anAttr = StudyBuild->FindOrCreateAttribute(mesh_box_face, "AttributeName");
153   Name = SALOMEDS::AttributeName::_narrow(anAttr);
154   Name->SetValue("mesh_box_face_0");
155
156   MESSAGE("Add Reference mesh_box_face to box");
157   StudyBuild->Addreference (mesh_box_face,box);
158
159   //cylinder
160   MESSAGE("Add Object  cylinder under GEOM");
161   SALOMEDS::SObject_var cylinder = StudyBuild->NewObject(Geom);
162
163   MESSAGE("Add Attribute Name in object cylinder");
164   anAttr = StudyBuild->FindOrCreateAttribute(cylinder, "AttributeName");
165   Name = SALOMEDS::AttributeName::_narrow(anAttr);
166   Name->SetValue("cylinder_0");
167
168   MESSAGE("Add Attribute Persistent Reference in object cylinder");
169   anAttr = StudyBuild->FindOrCreateAttribute(cylinder, "AttributePersistentRef");
170   PRef = SALOMEDS::AttributePersistentRef::_narrow(anAttr);
171   PRef->SetValue("/HDFROOT/GEOM/cylinder");
172
173   // Test Undo
174   MESSAGE("Test Undo");
175
176   StudyBuild->UndoLimit(3);
177   StudyBuild->NewCommand();
178   MESSAGE("Add Object  mesh_cylinder under MESH");
179   SALOMEDS::SObject_var mesh_cylinder = StudyBuild->NewObject(Mesh);
180   MESSAGE("Add Attribute Name in object mesh_cylinder");
181   anAttr = StudyBuild->FindOrCreateAttribute(mesh_cylinder, "AttributeName");
182   Name = SALOMEDS::AttributeName::_narrow(anAttr);
183   Name->SetValue("mesh_cylinder_0");
184   StudyBuild->CommitCommand();
185
186   MESSAGE("Test GetStudy");
187   SALOMEDS::Study_var stu = mesh_cylinder->GetStudy();
188   MESSAGE ("-> Study Name is "<<stu->Name());
189
190   DumpStudy(myStudy);
191
192   StudyBuild->Undo();
193   // Study should have no trace of object mesh_cylinder
194   DumpStudy(myStudy);
195   
196   // Save as
197   myStudy->SaveAs("/home/edeville/Study1.hdf", false);
198
199   // Get Persistent Reference of the study test
200   name = myStudy->GetPersistentReference();
201   MESSAGE("Persitent Reference of the study " << name);
202
203   // Get Transient Reference of the study test
204   name = myStudy->GetTransientReference();
205   MESSAGE("Transient Reference of the study " << name);
206
207   // FindComponent Test
208   SALOMEDS::SComponent_var compo = myStudy->FindComponent("GEOM");
209   // Get ComponentDataType test
210   MESSAGE("Find ComponentDataType of compo");
211   name = compo->ComponentDataType();
212   MESSAGE("-> ComponentDataType is "<<name);
213
214   // FindComponentID Test
215   SALOMEDS::SComponent_var compo1 = myStudy->FindComponentID("0:1:2");
216   // Get ComponentDataType test
217   MESSAGE("Find ComponentDataType of compo1");
218   name = compo1->ComponentDataType();
219   MESSAGE("-> ComponentDataType is "<<name);
220   
221   // FindObject Test
222   SALOMEDS::SObject_var objn = myStudy->FindObject("cylinder_0");
223  // Test FindAttribute function : get AttributeName attribute
224   MESSAGE("Find Name in object objn");
225   if (objn->FindAttribute(anAttr, "AttributeName")) {
226     Name = SALOMEDS::AttributeName::_narrow(anAttr);
227     CORBA::String_var Val = Name->Value();
228     MESSAGE("-> Name is "<<Val);
229   }
230   else {
231     MESSAGE("-> Name is not found");
232   }
233
234   // FindObjectID Test
235   SALOMEDS::SObject_var obj = myStudy->FindObjectID("0:1:2:1:1");
236  // Test FindAttribute function : get AttributeName attribute
237   MESSAGE("Find Name in object obj");
238   if (obj->FindAttribute(anAttr, "AttributeName")) {
239     Name = SALOMEDS::AttributeName::_narrow(anAttr);
240     CORBA::String_var Val = Name->Value();
241     MESSAGE("-> Name is "<<Val);
242   }
243   else {
244     MESSAGE("-> Name is not found");
245   }
246   //DumpStudy(myStudy);
247   }
248   catch(HDFexception)
249     {
250       MESSAGE( "HDFexception ! " )
251     } 
252 }
253
254 ///////////////////////////// MAIN ///////////////////////////////////////
255 //////////////////////////////////////////////////////////////////////////
256 int main(int argc, char** argv)
257
258   try {
259     // Initialise the ORB.
260 #if OMNIORB_VERSION >= 4
261     CORBA::ORB_var orb = CORBA::ORB_init( argc, argv, "omniORB4" ) ;
262 #else
263     CORBA::ORB_var orb = CORBA::ORB_init( argc, argv, "omniORB3" );
264 #endif
265     
266     // Obtain a reference to the root POA.
267     CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
268     PortableServer::POA_var poa = PortableServer::POA::_narrow(obj);
269
270     SALOME_NamingService * salomens = new SALOME_NamingService(orb);
271
272     MESSAGE("Find Study ");
273     CORBA::Object_ptr obj2 = salomens->Resolve("Study");
274     SALOMEDS::Study_var myStudy = SALOMEDS::Study::_narrow(obj2);
275
276     // Obtain a POAManager, and tell the POA to start accepting
277     // requests on its objects.
278     PortableServer::POAManager_var pman = poa->the_POAManager();
279     pman->activate();
280
281     // Test basic services
282     Test(myStudy);
283
284     delete salomens;
285     orb->run();
286     orb->destroy();
287   }
288   catch(CORBA::SystemException&) {
289     MESSAGE("Caught CORBA::SystemException." )
290   }
291   catch(CORBA::Exception&) {
292     MESSAGE( "Caught CORBA::Exception." )
293   }
294   catch(omniORB::fatalException& fe) {
295     MESSAGE( "Caught omniORB::fatalException:" )
296     MESSAGE( "  file: " << fe.file() )
297     MESSAGE( "  line: " << fe.line() )
298     MESSAGE( "  mesg: " << fe.errmsg() )
299   }
300   catch(...) {
301     MESSAGE( "Caught unknown exception." )
302   }
303   return 0;
304 }