Salome HOME
6cb1f2e42d2a6146015d917bf53a2a6996aa726b
[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   SALOMEDS::SComponentIterator_var itcomp = KERNEL::getStudyServant()->NewComponentIterator();
75   int offset = 1;
76   for (; itcomp->More(); itcomp->Next()) {
77     SALOMEDS::SComponent_var SC = itcomp->Value();
78     name = SC->ComponentDataType();
79     MESSAGE("-> ComponentDataType is "<<name);  
80     DumpComponent(SC,offset);
81   }
82 }
83
84 //============================================================================
85 /*! Function :
86  *  Purpose  : 
87  */
88 //============================================================================
89 static void Test()
90 {
91   try {
92   char* name;
93
94   SALOMEDS::Study_var myStudy = KERNEL::getStudyServant();
95
96   MESSAGE("Create Builder ");
97   SALOMEDS::StudyBuilder_var StudyBuild = myStudy->NewBuilder();
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   DumpStudy();
190
191   StudyBuild->Undo();
192   // Study should have no trace of object mesh_cylinder
193   DumpStudy();
194   
195   // Save as
196   myStudy->SaveAs(Kernel_Utils::decode("/home/edeville/Study1.hdf"), false, false);
197
198   // Get Persistent Reference of the study test
199   name = myStudy->GetPersistentReference();
200   MESSAGE("Persitent Reference of the study " << name);
201
202   // FindComponent Test
203   SALOMEDS::SComponent_var compo = myStudy->FindComponent("GEOM");
204   // Get ComponentDataType test
205   MESSAGE("Find ComponentDataType of compo");
206   name = compo->ComponentDataType();
207   MESSAGE("-> ComponentDataType is "<<name);
208
209   // FindComponentID Test
210   SALOMEDS::SComponent_var compo1 = myStudy->FindComponentID("0:1:2");
211   // Get ComponentDataType test
212   MESSAGE("Find ComponentDataType of compo1");
213   name = compo1->ComponentDataType();
214   MESSAGE("-> ComponentDataType is "<<name);
215   
216   // FindObject Test
217   SALOMEDS::SObject_var objn = myStudy->FindObject("cylinder_0");
218  // Test FindAttribute function : get AttributeName attribute
219   MESSAGE("Find Name in object objn");
220   if (objn->FindAttribute(anAttr, "AttributeName")) {
221     Name = SALOMEDS::AttributeName::_narrow(anAttr);
222     CORBA::String_var Val = Name->Value();
223     MESSAGE("-> Name is "<<Val);
224   }
225   else {
226     MESSAGE("-> Name is not found");
227   }
228
229   // FindObjectID Test
230   SALOMEDS::SObject_var obj = myStudy->FindObjectID("0:1:2:1:1");
231  // Test FindAttribute function : get AttributeName attribute
232   MESSAGE("Find Name in object obj");
233   if (obj->FindAttribute(anAttr, "AttributeName")) {
234     Name = SALOMEDS::AttributeName::_narrow(anAttr);
235     CORBA::String_var Val = Name->Value();
236     MESSAGE("-> Name is "<<Val);
237   }
238   else {
239     MESSAGE("-> Name is not found");
240   }
241   //DumpStudy();
242   }
243   catch(HDFexception)
244     {
245       MESSAGE( "HDFexception ! " )
246     } 
247 }
248
249 ///////////////////////////// MAIN ///////////////////////////////////////
250 //////////////////////////////////////////////////////////////////////////
251 int main(int argc, char** argv)
252
253   try {
254     // Initialise the ORB.
255 #if OMNIORB_VERSION >= 4
256     CORBA::ORB_var orb = CORBA::ORB_init( argc, argv, "omniORB4" ) ;
257 #else
258     CORBA::ORB_var orb = CORBA::ORB_init( argc, argv, "omniORB3" );
259 #endif
260     
261     // Obtain a reference to the root POA.
262     CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
263     PortableServer::POA_var poa = PortableServer::POA::_narrow(obj);
264
265     // Obtain a POAManager, and tell the POA to start accepting
266     // requests on its objects.
267     PortableServer::POAManager_var pman = poa->the_POAManager();
268     pman->activate();
269
270     // Test basic services
271     Test();
272
273     orb->run();
274     orb->destroy();
275   }
276   catch(CORBA::SystemException&) {
277     MESSAGE("Caught CORBA::SystemException." )
278   }
279   catch(CORBA::Exception&) {
280     MESSAGE( "Caught CORBA::Exception." )
281   }
282   catch(omniORB::fatalException& fe) {
283     MESSAGE( "Caught omniORB::fatalException:" )
284     MESSAGE( "  file: " << fe.file() )
285     MESSAGE( "  line: " << fe.line() )
286     MESSAGE( "  mesg: " << fe.errmsg() )
287   }
288   catch(...) {
289     MESSAGE( "Caught unknown exception." )
290   }
291   return 0;
292 }