Salome HOME
Copyright update 2022
[modules/smesh.git] / src / SMESH / SMESH_Gen.cxx
1 // Copyright (C) 2007-2022  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, or (at your option) any later version.
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 : implementation of SMESH idl descriptions
24 //  File   : SMESH_Gen.cxx
25 //  Author : Paul RASCLE, EDF
26 //  Module : SMESH
27 //
28
29 //#define CHRONODEF
30
31 #include "SMESH_Gen.hxx"
32
33 #include "SMDS_Mesh.hxx"
34 #include "SMDS_MeshElement.hxx"
35 #include "SMDS_MeshNode.hxx"
36 #include "SMESHDS_Document.hxx"
37 #include "SMESH_HypoFilter.hxx"
38 #include "SMESH_Mesh.hxx"
39 #include "SMESH_MesherHelper.hxx"
40 #include "SMESH_subMesh.hxx"
41
42 #include <utilities.h>
43 #include <Utils_ExceptHandlers.hxx>
44
45 #include <TopExp_Explorer.hxx>
46 #include <TopoDS.hxx>
47 #include <TopoDS_Iterator.hxx>
48
49 #include "memoire.h"
50
51 #ifdef WIN32
52   #include <windows.h>
53 #endif
54
55 #include <Basics_Utils.hxx>
56
57 using namespace std;
58
59 // Environment variable separator
60 #ifdef WIN32
61   #define env_sep ';'
62 #else
63   #define env_sep ':'
64 #endif
65
66 namespace
67 {
68   // a structure used to nullify SMESH_Gen field of SMESH_Hypothesis,
69   // which is needed for SMESH_Hypothesis not deleted before ~SMESH_Gen()
70   struct _Hyp : public SMESH_Hypothesis
71   {
72     void NullifyGen()
73     {
74       _gen = 0;
75     }
76   };
77
78   //================================================================================
79   /*!
80    * \brief Fill a map of shapes with all sub-shape of a sub-mesh
81    */
82   //================================================================================
83
84   TopTools_IndexedMapOfShape * fillAllowed( SMESH_subMesh*               sm,
85                                             const bool                   toFill,
86                                             TopTools_IndexedMapOfShape * allowedSub )
87   {
88     if ( !toFill || !allowedSub )
89     {
90       return nullptr;
91     }
92     if ( allowedSub->IsEmpty() )
93     {
94       allowedSub->ReSize( sm->DependsOn().size() + 1 );
95       allowedSub->Add( sm->GetSubShape() );
96       for ( const auto & key_sm : sm->DependsOn() )
97         allowedSub->Add( key_sm.second->GetSubShape() );
98     }
99     return allowedSub;
100   }
101 }
102
103 //=============================================================================
104 /*!
105  *  Constructor
106  */
107 //=============================================================================
108
109 SMESH_Gen::SMESH_Gen()
110 {
111   _studyContext = new StudyContextStruct;
112   _studyContext->myDocument = new SMESHDS_Document();
113   _localId = 0;
114   _hypId   = 0;
115   _segmentation = _nbSegments = 10;
116   _compute_canceled = false;
117 }
118
119 //=============================================================================
120 /*!
121  * Destructor
122  */
123 //=============================================================================
124
125 SMESH_Gen::~SMESH_Gen()
126 {
127   std::map < int, SMESH_Hypothesis * >::iterator i_hyp = _studyContext->mapHypothesis.begin();
128   for ( ; i_hyp != _studyContext->mapHypothesis.end(); ++i_hyp )
129   {
130     if ( _Hyp* h = static_cast< _Hyp*>( i_hyp->second ))
131       h->NullifyGen();
132   }
133   delete _studyContext->myDocument;
134   delete _studyContext;
135 }
136
137 //=============================================================================
138 /*!
139  * Creates a mesh in a study.
140  * if (theIsEmbeddedMode) { mesh modification commands are not logged }
141  */
142 //=============================================================================
143
144 SMESH_Mesh* SMESH_Gen::CreateMesh(bool theIsEmbeddedMode)
145 {
146   Unexpect aCatch(SalomeException);
147
148   // create a new SMESH_mesh object
149   SMESH_Mesh *aMesh = new SMESH_Mesh(_localId++,
150                                      this,
151                                      theIsEmbeddedMode,
152                                      _studyContext->myDocument);
153   _studyContext->mapMesh[_localId-1] = aMesh;
154
155   return aMesh;
156 }
157
158 //=============================================================================
159 /*
160  * Compute a mesh
161  */
162 //=============================================================================
163
164 bool SMESH_Gen::Compute(SMESH_Mesh &                aMesh,
165                         const TopoDS_Shape &        aShape,
166                         const int                   aFlags /*= COMPACT_MESH*/,
167                         const ::MeshDimension       aDim /*=::MeshDim_3D*/,
168                         TSetOfInt*                  aShapesId /*=0*/,
169                         TopTools_IndexedMapOfShape* anAllowedSubShapes/*=0*/)
170 {
171   MEMOSTAT;
172
173   const bool   aShapeOnly = aFlags & SHAPE_ONLY;
174   const bool     anUpward = aFlags & UPWARD;
175   const bool aCompactMesh = aFlags & COMPACT_MESH;
176
177   bool ret = true;
178
179   SMESH_subMesh *sm, *shapeSM = aMesh.GetSubMesh(aShape);
180
181   const bool includeSelf = true;
182   const bool complexShapeFirst = true;
183   const int  globalAlgoDim = 100;
184
185   SMESH_subMeshIteratorPtr smIt;
186
187   // Fix of Issue 22150. Due to !BLSURF->OnlyUnaryInput(), BLSURF computes edges
188   // that must be computed by Projection 1D-2D while the Projection asks to compute
189   // one face only.
190   SMESH_subMesh::compute_event computeEvent =
191     aShapeOnly ? SMESH_subMesh::COMPUTE_SUBMESH : SMESH_subMesh::COMPUTE;
192   if ( !aMesh.HasShapeToMesh() )
193     computeEvent = SMESH_subMesh::COMPUTE_NOGEOM; // if several algos and no geometry
194
195   TopTools_IndexedMapOfShape *allowedSubShapes = anAllowedSubShapes, allowedSub;
196   if ( aShapeOnly && !allowedSubShapes )
197     allowedSubShapes = &allowedSub;
198
199   if ( anUpward ) // is called from the below code in this method
200   {
201     // ===============================================
202     // Mesh all the sub-shapes starting from vertices
203     // ===============================================
204
205     smIt = shapeSM->getDependsOnIterator(includeSelf, !complexShapeFirst);
206     while ( smIt->more() )
207     {
208       SMESH_subMesh* smToCompute = smIt->next();
209
210       // do not mesh vertices of a pseudo shape
211       const TopoDS_Shape&        shape = smToCompute->GetSubShape();
212       const TopAbs_ShapeEnum shapeType = shape.ShapeType();
213       if ( !aMesh.HasShapeToMesh() && shapeType == TopAbs_VERTEX )
214         continue;
215
216       // check for preview dimension limitations
217       if ( aShapesId && GetShapeDim( shapeType ) > (int)aDim )
218       {
219         // clear compute state not to show previous compute errors
220         //  if preview invoked less dimension less than previous
221         smToCompute->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
222         continue;
223       }
224
225       if (smToCompute->GetComputeState() == SMESH_subMesh::READY_TO_COMPUTE)
226       {
227         if (_compute_canceled)
228           return false;
229         smToCompute->SetAllowedSubShapes( fillAllowed( shapeSM, aShapeOnly, allowedSubShapes ));
230         setCurrentSubMesh( smToCompute );
231         smToCompute->ComputeStateEngine( computeEvent );
232         setCurrentSubMesh( nullptr );
233         smToCompute->SetAllowedSubShapes( nullptr );
234       }
235
236       // we check all the sub-meshes here and detect if any of them failed to compute
237       if (smToCompute->GetComputeState() == SMESH_subMesh::FAILED_TO_COMPUTE &&
238           ( shapeType != TopAbs_EDGE || !SMESH_Algo::isDegenerated( TopoDS::Edge( shape ))))
239         ret = false;
240       else if ( aShapesId )
241         aShapesId->insert( smToCompute->GetId() );
242     }
243     //aMesh.GetMeshDS()->Modified();
244     return ret;
245   }
246   else
247   {
248     // ================================================================
249     // Apply algos that do NOT require discreteized boundaries
250     // ("all-dimensional") and do NOT support sub-meshes, starting from
251     // the most complex shapes and collect sub-meshes with algos that
252     // DO support sub-meshes
253     // ================================================================
254
255     list< SMESH_subMesh* > smWithAlgoSupportingSubmeshes[4]; // for each dim
256
257     // map to sort sm with same dim algos according to dim of
258     // the shape the algo assigned to (issue 0021217).
259     // Other issues influenced the algo applying order:
260     // 21406, 21556, 21893, 20206
261     multimap< int, SMESH_subMesh* > shDim2sm;
262     multimap< int, SMESH_subMesh* >::reverse_iterator shDim2smIt;
263     TopoDS_Shape algoShape;
264     int prevShapeDim = -1, aShapeDim;
265
266     smIt = shapeSM->getDependsOnIterator(includeSelf, complexShapeFirst);
267     while ( smIt->more() )
268     {
269       SMESH_subMesh* smToCompute = smIt->next();
270       if ( smToCompute->GetComputeState() != SMESH_subMesh::READY_TO_COMPUTE )
271         continue;
272
273       const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
274       aShapeDim = GetShapeDim( aSubShape );
275       if ( aShapeDim < 1 ) break;
276
277       // check for preview dimension limitations
278       if ( aShapesId && aShapeDim > (int)aDim )
279         continue;
280
281       SMESH_Algo* algo = GetAlgo( smToCompute, &algoShape );
282       if ( algo && !algo->NeedDiscreteBoundary() )
283       {
284         if ( algo->SupportSubmeshes() )
285         {
286           // reload sub-meshes from shDim2sm into smWithAlgoSupportingSubmeshes
287           // so that more local algos to go first
288           if ( prevShapeDim != aShapeDim )
289           {
290             prevShapeDim = aShapeDim;
291             for ( shDim2smIt = shDim2sm.rbegin(); shDim2smIt != shDim2sm.rend(); ++shDim2smIt )
292               if ( shDim2smIt->first == globalAlgoDim )
293                 smWithAlgoSupportingSubmeshes[ aShapeDim ].push_back( shDim2smIt->second );
294               else
295                 smWithAlgoSupportingSubmeshes[ aShapeDim ].push_front( shDim2smIt->second );
296             shDim2sm.clear();
297           }
298           // add smToCompute to shDim2sm map
299           if ( algoShape.IsSame( aMesh.GetShapeToMesh() ))
300           {
301             aShapeDim = globalAlgoDim; // to compute last
302           }
303           else
304           {
305             aShapeDim = GetShapeDim( algoShape );
306             if ( algoShape.ShapeType() == TopAbs_COMPOUND )
307             {
308               TopoDS_Iterator it( algoShape );
309               aShapeDim += GetShapeDim( it.Value() );
310             }
311           }
312           shDim2sm.insert( make_pair( aShapeDim, smToCompute ));
313         }
314         else // Compute w/o support of sub-meshes
315         {
316           if (_compute_canceled)
317             return false;
318           smToCompute->SetAllowedSubShapes( fillAllowed( shapeSM, aShapeOnly, allowedSubShapes ));
319           setCurrentSubMesh( smToCompute );
320           smToCompute->ComputeStateEngine( computeEvent );
321           setCurrentSubMesh( nullptr );
322           smToCompute->SetAllowedSubShapes( nullptr );
323           if ( aShapesId )
324             aShapesId->insert( smToCompute->GetId() );
325         }
326       }
327     }
328     // reload sub-meshes from shDim2sm into smWithAlgoSupportingSubmeshes
329     for ( shDim2smIt = shDim2sm.rbegin(); shDim2smIt != shDim2sm.rend(); ++shDim2smIt )
330       if ( shDim2smIt->first == globalAlgoDim )
331         smWithAlgoSupportingSubmeshes[3].push_back( shDim2smIt->second );
332       else
333         smWithAlgoSupportingSubmeshes[0].push_front( shDim2smIt->second );
334
335     // ======================================================
336     // Apply all-dimensional algorithms supporing sub-meshes
337     // ======================================================
338
339     std::vector< SMESH_subMesh* > smVec;
340     for ( aShapeDim = 0; aShapeDim < 4; ++aShapeDim )
341       smVec.insert( smVec.end(),
342                     smWithAlgoSupportingSubmeshes[aShapeDim].begin(),
343                     smWithAlgoSupportingSubmeshes[aShapeDim].end() );
344
345     // gather sub-shapes with local uni-dimensional algos (bos #29143)
346     // ----------------------------------------------------------------
347     TopTools_MapOfShape uniDimAlgoShapes;
348     if ( !smVec.empty() )
349     {
350       ShapeToHypothesis::Iterator s2hyps( aMesh.GetMeshDS()->GetHypotheses() );
351       for ( ; s2hyps.More(); s2hyps.Next() )
352       {
353         const TopoDS_Shape& s = s2hyps.Key();
354         if ( s.IsSame( aMesh.GetShapeToMesh() ))
355           continue;
356         for ( auto & hyp : s2hyps.Value() )
357         {
358           if ( const SMESH_Algo* algo = dynamic_cast< const SMESH_Algo*>( hyp ))
359             if ( algo->NeedDiscreteBoundary() )
360             {
361               TopAbs_ShapeEnum sType;
362               switch ( algo->GetDim() ) {
363               case 3:  sType = TopAbs_SOLID; break;
364               case 2:  sType = TopAbs_FACE; break;
365               default: sType = TopAbs_EDGE; break;
366               }
367               for ( TopExp_Explorer ex( s2hyps.Key(), sType ); ex.More(); ex.Next() )
368                 uniDimAlgoShapes.Add( ex.Current() );
369             }
370         }
371       }
372     }
373
374     {
375       // ------------------------------------------------
376       // sort list of sub-meshes according to mesh order
377       // ------------------------------------------------
378       aMesh.SortByMeshOrder( smVec );
379
380       // ------------------------------------------------------------
381       // compute sub-meshes with local uni-dimensional algos under
382       // sub-meshes with all-dimensional algos
383       // ------------------------------------------------------------
384       // start from lower shapes
385       for ( size_t i = 0; i < smVec.size(); ++i )
386       {
387         sm = smVec[i];
388         if ( sm->GetComputeState() != SMESH_subMesh::READY_TO_COMPUTE)
389           continue;
390
391         const TopAbs_ShapeEnum shapeType = sm->GetSubShape().ShapeType();
392
393         if ( !uniDimAlgoShapes.IsEmpty() )
394         {
395           // get a shape the algo is assigned to
396           if ( !GetAlgo( sm, & algoShape ))
397             continue; // strange...
398
399           // look for more local algos
400           if ( SMESH_subMesh* algoSM = aMesh.GetSubMesh( algoShape ))
401             smIt = algoSM->getDependsOnIterator(!includeSelf, !complexShapeFirst);
402           else
403             smIt = sm->getDependsOnIterator(!includeSelf, !complexShapeFirst);
404
405           while ( smIt->more() )
406           {
407             SMESH_subMesh* smToCompute = smIt->next();
408
409             const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
410             const           int aShapeDim = GetShapeDim( aSubShape );
411             if ( aShapeDim < 1 || aSubShape.ShapeType() <= shapeType )
412               continue;
413             if ( !uniDimAlgoShapes.Contains( aSubShape ))
414               continue; // [bos #29143] aMesh.GetHypothesis() is too long
415
416             // check for preview dimension limitations
417             if ( aShapesId && GetShapeDim( aSubShape.ShapeType() ) > (int)aDim )
418               continue;
419
420             SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
421             filter
422               .And( SMESH_HypoFilter::IsApplicableTo( aSubShape ))
423               .And( SMESH_HypoFilter::IsMoreLocalThan( algoShape, aMesh ));
424
425             if ( SMESH_Algo* subAlgo = (SMESH_Algo*) aMesh.GetHypothesis( smToCompute, filter, true))
426             {
427               if ( ! subAlgo->NeedDiscreteBoundary() ) continue;
428               TopTools_IndexedMapOfShape* localAllowed = allowedSubShapes;
429               if ( localAllowed && localAllowed->IsEmpty() )
430                 localAllowed = 0; // prevent fillAllowed() with  aSubShape
431
432               SMESH_Hypothesis::Hypothesis_Status status;
433               if ( subAlgo->CheckHypothesis( aMesh, aSubShape, status ))
434                 // mesh a lower smToCompute starting from vertices
435                 Compute( aMesh, aSubShape, aFlags | SHAPE_ONLY_UPWARD, aDim, aShapesId, localAllowed );
436             }
437           }
438         }
439         // --------------------------------
440         // apply the all-dimensional algo
441         // --------------------------------
442         {
443           if (_compute_canceled)
444             return false;
445           // check for preview dimension limitations
446           if ( aShapesId && GetShapeDim( shapeType ) > (int)aDim )
447             continue;
448           sm->SetAllowedSubShapes( fillAllowed( shapeSM, aShapeOnly, allowedSubShapes ));
449           setCurrentSubMesh( sm );
450           sm->ComputeStateEngine( computeEvent );
451           setCurrentSubMesh( NULL );
452           sm->SetAllowedSubShapes( nullptr );
453           if ( aShapesId )
454             aShapesId->insert( sm->GetId() );
455         }
456       }
457     }
458
459     // -----------------------------------------------
460     // mesh the rest sub-shapes starting from vertices
461     // -----------------------------------------------
462     ret = Compute( aMesh, aShape, aFlags | UPWARD, aDim, aShapesId, allowedSubShapes );
463   }
464
465   MEMOSTAT;
466
467   // fix quadratic mesh by bending iternal links near concave boundary
468   if ( aCompactMesh && // a final compute
469        aShape.IsSame( aMesh.GetShapeToMesh() ) &&
470        !aShapesId && // not preview
471        ret ) // everything is OK
472   {
473     SMESH_MesherHelper aHelper( aMesh );
474     if ( aHelper.IsQuadraticMesh() != SMESH_MesherHelper::LINEAR )
475     {
476       aHelper.FixQuadraticElements( shapeSM->GetComputeError() );
477     }
478   }
479
480   if ( aCompactMesh )
481   {
482     aMesh.GetMeshDS()->Modified();
483     aMesh.GetMeshDS()->CompactMesh();
484   }
485   return ret;
486 }
487
488 //=============================================================================
489 /*!
490  * Prepare Compute a mesh
491  */
492 //=============================================================================
493 void SMESH_Gen::PrepareCompute(SMESH_Mesh &          /*aMesh*/,
494                                const TopoDS_Shape &  /*aShape*/)
495 {
496   _compute_canceled = false;
497   resetCurrentSubMesh();
498 }
499
500 //=============================================================================
501 /*!
502  * Cancel Compute a mesh
503  */
504 //=============================================================================
505 void SMESH_Gen::CancelCompute(SMESH_Mesh &          /*aMesh*/,
506                               const TopoDS_Shape &  /*aShape*/)
507 {
508   _compute_canceled = true;
509   if ( const SMESH_subMesh* sm = GetCurrentSubMesh() )
510   {
511     const_cast< SMESH_subMesh* >( sm )->ComputeStateEngine( SMESH_subMesh::COMPUTE_CANCELED );
512   }
513   resetCurrentSubMesh();
514 }
515
516 //================================================================================
517 /*!
518  * \brief Returns a sub-mesh being currently computed
519  */
520 //================================================================================
521
522 const SMESH_subMesh* SMESH_Gen::GetCurrentSubMesh() const
523 {
524   return _sm_current.empty() ? 0 : _sm_current.back();
525 }
526
527 //================================================================================
528 /*!
529  * \brief Sets a sub-mesh being currently computed.
530  *
531  * An algorithm can call Compute() for a sub-shape, hence we keep a stack of sub-meshes
532  */
533 //================================================================================
534
535 void SMESH_Gen::setCurrentSubMesh(SMESH_subMesh* sm)
536 {
537   if ( sm )
538     _sm_current.push_back( sm );
539
540   else if ( !_sm_current.empty() )
541     _sm_current.pop_back();
542 }
543
544 void SMESH_Gen::resetCurrentSubMesh()
545 {
546   _sm_current.clear();
547 }
548
549 //=============================================================================
550 /*!
551  * Evaluate a mesh
552  */
553 //=============================================================================
554
555 bool SMESH_Gen::Evaluate(SMESH_Mesh &          aMesh,
556                          const TopoDS_Shape &  aShape,
557                          MapShapeNbElems&      aResMap,
558                          const bool            anUpward,
559                          TSetOfInt*            aShapesId)
560 {
561   bool ret = true;
562
563   SMESH_subMesh *sm = aMesh.GetSubMesh(aShape);
564
565   const bool includeSelf = true;
566   const bool complexShapeFirst = true;
567   SMESH_subMeshIteratorPtr smIt;
568
569   if ( anUpward ) { // is called from below code here
570     // -----------------------------------------------
571     // mesh all the sub-shapes starting from vertices
572     // -----------------------------------------------
573     smIt = sm->getDependsOnIterator(includeSelf, !complexShapeFirst);
574     while ( smIt->more() ) {
575       SMESH_subMesh* smToCompute = smIt->next();
576
577       // do not mesh vertices of a pseudo shape
578       const TopAbs_ShapeEnum shapeType = smToCompute->GetSubShape().ShapeType();
579       //if ( !aMesh.HasShapeToMesh() && shapeType == TopAbs_VERTEX )
580       //  continue;
581       if ( !aMesh.HasShapeToMesh() ) {
582         if( shapeType == TopAbs_VERTEX || shapeType == TopAbs_WIRE ||
583             shapeType == TopAbs_SHELL )
584           continue;
585       }
586
587       smToCompute->Evaluate(aResMap);
588       if( aShapesId )
589         aShapesId->insert( smToCompute->GetId() );
590     }
591     return ret;
592   }
593   else {
594     // -----------------------------------------------------------------
595     // apply algos that DO NOT require Discreteized boundaries and DO NOT
596     // support sub-meshes, starting from the most complex shapes
597     // and collect sub-meshes with algos that DO support sub-meshes
598     // -----------------------------------------------------------------
599     list< SMESH_subMesh* > smWithAlgoSupportingSubmeshes;
600     smIt = sm->getDependsOnIterator(includeSelf, complexShapeFirst);
601     while ( smIt->more() ) {
602       SMESH_subMesh* smToCompute = smIt->next();
603       const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
604       const int aShapeDim = GetShapeDim( aSubShape );
605       if ( aShapeDim < 1 ) break;
606       
607       SMESH_Algo* algo = GetAlgo( smToCompute );
608       if ( algo && !algo->NeedDiscreteBoundary() ) {
609         if ( algo->SupportSubmeshes() ) {
610           smWithAlgoSupportingSubmeshes.push_front( smToCompute );
611         }
612         else {
613           smToCompute->Evaluate(aResMap);
614           if ( aShapesId )
615             aShapesId->insert( smToCompute->GetId() );
616         }
617       }
618     }
619
620     // ------------------------------------------------------------
621     // sort list of meshes according to mesh order
622     // ------------------------------------------------------------
623     std::vector< SMESH_subMesh* > smVec( smWithAlgoSupportingSubmeshes.begin(),
624                                          smWithAlgoSupportingSubmeshes.end() );
625     aMesh.SortByMeshOrder( smVec );
626
627     // ------------------------------------------------------------
628     // compute sub-meshes under shapes with algos that DO NOT require
629     // Discreteized boundaries and DO support sub-meshes
630     // ------------------------------------------------------------
631     // start from lower shapes
632     for ( size_t i = 0; i < smVec.size(); ++i )
633     {
634       sm = smVec[i];
635
636       // get a shape the algo is assigned to
637       TopoDS_Shape algoShape;
638       if ( !GetAlgo( sm, & algoShape ))
639         continue; // strange...
640
641       // look for more local algos
642       smIt = sm->getDependsOnIterator(!includeSelf, !complexShapeFirst);
643       while ( smIt->more() ) {
644         SMESH_subMesh* smToCompute = smIt->next();
645
646         const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
647         const int aShapeDim = GetShapeDim( aSubShape );
648         if ( aShapeDim < 1 ) continue;
649
650         SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
651         filter
652           .And( SMESH_HypoFilter::IsApplicableTo( aSubShape ))
653           .And( SMESH_HypoFilter::IsMoreLocalThan( algoShape, aMesh ));
654
655         if ( SMESH_Algo* subAlgo = (SMESH_Algo*) aMesh.GetHypothesis( smToCompute, filter, true ))
656         {
657           if ( ! subAlgo->NeedDiscreteBoundary() ) continue;
658           SMESH_Hypothesis::Hypothesis_Status status;
659           if ( subAlgo->CheckHypothesis( aMesh, aSubShape, status ))
660             // mesh a lower smToCompute starting from vertices
661             Evaluate( aMesh, aSubShape, aResMap, /*anUpward=*/true, aShapesId );
662         }
663       }
664     }
665     // ----------------------------------------------------------
666     // apply the algos that do not require Discreteized boundaries
667     // ----------------------------------------------------------
668     for ( size_t i = 0; i < smVec.size(); ++i )
669     {
670       sm = smVec[i];
671       sm->Evaluate(aResMap);
672       if ( aShapesId )
673         aShapesId->insert( sm->GetId() );
674     }
675
676     // -----------------------------------------------
677     // mesh the rest sub-shapes starting from vertices
678     // -----------------------------------------------
679     ret = Evaluate( aMesh, aShape, aResMap, /*anUpward=*/true, aShapesId );
680   }
681
682   return ret;
683 }
684
685
686 //=======================================================================
687 //function : checkConformIgnoredAlgos
688 //purpose  :
689 //=======================================================================
690
691 static bool checkConformIgnoredAlgos(SMESH_Mesh&               aMesh,
692                                      SMESH_subMesh*            aSubMesh,
693                                      const SMESH_Algo*         aGlobIgnoAlgo,
694                                      const SMESH_Algo*         aLocIgnoAlgo,
695                                      bool &                    checkConform,
696                                      set<SMESH_subMesh*>&      aCheckedMap,
697                                      list< SMESH_Gen::TAlgoStateError > & theErrors)
698 {
699   ASSERT( aSubMesh );
700   if ( aSubMesh->GetSubShape().ShapeType() == TopAbs_VERTEX)
701     return true;
702
703
704   bool ret = true;
705
706   const list<const SMESHDS_Hypothesis*>& listHyp =
707     aMesh.GetMeshDS()->GetHypothesis( aSubMesh->GetSubShape() );
708   list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
709   for ( ; it != listHyp.end(); it++)
710   {
711     const SMESHDS_Hypothesis * aHyp = *it;
712     if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
713       continue;
714
715     const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
716     ASSERT ( algo );
717
718     if ( aLocIgnoAlgo ) // algo is hidden by a local algo of upper dim
719     {
720       theErrors.push_back( SMESH_Gen::TAlgoStateError() );
721       theErrors.back().Set( SMESH_Hypothesis::HYP_HIDDEN_ALGO, algo, false );
722       INFOS( "Local <" << algo->GetName() << "> is hidden by local <"
723             << aLocIgnoAlgo->GetName() << ">");
724     }
725     else
726     {
727       bool       isGlobal = (aMesh.IsMainShape( aSubMesh->GetSubShape() ));
728       int             dim = algo->GetDim();
729       int aMaxGlobIgnoDim = ( aGlobIgnoAlgo ? aGlobIgnoAlgo->GetDim() : -1 );
730       bool    isNeededDim = ( aGlobIgnoAlgo ? aGlobIgnoAlgo->NeedLowerHyps( dim ) : false );
731
732       if (( dim < aMaxGlobIgnoDim && !isNeededDim ) &&
733           ( isGlobal || !aGlobIgnoAlgo->SupportSubmeshes() ))
734       {
735         // algo is hidden by a global algo
736         theErrors.push_back( SMESH_Gen::TAlgoStateError() );
737         theErrors.back().Set( SMESH_Hypothesis::HYP_HIDDEN_ALGO, algo, true );
738         INFOS( ( isGlobal ? "Global" : "Local" )
739               << " <" << algo->GetName() << "> is hidden by global <"
740               << aGlobIgnoAlgo->GetName() << ">");
741       }
742       else if ( !algo->NeedDiscreteBoundary() && !isGlobal)
743       {
744         // local algo is not hidden and hides algos on sub-shapes
745         if (checkConform && !aSubMesh->IsConform( algo ))
746         {
747           ret = false;
748           checkConform = false; // no more check conformity
749           INFOS( "ERROR: Local <" << algo->GetName() <<
750                 "> would produce not conform mesh: "
751                 "<Not Conform Mesh Allowed> hypothesis is missing");
752           theErrors.push_back( SMESH_Gen::TAlgoStateError() );
753           theErrors.back().Set( SMESH_Hypothesis::HYP_NOTCONFORM, algo, false );
754         }
755
756         // sub-algos will be hidden by a local <algo> if <algo> does not support sub-meshes
757         if ( algo->SupportSubmeshes() )
758           algo = 0;
759         SMESH_subMeshIteratorPtr revItSub =
760           aSubMesh->getDependsOnIterator( /*includeSelf=*/false, /*complexShapeFirst=*/true);
761         bool checkConform2 = false;
762         while ( revItSub->more() )
763         {
764           SMESH_subMesh* sm = revItSub->next();
765           checkConformIgnoredAlgos (aMesh, sm, aGlobIgnoAlgo,
766                                     algo, checkConform2, aCheckedMap, theErrors);
767           aCheckedMap.insert( sm );
768         }
769       }
770     }
771   }
772
773   return ret;
774 }
775
776 //=======================================================================
777 //function : checkMissing
778 //purpose  : notify on missing hypothesis
779 //           Return false if algo or hipothesis is missing
780 //=======================================================================
781
782 static bool checkMissing(SMESH_Gen*                aGen,
783                          SMESH_Mesh&               aMesh,
784                          SMESH_subMesh*            aSubMesh,
785                          const int                 aTopAlgoDim,
786                          bool*                     globalChecked,
787                          const bool                checkNoAlgo,
788                          set<SMESH_subMesh*>&      aCheckedMap,
789                          list< SMESH_Gen::TAlgoStateError > & theErrors)
790 {
791   switch ( aSubMesh->GetSubShape().ShapeType() )
792   {
793   case TopAbs_EDGE:
794   case TopAbs_FACE:
795   case TopAbs_SOLID: break; // check this sub-mesh, it can be meshed
796   default:
797     return true; // not meshable sub-mesh
798   }
799   if ( aCheckedMap.count( aSubMesh ))
800     return true;
801
802   int ret = true;
803   SMESH_Algo* algo = 0;
804
805   switch (aSubMesh->GetAlgoState())
806   {
807   case SMESH_subMesh::NO_ALGO: {
808     if (checkNoAlgo)
809     {
810       // should there be any algo?
811       int shapeDim = SMESH_Gen::GetShapeDim( aSubMesh->GetSubShape() );
812       if (aTopAlgoDim > shapeDim)
813       {
814         MESSAGE( "ERROR: " << shapeDim << "D algorithm is missing" );
815         ret = false;
816         theErrors.push_back( SMESH_Gen::TAlgoStateError() );
817         theErrors.back().Set( SMESH_Hypothesis::HYP_MISSING, shapeDim, true );
818       }
819     }
820     return ret;
821   }
822   case SMESH_subMesh::MISSING_HYP: {
823     // notify if an algo missing hyp is attached to aSubMesh
824     algo = aSubMesh->GetAlgo();
825     ASSERT( algo );
826     bool IsGlobalHypothesis = aGen->IsGlobalHypothesis( algo, aMesh );
827     if (!IsGlobalHypothesis || !globalChecked[ algo->GetDim() ])
828     {
829       TAlgoStateErrorName errName = SMESH_Hypothesis::HYP_MISSING;
830       SMESH_Hypothesis::Hypothesis_Status status;
831       algo->CheckHypothesis( aMesh, aSubMesh->GetSubShape(), status );
832       if ( status == SMESH_Hypothesis::HYP_BAD_PARAMETER ) {
833         MESSAGE( "ERROR: hypothesis of " << (IsGlobalHypothesis ? "Global " : "Local ")
834                  << "<" << algo->GetName() << "> has a bad parameter value");
835         errName = status;
836       } else if ( status == SMESH_Hypothesis::HYP_BAD_GEOMETRY ) {
837         MESSAGE( "ERROR: " << (IsGlobalHypothesis ? "Global " : "Local ")
838                  << "<" << algo->GetName() << "> assigned to mismatching geometry");
839         errName = status;
840       } else {
841         MESSAGE( "ERROR: " << (IsGlobalHypothesis ? "Global " : "Local ")
842                  << "<" << algo->GetName() << "> misses some hypothesis");
843       }
844       if (IsGlobalHypothesis)
845         globalChecked[ algo->GetDim() ] = true;
846       theErrors.push_back( SMESH_Gen::TAlgoStateError() );
847       theErrors.back().Set( errName, algo, IsGlobalHypothesis );
848     }
849     ret = false;
850     break;
851   }
852   case SMESH_subMesh::HYP_OK:
853     algo = aSubMesh->GetAlgo();
854     ret = true;
855     if (!algo->NeedDiscreteBoundary())
856     {
857       SMESH_subMeshIteratorPtr itsub = aSubMesh->getDependsOnIterator( /*includeSelf=*/false,
858                                                                        /*complexShapeFirst=*/false);
859       while ( itsub->more() )
860         aCheckedMap.insert( itsub->next() );
861     }
862     break;
863   default: ASSERT(0);
864   }
865
866   // do not check under algo that hides sub-algos or
867   // re-start checking NO_ALGO state
868   ASSERT (algo);
869   bool isTopLocalAlgo =
870     ( aTopAlgoDim <= algo->GetDim() && !aGen->IsGlobalHypothesis( algo, aMesh ));
871   if (!algo->NeedDiscreteBoundary() || isTopLocalAlgo)
872   {
873     bool checkNoAlgo2 = ( algo->NeedDiscreteBoundary() );
874     SMESH_subMeshIteratorPtr itsub = aSubMesh->getDependsOnIterator( /*includeSelf=*/false,
875                                                                      /*complexShapeFirst=*/true);
876     while ( itsub->more() )
877     {
878       // sub-meshes should not be checked further more
879       SMESH_subMesh* sm = itsub->next();
880
881       if (isTopLocalAlgo)
882       {
883         //check algo on sub-meshes
884         int aTopAlgoDim2 = algo->GetDim();
885         if (!checkMissing (aGen, aMesh, sm, aTopAlgoDim2,
886                            globalChecked, checkNoAlgo2, aCheckedMap, theErrors))
887         {
888           ret = false;
889           if (sm->GetAlgoState() == SMESH_subMesh::NO_ALGO )
890             checkNoAlgo2 = false;
891         }
892       }
893       aCheckedMap.insert( sm );
894     }
895   }
896   return ret;
897 }
898
899 //=======================================================================
900 //function : CheckAlgoState
901 //purpose  : notify on bad state of attached algos, return false
902 //           if Compute() would fail because of some algo bad state
903 //=======================================================================
904
905 bool SMESH_Gen::CheckAlgoState(SMESH_Mesh& aMesh, const TopoDS_Shape& aShape)
906 {
907   list< TAlgoStateError > errors;
908   return GetAlgoState( aMesh, aShape, errors );
909 }
910
911 //=======================================================================
912 //function : GetAlgoState
913 //purpose  : notify on bad state of attached algos, return false
914 //           if Compute() would fail because of some algo bad state
915 //           theErrors list contains problems description
916 //=======================================================================
917
918 bool SMESH_Gen::GetAlgoState(SMESH_Mesh&               theMesh,
919                              const TopoDS_Shape&       theShape,
920                              list< TAlgoStateError > & theErrors)
921 {
922   bool ret = true;
923   bool hasAlgo = false;
924
925   SMESH_subMesh*          sm = theMesh.GetSubMesh(theShape);
926   const SMESHDS_Mesh* meshDS = theMesh.GetMeshDS();
927   TopoDS_Shape     mainShape = meshDS->ShapeToMesh();
928
929   // -----------------
930   // get global algos
931   // -----------------
932
933   const SMESH_Algo* aGlobAlgoArr[] = {0,0,0,0};
934
935   const list<const SMESHDS_Hypothesis*>& listHyp = meshDS->GetHypothesis( mainShape );
936   list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
937   for ( ; it != listHyp.end(); it++)
938   {
939     const SMESHDS_Hypothesis * aHyp = *it;
940     if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
941       continue;
942
943     const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
944     ASSERT ( algo );
945
946     int dim = algo->GetDim();
947     aGlobAlgoArr[ dim ] = algo;
948
949     hasAlgo = true;
950   }
951
952   // --------------------------------------------------------
953   // info on algos that will be ignored because of ones that
954   // don't NeedDiscreteBoundary() attached to super-shapes,
955   // check that a conform mesh will be produced
956   // --------------------------------------------------------
957
958
959   // find a global algo possibly hiding sub-algos
960   int dim;
961   const SMESH_Algo* aGlobIgnoAlgo = 0;
962   for (dim = 3; dim > 0; dim--)
963   {
964     if (aGlobAlgoArr[ dim ] &&
965         !aGlobAlgoArr[ dim ]->NeedDiscreteBoundary() /*&&
966         !aGlobAlgoArr[ dim ]->SupportSubmeshes()*/ )
967     {
968       aGlobIgnoAlgo = aGlobAlgoArr[ dim ];
969       break;
970     }
971   }
972
973   set<SMESH_subMesh*> aCheckedSubs;
974   bool checkConform = ( !theMesh.IsNotConformAllowed() );
975
976   // loop on theShape and its sub-shapes
977   SMESH_subMeshIteratorPtr revItSub = sm->getDependsOnIterator( /*includeSelf=*/true,
978                                                                 /*complexShapeFirst=*/true);
979   while ( revItSub->more() )
980   {
981     SMESH_subMesh* smToCheck = revItSub->next();
982     if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
983       break;
984
985     if ( aCheckedSubs.insert( smToCheck ).second ) // not yet checked
986       if (!checkConformIgnoredAlgos (theMesh, smToCheck, aGlobIgnoAlgo,
987                                      0, checkConform, aCheckedSubs, theErrors))
988         ret = false;
989
990     if ( smToCheck->GetAlgoState() != SMESH_subMesh::NO_ALGO )
991       hasAlgo = true;
992   }
993
994   // ----------------------------------------------------------------
995   // info on missing hypothesis and find out if all needed algos are
996   // well defined
997   // ----------------------------------------------------------------
998
999   // find max dim of global algo
1000   int aTopAlgoDim = 0;
1001   for (dim = 3; dim > 0; dim--)
1002   {
1003     if (aGlobAlgoArr[ dim ])
1004     {
1005       aTopAlgoDim = dim;
1006       break;
1007     }
1008   }
1009   bool checkNoAlgo = theMesh.HasShapeToMesh() ? bool( aTopAlgoDim ) : false;
1010   bool globalChecked[] = { false, false, false, false };
1011
1012   // loop on theShape and its sub-shapes
1013   aCheckedSubs.clear();
1014   revItSub = sm->getDependsOnIterator( /*includeSelf=*/true, /*complexShapeFirst=*/true);
1015   while ( revItSub->more() )
1016   {
1017     SMESH_subMesh* smToCheck = revItSub->next();
1018     if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
1019       break;
1020
1021     if (!checkMissing (this, theMesh, smToCheck, aTopAlgoDim,
1022                        globalChecked, checkNoAlgo, aCheckedSubs, theErrors))
1023     {
1024       ret = false;
1025       if (smToCheck->GetAlgoState() == SMESH_subMesh::NO_ALGO )
1026         checkNoAlgo = false;
1027     }
1028   }
1029
1030   if ( !hasAlgo ) {
1031     ret = false;
1032     theErrors.push_back( TAlgoStateError() );
1033     theErrors.back().Set( SMESH_Hypothesis::HYP_MISSING, theMesh.HasShapeToMesh() ? 1 : 3, true );
1034   }
1035
1036   return ret;
1037 }
1038
1039 //=======================================================================
1040 //function : IsGlobalHypothesis
1041 //purpose  : check if theAlgo is attached to the main shape
1042 //=======================================================================
1043
1044 bool SMESH_Gen::IsGlobalHypothesis(const SMESH_Hypothesis* theHyp, SMESH_Mesh& aMesh)
1045 {
1046   SMESH_HypoFilter filter( SMESH_HypoFilter::Is( theHyp ));
1047   return aMesh.GetHypothesis( aMesh.GetMeshDS()->ShapeToMesh(), filter, false );
1048 }
1049
1050 //================================================================================
1051 /*!
1052  * \brief Return paths to xml files of plugins
1053  */
1054 //================================================================================
1055
1056 std::vector< std::string > SMESH_Gen::GetPluginXMLPaths()
1057 {
1058   // Get paths to xml files of plugins
1059   vector< string > xmlPaths;
1060   string sep;
1061   if ( const char* meshersList = getenv("SMESH_MeshersList") )
1062   {
1063     string meshers = meshersList, plugin;
1064     string::size_type from = 0, pos;
1065     while ( from < meshers.size() )
1066     {
1067       // cut off plugin name
1068       pos = meshers.find( env_sep, from );
1069       if ( pos != string::npos )
1070         plugin = meshers.substr( from, pos-from );
1071       else
1072         plugin = meshers.substr( from ), pos = meshers.size();
1073       from = pos + 1;
1074
1075       // get PLUGIN_ROOT_DIR path
1076       string rootDirVar, pluginSubDir = plugin;
1077       if ( plugin == "StdMeshers" )
1078         rootDirVar = "SMESH", pluginSubDir = "smesh";
1079       else
1080         for ( pos = 0; pos < plugin.size(); ++pos )
1081           rootDirVar += toupper( plugin[pos] );
1082       rootDirVar += "_ROOT_DIR";
1083
1084       const char* rootDir = getenv( rootDirVar.c_str() );
1085       if ( !rootDir || strlen(rootDir) == 0 )
1086       {
1087         rootDirVar = plugin + "_ROOT_DIR"; // HexoticPLUGIN_ROOT_DIR
1088         rootDir = getenv( rootDirVar.c_str() );
1089         if ( !rootDir || strlen(rootDir) == 0 ) continue;
1090       }
1091
1092       // get a separator from rootDir
1093       for ( int i = strlen( rootDir )-1; i >= 0 && sep.empty(); --i )
1094         if ( rootDir[i] == '/' || rootDir[i] == '\\' )
1095         {
1096           sep = rootDir[i];
1097           break;
1098         }
1099 #ifdef WIN32
1100       if (sep.empty() ) sep = "\\";
1101 #else
1102       if (sep.empty() ) sep = "/";
1103 #endif
1104
1105       // get a path to resource file
1106       string xmlPath = rootDir;
1107       if ( xmlPath[ xmlPath.size()-1 ] != sep[0] )
1108         xmlPath += sep;
1109       xmlPath += "share" + sep + "salome" + sep + "resources" + sep;
1110       for ( pos = 0; pos < pluginSubDir.size(); ++pos )
1111         xmlPath += tolower( pluginSubDir[pos] );
1112       xmlPath += sep + plugin + ".xml";
1113       bool fileOK;
1114 #ifdef WIN32
1115 #  ifdef UNICODE
1116       const wchar_t* path = Kernel_Utils::decode_s(xmlPath);
1117       SMESHUtils::ArrayDeleter<const wchar_t> deleter( path );
1118 #  else
1119       const char* path = xmlPath.c_str();
1120 #  endif
1121       fileOK = (GetFileAttributes(path) != INVALID_FILE_ATTRIBUTES);
1122
1123 #else
1124       fileOK = (access(xmlPath.c_str(), F_OK) == 0);
1125 #endif
1126       if ( fileOK )
1127         xmlPaths.push_back( xmlPath );
1128     }
1129   }
1130
1131   return xmlPaths;
1132 }
1133
1134 //=============================================================================
1135 /*!
1136  * Finds algo to mesh a shape. Optionally returns a shape the found algo is bound to
1137  */
1138 //=============================================================================
1139
1140 SMESH_Algo *SMESH_Gen::GetAlgo(SMESH_Mesh &         aMesh,
1141                                const TopoDS_Shape & aShape,
1142                                TopoDS_Shape*        assignedTo)
1143 {
1144   return GetAlgo( aMesh.GetSubMesh( aShape ), assignedTo );
1145 }
1146
1147 //=============================================================================
1148 /*!
1149  * Finds algo to mesh a sub-mesh. Optionally returns a shape the found algo is bound to
1150  */
1151 //=============================================================================
1152
1153 SMESH_Algo *SMESH_Gen::GetAlgo(SMESH_subMesh * aSubMesh,
1154                                TopoDS_Shape*   assignedTo)
1155 {
1156   if ( !aSubMesh ) return 0;
1157
1158   const TopoDS_Shape & aShape = aSubMesh->GetSubShape();
1159   SMESH_Mesh&          aMesh  = *aSubMesh->GetFather();
1160
1161   SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
1162   if ( aMesh.HasShapeToMesh() )
1163     filter.And( filter.IsApplicableTo( aShape ));
1164
1165   typedef SMESH_Algo::Features AlgoData;
1166
1167   TopoDS_Shape assignedToShape;
1168   SMESH_Algo* algo =
1169     (SMESH_Algo*) aMesh.GetHypothesis( aSubMesh, filter, true, &assignedToShape );
1170
1171   if ( algo &&
1172        aShape.ShapeType() == TopAbs_FACE &&
1173        !aShape.IsSame( assignedToShape ) &&
1174        SMESH_MesherHelper::NbAncestors( aShape, aMesh, TopAbs_SOLID ) > 1 )
1175   {
1176     // Issue 0021559. If there is another 2D algo with different types of output
1177     // elements that can be used to mesh aShape, and 3D algos on adjacent SOLIDs
1178     // have different types of input elements, we choose a most appropriate 2D algo.
1179
1180     // try to find a concurrent 2D algo
1181     filter.AndNot( filter.Is( algo ));
1182     TopoDS_Shape assignedToShape2;
1183     SMESH_Algo* algo2 =
1184       (SMESH_Algo*) aMesh.GetHypothesis( aSubMesh, filter, true, &assignedToShape2 );
1185     if ( algo2 &&                                                  // algo found
1186          !assignedToShape2.IsSame( aMesh.GetShapeToMesh() ) &&     // algo is local
1187          ( SMESH_MesherHelper::GetGroupType( assignedToShape2 ) == // algo of the same level
1188            SMESH_MesherHelper::GetGroupType( assignedToShape )) &&
1189          aMesh.IsOrderOK( aMesh.GetSubMesh( assignedToShape2 ),    // no forced order
1190                           aMesh.GetSubMesh( assignedToShape  )))
1191     {
1192       // get algos on the adjacent SOLIDs
1193       filter.Init( filter.IsAlgo() ).And( filter.HasDim( 3 ));
1194       vector< SMESH_Algo* > algos3D;
1195       PShapeIteratorPtr solidIt = SMESH_MesherHelper::GetAncestors( aShape, aMesh,
1196                                                                     TopAbs_SOLID );
1197       while ( const TopoDS_Shape* solid = solidIt->next() )
1198         if ( SMESH_Algo* algo3D = (SMESH_Algo*) aMesh.GetHypothesis( *solid, filter, true ))
1199         {
1200           algos3D.push_back( algo3D );
1201           filter.AndNot( filter.HasName( algo3D->GetName() ));
1202         }
1203       // check compatibility of algos
1204       if ( algos3D.size() > 1 )
1205       {
1206         const AlgoData& algoData    = algo->SMESH_Algo::GetFeatures();
1207         const AlgoData& algoData2   = algo2->SMESH_Algo::GetFeatures();
1208         const AlgoData& algoData3d0 = algos3D[0]->SMESH_Algo::GetFeatures();
1209         const AlgoData& algoData3d1 = algos3D[1]->SMESH_Algo::GetFeatures();
1210         if (( algoData2.IsCompatible( algoData3d0 ) &&
1211               algoData2.IsCompatible( algoData3d1 ))
1212             &&
1213             !(algoData.IsCompatible( algoData3d0 ) &&
1214               algoData.IsCompatible( algoData3d1 )))
1215           algo = algo2;
1216       }
1217     }
1218   }
1219
1220   if ( assignedTo && algo )
1221     * assignedTo = assignedToShape;
1222
1223   return algo;
1224 }
1225
1226 //=============================================================================
1227 /*!
1228  * Returns StudyContextStruct for a study
1229  */
1230 //=============================================================================
1231
1232 StudyContextStruct *SMESH_Gen::GetStudyContext()
1233 {
1234   return _studyContext;
1235 }
1236
1237 //================================================================================
1238 /*!
1239  * \brief Return shape dimension by TopAbs_ShapeEnum
1240  */
1241 //================================================================================
1242
1243 int SMESH_Gen::GetShapeDim(const TopAbs_ShapeEnum & aShapeType)
1244 {
1245   static vector<int> dim;
1246   if ( dim.empty() )
1247   {
1248     dim.resize( TopAbs_SHAPE, -1 );
1249     dim[ TopAbs_COMPOUND ]  = MeshDim_3D;
1250     dim[ TopAbs_COMPSOLID ] = MeshDim_3D;
1251     dim[ TopAbs_SOLID ]     = MeshDim_3D;
1252     dim[ TopAbs_SHELL ]     = MeshDim_2D;
1253     dim[ TopAbs_FACE  ]     = MeshDim_2D;
1254     dim[ TopAbs_WIRE ]      = MeshDim_1D;
1255     dim[ TopAbs_EDGE ]      = MeshDim_1D;
1256     dim[ TopAbs_VERTEX ]    = MeshDim_0D;
1257   }
1258   return dim[ aShapeType ];
1259 }
1260
1261 //================================================================================
1262 /*!
1263  * \brief Return shape dimension by exploding compounds
1264  */
1265 //================================================================================
1266
1267 int SMESH_Gen::GetFlatShapeDim(const TopoDS_Shape &aShape)
1268 {
1269   int aShapeDim;
1270   if ( aShape.ShapeType() == TopAbs_COMPOUND ||
1271        aShape.ShapeType() == TopAbs_COMPSOLID )
1272   {
1273     TopoDS_Iterator it( aShape );
1274     aShapeDim = GetFlatShapeDim( it.Value() );
1275   }
1276   else
1277     aShapeDim = GetShapeDim( aShape );
1278   return aShapeDim;
1279 }
1280
1281 //=============================================================================
1282 /*!
1283  * Generate a new id unique within this Gen
1284  */
1285 //=============================================================================
1286
1287 int SMESH_Gen::GetANewId()
1288 {
1289   return _hypId++;
1290 }