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