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