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