Salome HOME
Merge from V6_5_BR 05/06/2012
[modules/smesh.git] / src / StdMeshers_I / StdMeshers_ObjRefUlils.cxx
1 // Copyright (C) 2007-2012  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 //function : GeomObjectToEntry
36 //purpose  : Return study entry of GEOM Object
37 //=======================================================================
38
39 std::string StdMeshers_ObjRefUlils::GeomObjectToEntry(GEOM::GEOM_Object_ptr&  theGeomObject)
40 {
41   if ( CORBA::is_nil( theGeomObject ))
42     return "NULL_OBJECT";
43
44   CORBA::String_var entry = theGeomObject->GetStudyEntry();
45   return entry.in();
46 }
47
48 //=======================================================================
49 //function : EntryOrShapeToGeomObject
50 //purpose  :  Return GEOM Object by its sytudy entry or TopoDS_Shape
51 //=======================================================================
52
53 GEOM::GEOM_Object_ptr
54 StdMeshers_ObjRefUlils::EntryOrShapeToGeomObject (const std::string&  theEntry,
55                                                   const TopoDS_Shape& theShape)
56 {
57   GEOM::GEOM_Object_var geom = GEOM::GEOM_Object::_nil();
58
59   // try by entry
60   if (SMESH_Gen_i* gen = SMESH_Gen_i::GetSMESHGen()) {
61     SALOMEDS::Study_var study = gen->GetCurrentStudy();
62     if ( ! theEntry.empty() && ! study->_is_nil() ) {
63       SALOMEDS::SObject_var sobj= study->FindObjectID( theEntry.c_str() );
64       CORBA::Object_var obj = gen->SObjectToObject( sobj );
65       geom = GEOM::GEOM_Object::_narrow( obj );
66     }
67   }
68   // try by TopoDS_Shape
69   if ( geom->_is_nil() )
70     geom = ShapeToGeomObject( theShape );
71
72   return geom._retn();
73 }
74
75 //================================================================================
76   /*!
77    * \brief Store the shape in the stream
78     * \param theShape - shape to store
79     * \param stream - the stream
80    */
81 //================================================================================
82
83 void StdMeshers_ObjRefUlils::SaveToStream( const TopoDS_Shape& theShape, ostream & stream)
84 {
85   bool ok = false;
86   if ( !theShape.IsNull() ) {
87     if (SMESH_Gen_i* gen = SMESH_Gen_i::GetSMESHGen()) {
88       GEOM::GEOM_Object_var geom = gen->ShapeToGeomObject( theShape );
89       if ( ! geom->_is_nil() ) {
90         SALOMEDS::SObject_var sobj = gen->ObjectToSObject( gen->GetCurrentStudy(), geom );
91         if ( !sobj->_is_nil() ) {
92           stream << " " << sobj->GetID();
93           ok = true;
94         }
95       }
96     }
97   }
98   if ( ! ok )
99     stream << " NULL_SHAPE ";
100 }
101
102 //================================================================================
103   /*!
104    * \brief Retrieve a shape from the stream
105     * \param stream - the stream
106     * \retval TopoDS_Shape - resulting shape
107    */
108 //================================================================================
109
110 TopoDS_Shape StdMeshers_ObjRefUlils::LoadFromStream( istream & stream)
111 {
112   if (SMESH_Gen_i* gen = SMESH_Gen_i::GetSMESHGen()) {
113     SALOMEDS::Study_var study = gen->GetCurrentStudy();
114     if ( ! study->_is_nil() ) {
115       string str;
116       if (stream >> str) {
117         SALOMEDS::SObject_var sobj= study->FindObjectID( str.c_str() );
118         CORBA::Object_var obj = gen->SObjectToObject( sobj );
119         GEOM::GEOM_Object_var geom = GEOM::GEOM_Object::_narrow( obj );
120         return gen->GeomObjectToShape( geom.in() );
121       }
122     }
123   }
124   return TopoDS_Shape();
125 }
126
127 //================================================================================
128   /*!
129    * \brief Store the CORBA object in the stream
130     * \param obj - object to store
131     * \param stream - the stream
132    */
133 //================================================================================
134
135 void StdMeshers_ObjRefUlils::SaveToStream( CORBA::Object_ptr obj,
136                                            std::ostream & stream)
137 {
138   bool ok = false;
139   if ( !CORBA::is_nil( obj ) ) {
140     if (SMESH_Gen_i* gen = SMESH_Gen_i::GetSMESHGen()) {
141       stream << " " << gen->GetObjectId( obj );
142       ok = true;
143     }
144   }
145   if ( ! ok )
146     stream << " NULL_OBJECT ";
147 }
148
149 //=======================================================================
150 //function : SaveToStream
151 //purpose  : Store the study entry of object in the stream
152 //=======================================================================
153
154 void StdMeshers_ObjRefUlils::SaveToStream( const std::string& studyEntry,
155                                            std::ostream &     stream)
156 {
157   if ( studyEntry.find_first_not_of( ' ' ) == std::string::npos )
158     stream << " NULL_OBJECT ";
159   else
160     stream << " " << studyEntry;
161 }