1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // SMESH StdMeshers_ImportSource1D : implementaion of SMESH idl descriptions
24 // File : StdMeshers_ImportSource1D.cxx
27 #include "StdMeshers_ImportSource.hxx"
29 #include "SMESHDS_GroupOnGeom.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"
37 #include "utilities.h"
39 #include <Standard_ErrorHandler.hxx>
41 #include <boost/shared_ptr.hpp>
45 //=============================================================================
47 * Creates StdMeshers_ImportSource1D
49 //=============================================================================
51 StdMeshers_ImportSource1D::StdMeshers_ImportSource1D(int hypId,
54 :SMESH_Hypothesis(hypId, studyId, gen),
58 _name = "ImportSource1D";
59 _param_algo_dim = 1; // is used by StdMeshers_Import_1D;
62 //=============================================================================
64 * Creates StdMeshers_ImportSource2D
66 //=============================================================================
68 StdMeshers_ImportSource2D::StdMeshers_ImportSource2D(int hypId,
71 :StdMeshers_ImportSource1D(hypId, studyId, gen)
73 _name = "ImportSource2D";
74 _param_algo_dim = 2; // is used by StdMeshers_Import_2D;
77 //=============================================================================
81 //=============================================================================
83 StdMeshers_ImportSource1D::~StdMeshers_ImportSource1D()
86 //=============================================================================
88 * Sets groups to import elements from
90 //=============================================================================
92 void StdMeshers_ImportSource1D::SetGroups(const std::vector<SMESH_Group*>& groups)
94 if (_groups != groups)
97 NotifySubMeshesHypothesisModification();
101 void StdMeshers_ImportSource1D::SetCopySourceMesh(bool toCopyMesh, bool toCopyGroups)
103 if ( !toCopyMesh ) toCopyGroups = false;
104 if ( _toCopyMesh != toCopyMesh || _toCopyGroups != toCopyGroups )
106 _toCopyMesh = toCopyMesh; _toCopyGroups = toCopyGroups;
107 NotifySubMeshesHypothesisModification();
110 void StdMeshers_ImportSource1D::GetCopySourceMesh(bool& toCopyMesh, bool& toCopyGroups) const
112 toCopyMesh = _toCopyMesh; toCopyGroups = _toCopyGroups;
117 //================================================================================
119 * \brief Return only alive groups
121 //================================================================================
123 vector<SMESH_Group*> getValidGroups(const vector<SMESH_Group*>& groups,
124 StudyContextStruct* studyContext)
126 vector<SMESH_Group*> okGroups;
127 for ( int i = 0; i < groups.size(); ++i )
131 // we expect SIGSEGV on a dead group
133 SMESH_Group* okGroup = 0;
134 map<int, SMESH_Mesh*>::iterator itm = itm = studyContext->mapMesh.begin();
135 for ( ; !okGroup && itm != studyContext->mapMesh.end(); itm++)
137 SMESH_Mesh::GroupIteratorPtr gIt = itm->second->GetGroups();
138 while ( gIt->more() && !okGroup )
139 if ( gIt->next() == groups[i] )
143 okGroups.push_back( okGroup );
151 //================================================================================
153 * \brief Pack meshes into a pair of ints
155 //================================================================================
157 pair<int, int> getResMapKey(const SMESHDS_Mesh& srcMesh, const SMESHDS_Mesh& tgtMesh)
159 return make_pair( srcMesh.GetPersistentId() , tgtMesh.GetPersistentId() );
161 //================================================================================
163 * \brief Return a target mesh by a pair of ints
165 //================================================================================
167 SMESH_Mesh* getTgtMeshByKey( const pair<int, int> & resMapKey,
168 StudyContextStruct* studyContext)
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++)
175 tgtMesh = (*itm).second;
176 if ( tgtMesh->GetMeshDS()->GetPersistentId() != tgtID )
181 //================================================================================
183 * \brief Return a target mesh by a pair of ints
185 //================================================================================
187 int getSrcMeshID( const pair<int, int> & resMapKey )
189 return resMapKey.first;
193 //=============================================================================
195 * Returns groups to import elements from
197 //=============================================================================
199 const std::vector<SMESH_Group*>& StdMeshers_ImportSource1D::GetGroups() const
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;
210 //================================================================================
212 * \brief Return source meshes
214 //================================================================================
216 std::vector<SMESH_Mesh*> StdMeshers_ImportSource1D::GetSourceMeshes() const
218 // GetPersistentId()'s of meshes
220 const vector<SMESH_Group*>& groups = GetGroups();
221 if ( !groups.empty() )
223 for ( unsigned i = 0; i < groups.size(); ++i )
225 const SMESHDS_GroupBase* gDS = groups[i]->GetGroupDS();
226 int id = gDS->GetMesh()->GetPersistentId();
227 meshIDs.insert( id );
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 ));
239 // Find corresponding meshes
240 vector<SMESH_Mesh*> meshes;
241 if ( !meshIDs.empty() )
243 StudyContextStruct* studyContext = _gen->GetStudyContext(_studyId);
244 for ( set<int>::iterator id = meshIDs.begin(); id != meshIDs.end(); ++id )
246 map<int, SMESH_Mesh*>::iterator itm = itm = studyContext->mapMesh.begin();
247 for ( ; itm != studyContext->mapMesh.end(); itm++)
249 SMESH_Mesh* mesh = (*itm).second;
250 if ( mesh->GetMeshDS()->GetPersistentId() == *id )
252 meshes.push_back( mesh );
261 //================================================================================
263 * \brief Return submeshes whose events affect the target mesh
265 //================================================================================
267 std::vector<SMESH_subMesh*>
268 StdMeshers_ImportSource1D::GetSourceSubMeshes(const SMESH_Mesh* srcMesh) const
270 if ( !srcMesh->HasShapeToMesh() )
272 SMESH_Mesh* srcM = const_cast< SMESH_Mesh* >( srcMesh );
273 return vector<SMESH_subMesh*>(1, srcM->GetSubMesh( srcM->GetShapeToMesh()));
276 const vector<SMESH_Group*>& groups = GetGroups();
277 const SMESHDS_Mesh * srcMeshDS = srcMesh->GetMeshDS();
278 for ( size_t i = 0; i < groups.size(); ++i )
280 SMESHDS_GroupBase * grDS = groups[i]->GetGroupDS();
281 if ( grDS->GetMesh() != srcMeshDS )
283 if ( SMESHDS_GroupOnGeom* gog = dynamic_cast<SMESHDS_GroupOnGeom*>( grDS ))
285 shapeIDs.insert( srcMeshDS->ShapeToIndex( gog->GetShape() ));
289 SMDS_ElemIteratorPtr elIt = grDS->GetElements();
290 while ( elIt->more() )
291 shapeIDs.insert( elIt->next()->getshapeId() );
294 if ( !shapeIDs.empty() && *shapeIDs.begin() < 1 )
296 shapeIDs.erase( shapeIDs.begin() );
297 shapeIDs.insert( 1 );
300 vector<SMESH_subMesh*> smVec( shapeIDs.size());
301 set<int>::iterator sID = shapeIDs.begin();
302 for ( int i = 0; sID != shapeIDs.end(); ++sID, ++i )
303 smVec[i] = srcMesh->GetSubMeshContaining( *sID );
308 //=============================================================================
310 * Save _toCopyMesh and _toCopyGroups to a stream
312 //=============================================================================
314 ostream & StdMeshers_ImportSource1D::SaveTo(ostream & save)
316 resultGroupsToIntVec();
318 save << " " << _toCopyMesh << " " << _toCopyGroups;
319 save << " " << _resultGroupsStorage.size();
320 for ( unsigned i = 0; i < _resultGroupsStorage.size(); ++i )
321 save << " " << _resultGroupsStorage[i];
326 //=============================================================================
328 * Load _toCopyMesh and _toCopyGroups from a stream
330 //=============================================================================
332 istream & StdMeshers_ImportSource1D::LoadFrom(istream & load)
334 load >> _toCopyMesh >> _toCopyGroups;
336 _resultGroupsStorage.clear();
340 _resultGroupsStorage.reserve(val);
341 while ( _resultGroupsStorage.size() < _resultGroupsStorage.capacity() && load >> val )
342 _resultGroupsStorage.push_back( val );
347 //================================================================================
349 * \brief Convert result groups into _resultGroupsStorage
351 //================================================================================
353 void StdMeshers_ImportSource1D::resultGroupsToIntVec()
355 _resultGroupsStorage.clear();
357 // store result groups
358 TResGroupMap::iterator key2groups = _resultGroups.begin();
359 for ( ; key2groups != _resultGroups.end(); ++key2groups )
361 const pair<int, int>& key = key2groups->first;
362 const vector<SMESH_Group*>& groups = key2groups->second;
363 // mesh ids, nb groups
364 _resultGroupsStorage.push_back( key.first );
365 _resultGroupsStorage.push_back( key.second );
366 _resultGroupsStorage.push_back( groups.size() );
367 for ( unsigned i = 0; i < groups.size(); ++i )
369 // store group names as sequence of ints each standing for a char
370 // of a name; that is to avoid pb with names containing white spaces
371 string name = groups[i]->GetGroupDS()->GetStoreName();
372 _resultGroupsStorage.push_back( name.size() );
373 for ( unsigned j = 0; j < name.size(); ++j )
374 _resultGroupsStorage.push_back( name[j] );
379 //================================================================================
381 * \brief Restore source groups and result groups by _resultGroupsStorage
383 //================================================================================
385 void StdMeshers_ImportSource1D::RestoreGroups(const std::vector<SMESH_Group*>& groups)
389 _resultGroups.clear();
391 while ( i < _resultGroupsStorage.size() )
393 int key1 = _resultGroupsStorage[i++];
394 int key2 = _resultGroupsStorage[i++];
395 pair<int, int> resMapKey( key1, key2 );
396 SMESH_Mesh* mesh = getTgtMeshByKey( resMapKey, _gen->GetStudyContext(_studyId));
397 // restore mesh ids at least
398 _resultGroups.insert( make_pair (resMapKey,vector<SMESH_Group*>() ));
400 int nbGroups = _resultGroupsStorage[i++];
401 for ( int j = 0; j < nbGroups; ++j )
403 string::size_type nameSize = _resultGroupsStorage[i++];
404 string groupName(nameSize, '\0');
405 for ( unsigned k = 0; k < nameSize; ++k )
406 groupName[k] = (char) _resultGroupsStorage[i++];
408 // find a group by name
411 SMESH_Group* group = 0;
412 SMESH_Mesh::GroupIteratorPtr gIt = mesh->GetGroups();
413 while ( !group && gIt->more() )
416 if ( !group->GetGroupDS() || groupName != group->GetGroupDS()->GetStoreName() )
420 _resultGroups[ resMapKey ].push_back( group );
426 //================================================================================
428 * \brief Remember groups imported from other mesh
429 * \param groups - result groups
430 * \param srcMesh - source mesh
431 * \param tgtMesh - destination mesh
433 //================================================================================
435 void StdMeshers_ImportSource1D::StoreResultGroups(const std::vector<SMESH_Group*>& groups,
436 const SMESHDS_Mesh& srcMesh,
437 const SMESHDS_Mesh& tgtMesh)
439 _resultGroups[ getResMapKey(srcMesh,tgtMesh) ] = groups;
442 //================================================================================
444 * \brief Return groups imported from other mesh
445 * \param srcMesh - source mesh
446 * \param tgtMesh - destination mesh
447 * \retval const std::vector<SMESH_Group*>& - groups
449 //================================================================================
451 std::vector<SMESH_Group*>*
452 StdMeshers_ImportSource1D::GetResultGroups(const SMESHDS_Mesh& srcMesh,
453 const SMESHDS_Mesh& tgtMesh)
455 TResGroupMap::iterator key2groups = _resultGroups.find( getResMapKey(srcMesh,tgtMesh ));
456 if ( key2groups == _resultGroups.end() )
458 vector<SMESH_Group*> vec = getValidGroups((*key2groups).second,
459 _gen->GetStudyContext(_studyId) );
460 if ( vec.size() != key2groups->second.size())
461 key2groups->second = vec;
463 return & key2groups->second;
466 //================================================================================
468 * \brief Initialize ImportSource value by the mesh built on the geometry
469 * \param theMesh - the built mesh
470 * \param theShape - the geometry of interest
471 * \retval bool - true if parameter values have been successfully defined
473 //================================================================================
475 bool StdMeshers_ImportSource1D::SetParametersByMesh(const SMESH_Mesh*, const TopoDS_Shape&)
480 //================================================================================
482 * \brief Initialize my parameter values by default parameters.
483 * \retval bool - true if parameter values have been successfully defined
485 //================================================================================
487 bool StdMeshers_ImportSource1D::SetParametersByDefaults(const TDefaults&, const SMESH_Mesh* )