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