1 // Copyright (C) 2007-2022 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 "SMESH_DriverMesh.hxx"
34 #include "SMDS_Mesh.hxx"
35 #include "SMDS_MeshElement.hxx"
36 #include "SMDS_MeshNode.hxx"
37 #include "SMESHDS_Document.hxx"
38 #include "SMESH_HypoFilter.hxx"
39 #include "SMESH_Mesh.hxx"
40 #include "SMESH_MesherHelper.hxx"
41 #include "SMESH_subMesh.hxx"
43 #include <utilities.h>
44 #include <Utils_ExceptHandlers.hxx>
46 #include <TopExp_Explorer.hxx>
48 #include <TopoDS_Iterator.hxx>
57 #include <Basics_Utils.hxx>
60 #include <boost/filesystem.hpp>
61 #include <boost/asio.hpp>
62 namespace fs = boost::filesystem;
64 // Environment variable separator
73 // a structure used to nullify SMESH_Gen field of SMESH_Hypothesis,
74 // which is needed for SMESH_Hypothesis not deleted before ~SMESH_Gen()
75 struct _Hyp : public SMESH_Hypothesis
83 //================================================================================
85 * \brief Fill a map of shapes with all sub-shape of a sub-mesh
87 //================================================================================
89 TopTools_IndexedMapOfShape * fillAllowed( SMESH_subMesh* sm,
91 TopTools_IndexedMapOfShape * allowedSub )
93 if ( !toFill || !allowedSub )
97 if ( allowedSub->IsEmpty() )
99 allowedSub->ReSize( sm->DependsOn().size() + 1 );
100 allowedSub->Add( sm->GetSubShape() );
101 for ( const auto & key_sm : sm->DependsOn() )
102 allowedSub->Add( key_sm.second->GetSubShape() );
108 //=============================================================================
112 //=============================================================================
114 SMESH_Gen::SMESH_Gen()
116 _studyContext = new StudyContextStruct;
117 _studyContext->myDocument = new SMESHDS_Document();
120 _segmentation = _nbSegments = 10;
121 _compute_canceled = false;
124 //=============================================================================
128 //=============================================================================
130 SMESH_Gen::~SMESH_Gen()
132 std::map < int, SMESH_Hypothesis * >::iterator i_hyp = _studyContext->mapHypothesis.begin();
133 for ( ; i_hyp != _studyContext->mapHypothesis.end(); ++i_hyp )
135 if ( _Hyp* h = static_cast< _Hyp*>( i_hyp->second ))
138 delete _studyContext->myDocument;
139 delete _studyContext;
142 //=============================================================================
144 * Creates a mesh in a study.
145 * if (theIsEmbeddedMode) { mesh modification commands are not logged }
147 //=============================================================================
149 SMESH_Mesh* SMESH_Gen::CreateMesh(bool theIsEmbeddedMode)
151 Unexpect aCatch(SalomeException);
153 // create a new SMESH_mesh object
154 SMESH_Mesh *aMesh = new SMESH_Mesh(_localId++,
157 _studyContext->myDocument);
158 _studyContext->mapMesh[_localId-1] = aMesh;
163 //=============================================================================
165 * Parallel compute of a submesh
166 * This function is used to pass to thread_pool
168 //=============================================================================
169 const std::function<void(int,
171 SMESH_subMesh::compute_event,
174 TopTools_IndexedMapOfShape *,
176 compute_function([&] (int id,
178 SMESH_subMesh::compute_event event,
179 SMESH_subMesh *shapeSM,
181 TopTools_IndexedMapOfShape *allowedSubShapes,
182 TSetOfInt* aShapesId) -> void
184 if (sm->GetComputeState() == SMESH_subMesh::READY_TO_COMPUTE)
186 sm->SetAllowedSubShapes( fillAllowed( shapeSM, aShapeOnly, allowedSubShapes ));
187 //setCurrentSubMesh( sm );
188 sm->ComputeStateEngine(event);
189 //setCurrentSubMesh( nullptr );
190 sm->SetAllowedSubShapes( nullptr );
194 aShapesId->insert( sm->GetId() );
198 //=============================================================================
202 //=============================================================================
204 bool SMESH_Gen::Compute(SMESH_Mesh & aMesh,
205 const TopoDS_Shape & aShape,
206 const int aFlags /*= COMPACT_MESH*/,
207 const ::MeshDimension aDim /*=::MeshDim_3D*/,
208 TSetOfInt* aShapesId /*=0*/,
209 TopTools_IndexedMapOfShape* anAllowedSubShapes/*=0*/)
213 const bool aShapeOnly = aFlags & SHAPE_ONLY;
214 const bool anUpward = aFlags & UPWARD;
215 const bool aCompactMesh = aFlags & COMPACT_MESH;
219 SMESH_subMesh *sm, *shapeSM = aMesh.GetSubMesh(aShape);
221 const bool includeSelf = true;
222 const bool complexShapeFirst = true;
223 const int globalAlgoDim = 100;
225 // Pool of thread for computation
226 if(aMesh.IsParallel())
227 aMesh.InitPoolThreads();
229 SMESH_subMeshIteratorPtr smIt;
231 // Fix of Issue 22150. Due to !BLSURF->OnlyUnaryInput(), BLSURF computes edges
232 // that must be computed by Projection 1D-2D while the Projection asks to compute
234 SMESH_subMesh::compute_event computeEvent =
235 aShapeOnly ? SMESH_subMesh::COMPUTE_SUBMESH : SMESH_subMesh::COMPUTE;
236 if ( !aMesh.HasShapeToMesh() )
237 computeEvent = SMESH_subMesh::COMPUTE_NOGEOM; // if several algos and no geometry
239 TopTools_IndexedMapOfShape *allowedSubShapes = anAllowedSubShapes, allowedSub;
240 if ( aShapeOnly && !allowedSubShapes )
241 allowedSubShapes = &allowedSub;
243 if ( anUpward ) // is called from the below code in this method
245 // ===============================================
246 // Mesh all the sub-shapes starting from vertices
247 // ===============================================
249 TopAbs_ShapeEnum previousShapeType = TopAbs_VERTEX;
250 int nbThreads = aMesh.GetNbThreads();
251 MESSAGE("Running mesh with threads: " << nbThreads << " mesher: " << aMesh.GetMesherNbThreads());
254 smIt = shapeSM->getDependsOnIterator(includeSelf, !complexShapeFirst);
255 while ( smIt->more() )
257 SMESH_subMesh* smToCompute = smIt->next();
259 // do not mesh vertices of a pseudo shape
260 const TopoDS_Shape& shape = smToCompute->GetSubShape();
261 const TopAbs_ShapeEnum shapeType = shape.ShapeType();
262 if ( !aMesh.HasShapeToMesh() && shapeType == TopAbs_VERTEX )
264 if(shapeType==TopAbs_FACE||shapeType==TopAbs_EDGE)
265 aMesh.SetNbThreads(0);
267 aMesh.SetNbThreads(nbThreads);
268 if ((aMesh.IsParallel()||nbThreads!=0) && shapeType != previousShapeType) {
269 // Waiting for all threads for the previous type to end
272 std::string file_name;
273 switch(previousShapeType){
275 file_name = "Mesh2D.med";
278 file_name = "Mesh1D.med";
281 file_name = "Mesh0D.med";
290 fs::path mesh_file = fs::path(aMesh.tmp_folder) / fs::path(file_name);
291 exportMesh(mesh_file.string(), aMesh, "MESH");
294 //Resetting threaded pool info
295 previousShapeType = shapeType;
298 // check for preview dimension limitations
299 if ( aShapesId && GetShapeDim( shapeType ) > (int)aDim )
301 // clear compute state not to show previous compute errors
302 // if preview invoked less dimension less than previous
303 smToCompute->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
306 if(aMesh.IsParallel())
308 boost::asio::post(*(aMesh._pool), std::bind(compute_function, 1, smToCompute, computeEvent,
309 shapeSM, aShapeOnly, allowedSubShapes,
312 compute_function(1 ,smToCompute, computeEvent,
313 shapeSM, aShapeOnly, allowedSubShapes,
316 if (smToCompute->GetComputeState() == SMESH_subMesh::FAILED_TO_COMPUTE &&
317 ( shapeType != TopAbs_EDGE || !SMESH_Algo::isDegenerated( TopoDS::Edge( shape ))))
319 else if ( aShapesId )
320 aShapesId->insert( smToCompute->GetId() );
324 // TODO: Check error handling in parallel mode
325 if(aMesh.IsParallel()){
326 // Waiting for the thread for Solids to finish
330 aMesh.GetMeshDS()->Modified();
336 // ================================================================
337 // Apply algos that do NOT require discretized boundaries
338 // ("all-dimensional") and do NOT support sub-meshes, starting from
339 // the most complex shapes and collect sub-meshes with algos that
340 // DO support sub-meshes
341 // ================================================================
342 list< SMESH_subMesh* > smWithAlgoSupportingSubmeshes[4]; // for each dim
344 // map to sort sm with same dim algos according to dim of
345 // the shape the algo assigned to (issue 0021217).
346 // Other issues influenced the algo applying order:
347 // 21406, 21556, 21893, 20206
348 multimap< int, SMESH_subMesh* > shDim2sm;
349 multimap< int, SMESH_subMesh* >::reverse_iterator shDim2smIt;
350 TopoDS_Shape algoShape;
351 int prevShapeDim = -1, aShapeDim;
353 smIt = shapeSM->getDependsOnIterator(includeSelf, complexShapeFirst);
354 while ( smIt->more() )
356 SMESH_subMesh* smToCompute = smIt->next();
357 if ( smToCompute->GetComputeState() != SMESH_subMesh::READY_TO_COMPUTE )
360 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
361 aShapeDim = GetShapeDim( aSubShape );
362 if ( aShapeDim < 1 ) break;
364 // check for preview dimension limitations
365 if ( aShapesId && aShapeDim > (int)aDim )
368 SMESH_Algo* algo = GetAlgo( smToCompute, &algoShape );
369 if ( algo && !algo->NeedDiscreteBoundary() )
371 if ( algo->SupportSubmeshes() )
373 // reload sub-meshes from shDim2sm into smWithAlgoSupportingSubmeshes
374 // so that more local algos to go first
375 if ( prevShapeDim != aShapeDim )
377 prevShapeDim = aShapeDim;
378 for ( shDim2smIt = shDim2sm.rbegin(); shDim2smIt != shDim2sm.rend(); ++shDim2smIt )
379 if ( shDim2smIt->first == globalAlgoDim )
380 smWithAlgoSupportingSubmeshes[ aShapeDim ].push_back( shDim2smIt->second );
382 smWithAlgoSupportingSubmeshes[ aShapeDim ].push_front( shDim2smIt->second );
385 // add smToCompute to shDim2sm map
386 if ( algoShape.IsSame( aMesh.GetShapeToMesh() ))
388 aShapeDim = globalAlgoDim; // to compute last
392 aShapeDim = GetShapeDim( algoShape );
393 if ( algoShape.ShapeType() == TopAbs_COMPOUND )
395 TopoDS_Iterator it( algoShape );
396 aShapeDim += GetShapeDim( it.Value() );
399 shDim2sm.insert( make_pair( aShapeDim, smToCompute ));
401 else // Compute w/o support of sub-meshes
403 if (_compute_canceled)
405 smToCompute->SetAllowedSubShapes( fillAllowed( shapeSM, aShapeOnly, allowedSubShapes ));
406 setCurrentSubMesh( smToCompute );
407 smToCompute->ComputeStateEngine( computeEvent );
408 setCurrentSubMesh( nullptr );
409 smToCompute->SetAllowedSubShapes( nullptr );
411 aShapesId->insert( smToCompute->GetId() );
415 // reload sub-meshes from shDim2sm into smWithAlgoSupportingSubmeshes
416 for ( shDim2smIt = shDim2sm.rbegin(); shDim2smIt != shDim2sm.rend(); ++shDim2smIt )
417 if ( shDim2smIt->first == globalAlgoDim )
418 smWithAlgoSupportingSubmeshes[3].push_back( shDim2smIt->second );
420 smWithAlgoSupportingSubmeshes[0].push_front( shDim2smIt->second );
422 // ======================================================
423 // Apply all-dimensional algorithms supporing sub-meshes
424 // ======================================================
426 std::vector< SMESH_subMesh* > smVec;
427 for ( aShapeDim = 0; aShapeDim < 4; ++aShapeDim )
428 smVec.insert( smVec.end(),
429 smWithAlgoSupportingSubmeshes[aShapeDim].begin(),
430 smWithAlgoSupportingSubmeshes[aShapeDim].end() );
432 // gather sub-shapes with local uni-dimensional algos (bos #29143)
433 // ----------------------------------------------------------------
434 TopTools_MapOfShape uniDimAlgoShapes;
435 if ( !smVec.empty() )
437 ShapeToHypothesis::Iterator s2hyps( aMesh.GetMeshDS()->GetHypotheses() );
438 for ( ; s2hyps.More(); s2hyps.Next() )
440 const TopoDS_Shape& s = s2hyps.Key();
441 if ( s.IsSame( aMesh.GetShapeToMesh() ))
443 for ( auto & hyp : s2hyps.Value() )
445 if ( const SMESH_Algo* algo = dynamic_cast< const SMESH_Algo*>( hyp ))
446 if ( algo->NeedDiscreteBoundary() )
448 TopAbs_ShapeEnum sType;
449 switch ( algo->GetDim() ) {
450 case 3: sType = TopAbs_SOLID; break;
451 case 2: sType = TopAbs_FACE; break;
452 default: sType = TopAbs_EDGE; break;
454 for ( TopExp_Explorer ex( s2hyps.Key(), sType ); ex.More(); ex.Next() )
455 uniDimAlgoShapes.Add( ex.Current() );
462 // ------------------------------------------------
463 // sort list of sub-meshes according to mesh order
464 // ------------------------------------------------
465 aMesh.SortByMeshOrder( smVec );
467 // ------------------------------------------------------------
468 // compute sub-meshes with local uni-dimensional algos under
469 // sub-meshes with all-dimensional algos
470 // ------------------------------------------------------------
471 // start from lower shapes
472 for ( size_t i = 0; i < smVec.size(); ++i )
475 if ( sm->GetComputeState() != SMESH_subMesh::READY_TO_COMPUTE)
478 const TopAbs_ShapeEnum shapeType = sm->GetSubShape().ShapeType();
480 if ( !uniDimAlgoShapes.IsEmpty() )
482 // get a shape the algo is assigned to
483 if ( !GetAlgo( sm, & algoShape ))
484 continue; // strange...
486 // look for more local algos
487 if ( SMESH_subMesh* algoSM = aMesh.GetSubMesh( algoShape ))
488 smIt = algoSM->getDependsOnIterator(!includeSelf, !complexShapeFirst);
490 smIt = sm->getDependsOnIterator(!includeSelf, !complexShapeFirst);
492 while ( smIt->more() )
494 SMESH_subMesh* smToCompute = smIt->next();
496 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
497 const int aShapeDim = GetShapeDim( aSubShape );
498 if ( aShapeDim < 1 || aSubShape.ShapeType() <= shapeType )
500 if ( !uniDimAlgoShapes.Contains( aSubShape ))
501 continue; // [bos #29143] aMesh.GetHypothesis() is too long
503 // check for preview dimension limitations
504 if ( aShapesId && GetShapeDim( aSubShape.ShapeType() ) > (int)aDim )
507 SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
509 .And( SMESH_HypoFilter::IsApplicableTo( aSubShape ))
510 .And( SMESH_HypoFilter::IsMoreLocalThan( algoShape, aMesh ));
512 if ( SMESH_Algo* subAlgo = (SMESH_Algo*) aMesh.GetHypothesis( smToCompute, filter, true))
514 if ( ! subAlgo->NeedDiscreteBoundary() ) continue;
515 TopTools_IndexedMapOfShape* localAllowed = allowedSubShapes;
516 if ( localAllowed && localAllowed->IsEmpty() )
517 localAllowed = 0; // prevent fillAllowed() with aSubShape
519 SMESH_Hypothesis::Hypothesis_Status status;
520 if ( subAlgo->CheckHypothesis( aMesh, aSubShape, status ))
521 // mesh a lower smToCompute starting from vertices
522 Compute( aMesh, aSubShape, aFlags | SHAPE_ONLY_UPWARD, aDim, aShapesId, localAllowed );
526 // --------------------------------
527 // apply the all-dimensional algo
528 // --------------------------------
530 if (_compute_canceled)
532 // check for preview dimension limitations
533 if ( aShapesId && GetShapeDim( shapeType ) > (int)aDim )
535 sm->SetAllowedSubShapes( fillAllowed( shapeSM, aShapeOnly, allowedSubShapes ));
536 setCurrentSubMesh( sm );
537 sm->ComputeStateEngine( computeEvent );
539 setCurrentSubMesh( NULL );
540 sm->SetAllowedSubShapes( nullptr );
542 aShapesId->insert( sm->GetId() );
547 // -----------------------------------------------
548 // mesh the rest sub-shapes starting from vertices
549 // -----------------------------------------------
550 ret = Compute( aMesh, aShape, aFlags | UPWARD, aDim, aShapesId, allowedSubShapes );
556 // fix quadratic mesh by bending iternal links near concave boundary
557 if ( aCompactMesh && // a final compute
558 aShape.IsSame( aMesh.GetShapeToMesh() ) &&
559 !aShapesId && // not preview
560 ret ) // everything is OK
562 SMESH_MesherHelper aHelper( aMesh );
563 if ( aHelper.IsQuadraticMesh() != SMESH_MesherHelper::LINEAR )
565 aHelper.FixQuadraticElements( shapeSM->GetComputeError() );
571 aMesh.GetMeshDS()->Modified();
572 aMesh.GetMeshDS()->CompactMesh();
577 //=============================================================================
579 * Prepare Compute a mesh
581 //=============================================================================
582 void SMESH_Gen::PrepareCompute(SMESH_Mesh & /*aMesh*/,
583 const TopoDS_Shape & /*aShape*/)
585 _compute_canceled = false;
586 resetCurrentSubMesh();
589 //=============================================================================
591 * Cancel Compute a mesh
593 //=============================================================================
594 void SMESH_Gen::CancelCompute(SMESH_Mesh & /*aMesh*/,
595 const TopoDS_Shape & /*aShape*/)
597 _compute_canceled = true;
598 if ( const SMESH_subMesh* sm = GetCurrentSubMesh() )
600 const_cast< SMESH_subMesh* >( sm )->ComputeStateEngine( SMESH_subMesh::COMPUTE_CANCELED );
602 resetCurrentSubMesh();
605 //================================================================================
607 * \brief Returns a sub-mesh being currently computed
609 //================================================================================
611 const SMESH_subMesh* SMESH_Gen::GetCurrentSubMesh() const
613 return _sm_current.empty() ? 0 : _sm_current.back();
616 //================================================================================
618 * \brief Sets a sub-mesh being currently computed.
620 * An algorithm can call Compute() for a sub-shape, hence we keep a stack of sub-meshes
622 //================================================================================
624 void SMESH_Gen::setCurrentSubMesh(SMESH_subMesh* sm)
627 _sm_current.push_back( sm );
629 else if ( !_sm_current.empty() )
630 _sm_current.pop_back();
633 void SMESH_Gen::resetCurrentSubMesh()
638 //=============================================================================
642 //=============================================================================
644 bool SMESH_Gen::Evaluate(SMESH_Mesh & aMesh,
645 const TopoDS_Shape & aShape,
646 MapShapeNbElems& aResMap,
648 TSetOfInt* aShapesId)
652 SMESH_subMesh *sm = aMesh.GetSubMesh(aShape);
654 const bool includeSelf = true;
655 const bool complexShapeFirst = true;
656 SMESH_subMeshIteratorPtr smIt;
658 if ( anUpward ) { // is called from below code here
659 // -----------------------------------------------
660 // mesh all the sub-shapes starting from vertices
661 // -----------------------------------------------
662 smIt = sm->getDependsOnIterator(includeSelf, !complexShapeFirst);
663 while ( smIt->more() ) {
664 SMESH_subMesh* smToCompute = smIt->next();
666 // do not mesh vertices of a pseudo shape
667 const TopAbs_ShapeEnum shapeType = smToCompute->GetSubShape().ShapeType();
668 //if ( !aMesh.HasShapeToMesh() && shapeType == TopAbs_VERTEX )
670 if ( !aMesh.HasShapeToMesh() ) {
671 if( shapeType == TopAbs_VERTEX || shapeType == TopAbs_WIRE ||
672 shapeType == TopAbs_SHELL )
676 smToCompute->Evaluate(aResMap);
678 aShapesId->insert( smToCompute->GetId() );
683 // -----------------------------------------------------------------
684 // apply algos that DO NOT require Discreteized boundaries and DO NOT
685 // support sub-meshes, starting from the most complex shapes
686 // and collect sub-meshes with algos that DO support sub-meshes
687 // -----------------------------------------------------------------
688 list< SMESH_subMesh* > smWithAlgoSupportingSubmeshes;
689 smIt = sm->getDependsOnIterator(includeSelf, complexShapeFirst);
690 while ( smIt->more() ) {
691 SMESH_subMesh* smToCompute = smIt->next();
692 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
693 const int aShapeDim = GetShapeDim( aSubShape );
694 if ( aShapeDim < 1 ) break;
696 SMESH_Algo* algo = GetAlgo( smToCompute );
697 if ( algo && !algo->NeedDiscreteBoundary() ) {
698 if ( algo->SupportSubmeshes() ) {
699 smWithAlgoSupportingSubmeshes.push_front( smToCompute );
702 smToCompute->Evaluate(aResMap);
704 aShapesId->insert( smToCompute->GetId() );
709 // ------------------------------------------------------------
710 // sort list of meshes according to mesh order
711 // ------------------------------------------------------------
712 std::vector< SMESH_subMesh* > smVec( smWithAlgoSupportingSubmeshes.begin(),
713 smWithAlgoSupportingSubmeshes.end() );
714 aMesh.SortByMeshOrder( smVec );
716 // ------------------------------------------------------------
717 // compute sub-meshes under shapes with algos that DO NOT require
718 // Discreteized boundaries and DO support sub-meshes
719 // ------------------------------------------------------------
720 // start from lower shapes
721 for ( size_t i = 0; i < smVec.size(); ++i )
725 // get a shape the algo is assigned to
726 TopoDS_Shape algoShape;
727 if ( !GetAlgo( sm, & algoShape ))
728 continue; // strange...
730 // look for more local algos
731 smIt = sm->getDependsOnIterator(!includeSelf, !complexShapeFirst);
732 while ( smIt->more() ) {
733 SMESH_subMesh* smToCompute = smIt->next();
735 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
736 const int aShapeDim = GetShapeDim( aSubShape );
737 if ( aShapeDim < 1 ) continue;
739 SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
741 .And( SMESH_HypoFilter::IsApplicableTo( aSubShape ))
742 .And( SMESH_HypoFilter::IsMoreLocalThan( algoShape, aMesh ));
744 if ( SMESH_Algo* subAlgo = (SMESH_Algo*) aMesh.GetHypothesis( smToCompute, filter, true ))
746 if ( ! subAlgo->NeedDiscreteBoundary() ) continue;
747 SMESH_Hypothesis::Hypothesis_Status status;
748 if ( subAlgo->CheckHypothesis( aMesh, aSubShape, status ))
749 // mesh a lower smToCompute starting from vertices
750 Evaluate( aMesh, aSubShape, aResMap, /*anUpward=*/true, aShapesId );
754 // ----------------------------------------------------------
755 // apply the algos that do not require Discreteized boundaries
756 // ----------------------------------------------------------
757 for ( size_t i = 0; i < smVec.size(); ++i )
760 sm->Evaluate(aResMap);
762 aShapesId->insert( sm->GetId() );
765 // -----------------------------------------------
766 // mesh the rest sub-shapes starting from vertices
767 // -----------------------------------------------
768 ret = Evaluate( aMesh, aShape, aResMap, /*anUpward=*/true, aShapesId );
775 //=======================================================================
776 //function : checkConformIgnoredAlgos
778 //=======================================================================
780 static bool checkConformIgnoredAlgos(SMESH_Mesh& aMesh,
781 SMESH_subMesh* aSubMesh,
782 const SMESH_Algo* aGlobIgnoAlgo,
783 const SMESH_Algo* aLocIgnoAlgo,
785 set<SMESH_subMesh*>& aCheckedMap,
786 list< SMESH_Gen::TAlgoStateError > & theErrors)
789 if ( aSubMesh->GetSubShape().ShapeType() == TopAbs_VERTEX)
795 const list<const SMESHDS_Hypothesis*>& listHyp =
796 aMesh.GetMeshDS()->GetHypothesis( aSubMesh->GetSubShape() );
797 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
798 for ( ; it != listHyp.end(); it++)
800 const SMESHDS_Hypothesis * aHyp = *it;
801 if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
804 const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
807 if ( aLocIgnoAlgo ) // algo is hidden by a local algo of upper dim
809 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
810 theErrors.back().Set( SMESH_Hypothesis::HYP_HIDDEN_ALGO, algo, false );
811 INFOS( "Local <" << algo->GetName() << "> is hidden by local <"
812 << aLocIgnoAlgo->GetName() << ">");
816 bool isGlobal = (aMesh.IsMainShape( aSubMesh->GetSubShape() ));
817 int dim = algo->GetDim();
818 int aMaxGlobIgnoDim = ( aGlobIgnoAlgo ? aGlobIgnoAlgo->GetDim() : -1 );
819 bool isNeededDim = ( aGlobIgnoAlgo ? aGlobIgnoAlgo->NeedLowerHyps( dim ) : false );
821 if (( dim < aMaxGlobIgnoDim && !isNeededDim ) &&
822 ( isGlobal || !aGlobIgnoAlgo->SupportSubmeshes() ))
824 // algo is hidden by a global algo
825 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
826 theErrors.back().Set( SMESH_Hypothesis::HYP_HIDDEN_ALGO, algo, true );
827 INFOS( ( isGlobal ? "Global" : "Local" )
828 << " <" << algo->GetName() << "> is hidden by global <"
829 << aGlobIgnoAlgo->GetName() << ">");
831 else if ( !algo->NeedDiscreteBoundary() && !isGlobal)
833 // local algo is not hidden and hides algos on sub-shapes
834 if (checkConform && !aSubMesh->IsConform( algo ))
837 checkConform = false; // no more check conformity
838 INFOS( "ERROR: Local <" << algo->GetName() <<
839 "> would produce not conform mesh: "
840 "<Not Conform Mesh Allowed> hypothesis is missing");
841 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
842 theErrors.back().Set( SMESH_Hypothesis::HYP_NOTCONFORM, algo, false );
845 // sub-algos will be hidden by a local <algo> if <algo> does not support sub-meshes
846 if ( algo->SupportSubmeshes() )
848 SMESH_subMeshIteratorPtr revItSub =
849 aSubMesh->getDependsOnIterator( /*includeSelf=*/false, /*complexShapeFirst=*/true);
850 bool checkConform2 = false;
851 while ( revItSub->more() )
853 SMESH_subMesh* sm = revItSub->next();
854 checkConformIgnoredAlgos (aMesh, sm, aGlobIgnoAlgo,
855 algo, checkConform2, aCheckedMap, theErrors);
856 aCheckedMap.insert( sm );
865 //=======================================================================
866 //function : checkMissing
867 //purpose : notify on missing hypothesis
868 // Return false if algo or hipothesis is missing
869 //=======================================================================
871 static bool checkMissing(SMESH_Gen* aGen,
873 SMESH_subMesh* aSubMesh,
874 const int aTopAlgoDim,
876 const bool checkNoAlgo,
877 set<SMESH_subMesh*>& aCheckedMap,
878 list< SMESH_Gen::TAlgoStateError > & theErrors)
880 switch ( aSubMesh->GetSubShape().ShapeType() )
884 case TopAbs_SOLID: break; // check this sub-mesh, it can be meshed
886 return true; // not meshable sub-mesh
888 if ( aCheckedMap.count( aSubMesh ))
892 SMESH_Algo* algo = 0;
894 switch (aSubMesh->GetAlgoState())
896 case SMESH_subMesh::NO_ALGO: {
899 // should there be any algo?
900 int shapeDim = SMESH_Gen::GetShapeDim( aSubMesh->GetSubShape() );
901 if (aTopAlgoDim > shapeDim)
903 MESSAGE( "ERROR: " << shapeDim << "D algorithm is missing" );
905 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
906 theErrors.back().Set( SMESH_Hypothesis::HYP_MISSING, shapeDim, true );
911 case SMESH_subMesh::MISSING_HYP: {
912 // notify if an algo missing hyp is attached to aSubMesh
913 algo = aSubMesh->GetAlgo();
915 bool IsGlobalHypothesis = aGen->IsGlobalHypothesis( algo, aMesh );
916 if (!IsGlobalHypothesis || !globalChecked[ algo->GetDim() ])
918 TAlgoStateErrorName errName = SMESH_Hypothesis::HYP_MISSING;
919 SMESH_Hypothesis::Hypothesis_Status status;
920 algo->CheckHypothesis( aMesh, aSubMesh->GetSubShape(), status );
921 if ( status == SMESH_Hypothesis::HYP_BAD_PARAMETER ) {
922 MESSAGE( "ERROR: hypothesis of " << (IsGlobalHypothesis ? "Global " : "Local ")
923 << "<" << algo->GetName() << "> has a bad parameter value");
925 } else if ( status == SMESH_Hypothesis::HYP_BAD_GEOMETRY ) {
926 MESSAGE( "ERROR: " << (IsGlobalHypothesis ? "Global " : "Local ")
927 << "<" << algo->GetName() << "> assigned to mismatching geometry");
930 MESSAGE( "ERROR: " << (IsGlobalHypothesis ? "Global " : "Local ")
931 << "<" << algo->GetName() << "> misses some hypothesis");
933 if (IsGlobalHypothesis)
934 globalChecked[ algo->GetDim() ] = true;
935 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
936 theErrors.back().Set( errName, algo, IsGlobalHypothesis );
941 case SMESH_subMesh::HYP_OK:
942 algo = aSubMesh->GetAlgo();
944 if (!algo->NeedDiscreteBoundary())
946 SMESH_subMeshIteratorPtr itsub = aSubMesh->getDependsOnIterator( /*includeSelf=*/false,
947 /*complexShapeFirst=*/false);
948 while ( itsub->more() )
949 aCheckedMap.insert( itsub->next() );
955 // do not check under algo that hides sub-algos or
956 // re-start checking NO_ALGO state
958 bool isTopLocalAlgo =
959 ( aTopAlgoDim <= algo->GetDim() && !aGen->IsGlobalHypothesis( algo, aMesh ));
960 if (!algo->NeedDiscreteBoundary() || isTopLocalAlgo)
962 bool checkNoAlgo2 = ( algo->NeedDiscreteBoundary() );
963 SMESH_subMeshIteratorPtr itsub = aSubMesh->getDependsOnIterator( /*includeSelf=*/false,
964 /*complexShapeFirst=*/true);
965 while ( itsub->more() )
967 // sub-meshes should not be checked further more
968 SMESH_subMesh* sm = itsub->next();
972 //check algo on sub-meshes
973 int aTopAlgoDim2 = algo->GetDim();
974 if (!checkMissing (aGen, aMesh, sm, aTopAlgoDim2,
975 globalChecked, checkNoAlgo2, aCheckedMap, theErrors))
978 if (sm->GetAlgoState() == SMESH_subMesh::NO_ALGO )
979 checkNoAlgo2 = false;
982 aCheckedMap.insert( sm );
988 //=======================================================================
989 //function : CheckAlgoState
990 //purpose : notify on bad state of attached algos, return false
991 // if Compute() would fail because of some algo bad state
992 //=======================================================================
994 bool SMESH_Gen::CheckAlgoState(SMESH_Mesh& aMesh, const TopoDS_Shape& aShape)
996 list< TAlgoStateError > errors;
997 return GetAlgoState( aMesh, aShape, errors );
1000 //=======================================================================
1001 //function : GetAlgoState
1002 //purpose : notify on bad state of attached algos, return false
1003 // if Compute() would fail because of some algo bad state
1004 // theErrors list contains problems description
1005 //=======================================================================
1007 bool SMESH_Gen::GetAlgoState(SMESH_Mesh& theMesh,
1008 const TopoDS_Shape& theShape,
1009 list< TAlgoStateError > & theErrors)
1012 bool hasAlgo = false;
1014 SMESH_subMesh* sm = theMesh.GetSubMesh(theShape);
1015 const SMESHDS_Mesh* meshDS = theMesh.GetMeshDS();
1016 TopoDS_Shape mainShape = meshDS->ShapeToMesh();
1018 // -----------------
1020 // -----------------
1022 const SMESH_Algo* aGlobAlgoArr[] = {0,0,0,0};
1024 const list<const SMESHDS_Hypothesis*>& listHyp = meshDS->GetHypothesis( mainShape );
1025 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
1026 for ( ; it != listHyp.end(); it++)
1028 const SMESHDS_Hypothesis * aHyp = *it;
1029 if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
1032 const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
1035 int dim = algo->GetDim();
1036 aGlobAlgoArr[ dim ] = algo;
1041 // --------------------------------------------------------
1042 // info on algos that will be ignored because of ones that
1043 // don't NeedDiscreteBoundary() attached to super-shapes,
1044 // check that a conform mesh will be produced
1045 // --------------------------------------------------------
1048 // find a global algo possibly hiding sub-algos
1050 const SMESH_Algo* aGlobIgnoAlgo = 0;
1051 for (dim = 3; dim > 0; dim--)
1053 if (aGlobAlgoArr[ dim ] &&
1054 !aGlobAlgoArr[ dim ]->NeedDiscreteBoundary() /*&&
1055 !aGlobAlgoArr[ dim ]->SupportSubmeshes()*/ )
1057 aGlobIgnoAlgo = aGlobAlgoArr[ dim ];
1062 set<SMESH_subMesh*> aCheckedSubs;
1063 bool checkConform = ( !theMesh.IsNotConformAllowed() );
1065 // loop on theShape and its sub-shapes
1066 SMESH_subMeshIteratorPtr revItSub = sm->getDependsOnIterator( /*includeSelf=*/true,
1067 /*complexShapeFirst=*/true);
1068 while ( revItSub->more() )
1070 SMESH_subMesh* smToCheck = revItSub->next();
1071 if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
1074 if ( aCheckedSubs.insert( smToCheck ).second ) // not yet checked
1075 if (!checkConformIgnoredAlgos (theMesh, smToCheck, aGlobIgnoAlgo,
1076 0, checkConform, aCheckedSubs, theErrors))
1079 if ( smToCheck->GetAlgoState() != SMESH_subMesh::NO_ALGO )
1083 // ----------------------------------------------------------------
1084 // info on missing hypothesis and find out if all needed algos are
1086 // ----------------------------------------------------------------
1088 // find max dim of global algo
1089 int aTopAlgoDim = 0;
1090 for (dim = 3; dim > 0; dim--)
1092 if (aGlobAlgoArr[ dim ])
1098 bool checkNoAlgo = theMesh.HasShapeToMesh() ? bool( aTopAlgoDim ) : false;
1099 bool globalChecked[] = { false, false, false, false };
1101 // loop on theShape and its sub-shapes
1102 aCheckedSubs.clear();
1103 revItSub = sm->getDependsOnIterator( /*includeSelf=*/true, /*complexShapeFirst=*/true);
1104 while ( revItSub->more() )
1106 SMESH_subMesh* smToCheck = revItSub->next();
1107 if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
1110 if (!checkMissing (this, theMesh, smToCheck, aTopAlgoDim,
1111 globalChecked, checkNoAlgo, aCheckedSubs, theErrors))
1114 if (smToCheck->GetAlgoState() == SMESH_subMesh::NO_ALGO )
1115 checkNoAlgo = false;
1121 theErrors.push_back( TAlgoStateError() );
1122 theErrors.back().Set( SMESH_Hypothesis::HYP_MISSING, theMesh.HasShapeToMesh() ? 1 : 3, true );
1128 //=======================================================================
1129 //function : IsGlobalHypothesis
1130 //purpose : check if theAlgo is attached to the main shape
1131 //=======================================================================
1133 bool SMESH_Gen::IsGlobalHypothesis(const SMESH_Hypothesis* theHyp, SMESH_Mesh& aMesh)
1135 SMESH_HypoFilter filter( SMESH_HypoFilter::Is( theHyp ));
1136 return aMesh.GetHypothesis( aMesh.GetMeshDS()->ShapeToMesh(), filter, false );
1139 //================================================================================
1141 * \brief Return paths to xml files of plugins
1143 //================================================================================
1145 std::vector< std::string > SMESH_Gen::GetPluginXMLPaths()
1147 // Get paths to xml files of plugins
1148 vector< string > xmlPaths;
1150 if ( const char* meshersList = getenv("SMESH_MeshersList") )
1152 string meshers = meshersList, plugin;
1153 string::size_type from = 0, pos;
1154 while ( from < meshers.size() )
1156 // cut off plugin name
1157 pos = meshers.find( env_sep, from );
1158 if ( pos != string::npos )
1159 plugin = meshers.substr( from, pos-from );
1161 plugin = meshers.substr( from ), pos = meshers.size();
1164 // get PLUGIN_ROOT_DIR path
1165 string rootDirVar, pluginSubDir = plugin;
1166 if ( plugin == "StdMeshers" )
1167 rootDirVar = "SMESH", pluginSubDir = "smesh";
1169 for ( pos = 0; pos < plugin.size(); ++pos )
1170 rootDirVar += toupper( plugin[pos] );
1171 rootDirVar += "_ROOT_DIR";
1173 const char* rootDir = getenv( rootDirVar.c_str() );
1174 if ( !rootDir || strlen(rootDir) == 0 )
1176 rootDirVar = plugin + "_ROOT_DIR"; // HexoticPLUGIN_ROOT_DIR
1177 rootDir = getenv( rootDirVar.c_str() );
1178 if ( !rootDir || strlen(rootDir) == 0 ) continue;
1181 // get a separator from rootDir
1182 for ( int i = strlen( rootDir )-1; i >= 0 && sep.empty(); --i )
1183 if ( rootDir[i] == '/' || rootDir[i] == '\\' )
1189 if (sep.empty() ) sep = "\\";
1191 if (sep.empty() ) sep = "/";
1194 // get a path to resource file
1195 string xmlPath = rootDir;
1196 if ( xmlPath[ xmlPath.size()-1 ] != sep[0] )
1198 xmlPath += "share" + sep + "salome" + sep + "resources" + sep;
1199 for ( pos = 0; pos < pluginSubDir.size(); ++pos )
1200 xmlPath += tolower( pluginSubDir[pos] );
1201 xmlPath += sep + plugin + ".xml";
1205 const wchar_t* path = Kernel_Utils::decode_s(xmlPath);
1206 SMESHUtils::ArrayDeleter<const wchar_t> deleter( path );
1208 const char* path = xmlPath.c_str();
1210 fileOK = (GetFileAttributes(path) != INVALID_FILE_ATTRIBUTES);
1213 fileOK = (access(xmlPath.c_str(), F_OK) == 0);
1216 xmlPaths.push_back( xmlPath );
1223 //=============================================================================
1225 * Finds algo to mesh a shape. Optionally returns a shape the found algo is bound to
1227 //=============================================================================
1229 SMESH_Algo *SMESH_Gen::GetAlgo(SMESH_Mesh & aMesh,
1230 const TopoDS_Shape & aShape,
1231 TopoDS_Shape* assignedTo)
1233 return GetAlgo( aMesh.GetSubMesh( aShape ), assignedTo );
1236 //=============================================================================
1238 * Finds algo to mesh a sub-mesh. Optionally returns a shape the found algo is bound to
1240 //=============================================================================
1242 SMESH_Algo *SMESH_Gen::GetAlgo(SMESH_subMesh * aSubMesh,
1243 TopoDS_Shape* assignedTo)
1245 if ( !aSubMesh ) return 0;
1247 const TopoDS_Shape & aShape = aSubMesh->GetSubShape();
1248 SMESH_Mesh& aMesh = *aSubMesh->GetFather();
1250 SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
1251 if ( aMesh.HasShapeToMesh() )
1252 filter.And( filter.IsApplicableTo( aShape ));
1254 typedef SMESH_Algo::Features AlgoData;
1256 TopoDS_Shape assignedToShape;
1258 (SMESH_Algo*) aMesh.GetHypothesis( aSubMesh, filter, true, &assignedToShape );
1261 aShape.ShapeType() == TopAbs_FACE &&
1262 !aShape.IsSame( assignedToShape ) &&
1263 SMESH_MesherHelper::NbAncestors( aShape, aMesh, TopAbs_SOLID ) > 1 )
1265 // Issue 0021559. If there is another 2D algo with different types of output
1266 // elements that can be used to mesh aShape, and 3D algos on adjacent SOLIDs
1267 // have different types of input elements, we choose a most appropriate 2D algo.
1269 // try to find a concurrent 2D algo
1270 filter.AndNot( filter.Is( algo ));
1271 TopoDS_Shape assignedToShape2;
1273 (SMESH_Algo*) aMesh.GetHypothesis( aSubMesh, filter, true, &assignedToShape2 );
1274 if ( algo2 && // algo found
1275 !assignedToShape2.IsSame( aMesh.GetShapeToMesh() ) && // algo is local
1276 ( SMESH_MesherHelper::GetGroupType( assignedToShape2 ) == // algo of the same level
1277 SMESH_MesherHelper::GetGroupType( assignedToShape )) &&
1278 aMesh.IsOrderOK( aMesh.GetSubMesh( assignedToShape2 ), // no forced order
1279 aMesh.GetSubMesh( assignedToShape )))
1281 // get algos on the adjacent SOLIDs
1282 filter.Init( filter.IsAlgo() ).And( filter.HasDim( 3 ));
1283 vector< SMESH_Algo* > algos3D;
1284 PShapeIteratorPtr solidIt = SMESH_MesherHelper::GetAncestors( aShape, aMesh,
1286 while ( const TopoDS_Shape* solid = solidIt->next() )
1287 if ( SMESH_Algo* algo3D = (SMESH_Algo*) aMesh.GetHypothesis( *solid, filter, true ))
1289 algos3D.push_back( algo3D );
1290 filter.AndNot( filter.HasName( algo3D->GetName() ));
1292 // check compatibility of algos
1293 if ( algos3D.size() > 1 )
1295 const AlgoData& algoData = algo->SMESH_Algo::GetFeatures();
1296 const AlgoData& algoData2 = algo2->SMESH_Algo::GetFeatures();
1297 const AlgoData& algoData3d0 = algos3D[0]->SMESH_Algo::GetFeatures();
1298 const AlgoData& algoData3d1 = algos3D[1]->SMESH_Algo::GetFeatures();
1299 if (( algoData2.IsCompatible( algoData3d0 ) &&
1300 algoData2.IsCompatible( algoData3d1 ))
1302 !(algoData.IsCompatible( algoData3d0 ) &&
1303 algoData.IsCompatible( algoData3d1 )))
1309 if ( assignedTo && algo )
1310 * assignedTo = assignedToShape;
1315 //=============================================================================
1317 * Returns StudyContextStruct for a study
1319 //=============================================================================
1321 StudyContextStruct *SMESH_Gen::GetStudyContext()
1323 return _studyContext;
1326 //================================================================================
1328 * \brief Return shape dimension by TopAbs_ShapeEnum
1330 //================================================================================
1332 int SMESH_Gen::GetShapeDim(const TopAbs_ShapeEnum & aShapeType)
1334 static vector<int> dim;
1337 dim.resize( TopAbs_SHAPE, -1 );
1338 dim[ TopAbs_COMPOUND ] = MeshDim_3D;
1339 dim[ TopAbs_COMPSOLID ] = MeshDim_3D;
1340 dim[ TopAbs_SOLID ] = MeshDim_3D;
1341 dim[ TopAbs_SHELL ] = MeshDim_2D;
1342 dim[ TopAbs_FACE ] = MeshDim_2D;
1343 dim[ TopAbs_WIRE ] = MeshDim_1D;
1344 dim[ TopAbs_EDGE ] = MeshDim_1D;
1345 dim[ TopAbs_VERTEX ] = MeshDim_0D;
1347 return dim[ aShapeType ];
1350 //================================================================================
1352 * \brief Return shape dimension by exploding compounds
1354 //================================================================================
1356 int SMESH_Gen::GetFlatShapeDim(const TopoDS_Shape &aShape)
1359 if ( aShape.ShapeType() == TopAbs_COMPOUND ||
1360 aShape.ShapeType() == TopAbs_COMPSOLID )
1362 TopoDS_Iterator it( aShape );
1363 aShapeDim = GetFlatShapeDim( it.Value() );
1366 aShapeDim = GetShapeDim( aShape );
1370 //=============================================================================
1372 * Generate a new id unique within this Gen
1374 //=============================================================================
1376 int SMESH_Gen::GetANewId()