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