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 <TopExp_Explorer.hxx>
47 #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)
146 Unexpect aCatch(SalomeException);
148 // create a new SMESH_mesh object
149 SMESH_Mesh *aMesh = new SMESH_Mesh(_localId++,
152 _studyContext->myDocument);
153 _studyContext->mapMesh[_localId-1] = aMesh;
158 //=============================================================================
162 //=============================================================================
164 bool SMESH_Gen::Compute(SMESH_Mesh & aMesh,
165 const TopoDS_Shape & aShape,
166 const int aFlags /*= COMPACT_MESH*/,
167 const ::MeshDimension aDim /*=::MeshDim_3D*/,
168 TSetOfInt* aShapesId /*=0*/,
169 TopTools_IndexedMapOfShape* anAllowedSubShapes/*=0*/)
173 const bool aShapeOnly = aFlags & SHAPE_ONLY;
174 const bool anUpward = aFlags & UPWARD;
175 const bool aCompactMesh = aFlags & COMPACT_MESH;
179 SMESH_subMesh *sm, *shapeSM = aMesh.GetSubMesh(aShape);
181 const bool includeSelf = true;
182 const bool complexShapeFirst = true;
183 const int globalAlgoDim = 100;
185 SMESH_subMeshIteratorPtr smIt;
187 // Fix of Issue 22150. Due to !BLSURF->OnlyUnaryInput(), BLSURF computes edges
188 // that must be computed by Projection 1D-2D while the Projection asks to compute
190 SMESH_subMesh::compute_event computeEvent =
191 aShapeOnly ? SMESH_subMesh::COMPUTE_SUBMESH : SMESH_subMesh::COMPUTE;
192 if ( !aMesh.HasShapeToMesh() )
193 computeEvent = SMESH_subMesh::COMPUTE_NOGEOM; // if several algos and no geometry
195 TopTools_IndexedMapOfShape *allowedSubShapes = anAllowedSubShapes, allowedSub;
196 if ( aShapeOnly && !allowedSubShapes )
197 allowedSubShapes = &allowedSub;
199 if ( anUpward ) // is called from the below code in this method
201 // ===============================================
202 // Mesh all the sub-shapes starting from vertices
203 // ===============================================
205 smIt = shapeSM->getDependsOnIterator(includeSelf, !complexShapeFirst);
206 while ( smIt->more() )
208 SMESH_subMesh* smToCompute = smIt->next();
210 // do not mesh vertices of a pseudo shape
211 const TopoDS_Shape& shape = smToCompute->GetSubShape();
212 const TopAbs_ShapeEnum shapeType = shape.ShapeType();
213 if ( !aMesh.HasShapeToMesh() && shapeType == TopAbs_VERTEX )
216 // check for preview dimension limitations
217 if ( aShapesId && GetShapeDim( shapeType ) > (int)aDim )
219 // clear compute state not to show previous compute errors
220 // if preview invoked less dimension less than previous
221 smToCompute->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
225 if (smToCompute->GetComputeState() == SMESH_subMesh::READY_TO_COMPUTE)
227 if (_compute_canceled)
229 smToCompute->SetAllowedSubShapes( fillAllowed( shapeSM, aShapeOnly, allowedSubShapes ));
230 setCurrentSubMesh( smToCompute );
231 smToCompute->ComputeStateEngine( computeEvent );
232 setCurrentSubMesh( nullptr );
233 smToCompute->SetAllowedSubShapes( nullptr );
236 // we check all the sub-meshes here and detect if any of them failed to compute
237 if (smToCompute->GetComputeState() == SMESH_subMesh::FAILED_TO_COMPUTE &&
238 ( shapeType != TopAbs_EDGE || !SMESH_Algo::isDegenerated( TopoDS::Edge( shape ))))
240 else if ( aShapesId )
241 aShapesId->insert( smToCompute->GetId() );
243 //aMesh.GetMeshDS()->Modified();
248 // ================================================================
249 // Apply algos that do NOT require discreteized boundaries
250 // ("all-dimensional") and do NOT support sub-meshes, starting from
251 // the most complex shapes and collect sub-meshes with algos that
252 // DO support sub-meshes
253 // ================================================================
255 list< SMESH_subMesh* > smWithAlgoSupportingSubmeshes[4]; // for each dim
257 // map to sort sm with same dim algos according to dim of
258 // the shape the algo assigned to (issue 0021217).
259 // Other issues influenced the algo applying order:
260 // 21406, 21556, 21893, 20206
261 multimap< int, SMESH_subMesh* > shDim2sm;
262 multimap< int, SMESH_subMesh* >::reverse_iterator shDim2smIt;
263 TopoDS_Shape algoShape;
264 int prevShapeDim = -1, aShapeDim;
266 smIt = shapeSM->getDependsOnIterator(includeSelf, complexShapeFirst);
267 while ( smIt->more() )
269 SMESH_subMesh* smToCompute = smIt->next();
270 if ( smToCompute->GetComputeState() != SMESH_subMesh::READY_TO_COMPUTE )
273 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
274 aShapeDim = GetShapeDim( aSubShape );
275 if ( aShapeDim < 1 ) break;
277 // check for preview dimension limitations
278 if ( aShapesId && aShapeDim > (int)aDim )
281 SMESH_Algo* algo = GetAlgo( smToCompute, &algoShape );
282 if ( algo && !algo->NeedDiscreteBoundary() )
284 if ( algo->SupportSubmeshes() )
286 // reload sub-meshes from shDim2sm into smWithAlgoSupportingSubmeshes
287 // so that more local algos to go first
288 if ( prevShapeDim != aShapeDim )
290 prevShapeDim = aShapeDim;
291 for ( shDim2smIt = shDim2sm.rbegin(); shDim2smIt != shDim2sm.rend(); ++shDim2smIt )
292 if ( shDim2smIt->first == globalAlgoDim )
293 smWithAlgoSupportingSubmeshes[ aShapeDim ].push_back( shDim2smIt->second );
295 smWithAlgoSupportingSubmeshes[ aShapeDim ].push_front( shDim2smIt->second );
298 // add smToCompute to shDim2sm map
299 if ( algoShape.IsSame( aMesh.GetShapeToMesh() ))
301 aShapeDim = globalAlgoDim; // to compute last
305 aShapeDim = GetShapeDim( algoShape );
306 if ( algoShape.ShapeType() == TopAbs_COMPOUND )
308 TopoDS_Iterator it( algoShape );
309 aShapeDim += GetShapeDim( it.Value() );
312 shDim2sm.insert( make_pair( aShapeDim, smToCompute ));
314 else // Compute w/o support of sub-meshes
316 if (_compute_canceled)
318 smToCompute->SetAllowedSubShapes( fillAllowed( shapeSM, aShapeOnly, allowedSubShapes ));
319 setCurrentSubMesh( smToCompute );
320 smToCompute->ComputeStateEngine( computeEvent );
321 setCurrentSubMesh( nullptr );
322 smToCompute->SetAllowedSubShapes( nullptr );
324 aShapesId->insert( smToCompute->GetId() );
328 // reload sub-meshes from shDim2sm into smWithAlgoSupportingSubmeshes
329 for ( shDim2smIt = shDim2sm.rbegin(); shDim2smIt != shDim2sm.rend(); ++shDim2smIt )
330 if ( shDim2smIt->first == globalAlgoDim )
331 smWithAlgoSupportingSubmeshes[3].push_back( shDim2smIt->second );
333 smWithAlgoSupportingSubmeshes[0].push_front( shDim2smIt->second );
335 // ======================================================
336 // Apply all-dimensional algorithms supporing sub-meshes
337 // ======================================================
339 std::vector< SMESH_subMesh* > smVec;
340 for ( aShapeDim = 0; aShapeDim < 4; ++aShapeDim )
341 smVec.insert( smVec.end(),
342 smWithAlgoSupportingSubmeshes[aShapeDim].begin(),
343 smWithAlgoSupportingSubmeshes[aShapeDim].end() );
345 // gather sub-shapes with local uni-dimensional algos (bos #29143)
346 // ----------------------------------------------------------------
347 TopTools_MapOfShape uniDimAlgoShapes;
348 if ( !smVec.empty() )
350 ShapeToHypothesis::Iterator s2hyps( aMesh.GetMeshDS()->GetHypotheses() );
351 for ( ; s2hyps.More(); s2hyps.Next() )
353 const TopoDS_Shape& s = s2hyps.Key();
354 if ( s.IsSame( aMesh.GetShapeToMesh() ))
356 for ( auto & hyp : s2hyps.Value() )
358 if ( const SMESH_Algo* algo = dynamic_cast< const SMESH_Algo*>( hyp ))
359 if ( algo->NeedDiscreteBoundary() )
361 TopAbs_ShapeEnum sType;
362 switch ( algo->GetDim() ) {
363 case 3: sType = TopAbs_SOLID; break;
364 case 2: sType = TopAbs_FACE; break;
365 default: sType = TopAbs_EDGE; break;
367 for ( TopExp_Explorer ex( s2hyps.Key(), sType ); ex.More(); ex.Next() )
368 uniDimAlgoShapes.Add( ex.Current() );
375 // ------------------------------------------------
376 // sort list of sub-meshes according to mesh order
377 // ------------------------------------------------
378 aMesh.SortByMeshOrder( smVec );
380 // ------------------------------------------------------------
381 // compute sub-meshes with local uni-dimensional algos under
382 // sub-meshes with all-dimensional algos
383 // ------------------------------------------------------------
384 // start from lower shapes
385 for ( size_t i = 0; i < smVec.size(); ++i )
388 if ( sm->GetComputeState() != SMESH_subMesh::READY_TO_COMPUTE)
391 const TopAbs_ShapeEnum shapeType = sm->GetSubShape().ShapeType();
393 // get a shape the algo is assigned to
394 if ( !GetAlgo( sm, & algoShape ))
395 continue; // strange...
397 // look for more local algos
398 if ( SMESH_subMesh* algoSM = aMesh.GetSubMesh( algoShape ))
399 smIt = algoSM->getDependsOnIterator(!includeSelf, !complexShapeFirst);
401 smIt = sm->getDependsOnIterator(!includeSelf, !complexShapeFirst);
403 while ( smIt->more() )
405 SMESH_subMesh* smToCompute = smIt->next();
407 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
408 const int aShapeDim = GetShapeDim( aSubShape );
409 if ( aShapeDim < 1 || aSubShape.ShapeType() <= shapeType )
411 if ( !uniDimAlgoShapes.Contains( aSubShape ))
412 continue; // [bos #29143] aMesh.GetHypothesis() is too long
414 // check for preview dimension limitations
415 if ( aShapesId && GetShapeDim( aSubShape.ShapeType() ) > (int)aDim )
418 SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
420 .And( SMESH_HypoFilter::IsApplicableTo( aSubShape ))
421 .And( SMESH_HypoFilter::IsMoreLocalThan( algoShape, aMesh ));
423 if ( SMESH_Algo* subAlgo = (SMESH_Algo*) aMesh.GetHypothesis( smToCompute, filter, true))
425 if ( ! subAlgo->NeedDiscreteBoundary() ) continue;
426 TopTools_IndexedMapOfShape* localAllowed = allowedSubShapes;
427 if ( localAllowed && localAllowed->IsEmpty() )
428 localAllowed = 0; // prevent fillAllowed() with aSubShape
430 SMESH_Hypothesis::Hypothesis_Status status;
431 if ( subAlgo->CheckHypothesis( aMesh, aSubShape, status ))
432 // mesh a lower smToCompute starting from vertices
433 Compute( aMesh, aSubShape, aFlags | SHAPE_ONLY_UPWARD, aDim, aShapesId, localAllowed );
436 // --------------------------------
437 // apply the all-dimensional algo
438 // --------------------------------
440 if (_compute_canceled)
442 // check for preview dimension limitations
443 if ( aShapesId && GetShapeDim( shapeType ) > (int)aDim )
445 sm->SetAllowedSubShapes( fillAllowed( shapeSM, aShapeOnly, allowedSubShapes ));
446 setCurrentSubMesh( sm );
447 sm->ComputeStateEngine( computeEvent );
448 setCurrentSubMesh( NULL );
449 sm->SetAllowedSubShapes( nullptr );
451 aShapesId->insert( sm->GetId() );
456 // -----------------------------------------------
457 // mesh the rest sub-shapes starting from vertices
458 // -----------------------------------------------
459 ret = Compute( aMesh, aShape, aFlags | UPWARD, aDim, aShapesId, allowedSubShapes );
464 // fix quadratic mesh by bending iternal links near concave boundary
465 if ( aCompactMesh && // a final compute
466 aShape.IsSame( aMesh.GetShapeToMesh() ) &&
467 !aShapesId && // not preview
468 ret ) // everything is OK
470 SMESH_MesherHelper aHelper( aMesh );
471 if ( aHelper.IsQuadraticMesh() != SMESH_MesherHelper::LINEAR )
473 aHelper.FixQuadraticElements( shapeSM->GetComputeError() );
479 aMesh.GetMeshDS()->Modified();
480 aMesh.GetMeshDS()->CompactMesh();
485 //=============================================================================
487 * Prepare Compute a mesh
489 //=============================================================================
490 void SMESH_Gen::PrepareCompute(SMESH_Mesh & /*aMesh*/,
491 const TopoDS_Shape & /*aShape*/)
493 _compute_canceled = false;
494 resetCurrentSubMesh();
497 //=============================================================================
499 * Cancel Compute a mesh
501 //=============================================================================
502 void SMESH_Gen::CancelCompute(SMESH_Mesh & /*aMesh*/,
503 const TopoDS_Shape & /*aShape*/)
505 _compute_canceled = true;
506 if ( const SMESH_subMesh* sm = GetCurrentSubMesh() )
508 const_cast< SMESH_subMesh* >( sm )->ComputeStateEngine( SMESH_subMesh::COMPUTE_CANCELED );
510 resetCurrentSubMesh();
513 //================================================================================
515 * \brief Returns a sub-mesh being currently computed
517 //================================================================================
519 const SMESH_subMesh* SMESH_Gen::GetCurrentSubMesh() const
521 return _sm_current.empty() ? 0 : _sm_current.back();
524 //================================================================================
526 * \brief Sets a sub-mesh being currently computed.
528 * An algorithm can call Compute() for a sub-shape, hence we keep a stack of sub-meshes
530 //================================================================================
532 void SMESH_Gen::setCurrentSubMesh(SMESH_subMesh* sm)
535 _sm_current.push_back( sm );
537 else if ( !_sm_current.empty() )
538 _sm_current.pop_back();
541 void SMESH_Gen::resetCurrentSubMesh()
546 //=============================================================================
550 //=============================================================================
552 bool SMESH_Gen::Evaluate(SMESH_Mesh & aMesh,
553 const TopoDS_Shape & aShape,
554 MapShapeNbElems& aResMap,
556 TSetOfInt* aShapesId)
560 SMESH_subMesh *sm = aMesh.GetSubMesh(aShape);
562 const bool includeSelf = true;
563 const bool complexShapeFirst = true;
564 SMESH_subMeshIteratorPtr smIt;
566 if ( anUpward ) { // is called from below code here
567 // -----------------------------------------------
568 // mesh all the sub-shapes starting from vertices
569 // -----------------------------------------------
570 smIt = sm->getDependsOnIterator(includeSelf, !complexShapeFirst);
571 while ( smIt->more() ) {
572 SMESH_subMesh* smToCompute = smIt->next();
574 // do not mesh vertices of a pseudo shape
575 const TopAbs_ShapeEnum shapeType = smToCompute->GetSubShape().ShapeType();
576 //if ( !aMesh.HasShapeToMesh() && shapeType == TopAbs_VERTEX )
578 if ( !aMesh.HasShapeToMesh() ) {
579 if( shapeType == TopAbs_VERTEX || shapeType == TopAbs_WIRE ||
580 shapeType == TopAbs_SHELL )
584 smToCompute->Evaluate(aResMap);
586 aShapesId->insert( smToCompute->GetId() );
591 // -----------------------------------------------------------------
592 // apply algos that DO NOT require Discreteized boundaries and DO NOT
593 // support sub-meshes, starting from the most complex shapes
594 // and collect sub-meshes with algos that DO support sub-meshes
595 // -----------------------------------------------------------------
596 list< SMESH_subMesh* > smWithAlgoSupportingSubmeshes;
597 smIt = sm->getDependsOnIterator(includeSelf, complexShapeFirst);
598 while ( smIt->more() ) {
599 SMESH_subMesh* smToCompute = smIt->next();
600 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
601 const int aShapeDim = GetShapeDim( aSubShape );
602 if ( aShapeDim < 1 ) break;
604 SMESH_Algo* algo = GetAlgo( smToCompute );
605 if ( algo && !algo->NeedDiscreteBoundary() ) {
606 if ( algo->SupportSubmeshes() ) {
607 smWithAlgoSupportingSubmeshes.push_front( smToCompute );
610 smToCompute->Evaluate(aResMap);
612 aShapesId->insert( smToCompute->GetId() );
617 // ------------------------------------------------------------
618 // sort list of meshes according to mesh order
619 // ------------------------------------------------------------
620 std::vector< SMESH_subMesh* > smVec( smWithAlgoSupportingSubmeshes.begin(),
621 smWithAlgoSupportingSubmeshes.end() );
622 aMesh.SortByMeshOrder( smVec );
624 // ------------------------------------------------------------
625 // compute sub-meshes under shapes with algos that DO NOT require
626 // Discreteized boundaries and DO support sub-meshes
627 // ------------------------------------------------------------
628 // start from lower shapes
629 for ( size_t i = 0; i < smVec.size(); ++i )
633 // get a shape the algo is assigned to
634 TopoDS_Shape algoShape;
635 if ( !GetAlgo( sm, & algoShape ))
636 continue; // strange...
638 // look for more local algos
639 smIt = sm->getDependsOnIterator(!includeSelf, !complexShapeFirst);
640 while ( smIt->more() ) {
641 SMESH_subMesh* smToCompute = smIt->next();
643 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
644 const int aShapeDim = GetShapeDim( aSubShape );
645 if ( aShapeDim < 1 ) continue;
647 SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
649 .And( SMESH_HypoFilter::IsApplicableTo( aSubShape ))
650 .And( SMESH_HypoFilter::IsMoreLocalThan( algoShape, aMesh ));
652 if ( SMESH_Algo* subAlgo = (SMESH_Algo*) aMesh.GetHypothesis( smToCompute, filter, true ))
654 if ( ! subAlgo->NeedDiscreteBoundary() ) continue;
655 SMESH_Hypothesis::Hypothesis_Status status;
656 if ( subAlgo->CheckHypothesis( aMesh, aSubShape, status ))
657 // mesh a lower smToCompute starting from vertices
658 Evaluate( aMesh, aSubShape, aResMap, /*anUpward=*/true, aShapesId );
662 // ----------------------------------------------------------
663 // apply the algos that do not require Discreteized boundaries
664 // ----------------------------------------------------------
665 for ( size_t i = 0; i < smVec.size(); ++i )
668 sm->Evaluate(aResMap);
670 aShapesId->insert( sm->GetId() );
673 // -----------------------------------------------
674 // mesh the rest sub-shapes starting from vertices
675 // -----------------------------------------------
676 ret = Evaluate( aMesh, aShape, aResMap, /*anUpward=*/true, aShapesId );
683 //=======================================================================
684 //function : checkConformIgnoredAlgos
686 //=======================================================================
688 static bool checkConformIgnoredAlgos(SMESH_Mesh& aMesh,
689 SMESH_subMesh* aSubMesh,
690 const SMESH_Algo* aGlobIgnoAlgo,
691 const SMESH_Algo* aLocIgnoAlgo,
693 set<SMESH_subMesh*>& aCheckedMap,
694 list< SMESH_Gen::TAlgoStateError > & theErrors)
697 if ( aSubMesh->GetSubShape().ShapeType() == TopAbs_VERTEX)
703 const list<const SMESHDS_Hypothesis*>& listHyp =
704 aMesh.GetMeshDS()->GetHypothesis( aSubMesh->GetSubShape() );
705 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
706 for ( ; it != listHyp.end(); it++)
708 const SMESHDS_Hypothesis * aHyp = *it;
709 if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
712 const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
715 if ( aLocIgnoAlgo ) // algo is hidden by a local algo of upper dim
717 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
718 theErrors.back().Set( SMESH_Hypothesis::HYP_HIDDEN_ALGO, algo, false );
719 INFOS( "Local <" << algo->GetName() << "> is hidden by local <"
720 << aLocIgnoAlgo->GetName() << ">");
724 bool isGlobal = (aMesh.IsMainShape( aSubMesh->GetSubShape() ));
725 int dim = algo->GetDim();
726 int aMaxGlobIgnoDim = ( aGlobIgnoAlgo ? aGlobIgnoAlgo->GetDim() : -1 );
727 bool isNeededDim = ( aGlobIgnoAlgo ? aGlobIgnoAlgo->NeedLowerHyps( dim ) : false );
729 if (( dim < aMaxGlobIgnoDim && !isNeededDim ) &&
730 ( isGlobal || !aGlobIgnoAlgo->SupportSubmeshes() ))
732 // algo is hidden by a global algo
733 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
734 theErrors.back().Set( SMESH_Hypothesis::HYP_HIDDEN_ALGO, algo, true );
735 INFOS( ( isGlobal ? "Global" : "Local" )
736 << " <" << algo->GetName() << "> is hidden by global <"
737 << aGlobIgnoAlgo->GetName() << ">");
739 else if ( !algo->NeedDiscreteBoundary() && !isGlobal)
741 // local algo is not hidden and hides algos on sub-shapes
742 if (checkConform && !aSubMesh->IsConform( algo ))
745 checkConform = false; // no more check conformity
746 INFOS( "ERROR: Local <" << algo->GetName() <<
747 "> would produce not conform mesh: "
748 "<Not Conform Mesh Allowed> hypothesis is missing");
749 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
750 theErrors.back().Set( SMESH_Hypothesis::HYP_NOTCONFORM, algo, false );
753 // sub-algos will be hidden by a local <algo> if <algo> does not support sub-meshes
754 if ( algo->SupportSubmeshes() )
756 SMESH_subMeshIteratorPtr revItSub =
757 aSubMesh->getDependsOnIterator( /*includeSelf=*/false, /*complexShapeFirst=*/true);
758 bool checkConform2 = false;
759 while ( revItSub->more() )
761 SMESH_subMesh* sm = revItSub->next();
762 checkConformIgnoredAlgos (aMesh, sm, aGlobIgnoAlgo,
763 algo, checkConform2, aCheckedMap, theErrors);
764 aCheckedMap.insert( sm );
773 //=======================================================================
774 //function : checkMissing
775 //purpose : notify on missing hypothesis
776 // Return false if algo or hipothesis is missing
777 //=======================================================================
779 static bool checkMissing(SMESH_Gen* aGen,
781 SMESH_subMesh* aSubMesh,
782 const int aTopAlgoDim,
784 const bool checkNoAlgo,
785 set<SMESH_subMesh*>& aCheckedMap,
786 list< SMESH_Gen::TAlgoStateError > & theErrors)
788 switch ( aSubMesh->GetSubShape().ShapeType() )
792 case TopAbs_SOLID: break; // check this sub-mesh, it can be meshed
794 return true; // not meshable sub-mesh
796 if ( aCheckedMap.count( aSubMesh ))
800 SMESH_Algo* algo = 0;
802 switch (aSubMesh->GetAlgoState())
804 case SMESH_subMesh::NO_ALGO: {
807 // should there be any algo?
808 int shapeDim = SMESH_Gen::GetShapeDim( aSubMesh->GetSubShape() );
809 if (aTopAlgoDim > shapeDim)
811 MESSAGE( "ERROR: " << shapeDim << "D algorithm is missing" );
813 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
814 theErrors.back().Set( SMESH_Hypothesis::HYP_MISSING, shapeDim, true );
819 case SMESH_subMesh::MISSING_HYP: {
820 // notify if an algo missing hyp is attached to aSubMesh
821 algo = aSubMesh->GetAlgo();
823 bool IsGlobalHypothesis = aGen->IsGlobalHypothesis( algo, aMesh );
824 if (!IsGlobalHypothesis || !globalChecked[ algo->GetDim() ])
826 TAlgoStateErrorName errName = SMESH_Hypothesis::HYP_MISSING;
827 SMESH_Hypothesis::Hypothesis_Status status;
828 algo->CheckHypothesis( aMesh, aSubMesh->GetSubShape(), status );
829 if ( status == SMESH_Hypothesis::HYP_BAD_PARAMETER ) {
830 MESSAGE( "ERROR: hypothesis of " << (IsGlobalHypothesis ? "Global " : "Local ")
831 << "<" << algo->GetName() << "> has a bad parameter value");
833 } else if ( status == SMESH_Hypothesis::HYP_BAD_GEOMETRY ) {
834 MESSAGE( "ERROR: " << (IsGlobalHypothesis ? "Global " : "Local ")
835 << "<" << algo->GetName() << "> assigned to mismatching geometry");
838 MESSAGE( "ERROR: " << (IsGlobalHypothesis ? "Global " : "Local ")
839 << "<" << algo->GetName() << "> misses some hypothesis");
841 if (IsGlobalHypothesis)
842 globalChecked[ algo->GetDim() ] = true;
843 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
844 theErrors.back().Set( errName, algo, IsGlobalHypothesis );
849 case SMESH_subMesh::HYP_OK:
850 algo = aSubMesh->GetAlgo();
852 if (!algo->NeedDiscreteBoundary())
854 SMESH_subMeshIteratorPtr itsub = aSubMesh->getDependsOnIterator( /*includeSelf=*/false,
855 /*complexShapeFirst=*/false);
856 while ( itsub->more() )
857 aCheckedMap.insert( itsub->next() );
863 // do not check under algo that hides sub-algos or
864 // re-start checking NO_ALGO state
866 bool isTopLocalAlgo =
867 ( aTopAlgoDim <= algo->GetDim() && !aGen->IsGlobalHypothesis( algo, aMesh ));
868 if (!algo->NeedDiscreteBoundary() || isTopLocalAlgo)
870 bool checkNoAlgo2 = ( algo->NeedDiscreteBoundary() );
871 SMESH_subMeshIteratorPtr itsub = aSubMesh->getDependsOnIterator( /*includeSelf=*/false,
872 /*complexShapeFirst=*/true);
873 while ( itsub->more() )
875 // sub-meshes should not be checked further more
876 SMESH_subMesh* sm = itsub->next();
880 //check algo on sub-meshes
881 int aTopAlgoDim2 = algo->GetDim();
882 if (!checkMissing (aGen, aMesh, sm, aTopAlgoDim2,
883 globalChecked, checkNoAlgo2, aCheckedMap, theErrors))
886 if (sm->GetAlgoState() == SMESH_subMesh::NO_ALGO )
887 checkNoAlgo2 = false;
890 aCheckedMap.insert( sm );
896 //=======================================================================
897 //function : CheckAlgoState
898 //purpose : notify on bad state of attached algos, return false
899 // if Compute() would fail because of some algo bad state
900 //=======================================================================
902 bool SMESH_Gen::CheckAlgoState(SMESH_Mesh& aMesh, const TopoDS_Shape& aShape)
904 list< TAlgoStateError > errors;
905 return GetAlgoState( aMesh, aShape, errors );
908 //=======================================================================
909 //function : GetAlgoState
910 //purpose : notify on bad state of attached algos, return false
911 // if Compute() would fail because of some algo bad state
912 // theErrors list contains problems description
913 //=======================================================================
915 bool SMESH_Gen::GetAlgoState(SMESH_Mesh& theMesh,
916 const TopoDS_Shape& theShape,
917 list< TAlgoStateError > & theErrors)
920 bool hasAlgo = false;
922 SMESH_subMesh* sm = theMesh.GetSubMesh(theShape);
923 const SMESHDS_Mesh* meshDS = theMesh.GetMeshDS();
924 TopoDS_Shape mainShape = meshDS->ShapeToMesh();
930 const SMESH_Algo* aGlobAlgoArr[] = {0,0,0,0};
932 const list<const SMESHDS_Hypothesis*>& listHyp = meshDS->GetHypothesis( mainShape );
933 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
934 for ( ; it != listHyp.end(); it++)
936 const SMESHDS_Hypothesis * aHyp = *it;
937 if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
940 const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
943 int dim = algo->GetDim();
944 aGlobAlgoArr[ dim ] = algo;
949 // --------------------------------------------------------
950 // info on algos that will be ignored because of ones that
951 // don't NeedDiscreteBoundary() attached to super-shapes,
952 // check that a conform mesh will be produced
953 // --------------------------------------------------------
956 // find a global algo possibly hiding sub-algos
958 const SMESH_Algo* aGlobIgnoAlgo = 0;
959 for (dim = 3; dim > 0; dim--)
961 if (aGlobAlgoArr[ dim ] &&
962 !aGlobAlgoArr[ dim ]->NeedDiscreteBoundary() /*&&
963 !aGlobAlgoArr[ dim ]->SupportSubmeshes()*/ )
965 aGlobIgnoAlgo = aGlobAlgoArr[ dim ];
970 set<SMESH_subMesh*> aCheckedSubs;
971 bool checkConform = ( !theMesh.IsNotConformAllowed() );
973 // loop on theShape and its sub-shapes
974 SMESH_subMeshIteratorPtr revItSub = sm->getDependsOnIterator( /*includeSelf=*/true,
975 /*complexShapeFirst=*/true);
976 while ( revItSub->more() )
978 SMESH_subMesh* smToCheck = revItSub->next();
979 if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
982 if ( aCheckedSubs.insert( smToCheck ).second ) // not yet checked
983 if (!checkConformIgnoredAlgos (theMesh, smToCheck, aGlobIgnoAlgo,
984 0, checkConform, aCheckedSubs, theErrors))
987 if ( smToCheck->GetAlgoState() != SMESH_subMesh::NO_ALGO )
991 // ----------------------------------------------------------------
992 // info on missing hypothesis and find out if all needed algos are
994 // ----------------------------------------------------------------
996 // find max dim of global algo
998 for (dim = 3; dim > 0; dim--)
1000 if (aGlobAlgoArr[ dim ])
1006 bool checkNoAlgo = theMesh.HasShapeToMesh() ? bool( aTopAlgoDim ) : false;
1007 bool globalChecked[] = { false, false, false, false };
1009 // loop on theShape and its sub-shapes
1010 aCheckedSubs.clear();
1011 revItSub = sm->getDependsOnIterator( /*includeSelf=*/true, /*complexShapeFirst=*/true);
1012 while ( revItSub->more() )
1014 SMESH_subMesh* smToCheck = revItSub->next();
1015 if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
1018 if (!checkMissing (this, theMesh, smToCheck, aTopAlgoDim,
1019 globalChecked, checkNoAlgo, aCheckedSubs, theErrors))
1022 if (smToCheck->GetAlgoState() == SMESH_subMesh::NO_ALGO )
1023 checkNoAlgo = false;
1029 theErrors.push_back( TAlgoStateError() );
1030 theErrors.back().Set( SMESH_Hypothesis::HYP_MISSING, theMesh.HasShapeToMesh() ? 1 : 3, true );
1036 //=======================================================================
1037 //function : IsGlobalHypothesis
1038 //purpose : check if theAlgo is attached to the main shape
1039 //=======================================================================
1041 bool SMESH_Gen::IsGlobalHypothesis(const SMESH_Hypothesis* theHyp, SMESH_Mesh& aMesh)
1043 SMESH_HypoFilter filter( SMESH_HypoFilter::Is( theHyp ));
1044 return aMesh.GetHypothesis( aMesh.GetMeshDS()->ShapeToMesh(), filter, false );
1047 //================================================================================
1049 * \brief Return paths to xml files of plugins
1051 //================================================================================
1053 std::vector< std::string > SMESH_Gen::GetPluginXMLPaths()
1055 // Get paths to xml files of plugins
1056 vector< string > xmlPaths;
1058 if ( const char* meshersList = getenv("SMESH_MeshersList") )
1060 string meshers = meshersList, plugin;
1061 string::size_type from = 0, pos;
1062 while ( from < meshers.size() )
1064 // cut off plugin name
1065 pos = meshers.find( env_sep, from );
1066 if ( pos != string::npos )
1067 plugin = meshers.substr( from, pos-from );
1069 plugin = meshers.substr( from ), pos = meshers.size();
1072 // get PLUGIN_ROOT_DIR path
1073 string rootDirVar, pluginSubDir = plugin;
1074 if ( plugin == "StdMeshers" )
1075 rootDirVar = "SMESH", pluginSubDir = "smesh";
1077 for ( pos = 0; pos < plugin.size(); ++pos )
1078 rootDirVar += toupper( plugin[pos] );
1079 rootDirVar += "_ROOT_DIR";
1081 const char* rootDir = getenv( rootDirVar.c_str() );
1082 if ( !rootDir || strlen(rootDir) == 0 )
1084 rootDirVar = plugin + "_ROOT_DIR"; // HexoticPLUGIN_ROOT_DIR
1085 rootDir = getenv( rootDirVar.c_str() );
1086 if ( !rootDir || strlen(rootDir) == 0 ) continue;
1089 // get a separator from rootDir
1090 for ( int i = strlen( rootDir )-1; i >= 0 && sep.empty(); --i )
1091 if ( rootDir[i] == '/' || rootDir[i] == '\\' )
1097 if (sep.empty() ) sep = "\\";
1099 if (sep.empty() ) sep = "/";
1102 // get a path to resource file
1103 string xmlPath = rootDir;
1104 if ( xmlPath[ xmlPath.size()-1 ] != sep[0] )
1106 xmlPath += "share" + sep + "salome" + sep + "resources" + sep;
1107 for ( pos = 0; pos < pluginSubDir.size(); ++pos )
1108 xmlPath += tolower( pluginSubDir[pos] );
1109 xmlPath += sep + plugin + ".xml";
1113 const wchar_t* path = Kernel_Utils::decode_s(xmlPath);
1114 SMESHUtils::ArrayDeleter<const wchar_t> deleter( path );
1116 const char* path = xmlPath.c_str();
1118 fileOK = (GetFileAttributes(path) != INVALID_FILE_ATTRIBUTES);
1121 fileOK = (access(xmlPath.c_str(), F_OK) == 0);
1124 xmlPaths.push_back( xmlPath );
1131 //=============================================================================
1133 * Finds algo to mesh a shape. Optionally returns a shape the found algo is bound to
1135 //=============================================================================
1137 SMESH_Algo *SMESH_Gen::GetAlgo(SMESH_Mesh & aMesh,
1138 const TopoDS_Shape & aShape,
1139 TopoDS_Shape* assignedTo)
1141 return GetAlgo( aMesh.GetSubMesh( aShape ), assignedTo );
1144 //=============================================================================
1146 * Finds algo to mesh a sub-mesh. Optionally returns a shape the found algo is bound to
1148 //=============================================================================
1150 SMESH_Algo *SMESH_Gen::GetAlgo(SMESH_subMesh * aSubMesh,
1151 TopoDS_Shape* assignedTo)
1153 if ( !aSubMesh ) return 0;
1155 const TopoDS_Shape & aShape = aSubMesh->GetSubShape();
1156 SMESH_Mesh& aMesh = *aSubMesh->GetFather();
1158 SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
1159 if ( aMesh.HasShapeToMesh() )
1160 filter.And( filter.IsApplicableTo( aShape ));
1162 typedef SMESH_Algo::Features AlgoData;
1164 TopoDS_Shape assignedToShape;
1166 (SMESH_Algo*) aMesh.GetHypothesis( aSubMesh, filter, true, &assignedToShape );
1169 aShape.ShapeType() == TopAbs_FACE &&
1170 !aShape.IsSame( assignedToShape ) &&
1171 SMESH_MesherHelper::NbAncestors( aShape, aMesh, TopAbs_SOLID ) > 1 )
1173 // Issue 0021559. If there is another 2D algo with different types of output
1174 // elements that can be used to mesh aShape, and 3D algos on adjacent SOLIDs
1175 // have different types of input elements, we choose a most appropriate 2D algo.
1177 // try to find a concurrent 2D algo
1178 filter.AndNot( filter.Is( algo ));
1179 TopoDS_Shape assignedToShape2;
1181 (SMESH_Algo*) aMesh.GetHypothesis( aSubMesh, filter, true, &assignedToShape2 );
1182 if ( algo2 && // algo found
1183 !assignedToShape2.IsSame( aMesh.GetShapeToMesh() ) && // algo is local
1184 ( SMESH_MesherHelper::GetGroupType( assignedToShape2 ) == // algo of the same level
1185 SMESH_MesherHelper::GetGroupType( assignedToShape )) &&
1186 aMesh.IsOrderOK( aMesh.GetSubMesh( assignedToShape2 ), // no forced order
1187 aMesh.GetSubMesh( assignedToShape )))
1189 // get algos on the adjacent SOLIDs
1190 filter.Init( filter.IsAlgo() ).And( filter.HasDim( 3 ));
1191 vector< SMESH_Algo* > algos3D;
1192 PShapeIteratorPtr solidIt = SMESH_MesherHelper::GetAncestors( aShape, aMesh,
1194 while ( const TopoDS_Shape* solid = solidIt->next() )
1195 if ( SMESH_Algo* algo3D = (SMESH_Algo*) aMesh.GetHypothesis( *solid, filter, true ))
1197 algos3D.push_back( algo3D );
1198 filter.AndNot( filter.HasName( algo3D->GetName() ));
1200 // check compatibility of algos
1201 if ( algos3D.size() > 1 )
1203 const AlgoData& algoData = algo->SMESH_Algo::GetFeatures();
1204 const AlgoData& algoData2 = algo2->SMESH_Algo::GetFeatures();
1205 const AlgoData& algoData3d0 = algos3D[0]->SMESH_Algo::GetFeatures();
1206 const AlgoData& algoData3d1 = algos3D[1]->SMESH_Algo::GetFeatures();
1207 if (( algoData2.IsCompatible( algoData3d0 ) &&
1208 algoData2.IsCompatible( algoData3d1 ))
1210 !(algoData.IsCompatible( algoData3d0 ) &&
1211 algoData.IsCompatible( algoData3d1 )))
1217 if ( assignedTo && algo )
1218 * assignedTo = assignedToShape;
1223 //=============================================================================
1225 * Returns StudyContextStruct for a study
1227 //=============================================================================
1229 StudyContextStruct *SMESH_Gen::GetStudyContext()
1231 return _studyContext;
1234 //================================================================================
1236 * \brief Return shape dimension by TopAbs_ShapeEnum
1238 //================================================================================
1240 int SMESH_Gen::GetShapeDim(const TopAbs_ShapeEnum & aShapeType)
1242 static vector<int> dim;
1245 dim.resize( TopAbs_SHAPE, -1 );
1246 dim[ TopAbs_COMPOUND ] = MeshDim_3D;
1247 dim[ TopAbs_COMPSOLID ] = MeshDim_3D;
1248 dim[ TopAbs_SOLID ] = MeshDim_3D;
1249 dim[ TopAbs_SHELL ] = MeshDim_2D;
1250 dim[ TopAbs_FACE ] = MeshDim_2D;
1251 dim[ TopAbs_WIRE ] = MeshDim_1D;
1252 dim[ TopAbs_EDGE ] = MeshDim_1D;
1253 dim[ TopAbs_VERTEX ] = MeshDim_0D;
1255 return dim[ aShapeType ];
1258 //================================================================================
1260 * \brief Return shape dimension by exploding compounds
1262 //================================================================================
1264 int SMESH_Gen::GetFlatShapeDim(const TopoDS_Shape &aShape)
1267 if ( aShape.ShapeType() == TopAbs_COMPOUND ||
1268 aShape.ShapeType() == TopAbs_COMPSOLID )
1270 TopoDS_Iterator it( aShape );
1271 aShapeDim = GetFlatShapeDim( it.Value() );
1274 aShapeDim = GetShapeDim( aShape );
1278 //=============================================================================
1280 * Generate a new id unique within this Gen
1282 //=============================================================================
1284 int SMESH_Gen::GetANewId()