Salome HOME
Merge multi-study removal branch.
[modules/smesh.git] / src / StdMeshers_I / StdMeshers_ImportSource2D_i.cxx
1 // Copyright (C) 2007-2016  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_I : idl implementation based on 'SMESH' unit's calsses
24 //  File   : StdMeshers_ImportSource2D_i.cxx
25 //  Module : SMESH
26 //
27 #include "StdMeshers_ImportSource2D_i.hxx"
28
29 #include "SMESH_Gen.hxx"
30 #include "SMESH_Gen_i.hxx"
31 #include "SMESH_Group_i.hxx"
32 #include "SMESH_PythonDump.hxx"
33 #include "StdMeshers_ObjRefUlils.hxx"
34
35 #include "Utils_CorbaException.hxx"
36 #include "utilities.h"
37
38 #include <TCollection_AsciiString.hxx>
39
40 #include CORBA_SERVER_HEADER(SMESH_Group)
41
42 using namespace std;
43
44 //=============================================================================
45 /*!
46  *  StdMeshers_ImportSource2D_i::StdMeshers_ImportSource2D_i
47  *
48  *  Constructor
49  */
50 //=============================================================================
51
52 StdMeshers_ImportSource2D_i::StdMeshers_ImportSource2D_i( PortableServer::POA_ptr thePOA,
53                                                           ::SMESH_Gen*            theGenImpl )
54   : SALOME::GenericObj_i( thePOA ), 
55     SMESH_Hypothesis_i( thePOA )
56 {
57   myBaseImpl = new ::StdMeshers_ImportSource2D( theGenImpl->GetANewId(),
58                                                 theGenImpl );
59   _groupEntries = new SMESH::string_array();
60 }
61
62 //=============================================================================
63 /*!
64  *  StdMeshers_ImportSource2D_i::~StdMeshers_ImportSource2D_i
65  *
66  *  Destructor
67  */
68 //=============================================================================
69
70 StdMeshers_ImportSource2D_i::~StdMeshers_ImportSource2D_i()
71 {
72 }
73
74 //=============================================================================
75 /*!
76  *  SetSourceFaces
77  */
78 //=============================================================================
79
80 void StdMeshers_ImportSource2D_i::SetSourceFaces(const SMESH::ListOfGroups& groups)
81 {
82   ASSERT( myBaseImpl );
83   try
84   {
85     std::vector<SMESH_Group*> smesh_groups;
86     std::vector<string> entries;
87     for ( CORBA::ULong i = 0; i < groups.length(); ++i )
88       if ( SMESH_GroupBase_i* gp_i = SMESH::DownCast<SMESH_GroupBase_i*>( groups[i] ))
89       {
90         if ( gp_i->GetType() != SMESH::FACE )
91           THROW_SALOME_CORBA_EXCEPTION("Wrong group type", SALOME::BAD_PARAM);
92         smesh_groups.push_back( gp_i->GetSmeshGroup() );
93
94         SALOMEDS::SObject_var so = SMESH_Gen_i::ObjectToSObject(groups[i]);
95         if ( !so->_is_nil())
96         {
97           CORBA::String_var entry = so->GetID();
98           entries.push_back( entry.in() );
99           so->UnRegister();
100         }
101       }
102     this->GetImpl()->SetGroups( smesh_groups );
103
104     _groupEntries = new SMESH::string_array;
105     _groupEntries->length( entries.size ());
106     for ( size_t i = 0; i < entries.size(); ++i )
107       _groupEntries[i] = entries[i].c_str();
108   }
109   catch ( SALOME_Exception& S_ex )
110   {
111     THROW_SALOME_CORBA_EXCEPTION( S_ex.what(), SALOME::BAD_PARAM );
112   }
113
114   // Update Python script
115   SMESH::TPythonDump() << _this() << ".SetSourceFaces( " << groups << " )";
116 }
117
118 //=============================================================================
119 /*!
120  * Return entries of groups
121  */
122 //=============================================================================
123
124 SMESH::string_array*  StdMeshers_ImportSource2D_i::GetSourceFaces()
125 {
126   SMESH::string_array_var res = new SMESH::string_array( _groupEntries );
127   return res._retn();
128 }
129
130 //================================================================================
131 /*!
132  * \brief Set to copy mesh and groups
133  */
134 //================================================================================
135
136 void StdMeshers_ImportSource2D_i::SetCopySourceMesh(CORBA::Boolean toCopyMesh,
137                                                     CORBA::Boolean toCopyGroups)
138 {
139   GetImpl()->SetCopySourceMesh(toCopyMesh,toCopyGroups);
140   SMESH::TPythonDump() << _this() << ".SetCopySourceMesh( "
141                        << toCopyMesh << ", " << toCopyGroups << " )";
142 }
143
144 //================================================================================
145 /*!
146  * \brief Return "to copy mesh and groups"
147  */
148 //================================================================================
149
150 void StdMeshers_ImportSource2D_i::GetCopySourceMesh(CORBA::Boolean& toCopyMesh,
151                                                     CORBA::Boolean& toCopyGroups)
152 {
153   GetImpl()->GetCopySourceMesh(toCopyMesh,toCopyGroups);
154 }
155
156 //================================================================================
157 /*!
158  * \brief Write parameters in a string
159   * \retval char* - resulting string
160  */
161 //================================================================================
162
163 char* StdMeshers_ImportSource2D_i::SaveTo()
164 {
165   std::ostringstream os;
166   os << " " << _groupEntries->length();
167
168   for ( CORBA::ULong i = 0; i < _groupEntries->length(); ++i )
169   {
170     // entry
171     os << " " << _groupEntries[i];
172
173     // id
174     SALOMEDS::SObject_var groupSO = SMESH_Gen_i::getStudyServant()->FindObjectID( _groupEntries[i] );
175     CORBA::Object_var    groupObj;
176     if ( !groupSO->_is_nil() ) {
177       groupObj = groupSO->GetObject();
178       groupSO->UnRegister();
179     }
180     StdMeshers_ObjRefUlils::SaveToStream( groupObj, os );
181   }
182
183   myBaseImpl->SaveTo( os );
184
185   return CORBA::string_dup( os.str().c_str() );
186 }
187
188 //================================================================================
189 /*!
190  * \brief Retrieve parameters from the string
191   * \param theStream - the input string
192  */
193 //================================================================================
194
195 void StdMeshers_ImportSource2D_i::LoadFrom( const char* theStream )
196 {
197   std::istringstream is( theStream );
198
199   int nbGroups;
200   is >> nbGroups;
201
202   _groupEntries = new SMESH::string_array;
203   _groupEntries->length( nbGroups );
204   std::string id, entry;
205   for ( CORBA::ULong i = 0; i < _groupEntries->length(); ++i )
206   {
207     if ( is >> entry )
208       _groupEntries[i] = entry.c_str();
209     else
210     {
211       _groupEntries->length( i );
212       is.clear(ios::badbit | is.rdstate());
213       break;
214     }
215     if ( is >> id )
216       _groupIDs.push_back( id );
217     else
218     {
219       is.clear(ios::badbit | is.rdstate());
220       break;
221     }
222   }
223
224   myBaseImpl->LoadFrom( is );
225 }
226
227 //================================================================================
228 /*!
229  * \brief Retrieve groups by their ids loaded by LoadFrom()
230  * This is possible only when all meshes are fully loaded
231  */
232 //================================================================================
233
234 void StdMeshers_ImportSource2D_i::UpdateAsMeshesRestored()
235 {
236   std::vector<SMESH_Group*> smesh_groups;
237   for ( unsigned i = 0; i < _groupIDs.size(); ++i )
238   {
239     std::istringstream is( _groupIDs[i].c_str() );
240     SMESH::SMESH_GroupBase_var group =
241       StdMeshers_ObjRefUlils::LoadObjectFromStream<SMESH::SMESH_GroupBase>( is );
242     if ( SMESH_GroupBase_i* gp_i = SMESH::DownCast<SMESH_GroupBase_i*>( group ))
243       smesh_groups.push_back( gp_i->GetSmeshGroup() );
244   }
245   GetImpl()->RestoreGroups(smesh_groups);
246 }
247
248 //=============================================================================
249 /*!
250  *  StdMeshers_ImportSource2D_i::GetImpl
251  *
252  *  Get implementation
253  */
254 //=============================================================================
255
256 ::StdMeshers_ImportSource2D* StdMeshers_ImportSource2D_i::GetImpl()
257 {
258   return ( ::StdMeshers_ImportSource2D* )myBaseImpl;
259 }
260
261 //================================================================================
262 /*!
263  * \brief Verify whether hypothesis supports given entity type 
264   * \param type - dimension (see SMESH::Dimension enumeration)
265   * \retval CORBA::Boolean - TRUE if dimension is supported, FALSE otherwise
266  * 
267  * Verify whether hypothesis supports given entity type (see SMESH::Dimension enumeration)
268  */
269 //================================================================================  
270 CORBA::Boolean StdMeshers_ImportSource2D_i::IsDimSupported( SMESH::Dimension type )
271 {
272   return type == SMESH::DIM_2D;
273 }
274