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