Salome HOME
Update copyright
[modules/smesh.git] / src / StdMeshers / StdMeshers_ImportSource.cxx
1 // Copyright (C) 2007-2011  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 StdMeshers_ImportSource1D : implementaion of SMESH idl descriptions
24 //  File   : StdMeshers_ImportSource1D.cxx
25 //  Module : SMESH
26 //
27 #include "StdMeshers_ImportSource.hxx"
28
29 #include "SMESHDS_GroupBase.hxx"
30 #include "SMESHDS_Mesh.hxx"
31 #include "SMESH_Algo.hxx"
32 #include "SMESH_Gen.hxx"
33 #include "SMESH_Group.hxx"
34 #include "SMESH_Mesh.hxx"
35 #include "SMESH_subMeshEventListener.hxx"
36
37 #include "utilities.h"
38
39 #include <Standard_ErrorHandler.hxx>
40
41 #include <boost/shared_ptr.hpp>
42
43 using namespace std;
44
45 //=============================================================================
46 /*!
47  * Creates StdMeshers_ImportSource1D
48  */
49 //=============================================================================
50
51 StdMeshers_ImportSource1D::StdMeshers_ImportSource1D(int         hypId,
52                                                      int         studyId,
53                                                      SMESH_Gen * gen)
54   :SMESH_Hypothesis(hypId, studyId, gen),
55    _toCopyMesh(false),
56    _toCopyGroups(false)
57 {
58   _name = "ImportSource1D";
59   _param_algo_dim = 1; // is used by StdMeshers_Import_1D;
60 }
61
62 //=============================================================================
63 /*!
64  * Creates StdMeshers_ImportSource2D
65  */
66 //=============================================================================
67
68 StdMeshers_ImportSource2D::StdMeshers_ImportSource2D(int         hypId,
69                                                      int         studyId,
70                                                      SMESH_Gen * gen)
71   :StdMeshers_ImportSource1D(hypId, studyId, gen)
72 {
73   _name = "ImportSource2D";
74   _param_algo_dim = 2; // is used by StdMeshers_Import_2D;
75 }
76
77 //=============================================================================
78 /*!
79  *
80  */
81 //=============================================================================
82
83 StdMeshers_ImportSource1D::~StdMeshers_ImportSource1D()
84 {
85 }
86 //=============================================================================
87 /*!
88  *  Sets groups to import elements from
89  */
90 //=============================================================================
91
92 void StdMeshers_ImportSource1D::SetGroups(const std::vector<SMESH_Group*>& groups)
93 {
94   if (_groups != groups)
95   {
96     _groups = groups;
97     NotifySubMeshesHypothesisModification();
98   }
99 }
100
101 void StdMeshers_ImportSource1D::SetCopySourceMesh(bool toCopyMesh, bool toCopyGroups)
102 {
103   if ( !toCopyMesh ) toCopyGroups = false;
104   if ( _toCopyMesh != toCopyMesh || _toCopyGroups != toCopyGroups )
105   {
106     _toCopyMesh = toCopyMesh; _toCopyGroups = toCopyGroups;
107     NotifySubMeshesHypothesisModification();
108   }
109 }
110 void StdMeshers_ImportSource1D::GetCopySourceMesh(bool& toCopyMesh, bool& toCopyGroups) const
111 {
112   toCopyMesh = _toCopyMesh; toCopyGroups = _toCopyGroups;
113 }
114   
115 namespace
116 {
117   //================================================================================
118   /*!
119    * \brief Return only alive groups
120    */
121   //================================================================================
122
123   vector<SMESH_Group*> getValidGroups(const vector<SMESH_Group*>& groups,
124                                       StudyContextStruct*         studyContext)
125   {
126     vector<SMESH_Group*> okGroups;
127     for ( int i = 0; i < groups.size(); ++i )
128     {
129       try
130       {
131         // we expect SIGSEGV on a dead group
132         OCC_CATCH_SIGNALS;
133         SMESH_Group* okGroup = 0;
134         map<int, SMESH_Mesh*>::iterator itm = itm = studyContext->mapMesh.begin();
135         for ( ; !okGroup && itm != studyContext->mapMesh.end(); itm++)
136         {
137           SMESH_Mesh::GroupIteratorPtr gIt = itm->second->GetGroups();
138           while ( gIt->more() && !okGroup )
139             if ( gIt->next() == groups[i] )
140               okGroup = groups[i];
141         }
142         if ( okGroup )
143           okGroups.push_back( okGroup );
144       }
145       catch(...)
146       {
147       }
148     }
149     return okGroups;
150   }
151   //================================================================================
152   /*!
153    * \brief Pack meshes into a pair of ints
154    */
155   //================================================================================
156
157   pair<int, int> getResMapKey(const SMESHDS_Mesh& srcMesh, const SMESHDS_Mesh& tgtMesh)
158   {
159     return make_pair( srcMesh.GetPersistentId() , tgtMesh.GetPersistentId() );
160   }
161   //================================================================================
162   /*!
163    * \brief Return a target mesh by a pair of ints
164    */
165   //================================================================================
166
167   SMESH_Mesh* getTgtMeshByKey( const pair<int, int> & resMapKey,
168                                StudyContextStruct*    studyContext)
169   {
170     int tgtID = resMapKey.second;
171     SMESH_Mesh* tgtMesh = 0;
172     map<int, SMESH_Mesh*>::iterator itm = itm = studyContext->mapMesh.begin();
173     for ( ; !tgtMesh && itm != studyContext->mapMesh.end(); itm++)
174     {
175       tgtMesh = (*itm).second;
176       if ( tgtMesh->GetMeshDS()->GetPersistentId() != tgtID )
177         tgtMesh = 0;
178     }
179     return tgtMesh;
180   }
181   //================================================================================
182   /*!
183    * \brief Return a target mesh by a pair of ints
184    */
185   //================================================================================
186
187   int getSrcMeshID( const pair<int, int> & resMapKey )
188   {
189     return resMapKey.first;
190   }
191 }
192
193 //=============================================================================
194 /*!
195  *  Returns groups to import elements from
196  */
197 //=============================================================================
198
199 const std::vector<SMESH_Group*>&  StdMeshers_ImportSource1D::GetGroups() const
200 {
201   // filter off deleted groups
202   vector<SMESH_Group*> okGroups = getValidGroups( _groups,
203                                                   _gen->GetStudyContext(_studyId) );
204   if ( okGroups.size() != _groups.size() )
205     ((StdMeshers_ImportSource1D*)this)->_groups = okGroups;
206
207   return _groups;
208 }
209
210 //================================================================================
211 /*!
212  * \brief Return source meshes
213  */
214 //================================================================================
215
216 std::vector<SMESH_Mesh*> StdMeshers_ImportSource1D::GetSourceMeshes() const
217 {
218   // GetPersistentId()'s of meshes
219   set<int> meshIDs;
220   const vector<SMESH_Group*>& groups = GetGroups();
221   if ( !groups.empty() )
222   {
223     for ( unsigned i = 0; i < groups.size(); ++i )
224     {
225       const SMESHDS_GroupBase* gDS = groups[i]->GetGroupDS();
226       int id = gDS->GetMesh()->GetPersistentId();
227       meshIDs.insert( id );
228     }
229   }
230   else
231   {
232     if ( _resultGroups.empty() )
233       ((StdMeshers_ImportSource1D*)this)->RestoreGroups(_groups);
234     TResGroupMap::const_iterator key_groups = _resultGroups.begin();
235     for ( ; key_groups != _resultGroups.end(); ++key_groups )
236       meshIDs.insert( getSrcMeshID( key_groups->first ));
237   }
238
239   // Find corresponding meshes
240   vector<SMESH_Mesh*> meshes;
241   if ( !meshIDs.empty() )
242   {
243     StudyContextStruct* studyContext = _gen->GetStudyContext(_studyId);
244     for ( set<int>::iterator id = meshIDs.begin(); id != meshIDs.end(); ++id )
245     {
246       map<int, SMESH_Mesh*>::iterator itm = itm = studyContext->mapMesh.begin();
247       for ( ; itm != studyContext->mapMesh.end(); itm++)
248       {
249         SMESH_Mesh* mesh = (*itm).second;
250         if ( mesh->GetMeshDS()->GetPersistentId() == *id )
251         {
252           meshes.push_back( mesh );
253           break;
254         }
255       }
256     }
257   }
258   return meshes;
259 }
260
261 //=============================================================================
262 /*!
263  * Save _toCopyMesh and _toCopyGroups to a stream
264  */
265 //=============================================================================
266
267 ostream & StdMeshers_ImportSource1D::SaveTo(ostream & save)
268 {
269   resultGroupsToIntVec();
270
271   save << " " << _toCopyMesh << " " << _toCopyGroups;
272   save << " " << _resultGroupsStorage.size();
273   for ( unsigned i = 0; i < _resultGroupsStorage.size(); ++i )
274     save << " " << _resultGroupsStorage[i];
275
276   return save;
277 }
278
279 //=============================================================================
280 /*!
281  * Load _toCopyMesh and _toCopyGroups from a stream
282  */
283 //=============================================================================
284
285 istream & StdMeshers_ImportSource1D::LoadFrom(istream & load)
286 {
287   load >> _toCopyMesh >> _toCopyGroups;
288
289   _resultGroupsStorage.clear();
290   int val;
291   if ( load >> val )
292   {
293     _resultGroupsStorage.reserve(val);
294     while ( _resultGroupsStorage.size() < _resultGroupsStorage.capacity() && load >> val )
295       _resultGroupsStorage.push_back( val );
296   }
297   return load;
298 }
299
300 //================================================================================
301 /*!
302  * \brief Convert result groups into _resultGroupsStorage
303  */
304 //================================================================================
305
306 void StdMeshers_ImportSource1D::resultGroupsToIntVec()
307 {
308   _resultGroupsStorage.clear();
309   
310   // store result groups
311   TResGroupMap::iterator key2groups = _resultGroups.begin();
312   for ( ; key2groups != _resultGroups.end(); ++key2groups )
313   {
314     const pair<int, int>&          key = key2groups->first;
315     const vector<SMESH_Group*>& groups = key2groups->second;
316     // mesh ids, nb groups
317     _resultGroupsStorage.push_back( key.first );
318     _resultGroupsStorage.push_back( key.second );
319     _resultGroupsStorage.push_back( groups.size() );
320     for ( unsigned i = 0; i < groups.size(); ++i )
321     {
322       // store group names as sequence of ints each standing for a char
323       // of a name; that is to avoid pb with names containing white spaces
324       string name = groups[i]->GetGroupDS()->GetStoreName();
325       _resultGroupsStorage.push_back( name.size() );
326       for ( unsigned j = 0; j < name.size(); ++j )
327         _resultGroupsStorage.push_back( name[j] );
328     }
329   }
330 }
331
332 //================================================================================
333 /*!
334  * \brief Restore source groups and result groups by _resultGroupsStorage
335  */
336 //================================================================================
337
338 void StdMeshers_ImportSource1D::RestoreGroups(const std::vector<SMESH_Group*>& groups)
339 {
340   _groups = groups;
341
342   _resultGroups.clear();
343   int i = 0;
344   while ( i < _resultGroupsStorage.size() )
345   {
346     int key1 = _resultGroupsStorage[i++];
347     int key2 = _resultGroupsStorage[i++];
348     pair<int, int> resMapKey( key1, key2 );
349     SMESH_Mesh* mesh = getTgtMeshByKey( resMapKey, _gen->GetStudyContext(_studyId));
350     // restore mesh ids at least
351     _resultGroups.insert( make_pair (resMapKey,vector<SMESH_Group*>() )); 
352
353     int nbGroups = _resultGroupsStorage[i++];
354     for ( int j = 0; j < nbGroups; ++j )
355     {
356       string::size_type nameSize = _resultGroupsStorage[i++];
357       string groupName(nameSize, '\0');
358       for ( unsigned k = 0; k < nameSize; ++k )
359         groupName[k] = (char) _resultGroupsStorage[i++];
360
361       // find a group by name
362       if ( mesh )
363       {
364         SMESH_Group* group = 0;
365         SMESH_Mesh::GroupIteratorPtr gIt = mesh->GetGroups();
366         while ( !group && gIt->more() )
367         {
368           group = gIt->next();
369           if ( !group->GetGroupDS() || groupName != group->GetGroupDS()->GetStoreName() )
370             group = 0;
371         }
372         if ( group )
373           _resultGroups[ resMapKey ].push_back( group );
374       }
375     }
376   }
377 }
378
379 //================================================================================
380 /*!
381  * \brief Remember groups imported from other mesh
382  *  \param groups - result groups
383  *  \param srcMesh - source mesh
384  *  \param tgtMesh - destination mesh
385  */
386 //================================================================================
387
388 void StdMeshers_ImportSource1D::StoreResultGroups(const std::vector<SMESH_Group*>& groups,
389                                                   const SMESHDS_Mesh&              srcMesh,
390                                                   const SMESHDS_Mesh&              tgtMesh)
391 {
392   _resultGroups[ getResMapKey(srcMesh,tgtMesh) ] = groups;
393 }
394
395 //================================================================================
396 /*!
397  * \brief Return groups imported from other mesh
398  *  \param srcMesh - source mesh
399  *  \param tgtMesh - destination mesh
400  *  \retval const std::vector<SMESH_Group*>& - groups
401  */
402 //================================================================================
403
404 std::vector<SMESH_Group*>*
405 StdMeshers_ImportSource1D::GetResultGroups(const SMESHDS_Mesh& srcMesh,
406                                            const SMESHDS_Mesh& tgtMesh) 
407 {
408   TResGroupMap::iterator key2groups = _resultGroups.find( getResMapKey(srcMesh,tgtMesh ));
409   if ( key2groups == _resultGroups.end() )
410     return 0;
411   vector<SMESH_Group*> vec = getValidGroups((*key2groups).second,
412                                             _gen->GetStudyContext(_studyId) );
413   if ( vec.size() != key2groups->second.size())
414     key2groups->second = vec;
415
416   return & key2groups->second;
417 }
418
419 //================================================================================
420 /*!
421  * \brief Initialize ImportSource value by the mesh built on the geometry
422  * \param theMesh - the built mesh
423  * \param theShape - the geometry of interest
424  * \retval bool - true if parameter values have been successfully defined
425  */
426 //================================================================================
427
428 bool StdMeshers_ImportSource1D::SetParametersByMesh(const SMESH_Mesh*, const TopoDS_Shape&)
429 {
430   return false;
431 }
432
433 //================================================================================
434 /*!
435  * \brief Initialize my parameter values by default parameters.
436  *  \retval bool - true if parameter values have been successfully defined
437  */
438 //================================================================================
439
440 bool StdMeshers_ImportSource1D::SetParametersByDefaults(const TDefaults&, const SMESH_Mesh* )
441 {
442   return false;
443 }