1 // Copyright (C) 2007-2020 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // SMESH SMESH : implementation of SMESH idl descriptions
24 // File : SMESH_Gen.cxx
25 // Author : Paul RASCLE, EDF
31 #include "SMESH_Gen.hxx"
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"
42 #include "utilities.h"
44 #include "Utils_ExceptHandlers.hxx"
46 #include <TopoDS_Iterator.hxx>
55 #include <Basics_Utils.hxx>
59 // Environment variable separator
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
78 //================================================================================
80 * \brief Fill a map of shapes with all sub-shape of a sub-mesh
82 //================================================================================
84 TopTools_IndexedMapOfShape * fillAllowed( SMESH_subMesh* sm,
86 TopTools_IndexedMapOfShape * allowedSub )
88 if ( !toFill || !allowedSub )
92 if ( allowedSub->IsEmpty() )
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() );
103 //=============================================================================
107 //=============================================================================
109 SMESH_Gen::SMESH_Gen()
111 _studyContext = new StudyContextStruct;
112 _studyContext->myDocument = new SMESHDS_Document();
115 _segmentation = _nbSegments = 10;
116 _compute_canceled = false;
119 //=============================================================================
123 //=============================================================================
125 SMESH_Gen::~SMESH_Gen()
127 std::map < int, SMESH_Hypothesis * >::iterator i_hyp = _studyContext->mapHypothesis.begin();
128 for ( ; i_hyp != _studyContext->mapHypothesis.end(); ++i_hyp )
130 if ( _Hyp* h = static_cast< _Hyp*>( i_hyp->second ))
133 delete _studyContext->myDocument;
134 delete _studyContext;
137 //=============================================================================
139 * Creates a mesh in a study.
140 * if (theIsEmbeddedMode) { mesh modification commands are not logged }
142 //=============================================================================
144 SMESH_Mesh* SMESH_Gen::CreateMesh(bool theIsEmbeddedMode)
145 throw(SALOME_Exception)
147 Unexpect aCatch(SalomeException);
149 // create a new SMESH_mesh object
150 SMESH_Mesh *aMesh = new SMESH_Mesh(_localId++,
153 _studyContext->myDocument);
154 _studyContext->mapMesh[_localId-1] = aMesh;
159 //=============================================================================
163 //=============================================================================
165 bool SMESH_Gen::Compute(SMESH_Mesh & aMesh,
166 const TopoDS_Shape & aShape,
167 const int aFlags /*= COMPACT_MESH*/,
168 const ::MeshDimension aDim /*=::MeshDim_3D*/,
169 TSetOfInt* aShapesId /*=0*/,
170 TopTools_IndexedMapOfShape* anAllowedSubShapes/*=0*/)
174 const bool aShapeOnly = aFlags & SHAPE_ONLY;
175 const bool anUpward = aFlags & UPWARD;
176 const bool aCompactMesh = aFlags & COMPACT_MESH;
180 SMESH_subMesh *sm, *shapeSM = aMesh.GetSubMesh(aShape);
182 const bool includeSelf = true;
183 const bool complexShapeFirst = true;
184 const int globalAlgoDim = 100;
186 SMESH_subMeshIteratorPtr smIt;
188 // Fix of Issue 22150. Due to !BLSURF->OnlyUnaryInput(), BLSURF computes edges
189 // that must be computed by Projection 1D-2D while the Projection asks to compute
191 SMESH_subMesh::compute_event computeEvent =
192 aShapeOnly ? SMESH_subMesh::COMPUTE_SUBMESH : SMESH_subMesh::COMPUTE;
193 if ( !aMesh.HasShapeToMesh() )
194 computeEvent = SMESH_subMesh::COMPUTE_NOGEOM; // if several algos and no geometry
196 TopTools_IndexedMapOfShape *allowedSubShapes = anAllowedSubShapes, allowedSub;
197 if ( aShapeOnly && !allowedSubShapes )
198 allowedSubShapes = &allowedSub;
200 if ( anUpward ) // is called from the below code in this method
202 // ===============================================
203 // Mesh all the sub-shapes starting from vertices
204 // ===============================================
206 smIt = shapeSM->getDependsOnIterator(includeSelf, !complexShapeFirst);
207 while ( smIt->more() )
209 SMESH_subMesh* smToCompute = smIt->next();
211 // do not mesh vertices of a pseudo shape
212 const TopoDS_Shape& shape = smToCompute->GetSubShape();
213 const TopAbs_ShapeEnum shapeType = shape.ShapeType();
214 if ( !aMesh.HasShapeToMesh() && shapeType == TopAbs_VERTEX )
217 // check for preview dimension limitations
218 if ( aShapesId && GetShapeDim( shapeType ) > (int)aDim )
220 // clear compute state not to show previous compute errors
221 // if preview invoked less dimension less than previous
222 smToCompute->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
226 if (smToCompute->GetComputeState() == SMESH_subMesh::READY_TO_COMPUTE)
228 if (_compute_canceled)
230 smToCompute->SetAllowedSubShapes( fillAllowed( shapeSM, aShapeOnly, allowedSubShapes ));
231 setCurrentSubMesh( smToCompute );
232 smToCompute->ComputeStateEngine( computeEvent );
233 setCurrentSubMesh( nullptr );
234 smToCompute->SetAllowedSubShapes( nullptr );
237 // we check all the sub-meshes here and detect if any of them failed to compute
238 if (smToCompute->GetComputeState() == SMESH_subMesh::FAILED_TO_COMPUTE &&
239 ( shapeType != TopAbs_EDGE || !SMESH_Algo::isDegenerated( TopoDS::Edge( shape ))))
241 else if ( aShapesId )
242 aShapesId->insert( smToCompute->GetId() );
244 //aMesh.GetMeshDS()->Modified();
249 // ================================================================
250 // Apply algos that do NOT require discreteized boundaries
251 // ("all-dimensional") and do NOT support sub-meshes, starting from
252 // the most complex shapes and collect sub-meshes with algos that
253 // DO support sub-meshes
254 // ================================================================
256 list< SMESH_subMesh* > smWithAlgoSupportingSubmeshes[4]; // for each dim
258 // map to sort sm with same dim algos according to dim of
259 // the shape the algo assigned to (issue 0021217).
260 // Other issues influenced the algo applying order:
261 // 21406, 21556, 21893, 20206
262 multimap< int, SMESH_subMesh* > shDim2sm;
263 multimap< int, SMESH_subMesh* >::reverse_iterator shDim2smIt;
264 TopoDS_Shape algoShape;
265 int prevShapeDim = -1, aShapeDim;
267 smIt = shapeSM->getDependsOnIterator(includeSelf, complexShapeFirst);
268 while ( smIt->more() )
270 SMESH_subMesh* smToCompute = smIt->next();
271 if ( smToCompute->GetComputeState() != SMESH_subMesh::READY_TO_COMPUTE )
274 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
275 aShapeDim = GetShapeDim( aSubShape );
276 if ( aShapeDim < 1 ) break;
278 // check for preview dimension limitations
279 if ( aShapesId && aShapeDim > (int)aDim )
282 SMESH_Algo* algo = GetAlgo( smToCompute, &algoShape );
283 if ( algo && !algo->NeedDiscreteBoundary() )
285 if ( algo->SupportSubmeshes() )
287 // reload sub-meshes from shDim2sm into smWithAlgoSupportingSubmeshes
288 // so that more local algos to go first
289 if ( prevShapeDim != aShapeDim )
291 prevShapeDim = aShapeDim;
292 for ( shDim2smIt = shDim2sm.rbegin(); shDim2smIt != shDim2sm.rend(); ++shDim2smIt )
293 if ( shDim2smIt->first == globalAlgoDim )
294 smWithAlgoSupportingSubmeshes[ aShapeDim ].push_back( shDim2smIt->second );
296 smWithAlgoSupportingSubmeshes[ aShapeDim ].push_front( shDim2smIt->second );
299 // add smToCompute to shDim2sm map
300 if ( algoShape.IsSame( aMesh.GetShapeToMesh() ))
302 aShapeDim = globalAlgoDim; // to compute last
306 aShapeDim = GetShapeDim( algoShape );
307 if ( algoShape.ShapeType() == TopAbs_COMPOUND )
309 TopoDS_Iterator it( algoShape );
310 aShapeDim += GetShapeDim( it.Value() );
313 shDim2sm.insert( make_pair( aShapeDim, smToCompute ));
315 else // Compute w/o support of sub-meshes
317 if (_compute_canceled)
319 smToCompute->SetAllowedSubShapes( fillAllowed( shapeSM, aShapeOnly, allowedSubShapes ));
320 setCurrentSubMesh( smToCompute );
321 smToCompute->ComputeStateEngine( computeEvent );
322 setCurrentSubMesh( nullptr );
323 smToCompute->SetAllowedSubShapes( nullptr );
325 aShapesId->insert( smToCompute->GetId() );
329 // reload sub-meshes from shDim2sm into smWithAlgoSupportingSubmeshes
330 for ( shDim2smIt = shDim2sm.rbegin(); shDim2smIt != shDim2sm.rend(); ++shDim2smIt )
331 if ( shDim2smIt->first == globalAlgoDim )
332 smWithAlgoSupportingSubmeshes[3].push_back( shDim2smIt->second );
334 smWithAlgoSupportingSubmeshes[0].push_front( shDim2smIt->second );
336 // ======================================================
337 // Apply all-dimensional algorithms supporing sub-meshes
338 // ======================================================
340 std::vector< SMESH_subMesh* > smVec;
341 for ( aShapeDim = 0; aShapeDim < 4; ++aShapeDim )
343 // ------------------------------------------------
344 // sort list of sub-meshes according to mesh order
345 // ------------------------------------------------
346 smVec.assign( smWithAlgoSupportingSubmeshes[ aShapeDim ].begin(),
347 smWithAlgoSupportingSubmeshes[ aShapeDim ].end() );
348 aMesh.SortByMeshOrder( smVec );
350 // ------------------------------------------------------------
351 // compute sub-meshes with local uni-dimensional algos under
352 // sub-meshes with all-dimensional algos
353 // ------------------------------------------------------------
354 // start from lower shapes
355 for ( size_t i = 0; i < smVec.size(); ++i )
359 // get a shape the algo is assigned to
360 if ( !GetAlgo( sm, & algoShape ))
361 continue; // strange...
363 // look for more local algos
364 smIt = sm->getDependsOnIterator(!includeSelf, !complexShapeFirst);
365 while ( smIt->more() )
367 SMESH_subMesh* smToCompute = smIt->next();
369 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
370 const int aShapeDim = GetShapeDim( aSubShape );
371 if ( aShapeDim < 1 ) continue;
373 // check for preview dimension limitations
374 if ( aShapesId && GetShapeDim( aSubShape.ShapeType() ) > (int)aDim )
377 SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
379 .And( SMESH_HypoFilter::IsApplicableTo( aSubShape ))
380 .And( SMESH_HypoFilter::IsMoreLocalThan( algoShape, aMesh ));
382 if ( SMESH_Algo* subAlgo = (SMESH_Algo*) aMesh.GetHypothesis( smToCompute, filter, true))
384 if ( ! subAlgo->NeedDiscreteBoundary() ) continue;
385 TopTools_IndexedMapOfShape* localAllowed = allowedSubShapes;
386 if ( localAllowed && localAllowed->IsEmpty() )
387 localAllowed = 0; // prevent fillAllowed() with aSubShape
389 SMESH_Hypothesis::Hypothesis_Status status;
390 if ( subAlgo->CheckHypothesis( aMesh, aSubShape, status ))
391 // mesh a lower smToCompute starting from vertices
392 Compute( aMesh, aSubShape, aFlags | SHAPE_ONLY_UPWARD, aDim, aShapesId, localAllowed );
396 // --------------------------------
397 // apply the all-dimensional algos
398 // --------------------------------
399 for ( size_t i = 0; i < smVec.size(); ++i )
402 if ( sm->GetComputeState() == SMESH_subMesh::READY_TO_COMPUTE)
404 const TopAbs_ShapeEnum shapeType = sm->GetSubShape().ShapeType();
405 // check for preview dimension limitations
406 if ( aShapesId && GetShapeDim( shapeType ) > (int)aDim )
409 if (_compute_canceled)
411 sm->SetAllowedSubShapes( fillAllowed( shapeSM, aShapeOnly, allowedSubShapes ));
412 setCurrentSubMesh( sm );
413 sm->ComputeStateEngine( computeEvent );
414 setCurrentSubMesh( NULL );
415 sm->SetAllowedSubShapes( nullptr );
417 aShapesId->insert( sm->GetId() );
420 } // loop on shape dimensions
422 // -----------------------------------------------
423 // mesh the rest sub-shapes starting from vertices
424 // -----------------------------------------------
425 ret = Compute( aMesh, aShape, aFlags | UPWARD, aDim, aShapesId, allowedSubShapes );
430 // fix quadratic mesh by bending iternal links near concave boundary
431 if ( aCompactMesh && // a final compute
432 aShape.IsSame( aMesh.GetShapeToMesh() ) &&
433 !aShapesId && // not preview
434 ret ) // everything is OK
436 SMESH_MesherHelper aHelper( aMesh );
437 if ( aHelper.IsQuadraticMesh() != SMESH_MesherHelper::LINEAR )
439 aHelper.FixQuadraticElements( shapeSM->GetComputeError() );
445 aMesh.GetMeshDS()->Modified();
446 aMesh.GetMeshDS()->CompactMesh();
451 //=============================================================================
453 * Prepare Compute a mesh
455 //=============================================================================
456 void SMESH_Gen::PrepareCompute(SMESH_Mesh & aMesh,
457 const TopoDS_Shape & aShape)
459 _compute_canceled = false;
460 resetCurrentSubMesh();
463 //=============================================================================
465 * Cancel Compute a mesh
467 //=============================================================================
468 void SMESH_Gen::CancelCompute(SMESH_Mesh & aMesh,
469 const TopoDS_Shape & aShape)
471 _compute_canceled = true;
472 if ( const SMESH_subMesh* sm = GetCurrentSubMesh() )
474 const_cast< SMESH_subMesh* >( sm )->ComputeStateEngine( SMESH_subMesh::COMPUTE_CANCELED );
476 resetCurrentSubMesh();
479 //================================================================================
481 * \brief Returns a sub-mesh being currently computed
483 //================================================================================
485 const SMESH_subMesh* SMESH_Gen::GetCurrentSubMesh() const
487 return _sm_current.empty() ? 0 : _sm_current.back();
490 //================================================================================
492 * \brief Sets a sub-mesh being currently computed.
494 * An algorithm can call Compute() for a sub-shape, hence we keep a stack of sub-meshes
496 //================================================================================
498 void SMESH_Gen::setCurrentSubMesh(SMESH_subMesh* sm)
501 _sm_current.push_back( sm );
503 else if ( !_sm_current.empty() )
504 _sm_current.pop_back();
507 void SMESH_Gen::resetCurrentSubMesh()
512 //=============================================================================
516 //=============================================================================
518 bool SMESH_Gen::Evaluate(SMESH_Mesh & aMesh,
519 const TopoDS_Shape & aShape,
520 MapShapeNbElems& aResMap,
522 TSetOfInt* aShapesId)
526 SMESH_subMesh *sm = aMesh.GetSubMesh(aShape);
528 const bool includeSelf = true;
529 const bool complexShapeFirst = true;
530 SMESH_subMeshIteratorPtr smIt;
532 if ( anUpward ) { // is called from below code here
533 // -----------------------------------------------
534 // mesh all the sub-shapes starting from vertices
535 // -----------------------------------------------
536 smIt = sm->getDependsOnIterator(includeSelf, !complexShapeFirst);
537 while ( smIt->more() ) {
538 SMESH_subMesh* smToCompute = smIt->next();
540 // do not mesh vertices of a pseudo shape
541 const TopAbs_ShapeEnum shapeType = smToCompute->GetSubShape().ShapeType();
542 //if ( !aMesh.HasShapeToMesh() && shapeType == TopAbs_VERTEX )
544 if ( !aMesh.HasShapeToMesh() ) {
545 if( shapeType == TopAbs_VERTEX || shapeType == TopAbs_WIRE ||
546 shapeType == TopAbs_SHELL )
550 smToCompute->Evaluate(aResMap);
552 aShapesId->insert( smToCompute->GetId() );
557 // -----------------------------------------------------------------
558 // apply algos that DO NOT require Discreteized boundaries and DO NOT
559 // support sub-meshes, starting from the most complex shapes
560 // and collect sub-meshes with algos that DO support sub-meshes
561 // -----------------------------------------------------------------
562 list< SMESH_subMesh* > smWithAlgoSupportingSubmeshes;
563 smIt = sm->getDependsOnIterator(includeSelf, complexShapeFirst);
564 while ( smIt->more() ) {
565 SMESH_subMesh* smToCompute = smIt->next();
566 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
567 const int aShapeDim = GetShapeDim( aSubShape );
568 if ( aShapeDim < 1 ) break;
570 SMESH_Algo* algo = GetAlgo( smToCompute );
571 if ( algo && !algo->NeedDiscreteBoundary() ) {
572 if ( algo->SupportSubmeshes() ) {
573 smWithAlgoSupportingSubmeshes.push_front( smToCompute );
576 smToCompute->Evaluate(aResMap);
578 aShapesId->insert( smToCompute->GetId() );
583 // ------------------------------------------------------------
584 // sort list of meshes according to mesh order
585 // ------------------------------------------------------------
586 std::vector< SMESH_subMesh* > smVec( smWithAlgoSupportingSubmeshes.begin(),
587 smWithAlgoSupportingSubmeshes.end() );
588 aMesh.SortByMeshOrder( smVec );
590 // ------------------------------------------------------------
591 // compute sub-meshes under shapes with algos that DO NOT require
592 // Discreteized boundaries and DO support sub-meshes
593 // ------------------------------------------------------------
594 // start from lower shapes
595 for ( size_t i = 0; i < smVec.size(); ++i )
599 // get a shape the algo is assigned to
600 TopoDS_Shape algoShape;
601 if ( !GetAlgo( sm, & algoShape ))
602 continue; // strange...
604 // look for more local algos
605 smIt = sm->getDependsOnIterator(!includeSelf, !complexShapeFirst);
606 while ( smIt->more() ) {
607 SMESH_subMesh* smToCompute = smIt->next();
609 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
610 const int aShapeDim = GetShapeDim( aSubShape );
611 if ( aShapeDim < 1 ) continue;
613 SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
615 .And( SMESH_HypoFilter::IsApplicableTo( aSubShape ))
616 .And( SMESH_HypoFilter::IsMoreLocalThan( algoShape, aMesh ));
618 if ( SMESH_Algo* subAlgo = (SMESH_Algo*) aMesh.GetHypothesis( smToCompute, filter, true ))
620 if ( ! subAlgo->NeedDiscreteBoundary() ) continue;
621 SMESH_Hypothesis::Hypothesis_Status status;
622 if ( subAlgo->CheckHypothesis( aMesh, aSubShape, status ))
623 // mesh a lower smToCompute starting from vertices
624 Evaluate( aMesh, aSubShape, aResMap, /*anUpward=*/true, aShapesId );
628 // ----------------------------------------------------------
629 // apply the algos that do not require Discreteized boundaries
630 // ----------------------------------------------------------
631 for ( size_t i = 0; i < smVec.size(); ++i )
634 sm->Evaluate(aResMap);
636 aShapesId->insert( sm->GetId() );
639 // -----------------------------------------------
640 // mesh the rest sub-shapes starting from vertices
641 // -----------------------------------------------
642 ret = Evaluate( aMesh, aShape, aResMap, /*anUpward=*/true, aShapesId );
649 //=======================================================================
650 //function : checkConformIgnoredAlgos
652 //=======================================================================
654 static bool checkConformIgnoredAlgos(SMESH_Mesh& aMesh,
655 SMESH_subMesh* aSubMesh,
656 const SMESH_Algo* aGlobIgnoAlgo,
657 const SMESH_Algo* aLocIgnoAlgo,
659 set<SMESH_subMesh*>& aCheckedMap,
660 list< SMESH_Gen::TAlgoStateError > & theErrors)
663 if ( aSubMesh->GetSubShape().ShapeType() == TopAbs_VERTEX)
669 const list<const SMESHDS_Hypothesis*>& listHyp =
670 aMesh.GetMeshDS()->GetHypothesis( aSubMesh->GetSubShape() );
671 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
672 for ( ; it != listHyp.end(); it++)
674 const SMESHDS_Hypothesis * aHyp = *it;
675 if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
678 const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
681 if ( aLocIgnoAlgo ) // algo is hidden by a local algo of upper dim
683 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
684 theErrors.back().Set( SMESH_Hypothesis::HYP_HIDDEN_ALGO, algo, false );
685 INFOS( "Local <" << algo->GetName() << "> is hidden by local <"
686 << aLocIgnoAlgo->GetName() << ">");
690 bool isGlobal = (aMesh.IsMainShape( aSubMesh->GetSubShape() ));
691 int dim = algo->GetDim();
692 int aMaxGlobIgnoDim = ( aGlobIgnoAlgo ? aGlobIgnoAlgo->GetDim() : -1 );
693 bool isNeededDim = ( aGlobIgnoAlgo ? aGlobIgnoAlgo->NeedLowerHyps( dim ) : false );
695 if (( dim < aMaxGlobIgnoDim && !isNeededDim ) &&
696 ( isGlobal || !aGlobIgnoAlgo->SupportSubmeshes() ))
698 // algo is hidden by a global algo
699 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
700 theErrors.back().Set( SMESH_Hypothesis::HYP_HIDDEN_ALGO, algo, true );
701 INFOS( ( isGlobal ? "Global" : "Local" )
702 << " <" << algo->GetName() << "> is hidden by global <"
703 << aGlobIgnoAlgo->GetName() << ">");
705 else if ( !algo->NeedDiscreteBoundary() && !isGlobal)
707 // local algo is not hidden and hides algos on sub-shapes
708 if (checkConform && !aSubMesh->IsConform( algo ))
711 checkConform = false; // no more check conformity
712 INFOS( "ERROR: Local <" << algo->GetName() <<
713 "> would produce not conform mesh: "
714 "<Not Conform Mesh Allowed> hypothesis is missing");
715 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
716 theErrors.back().Set( SMESH_Hypothesis::HYP_NOTCONFORM, algo, false );
719 // sub-algos will be hidden by a local <algo> if <algo> does not support sub-meshes
720 if ( algo->SupportSubmeshes() )
722 SMESH_subMeshIteratorPtr revItSub =
723 aSubMesh->getDependsOnIterator( /*includeSelf=*/false, /*complexShapeFirst=*/true);
724 bool checkConform2 = false;
725 while ( revItSub->more() )
727 SMESH_subMesh* sm = revItSub->next();
728 checkConformIgnoredAlgos (aMesh, sm, aGlobIgnoAlgo,
729 algo, checkConform2, aCheckedMap, theErrors);
730 aCheckedMap.insert( sm );
739 //=======================================================================
740 //function : checkMissing
741 //purpose : notify on missing hypothesis
742 // Return false if algo or hipothesis is missing
743 //=======================================================================
745 static bool checkMissing(SMESH_Gen* aGen,
747 SMESH_subMesh* aSubMesh,
748 const int aTopAlgoDim,
750 const bool checkNoAlgo,
751 set<SMESH_subMesh*>& aCheckedMap,
752 list< SMESH_Gen::TAlgoStateError > & theErrors)
754 switch ( aSubMesh->GetSubShape().ShapeType() )
758 case TopAbs_SOLID: break; // check this sub-mesh, it can be meshed
760 return true; // not meshable sub-mesh
762 if ( aCheckedMap.count( aSubMesh ))
766 SMESH_Algo* algo = 0;
768 switch (aSubMesh->GetAlgoState())
770 case SMESH_subMesh::NO_ALGO: {
773 // should there be any algo?
774 int shapeDim = SMESH_Gen::GetShapeDim( aSubMesh->GetSubShape() );
775 if (aTopAlgoDim > shapeDim)
777 MESSAGE( "ERROR: " << shapeDim << "D algorithm is missing" );
779 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
780 theErrors.back().Set( SMESH_Hypothesis::HYP_MISSING, shapeDim, true );
785 case SMESH_subMesh::MISSING_HYP: {
786 // notify if an algo missing hyp is attached to aSubMesh
787 algo = aSubMesh->GetAlgo();
789 bool IsGlobalHypothesis = aGen->IsGlobalHypothesis( algo, aMesh );
790 if (!IsGlobalHypothesis || !globalChecked[ algo->GetDim() ])
792 TAlgoStateErrorName errName = SMESH_Hypothesis::HYP_MISSING;
793 SMESH_Hypothesis::Hypothesis_Status status;
794 algo->CheckHypothesis( aMesh, aSubMesh->GetSubShape(), status );
795 if ( status == SMESH_Hypothesis::HYP_BAD_PARAMETER ) {
796 MESSAGE( "ERROR: hypothesis of " << (IsGlobalHypothesis ? "Global " : "Local ")
797 << "<" << algo->GetName() << "> has a bad parameter value");
799 } else if ( status == SMESH_Hypothesis::HYP_BAD_GEOMETRY ) {
800 MESSAGE( "ERROR: " << (IsGlobalHypothesis ? "Global " : "Local ")
801 << "<" << algo->GetName() << "> assigned to mismatching geometry");
804 MESSAGE( "ERROR: " << (IsGlobalHypothesis ? "Global " : "Local ")
805 << "<" << algo->GetName() << "> misses some hypothesis");
807 if (IsGlobalHypothesis)
808 globalChecked[ algo->GetDim() ] = true;
809 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
810 theErrors.back().Set( errName, algo, IsGlobalHypothesis );
815 case SMESH_subMesh::HYP_OK:
816 algo = aSubMesh->GetAlgo();
818 if (!algo->NeedDiscreteBoundary())
820 SMESH_subMeshIteratorPtr itsub = aSubMesh->getDependsOnIterator( /*includeSelf=*/false,
821 /*complexShapeFirst=*/false);
822 while ( itsub->more() )
823 aCheckedMap.insert( itsub->next() );
829 // do not check under algo that hides sub-algos or
830 // re-start checking NO_ALGO state
832 bool isTopLocalAlgo =
833 ( aTopAlgoDim <= algo->GetDim() && !aGen->IsGlobalHypothesis( algo, aMesh ));
834 if (!algo->NeedDiscreteBoundary() || isTopLocalAlgo)
836 bool checkNoAlgo2 = ( algo->NeedDiscreteBoundary() );
837 SMESH_subMeshIteratorPtr itsub = aSubMesh->getDependsOnIterator( /*includeSelf=*/false,
838 /*complexShapeFirst=*/true);
839 while ( itsub->more() )
841 // sub-meshes should not be checked further more
842 SMESH_subMesh* sm = itsub->next();
846 //check algo on sub-meshes
847 int aTopAlgoDim2 = algo->GetDim();
848 if (!checkMissing (aGen, aMesh, sm, aTopAlgoDim2,
849 globalChecked, checkNoAlgo2, aCheckedMap, theErrors))
852 if (sm->GetAlgoState() == SMESH_subMesh::NO_ALGO )
853 checkNoAlgo2 = false;
856 aCheckedMap.insert( sm );
862 //=======================================================================
863 //function : CheckAlgoState
864 //purpose : notify on bad state of attached algos, return false
865 // if Compute() would fail because of some algo bad state
866 //=======================================================================
868 bool SMESH_Gen::CheckAlgoState(SMESH_Mesh& aMesh, const TopoDS_Shape& aShape)
870 list< TAlgoStateError > errors;
871 return GetAlgoState( aMesh, aShape, errors );
874 //=======================================================================
875 //function : GetAlgoState
876 //purpose : notify on bad state of attached algos, return false
877 // if Compute() would fail because of some algo bad state
878 // theErrors list contains problems description
879 //=======================================================================
881 bool SMESH_Gen::GetAlgoState(SMESH_Mesh& theMesh,
882 const TopoDS_Shape& theShape,
883 list< TAlgoStateError > & theErrors)
886 bool hasAlgo = false;
888 SMESH_subMesh* sm = theMesh.GetSubMesh(theShape);
889 const SMESHDS_Mesh* meshDS = theMesh.GetMeshDS();
890 TopoDS_Shape mainShape = meshDS->ShapeToMesh();
896 const SMESH_Algo* aGlobAlgoArr[] = {0,0,0,0};
898 const list<const SMESHDS_Hypothesis*>& listHyp = meshDS->GetHypothesis( mainShape );
899 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
900 for ( ; it != listHyp.end(); it++)
902 const SMESHDS_Hypothesis * aHyp = *it;
903 if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
906 const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
909 int dim = algo->GetDim();
910 aGlobAlgoArr[ dim ] = algo;
915 // --------------------------------------------------------
916 // info on algos that will be ignored because of ones that
917 // don't NeedDiscreteBoundary() attached to super-shapes,
918 // check that a conform mesh will be produced
919 // --------------------------------------------------------
922 // find a global algo possibly hiding sub-algos
924 const SMESH_Algo* aGlobIgnoAlgo = 0;
925 for (dim = 3; dim > 0; dim--)
927 if (aGlobAlgoArr[ dim ] &&
928 !aGlobAlgoArr[ dim ]->NeedDiscreteBoundary() /*&&
929 !aGlobAlgoArr[ dim ]->SupportSubmeshes()*/ )
931 aGlobIgnoAlgo = aGlobAlgoArr[ dim ];
936 set<SMESH_subMesh*> aCheckedSubs;
937 bool checkConform = ( !theMesh.IsNotConformAllowed() );
939 // loop on theShape and its sub-shapes
940 SMESH_subMeshIteratorPtr revItSub = sm->getDependsOnIterator( /*includeSelf=*/true,
941 /*complexShapeFirst=*/true);
942 while ( revItSub->more() )
944 SMESH_subMesh* smToCheck = revItSub->next();
945 if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
948 if ( aCheckedSubs.insert( smToCheck ).second ) // not yet checked
949 if (!checkConformIgnoredAlgos (theMesh, smToCheck, aGlobIgnoAlgo,
950 0, checkConform, aCheckedSubs, theErrors))
953 if ( smToCheck->GetAlgoState() != SMESH_subMesh::NO_ALGO )
957 // ----------------------------------------------------------------
958 // info on missing hypothesis and find out if all needed algos are
960 // ----------------------------------------------------------------
962 // find max dim of global algo
964 for (dim = 3; dim > 0; dim--)
966 if (aGlobAlgoArr[ dim ])
972 bool checkNoAlgo = theMesh.HasShapeToMesh() ? bool( aTopAlgoDim ) : false;
973 bool globalChecked[] = { false, false, false, false };
975 // loop on theShape and its sub-shapes
976 aCheckedSubs.clear();
977 revItSub = sm->getDependsOnIterator( /*includeSelf=*/true, /*complexShapeFirst=*/true);
978 while ( revItSub->more() )
980 SMESH_subMesh* smToCheck = revItSub->next();
981 if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
984 if (!checkMissing (this, theMesh, smToCheck, aTopAlgoDim,
985 globalChecked, checkNoAlgo, aCheckedSubs, theErrors))
988 if (smToCheck->GetAlgoState() == SMESH_subMesh::NO_ALGO )
995 theErrors.push_back( TAlgoStateError() );
996 theErrors.back().Set( SMESH_Hypothesis::HYP_MISSING, theMesh.HasShapeToMesh() ? 1 : 3, true );
1002 //=======================================================================
1003 //function : IsGlobalHypothesis
1004 //purpose : check if theAlgo is attached to the main shape
1005 //=======================================================================
1007 bool SMESH_Gen::IsGlobalHypothesis(const SMESH_Hypothesis* theHyp, SMESH_Mesh& aMesh)
1009 SMESH_HypoFilter filter( SMESH_HypoFilter::Is( theHyp ));
1010 return aMesh.GetHypothesis( aMesh.GetMeshDS()->ShapeToMesh(), filter, false );
1013 //================================================================================
1015 * \brief Return paths to xml files of plugins
1017 //================================================================================
1019 std::vector< std::string > SMESH_Gen::GetPluginXMLPaths()
1021 // Get paths to xml files of plugins
1022 vector< string > xmlPaths;
1024 if ( const char* meshersList = getenv("SMESH_MeshersList") )
1026 string meshers = meshersList, plugin;
1027 string::size_type from = 0, pos;
1028 while ( from < meshers.size() )
1030 // cut off plugin name
1031 pos = meshers.find( env_sep, from );
1032 if ( pos != string::npos )
1033 plugin = meshers.substr( from, pos-from );
1035 plugin = meshers.substr( from ), pos = meshers.size();
1038 // get PLUGIN_ROOT_DIR path
1039 string rootDirVar, pluginSubDir = plugin;
1040 if ( plugin == "StdMeshers" )
1041 rootDirVar = "SMESH", pluginSubDir = "smesh";
1043 for ( pos = 0; pos < plugin.size(); ++pos )
1044 rootDirVar += toupper( plugin[pos] );
1045 rootDirVar += "_ROOT_DIR";
1047 const char* rootDir = getenv( rootDirVar.c_str() );
1048 if ( !rootDir || strlen(rootDir) == 0 )
1050 rootDirVar = plugin + "_ROOT_DIR"; // HexoticPLUGIN_ROOT_DIR
1051 rootDir = getenv( rootDirVar.c_str() );
1052 if ( !rootDir || strlen(rootDir) == 0 ) continue;
1055 // get a separator from rootDir
1056 for ( pos = strlen( rootDir )-1; pos >= 0 && sep.empty(); --pos )
1057 if ( rootDir[pos] == '/' || rootDir[pos] == '\\' )
1063 if (sep.empty() ) sep = "\\";
1065 if (sep.empty() ) sep = "/";
1068 // get a path to resource file
1069 string xmlPath = rootDir;
1070 if ( xmlPath[ xmlPath.size()-1 ] != sep[0] )
1072 xmlPath += "share" + sep + "salome" + sep + "resources" + sep;
1073 for ( pos = 0; pos < pluginSubDir.size(); ++pos )
1074 xmlPath += tolower( pluginSubDir[pos] );
1075 xmlPath += sep + plugin + ".xml";
1079 const wchar_t* path = Kernel_Utils::decode_s(xmlPath);
1081 const char* path = xmlPath.c_str();
1083 fileOK = (GetFileAttributes(path) != INVALID_FILE_ATTRIBUTES);
1088 fileOK = (access(xmlPath.c_str(), F_OK) == 0);
1091 xmlPaths.push_back( xmlPath );
1098 //=============================================================================
1100 * Finds algo to mesh a shape. Optionally returns a shape the found algo is bound to
1102 //=============================================================================
1104 SMESH_Algo *SMESH_Gen::GetAlgo(SMESH_Mesh & aMesh,
1105 const TopoDS_Shape & aShape,
1106 TopoDS_Shape* assignedTo)
1108 return GetAlgo( aMesh.GetSubMesh( aShape ), assignedTo );
1111 //=============================================================================
1113 * Finds algo to mesh a sub-mesh. Optionally returns a shape the found algo is bound to
1115 //=============================================================================
1117 SMESH_Algo *SMESH_Gen::GetAlgo(SMESH_subMesh * aSubMesh,
1118 TopoDS_Shape* assignedTo)
1120 if ( !aSubMesh ) return 0;
1122 const TopoDS_Shape & aShape = aSubMesh->GetSubShape();
1123 SMESH_Mesh& aMesh = *aSubMesh->GetFather();
1125 SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
1126 if ( aMesh.HasShapeToMesh() )
1127 filter.And( filter.IsApplicableTo( aShape ));
1129 typedef SMESH_Algo::Features AlgoData;
1131 TopoDS_Shape assignedToShape;
1133 (SMESH_Algo*) aMesh.GetHypothesis( aSubMesh, filter, true, &assignedToShape );
1136 aShape.ShapeType() == TopAbs_FACE &&
1137 !aShape.IsSame( assignedToShape ) &&
1138 SMESH_MesherHelper::NbAncestors( aShape, aMesh, TopAbs_SOLID ) > 1 )
1140 // Issue 0021559. If there is another 2D algo with different types of output
1141 // elements that can be used to mesh aShape, and 3D algos on adjacent SOLIDs
1142 // have different types of input elements, we choose a most appropriate 2D algo.
1144 // try to find a concurrent 2D algo
1145 filter.AndNot( filter.Is( algo ));
1146 TopoDS_Shape assignedToShape2;
1148 (SMESH_Algo*) aMesh.GetHypothesis( aSubMesh, filter, true, &assignedToShape2 );
1149 if ( algo2 && // algo found
1150 !assignedToShape2.IsSame( aMesh.GetShapeToMesh() ) && // algo is local
1151 ( SMESH_MesherHelper::GetGroupType( assignedToShape2 ) == // algo of the same level
1152 SMESH_MesherHelper::GetGroupType( assignedToShape )) &&
1153 aMesh.IsOrderOK( aMesh.GetSubMesh( assignedToShape2 ), // no forced order
1154 aMesh.GetSubMesh( assignedToShape )))
1156 // get algos on the adjacent SOLIDs
1157 filter.Init( filter.IsAlgo() ).And( filter.HasDim( 3 ));
1158 vector< SMESH_Algo* > algos3D;
1159 PShapeIteratorPtr solidIt = SMESH_MesherHelper::GetAncestors( aShape, aMesh,
1161 while ( const TopoDS_Shape* solid = solidIt->next() )
1162 if ( SMESH_Algo* algo3D = (SMESH_Algo*) aMesh.GetHypothesis( *solid, filter, true ))
1164 algos3D.push_back( algo3D );
1165 filter.AndNot( filter.HasName( algo3D->GetName() ));
1167 // check compatibility of algos
1168 if ( algos3D.size() > 1 )
1170 const AlgoData& algoData = algo->SMESH_Algo::GetFeatures();
1171 const AlgoData& algoData2 = algo2->SMESH_Algo::GetFeatures();
1172 const AlgoData& algoData3d0 = algos3D[0]->SMESH_Algo::GetFeatures();
1173 const AlgoData& algoData3d1 = algos3D[1]->SMESH_Algo::GetFeatures();
1174 if (( algoData2.IsCompatible( algoData3d0 ) &&
1175 algoData2.IsCompatible( algoData3d1 ))
1177 !(algoData.IsCompatible( algoData3d0 ) &&
1178 algoData.IsCompatible( algoData3d1 )))
1184 if ( assignedTo && algo )
1185 * assignedTo = assignedToShape;
1190 //=============================================================================
1192 * Returns StudyContextStruct for a study
1194 //=============================================================================
1196 StudyContextStruct *SMESH_Gen::GetStudyContext()
1198 return _studyContext;
1201 //================================================================================
1203 * \brief Return shape dimension by TopAbs_ShapeEnum
1205 //================================================================================
1207 int SMESH_Gen::GetShapeDim(const TopAbs_ShapeEnum & aShapeType)
1209 static vector<int> dim;
1212 dim.resize( TopAbs_SHAPE, -1 );
1213 dim[ TopAbs_COMPOUND ] = MeshDim_3D;
1214 dim[ TopAbs_COMPSOLID ] = MeshDim_3D;
1215 dim[ TopAbs_SOLID ] = MeshDim_3D;
1216 dim[ TopAbs_SHELL ] = MeshDim_2D;
1217 dim[ TopAbs_FACE ] = MeshDim_2D;
1218 dim[ TopAbs_WIRE ] = MeshDim_1D;
1219 dim[ TopAbs_EDGE ] = MeshDim_1D;
1220 dim[ TopAbs_VERTEX ] = MeshDim_0D;
1222 return dim[ aShapeType ];
1225 //=============================================================================
1227 * Generate a new id unique within this Gen
1229 //=============================================================================
1231 int SMESH_Gen::GetANewId()