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