1 // Copyright (C) 2007-2021 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"
43 #include "Utils_ExceptHandlers.hxx"
45 #include <TopoDS_Iterator.hxx>
54 #include <Basics_Utils.hxx>
58 // Environment variable separator
67 // a structure used to nullify SMESH_Gen field of SMESH_Hypothesis,
68 // which is needed for SMESH_Hypothesis not deleted before ~SMESH_Gen()
69 struct _Hyp : public SMESH_Hypothesis
77 //================================================================================
79 * \brief Fill a map of shapes with all sub-shape of a sub-mesh
81 //================================================================================
83 TopTools_IndexedMapOfShape * fillAllowed( SMESH_subMesh* sm,
85 TopTools_IndexedMapOfShape * allowedSub )
87 if ( !toFill || !allowedSub )
91 if ( allowedSub->IsEmpty() )
93 allowedSub->ReSize( sm->DependsOn().size() + 1 );
94 allowedSub->Add( sm->GetSubShape() );
95 for ( const auto & key_sm : sm->DependsOn() )
96 allowedSub->Add( key_sm.second->GetSubShape() );
102 //=============================================================================
106 //=============================================================================
108 SMESH_Gen::SMESH_Gen()
110 _studyContext = new StudyContextStruct;
111 _studyContext->myDocument = new SMESHDS_Document();
114 _segmentation = _nbSegments = 10;
115 _compute_canceled = false;
118 //=============================================================================
122 //=============================================================================
124 SMESH_Gen::~SMESH_Gen()
126 std::map < int, SMESH_Hypothesis * >::iterator i_hyp = _studyContext->mapHypothesis.begin();
127 for ( ; i_hyp != _studyContext->mapHypothesis.end(); ++i_hyp )
129 if ( _Hyp* h = static_cast< _Hyp*>( i_hyp->second ))
132 delete _studyContext->myDocument;
133 delete _studyContext;
136 //=============================================================================
138 * Creates a mesh in a study.
139 * if (theIsEmbeddedMode) { mesh modification commands are not logged }
141 //=============================================================================
143 SMESH_Mesh* SMESH_Gen::CreateMesh(bool theIsEmbeddedMode)
145 Unexpect aCatch(SalomeException);
147 // create a new SMESH_mesh object
148 SMESH_Mesh *aMesh = new SMESH_Mesh(_localId++,
151 _studyContext->myDocument);
152 _studyContext->mapMesh[_localId-1] = aMesh;
157 //=============================================================================
161 //=============================================================================
163 bool SMESH_Gen::Compute(SMESH_Mesh & aMesh,
164 const TopoDS_Shape & aShape,
165 const int aFlags /*= COMPACT_MESH*/,
166 const ::MeshDimension aDim /*=::MeshDim_3D*/,
167 TSetOfInt* aShapesId /*=0*/,
168 TopTools_IndexedMapOfShape* anAllowedSubShapes/*=0*/)
172 const bool aShapeOnly = aFlags & SHAPE_ONLY;
173 const bool anUpward = aFlags & UPWARD;
174 const bool aCompactMesh = aFlags & COMPACT_MESH;
178 SMESH_subMesh *sm, *shapeSM = aMesh.GetSubMesh(aShape);
180 const bool includeSelf = true;
181 const bool complexShapeFirst = true;
182 const int globalAlgoDim = 100;
184 SMESH_subMeshIteratorPtr smIt;
186 // Fix of Issue 22150. Due to !BLSURF->OnlyUnaryInput(), BLSURF computes edges
187 // that must be computed by Projection 1D-2D while the Projection asks to compute
189 SMESH_subMesh::compute_event computeEvent =
190 aShapeOnly ? SMESH_subMesh::COMPUTE_SUBMESH : SMESH_subMesh::COMPUTE;
191 if ( !aMesh.HasShapeToMesh() )
192 computeEvent = SMESH_subMesh::COMPUTE_NOGEOM; // if several algos and no geometry
194 TopTools_IndexedMapOfShape *allowedSubShapes = anAllowedSubShapes, allowedSub;
195 if ( aShapeOnly && !allowedSubShapes )
196 allowedSubShapes = &allowedSub;
198 if ( anUpward ) // is called from the below code in this method
200 // ===============================================
201 // Mesh all the sub-shapes starting from vertices
202 // ===============================================
204 smIt = shapeSM->getDependsOnIterator(includeSelf, !complexShapeFirst);
205 while ( smIt->more() )
207 SMESH_subMesh* smToCompute = smIt->next();
209 // do not mesh vertices of a pseudo shape
210 const TopoDS_Shape& shape = smToCompute->GetSubShape();
211 const TopAbs_ShapeEnum shapeType = shape.ShapeType();
212 if ( !aMesh.HasShapeToMesh() && shapeType == TopAbs_VERTEX )
215 // check for preview dimension limitations
216 if ( aShapesId && GetShapeDim( shapeType ) > (int)aDim )
218 // clear compute state not to show previous compute errors
219 // if preview invoked less dimension less than previous
220 smToCompute->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
224 if (smToCompute->GetComputeState() == SMESH_subMesh::READY_TO_COMPUTE)
226 if (_compute_canceled)
228 smToCompute->SetAllowedSubShapes( fillAllowed( shapeSM, aShapeOnly, allowedSubShapes ));
229 setCurrentSubMesh( smToCompute );
230 smToCompute->ComputeStateEngine( computeEvent );
231 setCurrentSubMesh( nullptr );
232 smToCompute->SetAllowedSubShapes( nullptr );
235 // we check all the sub-meshes here and detect if any of them failed to compute
236 if (smToCompute->GetComputeState() == SMESH_subMesh::FAILED_TO_COMPUTE &&
237 ( shapeType != TopAbs_EDGE || !SMESH_Algo::isDegenerated( TopoDS::Edge( shape ))))
239 else if ( aShapesId )
240 aShapesId->insert( smToCompute->GetId() );
242 //aMesh.GetMeshDS()->Modified();
247 // ================================================================
248 // Apply algos that do NOT require discreteized boundaries
249 // ("all-dimensional") and do NOT support sub-meshes, starting from
250 // the most complex shapes and collect sub-meshes with algos that
251 // DO support sub-meshes
252 // ================================================================
254 list< SMESH_subMesh* > smWithAlgoSupportingSubmeshes[4]; // for each dim
256 // map to sort sm with same dim algos according to dim of
257 // the shape the algo assigned to (issue 0021217).
258 // Other issues influenced the algo applying order:
259 // 21406, 21556, 21893, 20206
260 multimap< int, SMESH_subMesh* > shDim2sm;
261 multimap< int, SMESH_subMesh* >::reverse_iterator shDim2smIt;
262 TopoDS_Shape algoShape;
263 int prevShapeDim = -1, aShapeDim;
265 smIt = shapeSM->getDependsOnIterator(includeSelf, complexShapeFirst);
266 while ( smIt->more() )
268 SMESH_subMesh* smToCompute = smIt->next();
269 if ( smToCompute->GetComputeState() != SMESH_subMesh::READY_TO_COMPUTE )
272 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
273 aShapeDim = GetShapeDim( aSubShape );
274 if ( aShapeDim < 1 ) break;
276 // check for preview dimension limitations
277 if ( aShapesId && aShapeDim > (int)aDim )
280 SMESH_Algo* algo = GetAlgo( smToCompute, &algoShape );
281 if ( algo && !algo->NeedDiscreteBoundary() )
283 if ( algo->SupportSubmeshes() )
285 // reload sub-meshes from shDim2sm into smWithAlgoSupportingSubmeshes
286 // so that more local algos to go first
287 if ( prevShapeDim != aShapeDim )
289 prevShapeDim = aShapeDim;
290 for ( shDim2smIt = shDim2sm.rbegin(); shDim2smIt != shDim2sm.rend(); ++shDim2smIt )
291 if ( shDim2smIt->first == globalAlgoDim )
292 smWithAlgoSupportingSubmeshes[ aShapeDim ].push_back( shDim2smIt->second );
294 smWithAlgoSupportingSubmeshes[ aShapeDim ].push_front( shDim2smIt->second );
297 // add smToCompute to shDim2sm map
298 if ( algoShape.IsSame( aMesh.GetShapeToMesh() ))
300 aShapeDim = globalAlgoDim; // to compute last
304 aShapeDim = GetShapeDim( algoShape );
305 if ( algoShape.ShapeType() == TopAbs_COMPOUND )
307 TopoDS_Iterator it( algoShape );
308 aShapeDim += GetShapeDim( it.Value() );
311 shDim2sm.insert( make_pair( aShapeDim, smToCompute ));
313 else // Compute w/o support of sub-meshes
315 if (_compute_canceled)
317 smToCompute->SetAllowedSubShapes( fillAllowed( shapeSM, aShapeOnly, allowedSubShapes ));
318 setCurrentSubMesh( smToCompute );
319 smToCompute->ComputeStateEngine( computeEvent );
320 setCurrentSubMesh( nullptr );
321 smToCompute->SetAllowedSubShapes( nullptr );
323 aShapesId->insert( smToCompute->GetId() );
327 // reload sub-meshes from shDim2sm into smWithAlgoSupportingSubmeshes
328 for ( shDim2smIt = shDim2sm.rbegin(); shDim2smIt != shDim2sm.rend(); ++shDim2smIt )
329 if ( shDim2smIt->first == globalAlgoDim )
330 smWithAlgoSupportingSubmeshes[3].push_back( shDim2smIt->second );
332 smWithAlgoSupportingSubmeshes[0].push_front( shDim2smIt->second );
334 // ======================================================
335 // Apply all-dimensional algorithms supporing sub-meshes
336 // ======================================================
338 std::vector< SMESH_subMesh* > smVec;
339 for ( aShapeDim = 0; aShapeDim < 4; ++aShapeDim )
340 smVec.insert( smVec.end(),
341 smWithAlgoSupportingSubmeshes[aShapeDim].begin(),
342 smWithAlgoSupportingSubmeshes[aShapeDim].end() );
344 // ------------------------------------------------
345 // sort list of sub-meshes according to mesh order
346 // ------------------------------------------------
347 aMesh.SortByMeshOrder( smVec );
349 // ------------------------------------------------------------
350 // compute sub-meshes with local uni-dimensional algos under
351 // sub-meshes with all-dimensional algos
352 // ------------------------------------------------------------
353 // start from lower shapes
354 for ( size_t i = 0; i < smVec.size(); ++i )
357 if ( sm->GetComputeState() != SMESH_subMesh::READY_TO_COMPUTE)
360 const TopAbs_ShapeEnum shapeType = sm->GetSubShape().ShapeType();
362 // get a shape the algo is assigned to
363 if ( !GetAlgo( sm, & algoShape ))
364 continue; // strange...
366 // look for more local algos
367 if ( SMESH_subMesh* algoSM = aMesh.GetSubMesh( algoShape ))
368 smIt = algoSM->getDependsOnIterator(!includeSelf, !complexShapeFirst);
370 smIt = sm->getDependsOnIterator(!includeSelf, !complexShapeFirst);
372 while ( smIt->more() )
374 SMESH_subMesh* smToCompute = smIt->next();
376 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
377 const int aShapeDim = GetShapeDim( aSubShape );
378 if ( aShapeDim < 1 || aSubShape.ShapeType() <= shapeType )
381 // check for preview dimension limitations
382 if ( aShapesId && GetShapeDim( aSubShape.ShapeType() ) > (int)aDim )
385 SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
387 .And( SMESH_HypoFilter::IsApplicableTo( aSubShape ))
388 .And( SMESH_HypoFilter::IsMoreLocalThan( algoShape, aMesh ));
390 if ( SMESH_Algo* subAlgo = (SMESH_Algo*) aMesh.GetHypothesis( smToCompute, filter, true))
392 if ( ! subAlgo->NeedDiscreteBoundary() ) continue;
393 TopTools_IndexedMapOfShape* localAllowed = allowedSubShapes;
394 if ( localAllowed && localAllowed->IsEmpty() )
395 localAllowed = 0; // prevent fillAllowed() with aSubShape
397 SMESH_Hypothesis::Hypothesis_Status status;
398 if ( subAlgo->CheckHypothesis( aMesh, aSubShape, status ))
399 // mesh a lower smToCompute starting from vertices
400 Compute( aMesh, aSubShape, aFlags | SHAPE_ONLY_UPWARD, aDim, aShapesId, localAllowed );
403 // --------------------------------
404 // apply the all-dimensional algo
405 // --------------------------------
407 if (_compute_canceled)
409 // check for preview dimension limitations
410 if ( aShapesId && GetShapeDim( shapeType ) > (int)aDim )
412 sm->SetAllowedSubShapes( fillAllowed( shapeSM, aShapeOnly, allowedSubShapes ));
413 setCurrentSubMesh( sm );
414 sm->ComputeStateEngine( computeEvent );
415 setCurrentSubMesh( NULL );
416 sm->SetAllowedSubShapes( nullptr );
418 aShapesId->insert( sm->GetId() );
423 // -----------------------------------------------
424 // mesh the rest sub-shapes starting from vertices
425 // -----------------------------------------------
426 ret = Compute( aMesh, aShape, aFlags | UPWARD, aDim, aShapesId, allowedSubShapes );
431 // fix quadratic mesh by bending iternal links near concave boundary
432 if ( aCompactMesh && // a final compute
433 aShape.IsSame( aMesh.GetShapeToMesh() ) &&
434 !aShapesId && // not preview
435 ret ) // everything is OK
437 SMESH_MesherHelper aHelper( aMesh );
438 if ( aHelper.IsQuadraticMesh() != SMESH_MesherHelper::LINEAR )
440 aHelper.FixQuadraticElements( shapeSM->GetComputeError() );
446 aMesh.GetMeshDS()->Modified();
447 aMesh.GetMeshDS()->CompactMesh();
452 //=============================================================================
454 * Prepare Compute a mesh
456 //=============================================================================
457 void SMESH_Gen::PrepareCompute(SMESH_Mesh & /*aMesh*/,
458 const TopoDS_Shape & /*aShape*/)
460 _compute_canceled = false;
461 resetCurrentSubMesh();
464 //=============================================================================
466 * Cancel Compute a mesh
468 //=============================================================================
469 void SMESH_Gen::CancelCompute(SMESH_Mesh & /*aMesh*/,
470 const TopoDS_Shape & /*aShape*/)
472 _compute_canceled = true;
473 if ( const SMESH_subMesh* sm = GetCurrentSubMesh() )
475 const_cast< SMESH_subMesh* >( sm )->ComputeStateEngine( SMESH_subMesh::COMPUTE_CANCELED );
477 resetCurrentSubMesh();
480 //================================================================================
482 * \brief Returns a sub-mesh being currently computed
484 //================================================================================
486 const SMESH_subMesh* SMESH_Gen::GetCurrentSubMesh() const
488 return _sm_current.empty() ? 0 : _sm_current.back();
491 //================================================================================
493 * \brief Sets a sub-mesh being currently computed.
495 * An algorithm can call Compute() for a sub-shape, hence we keep a stack of sub-meshes
497 //================================================================================
499 void SMESH_Gen::setCurrentSubMesh(SMESH_subMesh* sm)
502 _sm_current.push_back( sm );
504 else if ( !_sm_current.empty() )
505 _sm_current.pop_back();
508 void SMESH_Gen::resetCurrentSubMesh()
513 //=============================================================================
517 //=============================================================================
519 bool SMESH_Gen::Evaluate(SMESH_Mesh & aMesh,
520 const TopoDS_Shape & aShape,
521 MapShapeNbElems& aResMap,
523 TSetOfInt* aShapesId)
527 SMESH_subMesh *sm = aMesh.GetSubMesh(aShape);
529 const bool includeSelf = true;
530 const bool complexShapeFirst = true;
531 SMESH_subMeshIteratorPtr smIt;
533 if ( anUpward ) { // is called from below code here
534 // -----------------------------------------------
535 // mesh all the sub-shapes starting from vertices
536 // -----------------------------------------------
537 smIt = sm->getDependsOnIterator(includeSelf, !complexShapeFirst);
538 while ( smIt->more() ) {
539 SMESH_subMesh* smToCompute = smIt->next();
541 // do not mesh vertices of a pseudo shape
542 const TopAbs_ShapeEnum shapeType = smToCompute->GetSubShape().ShapeType();
543 //if ( !aMesh.HasShapeToMesh() && shapeType == TopAbs_VERTEX )
545 if ( !aMesh.HasShapeToMesh() ) {
546 if( shapeType == TopAbs_VERTEX || shapeType == TopAbs_WIRE ||
547 shapeType == TopAbs_SHELL )
551 smToCompute->Evaluate(aResMap);
553 aShapesId->insert( smToCompute->GetId() );
558 // -----------------------------------------------------------------
559 // apply algos that DO NOT require Discreteized boundaries and DO NOT
560 // support sub-meshes, starting from the most complex shapes
561 // and collect sub-meshes with algos that DO support sub-meshes
562 // -----------------------------------------------------------------
563 list< SMESH_subMesh* > smWithAlgoSupportingSubmeshes;
564 smIt = sm->getDependsOnIterator(includeSelf, complexShapeFirst);
565 while ( smIt->more() ) {
566 SMESH_subMesh* smToCompute = smIt->next();
567 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
568 const int aShapeDim = GetShapeDim( aSubShape );
569 if ( aShapeDim < 1 ) break;
571 SMESH_Algo* algo = GetAlgo( smToCompute );
572 if ( algo && !algo->NeedDiscreteBoundary() ) {
573 if ( algo->SupportSubmeshes() ) {
574 smWithAlgoSupportingSubmeshes.push_front( smToCompute );
577 smToCompute->Evaluate(aResMap);
579 aShapesId->insert( smToCompute->GetId() );
584 // ------------------------------------------------------------
585 // sort list of meshes according to mesh order
586 // ------------------------------------------------------------
587 std::vector< SMESH_subMesh* > smVec( smWithAlgoSupportingSubmeshes.begin(),
588 smWithAlgoSupportingSubmeshes.end() );
589 aMesh.SortByMeshOrder( smVec );
591 // ------------------------------------------------------------
592 // compute sub-meshes under shapes with algos that DO NOT require
593 // Discreteized boundaries and DO support sub-meshes
594 // ------------------------------------------------------------
595 // start from lower shapes
596 for ( size_t i = 0; i < smVec.size(); ++i )
600 // get a shape the algo is assigned to
601 TopoDS_Shape algoShape;
602 if ( !GetAlgo( sm, & algoShape ))
603 continue; // strange...
605 // look for more local algos
606 smIt = sm->getDependsOnIterator(!includeSelf, !complexShapeFirst);
607 while ( smIt->more() ) {
608 SMESH_subMesh* smToCompute = smIt->next();
610 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
611 const int aShapeDim = GetShapeDim( aSubShape );
612 if ( aShapeDim < 1 ) continue;
614 SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
616 .And( SMESH_HypoFilter::IsApplicableTo( aSubShape ))
617 .And( SMESH_HypoFilter::IsMoreLocalThan( algoShape, aMesh ));
619 if ( SMESH_Algo* subAlgo = (SMESH_Algo*) aMesh.GetHypothesis( smToCompute, filter, true ))
621 if ( ! subAlgo->NeedDiscreteBoundary() ) continue;
622 SMESH_Hypothesis::Hypothesis_Status status;
623 if ( subAlgo->CheckHypothesis( aMesh, aSubShape, status ))
624 // mesh a lower smToCompute starting from vertices
625 Evaluate( aMesh, aSubShape, aResMap, /*anUpward=*/true, aShapesId );
629 // ----------------------------------------------------------
630 // apply the algos that do not require Discreteized boundaries
631 // ----------------------------------------------------------
632 for ( size_t i = 0; i < smVec.size(); ++i )
635 sm->Evaluate(aResMap);
637 aShapesId->insert( sm->GetId() );
640 // -----------------------------------------------
641 // mesh the rest sub-shapes starting from vertices
642 // -----------------------------------------------
643 ret = Evaluate( aMesh, aShape, aResMap, /*anUpward=*/true, aShapesId );
650 //=======================================================================
651 //function : checkConformIgnoredAlgos
653 //=======================================================================
655 static bool checkConformIgnoredAlgos(SMESH_Mesh& aMesh,
656 SMESH_subMesh* aSubMesh,
657 const SMESH_Algo* aGlobIgnoAlgo,
658 const SMESH_Algo* aLocIgnoAlgo,
660 set<SMESH_subMesh*>& aCheckedMap,
661 list< SMESH_Gen::TAlgoStateError > & theErrors)
664 if ( aSubMesh->GetSubShape().ShapeType() == TopAbs_VERTEX)
670 const list<const SMESHDS_Hypothesis*>& listHyp =
671 aMesh.GetMeshDS()->GetHypothesis( aSubMesh->GetSubShape() );
672 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
673 for ( ; it != listHyp.end(); it++)
675 const SMESHDS_Hypothesis * aHyp = *it;
676 if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
679 const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
682 if ( aLocIgnoAlgo ) // algo is hidden by a local algo of upper dim
684 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
685 theErrors.back().Set( SMESH_Hypothesis::HYP_HIDDEN_ALGO, algo, false );
686 INFOS( "Local <" << algo->GetName() << "> is hidden by local <"
687 << aLocIgnoAlgo->GetName() << ">");
691 bool isGlobal = (aMesh.IsMainShape( aSubMesh->GetSubShape() ));
692 int dim = algo->GetDim();
693 int aMaxGlobIgnoDim = ( aGlobIgnoAlgo ? aGlobIgnoAlgo->GetDim() : -1 );
694 bool isNeededDim = ( aGlobIgnoAlgo ? aGlobIgnoAlgo->NeedLowerHyps( dim ) : false );
696 if (( dim < aMaxGlobIgnoDim && !isNeededDim ) &&
697 ( isGlobal || !aGlobIgnoAlgo->SupportSubmeshes() ))
699 // algo is hidden by a global algo
700 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
701 theErrors.back().Set( SMESH_Hypothesis::HYP_HIDDEN_ALGO, algo, true );
702 INFOS( ( isGlobal ? "Global" : "Local" )
703 << " <" << algo->GetName() << "> is hidden by global <"
704 << aGlobIgnoAlgo->GetName() << ">");
706 else if ( !algo->NeedDiscreteBoundary() && !isGlobal)
708 // local algo is not hidden and hides algos on sub-shapes
709 if (checkConform && !aSubMesh->IsConform( algo ))
712 checkConform = false; // no more check conformity
713 INFOS( "ERROR: Local <" << algo->GetName() <<
714 "> would produce not conform mesh: "
715 "<Not Conform Mesh Allowed> hypothesis is missing");
716 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
717 theErrors.back().Set( SMESH_Hypothesis::HYP_NOTCONFORM, algo, false );
720 // sub-algos will be hidden by a local <algo> if <algo> does not support sub-meshes
721 if ( algo->SupportSubmeshes() )
723 SMESH_subMeshIteratorPtr revItSub =
724 aSubMesh->getDependsOnIterator( /*includeSelf=*/false, /*complexShapeFirst=*/true);
725 bool checkConform2 = false;
726 while ( revItSub->more() )
728 SMESH_subMesh* sm = revItSub->next();
729 checkConformIgnoredAlgos (aMesh, sm, aGlobIgnoAlgo,
730 algo, checkConform2, aCheckedMap, theErrors);
731 aCheckedMap.insert( sm );
740 //=======================================================================
741 //function : checkMissing
742 //purpose : notify on missing hypothesis
743 // Return false if algo or hipothesis is missing
744 //=======================================================================
746 static bool checkMissing(SMESH_Gen* aGen,
748 SMESH_subMesh* aSubMesh,
749 const int aTopAlgoDim,
751 const bool checkNoAlgo,
752 set<SMESH_subMesh*>& aCheckedMap,
753 list< SMESH_Gen::TAlgoStateError > & theErrors)
755 switch ( aSubMesh->GetSubShape().ShapeType() )
759 case TopAbs_SOLID: break; // check this sub-mesh, it can be meshed
761 return true; // not meshable sub-mesh
763 if ( aCheckedMap.count( aSubMesh ))
767 SMESH_Algo* algo = 0;
769 switch (aSubMesh->GetAlgoState())
771 case SMESH_subMesh::NO_ALGO: {
774 // should there be any algo?
775 int shapeDim = SMESH_Gen::GetShapeDim( aSubMesh->GetSubShape() );
776 if (aTopAlgoDim > shapeDim)
778 MESSAGE( "ERROR: " << shapeDim << "D algorithm is missing" );
780 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
781 theErrors.back().Set( SMESH_Hypothesis::HYP_MISSING, shapeDim, true );
786 case SMESH_subMesh::MISSING_HYP: {
787 // notify if an algo missing hyp is attached to aSubMesh
788 algo = aSubMesh->GetAlgo();
790 bool IsGlobalHypothesis = aGen->IsGlobalHypothesis( algo, aMesh );
791 if (!IsGlobalHypothesis || !globalChecked[ algo->GetDim() ])
793 TAlgoStateErrorName errName = SMESH_Hypothesis::HYP_MISSING;
794 SMESH_Hypothesis::Hypothesis_Status status;
795 algo->CheckHypothesis( aMesh, aSubMesh->GetSubShape(), status );
796 if ( status == SMESH_Hypothesis::HYP_BAD_PARAMETER ) {
797 MESSAGE( "ERROR: hypothesis of " << (IsGlobalHypothesis ? "Global " : "Local ")
798 << "<" << algo->GetName() << "> has a bad parameter value");
800 } else if ( status == SMESH_Hypothesis::HYP_BAD_GEOMETRY ) {
801 MESSAGE( "ERROR: " << (IsGlobalHypothesis ? "Global " : "Local ")
802 << "<" << algo->GetName() << "> assigned to mismatching geometry");
805 MESSAGE( "ERROR: " << (IsGlobalHypothesis ? "Global " : "Local ")
806 << "<" << algo->GetName() << "> misses some hypothesis");
808 if (IsGlobalHypothesis)
809 globalChecked[ algo->GetDim() ] = true;
810 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
811 theErrors.back().Set( errName, algo, IsGlobalHypothesis );
816 case SMESH_subMesh::HYP_OK:
817 algo = aSubMesh->GetAlgo();
819 if (!algo->NeedDiscreteBoundary())
821 SMESH_subMeshIteratorPtr itsub = aSubMesh->getDependsOnIterator( /*includeSelf=*/false,
822 /*complexShapeFirst=*/false);
823 while ( itsub->more() )
824 aCheckedMap.insert( itsub->next() );
830 // do not check under algo that hides sub-algos or
831 // re-start checking NO_ALGO state
833 bool isTopLocalAlgo =
834 ( aTopAlgoDim <= algo->GetDim() && !aGen->IsGlobalHypothesis( algo, aMesh ));
835 if (!algo->NeedDiscreteBoundary() || isTopLocalAlgo)
837 bool checkNoAlgo2 = ( algo->NeedDiscreteBoundary() );
838 SMESH_subMeshIteratorPtr itsub = aSubMesh->getDependsOnIterator( /*includeSelf=*/false,
839 /*complexShapeFirst=*/true);
840 while ( itsub->more() )
842 // sub-meshes should not be checked further more
843 SMESH_subMesh* sm = itsub->next();
847 //check algo on sub-meshes
848 int aTopAlgoDim2 = algo->GetDim();
849 if (!checkMissing (aGen, aMesh, sm, aTopAlgoDim2,
850 globalChecked, checkNoAlgo2, aCheckedMap, theErrors))
853 if (sm->GetAlgoState() == SMESH_subMesh::NO_ALGO )
854 checkNoAlgo2 = false;
857 aCheckedMap.insert( sm );
863 //=======================================================================
864 //function : CheckAlgoState
865 //purpose : notify on bad state of attached algos, return false
866 // if Compute() would fail because of some algo bad state
867 //=======================================================================
869 bool SMESH_Gen::CheckAlgoState(SMESH_Mesh& aMesh, const TopoDS_Shape& aShape)
871 list< TAlgoStateError > errors;
872 return GetAlgoState( aMesh, aShape, errors );
875 //=======================================================================
876 //function : GetAlgoState
877 //purpose : notify on bad state of attached algos, return false
878 // if Compute() would fail because of some algo bad state
879 // theErrors list contains problems description
880 //=======================================================================
882 bool SMESH_Gen::GetAlgoState(SMESH_Mesh& theMesh,
883 const TopoDS_Shape& theShape,
884 list< TAlgoStateError > & theErrors)
887 bool hasAlgo = false;
889 SMESH_subMesh* sm = theMesh.GetSubMesh(theShape);
890 const SMESHDS_Mesh* meshDS = theMesh.GetMeshDS();
891 TopoDS_Shape mainShape = meshDS->ShapeToMesh();
897 const SMESH_Algo* aGlobAlgoArr[] = {0,0,0,0};
899 const list<const SMESHDS_Hypothesis*>& listHyp = meshDS->GetHypothesis( mainShape );
900 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
901 for ( ; it != listHyp.end(); it++)
903 const SMESHDS_Hypothesis * aHyp = *it;
904 if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
907 const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
910 int dim = algo->GetDim();
911 aGlobAlgoArr[ dim ] = algo;
916 // --------------------------------------------------------
917 // info on algos that will be ignored because of ones that
918 // don't NeedDiscreteBoundary() attached to super-shapes,
919 // check that a conform mesh will be produced
920 // --------------------------------------------------------
923 // find a global algo possibly hiding sub-algos
925 const SMESH_Algo* aGlobIgnoAlgo = 0;
926 for (dim = 3; dim > 0; dim--)
928 if (aGlobAlgoArr[ dim ] &&
929 !aGlobAlgoArr[ dim ]->NeedDiscreteBoundary() /*&&
930 !aGlobAlgoArr[ dim ]->SupportSubmeshes()*/ )
932 aGlobIgnoAlgo = aGlobAlgoArr[ dim ];
937 set<SMESH_subMesh*> aCheckedSubs;
938 bool checkConform = ( !theMesh.IsNotConformAllowed() );
940 // loop on theShape and its sub-shapes
941 SMESH_subMeshIteratorPtr revItSub = sm->getDependsOnIterator( /*includeSelf=*/true,
942 /*complexShapeFirst=*/true);
943 while ( revItSub->more() )
945 SMESH_subMesh* smToCheck = revItSub->next();
946 if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
949 if ( aCheckedSubs.insert( smToCheck ).second ) // not yet checked
950 if (!checkConformIgnoredAlgos (theMesh, smToCheck, aGlobIgnoAlgo,
951 0, checkConform, aCheckedSubs, theErrors))
954 if ( smToCheck->GetAlgoState() != SMESH_subMesh::NO_ALGO )
958 // ----------------------------------------------------------------
959 // info on missing hypothesis and find out if all needed algos are
961 // ----------------------------------------------------------------
963 // find max dim of global algo
965 for (dim = 3; dim > 0; dim--)
967 if (aGlobAlgoArr[ dim ])
973 bool checkNoAlgo = theMesh.HasShapeToMesh() ? bool( aTopAlgoDim ) : false;
974 bool globalChecked[] = { false, false, false, false };
976 // loop on theShape and its sub-shapes
977 aCheckedSubs.clear();
978 revItSub = sm->getDependsOnIterator( /*includeSelf=*/true, /*complexShapeFirst=*/true);
979 while ( revItSub->more() )
981 SMESH_subMesh* smToCheck = revItSub->next();
982 if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
985 if (!checkMissing (this, theMesh, smToCheck, aTopAlgoDim,
986 globalChecked, checkNoAlgo, aCheckedSubs, theErrors))
989 if (smToCheck->GetAlgoState() == SMESH_subMesh::NO_ALGO )
996 theErrors.push_back( TAlgoStateError() );
997 theErrors.back().Set( SMESH_Hypothesis::HYP_MISSING, theMesh.HasShapeToMesh() ? 1 : 3, true );
1003 //=======================================================================
1004 //function : IsGlobalHypothesis
1005 //purpose : check if theAlgo is attached to the main shape
1006 //=======================================================================
1008 bool SMESH_Gen::IsGlobalHypothesis(const SMESH_Hypothesis* theHyp, SMESH_Mesh& aMesh)
1010 SMESH_HypoFilter filter( SMESH_HypoFilter::Is( theHyp ));
1011 return aMesh.GetHypothesis( aMesh.GetMeshDS()->ShapeToMesh(), filter, false );
1014 //================================================================================
1016 * \brief Return paths to xml files of plugins
1018 //================================================================================
1020 std::vector< std::string > SMESH_Gen::GetPluginXMLPaths()
1022 // Get paths to xml files of plugins
1023 vector< string > xmlPaths;
1025 if ( const char* meshersList = getenv("SMESH_MeshersList") )
1027 string meshers = meshersList, plugin;
1028 string::size_type from = 0, pos;
1029 while ( from < meshers.size() )
1031 // cut off plugin name
1032 pos = meshers.find( env_sep, from );
1033 if ( pos != string::npos )
1034 plugin = meshers.substr( from, pos-from );
1036 plugin = meshers.substr( from ), pos = meshers.size();
1039 // get PLUGIN_ROOT_DIR path
1040 string rootDirVar, pluginSubDir = plugin;
1041 if ( plugin == "StdMeshers" )
1042 rootDirVar = "SMESH", pluginSubDir = "smesh";
1044 for ( pos = 0; pos < plugin.size(); ++pos )
1045 rootDirVar += toupper( plugin[pos] );
1046 rootDirVar += "_ROOT_DIR";
1048 const char* rootDir = getenv( rootDirVar.c_str() );
1049 if ( !rootDir || strlen(rootDir) == 0 )
1051 rootDirVar = plugin + "_ROOT_DIR"; // HexoticPLUGIN_ROOT_DIR
1052 rootDir = getenv( rootDirVar.c_str() );
1053 if ( !rootDir || strlen(rootDir) == 0 ) continue;
1056 // get a separator from rootDir
1057 for ( int i = strlen( rootDir )-1; i >= 0 && sep.empty(); --i )
1058 if ( rootDir[i] == '/' || rootDir[i] == '\\' )
1064 if (sep.empty() ) sep = "\\";
1066 if (sep.empty() ) sep = "/";
1069 // get a path to resource file
1070 string xmlPath = rootDir;
1071 if ( xmlPath[ xmlPath.size()-1 ] != sep[0] )
1073 xmlPath += "share" + sep + "salome" + sep + "resources" + sep;
1074 for ( pos = 0; pos < pluginSubDir.size(); ++pos )
1075 xmlPath += tolower( pluginSubDir[pos] );
1076 xmlPath += sep + plugin + ".xml";
1080 const wchar_t* path = Kernel_Utils::decode_s(xmlPath);
1081 SMESHUtils::ArrayDeleter<const wchar_t> deleter( path );
1083 const char* path = xmlPath.c_str();
1085 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()