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