1 // Copyright (C) 2007-2019 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)
121 throw(SALOME_Exception)
123 Unexpect aCatch(SalomeException);
125 // create a new SMESH_mesh object
126 SMESH_Mesh *aMesh = new SMESH_Mesh(_localId++,
129 _studyContext->myDocument);
130 _studyContext->mapMesh[_localId-1] = aMesh;
135 //=============================================================================
139 //=============================================================================
141 bool SMESH_Gen::Compute(SMESH_Mesh & aMesh,
142 const TopoDS_Shape & aShape,
143 const int aFlags /*= COMPACT_MESH*/,
144 const ::MeshDimension aDim /*=::MeshDim_3D*/,
145 TSetOfInt* aShapesId /*=0*/)
149 const bool aShapeOnly = aFlags & SHAPE_ONLY;
150 const bool anUpward = aFlags & UPWARD;
151 const bool aCompactMesh = aFlags & COMPACT_MESH;
155 SMESH_subMesh *sm = aMesh.GetSubMesh(aShape);
157 const bool includeSelf = true;
158 const bool complexShapeFirst = true;
159 const int globalAlgoDim = 100;
161 SMESH_subMeshIteratorPtr smIt;
163 // Fix of Issue 22150. Due to !BLSURF->OnlyUnaryInput(), BLSURF computes edges
164 // that must be computed by Projection 1D-2D while the Projection asks to compute
166 SMESH_subMesh::compute_event computeEvent =
167 aShapeOnly ? SMESH_subMesh::COMPUTE_SUBMESH : SMESH_subMesh::COMPUTE;
168 if ( !aMesh.HasShapeToMesh() )
169 computeEvent = SMESH_subMesh::COMPUTE_NOGEOM; // if several algos and no geometry
171 if ( anUpward ) // is called from the below code in this method
173 // ===============================================
174 // Mesh all the sub-shapes starting from vertices
175 // ===============================================
177 smIt = sm->getDependsOnIterator(includeSelf, !complexShapeFirst);
178 while ( smIt->more() )
180 SMESH_subMesh* smToCompute = smIt->next();
182 // do not mesh vertices of a pseudo shape
183 const TopoDS_Shape& shape = smToCompute->GetSubShape();
184 const TopAbs_ShapeEnum shapeType = shape.ShapeType();
185 if ( !aMesh.HasShapeToMesh() && shapeType == TopAbs_VERTEX )
188 // check for preview dimension limitations
189 if ( aShapesId && GetShapeDim( shapeType ) > (int)aDim )
191 // clear compute state not to show previous compute errors
192 // if preview invoked less dimension less than previous
193 smToCompute->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
197 if (smToCompute->GetComputeState() == SMESH_subMesh::READY_TO_COMPUTE)
199 if (_compute_canceled)
201 setCurrentSubMesh( smToCompute );
202 smToCompute->ComputeStateEngine( computeEvent );
203 setCurrentSubMesh( NULL );
206 // we check all the sub-meshes here and detect if any of them failed to compute
207 if (smToCompute->GetComputeState() == SMESH_subMesh::FAILED_TO_COMPUTE &&
208 ( shapeType != TopAbs_EDGE || !SMESH_Algo::isDegenerated( TopoDS::Edge( shape ))))
210 else if ( aShapesId )
211 aShapesId->insert( smToCompute->GetId() );
213 //aMesh.GetMeshDS()->Modified();
218 // ================================================================
219 // Apply algos that do NOT require discreteized boundaries
220 // ("all-dimensional") and do NOT support sub-meshes, starting from
221 // the most complex shapes and collect sub-meshes with algos that
222 // DO support sub-meshes
223 // ================================================================
225 list< SMESH_subMesh* > smWithAlgoSupportingSubmeshes[4]; // for each dim
227 // map to sort sm with same dim algos according to dim of
228 // the shape the algo assigned to (issue 0021217).
229 // Other issues influenced the algo applying order:
230 // 21406, 21556, 21893, 20206
231 multimap< int, SMESH_subMesh* > shDim2sm;
232 multimap< int, SMESH_subMesh* >::reverse_iterator shDim2smIt;
233 TopoDS_Shape algoShape;
234 int prevShapeDim = -1, aShapeDim;
236 smIt = sm->getDependsOnIterator(includeSelf, complexShapeFirst);
237 while ( smIt->more() )
239 SMESH_subMesh* smToCompute = smIt->next();
240 if ( smToCompute->GetComputeState() != SMESH_subMesh::READY_TO_COMPUTE )
243 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
244 aShapeDim = GetShapeDim( aSubShape );
245 if ( aShapeDim < 1 ) break;
247 // check for preview dimension limitations
248 if ( aShapesId && aShapeDim > (int)aDim )
251 SMESH_Algo* algo = GetAlgo( smToCompute, &algoShape );
252 if ( algo && !algo->NeedDiscreteBoundary() )
254 if ( algo->SupportSubmeshes() )
256 // reload sub-meshes from shDim2sm into smWithAlgoSupportingSubmeshes
257 // so that more local algos to go first
258 if ( prevShapeDim != aShapeDim )
260 prevShapeDim = aShapeDim;
261 for ( shDim2smIt = shDim2sm.rbegin(); shDim2smIt != shDim2sm.rend(); ++shDim2smIt )
262 if ( shDim2smIt->first == globalAlgoDim )
263 smWithAlgoSupportingSubmeshes[ aShapeDim ].push_back( shDim2smIt->second );
265 smWithAlgoSupportingSubmeshes[ aShapeDim ].push_front( shDim2smIt->second );
268 // add smToCompute to shDim2sm map
269 if ( algoShape.IsSame( aMesh.GetShapeToMesh() ))
271 aShapeDim = globalAlgoDim; // to compute last
275 aShapeDim = GetShapeDim( algoShape );
276 if ( algoShape.ShapeType() == TopAbs_COMPOUND )
278 TopoDS_Iterator it( algoShape );
279 aShapeDim += GetShapeDim( it.Value() );
282 shDim2sm.insert( make_pair( aShapeDim, smToCompute ));
284 else // Compute w/o support of sub-meshes
286 if (_compute_canceled)
288 setCurrentSubMesh( smToCompute );
289 smToCompute->ComputeStateEngine( computeEvent );
290 setCurrentSubMesh( NULL );
292 aShapesId->insert( smToCompute->GetId() );
296 // reload sub-meshes from shDim2sm into smWithAlgoSupportingSubmeshes
297 for ( shDim2smIt = shDim2sm.rbegin(); shDim2smIt != shDim2sm.rend(); ++shDim2smIt )
298 if ( shDim2smIt->first == globalAlgoDim )
299 smWithAlgoSupportingSubmeshes[3].push_back( shDim2smIt->second );
301 smWithAlgoSupportingSubmeshes[0].push_front( shDim2smIt->second );
303 // ======================================================
304 // Apply all-dimensional algorithms supporing sub-meshes
305 // ======================================================
307 std::vector< SMESH_subMesh* > smVec;
308 for ( aShapeDim = 0; aShapeDim < 4; ++aShapeDim )
310 // ------------------------------------------------
311 // sort list of sub-meshes according to mesh order
312 // ------------------------------------------------
313 smVec.assign( smWithAlgoSupportingSubmeshes[ aShapeDim ].begin(),
314 smWithAlgoSupportingSubmeshes[ aShapeDim ].end() );
315 aMesh.SortByMeshOrder( smVec );
317 // ------------------------------------------------------------
318 // compute sub-meshes with local uni-dimensional algos under
319 // sub-meshes with all-dimensional algos
320 // ------------------------------------------------------------
321 // start from lower shapes
322 for ( size_t i = 0; i < smVec.size(); ++i )
326 // get a shape the algo is assigned to
327 if ( !GetAlgo( sm, & algoShape ))
328 continue; // strange...
330 // look for more local algos
331 smIt = sm->getDependsOnIterator(!includeSelf, !complexShapeFirst);
332 while ( smIt->more() )
334 SMESH_subMesh* smToCompute = smIt->next();
336 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
337 const int aShapeDim = GetShapeDim( aSubShape );
338 //if ( aSubShape.ShapeType() == TopAbs_VERTEX ) continue;
339 if ( aShapeDim < 1 ) continue;
341 // check for preview dimension limitations
342 if ( aShapesId && GetShapeDim( aSubShape.ShapeType() ) > (int)aDim )
345 SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
347 .And( SMESH_HypoFilter::IsApplicableTo( aSubShape ))
348 .And( SMESH_HypoFilter::IsMoreLocalThan( algoShape, aMesh ));
350 if ( SMESH_Algo* subAlgo = (SMESH_Algo*) aMesh.GetHypothesis( smToCompute, filter, true))
352 if ( ! subAlgo->NeedDiscreteBoundary() ) continue;
353 SMESH_Hypothesis::Hypothesis_Status status;
354 if ( subAlgo->CheckHypothesis( aMesh, aSubShape, status ))
355 // mesh a lower smToCompute starting from vertices
356 Compute( aMesh, aSubShape, aFlags | SHAPE_ONLY_UPWARD, aDim, aShapesId );
357 // Compute( aMesh, aSubShape, aShapeOnly, /*anUpward=*/true, aDim, aShapesId );
361 // --------------------------------
362 // apply the all-dimensional algos
363 // --------------------------------
364 for ( size_t i = 0; i < smVec.size(); ++i )
367 if ( sm->GetComputeState() == SMESH_subMesh::READY_TO_COMPUTE)
369 const TopAbs_ShapeEnum shapeType = sm->GetSubShape().ShapeType();
370 // check for preview dimension limitations
371 if ( aShapesId && GetShapeDim( shapeType ) > (int)aDim )
374 if (_compute_canceled)
376 setCurrentSubMesh( sm );
377 sm->ComputeStateEngine( computeEvent );
378 setCurrentSubMesh( NULL );
380 aShapesId->insert( sm->GetId() );
383 } // loop on shape dimensions
385 // -----------------------------------------------
386 // mesh the rest sub-shapes starting from vertices
387 // -----------------------------------------------
388 ret = Compute( aMesh, aShape, aFlags | UPWARD, aDim, aShapesId );
393 // fix quadratic mesh by bending iternal links near concave boundary
394 if ( aCompactMesh && // a final compute
395 aShape.IsSame( aMesh.GetShapeToMesh() ) &&
396 !aShapesId && // not preview
397 ret ) // everything is OK
399 SMESH_MesherHelper aHelper( aMesh );
400 if ( aHelper.IsQuadraticMesh() != SMESH_MesherHelper::LINEAR )
402 aHelper.FixQuadraticElements( sm->GetComputeError() );
408 aMesh.GetMeshDS()->Modified();
409 aMesh.GetMeshDS()->CompactMesh();
414 //=============================================================================
416 * Prepare Compute a mesh
418 //=============================================================================
419 void SMESH_Gen::PrepareCompute(SMESH_Mesh & aMesh,
420 const TopoDS_Shape & aShape)
422 _compute_canceled = false;
423 resetCurrentSubMesh();
426 //=============================================================================
428 * Cancel Compute a mesh
430 //=============================================================================
431 void SMESH_Gen::CancelCompute(SMESH_Mesh & aMesh,
432 const TopoDS_Shape & aShape)
434 _compute_canceled = true;
435 if ( const SMESH_subMesh* sm = GetCurrentSubMesh() )
437 const_cast< SMESH_subMesh* >( sm )->ComputeStateEngine( SMESH_subMesh::COMPUTE_CANCELED );
439 resetCurrentSubMesh();
442 //================================================================================
444 * \brief Returns a sub-mesh being currently computed
446 //================================================================================
448 const SMESH_subMesh* SMESH_Gen::GetCurrentSubMesh() const
450 return _sm_current.empty() ? 0 : _sm_current.back();
453 //================================================================================
455 * \brief Sets a sub-mesh being currently computed.
457 * An algorithm can call Compute() for a sub-shape, hence we keep a stack of sub-meshes
459 //================================================================================
461 void SMESH_Gen::setCurrentSubMesh(SMESH_subMesh* sm)
464 _sm_current.push_back( sm );
466 else if ( !_sm_current.empty() )
467 _sm_current.pop_back();
470 void SMESH_Gen::resetCurrentSubMesh()
475 //=============================================================================
479 //=============================================================================
481 bool SMESH_Gen::Evaluate(SMESH_Mesh & aMesh,
482 const TopoDS_Shape & aShape,
483 MapShapeNbElems& aResMap,
485 TSetOfInt* aShapesId)
489 SMESH_subMesh *sm = aMesh.GetSubMesh(aShape);
491 const bool includeSelf = true;
492 const bool complexShapeFirst = true;
493 SMESH_subMeshIteratorPtr smIt;
495 if ( anUpward ) { // is called from below code here
496 // -----------------------------------------------
497 // mesh all the sub-shapes starting from vertices
498 // -----------------------------------------------
499 smIt = sm->getDependsOnIterator(includeSelf, !complexShapeFirst);
500 while ( smIt->more() ) {
501 SMESH_subMesh* smToCompute = smIt->next();
503 // do not mesh vertices of a pseudo shape
504 const TopAbs_ShapeEnum shapeType = smToCompute->GetSubShape().ShapeType();
505 //if ( !aMesh.HasShapeToMesh() && shapeType == TopAbs_VERTEX )
507 if ( !aMesh.HasShapeToMesh() ) {
508 if( shapeType == TopAbs_VERTEX || shapeType == TopAbs_WIRE ||
509 shapeType == TopAbs_SHELL )
513 smToCompute->Evaluate(aResMap);
515 aShapesId->insert( smToCompute->GetId() );
520 // -----------------------------------------------------------------
521 // apply algos that DO NOT require Discreteized boundaries and DO NOT
522 // support sub-meshes, starting from the most complex shapes
523 // and collect sub-meshes with algos that DO support sub-meshes
524 // -----------------------------------------------------------------
525 list< SMESH_subMesh* > smWithAlgoSupportingSubmeshes;
526 smIt = sm->getDependsOnIterator(includeSelf, complexShapeFirst);
527 while ( smIt->more() ) {
528 SMESH_subMesh* smToCompute = smIt->next();
529 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
530 const int aShapeDim = GetShapeDim( aSubShape );
531 if ( aShapeDim < 1 ) break;
533 SMESH_Algo* algo = GetAlgo( smToCompute );
534 if ( algo && !algo->NeedDiscreteBoundary() ) {
535 if ( algo->SupportSubmeshes() ) {
536 smWithAlgoSupportingSubmeshes.push_front( smToCompute );
539 smToCompute->Evaluate(aResMap);
541 aShapesId->insert( smToCompute->GetId() );
546 // ------------------------------------------------------------
547 // sort list of meshes according to mesh order
548 // ------------------------------------------------------------
549 std::vector< SMESH_subMesh* > smVec( smWithAlgoSupportingSubmeshes.begin(),
550 smWithAlgoSupportingSubmeshes.end() );
551 aMesh.SortByMeshOrder( smVec );
553 // ------------------------------------------------------------
554 // compute sub-meshes under shapes with algos that DO NOT require
555 // Discreteized boundaries and DO support sub-meshes
556 // ------------------------------------------------------------
557 // start from lower shapes
558 for ( size_t i = 0; i < smVec.size(); ++i )
562 // get a shape the algo is assigned to
563 TopoDS_Shape algoShape;
564 if ( !GetAlgo( sm, & algoShape ))
565 continue; // strange...
567 // look for more local algos
568 smIt = sm->getDependsOnIterator(!includeSelf, !complexShapeFirst);
569 while ( smIt->more() ) {
570 SMESH_subMesh* smToCompute = smIt->next();
572 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
573 const int aShapeDim = GetShapeDim( aSubShape );
574 if ( aShapeDim < 1 ) continue;
576 SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
578 .And( SMESH_HypoFilter::IsApplicableTo( aSubShape ))
579 .And( SMESH_HypoFilter::IsMoreLocalThan( algoShape, aMesh ));
581 if ( SMESH_Algo* subAlgo = (SMESH_Algo*) aMesh.GetHypothesis( smToCompute, filter, true ))
583 if ( ! subAlgo->NeedDiscreteBoundary() ) continue;
584 SMESH_Hypothesis::Hypothesis_Status status;
585 if ( subAlgo->CheckHypothesis( aMesh, aSubShape, status ))
586 // mesh a lower smToCompute starting from vertices
587 Evaluate( aMesh, aSubShape, aResMap, /*anUpward=*/true, aShapesId );
591 // ----------------------------------------------------------
592 // apply the algos that do not require Discreteized boundaries
593 // ----------------------------------------------------------
594 for ( size_t i = 0; i < smVec.size(); ++i )
597 sm->Evaluate(aResMap);
599 aShapesId->insert( sm->GetId() );
602 // -----------------------------------------------
603 // mesh the rest sub-shapes starting from vertices
604 // -----------------------------------------------
605 ret = Evaluate( aMesh, aShape, aResMap, /*anUpward=*/true, aShapesId );
612 //=======================================================================
613 //function : checkConformIgnoredAlgos
615 //=======================================================================
617 static bool checkConformIgnoredAlgos(SMESH_Mesh& aMesh,
618 SMESH_subMesh* aSubMesh,
619 const SMESH_Algo* aGlobIgnoAlgo,
620 const SMESH_Algo* aLocIgnoAlgo,
622 set<SMESH_subMesh*>& aCheckedMap,
623 list< SMESH_Gen::TAlgoStateError > & theErrors)
626 if ( aSubMesh->GetSubShape().ShapeType() == TopAbs_VERTEX)
632 const list<const SMESHDS_Hypothesis*>& listHyp =
633 aMesh.GetMeshDS()->GetHypothesis( aSubMesh->GetSubShape() );
634 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
635 for ( ; it != listHyp.end(); it++)
637 const SMESHDS_Hypothesis * aHyp = *it;
638 if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
641 const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
644 if ( aLocIgnoAlgo ) // algo is hidden by a local algo of upper dim
646 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
647 theErrors.back().Set( SMESH_Hypothesis::HYP_HIDDEN_ALGO, algo, false );
648 INFOS( "Local <" << algo->GetName() << "> is hidden by local <"
649 << aLocIgnoAlgo->GetName() << ">");
653 bool isGlobal = (aMesh.IsMainShape( aSubMesh->GetSubShape() ));
654 int dim = algo->GetDim();
655 int aMaxGlobIgnoDim = ( aGlobIgnoAlgo ? aGlobIgnoAlgo->GetDim() : -1 );
656 bool isNeededDim = ( aGlobIgnoAlgo ? aGlobIgnoAlgo->NeedLowerHyps( dim ) : false );
658 if (( dim < aMaxGlobIgnoDim && !isNeededDim ) &&
659 ( isGlobal || !aGlobIgnoAlgo->SupportSubmeshes() ))
661 // algo is hidden by a global algo
662 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
663 theErrors.back().Set( SMESH_Hypothesis::HYP_HIDDEN_ALGO, algo, true );
664 INFOS( ( isGlobal ? "Global" : "Local" )
665 << " <" << algo->GetName() << "> is hidden by global <"
666 << aGlobIgnoAlgo->GetName() << ">");
668 else if ( !algo->NeedDiscreteBoundary() && !isGlobal)
670 // local algo is not hidden and hides algos on sub-shapes
671 if (checkConform && !aSubMesh->IsConform( algo ))
674 checkConform = false; // no more check conformity
675 INFOS( "ERROR: Local <" << algo->GetName() <<
676 "> would produce not conform mesh: "
677 "<Not Conform Mesh Allowed> hypothesis is missing");
678 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
679 theErrors.back().Set( SMESH_Hypothesis::HYP_NOTCONFORM, algo, false );
682 // sub-algos will be hidden by a local <algo> if <algo> does not support sub-meshes
683 if ( algo->SupportSubmeshes() )
685 SMESH_subMeshIteratorPtr revItSub =
686 aSubMesh->getDependsOnIterator( /*includeSelf=*/false, /*complexShapeFirst=*/true);
687 bool checkConform2 = false;
688 while ( revItSub->more() )
690 SMESH_subMesh* sm = revItSub->next();
691 checkConformIgnoredAlgos (aMesh, sm, aGlobIgnoAlgo,
692 algo, checkConform2, aCheckedMap, theErrors);
693 aCheckedMap.insert( sm );
702 //=======================================================================
703 //function : checkMissing
704 //purpose : notify on missing hypothesis
705 // Return false if algo or hipothesis is missing
706 //=======================================================================
708 static bool checkMissing(SMESH_Gen* aGen,
710 SMESH_subMesh* aSubMesh,
711 const int aTopAlgoDim,
713 const bool checkNoAlgo,
714 set<SMESH_subMesh*>& aCheckedMap,
715 list< SMESH_Gen::TAlgoStateError > & theErrors)
717 switch ( aSubMesh->GetSubShape().ShapeType() )
721 case TopAbs_SOLID: break; // check this sub-mesh, it can be meshed
723 return true; // not meshable sub-mesh
725 if ( aCheckedMap.count( aSubMesh ))
729 SMESH_Algo* algo = 0;
731 switch (aSubMesh->GetAlgoState())
733 case SMESH_subMesh::NO_ALGO: {
736 // should there be any algo?
737 int shapeDim = SMESH_Gen::GetShapeDim( aSubMesh->GetSubShape() );
738 if (aTopAlgoDim > shapeDim)
740 MESSAGE( "ERROR: " << shapeDim << "D algorithm is missing" );
742 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
743 theErrors.back().Set( SMESH_Hypothesis::HYP_MISSING, shapeDim, true );
748 case SMESH_subMesh::MISSING_HYP: {
749 // notify if an algo missing hyp is attached to aSubMesh
750 algo = aSubMesh->GetAlgo();
752 bool IsGlobalHypothesis = aGen->IsGlobalHypothesis( algo, aMesh );
753 if (!IsGlobalHypothesis || !globalChecked[ algo->GetDim() ])
755 TAlgoStateErrorName errName = SMESH_Hypothesis::HYP_MISSING;
756 SMESH_Hypothesis::Hypothesis_Status status;
757 algo->CheckHypothesis( aMesh, aSubMesh->GetSubShape(), status );
758 if ( status == SMESH_Hypothesis::HYP_BAD_PARAMETER ) {
759 MESSAGE( "ERROR: hypothesis of " << (IsGlobalHypothesis ? "Global " : "Local ")
760 << "<" << algo->GetName() << "> has a bad parameter value");
762 } else if ( status == SMESH_Hypothesis::HYP_BAD_GEOMETRY ) {
763 MESSAGE( "ERROR: " << (IsGlobalHypothesis ? "Global " : "Local ")
764 << "<" << algo->GetName() << "> assigned to mismatching geometry");
767 MESSAGE( "ERROR: " << (IsGlobalHypothesis ? "Global " : "Local ")
768 << "<" << algo->GetName() << "> misses some hypothesis");
770 if (IsGlobalHypothesis)
771 globalChecked[ algo->GetDim() ] = true;
772 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
773 theErrors.back().Set( errName, algo, IsGlobalHypothesis );
778 case SMESH_subMesh::HYP_OK:
779 algo = aSubMesh->GetAlgo();
781 if (!algo->NeedDiscreteBoundary())
783 SMESH_subMeshIteratorPtr itsub = aSubMesh->getDependsOnIterator( /*includeSelf=*/false,
784 /*complexShapeFirst=*/false);
785 while ( itsub->more() )
786 aCheckedMap.insert( itsub->next() );
792 // do not check under algo that hides sub-algos or
793 // re-start checking NO_ALGO state
795 bool isTopLocalAlgo =
796 ( aTopAlgoDim <= algo->GetDim() && !aGen->IsGlobalHypothesis( algo, aMesh ));
797 if (!algo->NeedDiscreteBoundary() || isTopLocalAlgo)
799 bool checkNoAlgo2 = ( algo->NeedDiscreteBoundary() );
800 SMESH_subMeshIteratorPtr itsub = aSubMesh->getDependsOnIterator( /*includeSelf=*/false,
801 /*complexShapeFirst=*/true);
802 while ( itsub->more() )
804 // sub-meshes should not be checked further more
805 SMESH_subMesh* sm = itsub->next();
809 //check algo on sub-meshes
810 int aTopAlgoDim2 = algo->GetDim();
811 if (!checkMissing (aGen, aMesh, sm, aTopAlgoDim2,
812 globalChecked, checkNoAlgo2, aCheckedMap, theErrors))
815 if (sm->GetAlgoState() == SMESH_subMesh::NO_ALGO )
816 checkNoAlgo2 = false;
819 aCheckedMap.insert( sm );
825 //=======================================================================
826 //function : CheckAlgoState
827 //purpose : notify on bad state of attached algos, return false
828 // if Compute() would fail because of some algo bad state
829 //=======================================================================
831 bool SMESH_Gen::CheckAlgoState(SMESH_Mesh& aMesh, const TopoDS_Shape& aShape)
833 list< TAlgoStateError > errors;
834 return GetAlgoState( aMesh, aShape, errors );
837 //=======================================================================
838 //function : GetAlgoState
839 //purpose : notify on bad state of attached algos, return false
840 // if Compute() would fail because of some algo bad state
841 // theErrors list contains problems description
842 //=======================================================================
844 bool SMESH_Gen::GetAlgoState(SMESH_Mesh& theMesh,
845 const TopoDS_Shape& theShape,
846 list< TAlgoStateError > & theErrors)
849 bool hasAlgo = false;
851 SMESH_subMesh* sm = theMesh.GetSubMesh(theShape);
852 const SMESHDS_Mesh* meshDS = theMesh.GetMeshDS();
853 TopoDS_Shape mainShape = meshDS->ShapeToMesh();
859 const SMESH_Algo* aGlobAlgoArr[] = {0,0,0,0};
861 const list<const SMESHDS_Hypothesis*>& listHyp = meshDS->GetHypothesis( mainShape );
862 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
863 for ( ; it != listHyp.end(); it++)
865 const SMESHDS_Hypothesis * aHyp = *it;
866 if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
869 const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
872 int dim = algo->GetDim();
873 aGlobAlgoArr[ dim ] = algo;
878 // --------------------------------------------------------
879 // info on algos that will be ignored because of ones that
880 // don't NeedDiscreteBoundary() attached to super-shapes,
881 // check that a conform mesh will be produced
882 // --------------------------------------------------------
885 // find a global algo possibly hiding sub-algos
887 const SMESH_Algo* aGlobIgnoAlgo = 0;
888 for (dim = 3; dim > 0; dim--)
890 if (aGlobAlgoArr[ dim ] &&
891 !aGlobAlgoArr[ dim ]->NeedDiscreteBoundary() /*&&
892 !aGlobAlgoArr[ dim ]->SupportSubmeshes()*/ )
894 aGlobIgnoAlgo = aGlobAlgoArr[ dim ];
899 set<SMESH_subMesh*> aCheckedSubs;
900 bool checkConform = ( !theMesh.IsNotConformAllowed() );
902 // loop on theShape and its sub-shapes
903 SMESH_subMeshIteratorPtr revItSub = sm->getDependsOnIterator( /*includeSelf=*/true,
904 /*complexShapeFirst=*/true);
905 while ( revItSub->more() )
907 SMESH_subMesh* smToCheck = revItSub->next();
908 if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
911 if ( aCheckedSubs.insert( smToCheck ).second ) // not yet checked
912 if (!checkConformIgnoredAlgos (theMesh, smToCheck, aGlobIgnoAlgo,
913 0, checkConform, aCheckedSubs, theErrors))
916 if ( smToCheck->GetAlgoState() != SMESH_subMesh::NO_ALGO )
920 // ----------------------------------------------------------------
921 // info on missing hypothesis and find out if all needed algos are
923 // ----------------------------------------------------------------
925 // find max dim of global algo
927 for (dim = 3; dim > 0; dim--)
929 if (aGlobAlgoArr[ dim ])
935 bool checkNoAlgo = theMesh.HasShapeToMesh() ? bool( aTopAlgoDim ) : false;
936 bool globalChecked[] = { false, false, false, false };
938 // loop on theShape and its sub-shapes
939 aCheckedSubs.clear();
940 revItSub = sm->getDependsOnIterator( /*includeSelf=*/true, /*complexShapeFirst=*/true);
941 while ( revItSub->more() )
943 SMESH_subMesh* smToCheck = revItSub->next();
944 if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
947 if (!checkMissing (this, theMesh, smToCheck, aTopAlgoDim,
948 globalChecked, checkNoAlgo, aCheckedSubs, theErrors))
951 if (smToCheck->GetAlgoState() == SMESH_subMesh::NO_ALGO )
958 theErrors.push_back( TAlgoStateError() );
959 theErrors.back().Set( SMESH_Hypothesis::HYP_MISSING, theMesh.HasShapeToMesh() ? 1 : 3, true );
965 //=======================================================================
966 //function : IsGlobalHypothesis
967 //purpose : check if theAlgo is attached to the main shape
968 //=======================================================================
970 bool SMESH_Gen::IsGlobalHypothesis(const SMESH_Hypothesis* theHyp, SMESH_Mesh& aMesh)
972 SMESH_HypoFilter filter( SMESH_HypoFilter::Is( theHyp ));
973 return aMesh.GetHypothesis( aMesh.GetMeshDS()->ShapeToMesh(), filter, false );
976 //================================================================================
978 * \brief Return paths to xml files of plugins
980 //================================================================================
982 std::vector< std::string > SMESH_Gen::GetPluginXMLPaths()
984 // Get paths to xml files of plugins
985 vector< string > xmlPaths;
987 if ( const char* meshersList = getenv("SMESH_MeshersList") )
989 string meshers = meshersList, plugin;
990 string::size_type from = 0, pos;
991 while ( from < meshers.size() )
993 // cut off plugin name
994 pos = meshers.find( env_sep, from );
995 if ( pos != string::npos )
996 plugin = meshers.substr( from, pos-from );
998 plugin = meshers.substr( from ), pos = meshers.size();
1001 // get PLUGIN_ROOT_DIR path
1002 string rootDirVar, pluginSubDir = plugin;
1003 if ( plugin == "StdMeshers" )
1004 rootDirVar = "SMESH", pluginSubDir = "smesh";
1006 for ( pos = 0; pos < plugin.size(); ++pos )
1007 rootDirVar += toupper( plugin[pos] );
1008 rootDirVar += "_ROOT_DIR";
1010 const char* rootDir = getenv( rootDirVar.c_str() );
1011 if ( !rootDir || strlen(rootDir) == 0 )
1013 rootDirVar = plugin + "_ROOT_DIR"; // HexoticPLUGIN_ROOT_DIR
1014 rootDir = getenv( rootDirVar.c_str() );
1015 if ( !rootDir || strlen(rootDir) == 0 ) continue;
1018 // get a separator from rootDir
1019 for ( pos = strlen( rootDir )-1; pos >= 0 && sep.empty(); --pos )
1020 if ( rootDir[pos] == '/' || rootDir[pos] == '\\' )
1026 if (sep.empty() ) sep = "\\";
1028 if (sep.empty() ) sep = "/";
1031 // get a path to resource file
1032 string xmlPath = rootDir;
1033 if ( xmlPath[ xmlPath.size()-1 ] != sep[0] )
1035 xmlPath += "share" + sep + "salome" + sep + "resources" + sep;
1036 for ( pos = 0; pos < pluginSubDir.size(); ++pos )
1037 xmlPath += tolower( pluginSubDir[pos] );
1038 xmlPath += sep + plugin + ".xml";
1042 const wchar_t* path = Kernel_Utils::decode_s(xmlPath);
1044 const char* path = xmlPath.c_str();
1046 fileOK = (GetFileAttributes(path) != INVALID_FILE_ATTRIBUTES);
1051 fileOK = (access(xmlPath.c_str(), F_OK) == 0);
1054 xmlPaths.push_back( xmlPath );
1061 //=============================================================================
1063 * Finds algo to mesh a shape. Optionally returns a shape the found algo is bound to
1065 //=============================================================================
1067 SMESH_Algo *SMESH_Gen::GetAlgo(SMESH_Mesh & aMesh,
1068 const TopoDS_Shape & aShape,
1069 TopoDS_Shape* assignedTo)
1071 return GetAlgo( aMesh.GetSubMesh( aShape ), assignedTo );
1074 //=============================================================================
1076 * Finds algo to mesh a sub-mesh. Optionally returns a shape the found algo is bound to
1078 //=============================================================================
1080 SMESH_Algo *SMESH_Gen::GetAlgo(SMESH_subMesh * aSubMesh,
1081 TopoDS_Shape* assignedTo)
1083 if ( !aSubMesh ) return 0;
1085 const TopoDS_Shape & aShape = aSubMesh->GetSubShape();
1086 SMESH_Mesh& aMesh = *aSubMesh->GetFather();
1088 SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
1089 if ( aMesh.HasShapeToMesh() )
1090 filter.And( filter.IsApplicableTo( aShape ));
1092 typedef SMESH_Algo::Features AlgoData;
1094 TopoDS_Shape assignedToShape;
1096 (SMESH_Algo*) aMesh.GetHypothesis( aSubMesh, filter, true, &assignedToShape );
1099 aShape.ShapeType() == TopAbs_FACE &&
1100 !aShape.IsSame( assignedToShape ) &&
1101 SMESH_MesherHelper::NbAncestors( aShape, aMesh, TopAbs_SOLID ) > 1 )
1103 // Issue 0021559. If there is another 2D algo with different types of output
1104 // elements that can be used to mesh aShape, and 3D algos on adjacent SOLIDs
1105 // have different types of input elements, we choose a most appropriate 2D algo.
1107 // try to find a concurrent 2D algo
1108 filter.AndNot( filter.Is( algo ));
1109 TopoDS_Shape assignedToShape2;
1111 (SMESH_Algo*) aMesh.GetHypothesis( aSubMesh, filter, true, &assignedToShape2 );
1112 if ( algo2 && // algo found
1113 !assignedToShape2.IsSame( aMesh.GetShapeToMesh() ) && // algo is local
1114 ( SMESH_MesherHelper::GetGroupType( assignedToShape2 ) == // algo of the same level
1115 SMESH_MesherHelper::GetGroupType( assignedToShape )) &&
1116 aMesh.IsOrderOK( aMesh.GetSubMesh( assignedToShape2 ), // no forced order
1117 aMesh.GetSubMesh( assignedToShape )))
1119 // get algos on the adjacent SOLIDs
1120 filter.Init( filter.IsAlgo() ).And( filter.HasDim( 3 ));
1121 vector< SMESH_Algo* > algos3D;
1122 PShapeIteratorPtr solidIt = SMESH_MesherHelper::GetAncestors( aShape, aMesh,
1124 while ( const TopoDS_Shape* solid = solidIt->next() )
1125 if ( SMESH_Algo* algo3D = (SMESH_Algo*) aMesh.GetHypothesis( *solid, filter, true ))
1127 algos3D.push_back( algo3D );
1128 filter.AndNot( filter.HasName( algo3D->GetName() ));
1130 // check compatibility of algos
1131 if ( algos3D.size() > 1 )
1133 const AlgoData& algoData = algo->SMESH_Algo::GetFeatures();
1134 const AlgoData& algoData2 = algo2->SMESH_Algo::GetFeatures();
1135 const AlgoData& algoData3d0 = algos3D[0]->SMESH_Algo::GetFeatures();
1136 const AlgoData& algoData3d1 = algos3D[1]->SMESH_Algo::GetFeatures();
1137 if (( algoData2.IsCompatible( algoData3d0 ) &&
1138 algoData2.IsCompatible( algoData3d1 ))
1140 !(algoData.IsCompatible( algoData3d0 ) &&
1141 algoData.IsCompatible( algoData3d1 )))
1147 if ( assignedTo && algo )
1148 * assignedTo = assignedToShape;
1153 //=============================================================================
1155 * Returns StudyContextStruct for a study
1157 //=============================================================================
1159 StudyContextStruct *SMESH_Gen::GetStudyContext()
1161 return _studyContext;
1164 //================================================================================
1166 * \brief Return shape dimension by TopAbs_ShapeEnum
1168 //================================================================================
1170 int SMESH_Gen::GetShapeDim(const TopAbs_ShapeEnum & aShapeType)
1172 static vector<int> dim;
1175 dim.resize( TopAbs_SHAPE, -1 );
1176 dim[ TopAbs_COMPOUND ] = MeshDim_3D;
1177 dim[ TopAbs_COMPSOLID ] = MeshDim_3D;
1178 dim[ TopAbs_SOLID ] = MeshDim_3D;
1179 dim[ TopAbs_SHELL ] = MeshDim_2D;
1180 dim[ TopAbs_FACE ] = MeshDim_2D;
1181 dim[ TopAbs_WIRE ] = MeshDim_1D;
1182 dim[ TopAbs_EDGE ] = MeshDim_1D;
1183 dim[ TopAbs_VERTEX ] = MeshDim_0D;
1185 return dim[ aShapeType ];
1188 //=============================================================================
1190 * Generate a new id unique within this Gen
1192 //=============================================================================
1194 int SMESH_Gen::GetANewId()