Salome HOME
Adjust to CVS HEAD modifications
[modules/smesh.git] / src / SMESH / SMESH_Mesh.cxx
1 //  SMESH SMESH : implementaion of SMESH idl descriptions
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SMESH_Mesh.cxx
25 //  Author : Paul RASCLE, EDF
26 //  Module : SMESH
27 //  $Header$
28
29 #include "SMESH_Mesh.hxx"
30 #include "SMESH_subMesh.hxx"
31 #include "SMESH_Gen.hxx"
32 #include "SMESH_Hypothesis.hxx"
33 #include "SMESH_Group.hxx"
34 #include "SMESH_HypoFilter.hxx"
35 #include "SMESHDS_Group.hxx"
36 #include "SMESHDS_Script.hxx"
37 #include "SMESHDS_GroupOnGeom.hxx"
38 #include "SMDS_MeshVolume.hxx"
39
40 #include "utilities.h"
41
42 #include "DriverMED_W_SMESHDS_Mesh.h"
43 #include "DriverDAT_W_SMDS_Mesh.h"
44 #include "DriverUNV_W_SMDS_Mesh.h"
45 #include "DriverSTL_W_SMDS_Mesh.h"
46
47 #include "DriverMED_R_SMESHDS_Mesh.h"
48 #include "DriverUNV_R_SMDS_Mesh.h"
49 #include "DriverSTL_R_SMDS_Mesh.h"
50
51 #include <BRepTools_WireExplorer.hxx>
52 #include <BRep_Builder.hxx>
53 #include <gp_Pnt.hxx>
54
55 #include <TCollection_AsciiString.hxx>
56 #include <TopExp.hxx>
57 #include <TopTools_ListOfShape.hxx>
58 #include <TopTools_Array1OfShape.hxx>
59 #include <TopTools_ListIteratorOfListOfShape.hxx>
60 #include <TopTools_MapOfShape.hxx>
61
62 #include <memory>
63
64 #include "Utils_ExceptHandlers.hxx"
65
66 #ifdef _DEBUG_
67 static int MYDEBUG = 0;
68 #else
69 static int MYDEBUG = 0;
70 #endif
71
72
73 //=============================================================================
74 /*!
75  * 
76  */
77 //=============================================================================
78
79 SMESH_Mesh::SMESH_Mesh(int localId, int studyId, SMESH_Gen * gen, SMESHDS_Document * myDocument)
80 : _groupId( 0 )
81 {
82   INFOS("SMESH_Mesh::SMESH_Mesh(int localId)");
83         _id = localId;
84         _studyId = studyId;
85         _gen = gen;
86         _myDocument = myDocument;
87         _idDoc = _myDocument->NewMesh();
88         _myMeshDS = _myDocument->GetMesh(_idDoc);
89         _isShapeToMesh = false;
90 }
91
92 //=============================================================================
93 /*!
94  * 
95  */
96 //=============================================================================
97
98 SMESH_Mesh::~SMESH_Mesh()
99 {
100   INFOS("SMESH_Mesh::~SMESH_Mesh");
101
102   // delete groups
103   map < int, SMESH_Group * >::iterator itg;
104   for (itg = _mapGroup.begin(); itg != _mapGroup.end(); itg++) {
105     SMESH_Group *aGroup = (*itg).second;
106     delete aGroup;
107   }
108 }
109
110 //=============================================================================
111 /*!
112  * 
113  */
114 //=============================================================================
115
116 void SMESH_Mesh::ShapeToMesh(const TopoDS_Shape & aShape)
117 {
118   if(MYDEBUG) MESSAGE("SMESH_Mesh::ShapeToMesh");
119
120   if ( !_myMeshDS->ShapeToMesh().IsNull() && aShape.IsNull() )
121   {
122     // removal of a shape to mesh, delete objects referring to sub-shapes:
123     // - sub-meshes
124     map <int, SMESH_subMesh *>::iterator i_sm = _mapSubMesh.begin();
125     for ( ; i_sm != _mapSubMesh.end(); ++i_sm )
126       delete i_sm->second;
127     _mapSubMesh.clear();
128     //  - groups on geometry
129     map <int, SMESH_Group *>::iterator i_gr = _mapGroup.begin();
130     while ( i_gr != _mapGroup.end() ) {
131       if ( dynamic_cast<SMESHDS_GroupOnGeom*>( i_gr->second->GetGroupDS() )) {
132         _myMeshDS->RemoveGroup( i_gr->second->GetGroupDS() );
133         delete i_gr->second;
134         _mapGroup.erase( i_gr++ );
135       }
136       else
137         i_gr++;
138     }
139     _mapPropagationChains.Clear();
140   }
141   else
142   {
143     if (_isShapeToMesh)
144       throw SALOME_Exception(LOCALIZED ("a shape to mesh has already been defined"));
145   }
146   _isShapeToMesh = true;
147   _myMeshDS->ShapeToMesh(aShape);
148
149   // fill _mapAncestors
150   _mapAncestors.Clear();
151   int desType, ancType;
152   for ( desType = TopAbs_EDGE; desType > TopAbs_COMPOUND; desType-- )
153     for ( ancType = desType - 1; ancType >= TopAbs_COMPOUND; ancType-- )
154       TopExp::MapShapesAndAncestors ( aShape,
155                                      (TopAbs_ShapeEnum) desType,
156                                      (TopAbs_ShapeEnum) ancType,
157                                      _mapAncestors );
158
159   // NRI : 24/02/03
160   //EAP: 1/9/04 TopExp::MapShapes(aShape, _subShapes); USE the same map of _myMeshDS
161 }
162
163 //=======================================================================
164 //function : UNVToMesh
165 //purpose  : 
166 //=======================================================================
167
168 int SMESH_Mesh::UNVToMesh(const char* theFileName)
169 {
170   if(MYDEBUG) MESSAGE("UNVToMesh - theFileName = "<<theFileName);
171   if(_isShapeToMesh)
172     throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
173   _isShapeToMesh = true;
174   DriverUNV_R_SMDS_Mesh myReader;
175   myReader.SetMesh(_myMeshDS);
176   myReader.SetFile(theFileName);
177   myReader.SetMeshId(-1);
178   myReader.Perform();
179   if(MYDEBUG){
180     MESSAGE("MEDToMesh - _myMeshDS->NbNodes() = "<<_myMeshDS->NbNodes());
181     MESSAGE("MEDToMesh - _myMeshDS->NbEdges() = "<<_myMeshDS->NbEdges());
182     MESSAGE("MEDToMesh - _myMeshDS->NbFaces() = "<<_myMeshDS->NbFaces());
183     MESSAGE("MEDToMesh - _myMeshDS->NbVolumes() = "<<_myMeshDS->NbVolumes());
184   }
185   return 1;
186 }
187
188 //=======================================================================
189 //function : MEDToMesh
190 //purpose  : 
191 //=======================================================================
192
193 int SMESH_Mesh::MEDToMesh(const char* theFileName, const char* theMeshName)
194 {
195   if(MYDEBUG) MESSAGE("MEDToMesh - theFileName = "<<theFileName<<", mesh name = "<<theMeshName);
196   if(_isShapeToMesh)
197     throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
198   _isShapeToMesh = true;
199   DriverMED_R_SMESHDS_Mesh myReader;
200   myReader.SetMesh(_myMeshDS);
201   myReader.SetMeshId(-1);
202   myReader.SetFile(theFileName);
203   myReader.SetMeshName(theMeshName);
204   Driver_Mesh::Status status = myReader.Perform();
205   if(MYDEBUG){
206     MESSAGE("MEDToMesh - _myMeshDS->NbNodes() = "<<_myMeshDS->NbNodes());
207     MESSAGE("MEDToMesh - _myMeshDS->NbEdges() = "<<_myMeshDS->NbEdges());
208     MESSAGE("MEDToMesh - _myMeshDS->NbFaces() = "<<_myMeshDS->NbFaces());
209     MESSAGE("MEDToMesh - _myMeshDS->NbVolumes() = "<<_myMeshDS->NbVolumes());
210   }
211
212   // Reading groups (sub-meshes are out of scope of MED import functionality)
213   list<TNameAndType> aGroupNames = myReader.GetGroupNamesAndTypes();
214   if(MYDEBUG) MESSAGE("MEDToMesh - Nb groups = "<<aGroupNames.size()); 
215   int anId;
216   list<TNameAndType>::iterator name_type = aGroupNames.begin();
217   for ( ; name_type != aGroupNames.end(); name_type++ ) {
218     SMESH_Group* aGroup = AddGroup( name_type->second, name_type->first.c_str(), anId );
219     if ( aGroup ) {
220       if(MYDEBUG) MESSAGE("MEDToMesh - group added: "<<name_type->first.c_str());      
221       SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( aGroup->GetGroupDS() );
222       if ( aGroupDS ) {
223         aGroupDS->SetStoreName( name_type->first.c_str() );
224         myReader.GetGroup( aGroupDS );
225       }
226     }
227   }
228   return (int) status;
229 }
230
231 //=======================================================================
232 //function : STLToMesh
233 //purpose  : 
234 //=======================================================================
235
236 int SMESH_Mesh::STLToMesh(const char* theFileName)
237 {
238   if(MYDEBUG) MESSAGE("UNVToMesh - theFileName = "<<theFileName);
239   if(_isShapeToMesh)
240     throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
241   _isShapeToMesh = true;
242   DriverSTL_R_SMDS_Mesh myReader;
243   myReader.SetMesh(_myMeshDS);
244   myReader.SetFile(theFileName);
245   myReader.SetMeshId(-1);
246   myReader.Perform();
247   if(MYDEBUG){
248     MESSAGE("MEDToMesh - _myMeshDS->NbNodes() = "<<_myMeshDS->NbNodes());
249     MESSAGE("MEDToMesh - _myMeshDS->NbEdges() = "<<_myMeshDS->NbEdges());
250     MESSAGE("MEDToMesh - _myMeshDS->NbFaces() = "<<_myMeshDS->NbFaces());
251     MESSAGE("MEDToMesh - _myMeshDS->NbVolumes() = "<<_myMeshDS->NbVolumes());
252   }
253   return 1;
254 }
255
256 //=============================================================================
257 /*!
258  * 
259  */
260 //=============================================================================
261
262 SMESH_Hypothesis::Hypothesis_Status
263   SMESH_Mesh::AddHypothesis(const TopoDS_Shape & aSubShape,
264                             int                  anHypId  ) throw(SALOME_Exception)
265 {
266   Unexpect aCatch(SalomeException);
267   if(MYDEBUG) MESSAGE("SMESH_Mesh::AddHypothesis");
268
269   SMESH_subMesh *subMesh = GetSubMesh(aSubShape);
270   SMESHDS_SubMesh *subMeshDS = subMesh->GetSubMeshDS();
271   if ( subMeshDS && subMeshDS->IsComplexSubmesh() ) // group of sub-shapes and maybe of not sub-
272   {
273     MESSAGE("AddHypothesis() to complex submesh");
274     // return the worst but not fatal state of all group memebers
275     SMESH_Hypothesis::Hypothesis_Status aBestRet, aWorstNotFatal, ret;
276     aBestRet = SMESH_Hypothesis::HYP_BAD_DIM;
277     aWorstNotFatal = SMESH_Hypothesis::HYP_OK;
278     for ( TopoDS_Iterator itS ( aSubShape ); itS.More(); itS.Next())
279     {
280       if ( !GetMeshDS()->ShapeToIndex( itS.Value() ))
281         continue; // not sub-shape
282       ret = AddHypothesis( itS.Value(), anHypId );
283       if ( !SMESH_Hypothesis::IsStatusFatal( ret ) && ret > aWorstNotFatal )
284         aWorstNotFatal = ret;
285       if ( ret < aBestRet )
286         aBestRet = ret;
287     }
288     if ( SMESH_Hypothesis::IsStatusFatal( aBestRet ))
289       return aBestRet;
290     return aWorstNotFatal;
291   }
292
293   StudyContextStruct *sc = _gen->GetStudyContext(_studyId);
294   if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
295   {
296     if(MYDEBUG) MESSAGE("Hypothesis ID does not give an hypothesis");
297     if(MYDEBUG) {
298       SCRUTE(_studyId);
299       SCRUTE(anHypId);
300     }
301     throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
302   }
303
304   SMESH_Hypothesis *anHyp = sc->mapHypothesis[anHypId];
305   MESSAGE( "SMESH_Mesh::AddHypothesis " << anHyp->GetName() );
306
307   bool isGlobalHyp = IsMainShape( aSubShape );
308
309   // NotConformAllowed can be only global
310   if ( !isGlobalHyp )
311   {
312     string hypName = anHyp->GetName();
313     if ( hypName == "NotConformAllowed" )
314     {
315       if(MYDEBUG) MESSAGE( "Hypotesis <NotConformAllowed> can be only global" );
316       return SMESH_Hypothesis::HYP_INCOMPATIBLE;
317     }
318   }
319
320   // shape 
321
322   int event;
323   if (anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
324     event = SMESH_subMesh::ADD_HYP;
325   else
326     event = SMESH_subMesh::ADD_ALGO;
327   SMESH_Hypothesis::Hypothesis_Status ret = subMesh->AlgoStateEngine(event, anHyp);
328
329   // subShapes
330   if (!SMESH_Hypothesis::IsStatusFatal(ret) &&
331       !subMesh->IsApplicableHypotesis( anHyp )) // is added on father
332   {
333     if (anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
334       event = SMESH_subMesh::ADD_FATHER_HYP;
335     else
336       event = SMESH_subMesh::ADD_FATHER_ALGO;
337     SMESH_Hypothesis::Hypothesis_Status ret2 =
338       subMesh->SubMeshesAlgoStateEngine(event, anHyp);
339     if (ret2 > ret)
340       ret = ret2;
341
342     // check concurent hypotheses on ansestors
343     if (ret < SMESH_Hypothesis::HYP_CONCURENT && !isGlobalHyp )
344     {
345       const map < int, SMESH_subMesh * >& smMap = subMesh->DependsOn();
346       map < int, SMESH_subMesh * >::const_iterator smIt = smMap.begin();
347       for ( ; smIt != smMap.end(); smIt++ ) {
348         if ( smIt->second->IsApplicableHypotesis( anHyp )) {
349           ret2 = smIt->second->CheckConcurentHypothesis( anHyp->GetType() );
350           if (ret2 > ret) {
351             ret = ret2;
352             break;
353           }
354         }
355       }
356     }
357   }
358
359   if(MYDEBUG) subMesh->DumpAlgoState(true);
360   SCRUTE(ret);
361   return ret;
362 }
363
364 //=============================================================================
365 /*!
366  * 
367  */
368 //=============================================================================
369
370 SMESH_Hypothesis::Hypothesis_Status
371   SMESH_Mesh::RemoveHypothesis(const TopoDS_Shape & aSubShape,
372                                int anHypId)throw(SALOME_Exception)
373 {
374   Unexpect aCatch(SalomeException);
375   if(MYDEBUG) MESSAGE("SMESH_Mesh::RemoveHypothesis");
376   
377   SMESH_subMesh *subMesh = GetSubMesh(aSubShape);
378   SMESHDS_SubMesh *subMeshDS = subMesh->GetSubMeshDS();
379   if ( subMeshDS && subMeshDS->IsComplexSubmesh() )
380   {
381     // return the worst but not fatal state of all group memebers
382     SMESH_Hypothesis::Hypothesis_Status aBestRet, aWorstNotFatal, ret;
383     aBestRet = SMESH_Hypothesis::HYP_BAD_DIM;
384     aWorstNotFatal = SMESH_Hypothesis::HYP_OK;
385     for ( TopoDS_Iterator itS ( aSubShape ); itS.More(); itS.Next())
386     {
387       if ( !GetMeshDS()->ShapeToIndex( itS.Value() ))
388         continue; // not sub-shape
389       ret = RemoveHypothesis( itS.Value(), anHypId );
390       if ( !SMESH_Hypothesis::IsStatusFatal( ret ) && ret > aWorstNotFatal )
391         aWorstNotFatal = ret;
392       if ( ret < aBestRet )
393         aBestRet = ret;
394     }
395     if ( SMESH_Hypothesis::IsStatusFatal( aBestRet ))
396       return aBestRet;
397     return aWorstNotFatal;
398   }
399
400   StudyContextStruct *sc = _gen->GetStudyContext(_studyId);
401   if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
402     throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
403   
404   SMESH_Hypothesis *anHyp = sc->mapHypothesis[anHypId];
405   int hypType = anHyp->GetType();
406   if(MYDEBUG) SCRUTE(hypType);
407   int event;
408   
409   // shape 
410   
411   if (anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
412     event = SMESH_subMesh::REMOVE_HYP;
413   else
414     event = SMESH_subMesh::REMOVE_ALGO;
415   SMESH_Hypothesis::Hypothesis_Status ret = subMesh->AlgoStateEngine(event, anHyp);
416
417   // there may appear concurrent hyps that were covered by the removed hyp
418   if (ret < SMESH_Hypothesis::HYP_CONCURENT &&
419       subMesh->IsApplicableHypotesis( anHyp ) &&
420       subMesh->CheckConcurentHypothesis( anHyp->GetType() ) != SMESH_Hypothesis::HYP_OK)
421     ret = SMESH_Hypothesis::HYP_CONCURENT;
422
423   // subShapes
424   if (!SMESH_Hypothesis::IsStatusFatal(ret) &&
425       !subMesh->IsApplicableHypotesis( anHyp )) // is removed from father
426   {
427     if (anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
428       event = SMESH_subMesh::REMOVE_FATHER_HYP;
429     else
430       event = SMESH_subMesh::REMOVE_FATHER_ALGO;
431     SMESH_Hypothesis::Hypothesis_Status ret2 =
432       subMesh->SubMeshesAlgoStateEngine(event, anHyp);
433     if (ret2 > ret) // more severe
434       ret = ret2;
435
436     // check concurent hypotheses on ansestors
437     if (ret < SMESH_Hypothesis::HYP_CONCURENT && !IsMainShape( aSubShape ) )
438     {
439       const map < int, SMESH_subMesh * >& smMap = subMesh->DependsOn();
440       map < int, SMESH_subMesh * >::const_iterator smIt = smMap.begin();
441       for ( ; smIt != smMap.end(); smIt++ ) {
442         if ( smIt->second->IsApplicableHypotesis( anHyp )) {
443           ret2 = smIt->second->CheckConcurentHypothesis( anHyp->GetType() );
444           if (ret2 > ret) {
445             ret = ret2;
446             break;
447           }
448         }
449       }
450     }
451   }
452   
453   if(MYDEBUG) subMesh->DumpAlgoState(true);
454   if(MYDEBUG) SCRUTE(ret);
455   return ret;
456 }
457
458 //=============================================================================
459 /*!
460  * 
461  */
462 //=============================================================================
463
464 SMESHDS_Mesh * SMESH_Mesh::GetMeshDS()
465 {
466   return _myMeshDS;
467 }
468
469 //=============================================================================
470 /*!
471  * 
472  */
473 //=============================================================================
474
475 const list<const SMESHDS_Hypothesis*>&
476 SMESH_Mesh::GetHypothesisList(const TopoDS_Shape & aSubShape) const
477   throw(SALOME_Exception)
478 {
479   Unexpect aCatch(SalomeException);
480   return _myMeshDS->GetHypothesis(aSubShape);
481 }
482
483 //=======================================================================
484 //function : GetHypothesis
485 //purpose  : 
486 //=======================================================================
487
488 const SMESH_Hypothesis * SMESH_Mesh::GetHypothesis(const TopoDS_Shape &    aSubShape,
489                                                    const SMESH_HypoFilter& aFilter,
490                                                    const bool              andAncestors) const
491 {
492   {
493     const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(aSubShape);
494     list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
495     for ( ; hyp != hypList.end(); hyp++ ) {
496       const SMESH_Hypothesis * h = static_cast<const SMESH_Hypothesis*>( *hyp );
497       if ( aFilter.IsOk( h, aSubShape))
498         return h;
499     }
500   }
501   if ( andAncestors )
502   {
503     TopTools_ListIteratorOfListOfShape it( GetAncestors( aSubShape ));
504     for (; it.More(); it.Next() )
505     {
506       const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(it.Value());
507       list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
508       for ( ; hyp != hypList.end(); hyp++ ) {
509         const SMESH_Hypothesis * h = static_cast<const SMESH_Hypothesis*>( *hyp );
510         if (aFilter.IsOk( h, it.Value() ))
511           return h;
512       }
513     }
514   }
515   return 0;
516 }
517
518 //=======================================================================
519 //function : GetHypotheses
520 //purpose  : 
521 //=======================================================================
522
523 bool SMESH_Mesh::GetHypotheses(const TopoDS_Shape &                aSubShape,
524                                const SMESH_HypoFilter&             aFilter,
525                                list <const SMESHDS_Hypothesis * >& aHypList,
526                                const bool                          andAncestors) const
527 {
528   int nbHyp = 0;
529   {
530     const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(aSubShape);
531     list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
532     for ( ; hyp != hypList.end(); hyp++ )
533       if ( aFilter.IsOk (static_cast<const SMESH_Hypothesis*>( *hyp ), aSubShape)) {
534         aHypList.push_back( *hyp );
535         nbHyp++;
536       }
537   }
538   // get hypos from shape of one type only: if any hypo is found on edge, do
539   // not look up on faces
540   if ( !nbHyp && andAncestors )
541   {
542     TopTools_MapOfShape map;
543     TopTools_ListIteratorOfListOfShape it( GetAncestors( aSubShape ));
544     int shapeType = it.More() ? it.Value().ShapeType() : TopAbs_SHAPE;
545     for (; it.More(); it.Next() )
546     {
547       if ( nbHyp && shapeType != it.Value().ShapeType() )
548         break;
549       shapeType = it.Value().ShapeType();
550       if ( !map.Add( it.Value() ))
551         continue;
552       const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(it.Value());
553       list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
554       for ( ; hyp != hypList.end(); hyp++ )
555         if (aFilter.IsOk( static_cast<const SMESH_Hypothesis*>( *hyp ), it.Value() )) {
556         aHypList.push_back( *hyp );
557         nbHyp++;
558       }
559     }
560   }
561   return nbHyp;
562 }
563
564 //=============================================================================
565 /*!
566  * 
567  */
568 //=============================================================================
569
570 const list<SMESHDS_Command*> & SMESH_Mesh::GetLog() throw(SALOME_Exception)
571 {
572   Unexpect aCatch(SalomeException);
573   if(MYDEBUG) MESSAGE("SMESH_Mesh::GetLog");
574   return _myMeshDS->GetScript()->GetCommands();
575 }
576
577 //=============================================================================
578 /*!
579  * 
580  */
581 //=============================================================================
582 void SMESH_Mesh::ClearLog() throw(SALOME_Exception)
583 {
584   Unexpect aCatch(SalomeException);
585   if(MYDEBUG) MESSAGE("SMESH_Mesh::ClearLog");
586   _myMeshDS->GetScript()->Clear();
587 }
588
589 //=============================================================================
590 /*!
591  * 
592  */
593 //=============================================================================
594
595 int SMESH_Mesh::GetId()
596 {
597   if(MYDEBUG) MESSAGE("SMESH_Mesh::GetId");
598   return _id;
599 }
600
601 //=============================================================================
602 /*!
603  * 
604  */
605 //=============================================================================
606
607 SMESH_Gen *SMESH_Mesh::GetGen()
608 {
609   return _gen;
610 }
611
612 //=============================================================================
613 /*!
614  * Get or Create the SMESH_subMesh object implementation
615  */
616 //=============================================================================
617
618 SMESH_subMesh *SMESH_Mesh::GetSubMesh(const TopoDS_Shape & aSubShape)
619 throw(SALOME_Exception)
620 {
621   Unexpect aCatch(SalomeException);
622   SMESH_subMesh *aSubMesh;
623   int index = _myMeshDS->ShapeToIndex(aSubShape);
624   
625   // for submeshes on GEOM Group
626   if ( !index && aSubShape.ShapeType() == TopAbs_COMPOUND ) {
627     TopoDS_Iterator it( aSubShape );
628     if ( it.More() )
629       index = _myMeshDS->AddCompoundSubmesh( aSubShape, it.Value().ShapeType() );
630   }
631
632   if (_mapSubMesh.find(index) != _mapSubMesh.end())
633     {
634       aSubMesh = _mapSubMesh[index];
635     }
636   else
637     {
638       aSubMesh = new SMESH_subMesh(index, this, _myMeshDS, aSubShape);
639       _mapSubMesh[index] = aSubMesh;
640     }
641   return aSubMesh;
642 }
643
644 //=============================================================================
645 /*!
646  * Get the SMESH_subMesh object implementation. Dont create it, return null
647  * if it does not exist.
648  */
649 //=============================================================================
650
651 SMESH_subMesh *SMESH_Mesh::GetSubMeshContaining(const TopoDS_Shape & aSubShape)
652 throw(SALOME_Exception)
653 {
654   Unexpect aCatch(SalomeException);
655   bool isFound = false;
656   SMESH_subMesh *aSubMesh = NULL;
657   
658   int index = _myMeshDS->ShapeToIndex(aSubShape);
659   if (_mapSubMesh.find(index) != _mapSubMesh.end())
660     {
661       aSubMesh = _mapSubMesh[index];
662       isFound = true;
663     }
664   if (!isFound)
665     aSubMesh = NULL;
666   return aSubMesh;
667 }
668
669 //=============================================================================
670 /*!
671  * Get the SMESH_subMesh object implementation. Dont create it, return null
672  * if it does not exist.
673  */
674 //=============================================================================
675
676 SMESH_subMesh *SMESH_Mesh::GetSubMeshContaining(const int aShapeID)
677 throw(SALOME_Exception)
678 {
679   Unexpect aCatch(SalomeException);
680   
681   map <int, SMESH_subMesh *>::iterator i_sm = _mapSubMesh.find(aShapeID);
682   if (i_sm == _mapSubMesh.end())
683     return NULL;
684   return i_sm->second;
685 }
686
687 //=======================================================================
688 //function : IsUsedHypothesis
689 //purpose  : Return True if anHyp is used to mesh aSubShape
690 //=======================================================================
691
692 bool SMESH_Mesh::IsUsedHypothesis(SMESHDS_Hypothesis * anHyp,
693                                   const TopoDS_Shape & aSubShape)
694 {
695   SMESH_Hypothesis* hyp = static_cast<SMESH_Hypothesis*>(anHyp);
696   // check if anHyp is applicable to aSubShape
697   SMESH_subMesh * subMesh = GetSubMeshContaining( aSubShape );
698   if ( !subMesh || !subMesh->IsApplicableHypotesis( hyp ))
699     return false;
700
701   SMESH_Algo *algo = _gen->GetAlgo(*this, aSubShape);
702
703   // algorithm
704   if (anHyp->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
705     return ( anHyp == algo );
706
707   // algorithm parameter
708   if (algo)
709   {
710     // look trough hypotheses used by algo
711     const list <const SMESHDS_Hypothesis * >&usedHyps =
712       algo->GetUsedHypothesis(*this, aSubShape);
713     return ( find( usedHyps.begin(), usedHyps.end(), anHyp ) != usedHyps.end() );
714   }
715
716   // look through all assigned hypotheses
717   SMESH_HypoFilter filter( SMESH_HypoFilter::Is( hyp ));
718   return GetHypothesis( aSubShape, filter, true );
719 }
720
721
722 //=============================================================================
723 /*!
724  *
725  */
726 //=============================================================================
727
728 const list < SMESH_subMesh * >&
729         SMESH_Mesh::GetSubMeshUsingHypothesis(SMESHDS_Hypothesis * anHyp)
730 throw(SALOME_Exception)
731 {
732   Unexpect aCatch(SalomeException);
733         if(MYDEBUG) MESSAGE("SMESH_Mesh::GetSubMeshUsingHypothesis");
734         map < int, SMESH_subMesh * >::iterator itsm;
735         _subMeshesUsingHypothesisList.clear();
736         for (itsm = _mapSubMesh.begin(); itsm != _mapSubMesh.end(); itsm++)
737         {
738                 SMESH_subMesh *aSubMesh = (*itsm).second;
739                 if ( IsUsedHypothesis ( anHyp, aSubMesh->GetSubShape() ))
740                         _subMeshesUsingHypothesisList.push_back(aSubMesh);
741         }
742         return _subMeshesUsingHypothesisList;
743 }
744
745 //=============================================================================
746 /*!
747  *
748  */
749 //=============================================================================
750
751 void SMESH_Mesh::ExportMED(const char *file, 
752                            const char* theMeshName, 
753                            bool theAutoGroups,
754                            int theVersion) 
755   throw(SALOME_Exception)
756 {
757   Unexpect aCatch(SalomeException);
758   DriverMED_W_SMESHDS_Mesh myWriter;
759   myWriter.SetFile    ( file, MED::EVersion(theVersion) );
760   myWriter.SetMesh    ( _myMeshDS   );
761   if ( !theMeshName ) 
762     myWriter.SetMeshId  ( _idDoc      );
763   else {
764     myWriter.SetMeshId  ( -1          );
765     myWriter.SetMeshName( theMeshName );
766   }
767
768   if ( theAutoGroups ) {
769     myWriter.AddGroupOfNodes();
770     myWriter.AddGroupOfEdges();
771     myWriter.AddGroupOfFaces();
772     myWriter.AddGroupOfVolumes();
773   }
774
775   for ( map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ ) {
776     SMESH_Group*       aGroup   = it->second;
777     SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
778     if ( aGroupDS ) {
779       aGroupDS->SetStoreName( aGroup->GetName() );
780       myWriter.AddGroup( aGroupDS );
781     }
782   }
783
784   myWriter.Perform();
785 }
786
787 void SMESH_Mesh::ExportDAT(const char *file) throw(SALOME_Exception)
788 {
789   Unexpect aCatch(SalomeException);
790   DriverDAT_W_SMDS_Mesh myWriter;
791   myWriter.SetFile(string(file));
792   myWriter.SetMesh(_myMeshDS);
793   myWriter.SetMeshId(_idDoc);
794   myWriter.Perform();
795 }
796
797 void SMESH_Mesh::ExportUNV(const char *file) throw(SALOME_Exception)
798 {
799   Unexpect aCatch(SalomeException);
800   DriverUNV_W_SMDS_Mesh myWriter;
801   myWriter.SetFile(string(file));
802   myWriter.SetMesh(_myMeshDS);
803   myWriter.SetMeshId(_idDoc);
804   myWriter.Perform();
805 }
806
807 void SMESH_Mesh::ExportSTL(const char *file, const bool isascii) throw(SALOME_Exception)
808 {
809   Unexpect aCatch(SalomeException);
810   DriverSTL_W_SMDS_Mesh myWriter;
811   myWriter.SetFile(string(file));
812   myWriter.SetIsAscii( isascii );
813   myWriter.SetMesh(_myMeshDS);
814   myWriter.SetMeshId(_idDoc);
815   myWriter.Perform();
816 }
817
818 //=============================================================================
819 /*!
820  *  
821  */
822 //=============================================================================
823 int SMESH_Mesh::NbNodes() throw(SALOME_Exception)
824 {
825   Unexpect aCatch(SalomeException);
826   return _myMeshDS->NbNodes();
827 }
828
829 //=============================================================================
830 /*!
831  *  
832  */
833 //=============================================================================
834 int SMESH_Mesh::NbEdges() throw(SALOME_Exception)
835 {
836   Unexpect aCatch(SalomeException);
837   return _myMeshDS->NbEdges();
838 }
839
840 //=============================================================================
841 /*!
842  *  
843  */
844 //=============================================================================
845 int SMESH_Mesh::NbFaces() throw(SALOME_Exception)
846 {
847   Unexpect aCatch(SalomeException);
848   return _myMeshDS->NbFaces();
849 }
850
851 ///////////////////////////////////////////////////////////////////////////////
852 /// Return the number of 3 nodes faces in the mesh. This method run in O(n)
853 ///////////////////////////////////////////////////////////////////////////////
854 int SMESH_Mesh::NbTriangles() throw(SALOME_Exception)
855 {
856   Unexpect aCatch(SalomeException);
857   int Nb = 0;
858   
859   SMDS_FaceIteratorPtr itFaces=_myMeshDS->facesIterator();
860   //while(itFaces->more()) if(itFaces->next()->NbNodes()==3) Nb++;
861   const SMDS_MeshFace * curFace;
862   while (itFaces->more()) {
863     curFace = itFaces->next();
864     if (!curFace->IsPoly() && curFace->NbNodes() == 3) Nb++;
865   }
866   return Nb;
867 }
868
869 ///////////////////////////////////////////////////////////////////////////////
870 /// Return the number of 4 nodes faces in the mesh. This method run in O(n)
871 ///////////////////////////////////////////////////////////////////////////////
872 int SMESH_Mesh::NbQuadrangles() throw(SALOME_Exception)
873 {
874   Unexpect aCatch(SalomeException);
875   int Nb = 0;
876   
877   SMDS_FaceIteratorPtr itFaces=_myMeshDS->facesIterator();
878   //while(itFaces->more()) if(itFaces->next()->NbNodes()==4) Nb++;
879   const SMDS_MeshFace * curFace;
880   while (itFaces->more()) {
881     curFace = itFaces->next();
882     if (!curFace->IsPoly() && curFace->NbNodes() == 4) Nb++;
883   }
884   return Nb;
885 }
886
887 ///////////////////////////////////////////////////////////////////////////////
888 /// Return the number of polygonal faces in the mesh. This method run in O(n)
889 ///////////////////////////////////////////////////////////////////////////////
890 int SMESH_Mesh::NbPolygons() throw(SALOME_Exception)
891 {
892   Unexpect aCatch(SalomeException);
893   int Nb = 0;
894   SMDS_FaceIteratorPtr itFaces = _myMeshDS->facesIterator();
895   while (itFaces->more())
896     if (itFaces->next()->IsPoly()) Nb++;
897   return Nb;
898 }
899
900 //=============================================================================
901 /*!
902  *  
903  */
904 //=============================================================================
905 int SMESH_Mesh::NbVolumes() throw(SALOME_Exception)
906 {
907   Unexpect aCatch(SalomeException);
908   return _myMeshDS->NbVolumes();
909 }
910
911 int SMESH_Mesh::NbTetras() throw(SALOME_Exception)
912 {
913   Unexpect aCatch(SalomeException);
914   int Nb = 0;
915   SMDS_VolumeIteratorPtr itVolumes=_myMeshDS->volumesIterator();
916   //while(itVolumes->more()) if(itVolumes->next()->NbNodes()==4) Nb++;
917   const SMDS_MeshVolume * curVolume;
918   while (itVolumes->more()) {
919     curVolume = itVolumes->next();
920     if (!curVolume->IsPoly() && curVolume->NbNodes() == 4) Nb++;
921   }
922   return Nb;
923 }
924
925 int SMESH_Mesh::NbHexas() throw(SALOME_Exception)
926 {
927   Unexpect aCatch(SalomeException);
928   int Nb = 0;
929   SMDS_VolumeIteratorPtr itVolumes=_myMeshDS->volumesIterator();
930   //while(itVolumes->more()) if(itVolumes->next()->NbNodes()==8) Nb++;
931   const SMDS_MeshVolume * curVolume;
932   while (itVolumes->more()) {
933     curVolume = itVolumes->next();
934     if (!curVolume->IsPoly() && curVolume->NbNodes() == 8) Nb++;
935   }
936   return Nb;
937 }
938
939 int SMESH_Mesh::NbPyramids() throw(SALOME_Exception)
940 {
941   Unexpect aCatch(SalomeException);
942   int Nb = 0;
943   SMDS_VolumeIteratorPtr itVolumes=_myMeshDS->volumesIterator();
944   //while(itVolumes->more()) if(itVolumes->next()->NbNodes()==5) Nb++;
945   const SMDS_MeshVolume * curVolume;
946   while (itVolumes->more()) {
947     curVolume = itVolumes->next();
948     if (!curVolume->IsPoly() && curVolume->NbNodes() == 5) Nb++;
949   }
950   return Nb;
951 }
952
953 int SMESH_Mesh::NbPrisms() throw(SALOME_Exception)
954 {
955   Unexpect aCatch(SalomeException);
956   int Nb = 0;
957   SMDS_VolumeIteratorPtr itVolumes=_myMeshDS->volumesIterator();
958   //while(itVolumes->more()) if(itVolumes->next()->NbNodes()==6) Nb++;
959   const SMDS_MeshVolume * curVolume;
960   while (itVolumes->more()) {
961     curVolume = itVolumes->next();
962     if (!curVolume->IsPoly() && curVolume->NbNodes() == 6) Nb++;
963   }
964   return Nb;
965 }
966
967 int SMESH_Mesh::NbPolyhedrons() throw(SALOME_Exception)
968 {
969   Unexpect aCatch(SalomeException);
970   int Nb = 0;
971   SMDS_VolumeIteratorPtr itVolumes = _myMeshDS->volumesIterator();
972   while (itVolumes->more())
973     if (itVolumes->next()->IsPoly()) Nb++;
974   return Nb;
975 }
976
977 //=============================================================================
978 /*!
979  *  
980  */
981 //=============================================================================
982 int SMESH_Mesh::NbSubMesh() throw(SALOME_Exception)
983 {
984   Unexpect aCatch(SalomeException);
985   return _myMeshDS->NbSubMesh();
986 }
987
988 //=======================================================================
989 //function : IsNotConformAllowed
990 //purpose  : check if a hypothesis alowing notconform mesh is present
991 //=======================================================================
992
993 bool SMESH_Mesh::IsNotConformAllowed() const
994 {
995   if(MYDEBUG) MESSAGE("SMESH_Mesh::IsNotConformAllowed");
996
997   SMESH_HypoFilter filter( SMESH_HypoFilter::HasName( "NotConformAllowed" ));
998   return GetHypothesis( _myMeshDS->ShapeToMesh(), filter, false );
999 }
1000
1001 //=======================================================================
1002 //function : IsMainShape
1003 //purpose  : 
1004 //=======================================================================
1005
1006 bool SMESH_Mesh::IsMainShape(const TopoDS_Shape& theShape) const
1007 {
1008   return theShape.IsSame(_myMeshDS->ShapeToMesh() );
1009 }
1010
1011 //=============================================================================
1012 /*!
1013  *  
1014  */
1015 //=============================================================================
1016
1017 SMESH_Group* SMESH_Mesh::AddGroup (const SMDSAbs_ElementType theType,
1018                                    const char*               theName,
1019                                    int&                      theId,
1020                                    const TopoDS_Shape&       theShape)
1021 {
1022   if (_mapGroup.find(_groupId) != _mapGroup.end())
1023     return NULL;
1024   theId = _groupId;
1025   SMESH_Group* aGroup = new SMESH_Group (theId, this, theType, theName, theShape);
1026   GetMeshDS()->AddGroup( aGroup->GetGroupDS() );
1027   _mapGroup[_groupId++] = aGroup;
1028   return aGroup;
1029 }
1030
1031 //=============================================================================
1032 /*!
1033  *  
1034  */
1035 //=============================================================================
1036
1037 SMESH_Group* SMESH_Mesh::GetGroup (const int theGroupID)
1038 {
1039   if (_mapGroup.find(theGroupID) == _mapGroup.end())
1040     return NULL;
1041   return _mapGroup[theGroupID];
1042 }
1043
1044
1045 //=============================================================================
1046 /*!
1047  *  
1048  */
1049 //=============================================================================
1050
1051 list<int> SMESH_Mesh::GetGroupIds()
1052 {
1053   list<int> anIds;
1054   for ( map<int, SMESH_Group*>::const_iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ )
1055     anIds.push_back( it->first );
1056   
1057   return anIds;
1058 }
1059
1060
1061 //=============================================================================
1062 /*!
1063  *  
1064  */
1065 //=============================================================================
1066
1067 void SMESH_Mesh::RemoveGroup (const int theGroupID)
1068 {
1069   if (_mapGroup.find(theGroupID) == _mapGroup.end())
1070     return;
1071   GetMeshDS()->RemoveGroup( _mapGroup[theGroupID]->GetGroupDS() );
1072   _mapGroup.erase (theGroupID);
1073   delete _mapGroup[theGroupID];
1074 }
1075
1076 //=============================================================================
1077 /*!
1078  *  IsLocal1DHypothesis
1079  *  Returns a local 1D hypothesis used for theEdge
1080  */
1081 //=============================================================================
1082 const SMESH_Hypothesis* SMESH_Mesh::IsLocal1DHypothesis (const TopoDS_Shape& theEdge)
1083 {
1084   SMESH_HypoFilter hypo ( SMESH_HypoFilter::HasDim( 1 ));
1085   hypo.AndNot( hypo.IsAlgo() ).AndNot( hypo.IsAssignedTo( GetMeshDS()->ShapeToMesh() ));
1086
1087   return GetHypothesis( theEdge, hypo, true );
1088 }
1089
1090 //=============================================================================
1091 /*!
1092  *  IsPropagationHypothesis
1093  */
1094 //=============================================================================
1095 bool SMESH_Mesh::IsPropagationHypothesis (const TopoDS_Shape& theEdge)
1096 {
1097   return _mapPropagationChains.Contains(theEdge);
1098 }
1099
1100 //=============================================================================
1101 /*!
1102  *  IsPropagatedHypothesis
1103  */
1104 //=============================================================================
1105 bool SMESH_Mesh::IsPropagatedHypothesis (const TopoDS_Shape& theEdge,
1106                                          TopoDS_Shape&       theMainEdge)
1107 {
1108   int nbChains = _mapPropagationChains.Extent();
1109   for (int i = 1; i <= nbChains; i++) {
1110     //const TopTools_IndexedMapOfShape& aChain = _mapPropagationChains.FindFromIndex(i);
1111     const SMESH_IndexedMapOfShape& aChain = _mapPropagationChains.FindFromIndex(i);
1112     if (aChain.Contains(theEdge)) {
1113       theMainEdge = _mapPropagationChains.FindKey(i);
1114       return true;
1115     }
1116   }
1117
1118   return false;
1119 }
1120 //=============================================================================
1121 /*!
1122  *  IsReversedInChain
1123  */
1124 //=============================================================================
1125
1126 bool SMESH_Mesh::IsReversedInChain (const TopoDS_Shape& theEdge,
1127                                     const TopoDS_Shape& theMainEdge)
1128 {
1129   if ( !theMainEdge.IsNull() && !theEdge.IsNull() &&
1130       _mapPropagationChains.Contains( theMainEdge ))
1131   {
1132     const SMESH_IndexedMapOfShape& aChain =
1133       _mapPropagationChains.FindFromKey( theMainEdge );
1134     int index = aChain.FindIndex( theEdge );
1135     if ( index )
1136       return aChain(index).Orientation() == TopAbs_REVERSED;
1137   }
1138   return false;
1139 }
1140
1141 //=============================================================================
1142 /*!
1143  *  CleanMeshOnPropagationChain
1144  */
1145 //=============================================================================
1146 void SMESH_Mesh::CleanMeshOnPropagationChain (const TopoDS_Shape& theMainEdge)
1147 {
1148   const SMESH_IndexedMapOfShape& aChain = _mapPropagationChains.FindFromKey(theMainEdge);
1149   int i, nbEdges = aChain.Extent();
1150   for (i = 1; i <= nbEdges; i++) {
1151     TopoDS_Shape anEdge = aChain.FindKey(i);
1152     SMESH_subMesh *subMesh = GetSubMesh(anEdge);
1153     SMESHDS_SubMesh *subMeshDS = subMesh->GetSubMeshDS();
1154     if (subMeshDS && subMeshDS->NbElements() > 0) {
1155       subMesh->ComputeStateEngine(SMESH_subMesh::CLEANDEP);
1156     }
1157   }
1158 }
1159
1160 //=============================================================================
1161 /*!
1162  *  RebuildPropagationChains
1163  *  Rebuild all existing propagation chains.
1164  *  Have to be used, if 1D hypothesis have been assigned/removed to/from any edge
1165  */
1166 //=============================================================================
1167 bool SMESH_Mesh::RebuildPropagationChains()
1168 {
1169   bool ret = true;
1170
1171   // Clean all chains, because they can be not up-to-date
1172   int i, nbChains = _mapPropagationChains.Extent();
1173   for (i = 1; i <= nbChains; i++) {
1174     TopoDS_Shape aMainEdge = _mapPropagationChains.FindKey(i);
1175     CleanMeshOnPropagationChain(aMainEdge);
1176     _mapPropagationChains.ChangeFromIndex(i).Clear();
1177   }
1178
1179   // Build all chains
1180   for (i = 1; i <= nbChains; i++) {
1181     TopoDS_Shape aMainEdge = _mapPropagationChains.FindKey(i);
1182     if (!BuildPropagationChain(aMainEdge))
1183       ret = false;
1184     CleanMeshOnPropagationChain(aMainEdge);
1185   }
1186
1187   return ret;
1188 }
1189
1190 //=============================================================================
1191 /*!
1192  *  RemovePropagationChain
1193  *  Have to be used, if Propagation hypothesis is removed from <theMainEdge>
1194  */
1195 //=============================================================================
1196 bool SMESH_Mesh::RemovePropagationChain (const TopoDS_Shape& theMainEdge)
1197 {
1198   if (!_mapPropagationChains.Contains(theMainEdge))
1199     return false;
1200
1201   // Clean mesh elements and nodes, built on the chain
1202   CleanMeshOnPropagationChain(theMainEdge);
1203
1204   // Clean the chain
1205   _mapPropagationChains.ChangeFromKey(theMainEdge).Clear();
1206
1207   // Remove the chain from the map
1208   int i = _mapPropagationChains.FindIndex(theMainEdge);
1209   if ( i == _mapPropagationChains.Extent() )
1210     _mapPropagationChains.RemoveLast();
1211   else {
1212     TopoDS_Vertex anEmptyShape;
1213     BRep_Builder BB;
1214     BB.MakeVertex(anEmptyShape, gp_Pnt(0,0,0), 0.1);
1215     SMESH_IndexedMapOfShape anEmptyMap;
1216     _mapPropagationChains.Substitute(i, anEmptyShape, anEmptyMap);
1217   }
1218
1219   return true;
1220 }
1221
1222 //=============================================================================
1223 /*!
1224  *  BuildPropagationChain
1225  */
1226 //=============================================================================
1227 bool SMESH_Mesh::BuildPropagationChain (const TopoDS_Shape& theMainEdge)
1228 {
1229   if (theMainEdge.ShapeType() != TopAbs_EDGE) return true;
1230
1231   // Add new chain, if there is no
1232   if (!_mapPropagationChains.Contains(theMainEdge)) {
1233     SMESH_IndexedMapOfShape aNewChain;
1234     _mapPropagationChains.Add(theMainEdge, aNewChain);
1235   }
1236
1237   // Check presence of 1D hypothesis to be propagated
1238   const SMESH_Hypothesis* aMainHyp = IsLocal1DHypothesis(theMainEdge);
1239   if (!aMainHyp) {
1240     MESSAGE("Warning: There is no 1D hypothesis to propagate. Please, assign.");
1241     return true;
1242   }
1243
1244   // Edges, on which the 1D hypothesis will be propagated from <theMainEdge>
1245   SMESH_IndexedMapOfShape& aChain = _mapPropagationChains.ChangeFromKey(theMainEdge);
1246   if (aChain.Extent() > 0) {
1247     CleanMeshOnPropagationChain(theMainEdge);
1248     aChain.Clear();
1249   }
1250
1251   // At first put <theMainEdge> in the chain
1252   aChain.Add(theMainEdge);
1253
1254   // List of edges, added to chain on the previous cycle pass
1255   TopTools_ListOfShape listPrevEdges;
1256   listPrevEdges.Append(theMainEdge.Oriented( TopAbs_FORWARD ));
1257
1258 //   5____4____3____4____5____6
1259 //   |    |    |    |    |    |
1260 //   |    |    |    |    |    |
1261 //   4____3____2____3____4____5
1262 //   |    |    |    |    |    |      Number in the each knot of
1263 //   |    |    |    |    |    |      grid indicates cycle pass,
1264 //   3____2____1____2____3____4      on which corresponding edge
1265 //   |    |    |    |    |    |      (perpendicular to the plane
1266 //   |    |    |    |    |    |      of view) will be found.
1267 //   2____1____0____1____2____3
1268 //   |    |    |    |    |    |
1269 //   |    |    |    |    |    |
1270 //   3____2____1____2____3____4
1271
1272   // Collect all edges pass by pass
1273   while (listPrevEdges.Extent() > 0) {
1274     // List of edges, added to chain on this cycle pass
1275     TopTools_ListOfShape listCurEdges;
1276
1277     // Find the next portion of edges
1278     TopTools_ListIteratorOfListOfShape itE (listPrevEdges);
1279     for (; itE.More(); itE.Next()) {
1280       TopoDS_Shape anE = itE.Value();
1281
1282       // Iterate on faces, having edge <anE>
1283       TopTools_ListIteratorOfListOfShape itA (GetAncestors(anE));
1284       for (; itA.More(); itA.Next()) {
1285         TopoDS_Shape aW = itA.Value();
1286
1287         // There are objects of different type among the ancestors of edge
1288         if (aW.ShapeType() == TopAbs_WIRE) {
1289           TopoDS_Shape anOppE;
1290
1291           BRepTools_WireExplorer aWE (TopoDS::Wire(aW));
1292           Standard_Integer nb = 1, found = 0;
1293           TopTools_Array1OfShape anEdges (1,4);
1294           for (; aWE.More(); aWE.Next(), nb++) {
1295             if (nb > 4) {
1296               found = 0;
1297               break;
1298             }
1299             anEdges(nb) = aWE.Current();
1300             if (!_mapAncestors.Contains(anEdges(nb))) {
1301               MESSAGE("WIRE EXPLORER HAVE GIVEN AN INVALID EDGE !!!");
1302               break;
1303             }
1304             if (anEdges(nb).IsSame(anE)) found = nb;
1305           }
1306
1307           if (nb == 5 && found > 0) {
1308             // Quadrangle face found, get an opposite edge
1309             Standard_Integer opp = found + 2;
1310             if (opp > 4) opp -= 4;
1311             anOppE = anEdges(opp);
1312
1313             // add anOppE to aChain if ...
1314             if (!aChain.Contains(anOppE)) { // ... anOppE is not in aChain
1315               if (!IsLocal1DHypothesis(anOppE)) { // ... no other 1d hyp on anOppE
1316                 TopoDS_Shape aMainEdgeForOppEdge; // ... no other hyp is propagated to anOppE
1317                 if (!IsPropagatedHypothesis(anOppE, aMainEdgeForOppEdge))
1318                 {
1319                   // Add found edge to the chain oriented so that to
1320                   // have it co-directed with a forward MainEdge
1321                   TopAbs_Orientation ori = anE.Orientation();
1322                   if ( anEdges(opp).Orientation() == anEdges(found).Orientation() )
1323                     ori = TopAbs::Reverse( ori );
1324                   anOppE.Orientation( ori );
1325                   aChain.Add(anOppE);
1326                   listCurEdges.Append(anOppE);
1327                 }
1328                 else {
1329                   // Collision!
1330                   MESSAGE("Error: Collision between propagated hypotheses");
1331                   CleanMeshOnPropagationChain(theMainEdge);
1332                   aChain.Clear();
1333                   return ( aMainHyp == IsLocal1DHypothesis(aMainEdgeForOppEdge) );
1334                 }
1335               }
1336             }
1337           } // if (nb == 5 && found > 0)
1338         } // if (aF.ShapeType() == TopAbs_WIRE)
1339       } // for (; itF.More(); itF.Next())
1340     } // for (; itE.More(); itE.Next())
1341
1342     listPrevEdges = listCurEdges;
1343   } // while (listPrevEdges.Extent() > 0)
1344
1345   CleanMeshOnPropagationChain(theMainEdge);
1346   return true;
1347 }
1348
1349 //=======================================================================
1350 //function : GetAncestors
1351 //purpose  : return list of ancestors of theSubShape in the order
1352 //           that lower dimention shapes come first.
1353 //=======================================================================
1354
1355 const TopTools_ListOfShape& SMESH_Mesh::GetAncestors(const TopoDS_Shape& theS) const
1356 {
1357   if ( _mapAncestors.Contains( theS ) )
1358     return _mapAncestors.FindFromKey( theS );
1359
1360   static TopTools_ListOfShape emptyList;
1361   return emptyList;
1362 }
1363
1364 //=======================================================================
1365 //function : Dump
1366 //purpose  : dumps contents of mesh to stream [ debug purposes ]
1367 //=======================================================================
1368 ostream& SMESH_Mesh::Dump(ostream& save)
1369 {
1370   save << "========================== Dump contents of mesh ==========================" << endl;
1371   save << "1) Total number of nodes:     " << NbNodes() << endl;
1372   save << "2) Total number of edges:     " << NbEdges() << endl;
1373   save << "3) Total number of faces:     " << NbFaces() << endl;
1374   if ( NbFaces() > 0 ) {
1375     int nb3 = NbTriangles();
1376     int nb4 = NbQuadrangles();
1377     save << "3.1.) Number of triangles:    " << nb3 << endl;
1378     save << "3.2.) Number of quadrangles:  " << nb4 << endl;
1379     if ( nb3 + nb4 !=  NbFaces() ) {
1380       map<int,int> myFaceMap;
1381       SMDS_FaceIteratorPtr itFaces=_myMeshDS->facesIterator();
1382       while( itFaces->more( ) ) {
1383         int nbNodes = itFaces->next()->NbNodes();
1384         if ( myFaceMap.find( nbNodes ) == myFaceMap.end() )
1385           myFaceMap[ nbNodes ] = 0;
1386         myFaceMap[ nbNodes ] = myFaceMap[ nbNodes ] + 1;
1387       }
1388       save << "3.3.) Faces in detail: " << endl;
1389       map <int,int>::iterator itF;
1390       for (itF = myFaceMap.begin(); itF != myFaceMap.end(); itF++)
1391         save << "--> nb nodes: " << itF->first << " - nb elemens: " << itF->second << endl;
1392     }
1393   }
1394   save << "4) Total number of volumes:   " << NbVolumes() << endl;
1395   if ( NbVolumes() > 0 ) {
1396     int nb8 = NbHexas();
1397     int nb4 = NbTetras();
1398     int nb5 = NbPyramids();
1399     int nb6 = NbPrisms();
1400     save << "4.1.) Number of hexahedrons:  " << nb8 << endl;
1401     save << "4.2.) Number of tetrahedrons: " << nb4 << endl;
1402     save << "4.3.) Number of prisms:       " << nb6 << endl;
1403     save << "4.4.) Number of pyramides:    " << nb5 << endl;
1404     if ( nb8 + nb4 + nb5 + nb6 != NbVolumes() ) {
1405       map<int,int> myVolumesMap;
1406       SMDS_VolumeIteratorPtr itVolumes=_myMeshDS->volumesIterator();
1407       while( itVolumes->more( ) ) {
1408         int nbNodes = itVolumes->next()->NbNodes();
1409         if ( myVolumesMap.find( nbNodes ) == myVolumesMap.end() )
1410           myVolumesMap[ nbNodes ] = 0;
1411         myVolumesMap[ nbNodes ] = myVolumesMap[ nbNodes ] + 1;
1412       }
1413       save << "4.5.) Volumes in detail: " << endl;
1414       map <int,int>::iterator itV;
1415       for (itV = myVolumesMap.begin(); itV != myVolumesMap.end(); itV++)
1416         save << "--> nb nodes: " << itV->first << " - nb elemens: " << itV->second << endl;
1417     }
1418   }
1419   save << "===========================================================================" << endl;
1420   return save;
1421 }
1422
1423 //=======================================================================
1424 //function : GetElementType
1425 //purpose  : Returns type of mesh element with certain id
1426 //=======================================================================
1427 SMDSAbs_ElementType SMESH_Mesh::GetElementType( const int id, const bool iselem )
1428 {
1429   return _myMeshDS->GetElementType( id, iselem );
1430 }