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