Salome HOME
Correct boost headers variable
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_Client.cxx
1 // Copyright (C) 2007-2020  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_KernelServices.hxx"
33 #include "Basics_Utils.hxx"
34 #include "utilities.h"
35 #include "HDFOI.hxx"
36
37 //============================================================================
38 /*! Function :
39  *  Purpose  : 
40  */
41 //============================================================================
42 static void DumpComponent(SALOMEDS::SObject_ptr SO, int offset) {
43   SALOMEDS::SObject_var RefSO;
44   SALOMEDS::ChildIterator_var it = KERNEL::getStudyServant()->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 (int i = 1; i <= offset ; i++) 
53         MESSAGE("--");
54       MESSAGE(">"<<CSO->GetID()<<Val);
55     }
56     if (CSO->ReferencedObject(RefSO)) {
57       for (int i = 1; i <= offset ; i++) 
58         MESSAGE(" ");
59       MESSAGE("*Reference"<<RefSO->GetID());
60     }
61     DumpComponent(CSO,offset+2);
62   }
63 }
64
65 //============================================================================
66 /*! Function :
67  *  Purpose  : 
68  */
69 //============================================================================
70 static void DumpStudy() {
71   MESSAGE("Explore Study and Write name of each object if it exists");
72   
73   char* name;
74   SALOME_UNUSED(name); // unused in release mode
75   SALOMEDS::SComponentIterator_var itcomp = KERNEL::getStudyServant()->NewComponentIterator();
76   int 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(SC,offset);
82   }
83 }
84
85 //============================================================================
86 /*! Function :
87  *  Purpose  : 
88  */
89 //============================================================================
90 static void Test()
91 {
92   try {
93   char* name;
94   SALOME_UNUSED(name); // unused in release mode
95
96   SALOMEDS::Study_var myStudy = KERNEL::getStudyServant();
97
98   MESSAGE("Create Builder ");
99   SALOMEDS::StudyBuilder_var StudyBuild = myStudy->NewBuilder();
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   DumpStudy();
192
193   StudyBuild->Undo();
194   // Study should have no trace of object mesh_cylinder
195   DumpStudy();
196   
197   // Save as
198   myStudy->SaveAs(Kernel_Utils::decode("/home/edeville/Study1.hdf"), false, false);
199
200   // Get Persistent Reference of the study test
201   name = myStudy->GetPersistentReference();
202   MESSAGE("Persitent Reference of the study " << name);
203
204   // FindComponent Test
205   SALOMEDS::SComponent_var compo = myStudy->FindComponent("GEOM");
206   // Get ComponentDataType test
207   MESSAGE("Find ComponentDataType of compo");
208   name = compo->ComponentDataType();
209   MESSAGE("-> ComponentDataType is "<<name);
210
211   // FindComponentID Test
212   SALOMEDS::SComponent_var compo1 = myStudy->FindComponentID("0:1:2");
213   // Get ComponentDataType test
214   MESSAGE("Find ComponentDataType of compo1");
215   name = compo1->ComponentDataType();
216   MESSAGE("-> ComponentDataType is "<<name);
217   
218   // FindObject Test
219   SALOMEDS::SObject_var objn = myStudy->FindObject("cylinder_0");
220  // Test FindAttribute function : get AttributeName attribute
221   MESSAGE("Find Name in object objn");
222   if (objn->FindAttribute(anAttr, "AttributeName")) {
223     Name = SALOMEDS::AttributeName::_narrow(anAttr);
224     CORBA::String_var Val = Name->Value();
225     MESSAGE("-> Name is "<<Val);
226   }
227   else {
228     MESSAGE("-> Name is not found");
229   }
230
231   // FindObjectID Test
232   SALOMEDS::SObject_var obj = myStudy->FindObjectID("0:1:2:1:1");
233  // Test FindAttribute function : get AttributeName attribute
234   MESSAGE("Find Name in object obj");
235   if (obj->FindAttribute(anAttr, "AttributeName")) {
236     Name = SALOMEDS::AttributeName::_narrow(anAttr);
237     CORBA::String_var Val = Name->Value();
238     MESSAGE("-> Name is "<<Val);
239   }
240   else {
241     MESSAGE("-> Name is not found");
242   }
243   //DumpStudy();
244   }
245   catch(HDFexception)
246     {
247       MESSAGE( "HDFexception ! " )
248     } 
249 }
250
251 ///////////////////////////// MAIN ///////////////////////////////////////
252 //////////////////////////////////////////////////////////////////////////
253 int main(int argc, char** argv)
254
255   try {
256     // Initialise the ORB.
257 #if OMNIORB_VERSION >= 4
258     CORBA::ORB_var orb = CORBA::ORB_init( argc, argv, "omniORB4" ) ;
259 #else
260     CORBA::ORB_var orb = CORBA::ORB_init( argc, argv, "omniORB3" );
261 #endif
262     
263     // Obtain a reference to the root POA.
264     CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
265     PortableServer::POA_var poa = PortableServer::POA::_narrow(obj);
266
267     // Obtain a POAManager, and tell the POA to start accepting
268     // requests on its objects.
269     PortableServer::POAManager_var pman = poa->the_POAManager();
270     pman->activate();
271
272     // Test basic services
273     Test();
274
275     orb->run();
276     orb->destroy();
277   }
278   catch(CORBA::SystemException&) {
279     MESSAGE("Caught CORBA::SystemException." )
280   }
281   catch(CORBA::Exception&) {
282     MESSAGE( "Caught CORBA::Exception." )
283   }
284   catch(omniORB::fatalException& fe) { //!< TODO: unused variable
285     MESSAGE( "Caught omniORB::fatalException:" )
286     MESSAGE( "  file: " << fe.file() )
287     MESSAGE( "  line: " << fe.line() )
288     MESSAGE( "  mesg: " << fe.errmsg() )
289   }
290   catch(...) {
291     MESSAGE( "Caught unknown exception." )
292   }
293   return 0;
294 }