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