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