Back to index

salome-kernel  6.5.0
SALOMEDS_Client.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  SALOME SALOMEDS : data structure of SALOME and sources of Salome data server 
00024 //  File   : SALOMEDS_Client.cxx
00025 //  Author : Yves FRICAUD
00026 //  Module : SALOME
00027 //  $Header: /home/server/cvs/KERNEL/KERNEL_SRC/src/SALOMEDS/SALOMEDS_Client.cxx,v 1.9.2.1.10.2.12.1 2012-04-12 14:05:26 vsr Exp $
00028 //
00029 #include <SALOMEconfig.h>
00030 #include CORBA_SERVER_HEADER(SALOMEDS)
00031 #include "SALOMEDS_StudyManager_i.hxx"
00032 #include "SALOMEDS_AttributeName_i.hxx"
00033 #include "utilities.h"
00034 #include "HDFOI.hxx"
00035 
00036 //============================================================================
00040 //============================================================================
00041 static void DumpComponent(SALOMEDS::Study_ptr Study,SALOMEDS::SObject_ptr SO, int offset) {
00042   SALOMEDS::SObject_var RefSO;
00043   SALOMEDS::ChildIterator_var it = Study->NewChildIterator(SO);
00044   for (; it->More();it->Next()){
00045     SALOMEDS::SObject_var CSO= it->Value();
00046     SALOMEDS::GenericAttribute_var anAttr;
00047     if (CSO->FindAttribute(anAttr,"AttributeName")) 
00048     {
00049       SALOMEDS::AttributeName_var Name = SALOMEDS::AttributeName::_narrow(anAttr);
00050       CORBA::String_var Val = Name->Value();
00051       for (int i = 1; i <= offset ; i++) 
00052         MESSAGE("--");
00053       MESSAGE(">"<<CSO->GetID()<<Val);
00054     }
00055     if (CSO->ReferencedObject(RefSO)) {
00056       for (int i = 1; i <= offset ; i++) 
00057         MESSAGE(" ");
00058       MESSAGE("*Reference"<<RefSO->GetID());
00059     }
00060     DumpComponent(Study,CSO,offset+2);
00061   }
00062 }
00063 
00064 //============================================================================
00068 //============================================================================
00069 static void DumpStudy (SALOMEDS::Study_ptr Study) {
00070   MESSAGE("Explore Study and Write name of each object if it exists");
00071   
00072   char* name;
00073   SALOMEDS::SComponentIterator_var itcomp = Study->NewComponentIterator();
00074   int offset = 1;
00075   for (; itcomp->More(); itcomp->Next()) {
00076     SALOMEDS::SComponent_var SC = itcomp->Value();
00077     name = SC->ComponentDataType();
00078     MESSAGE("-> ComponentDataType is "<<name);  
00079     DumpComponent(Study,SC,offset);
00080   }
00081 }
00082 
00083 //============================================================================
00087 //============================================================================
00088 static void Test(SALOMEDS::StudyManager_ptr myStudyMgr )
00089 {
00090   try {
00091   char* name;
00092   MESSAGE("Create New Study Study1");
00093   SALOMEDS::Study_var myStudy = myStudyMgr->NewStudy("Study1");
00094  
00095   MESSAGE("Create Builder ");
00096   SALOMEDS::StudyBuilder_var StudyBuild = myStudy->NewBuilder();
00097 
00098 
00099   // Create new components
00100   SALOMEDS::GenericAttribute_var anAttr;
00101   SALOMEDS::AttributeName_var Name;
00102   SALOMEDS::AttributeComment_var Comment;
00103   SALOMEDS::AttributePersistentRef_var PRef;
00104 
00105   // GEOM
00106   MESSAGE("Add Component GEOM"); 
00107   SALOMEDS::SComponent_var Geom = StudyBuild->NewComponent("GEOM");
00108   MESSAGE("Add attribute name GEOM to component Geom");
00109   anAttr = StudyBuild->FindOrCreateAttribute(Geom, "AttributeName");
00110   Name = SALOMEDS::AttributeName::_narrow(anAttr);
00111   Name->SetValue("Geometry");
00112 
00113   // MESH
00114   MESSAGE("Add Component MESH");
00115   SALOMEDS::SComponent_var Mesh = StudyBuild->NewComponent("MESH");
00116   anAttr = StudyBuild->FindOrCreateAttribute(Mesh, "AttributeName");
00117   Name = SALOMEDS::AttributeName::_narrow(anAttr);
00118   Name->SetValue("Mesh");
00119 
00120   //Create objects
00121 
00122   //Value
00123   //box
00124   MESSAGE("Add Object  box under GEOM");
00125   SALOMEDS::SObject_var box = StudyBuild->NewObject(Geom);
00126 
00127   MESSAGE("Add Attribute Name in object box");
00128   anAttr = StudyBuild->FindOrCreateAttribute(box, "AttributeName");
00129   Name = SALOMEDS::AttributeName::_narrow(anAttr);
00130   Name->SetValue("box_0");
00131 
00132   MESSAGE("Add Attribute Comment in object box");
00133   anAttr = StudyBuild->FindOrCreateAttribute(box, "AttributeComment");
00134   Comment = SALOMEDS::AttributeComment::_narrow(anAttr);
00135   Comment->SetValue("this is a box");
00136 
00137   //mesh_box
00138   MESSAGE("Add Object  mesh_box under MESH");
00139   SALOMEDS::SObject_var mesh_box = StudyBuild->NewObject(Mesh);
00140 
00141   MESSAGE("Add Attribute Name in object mesh_box");
00142   anAttr = StudyBuild->FindOrCreateAttribute(mesh_box, "AttributeName");
00143   Name = SALOMEDS::AttributeName::_narrow(anAttr);
00144   Name->SetValue("mesh_box_0");
00145 
00146   // Test attribute delete
00147   //MESSAGE("Delete Attribute Name in object mesh_box");
00148   //StudyBuild->RemoveAttribute (mesh_box,SALOMEDS::Name);
00149 
00150   //mesh_box_face
00151   MESSAGE("Add Object mesh_box_face under mesh_box");
00152   SALOMEDS::SObject_var mesh_box_face = StudyBuild->NewObject(mesh_box);
00153 
00154   MESSAGE("Add Attribute Name in object mesh_box_face");
00155   anAttr = StudyBuild->FindOrCreateAttribute(mesh_box_face, "AttributeName");
00156   Name = SALOMEDS::AttributeName::_narrow(anAttr);
00157   Name->SetValue("mesh_box_face_0");
00158 
00159   MESSAGE("Add Reference mesh_box_face to box");
00160   StudyBuild->Addreference (mesh_box_face,box);
00161 
00162   //cylinder
00163   MESSAGE("Add Object  cylinder under GEOM");
00164   SALOMEDS::SObject_var cylinder = StudyBuild->NewObject(Geom);
00165 
00166   MESSAGE("Add Attribute Name in object cylinder");
00167   anAttr = StudyBuild->FindOrCreateAttribute(cylinder, "AttributeName");
00168   Name = SALOMEDS::AttributeName::_narrow(anAttr);
00169   Name->SetValue("cylinder_0");
00170 
00171   MESSAGE("Add Attribute Persistent Reference in object cylinder");
00172   anAttr = StudyBuild->FindOrCreateAttribute(cylinder, "AttributePersistentRef");
00173   PRef = SALOMEDS::AttributePersistentRef::_narrow(anAttr);
00174   PRef->SetValue("/HDFROOT/GEOM/cylinder");
00175 
00176   // Test Undo
00177   MESSAGE("Test Undo");
00178 
00179   StudyBuild->UndoLimit(3);
00180   StudyBuild->NewCommand();
00181   MESSAGE("Add Object  mesh_cylinder under MESH");
00182   SALOMEDS::SObject_var mesh_cylinder = StudyBuild->NewObject(Mesh);
00183   MESSAGE("Add Attribute Name in object mesh_cylinder");
00184   anAttr = StudyBuild->FindOrCreateAttribute(mesh_cylinder, "AttributeName");
00185   Name = SALOMEDS::AttributeName::_narrow(anAttr);
00186   Name->SetValue("mesh_cylinder_0");
00187   StudyBuild->CommitCommand();
00188 
00189   MESSAGE("Test GetStudy");
00190   SALOMEDS::Study_var stu = mesh_cylinder->GetStudy();
00191   MESSAGE ("-> Study Name is "<<stu->Name());
00192 
00193   DumpStudy(myStudy);
00194 
00195   StudyBuild->Undo();
00196   // Study should have no trace of object mesh_cylinder
00197   DumpStudy(myStudy);
00198 
00199  
00200   //myStudyMgr->Open ((char*)name);
00201   //MESSAGE("Name " << name);
00202 
00203   // GetOpenStudies
00204   MESSAGE("GetOpenStudies list");
00205   SALOMEDS::ListOfOpenStudies_var _list_open_studies =  myStudyMgr->GetOpenStudies();
00206 
00207   for (unsigned int ind = 0; ind < _list_open_studies->length();ind++)
00208     {
00209       MESSAGE("Open studies list : " << _list_open_studies[ind]);  
00210     }
00211 
00212 
00213   // GetStudyByName
00214   SALOMEDS::Study_var myStudy1 =myStudyMgr->GetStudyByName(_list_open_studies[0]);
00215   MESSAGE("GetStudyByName done");
00216   
00217   // Save as
00218   myStudyMgr->SaveAs("/home/edeville/Study1.hdf",myStudy1, false);
00219 
00220   // Get Persistent Reference of the study test
00221   name = myStudy1->GetPersistentReference();
00222   MESSAGE("Persitent Reference of the study " << name);
00223 
00224   // Get Transient Reference of the study test
00225   name = myStudy1->GetTransientReference();
00226   MESSAGE("Transient Reference of the study " << name);
00227 
00228   // FindComponent Test
00229   SALOMEDS::SComponent_var compo = myStudy1->FindComponent("GEOM");
00230   // Get ComponentDataType test
00231   MESSAGE("Find ComponentDataType of compo");
00232   name = compo->ComponentDataType();
00233   MESSAGE("-> ComponentDataType is "<<name);  
00234 
00235   
00236   // FindComponentID Test
00237   SALOMEDS::SComponent_var compo1 = myStudy1->FindComponentID("0:1:2");
00238   // Get ComponentDataType test
00239   MESSAGE("Find ComponentDataType of compo1");
00240   name = compo1->ComponentDataType();
00241   MESSAGE("-> ComponentDataType is "<<name);  
00242 
00243   // FindObject Test
00244   SALOMEDS::SObject_var objn = myStudy1->FindObject("cylinder_0");
00245  // Test FindAttribute function : get AttributeName attribute
00246   MESSAGE("Find Name in object objn");
00247   if (objn->FindAttribute(anAttr, "AttributeName")) {
00248     Name = SALOMEDS::AttributeName::_narrow(anAttr);
00249     CORBA::String_var Val = Name->Value();
00250     MESSAGE("-> Name is "<<Val);
00251   }
00252   else {
00253     MESSAGE("-> Name is not found");
00254   }
00255 
00256   // FindObjectID Test
00257   SALOMEDS::SObject_var obj = myStudy1->FindObjectID("0:1:2:1:1");
00258  // Test FindAttribute function : get AttributeName attribute
00259   MESSAGE("Find Name in object obj");
00260   if (obj->FindAttribute(anAttr, "AttributeName")) {
00261     Name = SALOMEDS::AttributeName::_narrow(anAttr);
00262     CORBA::String_var Val = Name->Value();
00263     MESSAGE("-> Name is "<<Val);
00264   }
00265   else {
00266     MESSAGE("-> Name is not found");
00267   }
00268   //DumpStudy(myStudy1);
00269   }
00270   catch(HDFexception)
00271     {
00272       MESSAGE( "HDFexception ! " )
00273     } 
00274 }
00275 
00278 int main(int argc, char** argv)
00279 { 
00280   try {
00281     // Initialise the ORB.
00282 #if OMNIORB_VERSION >= 4
00283     CORBA::ORB_var orb = CORBA::ORB_init( argc, argv, "omniORB4" ) ;
00284 #else
00285     CORBA::ORB_var orb = CORBA::ORB_init( argc, argv, "omniORB3" );
00286 #endif
00287     
00288     // Obtain a reference to the root POA.
00289     CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
00290     PortableServer::POA_var poa = PortableServer::POA::_narrow(obj);
00291 
00292     SALOME_NamingService * salomens = new SALOME_NamingService(orb);
00293 
00294     MESSAGE("Find StudyManager ");
00295     CORBA::Object_ptr obj2 = salomens->Resolve("myStudyManager");
00296     SALOMEDS::StudyManager_var myStudyMgr = SALOMEDS::StudyManager::_narrow(obj2);
00297 
00298     // Obtain a POAManager, and tell the POA to start accepting
00299     // requests on its objects.
00300     PortableServer::POAManager_var pman = poa->the_POAManager();
00301     pman->activate();
00302 
00303     // Test basic services
00304     Test(myStudyMgr);
00305 
00306     orb->run();
00307     orb->destroy();
00308   }
00309   catch(CORBA::SystemException&) {
00310     MESSAGE("Caught CORBA::SystemException." )
00311   }
00312   catch(CORBA::Exception&) {
00313     MESSAGE( "Caught CORBA::Exception." )
00314   }
00315   catch(omniORB::fatalException& fe) {
00316     MESSAGE( "Caught omniORB::fatalException:" )
00317     MESSAGE( "  file: " << fe.file() )
00318     MESSAGE( "  line: " << fe.line() )
00319     MESSAGE( "  mesg: " << fe.errmsg() )
00320   }
00321   catch(...) {
00322     MESSAGE( "Caught unknown exception." )
00323   }
00324   return 0;
00325 }