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