Salome HOME
Exclude "None algorithm attached" message on GetAlgoState method call
[modules/smesh.git] / src / SMESH / SMESH_Gen.cxx
1 // Copyright (C) 2007-2012  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
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_MesherHelper.hxx"
39 #include "SMESH_subMesh.hxx"
40
41 #include "utilities.h"
42 #include "OpUtil.hxx"
43 #include "Utils_ExceptHandlers.hxx"
44
45 #include <TopoDS_Iterator.hxx>
46
47 #include "memoire.h"
48
49 #ifdef WNT
50 #include <windows.h>
51 #endif
52
53 using namespace std;
54
55 //=============================================================================
56 /*!
57  *  Constructor
58  */
59 //=============================================================================
60
61 SMESH_Gen::SMESH_Gen()
62 {
63         MESSAGE("SMESH_Gen::SMESH_Gen");
64         _localId = 0;
65         _hypId = 0;
66         _segmentation = _nbSegments = 10;
67         SMDS_Mesh::_meshList.clear();
68         MESSAGE(SMDS_Mesh::_meshList.size());
69         _counters = new counters(100);
70 #ifdef WITH_SMESH_CANCEL_COMPUTE
71         _compute_canceled = false;
72         _sm_current = NULL;
73 #endif
74 }
75
76 //=============================================================================
77 /*!
78  * Destructor
79  */
80 //=============================================================================
81
82 SMESH_Gen::~SMESH_Gen()
83 {
84   MESSAGE("SMESH_Gen::~SMESH_Gen");
85 }
86
87 //=============================================================================
88 /*!
89  * Creates a mesh in a study.
90  * if (theIsEmbeddedMode) { mesh modification commands are not logged }
91  */
92 //=============================================================================
93
94 SMESH_Mesh* SMESH_Gen::CreateMesh(int theStudyId, bool theIsEmbeddedMode)
95   throw(SALOME_Exception)
96 {
97   Unexpect aCatch(SalomeException);
98   MESSAGE("SMESH_Gen::CreateMesh");
99
100   // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
101   StudyContextStruct *aStudyContext = GetStudyContext(theStudyId);
102
103   // create a new SMESH_mesh object
104   SMESH_Mesh *aMesh = new SMESH_Mesh(_localId++,
105                                      theStudyId,
106                                      this,
107                                      theIsEmbeddedMode,
108                                      aStudyContext->myDocument);
109   aStudyContext->mapMesh[_localId-1] = aMesh;
110
111   return aMesh;
112 }
113
114 //=============================================================================
115 /*!
116  * Compute a mesh
117  */
118 //=============================================================================
119
120 bool SMESH_Gen::Compute(SMESH_Mesh &          aMesh,
121                         const TopoDS_Shape &  aShape,
122                         const bool            anUpward,
123                         const ::MeshDimension aDim,
124                         TSetOfInt*            aShapesId)
125 {
126   MESSAGE("SMESH_Gen::Compute");
127   MEMOSTAT;
128
129   bool ret = true;
130
131   SMESH_subMesh *sm = aMesh.GetSubMesh(aShape);
132
133   const bool includeSelf = true;
134   const bool complexShapeFirst = true;
135   const int  globalAlgoDim = 100;
136
137   SMESH_subMeshIteratorPtr smIt;
138
139   if ( anUpward ) // is called from below code here
140   {
141     // -----------------------------------------------
142     // mesh all the sub-shapes starting from vertices
143     // -----------------------------------------------
144     smIt = sm->getDependsOnIterator(includeSelf, !complexShapeFirst);
145     while ( smIt->more() )
146     {
147       SMESH_subMesh* smToCompute = smIt->next();
148
149       // do not mesh vertices of a pseudo shape
150       const TopAbs_ShapeEnum aShType = smToCompute->GetSubShape().ShapeType();
151       if ( !aMesh.HasShapeToMesh() && aShType == TopAbs_VERTEX )
152         continue;
153
154       // check for preview dimension limitations
155       if ( aShapesId && GetShapeDim( aShType ) > (int)aDim )
156       {
157         // clear compute state to not show previous compute errors
158         //  if preview invoked less dimension less than previous
159         smToCompute->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
160         continue;
161       }
162
163       if (smToCompute->GetComputeState() == SMESH_subMesh::READY_TO_COMPUTE)
164       {
165 #ifdef WITH_SMESH_CANCEL_COMPUTE
166         if (_compute_canceled)
167           return false;
168         _sm_current = smToCompute;
169 #endif
170         smToCompute->ComputeStateEngine( SMESH_subMesh::COMPUTE );
171 #ifdef WITH_SMESH_CANCEL_COMPUTE
172         _sm_current = NULL;
173 #endif
174       }
175
176       // we check all the submeshes here and detect if any of them failed to compute
177       if (smToCompute->GetComputeState() == SMESH_subMesh::FAILED_TO_COMPUTE)
178         ret = false;
179       else if ( aShapesId )
180         aShapesId->insert( smToCompute->GetId() );
181     }
182     //aMesh.GetMeshDS()->Modified();
183     return ret;
184   }
185   else
186   {
187     // -----------------------------------------------------------------
188     // apply algos that DO NOT require Discreteized boundaries and DO NOT
189     // support submeshes, starting from the most complex shapes
190     // and collect submeshes with algos that DO support submeshes
191     // -----------------------------------------------------------------
192     list< SMESH_subMesh* > smWithAlgoSupportingSubmeshes;
193
194     // map to sort sm with same dim algos according to dim of
195     // the shape the algo assigned to (issue 0021217)
196     multimap< int, SMESH_subMesh* > shDim2sm;
197     multimap< int, SMESH_subMesh* >::reverse_iterator shDim2smIt;
198     TopoDS_Shape algoShape;
199     int prevShapeDim = -1;
200
201     smIt = sm->getDependsOnIterator(includeSelf, complexShapeFirst);
202     while ( smIt->more() )
203     {
204       SMESH_subMesh* smToCompute = smIt->next();
205       if ( smToCompute->GetComputeState() != SMESH_subMesh::READY_TO_COMPUTE )
206         continue;
207
208       const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
209       int aShapeDim = GetShapeDim( aSubShape );
210       if ( aShapeDim < 1 ) break;
211       
212       // check for preview dimension limitations
213       if ( aShapesId && aShapeDim > (int)aDim )
214         continue;
215
216       SMESH_Algo* algo = GetAlgo( aMesh, aSubShape, &algoShape );
217       if ( algo && !algo->NeedDiscreteBoundary() )
218       {
219         if ( algo->SupportSubmeshes() )
220         {
221           // reload sub-meshes from shDim2sm into smWithAlgoSupportingSubmeshes
222           // so that more local algos to go first
223           if ( prevShapeDim != aShapeDim )
224           {
225             prevShapeDim = aShapeDim;
226             for ( shDim2smIt = shDim2sm.rbegin(); shDim2smIt != shDim2sm.rend(); ++shDim2smIt )
227               if ( shDim2smIt->first == globalAlgoDim )
228                 smWithAlgoSupportingSubmeshes.push_back( shDim2smIt->second );
229               else
230                 smWithAlgoSupportingSubmeshes.push_front( shDim2smIt->second );
231             shDim2sm.clear();
232           }
233           // add smToCompute to shDim2sm map
234           if ( algoShape.IsSame( aMesh.GetShapeToMesh() ))
235           {
236             aShapeDim = globalAlgoDim; // to compute last
237           }
238           else
239           {
240             aShapeDim = GetShapeDim( algoShape );
241             if ( algoShape.ShapeType() == TopAbs_COMPOUND )
242             {
243               TopoDS_Iterator it( algoShape );
244               aShapeDim += GetShapeDim( it.Value() );
245             }
246           }
247           shDim2sm.insert( make_pair( aShapeDim, smToCompute ));
248         }
249         else
250         {
251 #ifdef WITH_SMESH_CANCEL_COMPUTE
252           if (_compute_canceled)
253             return false;
254           _sm_current = smToCompute;
255 #endif
256           smToCompute->ComputeStateEngine( SMESH_subMesh::COMPUTE );
257 #ifdef WITH_SMESH_CANCEL_COMPUTE
258           _sm_current = NULL;
259 #endif
260           if ( aShapesId )
261             aShapesId->insert( smToCompute->GetId() );
262         }
263       }
264     }
265     // reload sub-meshes from shDim2sm into smWithAlgoSupportingSubmeshes
266     for ( shDim2smIt = shDim2sm.rbegin(); shDim2smIt != shDim2sm.rend(); ++shDim2smIt )
267       if ( shDim2smIt->first == globalAlgoDim )
268         smWithAlgoSupportingSubmeshes.push_back( shDim2smIt->second );
269       else
270         smWithAlgoSupportingSubmeshes.push_front( shDim2smIt->second );
271
272     // ------------------------------------------------------------
273     // sort list of submeshes according to mesh order
274     // ------------------------------------------------------------
275     aMesh.SortByMeshOrder( smWithAlgoSupportingSubmeshes );
276
277     // ------------------------------------------------------------
278     // compute submeshes under shapes with algos that DO NOT require
279     // Discreteized boundaries and DO support submeshes
280     // ------------------------------------------------------------
281     list< SMESH_subMesh* >::iterator subIt, subEnd;
282     subIt  = smWithAlgoSupportingSubmeshes.begin();
283     subEnd = smWithAlgoSupportingSubmeshes.end();
284     // start from lower shapes
285     for ( ; subIt != subEnd; ++subIt )
286     {
287       sm = *subIt;
288
289       // get a shape the algo is assigned to
290       if ( !GetAlgo( aMesh, sm->GetSubShape(), & algoShape ))
291         continue; // strange...
292
293       // look for more local algos
294       smIt = sm->getDependsOnIterator(!includeSelf, !complexShapeFirst);
295       while ( smIt->more() )
296       {
297         SMESH_subMesh* smToCompute = smIt->next();
298
299         const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
300         const int aShapeDim = GetShapeDim( aSubShape );
301         //if ( aSubShape.ShapeType() == TopAbs_VERTEX ) continue;
302         if ( aShapeDim < 1 ) continue;
303
304         // check for preview dimension limitations
305         if ( aShapesId && GetShapeDim( aSubShape.ShapeType() ) > (int)aDim )
306           continue;
307         
308         SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
309         filter
310           .And( SMESH_HypoFilter::IsApplicableTo( aSubShape ))
311           .And( SMESH_HypoFilter::IsMoreLocalThan( algoShape, aMesh ));
312
313         if ( SMESH_Algo* subAlgo = (SMESH_Algo*) aMesh.GetHypothesis( aSubShape, filter, true )) {
314           SMESH_Hypothesis::Hypothesis_Status status;
315           if ( subAlgo->CheckHypothesis( aMesh, aSubShape, status ))
316             // mesh a lower smToCompute starting from vertices
317             Compute( aMesh, aSubShape, /*anUpward=*/true, aDim, aShapesId );
318         }
319       }
320     }
321     // ----------------------------------------------------------
322     // apply the algos that do not require Discreteized boundaries
323     // ----------------------------------------------------------
324     for ( subIt = smWithAlgoSupportingSubmeshes.begin(); subIt != subEnd; ++subIt )
325     {
326       sm = *subIt;
327       if ( sm->GetComputeState() == SMESH_subMesh::READY_TO_COMPUTE)
328       {
329         const TopAbs_ShapeEnum aShType = sm->GetSubShape().ShapeType();
330         // check for preview dimension limitations
331         if ( aShapesId && GetShapeDim( aShType ) > (int)aDim )
332           continue;
333
334 #ifdef WITH_SMESH_CANCEL_COMPUTE
335         if (_compute_canceled)
336           return false;
337         _sm_current = sm;
338 #endif
339         sm->ComputeStateEngine( SMESH_subMesh::COMPUTE );
340 #ifdef WITH_SMESH_CANCEL_COMPUTE
341         _sm_current = NULL;
342 #endif
343         if ( aShapesId )
344           aShapesId->insert( sm->GetId() );
345       }
346     }
347     // -----------------------------------------------
348     // mesh the rest sub-shapes starting from vertices
349     // -----------------------------------------------
350     ret = Compute( aMesh, aShape, /*anUpward=*/true, aDim, aShapesId );
351   }
352
353   MESSAGE( "VSR - SMESH_Gen::Compute() finished, OK = " << ret);
354   MEMOSTAT;
355
356   SMESHDS_Mesh *myMesh = aMesh.GetMeshDS();
357   myMesh->adjustStructure();
358   MESSAGE("*** compactMesh after compute");
359   myMesh->compactMesh();
360   //myMesh->adjustStructure();
361   list<int> listind = myMesh->SubMeshIndices();
362   list<int>::iterator it = listind.begin();
363   int total = 0;
364   for(; it != listind.end(); ++it)
365     {
366       ::SMESHDS_SubMesh *subMesh = myMesh->MeshElements(*it);
367       total +=  subMesh->getSize();
368     }
369   MESSAGE("total elements and nodes in submesh sets:" << total);
370   MESSAGE("Number of node objects " << SMDS_MeshNode::nbNodes);
371   MESSAGE("Number of cell objects " << SMDS_MeshCell::nbCells);
372   //myMesh->dumpGrid();
373   //aMesh.GetMeshDS()->Modified();
374
375   // fix quadratic mesh by bending iternal links near concave boundary
376   if ( aShape.IsSame( aMesh.GetShapeToMesh() ) &&
377        !aShapesId ) // not preview
378   {
379     SMESH_MesherHelper aHelper( aMesh );
380     if ( aHelper.IsQuadraticMesh() != SMESH_MesherHelper::LINEAR )
381       aHelper.FixQuadraticElements();
382   }
383   return ret;
384 }
385
386
387 #ifdef WITH_SMESH_CANCEL_COMPUTE
388 //=============================================================================
389 /*!
390  * Prepare Compute a mesh
391  */
392 //=============================================================================
393 void SMESH_Gen::PrepareCompute(SMESH_Mesh &          aMesh,
394                                const TopoDS_Shape &  aShape)
395 {
396   _compute_canceled = false;
397   _sm_current = NULL;
398 }
399 //=============================================================================
400 /*!
401  * Cancel Compute a mesh
402  */
403 //=============================================================================
404 void SMESH_Gen::CancelCompute(SMESH_Mesh &          aMesh,
405                               const TopoDS_Shape &  aShape)
406 {
407   _compute_canceled = true;
408   if(_sm_current)
409     {
410       _sm_current->ComputeStateEngine( SMESH_subMesh::COMPUTE_CANCELED );
411     }
412 }
413 #endif
414
415 //=============================================================================
416 /*!
417  * Evaluate a mesh
418  */
419 //=============================================================================
420
421 bool SMESH_Gen::Evaluate(SMESH_Mesh &          aMesh,
422                          const TopoDS_Shape &  aShape,
423                          MapShapeNbElems&      aResMap,
424                          const bool            anUpward,
425                          TSetOfInt*            aShapesId)
426 {
427   MESSAGE("SMESH_Gen::Evaluate");
428
429   bool ret = true;
430
431   SMESH_subMesh *sm = aMesh.GetSubMesh(aShape);
432
433   const bool includeSelf = true;
434   const bool complexShapeFirst = true;
435   SMESH_subMeshIteratorPtr smIt;
436
437   if ( anUpward ) { // is called from below code here
438     // -----------------------------------------------
439     // mesh all the sub-shapes starting from vertices
440     // -----------------------------------------------
441     smIt = sm->getDependsOnIterator(includeSelf, !complexShapeFirst);
442     while ( smIt->more() ) {
443       SMESH_subMesh* smToCompute = smIt->next();
444
445       // do not mesh vertices of a pseudo shape
446       const TopAbs_ShapeEnum aShType = smToCompute->GetSubShape().ShapeType();
447       //if ( !aMesh.HasShapeToMesh() && aShType == TopAbs_VERTEX )
448       //  continue;
449       if ( !aMesh.HasShapeToMesh() ) {
450         if( aShType == TopAbs_VERTEX || aShType == TopAbs_WIRE ||
451             aShType == TopAbs_SHELL )
452           continue;
453       }
454
455       smToCompute->Evaluate(aResMap);
456       if( aShapesId )
457         aShapesId->insert( smToCompute->GetId() );
458     }
459     return ret;
460   }
461   else {
462     // -----------------------------------------------------------------
463     // apply algos that DO NOT require Discreteized boundaries and DO NOT
464     // support submeshes, starting from the most complex shapes
465     // and collect submeshes with algos that DO support submeshes
466     // -----------------------------------------------------------------
467     list< SMESH_subMesh* > smWithAlgoSupportingSubmeshes;
468     smIt = sm->getDependsOnIterator(includeSelf, complexShapeFirst);
469     while ( smIt->more() ) {
470       SMESH_subMesh* smToCompute = smIt->next();
471       const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
472       const int aShapeDim = GetShapeDim( aSubShape );
473       if ( aShapeDim < 1 ) break;
474       
475       SMESH_Algo* algo = GetAlgo( aMesh, aSubShape );
476       if ( algo && !algo->NeedDiscreteBoundary() ) {
477         if ( algo->SupportSubmeshes() ) {
478           smWithAlgoSupportingSubmeshes.push_front( smToCompute );
479         }
480         else {
481           smToCompute->Evaluate(aResMap);
482           if ( aShapesId )
483             aShapesId->insert( smToCompute->GetId() );
484         }
485       }
486     }
487
488     // ------------------------------------------------------------
489     // sort list of meshes according to mesh order
490     // ------------------------------------------------------------
491     aMesh.SortByMeshOrder( smWithAlgoSupportingSubmeshes );
492
493     // ------------------------------------------------------------
494     // compute submeshes under shapes with algos that DO NOT require
495     // Discreteized boundaries and DO support submeshes
496     // ------------------------------------------------------------
497     list< SMESH_subMesh* >::iterator subIt, subEnd;
498     subIt  = smWithAlgoSupportingSubmeshes.begin();
499     subEnd = smWithAlgoSupportingSubmeshes.end();
500     // start from lower shapes
501     for ( ; subIt != subEnd; ++subIt ) {
502       sm = *subIt;
503
504       // get a shape the algo is assigned to
505       TopoDS_Shape algoShape;
506       if ( !GetAlgo( aMesh, sm->GetSubShape(), & algoShape ))
507         continue; // strange...
508
509       // look for more local algos
510       smIt = sm->getDependsOnIterator(!includeSelf, !complexShapeFirst);
511       while ( smIt->more() ) {
512         SMESH_subMesh* smToCompute = smIt->next();
513
514         const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
515         const int aShapeDim = GetShapeDim( aSubShape );
516         if ( aShapeDim < 1 ) continue;
517
518         //const TopAbs_ShapeEnum aShType = smToCompute->GetSubShape().ShapeType();
519
520         SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
521         filter
522           .And( SMESH_HypoFilter::IsApplicableTo( aSubShape ))
523           .And( SMESH_HypoFilter::IsMoreLocalThan( algoShape, aMesh ));
524
525         if ( SMESH_Algo* subAlgo = (SMESH_Algo*) aMesh.GetHypothesis( aSubShape, filter, true )) {
526           SMESH_Hypothesis::Hypothesis_Status status;
527           if ( subAlgo->CheckHypothesis( aMesh, aSubShape, status ))
528             // mesh a lower smToCompute starting from vertices
529             Evaluate( aMesh, aSubShape, aResMap, /*anUpward=*/true, aShapesId );
530         }
531       }
532     }
533     // ----------------------------------------------------------
534     // apply the algos that do not require Discreteized boundaries
535     // ----------------------------------------------------------
536     for ( subIt = smWithAlgoSupportingSubmeshes.begin(); subIt != subEnd; ++subIt )
537     {
538       sm = *subIt;
539       sm->Evaluate(aResMap);
540       if ( aShapesId )
541         aShapesId->insert( sm->GetId() );
542     }
543
544     // -----------------------------------------------
545     // mesh the rest sub-shapes starting from vertices
546     // -----------------------------------------------
547     ret = Evaluate( aMesh, aShape, aResMap, /*anUpward=*/true, aShapesId );
548   }
549
550   MESSAGE( "VSR - SMESH_Gen::Evaluate() finished, OK = " << ret);
551   return ret;
552 }
553
554
555 //=======================================================================
556 //function : checkConformIgnoredAlgos
557 //purpose  :
558 //=======================================================================
559
560 static bool checkConformIgnoredAlgos(SMESH_Mesh&               aMesh,
561                                      SMESH_subMesh*            aSubMesh,
562                                      const SMESH_Algo*         aGlobIgnoAlgo,
563                                      const SMESH_Algo*         aLocIgnoAlgo,
564                                      bool &                    checkConform,
565                                      set<SMESH_subMesh*>&      aCheckedMap,
566                                      list< SMESH_Gen::TAlgoStateError > & theErrors)
567 {
568   ASSERT( aSubMesh );
569   if ( aSubMesh->GetSubShape().ShapeType() == TopAbs_VERTEX)
570     return true;
571
572
573   bool ret = true;
574
575   const list<const SMESHDS_Hypothesis*>& listHyp =
576     aMesh.GetMeshDS()->GetHypothesis( aSubMesh->GetSubShape() );
577   list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
578   for ( ; it != listHyp.end(); it++)
579   {
580     const SMESHDS_Hypothesis * aHyp = *it;
581     if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
582       continue;
583
584     const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
585     ASSERT ( algo );
586
587     if ( aLocIgnoAlgo ) // algo is hidden by a local algo of upper dim
588     {
589       INFOS( "Local <" << algo->GetName() << "> is hidden by local <"
590             << aLocIgnoAlgo->GetName() << ">");
591     }
592     else
593     {
594       bool isGlobal = (aMesh.IsMainShape( aSubMesh->GetSubShape() ));
595       int dim = algo->GetDim();
596       int aMaxGlobIgnoDim = ( aGlobIgnoAlgo ? aGlobIgnoAlgo->GetDim() : -1 );
597
598       if ( dim < aMaxGlobIgnoDim )
599       {
600         // algo is hidden by a global algo
601         INFOS( ( isGlobal ? "Global" : "Local" )
602               << " <" << algo->GetName() << "> is hidden by global <"
603               << aGlobIgnoAlgo->GetName() << ">");
604       }
605       else if ( !algo->NeedDiscreteBoundary() && !isGlobal)
606       {
607         // local algo is not hidden and hides algos on sub-shapes
608         if (checkConform && !aSubMesh->IsConform( algo ))
609         {
610           ret = false;
611           checkConform = false; // no more check conformity
612           INFOS( "ERROR: Local <" << algo->GetName() <<
613                 "> would produce not conform mesh: "
614                 "<Not Conform Mesh Allowed> hypotesis is missing");
615           theErrors.push_back( SMESH_Gen::TAlgoStateError() );
616           theErrors.back().Set( SMESH_Hypothesis::HYP_NOTCONFORM, algo, false );
617         }
618
619         // sub-algos will be hidden by a local <algo>
620         SMESH_subMeshIteratorPtr revItSub =
621           aSubMesh->getDependsOnIterator( /*includeSelf=*/false, /*complexShapeFirst=*/true);
622         bool checkConform2 = false;
623         while ( revItSub->more() )
624         {
625           SMESH_subMesh* sm = revItSub->next();
626           checkConformIgnoredAlgos (aMesh, sm, aGlobIgnoAlgo,
627                                     algo, checkConform2, aCheckedMap, theErrors);
628           aCheckedMap.insert( sm );
629         }
630       }
631     }
632   }
633
634   return ret;
635 }
636
637 //=======================================================================
638 //function : checkMissing
639 //purpose  : notify on missing hypothesis
640 //           Return false if algo or hipothesis is missing
641 //=======================================================================
642
643 static bool checkMissing(SMESH_Gen*                aGen,
644                          SMESH_Mesh&               aMesh,
645                          SMESH_subMesh*            aSubMesh,
646                          const int                 aTopAlgoDim,
647                          bool*                     globalChecked,
648                          const bool                checkNoAlgo,
649                          set<SMESH_subMesh*>&      aCheckedMap,
650                          list< SMESH_Gen::TAlgoStateError > & theErrors)
651 {
652   if ( aSubMesh->GetSubShape().ShapeType() == TopAbs_VERTEX)
653     return true;
654
655   //MESSAGE("=====checkMissing");
656
657   int ret = true;
658   SMESH_Algo* algo = 0;
659
660   switch (aSubMesh->GetAlgoState())
661   {
662   case SMESH_subMesh::NO_ALGO: {
663     if (checkNoAlgo)
664     {
665       // should there be any algo?
666       int shapeDim = SMESH_Gen::GetShapeDim( aSubMesh->GetSubShape() );
667       if (aTopAlgoDim > shapeDim)
668       {
669         MESSAGE( "ERROR: " << shapeDim << "D algorithm is missing" );
670         ret = false;
671         theErrors.push_back( SMESH_Gen::TAlgoStateError() );
672         theErrors.back().Set( SMESH_Hypothesis::HYP_MISSING, shapeDim, true );
673       }
674     }
675     return ret;
676   }
677   case SMESH_subMesh::MISSING_HYP: {
678     // notify if an algo missing hyp is attached to aSubMesh
679     algo = aGen->GetAlgo( aMesh, aSubMesh->GetSubShape() );
680     ASSERT( algo );
681     bool IsGlobalHypothesis = aGen->IsGlobalHypothesis( algo, aMesh );
682     if (!IsGlobalHypothesis || !globalChecked[ algo->GetDim() ])
683     {
684       TAlgoStateErrorName errName = SMESH_Hypothesis::HYP_MISSING;
685       SMESH_Hypothesis::Hypothesis_Status status;
686       algo->CheckHypothesis( aMesh, aSubMesh->GetSubShape(), status );
687       if ( status == SMESH_Hypothesis::HYP_BAD_PARAMETER ) {
688         MESSAGE( "ERROR: hypothesis of " << (IsGlobalHypothesis ? "Global " : "Local ")
689                  << "<" << algo->GetName() << "> has a bad parameter value");
690         errName = status;
691       } else if ( status == SMESH_Hypothesis::HYP_BAD_GEOMETRY ) {
692         MESSAGE( "ERROR: " << (IsGlobalHypothesis ? "Global " : "Local ")
693                  << "<" << algo->GetName() << "> assigned to mismatching geometry");
694         errName = status;
695       } else {
696         MESSAGE( "ERROR: " << (IsGlobalHypothesis ? "Global " : "Local ")
697                  << "<" << algo->GetName() << "> misses some hypothesis");
698       }
699       if (IsGlobalHypothesis)
700         globalChecked[ algo->GetDim() ] = true;
701       theErrors.push_back( SMESH_Gen::TAlgoStateError() );
702       theErrors.back().Set( errName, algo, IsGlobalHypothesis );
703     }
704     ret = false;
705     break;
706   }
707   case SMESH_subMesh::HYP_OK:
708     algo = aGen->GetAlgo( aMesh, aSubMesh->GetSubShape() );
709     ret = true;
710     break;
711   default: ASSERT(0);
712   }
713
714   // do not check under algo that hides sub-algos or
715   // re-start checking NO_ALGO state
716   ASSERT (algo);
717   bool isTopLocalAlgo =
718     ( aTopAlgoDim <= algo->GetDim() && !aGen->IsGlobalHypothesis( algo, aMesh ));
719   if (!algo->NeedDiscreteBoundary() || isTopLocalAlgo)
720   {
721     bool checkNoAlgo2 = ( algo->NeedDiscreteBoundary() );
722     SMESH_subMeshIteratorPtr itsub = aSubMesh->getDependsOnIterator( /*includeSelf=*/false,
723                                                                      /*complexShapeFirst=*/false);
724     while ( itsub->more() )
725     {
726       // sub-meshes should not be checked further more
727       SMESH_subMesh* sm = itsub->next();
728       aCheckedMap.insert( sm );
729
730       if (isTopLocalAlgo)
731       {
732         //check algo on sub-meshes
733         int aTopAlgoDim2 = algo->GetDim();
734         if (!checkMissing (aGen, aMesh, sm, aTopAlgoDim2,
735                            globalChecked, checkNoAlgo2, aCheckedMap, theErrors))
736         {
737           ret = false;
738           if (sm->GetAlgoState() == SMESH_subMesh::NO_ALGO )
739             checkNoAlgo2 = false;
740         }
741       }
742     }
743   }
744   return ret;
745 }
746
747 //=======================================================================
748 //function : CheckAlgoState
749 //purpose  : notify on bad state of attached algos, return false
750 //           if Compute() would fail because of some algo bad state
751 //=======================================================================
752
753 bool SMESH_Gen::CheckAlgoState(SMESH_Mesh& aMesh, const TopoDS_Shape& aShape)
754 {
755   list< TAlgoStateError > errors;
756   return GetAlgoState( aMesh, aShape, errors );
757 }
758
759 //=======================================================================
760 //function : GetAlgoState
761 //purpose  : notify on bad state of attached algos, return false
762 //           if Compute() would fail because of some algo bad state
763 //           theErrors list contains problems description
764 //=======================================================================
765
766 bool SMESH_Gen::GetAlgoState(SMESH_Mesh&               theMesh,
767                              const TopoDS_Shape&       theShape,
768                              list< TAlgoStateError > & theErrors)
769 {
770   //MESSAGE("SMESH_Gen::CheckAlgoState");
771
772   bool ret = true;
773   bool hasAlgo = false;
774
775   SMESH_subMesh* sm = theMesh.GetSubMesh(theShape);
776   const SMESHDS_Mesh* meshDS = theMesh.GetMeshDS();
777   TopoDS_Shape mainShape = meshDS->ShapeToMesh();
778
779   // -----------------
780   // get global algos
781   // -----------------
782
783   const SMESH_Algo* aGlobAlgoArr[] = {0,0,0,0};
784
785   const list<const SMESHDS_Hypothesis*>& listHyp = meshDS->GetHypothesis( mainShape );
786   list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
787   for ( ; it != listHyp.end(); it++)
788   {
789     const SMESHDS_Hypothesis * aHyp = *it;
790     if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
791       continue;
792
793     const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
794     ASSERT ( algo );
795
796     int dim = algo->GetDim();
797     aGlobAlgoArr[ dim ] = algo;
798
799     hasAlgo = true;
800   }
801
802   // --------------------------------------------------------
803   // info on algos that will be ignored because of ones that
804   // don't NeedDiscreteBoundary() attached to super-shapes,
805   // check that a conform mesh will be produced
806   // --------------------------------------------------------
807
808
809   // find a global algo possibly hiding sub-algos
810   int dim;
811   const SMESH_Algo* aGlobIgnoAlgo = 0;
812   for (dim = 3; dim > 0; dim--)
813   {
814     if (aGlobAlgoArr[ dim ] &&
815         !aGlobAlgoArr[ dim ]->NeedDiscreteBoundary())
816     {
817       aGlobIgnoAlgo = aGlobAlgoArr[ dim ];
818       break;
819     }
820   }
821
822   set<SMESH_subMesh*> aCheckedSubs;
823   bool checkConform = ( !theMesh.IsNotConformAllowed() );
824
825   // loop on theShape and its sub-shapes
826   SMESH_subMeshIteratorPtr revItSub = sm->getDependsOnIterator( /*includeSelf=*/true,
827                                                                 /*complexShapeFirst=*/true);
828   while ( revItSub->more() )
829   {
830     SMESH_subMesh* smToCheck = revItSub->next();
831     if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
832       break;
833
834     if ( aCheckedSubs.insert( smToCheck ).second ) // not yet checked
835       if (!checkConformIgnoredAlgos (theMesh, smToCheck, aGlobIgnoAlgo,
836                                      0, checkConform, aCheckedSubs, theErrors))
837         ret = false;
838
839     if ( smToCheck->GetAlgoState() != SMESH_subMesh::NO_ALGO )
840       hasAlgo = true;
841   }
842
843   // ----------------------------------------------------------------
844   // info on missing hypothesis and find out if all needed algos are
845   // well defined
846   // ----------------------------------------------------------------
847
848   //MESSAGE( "---info on missing hypothesis and find out if all needed algos are");
849
850   // find max dim of global algo
851   int aTopAlgoDim = 0;
852   for (dim = 3; dim > 0; dim--)
853   {
854     if (aGlobAlgoArr[ dim ])
855     {
856       aTopAlgoDim = dim;
857       break;
858     }
859   }
860   bool checkNoAlgo = theMesh.HasShapeToMesh() ? bool( aTopAlgoDim ) : false;
861   bool globalChecked[] = { false, false, false, false };
862
863   // loop on theShape and its sub-shapes
864   aCheckedSubs.clear();
865   revItSub = sm->getDependsOnIterator( /*includeSelf=*/true, /*complexShapeFirst=*/true);
866   while ( revItSub->more() )
867   {
868     SMESH_subMesh* smToCheck = revItSub->next();
869     if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
870       break;
871
872     if ( aCheckedSubs.insert( smToCheck ).second ) // not yet checked
873       if (!checkMissing (this, theMesh, smToCheck, aTopAlgoDim,
874                          globalChecked, checkNoAlgo, aCheckedSubs, theErrors))
875       {
876         ret = false;
877         if (smToCheck->GetAlgoState() == SMESH_subMesh::NO_ALGO )
878           checkNoAlgo = false;
879       }
880   }
881
882   if ( !hasAlgo ) {
883     ret = false;
884     //INFOS( "None algorithm attached" );
885     theErrors.push_back( TAlgoStateError() );
886     theErrors.back().Set( SMESH_Hypothesis::HYP_MISSING, 1, true );
887   }
888
889   return ret;
890 }
891
892 //=======================================================================
893 //function : IsGlobalHypothesis
894 //purpose  : check if theAlgo is attached to the main shape
895 //=======================================================================
896
897 bool SMESH_Gen::IsGlobalHypothesis(const SMESH_Hypothesis* theHyp, SMESH_Mesh& aMesh)
898 {
899   SMESH_HypoFilter filter( SMESH_HypoFilter::Is( theHyp ));
900   return aMesh.GetHypothesis( aMesh.GetMeshDS()->ShapeToMesh(), filter, false );
901 }
902
903 //================================================================================
904 /*!
905  * \brief Return paths to xml files of plugins
906  */
907 //================================================================================
908
909 std::vector< std::string > SMESH_Gen::GetPluginXMLPaths()
910 {
911   // Get paths to xml files of plugins
912   vector< string > xmlPaths;
913   string sep;
914   if ( const char* meshersList = getenv("SMESH_MeshersList") )
915   {
916     string meshers = meshersList, plugin;
917     string::size_type from = 0, pos;
918     while ( from < meshers.size() )
919     {
920       // cut off plugin name
921       pos = meshers.find( ':', from );
922       if ( pos != string::npos )
923         plugin = meshers.substr( from, pos-from );
924       else
925         plugin = meshers.substr( from ), pos = meshers.size();
926       from = pos + 1;
927
928       // get PLUGIN_ROOT_DIR path
929       string rootDirVar, pluginSubDir = plugin;
930       if ( plugin == "StdMeshers" )
931         rootDirVar = "SMESH", pluginSubDir = "smesh";
932       else
933         for ( pos = 0; pos < plugin.size(); ++pos )
934           rootDirVar += toupper( plugin[pos] );
935       rootDirVar += "_ROOT_DIR";
936
937       const char* rootDir = getenv( rootDirVar.c_str() );
938       if ( !rootDir || strlen(rootDir) == 0 )
939       {
940         rootDirVar = plugin + "_ROOT_DIR"; // HexoticPLUGIN_ROOT_DIR
941         rootDir = getenv( rootDirVar.c_str() );
942         if ( !rootDir || strlen(rootDir) == 0 ) continue;
943       }
944
945       // get a separator from rootDir
946       for ( pos = strlen( rootDir )-1; pos >= 0 && sep.empty(); --pos )
947         if ( rootDir[pos] == '/' || rootDir[pos] == '\\' )
948         {
949           sep = rootDir[pos];
950           break;
951         }
952 #ifdef WNT
953       if (sep.empty() ) sep = "\\";
954 #else
955       if (sep.empty() ) sep = "/";
956 #endif
957
958       // get a path to resource file
959       string xmlPath = rootDir;
960       if ( xmlPath[ xmlPath.size()-1 ] != sep[0] )
961         xmlPath += sep;
962       xmlPath += "share" + sep + "salome" + sep + "resources" + sep;
963       for ( pos = 0; pos < pluginSubDir.size(); ++pos )
964         xmlPath += tolower( pluginSubDir[pos] );
965       xmlPath += sep + plugin + ".xml";
966       bool fileOK;
967 #ifdef WNT
968       fileOK = (GetFileAttributes(xmlPath.c_str()) != INVALID_FILE_ATTRIBUTES);
969 #else
970       fileOK = (access(xmlPath.c_str(), F_OK) == 0);
971 #endif
972       if ( fileOK )
973         xmlPaths.push_back( xmlPath );
974     }
975   }
976
977   return xmlPaths;
978 }
979
980 //=============================================================================
981 /*!
982  * Finds algo to mesh a shape. Optionally returns a shape the found algo is bound to
983  */
984 //=============================================================================
985
986 SMESH_Algo *SMESH_Gen::GetAlgo(SMESH_Mesh &         aMesh,
987                                const TopoDS_Shape & aShape,
988                                TopoDS_Shape*        assignedTo)
989 {
990   SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
991   filter.And( filter.IsApplicableTo( aShape ));
992
993   return (SMESH_Algo*) aMesh.GetHypothesis( aShape, filter, true, assignedTo );
994 }
995
996 //=============================================================================
997 /*!
998  * Returns StudyContextStruct for a study
999  */
1000 //=============================================================================
1001
1002 StudyContextStruct *SMESH_Gen::GetStudyContext(int studyId)
1003 {
1004   // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
1005
1006   if (_mapStudyContext.find(studyId) == _mapStudyContext.end())
1007   {
1008     _mapStudyContext[studyId] = new StudyContextStruct;
1009     _mapStudyContext[studyId]->myDocument = new SMESHDS_Document(studyId);
1010   }
1011   StudyContextStruct *myStudyContext = _mapStudyContext[studyId];
1012   return myStudyContext;
1013 }
1014
1015 //================================================================================
1016 /*!
1017  * \brief Return shape dimension by TopAbs_ShapeEnum
1018  */
1019 //================================================================================
1020
1021 int SMESH_Gen::GetShapeDim(const TopAbs_ShapeEnum & aShapeType)
1022 {
1023   static vector<int> dim;
1024   if ( dim.empty() )
1025   {
1026     dim.resize( TopAbs_SHAPE, -1 );
1027     dim[ TopAbs_COMPOUND ]  = MeshDim_3D;
1028     dim[ TopAbs_COMPSOLID ] = MeshDim_3D;
1029     dim[ TopAbs_SOLID ]     = MeshDim_3D;
1030     dim[ TopAbs_SHELL ]     = MeshDim_2D;
1031     dim[ TopAbs_FACE  ]     = MeshDim_2D;
1032     dim[ TopAbs_WIRE ]      = MeshDim_1D;
1033     dim[ TopAbs_EDGE ]      = MeshDim_1D;
1034     dim[ TopAbs_VERTEX ]    = MeshDim_0D;
1035   }
1036   return dim[ aShapeType ];
1037 }
1038
1039 //=============================================================================
1040 /*!
1041  * Genarate a new id unique withing this Gen
1042  */
1043 //=============================================================================
1044
1045 int SMESH_Gen::GetANewId()
1046 {
1047   return _hypId++;
1048 }