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