Salome HOME
Merge from V6_3_BR 06/06/2011
[modules/smesh.git] / src / StdMeshers_I / StdMeshers_ObjRefUlils.cxx
1 // Copyright (C) 2007-2011  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.
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 //  SMESH SMESH : implementaion of SMESH idl descriptions
24 // File      : StdMeshers_ObjRefUlils.cxx
25 // Created   : Wed Oct 18 15:38:22 2006
26 // Author    : Edward AGAPOV (eap)
27 //
28 #include "StdMeshers_ObjRefUlils.hxx"
29
30 #include <TopoDS_Shape.hxx>
31
32 using namespace std;
33
34 //================================================================================
35   /*!
36    * \brief Store the shape in the stream
37     * \param theShape - shape to store
38     * \param stream - the stream
39    */
40 //================================================================================
41
42 void StdMeshers_ObjRefUlils::SaveToStream( const TopoDS_Shape& theShape, ostream & stream)
43 {
44   bool ok = false;
45   if ( !theShape.IsNull() ) {
46     if (SMESH_Gen_i* gen = SMESH_Gen_i::GetSMESHGen()) {
47       GEOM::GEOM_Object_var geom = gen->ShapeToGeomObject( theShape );
48       if ( ! geom->_is_nil() ) {
49         SALOMEDS::SObject_var sobj = gen->ObjectToSObject( gen->GetCurrentStudy(), geom );
50         if ( !sobj->_is_nil() ) {
51           stream << " " << sobj->GetID();
52           ok = true;
53         }
54       }
55     }
56   }
57   if ( ! ok )
58     stream << " NULL_SHAPE ";
59 }
60
61 //================================================================================
62   /*!
63    * \brief Retrieve a shape from the stream
64     * \param stream - the stream
65     * \retval TopoDS_Shape - resulting shape
66    */
67 //================================================================================
68
69 TopoDS_Shape StdMeshers_ObjRefUlils::LoadFromStream( istream & stream)
70 {
71   if (SMESH_Gen_i* gen = SMESH_Gen_i::GetSMESHGen()) {
72     SALOMEDS::Study_var study = gen->GetCurrentStudy();
73     if ( ! study->_is_nil() ) {
74       string str;
75       if (stream >> str) {
76         SALOMEDS::SObject_var sobj= study->FindObjectID( str.c_str() );
77         CORBA::Object_var obj = gen->SObjectToObject( sobj );
78         GEOM::GEOM_Object_var geom = GEOM::GEOM_Object::_narrow( obj );
79         return gen->GeomObjectToShape( geom.in() );
80       }
81     }
82   }
83   return TopoDS_Shape();
84 }
85
86 //================================================================================
87   /*!
88    * \brief Store the CORBA object in the stream
89     * \param obj - object to store
90     * \param stream - the stream
91    */
92 //================================================================================
93
94 void StdMeshers_ObjRefUlils::SaveToStream( CORBA::Object_ptr obj,
95                                            std::ostream & stream)
96 {
97   bool ok = false;
98   if ( !CORBA::is_nil( obj ) ) {
99     if (SMESH_Gen_i* gen = SMESH_Gen_i::GetSMESHGen()) {
100       stream << " " << gen->GetObjectId( obj );
101       ok = true;
102     }
103   }
104   if ( ! ok )
105     stream << " NULL_OBJECT ";
106 }