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