Salome HOME
PR: merged from V5_1_4rc1
[modules/smesh.git] / src / SMESH / SMESH_Gen.cxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  SMESH SMESH : implementaion of SMESH idl descriptions
24 //  File   : SMESH_Gen.cxx
25 //  Author : Paul RASCLE, EDF
26 //  Module : SMESH
27 //
28 #include "SMESH_Gen.hxx"
29 #include "SMESH_subMesh.hxx"
30 #include "SMESH_HypoFilter.hxx"
31 #include "SMESHDS_Document.hxx"
32 #include "SMDS_MeshElement.hxx"
33 #include "SMDS_MeshNode.hxx"
34 #include "SMDS_Mesh.hxx"
35
36 #include "utilities.h"
37 #include "OpUtil.hxx"
38 #include "Utils_ExceptHandlers.hxx"
39
40 #include <gp_Pnt.hxx>
41 #include <BRep_Tool.hxx>
42 #include <TopTools_ListOfShape.hxx>
43 #include <TopTools_ListIteratorOfListOfShape.hxx>
44
45 #include "memoire.h"
46
47 using namespace std;
48
49 //=============================================================================
50 /*!
51  *  Constructor
52  */
53 //=============================================================================
54
55 SMESH_Gen::SMESH_Gen()
56 {
57         MESSAGE("SMESH_Gen::SMESH_Gen");
58         _localId = 0;
59         _hypId = 0;
60         _segmentation = 10;
61         SMDS_Mesh::_meshList.clear();
62         MESSAGE(SMDS_Mesh::_meshList.size());
63 }
64
65 //=============================================================================
66 /*!
67  * Destructor
68  */
69 //=============================================================================
70
71 SMESH_Gen::~SMESH_Gen()
72 {
73   MESSAGE("SMESH_Gen::~SMESH_Gen");
74 }
75
76 //=============================================================================
77 /*!
78  * Creates a mesh in a study.
79  * if (theIsEmbeddedMode) { mesh modification commands are not logged }
80  */
81 //=============================================================================
82
83 SMESH_Mesh* SMESH_Gen::CreateMesh(int theStudyId, bool theIsEmbeddedMode)
84   throw(SALOME_Exception)
85 {
86   Unexpect aCatch(SalomeException);
87   MESSAGE("SMESH_Gen::CreateMesh");
88
89   // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
90   StudyContextStruct *aStudyContext = GetStudyContext(theStudyId);
91
92   // create a new SMESH_mesh object
93   SMESH_Mesh *aMesh = new SMESH_Mesh(_localId++,
94                                      theStudyId,
95                                      this,
96                                      theIsEmbeddedMode,
97                                      aStudyContext->myDocument);
98   aStudyContext->mapMesh[_localId] = aMesh;
99
100   return aMesh;
101 }
102
103 //=============================================================================
104 /*!
105  * Compute a mesh
106  */
107 //=============================================================================
108
109 bool SMESH_Gen::Compute(SMESH_Mesh &          aMesh,
110                         const TopoDS_Shape &  aShape,
111                         const bool            anUpward,
112                         const ::MeshDimension aDim,
113                         TSetOfInt*            aShapesId)
114 {
115   MESSAGE("SMESH_Gen::Compute");
116   MEMOSTAT;
117
118   bool ret = true;
119
120   SMESH_subMesh *sm = aMesh.GetSubMesh(aShape);
121
122   const bool includeSelf = true;
123   const bool complexShapeFirst = true;
124
125   SMESH_subMeshIteratorPtr smIt;
126
127   if ( anUpward ) // is called from below code here
128   {
129     // -----------------------------------------------
130     // mesh all the subshapes starting from vertices
131     // -----------------------------------------------
132     smIt = sm->getDependsOnIterator(includeSelf, !complexShapeFirst);
133     while ( smIt->more() )
134     {
135       SMESH_subMesh* smToCompute = smIt->next();
136
137       // do not mesh vertices of a pseudo shape
138       const TopAbs_ShapeEnum aShType = smToCompute->GetSubShape().ShapeType();
139       if ( !aMesh.HasShapeToMesh() && aShType == TopAbs_VERTEX )
140         continue;
141
142       // check for preview dimension limitations
143       if ( aShapesId && GetShapeDim( aShType ) > (int)aDim )
144       {
145         // clear compute state to not show previous compute errors
146         //  if preview invoked less dimension less than previous
147         smToCompute->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
148         continue;
149       }
150
151       if (smToCompute->GetComputeState() == SMESH_subMesh::READY_TO_COMPUTE)
152         smToCompute->ComputeStateEngine( SMESH_subMesh::COMPUTE );
153
154       // we check all the submeshes here and detect if any of them failed to compute
155       if (smToCompute->GetComputeState() == SMESH_subMesh::FAILED_TO_COMPUTE)
156         ret = false;
157       else if ( aShapesId )
158         aShapesId->insert( smToCompute->GetId() );
159     }
160     return ret;
161   }
162   else
163   {
164     // -----------------------------------------------------------------
165     // apply algos that DO NOT require descretized boundaries and DO NOT
166     // support submeshes, starting from the most complex shapes
167     // and collect submeshes with algos that DO support submeshes
168     // -----------------------------------------------------------------
169     list< SMESH_subMesh* > smWithAlgoSupportingSubmeshes;
170     smIt = sm->getDependsOnIterator(includeSelf, complexShapeFirst);
171     while ( smIt->more() )
172     {
173       SMESH_subMesh* smToCompute = smIt->next();
174       if ( smToCompute->GetComputeState() != SMESH_subMesh::READY_TO_COMPUTE )
175         continue;
176
177       const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
178       const int aShapeDim = GetShapeDim( aSubShape );
179       if ( aShapeDim < 1 ) break;
180       
181       // check for preview dimension limitations
182       if ( aShapesId && aShapeDim > (int)aDim )
183         continue;
184
185       SMESH_Algo* algo = GetAlgo( aMesh, aSubShape );
186       if ( algo && !algo->NeedDescretBoundary() )
187       {
188         if ( algo->SupportSubmeshes() )
189           smWithAlgoSupportingSubmeshes.push_front( smToCompute );
190         else
191         {
192           smToCompute->ComputeStateEngine( SMESH_subMesh::COMPUTE );
193           if ( aShapesId )
194             aShapesId->insert( smToCompute->GetId() );
195         }
196       }
197     }
198     
199     // ------------------------------------------------------------
200     // sort list of meshes according to mesh order
201     // ------------------------------------------------------------
202     aMesh.SortByMeshOrder( smWithAlgoSupportingSubmeshes );
203
204     // ------------------------------------------------------------
205     // compute submeshes under shapes with algos that DO NOT require
206     // descretized boundaries and DO support submeshes
207     // ------------------------------------------------------------
208     list< SMESH_subMesh* >::iterator subIt, subEnd;
209     subIt  = smWithAlgoSupportingSubmeshes.begin();
210     subEnd = smWithAlgoSupportingSubmeshes.end();
211     // start from lower shapes
212     for ( ; subIt != subEnd; ++subIt )
213     {
214       sm = *subIt;
215
216       // get a shape the algo is assigned to
217       TopoDS_Shape algoShape;
218       if ( !GetAlgo( aMesh, sm->GetSubShape(), & algoShape ))
219         continue; // strange...
220
221       // look for more local algos
222       smIt = sm->getDependsOnIterator(!includeSelf, !complexShapeFirst);
223       while ( smIt->more() )
224       {
225         SMESH_subMesh* smToCompute = smIt->next();
226
227         const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
228         const int aShapeDim = GetShapeDim( aSubShape );
229         //if ( aSubShape.ShapeType() == TopAbs_VERTEX ) continue;
230         if ( aShapeDim < 1 ) continue;
231
232         // check for preview dimension limitations
233         if ( aShapesId && GetShapeDim( aSubShape.ShapeType() ) > (int)aDim )
234           continue;
235         
236         SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
237         filter
238           .And( SMESH_HypoFilter::IsApplicableTo( aSubShape ))
239           .And( SMESH_HypoFilter::IsMoreLocalThan( algoShape ));
240
241         if ( SMESH_Algo* subAlgo = (SMESH_Algo*) aMesh.GetHypothesis( aSubShape, filter, true )) {
242           SMESH_Hypothesis::Hypothesis_Status status;
243           if ( subAlgo->CheckHypothesis( aMesh, aSubShape, status ))
244             // mesh a lower smToCompute starting from vertices
245             Compute( aMesh, aSubShape, /*anUpward=*/true, aDim, aShapesId );
246         }
247       }
248     }
249     // ----------------------------------------------------------
250     // apply the algos that do not require descretized boundaries
251     // ----------------------------------------------------------
252     for ( subIt = smWithAlgoSupportingSubmeshes.begin(); subIt != subEnd; ++subIt )
253     {
254       sm = *subIt;
255       if ( sm->GetComputeState() == SMESH_subMesh::READY_TO_COMPUTE)
256       {
257         const TopAbs_ShapeEnum aShType = sm->GetSubShape().ShapeType();
258         // check for preview dimension limitations
259         if ( aShapesId && GetShapeDim( aShType ) > (int)aDim )
260           continue;
261
262         sm->ComputeStateEngine( SMESH_subMesh::COMPUTE );
263         if ( aShapesId )
264           aShapesId->insert( sm->GetId() );
265       }
266     }
267     // -----------------------------------------------
268     // mesh the rest subshapes starting from vertices
269     // -----------------------------------------------
270     ret = Compute( aMesh, aShape, /*anUpward=*/true, aDim, aShapesId );
271   }
272
273   MESSAGE( "VSR - SMESH_Gen::Compute() finished, OK = " << ret);
274   MEMOSTAT;
275
276   SMESHDS_Mesh *myMesh = aMesh.GetMeshDS();
277   myMesh->adjustStructure();
278   myMesh->compactMesh();
279   //myMesh->adjustStructure();
280   list<int> listind = myMesh->SubMeshIndices();
281   list<int>::iterator it = listind.begin();
282   int total = 0;
283   for(; it != listind.end(); ++it)
284     {
285       ::SMESHDS_SubMesh *subMesh = myMesh->MeshElements(*it);
286       total +=  subMesh->getSize();
287     }
288   MESSAGE("total elements and nodes in submesh sets:" << total);
289   MESSAGE("Number of node objects " << SMDS_MeshNode::nbNodes);
290   MESSAGE("Number of cell objects " << SMDS_MeshCell::nbCells);
291   //myMesh->dumpGrid();
292   return ret;
293 }
294
295
296 //=============================================================================
297 /*!
298  * Evaluate a mesh
299  */
300 //=============================================================================
301
302 bool SMESH_Gen::Evaluate(SMESH_Mesh &          aMesh,
303                          const TopoDS_Shape &  aShape,
304                          MapShapeNbElems&      aResMap,
305                          const bool            anUpward,
306                          TSetOfInt*            aShapesId)
307 {
308   MESSAGE("SMESH_Gen::Evaluate");
309
310   bool ret = true;
311
312   SMESH_subMesh *sm = aMesh.GetSubMesh(aShape);
313
314   const bool includeSelf = true;
315   const bool complexShapeFirst = true;
316   SMESH_subMeshIteratorPtr smIt;
317
318   if ( anUpward ) { // is called from below code here
319     // -----------------------------------------------
320     // mesh all the subshapes starting from vertices
321     // -----------------------------------------------
322     smIt = sm->getDependsOnIterator(includeSelf, !complexShapeFirst);
323     while ( smIt->more() ) {
324       SMESH_subMesh* smToCompute = smIt->next();
325
326       // do not mesh vertices of a pseudo shape
327       const TopAbs_ShapeEnum aShType = smToCompute->GetSubShape().ShapeType();
328       //if ( !aMesh.HasShapeToMesh() && aShType == TopAbs_VERTEX )
329       //  continue;
330       if ( !aMesh.HasShapeToMesh() ) {
331         if( aShType == TopAbs_VERTEX || aShType == TopAbs_WIRE ||
332             aShType == TopAbs_SHELL )
333           continue;
334       }
335
336       smToCompute->Evaluate(aResMap);
337       if( aShapesId )
338         aShapesId->insert( smToCompute->GetId() );
339     }
340     return ret;
341   }
342   else {
343     // -----------------------------------------------------------------
344     // apply algos that DO NOT require descretized boundaries and DO NOT
345     // support submeshes, starting from the most complex shapes
346     // and collect submeshes with algos that DO support submeshes
347     // -----------------------------------------------------------------
348     list< SMESH_subMesh* > smWithAlgoSupportingSubmeshes;
349     smIt = sm->getDependsOnIterator(includeSelf, complexShapeFirst);
350     while ( smIt->more() ) {
351       SMESH_subMesh* smToCompute = smIt->next();
352       const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
353       const int aShapeDim = GetShapeDim( aSubShape );
354       if ( aShapeDim < 1 ) break;
355       
356       SMESH_Algo* algo = GetAlgo( aMesh, aSubShape );
357       if ( algo && !algo->NeedDescretBoundary() ) {
358         if ( algo->SupportSubmeshes() ) {
359           smWithAlgoSupportingSubmeshes.push_front( smToCompute );
360         }
361         else {
362           smToCompute->Evaluate(aResMap);
363           if ( aShapesId )
364             aShapesId->insert( smToCompute->GetId() );
365         }
366       }
367     }
368
369     // ------------------------------------------------------------
370     // sort list of meshes according to mesh order
371     // ------------------------------------------------------------
372     aMesh.SortByMeshOrder( smWithAlgoSupportingSubmeshes );
373
374     // ------------------------------------------------------------
375     // compute submeshes under shapes with algos that DO NOT require
376     // descretized boundaries and DO support submeshes
377     // ------------------------------------------------------------
378     list< SMESH_subMesh* >::iterator subIt, subEnd;
379     subIt  = smWithAlgoSupportingSubmeshes.begin();
380     subEnd = smWithAlgoSupportingSubmeshes.end();
381     // start from lower shapes
382     for ( ; subIt != subEnd; ++subIt ) {
383       sm = *subIt;
384
385       // get a shape the algo is assigned to
386       TopoDS_Shape algoShape;
387       if ( !GetAlgo( aMesh, sm->GetSubShape(), & algoShape ))
388         continue; // strange...
389
390       // look for more local algos
391       smIt = sm->getDependsOnIterator(!includeSelf, !complexShapeFirst);
392       while ( smIt->more() ) {
393         SMESH_subMesh* smToCompute = smIt->next();
394
395         const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
396         const int aShapeDim = GetShapeDim( aSubShape );
397         if ( aShapeDim < 1 ) continue;
398
399         //const TopAbs_ShapeEnum aShType = smToCompute->GetSubShape().ShapeType();
400
401         SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
402         filter
403           .And( SMESH_HypoFilter::IsApplicableTo( aSubShape ))
404           .And( SMESH_HypoFilter::IsMoreLocalThan( algoShape ));
405
406         if ( SMESH_Algo* subAlgo = (SMESH_Algo*) aMesh.GetHypothesis( aSubShape, filter, true )) {
407           SMESH_Hypothesis::Hypothesis_Status status;
408           if ( subAlgo->CheckHypothesis( aMesh, aSubShape, status ))
409             // mesh a lower smToCompute starting from vertices
410             Evaluate( aMesh, aSubShape, aResMap, /*anUpward=*/true, aShapesId );
411         }
412       }
413     }
414     // ----------------------------------------------------------
415     // apply the algos that do not require descretized boundaries
416     // ----------------------------------------------------------
417     for ( subIt = smWithAlgoSupportingSubmeshes.begin(); subIt != subEnd; ++subIt )
418     {
419       sm = *subIt;
420       sm->Evaluate(aResMap);
421       if ( aShapesId )
422         aShapesId->insert( sm->GetId() );
423     }
424
425     // -----------------------------------------------
426     // mesh the rest subshapes starting from vertices
427     // -----------------------------------------------
428     ret = Evaluate( aMesh, aShape, aResMap, /*anUpward=*/true, aShapesId );
429   }
430
431   MESSAGE( "VSR - SMESH_Gen::Evaluate() finished, OK = " << ret);
432   return ret;
433 }
434
435
436 //=======================================================================
437 //function : checkConformIgnoredAlgos
438 //purpose  :
439 //=======================================================================
440
441 static bool checkConformIgnoredAlgos(SMESH_Mesh&               aMesh,
442                                      SMESH_subMesh*            aSubMesh,
443                                      const SMESH_Algo*         aGlobIgnoAlgo,
444                                      const SMESH_Algo*         aLocIgnoAlgo,
445                                      bool &                    checkConform,
446                                      set<SMESH_subMesh*>&      aCheckedMap,
447                                      list< SMESH_Gen::TAlgoStateError > & theErrors)
448 {
449   ASSERT( aSubMesh );
450   if ( aSubMesh->GetSubShape().ShapeType() == TopAbs_VERTEX)
451     return true;
452
453
454   bool ret = true;
455
456   const list<const SMESHDS_Hypothesis*>& listHyp =
457     aMesh.GetMeshDS()->GetHypothesis( aSubMesh->GetSubShape() );
458   list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
459   for ( ; it != listHyp.end(); it++)
460   {
461     const SMESHDS_Hypothesis * aHyp = *it;
462     if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
463       continue;
464
465     const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
466     ASSERT ( algo );
467
468     if ( aLocIgnoAlgo ) // algo is hidden by a local algo of upper dim
469     {
470       INFOS( "Local <" << algo->GetName() << "> is hidden by local <"
471             << aLocIgnoAlgo->GetName() << ">");
472     }
473     else
474     {
475       bool isGlobal = (aMesh.IsMainShape( aSubMesh->GetSubShape() ));
476       int dim = algo->GetDim();
477       int aMaxGlobIgnoDim = ( aGlobIgnoAlgo ? aGlobIgnoAlgo->GetDim() : -1 );
478
479       if ( dim < aMaxGlobIgnoDim )
480       {
481         // algo is hidden by a global algo
482         INFOS( ( isGlobal ? "Global" : "Local" )
483               << " <" << algo->GetName() << "> is hidden by global <"
484               << aGlobIgnoAlgo->GetName() << ">");
485       }
486       else if ( !algo->NeedDescretBoundary() && !isGlobal)
487       {
488         // local algo is not hidden and hides algos on sub-shapes
489         if (checkConform && !aSubMesh->IsConform( algo ))
490         {
491           ret = false;
492           checkConform = false; // no more check conformity
493           INFOS( "ERROR: Local <" << algo->GetName() <<
494                 "> would produce not conform mesh: "
495                 "<Not Conform Mesh Allowed> hypotesis is missing");
496           theErrors.push_back( SMESH_Gen::TAlgoStateError() );
497           theErrors.back().Set( SMESH_Hypothesis::HYP_NOTCONFORM, algo, false );
498         }
499
500         // sub-algos will be hidden by a local <algo>
501         SMESH_subMeshIteratorPtr revItSub =
502           aSubMesh->getDependsOnIterator( /*includeSelf=*/false, /*complexShapeFirst=*/true);
503         bool checkConform2 = false;
504         while ( revItSub->more() )
505         {
506           SMESH_subMesh* sm = revItSub->next();
507           checkConformIgnoredAlgos (aMesh, sm, aGlobIgnoAlgo,
508                                     algo, checkConform2, aCheckedMap, theErrors);
509           aCheckedMap.insert( sm );
510         }
511       }
512     }
513   }
514
515   return ret;
516 }
517
518 //=======================================================================
519 //function : checkMissing
520 //purpose  : notify on missing hypothesis
521 //           Return false if algo or hipothesis is missing
522 //=======================================================================
523
524 static bool checkMissing(SMESH_Gen*                aGen,
525                          SMESH_Mesh&               aMesh,
526                          SMESH_subMesh*            aSubMesh,
527                          const int                 aTopAlgoDim,
528                          bool*                     globalChecked,
529                          const bool                checkNoAlgo,
530                          set<SMESH_subMesh*>&      aCheckedMap,
531                          list< SMESH_Gen::TAlgoStateError > & theErrors)
532 {
533   if ( aSubMesh->GetSubShape().ShapeType() == TopAbs_VERTEX)
534     return true;
535
536   //MESSAGE("=====checkMissing");
537
538   int ret = true;
539   SMESH_Algo* algo = 0;
540
541   switch (aSubMesh->GetAlgoState())
542   {
543   case SMESH_subMesh::NO_ALGO: {
544     if (checkNoAlgo)
545     {
546       // should there be any algo?
547       int shapeDim = SMESH_Gen::GetShapeDim( aSubMesh->GetSubShape() );
548       if (aTopAlgoDim > shapeDim)
549       {
550         MESSAGE( "ERROR: " << shapeDim << "D algorithm is missing" );
551         ret = false;
552         theErrors.push_back( SMESH_Gen::TAlgoStateError() );
553         theErrors.back().Set( SMESH_Hypothesis::HYP_MISSING, shapeDim, true );
554       }
555     }
556     return ret;
557   }
558   case SMESH_subMesh::MISSING_HYP: {
559     // notify if an algo missing hyp is attached to aSubMesh
560     algo = aGen->GetAlgo( aMesh, aSubMesh->GetSubShape() );
561     ASSERT( algo );
562     bool IsGlobalHypothesis = aGen->IsGlobalHypothesis( algo, aMesh );
563     if (!IsGlobalHypothesis || !globalChecked[ algo->GetDim() ])
564     {
565       TAlgoStateErrorName errName = SMESH_Hypothesis::HYP_MISSING;
566       SMESH_Hypothesis::Hypothesis_Status status;
567       algo->CheckHypothesis( aMesh, aSubMesh->GetSubShape(), status );
568       if ( status == SMESH_Hypothesis::HYP_BAD_PARAMETER ) {
569         MESSAGE( "ERROR: hypothesis of " << (IsGlobalHypothesis ? "Global " : "Local ")
570                  << "<" << algo->GetName() << "> has a bad parameter value");
571         errName = status;
572       } else if ( status == SMESH_Hypothesis::HYP_BAD_GEOMETRY ) {
573         MESSAGE( "ERROR: " << (IsGlobalHypothesis ? "Global " : "Local ")
574                  << "<" << algo->GetName() << "> assigned to mismatching geometry");
575         errName = status;
576       } else {
577         MESSAGE( "ERROR: " << (IsGlobalHypothesis ? "Global " : "Local ")
578                  << "<" << algo->GetName() << "> misses some hypothesis");
579       }
580       if (IsGlobalHypothesis)
581         globalChecked[ algo->GetDim() ] = true;
582       theErrors.push_back( SMESH_Gen::TAlgoStateError() );
583       theErrors.back().Set( errName, algo, IsGlobalHypothesis );
584     }
585     ret = false;
586     break;
587   }
588   case SMESH_subMesh::HYP_OK:
589     algo = aGen->GetAlgo( aMesh, aSubMesh->GetSubShape() );
590     ret = true;
591     break;
592   default: ASSERT(0);
593   }
594
595   // do not check under algo that hides sub-algos or
596   // re-start checking NO_ALGO state
597   ASSERT (algo);
598   bool isTopLocalAlgo =
599     ( aTopAlgoDim <= algo->GetDim() && !aGen->IsGlobalHypothesis( algo, aMesh ));
600   if (!algo->NeedDescretBoundary() || isTopLocalAlgo)
601   {
602     bool checkNoAlgo2 = ( algo->NeedDescretBoundary() );
603     SMESH_subMeshIteratorPtr itsub = aSubMesh->getDependsOnIterator( /*includeSelf=*/false,
604                                                                      /*complexShapeFirst=*/false);
605     while ( itsub->more() )
606     {
607       // sub-meshes should not be checked further more
608       SMESH_subMesh* sm = itsub->next();
609       aCheckedMap.insert( sm );
610
611       if (isTopLocalAlgo)
612       {
613         //check algo on sub-meshes
614         int aTopAlgoDim2 = algo->GetDim();
615         if (!checkMissing (aGen, aMesh, sm, aTopAlgoDim2,
616                            globalChecked, checkNoAlgo2, aCheckedMap, theErrors))
617         {
618           ret = false;
619           if (sm->GetAlgoState() == SMESH_subMesh::NO_ALGO )
620             checkNoAlgo2 = false;
621         }
622       }
623     }
624   }
625   return ret;
626 }
627
628 //=======================================================================
629 //function : CheckAlgoState
630 //purpose  : notify on bad state of attached algos, return false
631 //           if Compute() would fail because of some algo bad state
632 //=======================================================================
633
634 bool SMESH_Gen::CheckAlgoState(SMESH_Mesh& aMesh, const TopoDS_Shape& aShape)
635 {
636   list< TAlgoStateError > errors;
637   return GetAlgoState( aMesh, aShape, errors );
638 }
639
640 //=======================================================================
641 //function : GetAlgoState
642 //purpose  : notify on bad state of attached algos, return false
643 //           if Compute() would fail because of some algo bad state
644 //           theErrors list contains problems description
645 //=======================================================================
646
647 bool SMESH_Gen::GetAlgoState(SMESH_Mesh&               theMesh,
648                              const TopoDS_Shape&       theShape,
649                              list< TAlgoStateError > & theErrors)
650 {
651   //MESSAGE("SMESH_Gen::CheckAlgoState");
652
653   bool ret = true;
654   bool hasAlgo = false;
655
656   SMESH_subMesh* sm = theMesh.GetSubMesh(theShape);
657   const SMESHDS_Mesh* meshDS = theMesh.GetMeshDS();
658   TopoDS_Shape mainShape = meshDS->ShapeToMesh();
659
660   // -----------------
661   // get global algos
662   // -----------------
663
664   const SMESH_Algo* aGlobAlgoArr[] = {0,0,0,0};
665
666   const list<const SMESHDS_Hypothesis*>& listHyp = meshDS->GetHypothesis( mainShape );
667   list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
668   for ( ; it != listHyp.end(); it++)
669   {
670     const SMESHDS_Hypothesis * aHyp = *it;
671     if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
672       continue;
673
674     const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
675     ASSERT ( algo );
676
677     int dim = algo->GetDim();
678     aGlobAlgoArr[ dim ] = algo;
679
680     hasAlgo = true;
681   }
682
683   // --------------------------------------------------------
684   // info on algos that will be ignored because of ones that
685   // don't NeedDescretBoundary() attached to super-shapes,
686   // check that a conform mesh will be produced
687   // --------------------------------------------------------
688
689
690   // find a global algo possibly hiding sub-algos
691   int dim;
692   const SMESH_Algo* aGlobIgnoAlgo = 0;
693   for (dim = 3; dim > 0; dim--)
694   {
695     if (aGlobAlgoArr[ dim ] &&
696         !aGlobAlgoArr[ dim ]->NeedDescretBoundary())
697     {
698       aGlobIgnoAlgo = aGlobAlgoArr[ dim ];
699       break;
700     }
701   }
702
703   set<SMESH_subMesh*> aCheckedSubs;
704   bool checkConform = ( !theMesh.IsNotConformAllowed() );
705
706   // loop on theShape and its sub-shapes
707   SMESH_subMeshIteratorPtr revItSub = sm->getDependsOnIterator( /*includeSelf=*/true,
708                                                                 /*complexShapeFirst=*/true);
709   while ( revItSub->more() )
710   {
711     SMESH_subMesh* smToCheck = revItSub->next();
712     if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
713       break;
714
715     if ( aCheckedSubs.insert( smToCheck ).second ) // not yet checked
716       if (!checkConformIgnoredAlgos (theMesh, smToCheck, aGlobIgnoAlgo,
717                                      0, checkConform, aCheckedSubs, theErrors))
718         ret = false;
719
720     if ( smToCheck->GetAlgoState() != SMESH_subMesh::NO_ALGO )
721       hasAlgo = true;
722   }
723
724   // ----------------------------------------------------------------
725   // info on missing hypothesis and find out if all needed algos are
726   // well defined
727   // ----------------------------------------------------------------
728
729   //MESSAGE( "---info on missing hypothesis and find out if all needed algos are");
730
731   // find max dim of global algo
732   int aTopAlgoDim = 0;
733   for (dim = 3; dim > 0; dim--)
734   {
735     if (aGlobAlgoArr[ dim ])
736     {
737       aTopAlgoDim = dim;
738       break;
739     }
740   }
741   bool checkNoAlgo = theMesh.HasShapeToMesh() ? bool( aTopAlgoDim ) : false;
742   bool globalChecked[] = { false, false, false, false };
743
744   // loop on theShape and its sub-shapes
745   aCheckedSubs.clear();
746   revItSub = sm->getDependsOnIterator( /*includeSelf=*/true, /*complexShapeFirst=*/true);
747   while ( revItSub->more() )
748   {
749     SMESH_subMesh* smToCheck = revItSub->next();
750     if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
751       break;
752
753     if ( aCheckedSubs.insert( smToCheck ).second ) // not yet checked
754       if (!checkMissing (this, theMesh, smToCheck, aTopAlgoDim,
755                          globalChecked, checkNoAlgo, aCheckedSubs, theErrors))
756       {
757         ret = false;
758         if (smToCheck->GetAlgoState() == SMESH_subMesh::NO_ALGO )
759           checkNoAlgo = false;
760       }
761   }
762
763   if ( !hasAlgo ) {
764     ret = false;
765     INFOS( "None algorithm attached" );
766     theErrors.push_back( TAlgoStateError() );
767     theErrors.back().Set( SMESH_Hypothesis::HYP_MISSING, 1, true );
768   }
769
770   return ret;
771 }
772
773 //=======================================================================
774 //function : IsGlobalHypothesis
775 //purpose  : check if theAlgo is attached to the main shape
776 //=======================================================================
777
778 bool SMESH_Gen::IsGlobalHypothesis(const SMESH_Hypothesis* theHyp, SMESH_Mesh& aMesh)
779 {
780   SMESH_HypoFilter filter( SMESH_HypoFilter::Is( theHyp ));
781   return aMesh.GetHypothesis( aMesh.GetMeshDS()->ShapeToMesh(), filter, false );
782 }
783
784 //=============================================================================
785 /*!
786  * Finds algo to mesh a shape. Optionally returns a shape the found algo is bound to
787  */
788 //=============================================================================
789
790 SMESH_Algo *SMESH_Gen::GetAlgo(SMESH_Mesh &         aMesh,
791                                const TopoDS_Shape & aShape,
792                                TopoDS_Shape*        assignedTo)
793 {
794   SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
795   filter.And( filter.IsApplicableTo( aShape ));
796
797   return (SMESH_Algo*) aMesh.GetHypothesis( aShape, filter, true, assignedTo );
798 }
799
800 //=============================================================================
801 /*!
802  * Returns StudyContextStruct for a study
803  */
804 //=============================================================================
805
806 StudyContextStruct *SMESH_Gen::GetStudyContext(int studyId)
807 {
808   // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
809
810   if (_mapStudyContext.find(studyId) == _mapStudyContext.end())
811   {
812     _mapStudyContext[studyId] = new StudyContextStruct;
813     _mapStudyContext[studyId]->myDocument = new SMESHDS_Document(studyId);
814   }
815   StudyContextStruct *myStudyContext = _mapStudyContext[studyId];
816   return myStudyContext;
817 }
818
819 //================================================================================
820 /*!
821  * \brief Return shape dimension by TopAbs_ShapeEnum
822  */
823 //================================================================================
824
825 int SMESH_Gen::GetShapeDim(const TopAbs_ShapeEnum & aShapeType)
826 {
827   static vector<int> dim;
828   if ( dim.empty() )
829   {
830     dim.resize( TopAbs_SHAPE, -1 );
831     dim[ TopAbs_COMPOUND ]  = MeshDim_3D;
832     dim[ TopAbs_COMPSOLID ] = MeshDim_3D;
833     dim[ TopAbs_SOLID ]     = MeshDim_3D;
834     dim[ TopAbs_SHELL ]     = MeshDim_3D;
835     dim[ TopAbs_FACE  ]     = MeshDim_2D;
836     dim[ TopAbs_WIRE ]      = MeshDim_1D;
837     dim[ TopAbs_EDGE ]      = MeshDim_1D;
838     dim[ TopAbs_VERTEX ]    = MeshDim_0D;
839   }
840   return dim[ aShapeType ];
841 }
842
843 //=============================================================================
844 /*!
845  * Genarate a new id unique withing this Gen
846  */
847 //=============================================================================
848
849 int SMESH_Gen::GetANewId()
850 {
851   return _hypId++;
852 }