Back to index

salome-smesh  6.5.0
StdMeshers_ImportSource.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  SMESH StdMeshers_ImportSource1D : implementaion of SMESH idl descriptions
00024 //  File   : StdMeshers_ImportSource1D.cxx
00025 //  Module : SMESH
00026 //
00027 #include "StdMeshers_ImportSource.hxx"
00028 
00029 #include "SMESHDS_GroupOnGeom.hxx"
00030 #include "SMESHDS_Mesh.hxx"
00031 #include "SMESH_Algo.hxx"
00032 #include "SMESH_Gen.hxx"
00033 #include "SMESH_Group.hxx"
00034 #include "SMESH_Mesh.hxx"
00035 #include "SMESH_subMeshEventListener.hxx"
00036 
00037 #include "utilities.h"
00038 
00039 #include <Standard_ErrorHandler.hxx>
00040 
00041 #include <boost/shared_ptr.hpp>
00042 
00043 using namespace std;
00044 
00045 //=============================================================================
00049 //=============================================================================
00050 
00051 StdMeshers_ImportSource1D::StdMeshers_ImportSource1D(int         hypId,
00052                                                      int         studyId,
00053                                                      SMESH_Gen * gen)
00054   :SMESH_Hypothesis(hypId, studyId, gen),
00055    _toCopyMesh(false),
00056    _toCopyGroups(false)
00057 {
00058   _name = "ImportSource1D";
00059   _param_algo_dim = 1; // is used by StdMeshers_Import_1D;
00060 }
00061 
00062 //=============================================================================
00066 //=============================================================================
00067 
00068 StdMeshers_ImportSource2D::StdMeshers_ImportSource2D(int         hypId,
00069                                                      int         studyId,
00070                                                      SMESH_Gen * gen)
00071   :StdMeshers_ImportSource1D(hypId, studyId, gen)
00072 {
00073   _name = "ImportSource2D";
00074   _param_algo_dim = 2; // is used by StdMeshers_Import_2D;
00075 }
00076 
00077 //=============================================================================
00081 //=============================================================================
00082 
00083 StdMeshers_ImportSource1D::~StdMeshers_ImportSource1D()
00084 {
00085 }
00086 //=============================================================================
00090 //=============================================================================
00091 
00092 void StdMeshers_ImportSource1D::SetGroups(const std::vector<SMESH_Group*>& groups)
00093 {
00094   if (_groups != groups)
00095   {
00096     _groups = groups;
00097     NotifySubMeshesHypothesisModification();
00098   }
00099 }
00100 
00101 void StdMeshers_ImportSource1D::SetCopySourceMesh(bool toCopyMesh, bool toCopyGroups)
00102 {
00103   if ( !toCopyMesh ) toCopyGroups = false;
00104   if ( _toCopyMesh != toCopyMesh || _toCopyGroups != toCopyGroups )
00105   {
00106     _toCopyMesh = toCopyMesh; _toCopyGroups = toCopyGroups;
00107     NotifySubMeshesHypothesisModification();
00108   }
00109 }
00110 void StdMeshers_ImportSource1D::GetCopySourceMesh(bool& toCopyMesh, bool& toCopyGroups) const
00111 {
00112   toCopyMesh = _toCopyMesh; toCopyGroups = _toCopyGroups;
00113 }
00114   
00115 namespace
00116 {
00117   //================================================================================
00121   //================================================================================
00122 
00123   vector<SMESH_Group*> getValidGroups(const vector<SMESH_Group*>& groups,
00124                                       StudyContextStruct*         studyContext)
00125   {
00126     vector<SMESH_Group*> okGroups;
00127     for ( int i = 0; i < groups.size(); ++i )
00128     {
00129       try
00130       {
00131         // we expect SIGSEGV on a dead group
00132         OCC_CATCH_SIGNALS;
00133         SMESH_Group* okGroup = 0;
00134         map<int, SMESH_Mesh*>::iterator itm = itm = studyContext->mapMesh.begin();
00135         for ( ; !okGroup && itm != studyContext->mapMesh.end(); itm++)
00136         {
00137           SMESH_Mesh::GroupIteratorPtr gIt = itm->second->GetGroups();
00138           while ( gIt->more() && !okGroup )
00139             if ( gIt->next() == groups[i] )
00140               okGroup = groups[i];
00141         }
00142         if ( okGroup )
00143           okGroups.push_back( okGroup );
00144       }
00145       catch(...)
00146       {
00147       }
00148     }
00149     return okGroups;
00150   }
00151   //================================================================================
00155   //================================================================================
00156 
00157   pair<int, int> getResMapKey(const SMESHDS_Mesh& srcMesh, const SMESHDS_Mesh& tgtMesh)
00158   {
00159     return make_pair( srcMesh.GetPersistentId() , tgtMesh.GetPersistentId() );
00160   }
00161   //================================================================================
00165   //================================================================================
00166 
00167   SMESH_Mesh* getTgtMeshByKey( const pair<int, int> & resMapKey,
00168                                StudyContextStruct*    studyContext)
00169   {
00170     int tgtID = resMapKey.second;
00171     SMESH_Mesh* tgtMesh = 0;
00172     map<int, SMESH_Mesh*>::iterator itm = itm = studyContext->mapMesh.begin();
00173     for ( ; !tgtMesh && itm != studyContext->mapMesh.end(); itm++)
00174     {
00175       tgtMesh = (*itm).second;
00176       if ( tgtMesh->GetMeshDS()->GetPersistentId() != tgtID )
00177         tgtMesh = 0;
00178     }
00179     return tgtMesh;
00180   }
00181   //================================================================================
00185   //================================================================================
00186 
00187   int getSrcMeshID( const pair<int, int> & resMapKey )
00188   {
00189     return resMapKey.first;
00190   }
00191 }
00192 
00193 //=============================================================================
00197 //=============================================================================
00198 
00199 const std::vector<SMESH_Group*>&  StdMeshers_ImportSource1D::GetGroups() const
00200 {
00201   // filter off deleted groups
00202   vector<SMESH_Group*> okGroups = getValidGroups( _groups,
00203                                                   _gen->GetStudyContext(_studyId) );
00204   if ( okGroups.size() != _groups.size() )
00205     ((StdMeshers_ImportSource1D*)this)->_groups = okGroups;
00206 
00207   return _groups;
00208 }
00209 
00210 //================================================================================
00214 //================================================================================
00215 
00216 std::vector<SMESH_Mesh*> StdMeshers_ImportSource1D::GetSourceMeshes() const
00217 {
00218   // GetPersistentId()'s of meshes
00219   set<int> meshIDs;
00220   const vector<SMESH_Group*>& groups = GetGroups();
00221   if ( !groups.empty() )
00222   {
00223     for ( unsigned i = 0; i < groups.size(); ++i )
00224     {
00225       const SMESHDS_GroupBase* gDS = groups[i]->GetGroupDS();
00226       int id = gDS->GetMesh()->GetPersistentId();
00227       meshIDs.insert( id );
00228     }
00229   }
00230   else
00231   {
00232     if ( _resultGroups.empty() )
00233       ((StdMeshers_ImportSource1D*)this)->RestoreGroups(_groups);
00234     TResGroupMap::const_iterator key_groups = _resultGroups.begin();
00235     for ( ; key_groups != _resultGroups.end(); ++key_groups )
00236       meshIDs.insert( getSrcMeshID( key_groups->first ));
00237   }
00238 
00239   // Find corresponding meshes
00240   vector<SMESH_Mesh*> meshes;
00241   if ( !meshIDs.empty() )
00242   {
00243     StudyContextStruct* studyContext = _gen->GetStudyContext(_studyId);
00244     for ( set<int>::iterator id = meshIDs.begin(); id != meshIDs.end(); ++id )
00245     {
00246       map<int, SMESH_Mesh*>::iterator itm = itm = studyContext->mapMesh.begin();
00247       for ( ; itm != studyContext->mapMesh.end(); itm++)
00248       {
00249         SMESH_Mesh* mesh = (*itm).second;
00250         if ( mesh->GetMeshDS()->GetPersistentId() == *id )
00251         {
00252           meshes.push_back( mesh );
00253           break;
00254         }
00255       }
00256     }
00257   }
00258   return meshes;
00259 }
00260 
00261 //================================================================================
00265 //================================================================================
00266 
00267 std::vector<SMESH_subMesh*>
00268 StdMeshers_ImportSource1D::GetSourceSubMeshes(const SMESH_Mesh* srcMesh) const
00269 {
00270   if ( !srcMesh->HasShapeToMesh() )
00271   {
00272     SMESH_Mesh* srcM = const_cast< SMESH_Mesh* >( srcMesh );
00273     return vector<SMESH_subMesh*>(1, srcM->GetSubMesh( srcM->GetShapeToMesh()));
00274   }
00275   set<int> shapeIDs;
00276   const vector<SMESH_Group*>& groups = GetGroups();
00277   const SMESHDS_Mesh * srcMeshDS = srcMesh->GetMeshDS();
00278   for ( size_t i = 0; i < groups.size(); ++i )
00279   {
00280     SMESHDS_GroupBase * grDS = groups[i]->GetGroupDS();
00281     if ( grDS->GetMesh() != srcMeshDS )
00282       continue;
00283     if ( SMESHDS_GroupOnGeom* gog = dynamic_cast<SMESHDS_GroupOnGeom*>( grDS ))
00284     {
00285       shapeIDs.insert( srcMeshDS->ShapeToIndex( gog->GetShape() ));
00286     }
00287     else
00288     {
00289       SMDS_ElemIteratorPtr elIt = grDS->GetElements();
00290       while ( elIt->more() )
00291         shapeIDs.insert( elIt->next()->getshapeId() );
00292     }
00293   }
00294   if ( !shapeIDs.empty() && *shapeIDs.begin() < 1 )
00295   {
00296     shapeIDs.erase( shapeIDs.begin() );
00297     shapeIDs.insert( 1 );
00298   }
00299 
00300   vector<SMESH_subMesh*> smVec( shapeIDs.size());
00301   set<int>::iterator sID = shapeIDs.begin();
00302   for ( int i = 0; sID != shapeIDs.end(); ++sID, ++i )
00303     smVec[i] = srcMesh->GetSubMeshContaining( *sID );
00304 
00305   return smVec;
00306 }
00307 
00308 //=============================================================================
00312 //=============================================================================
00313 
00314 ostream & StdMeshers_ImportSource1D::SaveTo(ostream & save)
00315 {
00316   resultGroupsToIntVec();
00317 
00318   save << " " << _toCopyMesh << " " << _toCopyGroups;
00319   save << " " << _resultGroupsStorage.size();
00320   for ( unsigned i = 0; i < _resultGroupsStorage.size(); ++i )
00321     save << " " << _resultGroupsStorage[i];
00322 
00323   return save;
00324 }
00325 
00326 //=============================================================================
00330 //=============================================================================
00331 
00332 istream & StdMeshers_ImportSource1D::LoadFrom(istream & load)
00333 {
00334   load >> _toCopyMesh >> _toCopyGroups;
00335 
00336   _resultGroupsStorage.clear();
00337   int val;
00338   if ( load >> val )
00339   {
00340     _resultGroupsStorage.reserve(val);
00341     while ( _resultGroupsStorage.size() < _resultGroupsStorage.capacity() && load >> val )
00342       _resultGroupsStorage.push_back( val );
00343   }
00344   return load;
00345 }
00346 
00347 //================================================================================
00351 //================================================================================
00352 
00353 void StdMeshers_ImportSource1D::resultGroupsToIntVec()
00354 {
00355   _resultGroupsStorage.clear();
00356   
00357   // store result groups
00358   TResGroupMap::iterator key2groups = _resultGroups.begin();
00359   for ( ; key2groups != _resultGroups.end(); ++key2groups )
00360   {
00361     const pair<int, int>&          key = key2groups->first;
00362     const vector<SMESH_Group*>& groups = key2groups->second;
00363     // mesh ids, nb groups
00364     _resultGroupsStorage.push_back( key.first );
00365     _resultGroupsStorage.push_back( key.second );
00366     _resultGroupsStorage.push_back( groups.size() );
00367     for ( unsigned i = 0; i < groups.size(); ++i )
00368     {
00369       // store group names as sequence of ints each standing for a char
00370       // of a name; that is to avoid pb with names containing white spaces
00371       string name = groups[i]->GetGroupDS()->GetStoreName();
00372       _resultGroupsStorage.push_back( name.size() );
00373       for ( unsigned j = 0; j < name.size(); ++j )
00374         _resultGroupsStorage.push_back( name[j] );
00375     }
00376   }
00377 }
00378 
00379 //================================================================================
00383 //================================================================================
00384 
00385 void StdMeshers_ImportSource1D::RestoreGroups(const std::vector<SMESH_Group*>& groups)
00386 {
00387   _groups = groups;
00388 
00389   _resultGroups.clear();
00390   int i = 0;
00391   while ( i < _resultGroupsStorage.size() )
00392   {
00393     int key1 = _resultGroupsStorage[i++];
00394     int key2 = _resultGroupsStorage[i++];
00395     pair<int, int> resMapKey( key1, key2 );
00396     SMESH_Mesh* mesh = getTgtMeshByKey( resMapKey, _gen->GetStudyContext(_studyId));
00397     // restore mesh ids at least
00398     _resultGroups.insert( make_pair (resMapKey,vector<SMESH_Group*>() )); 
00399 
00400     int nbGroups = _resultGroupsStorage[i++];
00401     for ( int j = 0; j < nbGroups; ++j )
00402     {
00403       string::size_type nameSize = _resultGroupsStorage[i++];
00404       string groupName(nameSize, '\0');
00405       for ( unsigned k = 0; k < nameSize; ++k )
00406         groupName[k] = (char) _resultGroupsStorage[i++];
00407 
00408       // find a group by name
00409       if ( mesh )
00410       {
00411         SMESH_Group* group = 0;
00412         SMESH_Mesh::GroupIteratorPtr gIt = mesh->GetGroups();
00413         while ( !group && gIt->more() )
00414         {
00415           group = gIt->next();
00416           if ( !group->GetGroupDS() || groupName != group->GetGroupDS()->GetStoreName() )
00417             group = 0;
00418         }
00419         if ( group )
00420           _resultGroups[ resMapKey ].push_back( group );
00421       }
00422     }
00423   }
00424 }
00425 
00426 //================================================================================
00433 //================================================================================
00434 
00435 void StdMeshers_ImportSource1D::StoreResultGroups(const std::vector<SMESH_Group*>& groups,
00436                                                   const SMESHDS_Mesh&              srcMesh,
00437                                                   const SMESHDS_Mesh&              tgtMesh)
00438 {
00439   _resultGroups[ getResMapKey(srcMesh,tgtMesh) ] = groups;
00440 }
00441 
00442 //================================================================================
00449 //================================================================================
00450 
00451 std::vector<SMESH_Group*>*
00452 StdMeshers_ImportSource1D::GetResultGroups(const SMESHDS_Mesh& srcMesh,
00453                                            const SMESHDS_Mesh& tgtMesh) 
00454 {
00455   TResGroupMap::iterator key2groups = _resultGroups.find( getResMapKey(srcMesh,tgtMesh ));
00456   if ( key2groups == _resultGroups.end() )
00457     return 0;
00458   vector<SMESH_Group*> vec = getValidGroups((*key2groups).second,
00459                                             _gen->GetStudyContext(_studyId) );
00460   if ( vec.size() != key2groups->second.size())
00461     key2groups->second = vec;
00462 
00463   return & key2groups->second;
00464 }
00465 
00466 //================================================================================
00473 //================================================================================
00474 
00475 bool StdMeshers_ImportSource1D::SetParametersByMesh(const SMESH_Mesh*, const TopoDS_Shape&)
00476 {
00477   return false;
00478 }
00479 
00480 //================================================================================
00485 //================================================================================
00486 
00487 bool StdMeshers_ImportSource1D::SetParametersByDefaults(const TDefaults&, const SMESH_Mesh* )
00488 {
00489   return false;
00490 }