Back to index

salome-smesh  6.5.0
StdMeshers_ObjRefUlils.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 //  SMESH SMESH : implementaion of SMESH idl descriptions
00024 // File      : StdMeshers_ObjRefUlils.cxx
00025 // Created   : Wed Oct 18 15:38:22 2006
00026 // Author    : Edward AGAPOV (eap)
00027 //
00028 #include "StdMeshers_ObjRefUlils.hxx"
00029 
00030 #include <TopoDS_Shape.hxx>
00031 
00032 using namespace std;
00033 
00034 //=======================================================================
00035 //function : GeomObjectToEntry
00036 //purpose  : Return study entry of GEOM Object
00037 //=======================================================================
00038 
00039 std::string StdMeshers_ObjRefUlils::GeomObjectToEntry(GEOM::GEOM_Object_ptr&  theGeomObject)
00040 {
00041   if ( CORBA::is_nil( theGeomObject ))
00042     return "NULL_OBJECT";
00043 
00044   CORBA::String_var entry = theGeomObject->GetStudyEntry();
00045   return entry.in();
00046 }
00047 
00048 //=======================================================================
00049 //function : EntryOrShapeToGeomObject
00050 //purpose  :  Return GEOM Object by its sytudy entry or TopoDS_Shape
00051 //=======================================================================
00052 
00053 GEOM::GEOM_Object_ptr
00054 StdMeshers_ObjRefUlils::EntryOrShapeToGeomObject (const std::string&  theEntry,
00055                                                   const TopoDS_Shape& theShape)
00056 {
00057   GEOM::GEOM_Object_var geom = GEOM::GEOM_Object::_nil();
00058 
00059   // try by entry
00060   if (SMESH_Gen_i* gen = SMESH_Gen_i::GetSMESHGen()) {
00061     SALOMEDS::Study_var study = gen->GetCurrentStudy();
00062     if ( ! theEntry.empty() && ! study->_is_nil() ) {
00063       SALOMEDS::SObject_var sobj= study->FindObjectID( theEntry.c_str() );
00064       CORBA::Object_var obj = gen->SObjectToObject( sobj );
00065       geom = GEOM::GEOM_Object::_narrow( obj );
00066     }
00067   }
00068   // try by TopoDS_Shape
00069   if ( geom->_is_nil() )
00070     geom = ShapeToGeomObject( theShape );
00071 
00072   return geom._retn();
00073 }
00074 
00075 //================================================================================
00081 //================================================================================
00082 
00083 void StdMeshers_ObjRefUlils::SaveToStream( const TopoDS_Shape& theShape, ostream & stream)
00084 {
00085   bool ok = false;
00086   if ( !theShape.IsNull() ) {
00087     if (SMESH_Gen_i* gen = SMESH_Gen_i::GetSMESHGen()) {
00088       GEOM::GEOM_Object_var geom = gen->ShapeToGeomObject( theShape );
00089       if ( ! geom->_is_nil() ) {
00090         SALOMEDS::SObject_var sobj = gen->ObjectToSObject( gen->GetCurrentStudy(), geom );
00091         if ( !sobj->_is_nil() ) {
00092           stream << " " << sobj->GetID();
00093           ok = true;
00094         }
00095       }
00096     }
00097   }
00098   if ( ! ok )
00099     stream << " NULL_SHAPE ";
00100 }
00101 
00102 //================================================================================
00108 //================================================================================
00109 
00110 TopoDS_Shape StdMeshers_ObjRefUlils::LoadFromStream( istream & stream)
00111 {
00112   if (SMESH_Gen_i* gen = SMESH_Gen_i::GetSMESHGen()) {
00113     SALOMEDS::Study_var study = gen->GetCurrentStudy();
00114     if ( ! study->_is_nil() ) {
00115       string str;
00116       if (stream >> str) {
00117         SALOMEDS::SObject_var sobj= study->FindObjectID( str.c_str() );
00118         CORBA::Object_var obj = gen->SObjectToObject( sobj );
00119         GEOM::GEOM_Object_var geom = GEOM::GEOM_Object::_narrow( obj );
00120         return gen->GeomObjectToShape( geom.in() );
00121       }
00122     }
00123   }
00124   return TopoDS_Shape();
00125 }
00126 
00127 //================================================================================
00133 //================================================================================
00134 
00135 void StdMeshers_ObjRefUlils::SaveToStream( CORBA::Object_ptr obj,
00136                                            std::ostream & stream)
00137 {
00138   bool ok = false;
00139   if ( !CORBA::is_nil( obj ) ) {
00140     if (SMESH_Gen_i* gen = SMESH_Gen_i::GetSMESHGen()) {
00141       stream << " " << gen->GetObjectId( obj );
00142       ok = true;
00143     }
00144   }
00145   if ( ! ok )
00146     stream << " NULL_OBJECT ";
00147 }
00148 
00149 //=======================================================================
00150 //function : SaveToStream
00151 //purpose  : Store the study entry of object in the stream
00152 //=======================================================================
00153 
00154 void StdMeshers_ObjRefUlils::SaveToStream( const std::string& studyEntry,
00155                                            std::ostream &     stream)
00156 {
00157   if ( studyEntry.find_first_not_of( ' ' ) == std::string::npos )
00158     stream << " NULL_OBJECT ";
00159   else
00160     stream << " " << studyEntry;
00161 }