Salome HOME
Merge from V6_4_BR 05/12/2011
[modules/smesh.git] / src / SMESH / SMESH_Mesh.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 SMESH : implementaion of SMESH idl descriptions
24 //  File   : SMESH_Mesh.cxx
25 //  Author : Paul RASCLE, EDF
26 //  Module : SMESH
27 //
28 #include "SMESH_Mesh.hxx"
29 #include "SMESH_subMesh.hxx"
30 #include "SMESH_Gen.hxx"
31 #include "SMESH_Hypothesis.hxx"
32 #include "SMESH_Group.hxx"
33 #include "SMESH_HypoFilter.hxx"
34 #include "SMESHDS_Group.hxx"
35 #include "SMESHDS_Script.hxx"
36 #include "SMESHDS_GroupOnGeom.hxx"
37 #include "SMESHDS_Document.hxx"
38 #include "SMDS_MeshVolume.hxx"
39 #include "SMDS_SetIterator.hxx"
40
41 #include "utilities.h"
42
43 #include "DriverMED_W_SMESHDS_Mesh.h"
44 #include "DriverDAT_W_SMDS_Mesh.h"
45 #include "DriverUNV_W_SMDS_Mesh.h"
46 #include "DriverSTL_W_SMDS_Mesh.h"
47
48 #include "DriverMED_R_SMESHDS_Mesh.h"
49 #include "DriverUNV_R_SMDS_Mesh.h"
50 #include "DriverSTL_R_SMDS_Mesh.h"
51 #ifdef WITH_CGNS
52 #include "DriverCGNS_Read.hxx"
53 #include "DriverCGNS_Write.hxx"
54 #endif
55
56 #undef _Precision_HeaderFile
57 #include <BRepBndLib.hxx>
58 #include <BRepPrimAPI_MakeBox.hxx>
59 #include <Bnd_Box.hxx>
60 #include <TopExp.hxx>
61 #include <TopExp_Explorer.hxx>
62 #include <TopTools_ListIteratorOfListOfShape.hxx>
63 #include <TopTools_ListOfShape.hxx>
64 #include <TopTools_MapOfShape.hxx>
65 #include <TopoDS_Iterator.hxx>
66
67 #include "Utils_ExceptHandlers.hxx"
68
69 using namespace std;
70
71 // maximum stored group name length in MED file
72 #define MAX_MED_GROUP_NAME_LENGTH 80
73
74 #ifdef _DEBUG_
75 static int MYDEBUG = 0;
76 #else
77 static int MYDEBUG = 0;
78 #endif
79
80 #define cSMESH_Hyp(h) static_cast<const SMESH_Hypothesis*>(h)
81
82 typedef SMESH_HypoFilter THypType;
83
84 //=============================================================================
85 /*!
86  * 
87  */
88 //=============================================================================
89
90 SMESH_Mesh::SMESH_Mesh(int               theLocalId, 
91                        int               theStudyId, 
92                        SMESH_Gen*        theGen,
93                        bool              theIsEmbeddedMode,
94                        SMESHDS_Document* theDocument):
95   _groupId( 0 ), _nbSubShapes( 0 )
96 {
97   MESSAGE("SMESH_Mesh::SMESH_Mesh(int localId)");
98   _id            = theLocalId;
99   _studyId       = theStudyId;
100   _gen           = theGen;
101   _myDocument    = theDocument;
102   _idDoc         = theDocument->NewMesh(theIsEmbeddedMode);
103   _myMeshDS      = theDocument->GetMesh(_idDoc);
104   _isShapeToMesh = false;
105   _isAutoColor   = false;
106   _isModified    = false;
107   _shapeDiagonal = 0.0;
108   _rmGroupCallUp = 0;
109   _myMeshDS->ShapeToMesh( PseudoShape() );
110 }
111
112 //================================================================================
113 /*!
114  * \brief Constructor of SMESH_Mesh being a base of some descendant class
115  */
116 //================================================================================
117
118 SMESH_Mesh::SMESH_Mesh():
119   _groupId( 0 ), _nbSubShapes( 0 )
120 {
121   _myMeshDS      = 0;
122   _isShapeToMesh = false;
123   _isAutoColor   = false;
124   _isModified    = false;
125   _shapeDiagonal = 0.0;
126   _rmGroupCallUp = 0;
127 }
128
129 //=============================================================================
130 /*!
131  * 
132  */
133 //=============================================================================
134
135 SMESH_Mesh::~SMESH_Mesh()
136 {
137   MESSAGE("SMESH_Mesh::~SMESH_Mesh");
138
139   // issue 0020340: EDF 1022 SMESH : Crash with FindNodeClosestTo in a second new study
140   //   Notify event listeners at least that something happens
141   if ( SMESH_subMesh * sm = GetSubMeshContaining(1))
142     sm->ComputeStateEngine( SMESH_subMesh::MESH_ENTITY_REMOVED );
143
144   // delete groups
145   map < int, SMESH_Group * >::iterator itg;
146   for (itg = _mapGroup.begin(); itg != _mapGroup.end(); itg++) {
147     SMESH_Group *aGroup = (*itg).second;
148     delete aGroup;
149   }
150   _mapGroup.clear();
151
152   if ( _rmGroupCallUp) delete _rmGroupCallUp;
153   _rmGroupCallUp = 0;
154 }
155
156 //=============================================================================
157 /*!
158  * \brief Set geometry to be meshed
159  */
160 //=============================================================================
161
162 void SMESH_Mesh::ShapeToMesh(const TopoDS_Shape & aShape)
163 {
164   if(MYDEBUG) MESSAGE("SMESH_Mesh::ShapeToMesh");
165
166   if ( !aShape.IsNull() && _isShapeToMesh ) {
167     if ( aShape.ShapeType() != TopAbs_COMPOUND && // group contents is allowed to change
168          _myMeshDS->ShapeToMesh().ShapeType() != TopAbs_COMPOUND )
169       throw SALOME_Exception(LOCALIZED ("a shape to mesh has already been defined"));
170   }
171   // clear current data
172   if ( !_myMeshDS->ShapeToMesh().IsNull() )
173   {
174     // removal of a shape to mesh, delete objects referring to sub-shapes:
175     // - sub-meshes
176     map <int, SMESH_subMesh *>::iterator i_sm = _mapSubMesh.begin();
177     for ( ; i_sm != _mapSubMesh.end(); ++i_sm )
178       delete i_sm->second;
179     _mapSubMesh.clear();
180     //  - groups on geometry
181     map <int, SMESH_Group *>::iterator i_gr = _mapGroup.begin();
182     while ( i_gr != _mapGroup.end() ) {
183       if ( dynamic_cast<SMESHDS_GroupOnGeom*>( i_gr->second->GetGroupDS() )) {
184         _myMeshDS->RemoveGroup( i_gr->second->GetGroupDS() );
185         delete i_gr->second;
186         _mapGroup.erase( i_gr++ );
187       }
188       else
189         i_gr++;
190     }
191     _mapAncestors.Clear();
192
193     // clear SMESHDS
194     TopoDS_Shape aNullShape;
195     _myMeshDS->ShapeToMesh( aNullShape );
196
197     _shapeDiagonal = 0.0;
198   }
199
200   // set a new geometry
201   if ( !aShape.IsNull() )
202   {
203     _myMeshDS->ShapeToMesh(aShape);
204     _isShapeToMesh = true;
205     _nbSubShapes = _myMeshDS->MaxShapeIndex();
206
207     // fill map of ancestors
208     fillAncestorsMap(aShape);
209   }
210   else
211   {
212     _isShapeToMesh = false;
213     _shapeDiagonal = 0.0;
214     _myMeshDS->ShapeToMesh( PseudoShape() );
215   }
216   _isModified = false;
217 }
218
219 //=======================================================================
220 /*!
221  * \brief Return geometry to be meshed. (It may be a PseudoShape()!)
222  */
223 //=======================================================================
224
225 TopoDS_Shape SMESH_Mesh::GetShapeToMesh() const
226 {
227   return _myMeshDS->ShapeToMesh();
228 }
229
230 //=======================================================================
231 /*!
232  * \brief Return a solid which is returned by GetShapeToMesh() if
233  *        a real geometry to be meshed was not set
234  */
235 //=======================================================================
236
237 const TopoDS_Solid& SMESH_Mesh::PseudoShape()
238 {
239   static TopoDS_Solid aSolid;
240   if ( aSolid.IsNull() )
241   {
242     aSolid = BRepPrimAPI_MakeBox(1,1,1);
243   }
244   return aSolid;
245 }
246
247 //=======================================================================
248 /*!
249  * \brief Return diagonal size of bounding box of a shape
250  */
251 //=======================================================================
252
253 double SMESH_Mesh::GetShapeDiagonalSize(const TopoDS_Shape & aShape)
254 {
255   if ( !aShape.IsNull() ) {
256     Bnd_Box Box;
257     BRepBndLib::Add(aShape, Box);
258     return sqrt( Box.SquareExtent() );
259   }
260   return 0;
261 }
262
263 //=======================================================================
264 /*!
265  * \brief Return diagonal size of bounding box of shape to mesh
266  */
267 //=======================================================================
268
269 double SMESH_Mesh::GetShapeDiagonalSize() const
270 {
271   if ( _shapeDiagonal == 0. && _isShapeToMesh )
272     const_cast<SMESH_Mesh*>(this)->_shapeDiagonal = GetShapeDiagonalSize( GetShapeToMesh() );
273
274   return _shapeDiagonal;
275 }
276
277 //=======================================================================
278 /*!
279  * \brief Remove all nodes and elements
280  */
281 //=======================================================================
282
283 void SMESH_Mesh::Clear()
284 {
285   // clear mesh data
286   _myMeshDS->ClearMesh();
287
288   // update compute state of submeshes
289   if ( SMESH_subMesh *sm = GetSubMeshContaining( GetShapeToMesh() ) )
290   {
291     sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
292     sm->ComputeSubMeshStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
293     sm->ComputeStateEngine( SMESH_subMesh::CLEAN ); // for event listeners (issue 0020918)
294     sm->ComputeSubMeshStateEngine( SMESH_subMesh::CLEAN );
295   }
296   _isModified = false;
297 }
298
299 //=======================================================================
300 /*!
301  * \brief Remove all nodes and elements of indicated shape
302  */
303 //=======================================================================
304
305 void SMESH_Mesh::ClearSubMesh(const int theShapeId)
306 {
307   // clear sub-meshes; get ready to re-compute as a side-effect 
308   if ( SMESH_subMesh *sm = GetSubMeshContaining( theShapeId ) )
309   {
310     SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(/*includeSelf=*/true,
311                                                              /*complexShapeFirst=*/false);
312     while ( smIt->more() )
313     {
314       sm = smIt->next();
315       TopAbs_ShapeEnum shapeType = sm->GetSubShape().ShapeType();      
316       if ( shapeType == TopAbs_VERTEX || shapeType < TopAbs_SOLID )
317         // all other shapes depends on vertices so they are already cleaned
318         sm->ComputeStateEngine( SMESH_subMesh::CLEAN );
319       // to recompute even if failed
320       sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
321     }
322   }
323 }
324
325 //=======================================================================
326 //function : UNVToMesh
327 //purpose  : 
328 //=======================================================================
329
330 int SMESH_Mesh::UNVToMesh(const char* theFileName)
331 {
332   if(MYDEBUG) MESSAGE("UNVToMesh - theFileName = "<<theFileName);
333   if(_isShapeToMesh)
334     throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
335   _isShapeToMesh = false;
336   DriverUNV_R_SMDS_Mesh myReader;
337   myReader.SetMesh(_myMeshDS);
338   myReader.SetFile(theFileName);
339   myReader.SetMeshId(-1);
340   myReader.Perform();
341   if(MYDEBUG){
342     MESSAGE("UNVToMesh - _myMeshDS->NbNodes() = "<<_myMeshDS->NbNodes());
343     MESSAGE("UNVToMesh - _myMeshDS->NbEdges() = "<<_myMeshDS->NbEdges());
344     MESSAGE("UNVToMesh - _myMeshDS->NbFaces() = "<<_myMeshDS->NbFaces());
345     MESSAGE("UNVToMesh - _myMeshDS->NbVolumes() = "<<_myMeshDS->NbVolumes());
346   }
347   SMDS_MeshGroup* aGroup = (SMDS_MeshGroup*) myReader.GetGroup();
348   if (aGroup != 0) {
349     TGroupNamesMap aGroupNames = myReader.GetGroupNamesMap();
350     //const TGroupIdMap& aGroupId = myReader.GetGroupIdMap();
351     aGroup->InitSubGroupsIterator();
352     while (aGroup->MoreSubGroups()) {
353       SMDS_MeshGroup* aSubGroup = (SMDS_MeshGroup*) aGroup->NextSubGroup();
354       string aName = aGroupNames[aSubGroup];
355       int aId;
356
357       SMESH_Group* aSMESHGroup = AddGroup( aSubGroup->GetType(), aName.c_str(), aId );
358       if ( aSMESHGroup ) {
359         if(MYDEBUG) MESSAGE("UNVToMesh - group added: "<<aName);      
360         SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( aSMESHGroup->GetGroupDS() );
361         if ( aGroupDS ) {
362           aGroupDS->SetStoreName(aName.c_str());
363           aSubGroup->InitIterator();
364           const SMDS_MeshElement* aElement = 0;
365           while (aSubGroup->More()) {
366             aElement = aSubGroup->Next();
367             if (aElement) {
368               aGroupDS->SMDSGroup().Add(aElement);
369             }
370           }
371           if (aElement)
372             aGroupDS->SetType(aElement->GetType());
373         }
374       }
375     }
376   }
377   return 1;
378 }
379
380 //=======================================================================
381 //function : MEDToMesh
382 //purpose  : 
383 //=======================================================================
384
385 int SMESH_Mesh::MEDToMesh(const char* theFileName, const char* theMeshName)
386 {
387   if(MYDEBUG) MESSAGE("MEDToMesh - theFileName = "<<theFileName<<", mesh name = "<<theMeshName);
388   if(_isShapeToMesh)
389     throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
390   _isShapeToMesh = false;
391   DriverMED_R_SMESHDS_Mesh myReader;
392   myReader.SetMesh(_myMeshDS);
393   myReader.SetMeshId(-1);
394   myReader.SetFile(theFileName);
395   myReader.SetMeshName(theMeshName);
396   Driver_Mesh::Status status = myReader.Perform();
397   if(MYDEBUG){
398     MESSAGE("MEDToMesh - _myMeshDS->NbNodes() = "<<_myMeshDS->NbNodes());
399     MESSAGE("MEDToMesh - _myMeshDS->NbEdges() = "<<_myMeshDS->NbEdges());
400     MESSAGE("MEDToMesh - _myMeshDS->NbFaces() = "<<_myMeshDS->NbFaces());
401     MESSAGE("MEDToMesh - _myMeshDS->NbVolumes() = "<<_myMeshDS->NbVolumes());
402   }
403
404   // Reading groups (sub-meshes are out of scope of MED import functionality)
405   list<TNameAndType> aGroupNames = myReader.GetGroupNamesAndTypes();
406   if(MYDEBUG) MESSAGE("MEDToMesh - Nb groups = "<<aGroupNames.size()); 
407   int anId;
408   list<TNameAndType>::iterator name_type = aGroupNames.begin();
409   for ( ; name_type != aGroupNames.end(); name_type++ ) {
410     SMESH_Group* aGroup = AddGroup( name_type->second, name_type->first.c_str(), anId );
411     if ( aGroup ) {
412       if(MYDEBUG) MESSAGE("MEDToMesh - group added: "<<name_type->first.c_str());      
413       SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( aGroup->GetGroupDS() );
414       if ( aGroupDS ) {
415         aGroupDS->SetStoreName( name_type->first.c_str() );
416         myReader.GetGroup( aGroupDS );
417       }
418     }
419   }
420   return (int) status;
421 }
422
423 //=======================================================================
424 //function : STLToMesh
425 //purpose  : 
426 //=======================================================================
427
428 int SMESH_Mesh::STLToMesh(const char* theFileName)
429 {
430   if(MYDEBUG) MESSAGE("STLToMesh - theFileName = "<<theFileName);
431   if(_isShapeToMesh)
432     throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
433   _isShapeToMesh = false;
434   DriverSTL_R_SMDS_Mesh myReader;
435   myReader.SetMesh(_myMeshDS);
436   myReader.SetFile(theFileName);
437   myReader.SetMeshId(-1);
438   myReader.Perform();
439   if(MYDEBUG){
440     MESSAGE("STLToMesh - _myMeshDS->NbNodes() = "<<_myMeshDS->NbNodes());
441     MESSAGE("STLToMesh - _myMeshDS->NbEdges() = "<<_myMeshDS->NbEdges());
442     MESSAGE("STLToMesh - _myMeshDS->NbFaces() = "<<_myMeshDS->NbFaces());
443     MESSAGE("STLToMesh - _myMeshDS->NbVolumes() = "<<_myMeshDS->NbVolumes());
444   }
445   return 1;
446 }
447
448 //================================================================================
449 /*!
450  * \brief Reads the given mesh from the CGNS file
451  *  \param theFileName - name of the file
452  *  \retval int - Driver_Mesh::Status
453  */
454 //================================================================================
455
456 int SMESH_Mesh::CGNSToMesh(const char*  theFileName,
457                            const int    theMeshIndex,
458                            std::string& theMeshName)
459 {
460   int res = Driver_Mesh::DRS_FAIL;
461 #ifdef WITH_CGNS
462
463   DriverCGNS_Read myReader;
464   myReader.SetMesh(_myMeshDS);
465   myReader.SetFile(theFileName);
466   myReader.SetMeshId(theMeshIndex);
467   res = myReader.Perform();
468   theMeshName = myReader.GetMeshName();
469
470   // create groups
471   SynchronizeGroups();
472
473 #endif
474   return res;
475 }
476
477 //=============================================================================
478 /*!
479  * 
480  */
481 //=============================================================================
482
483 SMESH_Hypothesis::Hypothesis_Status
484   SMESH_Mesh::AddHypothesis(const TopoDS_Shape & aSubShape,
485                             int                  anHypId  ) throw(SALOME_Exception)
486 {
487   Unexpect aCatch(SalomeException);
488   if(MYDEBUG) MESSAGE("SMESH_Mesh::AddHypothesis");
489
490   SMESH_subMesh *subMesh = GetSubMesh(aSubShape);
491   if ( !subMesh || !subMesh->GetId())
492     return SMESH_Hypothesis::HYP_BAD_SUBSHAPE;
493
494   StudyContextStruct *sc = _gen->GetStudyContext(_studyId);
495   if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
496   {
497     if(MYDEBUG) MESSAGE("Hypothesis ID does not give an hypothesis");
498     if(MYDEBUG) {
499       SCRUTE(_studyId);
500       SCRUTE(anHypId);
501     }
502     throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
503   }
504
505   SMESH_Hypothesis *anHyp = sc->mapHypothesis[anHypId];
506   MESSAGE( "SMESH_Mesh::AddHypothesis " << anHyp->GetName() );
507
508   bool isGlobalHyp = IsMainShape( aSubShape );
509
510   // NotConformAllowed can be only global
511   if ( !isGlobalHyp )
512   {
513     // NOTE: this is not a correct way to check a name of hypothesis,
514     // there should be an attribute of hypothesis saying that it can/can't
515     // be global/local
516     string hypName = anHyp->GetName();
517     if ( hypName == "NotConformAllowed" )
518     {
519       if(MYDEBUG) MESSAGE( "Hypotesis <NotConformAllowed> can be only global" );
520       return SMESH_Hypothesis::HYP_INCOMPATIBLE;
521     }
522   }
523
524   // shape 
525
526   bool isAlgo = ( !anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO );
527   int event = isAlgo ? SMESH_subMesh::ADD_ALGO : SMESH_subMesh::ADD_HYP;
528
529   SMESH_Hypothesis::Hypothesis_Status ret = subMesh->AlgoStateEngine(event, anHyp);
530
531   // subShapes
532   if (!SMESH_Hypothesis::IsStatusFatal(ret) &&
533       anHyp->GetDim() <= SMESH_Gen::GetShapeDim(aSubShape)) // is added on father
534   {
535     event = isAlgo ? SMESH_subMesh::ADD_FATHER_ALGO : SMESH_subMesh::ADD_FATHER_HYP;
536
537     SMESH_Hypothesis::Hypothesis_Status ret2 =
538       subMesh->SubMeshesAlgoStateEngine(event, anHyp);
539     if (ret2 > ret)
540       ret = ret2;
541
542     // check concurent hypotheses on ancestors
543     if (ret < SMESH_Hypothesis::HYP_CONCURENT && !isGlobalHyp )
544     {
545       SMESH_subMeshIteratorPtr smIt = subMesh->getDependsOnIterator(false,false);
546       while ( smIt->more() ) {
547         SMESH_subMesh* sm = smIt->next();
548         if ( sm->IsApplicableHypotesis( anHyp )) {
549           ret2 = sm->CheckConcurentHypothesis( anHyp->GetType() );
550           if (ret2 > ret) {
551             ret = ret2;
552             break;
553           }
554         }
555       }
556     }
557   }
558   HasModificationsToDiscard(); // to reset _isModified flag if a mesh becomes empty
559
560   GetMeshDS()->Modified();
561
562   if(MYDEBUG) subMesh->DumpAlgoState(true);
563   if(MYDEBUG) SCRUTE(ret);
564   return ret;
565 }
566
567 //=============================================================================
568 /*!
569  * 
570  */
571 //=============================================================================
572
573 SMESH_Hypothesis::Hypothesis_Status
574   SMESH_Mesh::RemoveHypothesis(const TopoDS_Shape & aSubShape,
575                                int anHypId)throw(SALOME_Exception)
576 {
577   Unexpect aCatch(SalomeException);
578   if(MYDEBUG) MESSAGE("SMESH_Mesh::RemoveHypothesis");
579   
580   StudyContextStruct *sc = _gen->GetStudyContext(_studyId);
581   if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
582     throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
583   
584   SMESH_Hypothesis *anHyp = sc->mapHypothesis[anHypId];
585   if(MYDEBUG) {
586     int hypType = anHyp->GetType();
587     SCRUTE(hypType);
588   }
589   
590   // shape 
591   
592   bool isAlgo = ( !anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO );
593   int event = isAlgo ? SMESH_subMesh::REMOVE_ALGO : SMESH_subMesh::REMOVE_HYP;
594
595   SMESH_subMesh *subMesh = GetSubMesh(aSubShape);
596
597   SMESH_Hypothesis::Hypothesis_Status ret = subMesh->AlgoStateEngine(event, anHyp);
598
599   // there may appear concurrent hyps that were covered by the removed hyp
600   if (ret < SMESH_Hypothesis::HYP_CONCURENT &&
601       subMesh->IsApplicableHypotesis( anHyp ) &&
602       subMesh->CheckConcurentHypothesis( anHyp->GetType() ) != SMESH_Hypothesis::HYP_OK)
603     ret = SMESH_Hypothesis::HYP_CONCURENT;
604
605   // subShapes
606   if (!SMESH_Hypothesis::IsStatusFatal(ret) &&
607       anHyp->GetDim() <= SMESH_Gen::GetShapeDim(aSubShape)) // is removed from father
608   {
609     event = isAlgo ? SMESH_subMesh::REMOVE_FATHER_ALGO : SMESH_subMesh::REMOVE_FATHER_HYP;
610
611     SMESH_Hypothesis::Hypothesis_Status ret2 =
612       subMesh->SubMeshesAlgoStateEngine(event, anHyp);
613     if (ret2 > ret) // more severe
614       ret = ret2;
615
616     // check concurent hypotheses on ancestors
617     if (ret < SMESH_Hypothesis::HYP_CONCURENT && !IsMainShape( aSubShape ) )
618     {
619       SMESH_subMeshIteratorPtr smIt = subMesh->getDependsOnIterator(false,false);
620       while ( smIt->more() ) {
621         SMESH_subMesh* sm = smIt->next();
622         if ( sm->IsApplicableHypotesis( anHyp )) {
623           ret2 = sm->CheckConcurentHypothesis( anHyp->GetType() );
624           if (ret2 > ret) {
625             ret = ret2;
626             break;
627           }
628         }
629       }
630     }
631   }
632
633   HasModificationsToDiscard(); // to reset _isModified flag if mesh become empty
634
635   GetMeshDS()->Modified();
636
637   if(MYDEBUG) subMesh->DumpAlgoState(true);
638   if(MYDEBUG) SCRUTE(ret);
639   return ret;
640 }
641
642 //=============================================================================
643 /*!
644  * 
645  */
646 //=============================================================================
647
648 const list<const SMESHDS_Hypothesis*>&
649 SMESH_Mesh::GetHypothesisList(const TopoDS_Shape & aSubShape) const
650   throw(SALOME_Exception)
651 {
652   Unexpect aCatch(SalomeException);
653   return _myMeshDS->GetHypothesis(aSubShape);
654 }
655
656 //=======================================================================
657 /*!
658  * \brief Return the hypothesis assigned to the shape
659  *  \param aSubShape    - the shape to check
660  *  \param aFilter      - the hypothesis filter
661  *  \param andAncestors - flag to check hypos assigned to ancestors of the shape
662  *  \param assignedTo   - to return the shape the found hypo is assigned to
663  *  \retval SMESH_Hypothesis* - the first hypo passed through aFilter
664  */
665 //=======================================================================
666
667 const SMESH_Hypothesis * SMESH_Mesh::GetHypothesis(const TopoDS_Shape &    aSubShape,
668                                                    const SMESH_HypoFilter& aFilter,
669                                                    const bool              andAncestors,
670                                                    TopoDS_Shape*           assignedTo) const
671 {
672   {
673     const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(aSubShape);
674     list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
675     for ( ; hyp != hypList.end(); hyp++ ) {
676       const SMESH_Hypothesis * h = cSMESH_Hyp( *hyp );
677       if ( aFilter.IsOk( h, aSubShape)) {
678         if ( assignedTo ) *assignedTo = aSubShape;
679         return h;
680       }
681     }
682   }
683   if ( andAncestors )
684   {
685     // user sorted submeshes of ancestors, according to stored submesh priority
686     const list<SMESH_subMesh*> smList = getAncestorsSubMeshes( aSubShape );
687     list<SMESH_subMesh*>::const_iterator smIt = smList.begin(); 
688     for ( ; smIt != smList.end(); smIt++ )
689     {
690       const TopoDS_Shape& curSh = (*smIt)->GetSubShape();
691       const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(curSh);
692       list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
693       for ( ; hyp != hypList.end(); hyp++ ) {
694         const SMESH_Hypothesis * h = cSMESH_Hyp( *hyp );
695         if (aFilter.IsOk( h, curSh )) {
696           if ( assignedTo ) *assignedTo = curSh;
697           return h;
698         }
699       }
700     }
701   }
702   return 0;
703 }
704
705 //================================================================================
706 /*!
707  * \brief Return hypothesis assigned to the shape
708   * \param aSubShape - the shape to check
709   * \param aFilter - the hypothesis filter
710   * \param aHypList - the list of the found hypotheses
711   * \param andAncestors - flag to check hypos assigned to ancestors of the shape
712   * \retval int - number of unique hypos in aHypList
713  */
714 //================================================================================
715
716 int SMESH_Mesh::GetHypotheses(const TopoDS_Shape &                aSubShape,
717                               const SMESH_HypoFilter&             aFilter,
718                               list <const SMESHDS_Hypothesis * >& aHypList,
719                               const bool                          andAncestors) const
720 {
721   set<string> hypTypes; // to exclude same type hypos from the result list
722   int nbHyps = 0;
723
724   // only one main hypothesis is allowed
725   bool mainHypFound = false;
726
727   // fill in hypTypes
728   list<const SMESHDS_Hypothesis*>::const_iterator hyp;
729   for ( hyp = aHypList.begin(); hyp != aHypList.end(); hyp++ ) {
730     if ( hypTypes.insert( (*hyp)->GetName() ).second )
731       nbHyps++;
732     if ( !cSMESH_Hyp(*hyp)->IsAuxiliary() )
733       mainHypFound = true;
734   }
735
736   // get hypos from aSubShape
737   {
738     const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(aSubShape);
739     for ( hyp = hypList.begin(); hyp != hypList.end(); hyp++ )
740       if ( aFilter.IsOk (cSMESH_Hyp( *hyp ), aSubShape) &&
741            ( cSMESH_Hyp(*hyp)->IsAuxiliary() || !mainHypFound ) &&
742            hypTypes.insert( (*hyp)->GetName() ).second )
743       {
744         aHypList.push_back( *hyp );
745         nbHyps++;
746         if ( !cSMESH_Hyp(*hyp)->IsAuxiliary() )
747           mainHypFound = true;
748       }
749   }
750
751   // get hypos from ancestors of aSubShape
752   if ( andAncestors )
753   {
754     TopTools_MapOfShape map;
755
756     // user sorted submeshes of ancestors, according to stored submesh priority
757     const list<SMESH_subMesh*> smList = getAncestorsSubMeshes( aSubShape );
758     list<SMESH_subMesh*>::const_iterator smIt = smList.begin(); 
759     for ( ; smIt != smList.end(); smIt++ )
760     {
761       const TopoDS_Shape& curSh = (*smIt)->GetSubShape();
762      if ( !map.Add( curSh ))
763         continue;
764       const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(curSh);
765       for ( hyp = hypList.begin(); hyp != hypList.end(); hyp++ )
766         if (aFilter.IsOk( cSMESH_Hyp( *hyp ), curSh ) &&
767             ( cSMESH_Hyp(*hyp)->IsAuxiliary() || !mainHypFound ) &&
768             hypTypes.insert( (*hyp)->GetName() ).second )
769         {
770           aHypList.push_back( *hyp );
771           nbHyps++;
772           if ( !cSMESH_Hyp(*hyp)->IsAuxiliary() )
773             mainHypFound = true;
774         }
775     }
776   }
777   return nbHyps;
778 }
779
780 //=============================================================================
781 /*!
782  * 
783  */
784 //=============================================================================
785
786 const list<SMESHDS_Command*> & SMESH_Mesh::GetLog() throw(SALOME_Exception)
787 {
788   Unexpect aCatch(SalomeException);
789   if(MYDEBUG) MESSAGE("SMESH_Mesh::GetLog");
790   return _myMeshDS->GetScript()->GetCommands();
791 }
792
793 //=============================================================================
794 /*!
795  * 
796  */
797 //=============================================================================
798 void SMESH_Mesh::ClearLog() throw(SALOME_Exception)
799 {
800   Unexpect aCatch(SalomeException);
801   if(MYDEBUG) MESSAGE("SMESH_Mesh::ClearLog");
802   _myMeshDS->GetScript()->Clear();
803 }
804
805 //=============================================================================
806 /*!
807  * Get or Create the SMESH_subMesh object implementation
808  */
809 //=============================================================================
810
811 SMESH_subMesh *SMESH_Mesh::GetSubMesh(const TopoDS_Shape & aSubShape)
812   throw(SALOME_Exception)
813 {
814   Unexpect aCatch(SalomeException);
815   SMESH_subMesh *aSubMesh;
816   int index = _myMeshDS->ShapeToIndex(aSubShape);
817
818   // for submeshes on GEOM Group
819   if (( !index || index > _nbSubShapes ) && aSubShape.ShapeType() == TopAbs_COMPOUND ) {
820     TopoDS_Iterator it( aSubShape );
821     if ( it.More() )
822     {
823       index = _myMeshDS->AddCompoundSubmesh( aSubShape, it.Value().ShapeType() );
824       if ( index > _nbSubShapes ) _nbSubShapes = index; // not to create sm for this group again
825
826       // fill map of Ancestors
827       fillAncestorsMap(aSubShape);
828     }
829   }
830 //   if ( !index )
831 //     return NULL; // neither sub-shape nor a group
832
833   map <int, SMESH_subMesh *>::iterator i_sm = _mapSubMesh.find(index);
834   if ( i_sm != _mapSubMesh.end())
835   {
836     aSubMesh = i_sm->second;
837   }
838   else
839   {
840     aSubMesh = new SMESH_subMesh(index, this, _myMeshDS, aSubShape);
841     _mapSubMesh[index] = aSubMesh;
842     ClearMeshOrder();
843   }
844   return aSubMesh;
845 }
846
847 //=============================================================================
848 /*!
849  * Get the SMESH_subMesh object implementation. Dont create it, return null
850  * if it does not exist.
851  */
852 //=============================================================================
853
854 SMESH_subMesh *SMESH_Mesh::GetSubMeshContaining(const TopoDS_Shape & aSubShape) const
855   throw(SALOME_Exception)
856 {
857   Unexpect aCatch(SalomeException);
858   SMESH_subMesh *aSubMesh = NULL;
859   
860   int index = _myMeshDS->ShapeToIndex(aSubShape);
861
862   map <int, SMESH_subMesh *>::const_iterator i_sm = _mapSubMesh.find(index);
863   if ( i_sm != _mapSubMesh.end())
864     aSubMesh = i_sm->second;
865
866   return aSubMesh;
867 }
868 //=============================================================================
869 /*!
870  * Get the SMESH_subMesh object implementation. Dont create it, return null
871  * if it does not exist.
872  */
873 //=============================================================================
874
875 SMESH_subMesh *SMESH_Mesh::GetSubMeshContaining(const int aShapeID) const
876 throw(SALOME_Exception)
877 {
878   Unexpect aCatch(SalomeException);
879   
880   map <int, SMESH_subMesh *>::const_iterator i_sm = _mapSubMesh.find(aShapeID);
881   if (i_sm == _mapSubMesh.end())
882     return NULL;
883   return i_sm->second;
884 }
885 //================================================================================
886 /*!
887  * \brief Return submeshes of groups containing the given subshape
888  */
889 //================================================================================
890
891 list<SMESH_subMesh*>
892 SMESH_Mesh::GetGroupSubMeshesContaining(const TopoDS_Shape & aSubShape) const
893   throw(SALOME_Exception)
894 {
895   Unexpect aCatch(SalomeException);
896   list<SMESH_subMesh*> found;
897
898   SMESH_subMesh * subMesh = GetSubMeshContaining(aSubShape);
899   if ( !subMesh )
900     return found;
901
902   // submeshes of groups have max IDs, so search from the map end
903   map<int, SMESH_subMesh *>::const_reverse_iterator i_sm;
904   for ( i_sm = _mapSubMesh.rbegin(); i_sm != _mapSubMesh.rend(); ++i_sm) {
905     SMESHDS_SubMesh * ds = i_sm->second->GetSubMeshDS();
906     if ( ds && ds->IsComplexSubmesh() ) {
907       TopExp_Explorer exp( i_sm->second->GetSubShape(), aSubShape.ShapeType() );
908       for ( ; exp.More(); exp.Next() ) {
909         if ( aSubShape.IsSame( exp.Current() )) {
910           found.push_back( i_sm->second );
911           break;
912         }
913       }
914     } else {
915       break;
916     }
917   }
918   return found;
919 }
920 //=======================================================================
921 //function : IsUsedHypothesis
922 //purpose  : Return True if anHyp is used to mesh aSubShape
923 //=======================================================================
924
925 bool SMESH_Mesh::IsUsedHypothesis(SMESHDS_Hypothesis * anHyp,
926                                   const SMESH_subMesh* aSubMesh)
927 {
928   SMESH_Hypothesis* hyp = static_cast<SMESH_Hypothesis*>(anHyp);
929
930   // check if anHyp can be used to mesh aSubMesh
931   if ( !aSubMesh || !aSubMesh->IsApplicableHypotesis( hyp ))
932     return false;
933
934   const TopoDS_Shape & aSubShape = const_cast<SMESH_subMesh*>( aSubMesh )->GetSubShape();
935
936   SMESH_Algo *algo = _gen->GetAlgo(*this, aSubShape );
937
938   // algorithm
939   if (anHyp->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
940     return ( anHyp == algo );
941
942   // algorithm parameter
943   if (algo)
944   {
945     // look trough hypotheses used by algo
946     SMESH_HypoFilter hypoKind;
947     if ( algo->InitCompatibleHypoFilter( hypoKind, !hyp->IsAuxiliary() )) {
948       list <const SMESHDS_Hypothesis * > usedHyps;
949       if ( GetHypotheses( aSubShape, hypoKind, usedHyps, true ))
950         return ( find( usedHyps.begin(), usedHyps.end(), anHyp ) != usedHyps.end() );
951     }
952   }
953
954   // look through all assigned hypotheses
955   //SMESH_HypoFilter filter( SMESH_HypoFilter::Is( hyp ));
956   return false; //GetHypothesis( aSubShape, filter, true );
957 }
958
959 //=============================================================================
960 /*!
961  *
962  */
963 //=============================================================================
964
965 const list < SMESH_subMesh * >&
966 SMESH_Mesh::GetSubMeshUsingHypothesis(SMESHDS_Hypothesis * anHyp)
967   throw(SALOME_Exception)
968 {
969   Unexpect aCatch(SalomeException);
970   if(MYDEBUG) MESSAGE("SMESH_Mesh::GetSubMeshUsingHypothesis");
971   map < int, SMESH_subMesh * >::iterator itsm;
972   _subMeshesUsingHypothesisList.clear();
973   for (itsm = _mapSubMesh.begin(); itsm != _mapSubMesh.end(); itsm++)
974   {
975     SMESH_subMesh *aSubMesh = (*itsm).second;
976     if ( IsUsedHypothesis ( anHyp, aSubMesh ))
977       _subMeshesUsingHypothesisList.push_back(aSubMesh);
978   }
979   return _subMeshesUsingHypothesisList;
980 }
981
982 //=======================================================================
983 //function : NotifySubMeshesHypothesisModification
984 //purpose  : Say all submeshes using theChangedHyp that it has been modified
985 //=======================================================================
986
987 void SMESH_Mesh::NotifySubMeshesHypothesisModification(const SMESH_Hypothesis* hyp)
988 {
989   Unexpect aCatch(SalomeException);
990
991   if ( !GetMeshDS()->IsUsedHypothesis( hyp ))
992     return;
993
994   const SMESH_Algo *foundAlgo = 0;
995   SMESH_HypoFilter algoKind, compatibleHypoKind;
996   list <const SMESHDS_Hypothesis * > usedHyps;
997
998
999   map < int, SMESH_subMesh * >::iterator itsm;
1000   for (itsm = _mapSubMesh.begin(); itsm != _mapSubMesh.end(); itsm++)
1001   {
1002     SMESH_subMesh *aSubMesh = (*itsm).second;
1003     if ( aSubMesh->IsApplicableHypotesis( hyp ))
1004     {
1005       const TopoDS_Shape & aSubShape = aSubMesh->GetSubShape();
1006
1007       if ( !foundAlgo ) // init filter for algo search
1008         algoKind.Init( THypType::IsAlgo() ).And( THypType::IsApplicableTo( aSubShape ));
1009       
1010       const SMESH_Algo *algo = static_cast<const SMESH_Algo*>
1011         ( GetHypothesis( aSubShape, algoKind, true ));
1012
1013       if ( algo )
1014       {
1015         bool sameAlgo = ( algo == foundAlgo );
1016         if ( !sameAlgo && foundAlgo )
1017           sameAlgo = ( strcmp( algo->GetName(), foundAlgo->GetName() ) == 0);
1018
1019         if ( !sameAlgo ) { // init filter for used hypos search
1020           if ( !algo->InitCompatibleHypoFilter( compatibleHypoKind, !hyp->IsAuxiliary() ))
1021             continue; // algo does not use any hypothesis
1022           foundAlgo = algo;
1023         }
1024
1025         // check if hyp is used by algo
1026         usedHyps.clear();
1027         if ( GetHypotheses( aSubShape, compatibleHypoKind, usedHyps, true ) &&
1028              find( usedHyps.begin(), usedHyps.end(), hyp ) != usedHyps.end() )
1029         {
1030           aSubMesh->AlgoStateEngine(SMESH_subMesh::MODIF_HYP,
1031                                     const_cast< SMESH_Hypothesis*>( hyp ));
1032         }
1033       }
1034     }
1035   }
1036   HasModificationsToDiscard(); // to reset _isModified flag if mesh becomes empty
1037   GetMeshDS()->Modified();
1038 }
1039
1040 //=============================================================================
1041 /*!
1042  *  Auto color functionality
1043  */
1044 //=============================================================================
1045 void SMESH_Mesh::SetAutoColor(bool theAutoColor) throw(SALOME_Exception)
1046 {
1047   Unexpect aCatch(SalomeException);
1048   _isAutoColor = theAutoColor;
1049 }
1050
1051 bool SMESH_Mesh::GetAutoColor() throw(SALOME_Exception)
1052 {
1053   Unexpect aCatch(SalomeException);
1054   return _isAutoColor;
1055 }
1056
1057 //=======================================================================
1058 //function : SetIsModified
1059 //purpose  : Set the flag meaning that the mesh has been edited "manually"
1060 //=======================================================================
1061
1062 void SMESH_Mesh::SetIsModified(bool isModified)
1063 {
1064   _isModified = isModified;
1065
1066   if ( _isModified )
1067     // check if mesh becomes empty as result of modification
1068     HasModificationsToDiscard();
1069 }
1070
1071 //=======================================================================
1072 //function : HasModificationsToDiscard
1073 //purpose  : Return true if the mesh has been edited since a total re-compute
1074 //           and those modifications may prevent successful partial re-compute.
1075 //           As a side effect reset _isModified flag if mesh is empty
1076 //issue    : 0020693
1077 //=======================================================================
1078
1079 bool SMESH_Mesh::HasModificationsToDiscard() const
1080 {
1081   if ( ! _isModified )
1082     return false;
1083
1084   // return true if the next Compute() will be partial and
1085   // existing but changed elements may prevent successful re-compute
1086   bool hasComputed = false, hasNotComputed = false;
1087   map <int, SMESH_subMesh*>::const_iterator i_sm = _mapSubMesh.begin();
1088   for ( ; i_sm != _mapSubMesh.end() ; ++i_sm )
1089     switch ( i_sm->second->GetSubShape().ShapeType() )
1090     {
1091     case TopAbs_EDGE:
1092     case TopAbs_FACE:
1093     case TopAbs_SOLID:
1094       if ( i_sm->second->IsMeshComputed() )
1095         hasComputed = true;
1096       else
1097         hasNotComputed = true;
1098       if ( hasComputed && hasNotComputed)
1099         return true;
1100     }
1101
1102   if ( NbNodes() < 1 )
1103     const_cast<SMESH_Mesh*>(this)->_isModified = false;
1104
1105   return false;
1106 }
1107
1108 //================================================================================
1109 /*!
1110  * \brief Check if any groups of the same type have equal names
1111  */
1112 //================================================================================
1113
1114 bool SMESH_Mesh::HasDuplicatedGroupNamesMED()
1115 {
1116   //set<string> aGroupNames; // Corrected for Mantis issue 0020028
1117   map< SMDSAbs_ElementType, set<string> > aGroupNames;
1118   for ( map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ )
1119   {
1120     SMESH_Group* aGroup = it->second;
1121     SMDSAbs_ElementType aType = aGroup->GetGroupDS()->GetType();
1122     string aGroupName = aGroup->GetName();
1123     aGroupName.resize(MAX_MED_GROUP_NAME_LENGTH);
1124     if (!aGroupNames[aType].insert(aGroupName).second)
1125       return true;
1126   }
1127
1128   return false;
1129 }
1130
1131 //================================================================================
1132 /*!
1133  * \brief Export the mesh to a med file
1134  */
1135 //================================================================================
1136
1137 void SMESH_Mesh::ExportMED(const char *        file, 
1138                            const char*         theMeshName, 
1139                            bool                theAutoGroups,
1140                            int                 theVersion,
1141                            const SMESHDS_Mesh* meshPart) 
1142   throw(SALOME_Exception)
1143 {
1144   Unexpect aCatch(SalomeException);
1145
1146   DriverMED_W_SMESHDS_Mesh myWriter;
1147   myWriter.SetFile    ( file, MED::EVersion(theVersion) );
1148   myWriter.SetMesh    ( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS   );
1149   if ( !theMeshName ) 
1150     myWriter.SetMeshId  ( _idDoc      );
1151   else {
1152     myWriter.SetMeshId  ( -1          );
1153     myWriter.SetMeshName( theMeshName );
1154   }
1155
1156   if ( theAutoGroups ) {
1157     myWriter.AddGroupOfNodes();
1158     myWriter.AddGroupOfEdges();
1159     myWriter.AddGroupOfFaces();
1160     myWriter.AddGroupOfVolumes();
1161   }
1162
1163   // Pass groups to writer. Provide unique group names.
1164   //set<string> aGroupNames; // Corrected for Mantis issue 0020028
1165   if ( !meshPart )
1166   {
1167     map< SMDSAbs_ElementType, set<string> > aGroupNames;
1168     char aString [256];
1169     int maxNbIter = 10000; // to guarantee cycle finish
1170     for ( map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ ) {
1171       SMESH_Group*       aGroup   = it->second;
1172       SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
1173       if ( aGroupDS ) {
1174         SMDSAbs_ElementType aType = aGroupDS->GetType();
1175         string aGroupName0 = aGroup->GetName();
1176         aGroupName0.resize(MAX_MED_GROUP_NAME_LENGTH);
1177         string aGroupName = aGroupName0;
1178         for (int i = 1; !aGroupNames[aType].insert(aGroupName).second && i < maxNbIter; i++) {
1179           sprintf(&aString[0], "GR_%d_%s", i, aGroupName0.c_str());
1180           aGroupName = aString;
1181           aGroupName.resize(MAX_MED_GROUP_NAME_LENGTH);
1182         }
1183         aGroupDS->SetStoreName( aGroupName.c_str() );
1184         myWriter.AddGroup( aGroupDS );
1185       }
1186     }
1187   }
1188   // Perform export
1189   myWriter.Perform();
1190 }
1191
1192 void SMESH_Mesh::ExportSAUV(const char *file, 
1193                             const char* theMeshName, 
1194                             bool theAutoGroups)
1195   throw(SALOME_Exception)
1196 {
1197   std::string medfilename(file);
1198   medfilename += ".med";
1199   std::string cmd;
1200 #ifdef WNT
1201   cmd = "%PYTHONBIN% ";
1202 #else
1203   cmd = "python ";
1204 #endif
1205   cmd += "-c \"";
1206   cmd += "from medutilities import my_remove ; my_remove(r'" + medfilename + "')";
1207   cmd += "\"";
1208   system(cmd.c_str());
1209   ExportMED(medfilename.c_str(), theMeshName, theAutoGroups, 1);
1210 #ifdef WNT
1211   cmd = "%PYTHONBIN% ";
1212 #else
1213   cmd = "python ";
1214 #endif
1215   cmd += "-c \"";
1216   cmd += "from medutilities import convert ; convert(r'" + medfilename + "', 'MED', 'GIBI', 1, r'" + file + "')";
1217   cmd += "\"";
1218   system(cmd.c_str());
1219 #ifdef WNT
1220   cmd = "%PYTHONBIN% ";
1221 #else
1222   cmd = "python ";
1223 #endif
1224   cmd += "-c \"";
1225   cmd += "from medutilities import my_remove ; my_remove(r'" + medfilename + "')";
1226   cmd += "\"";
1227   system(cmd.c_str());
1228 }
1229
1230 //================================================================================
1231 /*!
1232  * \brief Export the mesh to a DAT file
1233  */
1234 //================================================================================
1235
1236 void SMESH_Mesh::ExportDAT(const char *        file,
1237                            const SMESHDS_Mesh* meshPart) throw(SALOME_Exception)
1238 {
1239   Unexpect aCatch(SalomeException);
1240   DriverDAT_W_SMDS_Mesh myWriter;
1241   myWriter.SetFile( file );
1242   myWriter.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS );
1243   myWriter.SetMeshId(_idDoc);
1244   myWriter.Perform();
1245 }
1246
1247 //================================================================================
1248 /*!
1249  * \brief Export the mesh to an UNV file
1250  */
1251 //================================================================================
1252
1253 void SMESH_Mesh::ExportUNV(const char *        file,
1254                            const SMESHDS_Mesh* meshPart) throw(SALOME_Exception)
1255 {
1256   Unexpect aCatch(SalomeException);
1257   DriverUNV_W_SMDS_Mesh myWriter;
1258   myWriter.SetFile( file );
1259   myWriter.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS );
1260   myWriter.SetMeshId(_idDoc);
1261   //  myWriter.SetGroups(_mapGroup);
1262
1263   if ( !meshPart )
1264   {
1265     for ( map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ ) {
1266       SMESH_Group*       aGroup   = it->second;
1267       SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
1268       if ( aGroupDS ) {
1269         string aGroupName = aGroup->GetName();
1270         aGroupDS->SetStoreName( aGroupName.c_str() );
1271         myWriter.AddGroup( aGroupDS );
1272       }
1273     }
1274   }
1275   myWriter.Perform();
1276 }
1277
1278 //================================================================================
1279 /*!
1280  * \brief Export the mesh to an STL file
1281  */
1282 //================================================================================
1283
1284 void SMESH_Mesh::ExportSTL(const char *        file,
1285                            const bool          isascii,
1286                            const SMESHDS_Mesh* meshPart) throw(SALOME_Exception)
1287 {
1288   Unexpect aCatch(SalomeException);
1289   DriverSTL_W_SMDS_Mesh myWriter;
1290   myWriter.SetFile( file );
1291   myWriter.SetIsAscii( isascii );
1292   myWriter.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS);
1293   myWriter.SetMeshId(_idDoc);
1294   myWriter.Perform();
1295 }
1296
1297 //================================================================================
1298 /*!
1299  * \brief Export the mesh to the CGNS file
1300  */
1301 //================================================================================
1302
1303 void SMESH_Mesh::ExportCGNS(const char *        file,
1304                             const SMESHDS_Mesh* meshDS)
1305 {
1306   int res = Driver_Mesh::DRS_FAIL;
1307 #ifdef WITH_CGNS
1308   DriverCGNS_Write myWriter;
1309   myWriter.SetFile( file );
1310   myWriter.SetMesh( const_cast<SMESHDS_Mesh*>( meshDS ));
1311   myWriter.SetMeshName( SMESH_Comment("Mesh_") << meshDS->GetPersistentId());
1312   res = myWriter.Perform();
1313 #endif
1314   if ( res != Driver_Mesh::DRS_OK )
1315     throw SALOME_Exception("Export failed");
1316 }
1317
1318 //================================================================================
1319 /*!
1320  * \brief Return number of nodes in the mesh
1321  */
1322 //================================================================================
1323
1324 int SMESH_Mesh::NbNodes() const throw(SALOME_Exception)
1325 {
1326   Unexpect aCatch(SalomeException);
1327   return _myMeshDS->NbNodes();
1328 }
1329
1330 //================================================================================
1331 /*!
1332  * \brief  Return number of edges of given order in the mesh
1333  */
1334 //================================================================================
1335
1336 int SMESH_Mesh::Nb0DElements() const throw(SALOME_Exception)
1337 {
1338   Unexpect aCatch(SalomeException);
1339   return _myMeshDS->GetMeshInfo().Nb0DElements();
1340 }
1341
1342 //================================================================================
1343 /*!
1344  * \brief  Return number of edges of given order in the mesh
1345  */
1346 //================================================================================
1347
1348 int SMESH_Mesh::NbEdges(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1349 {
1350   Unexpect aCatch(SalomeException);
1351   return _myMeshDS->GetMeshInfo().NbEdges(order);
1352 }
1353
1354 //================================================================================
1355 /*!
1356  * \brief Return number of faces of given order in the mesh
1357  */
1358 //================================================================================
1359
1360 int SMESH_Mesh::NbFaces(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1361 {
1362   Unexpect aCatch(SalomeException);
1363   return _myMeshDS->GetMeshInfo().NbFaces(order);
1364 }
1365
1366 //================================================================================
1367 /*!
1368  * \brief Return the number of faces in the mesh
1369  */
1370 //================================================================================
1371
1372 int SMESH_Mesh::NbTriangles(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1373 {
1374   Unexpect aCatch(SalomeException);
1375   return _myMeshDS->GetMeshInfo().NbTriangles(order);
1376 }
1377
1378 //================================================================================
1379 /*!
1380  * \brief Return the number nodes faces in the mesh
1381  */
1382 //================================================================================
1383
1384 int SMESH_Mesh::NbQuadrangles(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1385 {
1386   Unexpect aCatch(SalomeException);
1387   return _myMeshDS->GetMeshInfo().NbQuadrangles(order);
1388 }
1389
1390 //================================================================================
1391 /*!
1392  * \brief Return the number of polygonal faces in the mesh
1393  */
1394 //================================================================================
1395
1396 int SMESH_Mesh::NbPolygons() const throw(SALOME_Exception)
1397 {
1398   Unexpect aCatch(SalomeException);
1399   return _myMeshDS->GetMeshInfo().NbPolygons();
1400 }
1401
1402 //================================================================================
1403 /*!
1404  * \brief Return number of volumes of given order in the mesh
1405  */
1406 //================================================================================
1407
1408 int SMESH_Mesh::NbVolumes(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1409 {
1410   Unexpect aCatch(SalomeException);
1411   return _myMeshDS->GetMeshInfo().NbVolumes(order);
1412 }
1413
1414 //================================================================================
1415 /*!
1416  * \brief  Return number of tetrahedrons of given order in the mesh
1417  */
1418 //================================================================================
1419
1420 int SMESH_Mesh::NbTetras(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1421 {
1422   Unexpect aCatch(SalomeException);
1423   return _myMeshDS->GetMeshInfo().NbTetras(order);
1424 }
1425
1426 //================================================================================
1427 /*!
1428  * \brief  Return number of hexahedrons of given order in the mesh
1429  */
1430 //================================================================================
1431
1432 int SMESH_Mesh::NbHexas(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1433 {
1434   Unexpect aCatch(SalomeException);
1435   return _myMeshDS->GetMeshInfo().NbHexas(order);
1436 }
1437
1438 //================================================================================
1439 /*!
1440  * \brief  Return number of pyramids of given order in the mesh
1441  */
1442 //================================================================================
1443
1444 int SMESH_Mesh::NbPyramids(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1445 {
1446   Unexpect aCatch(SalomeException);
1447   return _myMeshDS->GetMeshInfo().NbPyramids(order);
1448 }
1449
1450 //================================================================================
1451 /*!
1452  * \brief  Return number of prisms (penthahedrons) of given order in the mesh
1453  */
1454 //================================================================================
1455
1456 int SMESH_Mesh::NbPrisms(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1457 {
1458   Unexpect aCatch(SalomeException);
1459   return _myMeshDS->GetMeshInfo().NbPrisms(order);
1460 }
1461
1462 //================================================================================
1463 /*!
1464  * \brief  Return number of polyhedrons in the mesh
1465  */
1466 //================================================================================
1467
1468 int SMESH_Mesh::NbPolyhedrons() const throw(SALOME_Exception)
1469 {
1470   Unexpect aCatch(SalomeException);
1471   return _myMeshDS->GetMeshInfo().NbPolyhedrons();
1472 }
1473
1474 //================================================================================
1475 /*!
1476  * \brief  Return number of submeshes in the mesh
1477  */
1478 //================================================================================
1479
1480 int SMESH_Mesh::NbSubMesh() const throw(SALOME_Exception)
1481 {
1482   Unexpect aCatch(SalomeException);
1483   return _myMeshDS->NbSubMesh();
1484 }
1485
1486 //=======================================================================
1487 //function : IsNotConformAllowed
1488 //purpose  : check if a hypothesis alowing notconform mesh is present
1489 //=======================================================================
1490
1491 bool SMESH_Mesh::IsNotConformAllowed() const
1492 {
1493   if(MYDEBUG) MESSAGE("SMESH_Mesh::IsNotConformAllowed");
1494
1495   static SMESH_HypoFilter filter( SMESH_HypoFilter::HasName( "NotConformAllowed" ));
1496   return GetHypothesis( _myMeshDS->ShapeToMesh(), filter, false );
1497 }
1498
1499 //=======================================================================
1500 //function : IsMainShape
1501 //purpose  : 
1502 //=======================================================================
1503
1504 bool SMESH_Mesh::IsMainShape(const TopoDS_Shape& theShape) const
1505 {
1506   return theShape.IsSame(_myMeshDS->ShapeToMesh() );
1507 }
1508
1509 //=============================================================================
1510 /*!
1511  *  
1512  */
1513 //=============================================================================
1514
1515 SMESH_Group* SMESH_Mesh::AddGroup (const SMDSAbs_ElementType theType,
1516                                    const char*               theName,
1517                                    int&                      theId,
1518                                    const TopoDS_Shape&       theShape,
1519                                    const SMESH_PredicatePtr& thePredicate)
1520 {
1521   if (_mapGroup.count(_groupId))
1522     return NULL;
1523   theId = _groupId;
1524   SMESH_Group* aGroup = new SMESH_Group (theId, this, theType, theName, theShape, thePredicate);
1525   GetMeshDS()->AddGroup( aGroup->GetGroupDS() );
1526   _mapGroup[_groupId++] = aGroup;
1527   return aGroup;
1528 }
1529
1530 //================================================================================
1531 /*!
1532  * \brief Creates SMESH_Groups for not wrapped SMESHDS_Groups
1533  *  \retval bool - true if new SMESH_Groups have been created
1534  * 
1535  */
1536 //================================================================================
1537
1538 bool SMESH_Mesh::SynchronizeGroups()
1539 {
1540   int nbGroups = _mapGroup.size();
1541   const set<SMESHDS_GroupBase*>& groups = _myMeshDS->GetGroups();
1542   set<SMESHDS_GroupBase*>::const_iterator gIt = groups.begin();
1543   for ( ; gIt != groups.end(); ++gIt )
1544   {
1545     SMESHDS_GroupBase* groupDS = (SMESHDS_GroupBase*) *gIt;
1546     _groupId = groupDS->GetID();
1547     if ( !_mapGroup.count( _groupId ))
1548       _mapGroup[_groupId] = new SMESH_Group( groupDS );
1549   }
1550   if ( !_mapGroup.empty() )
1551     _groupId = _mapGroup.rbegin()->first + 1;
1552
1553   return nbGroups < _mapGroup.size();
1554 }
1555
1556 //================================================================================
1557 /*!
1558  * \brief Return iterator on all existing groups
1559  */
1560 //================================================================================
1561
1562 SMESH_Mesh::GroupIteratorPtr SMESH_Mesh::GetGroups() const
1563 {
1564   typedef map <int, SMESH_Group *> TMap;
1565   return GroupIteratorPtr( new SMDS_mapIterator<TMap>( _mapGroup ));
1566 }
1567
1568 //=============================================================================
1569 /*!
1570  * \brief Return a group by ID
1571  */
1572 //=============================================================================
1573
1574 SMESH_Group* SMESH_Mesh::GetGroup (const int theGroupID)
1575 {
1576   if (_mapGroup.find(theGroupID) == _mapGroup.end())
1577     return NULL;
1578   return _mapGroup[theGroupID];
1579 }
1580
1581
1582 //=============================================================================
1583 /*!
1584  * \brief Return IDs of all groups
1585  */
1586 //=============================================================================
1587
1588 list<int> SMESH_Mesh::GetGroupIds() const
1589 {
1590   list<int> anIds;
1591   for ( map<int, SMESH_Group*>::const_iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ )
1592     anIds.push_back( it->first );
1593   
1594   return anIds;
1595 }
1596
1597 //================================================================================
1598 /*!
1599  * \brief Set a caller of RemoveGroup() at level of CORBA API implementation.
1600  * The set upCaller will be deleted by SMESH_Mesh
1601  */
1602 //================================================================================
1603
1604 void SMESH_Mesh::SetRemoveGroupCallUp( TRmGroupCallUp* upCaller )
1605 {
1606   if ( _rmGroupCallUp ) delete _rmGroupCallUp;
1607   _rmGroupCallUp = upCaller;
1608 }
1609
1610 //=============================================================================
1611 /*!
1612  *  
1613  */
1614 //=============================================================================
1615
1616 bool SMESH_Mesh::RemoveGroup (const int theGroupID)
1617 {
1618   if (_mapGroup.find(theGroupID) == _mapGroup.end())
1619     return false;
1620   GetMeshDS()->RemoveGroup( _mapGroup[theGroupID]->GetGroupDS() );
1621   delete _mapGroup[theGroupID];
1622   _mapGroup.erase (theGroupID);
1623   if (_rmGroupCallUp)
1624     _rmGroupCallUp->RemoveGroup( theGroupID );
1625   return true;
1626 }
1627
1628 //=======================================================================
1629 //function : GetAncestors
1630 //purpose  : return list of ancestors of theSubShape in the order
1631 //           that lower dimention shapes come first.
1632 //=======================================================================
1633
1634 const TopTools_ListOfShape& SMESH_Mesh::GetAncestors(const TopoDS_Shape& theS) const
1635 {
1636   if ( _mapAncestors.Contains( theS ) )
1637     return _mapAncestors.FindFromKey( theS );
1638
1639   static TopTools_ListOfShape emptyList;
1640   return emptyList;
1641 }
1642
1643 //=======================================================================
1644 //function : Dump
1645 //purpose  : dumps contents of mesh to stream [ debug purposes ]
1646 //=======================================================================
1647
1648 ostream& SMESH_Mesh::Dump(ostream& save)
1649 {
1650   int clause = 0;
1651   save << "========================== Dump contents of mesh ==========================" << endl << endl;
1652   save << ++clause << ") Total number of nodes:   \t"    << NbNodes() << endl;
1653   save << ++clause << ") Total number of edges:   \t"    << NbEdges() << endl;
1654   save << ++clause << ") Total number of faces:   \t"    << NbFaces() << endl;
1655   save << ++clause << ") Total number of polygons:\t"    << NbPolygons() << endl;
1656   save << ++clause << ") Total number of volumes:\t"     << NbVolumes() << endl;
1657   save << ++clause << ") Total number of polyhedrons:\t" << NbPolyhedrons() << endl << endl;
1658   for ( int isQuadratic = 0; isQuadratic < 2; ++isQuadratic )
1659   {
1660     string orderStr = isQuadratic ? "quadratic" : "linear";
1661     SMDSAbs_ElementOrder order  = isQuadratic ? ORDER_QUADRATIC : ORDER_LINEAR;
1662
1663     save << ++clause << ") Total number of " << orderStr << " edges:\t" << NbEdges(order) << endl;
1664     save << ++clause << ") Total number of " << orderStr << " faces:\t" << NbFaces(order) << endl;
1665     if ( NbFaces(order) > 0 ) {
1666       int nb3 = NbTriangles(order);
1667       int nb4 = NbQuadrangles(order);
1668       save << clause << ".1) Number of " << orderStr << " triangles:  \t" << nb3 << endl;
1669       save << clause << ".2) Number of " << orderStr << " quadrangles:\t" << nb4 << endl;
1670       if ( nb3 + nb4 !=  NbFaces(order) ) {
1671         map<int,int> myFaceMap;
1672         SMDS_FaceIteratorPtr itFaces=_myMeshDS->facesIterator();
1673         while( itFaces->more( ) ) {
1674           int nbNodes = itFaces->next()->NbNodes();
1675           if ( myFaceMap.find( nbNodes ) == myFaceMap.end() )
1676             myFaceMap[ nbNodes ] = 0;
1677           myFaceMap[ nbNodes ] = myFaceMap[ nbNodes ] + 1;
1678         }
1679         save << clause << ".3) Faces in detail: " << endl;
1680         map <int,int>::iterator itF;
1681         for (itF = myFaceMap.begin(); itF != myFaceMap.end(); itF++)
1682           save << "--> nb nodes: " << itF->first << " - nb elemens:\t" << itF->second << endl;
1683       }
1684     }
1685     save << ++clause << ") Total number of " << orderStr << " volumes:\t" << NbVolumes(order) << endl;
1686     if ( NbVolumes(order) > 0 ) {
1687       int nb8 = NbHexas(order);
1688       int nb4 = NbTetras(order);
1689       int nb5 = NbPyramids(order);
1690       int nb6 = NbPrisms(order);
1691       save << clause << ".1) Number of " << orderStr << " hexahedrons:\t" << nb8 << endl;
1692       save << clause << ".2) Number of " << orderStr << " tetrahedrons:\t" << nb4 << endl;
1693       save << clause << ".3) Number of " << orderStr << " prisms:      \t" << nb6 << endl;
1694       save << clause << ".4) Number of " << orderStr << " pyramids:\t" << nb5 << endl;
1695       if ( nb8 + nb4 + nb5 + nb6 != NbVolumes(order) ) {
1696         map<int,int> myVolumesMap;
1697         SMDS_VolumeIteratorPtr itVolumes=_myMeshDS->volumesIterator();
1698         while( itVolumes->more( ) ) {
1699           int nbNodes = itVolumes->next()->NbNodes();
1700           if ( myVolumesMap.find( nbNodes ) == myVolumesMap.end() )
1701             myVolumesMap[ nbNodes ] = 0;
1702           myVolumesMap[ nbNodes ] = myVolumesMap[ nbNodes ] + 1;
1703         }
1704         save << clause << ".5) Volumes in detail: " << endl;
1705         map <int,int>::iterator itV;
1706         for (itV = myVolumesMap.begin(); itV != myVolumesMap.end(); itV++)
1707           save << "--> nb nodes: " << itV->first << " - nb elemens:\t" << itV->second << endl;
1708       }
1709     }
1710     save << endl;
1711   }
1712   save << "===========================================================================" << endl;
1713   return save;
1714 }
1715
1716 //=======================================================================
1717 //function : GetElementType
1718 //purpose  : Returns type of mesh element with certain id
1719 //=======================================================================
1720
1721 SMDSAbs_ElementType SMESH_Mesh::GetElementType( const int id, const bool iselem )
1722 {
1723   return _myMeshDS->GetElementType( id, iselem );
1724 }
1725
1726 //=============================================================================
1727 /*!
1728  *  \brief Convert group on geometry into standalone group
1729  */
1730 //=============================================================================
1731
1732 SMESH_Group* SMESH_Mesh::ConvertToStandalone ( int theGroupID )
1733 {
1734   SMESH_Group* aGroup = 0;
1735   map < int, SMESH_Group * >::iterator itg = _mapGroup.find( theGroupID );
1736   if ( itg == _mapGroup.end() )
1737     return aGroup;
1738
1739   SMESH_Group* anOldGrp = (*itg).second;
1740   SMESHDS_GroupBase* anOldGrpDS = anOldGrp->GetGroupDS();
1741   if ( !anOldGrp || !anOldGrpDS )
1742     return aGroup;
1743
1744   // create new standalone group
1745   aGroup = new SMESH_Group (theGroupID, this, anOldGrpDS->GetType(), anOldGrp->GetName() );
1746   _mapGroup[theGroupID] = aGroup;
1747
1748   SMESHDS_Group* aNewGrpDS = dynamic_cast<SMESHDS_Group*>( aGroup->GetGroupDS() );
1749   GetMeshDS()->RemoveGroup( anOldGrpDS );
1750   GetMeshDS()->AddGroup( aNewGrpDS );
1751
1752   // add elements (or nodes) into new created group
1753   SMDS_ElemIteratorPtr anItr = anOldGrpDS->GetElements();
1754   while ( anItr->more() )
1755     aNewGrpDS->Add( (anItr->next())->GetID() );
1756
1757   // set color
1758   aNewGrpDS->SetColor( anOldGrpDS->GetColor() );
1759
1760   // remove old group
1761   delete anOldGrp;
1762
1763   return aGroup;
1764 }
1765
1766 //=============================================================================
1767 /*!
1768  *  \brief remove submesh order  from Mesh
1769  */
1770 //=============================================================================
1771
1772 void SMESH_Mesh::ClearMeshOrder()
1773 {
1774   _mySubMeshOrder.clear();
1775 }
1776
1777 //=============================================================================
1778 /*!
1779  *  \brief remove submesh order  from Mesh
1780  */
1781 //=============================================================================
1782
1783 void SMESH_Mesh::SetMeshOrder(const TListOfListOfInt& theOrder )
1784 {
1785   _mySubMeshOrder = theOrder;
1786 }
1787
1788 //=============================================================================
1789 /*!
1790  *  \brief return submesh order if any
1791  */
1792 //=============================================================================
1793
1794 const TListOfListOfInt& SMESH_Mesh::GetMeshOrder() const
1795 {
1796   return _mySubMeshOrder;
1797 }
1798
1799 //=============================================================================
1800 /*!
1801  *  \brief fill _mapAncestors
1802  */
1803 //=============================================================================
1804
1805 void SMESH_Mesh::fillAncestorsMap(const TopoDS_Shape& theShape)
1806 {
1807
1808   int desType, ancType;
1809   if ( !theShape.IsSame( GetShapeToMesh()) && theShape.ShapeType() == TopAbs_COMPOUND )
1810   {
1811     // a geom group is added. Insert it into lists of ancestors before
1812     // the first ancestor more complex than group members
1813     int memberType = TopoDS_Iterator( theShape ).Value().ShapeType();
1814     for ( desType = TopAbs_VERTEX; desType >= memberType; desType-- )
1815       for (TopExp_Explorer des( theShape, TopAbs_ShapeEnum( desType )); des.More(); des.Next())
1816       {
1817         if ( !_mapAncestors.Contains( des.Current() )) continue;// issue 0020982
1818         TopTools_ListOfShape& ancList = _mapAncestors.ChangeFromKey( des.Current() );
1819         TopTools_ListIteratorOfListOfShape ancIt (ancList);
1820         while ( ancIt.More() && ancIt.Value().ShapeType() >= memberType )
1821           ancIt.Next();
1822         if ( ancIt.More() )
1823           ancList.InsertBefore( theShape, ancIt );
1824       }
1825   }
1826   {
1827     for ( desType = TopAbs_VERTEX; desType > TopAbs_COMPOUND; desType-- )
1828       for ( ancType = desType - 1; ancType >= TopAbs_COMPOUND; ancType-- )
1829         TopExp::MapShapesAndAncestors ( theShape,
1830                                         (TopAbs_ShapeEnum) desType,
1831                                         (TopAbs_ShapeEnum) ancType,
1832                                         _mapAncestors );
1833   }
1834 }
1835
1836 //=============================================================================
1837 /*!
1838  * \brief sort submeshes according to stored mesh order
1839  * \param theListToSort in out list to be sorted
1840  * \return FALSE if nothing sorted
1841  */
1842 //=============================================================================
1843
1844 bool SMESH_Mesh::SortByMeshOrder(list<SMESH_subMesh*>& theListToSort) const
1845 {
1846   if ( !_mySubMeshOrder.size() || theListToSort.size() < 2)
1847     return true;
1848   
1849   bool res = false;
1850   list<SMESH_subMesh*> onlyOrderedList;
1851   // collect all ordered submeshes in one list as pointers
1852   // and get their positions within theListToSort
1853   typedef list<SMESH_subMesh*>::iterator TPosInList;
1854   map< int, TPosInList > sortedPos;
1855   TPosInList smBeg = theListToSort.begin(), smEnd = theListToSort.end();
1856   TListOfListOfInt::const_iterator listIddIt = _mySubMeshOrder.begin();
1857   for( ; listIddIt != _mySubMeshOrder.end(); listIddIt++) {
1858     const TListOfInt& listOfId = *listIddIt;
1859     TListOfInt::const_iterator idIt = listOfId.begin();
1860     for ( ; idIt != listOfId.end(); idIt++ ) {
1861       if ( SMESH_subMesh * sm = GetSubMeshContaining( *idIt )) {
1862         TPosInList smPos = find( smBeg, smEnd, sm );
1863         if ( smPos != smEnd ) {
1864           onlyOrderedList.push_back( sm );
1865           sortedPos[ distance( smBeg, smPos )] = smPos;
1866         }
1867       }
1868     }
1869   }
1870   if (onlyOrderedList.size() < 2)
1871     return res;
1872   res = true;
1873
1874   list<SMESH_subMesh*>::iterator onlyBIt = onlyOrderedList.begin();
1875   list<SMESH_subMesh*>::iterator onlyEIt = onlyOrderedList.end();
1876
1877   // iterate on ordered submeshes and insert them in detected positions
1878   map< int, TPosInList >::iterator i_pos = sortedPos.begin();
1879   for ( ; onlyBIt != onlyEIt; ++onlyBIt, ++i_pos )
1880     *(i_pos->second) = *onlyBIt;
1881
1882   return res;
1883 }
1884
1885 //=============================================================================
1886 /*!
1887  * \brief sort submeshes according to stored mesh order
1888  * \param theListToSort in out list to be sorted
1889  * \return FALSE if nothing sorted
1890  */
1891 //=============================================================================
1892
1893 list<SMESH_subMesh*> SMESH_Mesh::getAncestorsSubMeshes
1894   (const TopoDS_Shape& theSubShape) const
1895 {
1896   list<SMESH_subMesh*> listOfSubMesh;
1897   TopTools_ListIteratorOfListOfShape it( GetAncestors( theSubShape ));
1898   for (; it.More(); it.Next() )
1899     if ( SMESH_subMesh* sm = GetSubMeshContaining( it.Value() ))
1900       listOfSubMesh.push_back(sm);
1901
1902   // sort submeshes according to stored mesh order
1903   SortByMeshOrder( listOfSubMesh );
1904
1905   return listOfSubMesh;
1906 }