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