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