Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "StdMeshers_ImportSource.hxx"
00028
00029 #include "SMESHDS_GroupBase.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;
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;
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
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
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
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
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 ostream & StdMeshers_ImportSource1D::SaveTo(ostream & save)
00268 {
00269 resultGroupsToIntVec();
00270
00271 save << " " << _toCopyMesh << " " << _toCopyGroups;
00272 save << " " << _resultGroupsStorage.size();
00273 for ( unsigned i = 0; i < _resultGroupsStorage.size(); ++i )
00274 save << " " << _resultGroupsStorage[i];
00275
00276 return save;
00277 }
00278
00279
00283
00284
00285 istream & StdMeshers_ImportSource1D::LoadFrom(istream & load)
00286 {
00287 load >> _toCopyMesh >> _toCopyGroups;
00288
00289 _resultGroupsStorage.clear();
00290 int val;
00291 if ( load >> val )
00292 {
00293 _resultGroupsStorage.reserve(val);
00294 while ( _resultGroupsStorage.size() < _resultGroupsStorage.capacity() && load >> val )
00295 _resultGroupsStorage.push_back( val );
00296 }
00297 return load;
00298 }
00299
00300
00304
00305
00306 void StdMeshers_ImportSource1D::resultGroupsToIntVec()
00307 {
00308 _resultGroupsStorage.clear();
00309
00310
00311 TResGroupMap::iterator key2groups = _resultGroups.begin();
00312 for ( ; key2groups != _resultGroups.end(); ++key2groups )
00313 {
00314 const pair<int, int>& key = key2groups->first;
00315 const vector<SMESH_Group*>& groups = key2groups->second;
00316
00317 _resultGroupsStorage.push_back( key.first );
00318 _resultGroupsStorage.push_back( key.second );
00319 _resultGroupsStorage.push_back( groups.size() );
00320 for ( unsigned i = 0; i < groups.size(); ++i )
00321 {
00322
00323
00324 string name = groups[i]->GetGroupDS()->GetStoreName();
00325 _resultGroupsStorage.push_back( name.size() );
00326 for ( unsigned j = 0; j < name.size(); ++j )
00327 _resultGroupsStorage.push_back( name[j] );
00328 }
00329 }
00330 }
00331
00332
00336
00337
00338 void StdMeshers_ImportSource1D::RestoreGroups(const std::vector<SMESH_Group*>& groups)
00339 {
00340 _groups = groups;
00341
00342 _resultGroups.clear();
00343 int i = 0;
00344 while ( i < _resultGroupsStorage.size() )
00345 {
00346 int key1 = _resultGroupsStorage[i++];
00347 int key2 = _resultGroupsStorage[i++];
00348 pair<int, int> resMapKey( key1, key2 );
00349 SMESH_Mesh* mesh = getTgtMeshByKey( resMapKey, _gen->GetStudyContext(_studyId));
00350
00351 _resultGroups.insert( make_pair (resMapKey,vector<SMESH_Group*>() ));
00352
00353 int nbGroups = _resultGroupsStorage[i++];
00354 for ( int j = 0; j < nbGroups; ++j )
00355 {
00356 string::size_type nameSize = _resultGroupsStorage[i++];
00357 string groupName(nameSize, '\0');
00358 for ( unsigned k = 0; k < nameSize; ++k )
00359 groupName[k] = (char) _resultGroupsStorage[i++];
00360
00361
00362 if ( mesh )
00363 {
00364 SMESH_Group* group = 0;
00365 SMESH_Mesh::GroupIteratorPtr gIt = mesh->GetGroups();
00366 while ( !group && gIt->more() )
00367 {
00368 group = gIt->next();
00369 if ( !group->GetGroupDS() || groupName != group->GetGroupDS()->GetStoreName() )
00370 group = 0;
00371 }
00372 if ( group )
00373 _resultGroups[ resMapKey ].push_back( group );
00374 }
00375 }
00376 }
00377 }
00378
00379
00386
00387
00388 void StdMeshers_ImportSource1D::StoreResultGroups(const std::vector<SMESH_Group*>& groups,
00389 const SMESHDS_Mesh& srcMesh,
00390 const SMESHDS_Mesh& tgtMesh)
00391 {
00392 _resultGroups[ getResMapKey(srcMesh,tgtMesh) ] = groups;
00393 }
00394
00395
00402
00403
00404 std::vector<SMESH_Group*>*
00405 StdMeshers_ImportSource1D::GetResultGroups(const SMESHDS_Mesh& srcMesh,
00406 const SMESHDS_Mesh& tgtMesh)
00407 {
00408 TResGroupMap::iterator key2groups = _resultGroups.find( getResMapKey(srcMesh,tgtMesh ));
00409 if ( key2groups == _resultGroups.end() )
00410 return 0;
00411 vector<SMESH_Group*> vec = getValidGroups((*key2groups).second,
00412 _gen->GetStudyContext(_studyId) );
00413 if ( vec.size() != key2groups->second.size())
00414 key2groups->second = vec;
00415
00416 return & key2groups->second;
00417 }
00418
00419
00426
00427
00428 bool StdMeshers_ImportSource1D::SetParametersByMesh(const SMESH_Mesh*, const TopoDS_Shape&)
00429 {
00430 return false;
00431 }
00432
00433
00438
00439
00440 bool StdMeshers_ImportSource1D::SetParametersByDefaults(const TDefaults&, const SMESH_Mesh* )
00441 {
00442 return false;
00443 }