Salome HOME
NRI : Remove dependence with VISU.
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_Client.cxx
1 using namespace std;
2 //  File      : SALOMEDS_Client.cxx
3 //  Created   : Wed Nov 28 16:20:25 2001
4 //  Author    : Yves FRICAUD
5
6 //  Project   : SALOME
7 //  Module    : SALOMEDS
8 //  Copyright : Open CASCADE 2001
9 //  $Header$
10
11 #include <SALOMEconfig.h>
12 #include CORBA_SERVER_HEADER(SALOMEDS)
13 #include "SALOMEDS_StudyManager_i.hxx"
14 #include "utilities.h"
15 #include "SALOMEDS_AttributeName_i.hxx"
16
17 //============================================================================
18 /*! Function :
19  *  Purpose  : 
20  */
21 //============================================================================
22 static void DumpComponent(SALOMEDS::Study_ptr Study,SALOMEDS::SObject_ptr SO,Standard_Integer offset) {
23   SALOMEDS::SObject_var RefSO;
24   SALOMEDS::ChildIterator_var it = Study->NewChildIterator(SO);
25   for (; it->More();it->Next()){
26     SALOMEDS::SObject_var CSO= it->Value();
27     SALOMEDS::GenericAttribute_var anAttr;
28     if (CSO->FindAttribute(anAttr,"AttributeName")) 
29     {
30       SALOMEDS::AttributeName_var Name = SALOMEDS::AttributeName::_narrow(anAttr);
31       CORBA::String_var Val = Name->Value();
32       for (Standard_Integer i = 1; i <= offset ; i++) 
33         MESSAGE("--");
34       MESSAGE(">"<<CSO->GetID()<<Val);
35     }
36     if (CSO->ReferencedObject(RefSO)) {
37       for (Standard_Integer i = 1; i <= offset ; i++) 
38         MESSAGE(" ");
39       MESSAGE("*Reference"<<RefSO->GetID());
40     }
41     DumpComponent(Study,CSO,offset+2);
42   }
43 }
44
45 //============================================================================
46 /*! Function :
47  *  Purpose  : 
48  */
49 //============================================================================
50 static void DumpStudy (SALOMEDS::Study_ptr Study) {
51   MESSAGE("Explore Study and Write name of each object if it exists");
52   
53   Standard_CString name;
54   SALOMEDS::SComponentIterator_var itcomp = Study->NewComponentIterator();
55   Standard_Integer offset = 1;
56   for (; itcomp->More(); itcomp->Next()) {
57     SALOMEDS::SComponent_var SC = itcomp->Value();
58     name = SC->ComponentDataType();
59     MESSAGE("-> ComponentDataType is "<<name);  
60     DumpComponent(Study,SC,offset);
61   }
62 }
63
64 //============================================================================
65 /*! Function :
66  *  Purpose  : 
67  */
68 //============================================================================
69 static void Test(SALOMEDS::StudyManager_ptr myStudyMgr )
70 {
71   try {
72   Standard_CString name;
73   MESSAGE("Create New Study Study1");
74   SALOMEDS::Study_var myStudy = myStudyMgr->NewStudy("Study1");
75  
76   MESSAGE("Create Builder ");
77   SALOMEDS::StudyBuilder_var StudyBuild = myStudy->NewBuilder();
78
79
80   // Create new components
81   SALOMEDS::GenericAttribute_var anAttr;
82   SALOMEDS::AttributeName_var Name;
83   SALOMEDS::AttributeComment_var Comment;
84   SALOMEDS::AttributePersistentRef_var PRef;
85
86   // GEOM
87   MESSAGE("Add Component GEOM"); 
88   SALOMEDS::SComponent_var Geom = StudyBuild->NewComponent("GEOM");
89   MESSAGE("Add attribute name GEOM to component Geom");
90   anAttr = StudyBuild->FindOrCreateAttribute(Geom, "AttributeName");
91   Name = SALOMEDS::AttributeName::_narrow(anAttr);
92   Name->SetValue("Geometry");
93
94   // MESH
95   MESSAGE("Add Component MESH");
96   SALOMEDS::SComponent_var Mesh = StudyBuild->NewComponent("MESH");
97   anAttr = StudyBuild->FindOrCreateAttribute(Mesh, "AttributeName");
98   Name = SALOMEDS::AttributeName::_narrow(anAttr);
99   Name->SetValue("Mesh");
100
101   //Create objects
102
103   //Value
104   //box
105   MESSAGE("Add Object  box under GEOM");
106   SALOMEDS::SObject_var box = StudyBuild->NewObject(Geom);
107
108   MESSAGE("Add Attribute Name in object box");
109   anAttr = StudyBuild->FindOrCreateAttribute(box, "AttributeName");
110   Name = SALOMEDS::AttributeName::_narrow(anAttr);
111   Name->SetValue("box_0");
112
113   MESSAGE("Add Attribute Comment in object box");
114   anAttr = StudyBuild->FindOrCreateAttribute(box, "AttributeComment");
115   Comment = SALOMEDS::AttributeComment::_narrow(anAttr);
116   Comment->SetValue("this is a box");
117
118   //mesh_box
119   MESSAGE("Add Object  mesh_box under MESH");
120   SALOMEDS::SObject_var mesh_box = StudyBuild->NewObject(Mesh);
121
122   MESSAGE("Add Attribute Name in object mesh_box");
123   anAttr = StudyBuild->FindOrCreateAttribute(mesh_box, "AttributeName");
124   Name = SALOMEDS::AttributeName::_narrow(anAttr);
125   Name->SetValue("mesh_box_0");
126
127   // Test attribute delete
128   //MESSAGE("Delete Attribute Name in object mesh_box");
129   //StudyBuild->RemoveAttribute (mesh_box,SALOMEDS::Name);
130
131   //mesh_box_face
132   MESSAGE("Add Object mesh_box_face under mesh_box");
133   SALOMEDS::SObject_var mesh_box_face = StudyBuild->NewObject(mesh_box);
134
135   MESSAGE("Add Attribute Name in object mesh_box_face");
136   anAttr = StudyBuild->FindOrCreateAttribute(mesh_box_face, "AttributeName");
137   Name = SALOMEDS::AttributeName::_narrow(anAttr);
138   Name->SetValue("mesh_box_face_0");
139
140   MESSAGE("Add Reference mesh_box_face to box");
141   StudyBuild->Addreference (mesh_box_face,box);
142
143   //cylinder
144   MESSAGE("Add Object  cylinder under GEOM");
145   SALOMEDS::SObject_var cylinder = StudyBuild->NewObject(Geom);
146
147   MESSAGE("Add Attribute Name in object cylinder");
148   anAttr = StudyBuild->FindOrCreateAttribute(cylinder, "AttributeName");
149   Name = SALOMEDS::AttributeName::_narrow(anAttr);
150   Name->SetValue("cylinder_0");
151
152   MESSAGE("Add Attribute Persistent Reference in object cylinder");
153   anAttr = StudyBuild->FindOrCreateAttribute(cylinder, "AttributePersistentRef");
154   PRef = SALOMEDS::AttributePersistentRef::_narrow(anAttr);
155   PRef->SetValue("/HDFROOT/GEOM/cylinder");
156
157   // Test Undo
158   MESSAGE("Test Undo");
159
160   StudyBuild->UndoLimit(3);
161   StudyBuild->NewCommand();
162   MESSAGE("Add Object  mesh_cylinder under MESH");
163   SALOMEDS::SObject_var mesh_cylinder = StudyBuild->NewObject(Mesh);
164   MESSAGE("Add Attribute Name in object mesh_cylinder");
165   anAttr = StudyBuild->FindOrCreateAttribute(mesh_cylinder, "AttributeName");
166   Name = SALOMEDS::AttributeName::_narrow(anAttr);
167   Name->SetValue("mesh_cylinder_0");
168   StudyBuild->CommitCommand();
169
170   MESSAGE("Test GetStudy");
171   SALOMEDS::Study_var stu = mesh_cylinder->GetStudy();
172   MESSAGE ("-> Study Name is "<<stu->Name());
173
174   DumpStudy(myStudy);
175
176   StudyBuild->Undo();
177   // Study should have no trace of object mesh_cylinder
178   DumpStudy(myStudy);
179
180  
181   //myStudyMgr->Open ((char*)name);
182   //MESSAGE("Name " << name);
183
184   // GetOpenStudies
185   MESSAGE("GetOpenStudies list");
186   SALOMEDS::ListOfOpenStudies_var _list_open_studies =  myStudyMgr->GetOpenStudies();
187
188   for (unsigned int ind = 0; ind < _list_open_studies->length();ind++)
189     {
190       MESSAGE("Open studies list : " << _list_open_studies[ind]);  
191     }
192
193
194   // GetStudyByName
195   SALOMEDS::Study_var myStudy1 =myStudyMgr->GetStudyByName(_list_open_studies[0]);
196   MESSAGE("GetStudyByName done");
197   
198   // Save as
199   myStudyMgr->SaveAs("/home/edeville/Study1.hdf",myStudy1, false);
200
201   // Get Persistent Reference of the study test
202   name = myStudy1->GetPersistentReference();
203   MESSAGE("Persitent Reference of the study " << name);
204
205   // Get Transient Reference of the study test
206   name = myStudy1->GetTransientReference();
207   MESSAGE("Transient Reference of the study " << name);
208
209   // FindComponent Test
210   SALOMEDS::SComponent_var compo = myStudy1->FindComponent("GEOM");
211   // Get ComponentDataType test
212   MESSAGE("Find ComponentDataType of compo");
213   name = compo->ComponentDataType();
214   MESSAGE("-> ComponentDataType is "<<name);  
215
216   
217   // FindComponentID Test
218   SALOMEDS::SComponent_var compo1 = myStudy1->FindComponentID("0:1:2");
219   // Get ComponentDataType test
220   MESSAGE("Find ComponentDataType of compo1");
221   name = compo1->ComponentDataType();
222   MESSAGE("-> ComponentDataType is "<<name);  
223
224   // FindObject Test
225   SALOMEDS::SObject_var objn = myStudy1->FindObject("cylinder_0");
226  // Test FindAttribute function : get AttributeName attribute
227   MESSAGE("Find Name in object objn");
228   if (objn->FindAttribute(anAttr, "AttributeName")) {
229     Name = SALOMEDS::AttributeName::_narrow(anAttr);
230     CORBA::String_var Val = Name->Value();
231     MESSAGE("-> Name is "<<Val);
232   }
233   else {
234     MESSAGE("-> Name is not found");
235   }
236
237   // FindObjectID Test
238   SALOMEDS::SObject_var obj = myStudy1->FindObjectID("0:1:2:1:1");
239  // Test FindAttribute function : get AttributeName attribute
240   MESSAGE("Find Name in object obj");
241   if (obj->FindAttribute(anAttr, "AttributeName")) {
242     Name = SALOMEDS::AttributeName::_narrow(anAttr);
243     CORBA::String_var Val = Name->Value();
244     MESSAGE("-> Name is "<<Val);
245   }
246   else {
247     MESSAGE("-> Name is not found");
248   }
249   //DumpStudy(myStudy1);
250   }
251   catch(HDFexception)
252     {
253       MESSAGE( "HDFexception ! " )
254     } 
255 }
256
257 ///////////////////////////// MAIN ///////////////////////////////////////
258 //////////////////////////////////////////////////////////////////////////
259 int main(int argc, char** argv)
260
261   try {
262     // Initialise the ORB.
263     CORBA::ORB_var orb = CORBA::ORB_init(argc, argv, "omniORB3");
264
265
266     
267     // Obtain a reference to the root POA.
268     CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
269     PortableServer::POA_var poa = PortableServer::POA::_narrow(obj);
270
271     SALOME_NamingService * salomens = new SALOME_NamingService(orb);
272
273     MESSAGE("Find StudyManager ");
274     CORBA::Object_ptr obj2 = salomens->Resolve("myStudyManager");
275     SALOMEDS::StudyManager_var myStudyMgr = SALOMEDS::StudyManager::_narrow(obj2);
276
277     // Obtain a POAManager, and tell the POA to start accepting
278     // requests on its objects.
279     PortableServer::POAManager_var pman = poa->the_POAManager();
280     pman->activate();
281
282     // Test basic services
283     Test(myStudyMgr);
284
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 }
305