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