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
66 //=============================================================================
70 //=============================================================================
72 SMESH_Gen::SMESH_Gen()
74 _studyContext = new StudyContextStruct;
75 _studyContext->myDocument = new SMESHDS_Document();
78 _segmentation = _nbSegments = 10;
79 _compute_canceled = false;
84 // a structure used to nullify SMESH_Gen field of SMESH_Hypothesis,
85 // which is needed for SMESH_Hypothesis not deleted before ~SMESH_Gen()
86 struct _Hyp : public SMESH_Hypothesis
95 //=============================================================================
99 //=============================================================================
101 SMESH_Gen::~SMESH_Gen()
103 std::map < int, SMESH_Hypothesis * >::iterator i_hyp = _studyContext->mapHypothesis.begin();
104 for ( ; i_hyp != _studyContext->mapHypothesis.end(); ++i_hyp )
106 if ( _Hyp* h = static_cast< _Hyp*>( i_hyp->second ))
109 delete _studyContext->myDocument;
110 delete _studyContext;
113 //=============================================================================
115 * Creates a mesh in a study.
116 * if (theIsEmbeddedMode) { mesh modification commands are not logged }
118 //=============================================================================
120 SMESH_Mesh* SMESH_Gen::CreateMesh(bool theIsEmbeddedMode)
122 Unexpect aCatch(SalomeException);
124 // create a new SMESH_mesh object
125 SMESH_Mesh *aMesh = new SMESH_Mesh(_localId++,
128 _studyContext->myDocument);
129 _studyContext->mapMesh[_localId-1] = aMesh;
134 //=============================================================================
138 //=============================================================================
140 bool SMESH_Gen::Compute(SMESH_Mesh & aMesh,
141 const TopoDS_Shape & aShape,
142 const int aFlags /*= COMPACT_MESH*/,
143 const ::MeshDimension aDim /*=::MeshDim_3D*/,
144 TSetOfInt* aShapesId /*=0*/)
148 const bool aShapeOnly = aFlags & SHAPE_ONLY;
149 const bool anUpward = aFlags & UPWARD;
150 const bool aCompactMesh = aFlags & COMPACT_MESH;
154 SMESH_subMesh *sm = aMesh.GetSubMesh(aShape);
156 const bool includeSelf = true;
157 const bool complexShapeFirst = true;
158 const int globalAlgoDim = 100;
160 SMESH_subMeshIteratorPtr smIt;
162 // Fix of Issue 22150. Due to !BLSURF->OnlyUnaryInput(), BLSURF computes edges
163 // that must be computed by Projection 1D-2D while the Projection asks to compute
165 SMESH_subMesh::compute_event computeEvent =
166 aShapeOnly ? SMESH_subMesh::COMPUTE_SUBMESH : SMESH_subMesh::COMPUTE;
167 if ( !aMesh.HasShapeToMesh() )
168 computeEvent = SMESH_subMesh::COMPUTE_NOGEOM; // if several algos and no geometry
170 if ( anUpward ) // is called from the below code in this method
172 // ===============================================
173 // Mesh all the sub-shapes starting from vertices
174 // ===============================================
176 smIt = sm->getDependsOnIterator(includeSelf, !complexShapeFirst);
177 while ( smIt->more() )
179 SMESH_subMesh* smToCompute = smIt->next();
181 // do not mesh vertices of a pseudo shape
182 const TopoDS_Shape& shape = smToCompute->GetSubShape();
183 const TopAbs_ShapeEnum shapeType = shape.ShapeType();
184 if ( !aMesh.HasShapeToMesh() && shapeType == TopAbs_VERTEX )
187 // check for preview dimension limitations
188 if ( aShapesId && GetShapeDim( shapeType ) > (int)aDim )
190 // clear compute state not to show previous compute errors
191 // if preview invoked less dimension less than previous
192 smToCompute->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
196 if (smToCompute->GetComputeState() == SMESH_subMesh::READY_TO_COMPUTE)
198 if (_compute_canceled)
200 setCurrentSubMesh( smToCompute );
201 smToCompute->ComputeStateEngine( computeEvent );
202 setCurrentSubMesh( NULL );
205 // we check all the sub-meshes here and detect if any of them failed to compute
206 if (smToCompute->GetComputeState() == SMESH_subMesh::FAILED_TO_COMPUTE &&
207 ( shapeType != TopAbs_EDGE || !SMESH_Algo::isDegenerated( TopoDS::Edge( shape ))))
209 else if ( aShapesId )
210 aShapesId->insert( smToCompute->GetId() );
212 //aMesh.GetMeshDS()->Modified();
217 // ================================================================
218 // Apply algos that do NOT require discreteized boundaries
219 // ("all-dimensional") and do NOT support sub-meshes, starting from
220 // the most complex shapes and collect sub-meshes with algos that
221 // DO support sub-meshes
222 // ================================================================
224 list< SMESH_subMesh* > smWithAlgoSupportingSubmeshes[4]; // for each dim
226 // map to sort sm with same dim algos according to dim of
227 // the shape the algo assigned to (issue 0021217).
228 // Other issues influenced the algo applying order:
229 // 21406, 21556, 21893, 20206
230 multimap< int, SMESH_subMesh* > shDim2sm;
231 multimap< int, SMESH_subMesh* >::reverse_iterator shDim2smIt;
232 TopoDS_Shape algoShape;
233 int prevShapeDim = -1, aShapeDim;
235 smIt = sm->getDependsOnIterator(includeSelf, complexShapeFirst);
236 while ( smIt->more() )
238 SMESH_subMesh* smToCompute = smIt->next();
239 if ( smToCompute->GetComputeState() != SMESH_subMesh::READY_TO_COMPUTE )
242 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
243 aShapeDim = GetShapeDim( aSubShape );
244 if ( aShapeDim < 1 ) break;
246 // check for preview dimension limitations
247 if ( aShapesId && aShapeDim > (int)aDim )
250 SMESH_Algo* algo = GetAlgo( smToCompute, &algoShape );
251 if ( algo && !algo->NeedDiscreteBoundary() )
253 if ( algo->SupportSubmeshes() )
255 // reload sub-meshes from shDim2sm into smWithAlgoSupportingSubmeshes
256 // so that more local algos to go first
257 if ( prevShapeDim != aShapeDim )
259 prevShapeDim = aShapeDim;
260 for ( shDim2smIt = shDim2sm.rbegin(); shDim2smIt != shDim2sm.rend(); ++shDim2smIt )
261 if ( shDim2smIt->first == globalAlgoDim )
262 smWithAlgoSupportingSubmeshes[ aShapeDim ].push_back( shDim2smIt->second );
264 smWithAlgoSupportingSubmeshes[ aShapeDim ].push_front( shDim2smIt->second );
267 // add smToCompute to shDim2sm map
268 if ( algoShape.IsSame( aMesh.GetShapeToMesh() ))
270 aShapeDim = globalAlgoDim; // to compute last
274 aShapeDim = GetShapeDim( algoShape );
275 if ( algoShape.ShapeType() == TopAbs_COMPOUND )
277 TopoDS_Iterator it( algoShape );
278 aShapeDim += GetShapeDim( it.Value() );
281 shDim2sm.insert( make_pair( aShapeDim, smToCompute ));
283 else // Compute w/o support of sub-meshes
285 if (_compute_canceled)
287 setCurrentSubMesh( smToCompute );
288 smToCompute->ComputeStateEngine( computeEvent );
289 setCurrentSubMesh( NULL );
291 aShapesId->insert( smToCompute->GetId() );
295 // reload sub-meshes from shDim2sm into smWithAlgoSupportingSubmeshes
296 for ( shDim2smIt = shDim2sm.rbegin(); shDim2smIt != shDim2sm.rend(); ++shDim2smIt )
297 if ( shDim2smIt->first == globalAlgoDim )
298 smWithAlgoSupportingSubmeshes[3].push_back( shDim2smIt->second );
300 smWithAlgoSupportingSubmeshes[0].push_front( shDim2smIt->second );
302 // ======================================================
303 // Apply all-dimensional algorithms supporing sub-meshes
304 // ======================================================
306 std::vector< SMESH_subMesh* > smVec;
307 for ( aShapeDim = 0; aShapeDim < 4; ++aShapeDim )
309 // ------------------------------------------------
310 // sort list of sub-meshes according to mesh order
311 // ------------------------------------------------
312 smVec.assign( smWithAlgoSupportingSubmeshes[ aShapeDim ].begin(),
313 smWithAlgoSupportingSubmeshes[ aShapeDim ].end() );
314 aMesh.SortByMeshOrder( smVec );
316 // ------------------------------------------------------------
317 // compute sub-meshes with local uni-dimensional algos under
318 // sub-meshes with all-dimensional algos
319 // ------------------------------------------------------------
320 // start from lower shapes
321 for ( size_t i = 0; i < smVec.size(); ++i )
325 // get a shape the algo is assigned to
326 if ( !GetAlgo( sm, & algoShape ))
327 continue; // strange...
329 // look for more local algos
330 smIt = sm->getDependsOnIterator(!includeSelf, !complexShapeFirst);
331 while ( smIt->more() )
333 SMESH_subMesh* smToCompute = smIt->next();
335 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
336 const int aShapeDim = GetShapeDim( aSubShape );
337 //if ( aSubShape.ShapeType() == TopAbs_VERTEX ) continue;
338 if ( aShapeDim < 1 ) continue;
340 // check for preview dimension limitations
341 if ( aShapesId && GetShapeDim( aSubShape.ShapeType() ) > (int)aDim )
344 SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
346 .And( SMESH_HypoFilter::IsApplicableTo( aSubShape ))
347 .And( SMESH_HypoFilter::IsMoreLocalThan( algoShape, aMesh ));
349 if ( SMESH_Algo* subAlgo = (SMESH_Algo*) aMesh.GetHypothesis( smToCompute, filter, true))
351 if ( ! subAlgo->NeedDiscreteBoundary() ) continue;
352 SMESH_Hypothesis::Hypothesis_Status status;
353 if ( subAlgo->CheckHypothesis( aMesh, aSubShape, status ))
354 // mesh a lower smToCompute starting from vertices
355 Compute( aMesh, aSubShape, aFlags | SHAPE_ONLY_UPWARD, aDim, aShapesId );
356 // Compute( aMesh, aSubShape, aShapeOnly, /*anUpward=*/true, aDim, aShapesId );
360 // --------------------------------
361 // apply the all-dimensional algos
362 // --------------------------------
363 for ( size_t i = 0; i < smVec.size(); ++i )
366 if ( sm->GetComputeState() == SMESH_subMesh::READY_TO_COMPUTE)
368 const TopAbs_ShapeEnum shapeType = sm->GetSubShape().ShapeType();
369 // check for preview dimension limitations
370 if ( aShapesId && GetShapeDim( shapeType ) > (int)aDim )
373 if (_compute_canceled)
375 setCurrentSubMesh( sm );
376 sm->ComputeStateEngine( computeEvent );
377 setCurrentSubMesh( NULL );
379 aShapesId->insert( sm->GetId() );
382 } // loop on shape dimensions
384 // -----------------------------------------------
385 // mesh the rest sub-shapes starting from vertices
386 // -----------------------------------------------
387 ret = Compute( aMesh, aShape, aFlags | UPWARD, aDim, aShapesId );
392 // fix quadratic mesh by bending iternal links near concave boundary
393 if ( aCompactMesh && // a final compute
394 aShape.IsSame( aMesh.GetShapeToMesh() ) &&
395 !aShapesId && // not preview
396 ret ) // everything is OK
398 SMESH_MesherHelper aHelper( aMesh );
399 if ( aHelper.IsQuadraticMesh() != SMESH_MesherHelper::LINEAR )
401 aHelper.FixQuadraticElements( sm->GetComputeError() );
407 aMesh.GetMeshDS()->Modified();
408 aMesh.GetMeshDS()->CompactMesh();
413 //=============================================================================
415 * Prepare Compute a mesh
417 //=============================================================================
418 void SMESH_Gen::PrepareCompute(SMESH_Mesh & aMesh,
419 const TopoDS_Shape & aShape)
421 _compute_canceled = false;
422 resetCurrentSubMesh();
425 //=============================================================================
427 * Cancel Compute a mesh
429 //=============================================================================
430 void SMESH_Gen::CancelCompute(SMESH_Mesh & aMesh,
431 const TopoDS_Shape & aShape)
433 _compute_canceled = true;
434 if ( const SMESH_subMesh* sm = GetCurrentSubMesh() )
436 const_cast< SMESH_subMesh* >( sm )->ComputeStateEngine( SMESH_subMesh::COMPUTE_CANCELED );
438 resetCurrentSubMesh();
441 //================================================================================
443 * \brief Returns a sub-mesh being currently computed
445 //================================================================================
447 const SMESH_subMesh* SMESH_Gen::GetCurrentSubMesh() const
449 return _sm_current.empty() ? 0 : _sm_current.back();
452 //================================================================================
454 * \brief Sets a sub-mesh being currently computed.
456 * An algorithm can call Compute() for a sub-shape, hence we keep a stack of sub-meshes
458 //================================================================================
460 void SMESH_Gen::setCurrentSubMesh(SMESH_subMesh* sm)
463 _sm_current.push_back( sm );
465 else if ( !_sm_current.empty() )
466 _sm_current.pop_back();
469 void SMESH_Gen::resetCurrentSubMesh()
474 //=============================================================================
478 //=============================================================================
480 bool SMESH_Gen::Evaluate(SMESH_Mesh & aMesh,
481 const TopoDS_Shape & aShape,
482 MapShapeNbElems& aResMap,
484 TSetOfInt* aShapesId)
488 SMESH_subMesh *sm = aMesh.GetSubMesh(aShape);
490 const bool includeSelf = true;
491 const bool complexShapeFirst = true;
492 SMESH_subMeshIteratorPtr smIt;
494 if ( anUpward ) { // is called from below code here
495 // -----------------------------------------------
496 // mesh all the sub-shapes starting from vertices
497 // -----------------------------------------------
498 smIt = sm->getDependsOnIterator(includeSelf, !complexShapeFirst);
499 while ( smIt->more() ) {
500 SMESH_subMesh* smToCompute = smIt->next();
502 // do not mesh vertices of a pseudo shape
503 const TopAbs_ShapeEnum shapeType = smToCompute->GetSubShape().ShapeType();
504 //if ( !aMesh.HasShapeToMesh() && shapeType == TopAbs_VERTEX )
506 if ( !aMesh.HasShapeToMesh() ) {
507 if( shapeType == TopAbs_VERTEX || shapeType == TopAbs_WIRE ||
508 shapeType == TopAbs_SHELL )
512 smToCompute->Evaluate(aResMap);
514 aShapesId->insert( smToCompute->GetId() );
519 // -----------------------------------------------------------------
520 // apply algos that DO NOT require Discreteized boundaries and DO NOT
521 // support sub-meshes, starting from the most complex shapes
522 // and collect sub-meshes with algos that DO support sub-meshes
523 // -----------------------------------------------------------------
524 list< SMESH_subMesh* > smWithAlgoSupportingSubmeshes;
525 smIt = sm->getDependsOnIterator(includeSelf, complexShapeFirst);
526 while ( smIt->more() ) {
527 SMESH_subMesh* smToCompute = smIt->next();
528 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
529 const int aShapeDim = GetShapeDim( aSubShape );
530 if ( aShapeDim < 1 ) break;
532 SMESH_Algo* algo = GetAlgo( smToCompute );
533 if ( algo && !algo->NeedDiscreteBoundary() ) {
534 if ( algo->SupportSubmeshes() ) {
535 smWithAlgoSupportingSubmeshes.push_front( smToCompute );
538 smToCompute->Evaluate(aResMap);
540 aShapesId->insert( smToCompute->GetId() );
545 // ------------------------------------------------------------
546 // sort list of meshes according to mesh order
547 // ------------------------------------------------------------
548 std::vector< SMESH_subMesh* > smVec( smWithAlgoSupportingSubmeshes.begin(),
549 smWithAlgoSupportingSubmeshes.end() );
550 aMesh.SortByMeshOrder( smVec );
552 // ------------------------------------------------------------
553 // compute sub-meshes under shapes with algos that DO NOT require
554 // Discreteized boundaries and DO support sub-meshes
555 // ------------------------------------------------------------
556 // start from lower shapes
557 for ( size_t i = 0; i < smVec.size(); ++i )
561 // get a shape the algo is assigned to
562 TopoDS_Shape algoShape;
563 if ( !GetAlgo( sm, & algoShape ))
564 continue; // strange...
566 // look for more local algos
567 smIt = sm->getDependsOnIterator(!includeSelf, !complexShapeFirst);
568 while ( smIt->more() ) {
569 SMESH_subMesh* smToCompute = smIt->next();
571 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
572 const int aShapeDim = GetShapeDim( aSubShape );
573 if ( aShapeDim < 1 ) continue;
575 SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
577 .And( SMESH_HypoFilter::IsApplicableTo( aSubShape ))
578 .And( SMESH_HypoFilter::IsMoreLocalThan( algoShape, aMesh ));
580 if ( SMESH_Algo* subAlgo = (SMESH_Algo*) aMesh.GetHypothesis( smToCompute, filter, true ))
582 if ( ! subAlgo->NeedDiscreteBoundary() ) continue;
583 SMESH_Hypothesis::Hypothesis_Status status;
584 if ( subAlgo->CheckHypothesis( aMesh, aSubShape, status ))
585 // mesh a lower smToCompute starting from vertices
586 Evaluate( aMesh, aSubShape, aResMap, /*anUpward=*/true, aShapesId );
590 // ----------------------------------------------------------
591 // apply the algos that do not require Discreteized boundaries
592 // ----------------------------------------------------------
593 for ( size_t i = 0; i < smVec.size(); ++i )
596 sm->Evaluate(aResMap);
598 aShapesId->insert( sm->GetId() );
601 // -----------------------------------------------
602 // mesh the rest sub-shapes starting from vertices
603 // -----------------------------------------------
604 ret = Evaluate( aMesh, aShape, aResMap, /*anUpward=*/true, aShapesId );
611 //=======================================================================
612 //function : checkConformIgnoredAlgos
614 //=======================================================================
616 static bool checkConformIgnoredAlgos(SMESH_Mesh& aMesh,
617 SMESH_subMesh* aSubMesh,
618 const SMESH_Algo* aGlobIgnoAlgo,
619 const SMESH_Algo* aLocIgnoAlgo,
621 set<SMESH_subMesh*>& aCheckedMap,
622 list< SMESH_Gen::TAlgoStateError > & theErrors)
625 if ( aSubMesh->GetSubShape().ShapeType() == TopAbs_VERTEX)
631 const list<const SMESHDS_Hypothesis*>& listHyp =
632 aMesh.GetMeshDS()->GetHypothesis( aSubMesh->GetSubShape() );
633 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
634 for ( ; it != listHyp.end(); it++)
636 const SMESHDS_Hypothesis * aHyp = *it;
637 if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
640 const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
643 if ( aLocIgnoAlgo ) // algo is hidden by a local algo of upper dim
645 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
646 theErrors.back().Set( SMESH_Hypothesis::HYP_HIDDEN_ALGO, algo, false );
647 INFOS( "Local <" << algo->GetName() << "> is hidden by local <"
648 << aLocIgnoAlgo->GetName() << ">");
652 bool isGlobal = (aMesh.IsMainShape( aSubMesh->GetSubShape() ));
653 int dim = algo->GetDim();
654 int aMaxGlobIgnoDim = ( aGlobIgnoAlgo ? aGlobIgnoAlgo->GetDim() : -1 );
655 bool isNeededDim = ( aGlobIgnoAlgo ? aGlobIgnoAlgo->NeedLowerHyps( dim ) : false );
657 if (( dim < aMaxGlobIgnoDim && !isNeededDim ) &&
658 ( isGlobal || !aGlobIgnoAlgo->SupportSubmeshes() ))
660 // algo is hidden by a global algo
661 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
662 theErrors.back().Set( SMESH_Hypothesis::HYP_HIDDEN_ALGO, algo, true );
663 INFOS( ( isGlobal ? "Global" : "Local" )
664 << " <" << algo->GetName() << "> is hidden by global <"
665 << aGlobIgnoAlgo->GetName() << ">");
667 else if ( !algo->NeedDiscreteBoundary() && !isGlobal)
669 // local algo is not hidden and hides algos on sub-shapes
670 if (checkConform && !aSubMesh->IsConform( algo ))
673 checkConform = false; // no more check conformity
674 INFOS( "ERROR: Local <" << algo->GetName() <<
675 "> would produce not conform mesh: "
676 "<Not Conform Mesh Allowed> hypothesis is missing");
677 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
678 theErrors.back().Set( SMESH_Hypothesis::HYP_NOTCONFORM, algo, false );
681 // sub-algos will be hidden by a local <algo> if <algo> does not support sub-meshes
682 if ( algo->SupportSubmeshes() )
684 SMESH_subMeshIteratorPtr revItSub =
685 aSubMesh->getDependsOnIterator( /*includeSelf=*/false, /*complexShapeFirst=*/true);
686 bool checkConform2 = false;
687 while ( revItSub->more() )
689 SMESH_subMesh* sm = revItSub->next();
690 checkConformIgnoredAlgos (aMesh, sm, aGlobIgnoAlgo,
691 algo, checkConform2, aCheckedMap, theErrors);
692 aCheckedMap.insert( sm );
701 //=======================================================================
702 //function : checkMissing
703 //purpose : notify on missing hypothesis
704 // Return false if algo or hipothesis is missing
705 //=======================================================================
707 static bool checkMissing(SMESH_Gen* aGen,
709 SMESH_subMesh* aSubMesh,
710 const int aTopAlgoDim,
712 const bool checkNoAlgo,
713 set<SMESH_subMesh*>& aCheckedMap,
714 list< SMESH_Gen::TAlgoStateError > & theErrors)
716 switch ( aSubMesh->GetSubShape().ShapeType() )
720 case TopAbs_SOLID: break; // check this sub-mesh, it can be meshed
722 return true; // not meshable sub-mesh
724 if ( aCheckedMap.count( aSubMesh ))
728 SMESH_Algo* algo = 0;
730 switch (aSubMesh->GetAlgoState())
732 case SMESH_subMesh::NO_ALGO: {
735 // should there be any algo?
736 int shapeDim = SMESH_Gen::GetShapeDim( aSubMesh->GetSubShape() );
737 if (aTopAlgoDim > shapeDim)
739 MESSAGE( "ERROR: " << shapeDim << "D algorithm is missing" );
741 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
742 theErrors.back().Set( SMESH_Hypothesis::HYP_MISSING, shapeDim, true );
747 case SMESH_subMesh::MISSING_HYP: {
748 // notify if an algo missing hyp is attached to aSubMesh
749 algo = aSubMesh->GetAlgo();
751 bool IsGlobalHypothesis = aGen->IsGlobalHypothesis( algo, aMesh );
752 if (!IsGlobalHypothesis || !globalChecked[ algo->GetDim() ])
754 TAlgoStateErrorName errName = SMESH_Hypothesis::HYP_MISSING;
755 SMESH_Hypothesis::Hypothesis_Status status;
756 algo->CheckHypothesis( aMesh, aSubMesh->GetSubShape(), status );
757 if ( status == SMESH_Hypothesis::HYP_BAD_PARAMETER ) {
758 MESSAGE( "ERROR: hypothesis of " << (IsGlobalHypothesis ? "Global " : "Local ")
759 << "<" << algo->GetName() << "> has a bad parameter value");
761 } else if ( status == SMESH_Hypothesis::HYP_BAD_GEOMETRY ) {
762 MESSAGE( "ERROR: " << (IsGlobalHypothesis ? "Global " : "Local ")
763 << "<" << algo->GetName() << "> assigned to mismatching geometry");
766 MESSAGE( "ERROR: " << (IsGlobalHypothesis ? "Global " : "Local ")
767 << "<" << algo->GetName() << "> misses some hypothesis");
769 if (IsGlobalHypothesis)
770 globalChecked[ algo->GetDim() ] = true;
771 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
772 theErrors.back().Set( errName, algo, IsGlobalHypothesis );
777 case SMESH_subMesh::HYP_OK:
778 algo = aSubMesh->GetAlgo();
780 if (!algo->NeedDiscreteBoundary())
782 SMESH_subMeshIteratorPtr itsub = aSubMesh->getDependsOnIterator( /*includeSelf=*/false,
783 /*complexShapeFirst=*/false);
784 while ( itsub->more() )
785 aCheckedMap.insert( itsub->next() );
791 // do not check under algo that hides sub-algos or
792 // re-start checking NO_ALGO state
794 bool isTopLocalAlgo =
795 ( aTopAlgoDim <= algo->GetDim() && !aGen->IsGlobalHypothesis( algo, aMesh ));
796 if (!algo->NeedDiscreteBoundary() || isTopLocalAlgo)
798 bool checkNoAlgo2 = ( algo->NeedDiscreteBoundary() );
799 SMESH_subMeshIteratorPtr itsub = aSubMesh->getDependsOnIterator( /*includeSelf=*/false,
800 /*complexShapeFirst=*/true);
801 while ( itsub->more() )
803 // sub-meshes should not be checked further more
804 SMESH_subMesh* sm = itsub->next();
808 //check algo on sub-meshes
809 int aTopAlgoDim2 = algo->GetDim();
810 if (!checkMissing (aGen, aMesh, sm, aTopAlgoDim2,
811 globalChecked, checkNoAlgo2, aCheckedMap, theErrors))
814 if (sm->GetAlgoState() == SMESH_subMesh::NO_ALGO )
815 checkNoAlgo2 = false;
818 aCheckedMap.insert( sm );
824 //=======================================================================
825 //function : CheckAlgoState
826 //purpose : notify on bad state of attached algos, return false
827 // if Compute() would fail because of some algo bad state
828 //=======================================================================
830 bool SMESH_Gen::CheckAlgoState(SMESH_Mesh& aMesh, const TopoDS_Shape& aShape)
832 list< TAlgoStateError > errors;
833 return GetAlgoState( aMesh, aShape, errors );
836 //=======================================================================
837 //function : GetAlgoState
838 //purpose : notify on bad state of attached algos, return false
839 // if Compute() would fail because of some algo bad state
840 // theErrors list contains problems description
841 //=======================================================================
843 bool SMESH_Gen::GetAlgoState(SMESH_Mesh& theMesh,
844 const TopoDS_Shape& theShape,
845 list< TAlgoStateError > & theErrors)
848 bool hasAlgo = false;
850 SMESH_subMesh* sm = theMesh.GetSubMesh(theShape);
851 const SMESHDS_Mesh* meshDS = theMesh.GetMeshDS();
852 TopoDS_Shape mainShape = meshDS->ShapeToMesh();
858 const SMESH_Algo* aGlobAlgoArr[] = {0,0,0,0};
860 const list<const SMESHDS_Hypothesis*>& listHyp = meshDS->GetHypothesis( mainShape );
861 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
862 for ( ; it != listHyp.end(); it++)
864 const SMESHDS_Hypothesis * aHyp = *it;
865 if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
868 const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
871 int dim = algo->GetDim();
872 aGlobAlgoArr[ dim ] = algo;
877 // --------------------------------------------------------
878 // info on algos that will be ignored because of ones that
879 // don't NeedDiscreteBoundary() attached to super-shapes,
880 // check that a conform mesh will be produced
881 // --------------------------------------------------------
884 // find a global algo possibly hiding sub-algos
886 const SMESH_Algo* aGlobIgnoAlgo = 0;
887 for (dim = 3; dim > 0; dim--)
889 if (aGlobAlgoArr[ dim ] &&
890 !aGlobAlgoArr[ dim ]->NeedDiscreteBoundary() /*&&
891 !aGlobAlgoArr[ dim ]->SupportSubmeshes()*/ )
893 aGlobIgnoAlgo = aGlobAlgoArr[ dim ];
898 set<SMESH_subMesh*> aCheckedSubs;
899 bool checkConform = ( !theMesh.IsNotConformAllowed() );
901 // loop on theShape and its sub-shapes
902 SMESH_subMeshIteratorPtr revItSub = sm->getDependsOnIterator( /*includeSelf=*/true,
903 /*complexShapeFirst=*/true);
904 while ( revItSub->more() )
906 SMESH_subMesh* smToCheck = revItSub->next();
907 if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
910 if ( aCheckedSubs.insert( smToCheck ).second ) // not yet checked
911 if (!checkConformIgnoredAlgos (theMesh, smToCheck, aGlobIgnoAlgo,
912 0, checkConform, aCheckedSubs, theErrors))
915 if ( smToCheck->GetAlgoState() != SMESH_subMesh::NO_ALGO )
919 // ----------------------------------------------------------------
920 // info on missing hypothesis and find out if all needed algos are
922 // ----------------------------------------------------------------
924 // find max dim of global algo
926 for (dim = 3; dim > 0; dim--)
928 if (aGlobAlgoArr[ dim ])
934 bool checkNoAlgo = theMesh.HasShapeToMesh() ? bool( aTopAlgoDim ) : false;
935 bool globalChecked[] = { false, false, false, false };
937 // loop on theShape and its sub-shapes
938 aCheckedSubs.clear();
939 revItSub = sm->getDependsOnIterator( /*includeSelf=*/true, /*complexShapeFirst=*/true);
940 while ( revItSub->more() )
942 SMESH_subMesh* smToCheck = revItSub->next();
943 if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
946 if (!checkMissing (this, theMesh, smToCheck, aTopAlgoDim,
947 globalChecked, checkNoAlgo, aCheckedSubs, theErrors))
950 if (smToCheck->GetAlgoState() == SMESH_subMesh::NO_ALGO )
957 theErrors.push_back( TAlgoStateError() );
958 theErrors.back().Set( SMESH_Hypothesis::HYP_MISSING, theMesh.HasShapeToMesh() ? 1 : 3, true );
964 //=======================================================================
965 //function : IsGlobalHypothesis
966 //purpose : check if theAlgo is attached to the main shape
967 //=======================================================================
969 bool SMESH_Gen::IsGlobalHypothesis(const SMESH_Hypothesis* theHyp, SMESH_Mesh& aMesh)
971 SMESH_HypoFilter filter( SMESH_HypoFilter::Is( theHyp ));
972 return aMesh.GetHypothesis( aMesh.GetMeshDS()->ShapeToMesh(), filter, false );
975 //================================================================================
977 * \brief Return paths to xml files of plugins
979 //================================================================================
981 std::vector< std::string > SMESH_Gen::GetPluginXMLPaths()
983 // Get paths to xml files of plugins
984 vector< string > xmlPaths;
986 if ( const char* meshersList = getenv("SMESH_MeshersList") )
988 string meshers = meshersList, plugin;
989 string::size_type from = 0, pos;
990 while ( from < meshers.size() )
992 // cut off plugin name
993 pos = meshers.find( env_sep, from );
994 if ( pos != string::npos )
995 plugin = meshers.substr( from, pos-from );
997 plugin = meshers.substr( from ), pos = meshers.size();
1000 // get PLUGIN_ROOT_DIR path
1001 string rootDirVar, pluginSubDir = plugin;
1002 if ( plugin == "StdMeshers" )
1003 rootDirVar = "SMESH", pluginSubDir = "smesh";
1005 for ( pos = 0; pos < plugin.size(); ++pos )
1006 rootDirVar += toupper( plugin[pos] );
1007 rootDirVar += "_ROOT_DIR";
1009 const char* rootDir = getenv( rootDirVar.c_str() );
1010 if ( !rootDir || strlen(rootDir) == 0 )
1012 rootDirVar = plugin + "_ROOT_DIR"; // HexoticPLUGIN_ROOT_DIR
1013 rootDir = getenv( rootDirVar.c_str() );
1014 if ( !rootDir || strlen(rootDir) == 0 ) continue;
1017 // get a separator from rootDir
1018 for ( pos = strlen( rootDir )-1; pos >= 0 && sep.empty(); --pos )
1019 if ( rootDir[pos] == '/' || rootDir[pos] == '\\' )
1025 if (sep.empty() ) sep = "\\";
1027 if (sep.empty() ) sep = "/";
1030 // get a path to resource file
1031 string xmlPath = rootDir;
1032 if ( xmlPath[ xmlPath.size()-1 ] != sep[0] )
1034 xmlPath += "share" + sep + "salome" + sep + "resources" + sep;
1035 for ( pos = 0; pos < pluginSubDir.size(); ++pos )
1036 xmlPath += tolower( pluginSubDir[pos] );
1037 xmlPath += sep + plugin + ".xml";
1041 const wchar_t* path = Kernel_Utils::decode_s(xmlPath);
1043 const char* path = xmlPath.c_str();
1045 fileOK = (GetFileAttributes(path) != INVALID_FILE_ATTRIBUTES);
1050 fileOK = (access(xmlPath.c_str(), F_OK) == 0);
1053 xmlPaths.push_back( xmlPath );
1060 //=============================================================================
1062 * Finds algo to mesh a shape. Optionally returns a shape the found algo is bound to
1064 //=============================================================================
1066 SMESH_Algo *SMESH_Gen::GetAlgo(SMESH_Mesh & aMesh,
1067 const TopoDS_Shape & aShape,
1068 TopoDS_Shape* assignedTo)
1070 return GetAlgo( aMesh.GetSubMesh( aShape ), assignedTo );
1073 //=============================================================================
1075 * Finds algo to mesh a sub-mesh. Optionally returns a shape the found algo is bound to
1077 //=============================================================================
1079 SMESH_Algo *SMESH_Gen::GetAlgo(SMESH_subMesh * aSubMesh,
1080 TopoDS_Shape* assignedTo)
1082 if ( !aSubMesh ) return 0;
1084 const TopoDS_Shape & aShape = aSubMesh->GetSubShape();
1085 SMESH_Mesh& aMesh = *aSubMesh->GetFather();
1087 SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
1088 if ( aMesh.HasShapeToMesh() )
1089 filter.And( filter.IsApplicableTo( aShape ));
1091 typedef SMESH_Algo::Features AlgoData;
1093 TopoDS_Shape assignedToShape;
1095 (SMESH_Algo*) aMesh.GetHypothesis( aSubMesh, filter, true, &assignedToShape );
1098 aShape.ShapeType() == TopAbs_FACE &&
1099 !aShape.IsSame( assignedToShape ) &&
1100 SMESH_MesherHelper::NbAncestors( aShape, aMesh, TopAbs_SOLID ) > 1 )
1102 // Issue 0021559. If there is another 2D algo with different types of output
1103 // elements that can be used to mesh aShape, and 3D algos on adjacent SOLIDs
1104 // have different types of input elements, we choose a most appropriate 2D algo.
1106 // try to find a concurrent 2D algo
1107 filter.AndNot( filter.Is( algo ));
1108 TopoDS_Shape assignedToShape2;
1110 (SMESH_Algo*) aMesh.GetHypothesis( aSubMesh, filter, true, &assignedToShape2 );
1111 if ( algo2 && // algo found
1112 !assignedToShape2.IsSame( aMesh.GetShapeToMesh() ) && // algo is local
1113 ( SMESH_MesherHelper::GetGroupType( assignedToShape2 ) == // algo of the same level
1114 SMESH_MesherHelper::GetGroupType( assignedToShape )) &&
1115 aMesh.IsOrderOK( aMesh.GetSubMesh( assignedToShape2 ), // no forced order
1116 aMesh.GetSubMesh( assignedToShape )))
1118 // get algos on the adjacent SOLIDs
1119 filter.Init( filter.IsAlgo() ).And( filter.HasDim( 3 ));
1120 vector< SMESH_Algo* > algos3D;
1121 PShapeIteratorPtr solidIt = SMESH_MesherHelper::GetAncestors( aShape, aMesh,
1123 while ( const TopoDS_Shape* solid = solidIt->next() )
1124 if ( SMESH_Algo* algo3D = (SMESH_Algo*) aMesh.GetHypothesis( *solid, filter, true ))
1126 algos3D.push_back( algo3D );
1127 filter.AndNot( filter.HasName( algo3D->GetName() ));
1129 // check compatibility of algos
1130 if ( algos3D.size() > 1 )
1132 const AlgoData& algoData = algo->SMESH_Algo::GetFeatures();
1133 const AlgoData& algoData2 = algo2->SMESH_Algo::GetFeatures();
1134 const AlgoData& algoData3d0 = algos3D[0]->SMESH_Algo::GetFeatures();
1135 const AlgoData& algoData3d1 = algos3D[1]->SMESH_Algo::GetFeatures();
1136 if (( algoData2.IsCompatible( algoData3d0 ) &&
1137 algoData2.IsCompatible( algoData3d1 ))
1139 !(algoData.IsCompatible( algoData3d0 ) &&
1140 algoData.IsCompatible( algoData3d1 )))
1146 if ( assignedTo && algo )
1147 * assignedTo = assignedToShape;
1152 //=============================================================================
1154 * Returns StudyContextStruct for a study
1156 //=============================================================================
1158 StudyContextStruct *SMESH_Gen::GetStudyContext()
1160 return _studyContext;
1163 //================================================================================
1165 * \brief Return shape dimension by TopAbs_ShapeEnum
1167 //================================================================================
1169 int SMESH_Gen::GetShapeDim(const TopAbs_ShapeEnum & aShapeType)
1171 static vector<int> dim;
1174 dim.resize( TopAbs_SHAPE, -1 );
1175 dim[ TopAbs_COMPOUND ] = MeshDim_3D;
1176 dim[ TopAbs_COMPSOLID ] = MeshDim_3D;
1177 dim[ TopAbs_SOLID ] = MeshDim_3D;
1178 dim[ TopAbs_SHELL ] = MeshDim_2D;
1179 dim[ TopAbs_FACE ] = MeshDim_2D;
1180 dim[ TopAbs_WIRE ] = MeshDim_1D;
1181 dim[ TopAbs_EDGE ] = MeshDim_1D;
1182 dim[ TopAbs_VERTEX ] = MeshDim_0D;
1184 return dim[ aShapeType ];
1187 //=============================================================================
1189 * Generate a new id unique within this Gen
1191 //=============================================================================
1193 int SMESH_Gen::GetANewId()