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