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>
58 #include <Basics_Utils.hxx>
61 #include <boost/filesystem.hpp>
62 #include <boost/asio.hpp>
63 namespace fs = boost::filesystem;
65 // Environment variable separator
74 // a structure used to nullify SMESH_Gen field of SMESH_Hypothesis,
75 // which is needed for SMESH_Hypothesis not deleted before ~SMESH_Gen()
76 struct _Hyp : public SMESH_Hypothesis
84 //================================================================================
86 * \brief Fill a map of shapes with all sub-shape of a sub-mesh
88 //================================================================================
90 TopTools_IndexedMapOfShape * fillAllowed( SMESH_subMesh* sm,
92 TopTools_IndexedMapOfShape * allowedSub )
94 if ( !toFill || !allowedSub )
98 if ( allowedSub->IsEmpty() )
100 allowedSub->ReSize( sm->DependsOn().size() + 1 );
101 allowedSub->Add( sm->GetSubShape() );
102 for ( const auto & key_sm : sm->DependsOn() )
103 allowedSub->Add( key_sm.second->GetSubShape() );
109 //=============================================================================
113 //=============================================================================
115 SMESH_Gen::SMESH_Gen()
117 _studyContext = new StudyContextStruct;
118 _studyContext->myDocument = new SMESHDS_Document();
121 _segmentation = _nbSegments = 10;
122 _compute_canceled = false;
125 //=============================================================================
129 //=============================================================================
131 SMESH_Gen::~SMESH_Gen()
133 std::map < int, SMESH_Hypothesis * >::iterator i_hyp = _studyContext->mapHypothesis.begin();
134 for ( ; i_hyp != _studyContext->mapHypothesis.end(); ++i_hyp )
136 if ( _Hyp* h = static_cast< _Hyp*>( i_hyp->second ))
139 delete _studyContext->myDocument;
140 delete _studyContext;
143 //=============================================================================
145 * Creates a mesh in a study.
146 * if (theIsEmbeddedMode) { mesh modification commands are not logged }
148 //=============================================================================
150 SMESH_Mesh* SMESH_Gen::CreateMesh(bool theIsEmbeddedMode)
152 Unexpect aCatch(SalomeException);
154 // create a new SMESH_mesh object
155 SMESH_Mesh *aMesh = new SMESH_Mesh(_localId++,
158 _studyContext->myDocument);
159 _studyContext->mapMesh[_localId-1] = aMesh;
164 //=============================================================================
166 * Parallel compute of a submesh
167 * This function is used to pass to thread_pool
169 //=============================================================================
170 const std::function<void(int,
172 SMESH_subMesh::compute_event,
175 TopTools_IndexedMapOfShape *,
177 compute_function([&] (int id,
179 SMESH_subMesh::compute_event event,
180 SMESH_subMesh *shapeSM,
182 TopTools_IndexedMapOfShape *allowedSubShapes,
183 TSetOfInt* aShapesId) -> void
185 if (sm->GetComputeState() == SMESH_subMesh::READY_TO_COMPUTE)
187 sm->SetAllowedSubShapes( fillAllowed( shapeSM, aShapeOnly, allowedSubShapes ));
188 //setCurrentSubMesh( sm );
189 sm->ComputeStateEngine(event);
190 //setCurrentSubMesh( nullptr );
191 sm->SetAllowedSubShapes( nullptr );
194 // TODO: Check if this is necessary
196 aShapesId->insert( sm->GetId() );
200 //=============================================================================
204 //=============================================================================
206 bool SMESH_Gen::Compute(SMESH_Mesh & aMesh,
207 const TopoDS_Shape & aShape,
208 const int aFlags /*= COMPACT_MESH*/,
209 const ::MeshDimension aDim /*=::MeshDim_3D*/,
210 TSetOfInt* aShapesId /*=0*/,
211 TopTools_IndexedMapOfShape* anAllowedSubShapes/*=0*/)
215 const bool aShapeOnly = aFlags & SHAPE_ONLY;
216 const bool anUpward = aFlags & UPWARD;
217 const bool aCompactMesh = aFlags & COMPACT_MESH;
221 SMESH_subMesh *sm, *shapeSM = aMesh.GetSubMesh(aShape);
223 const bool includeSelf = true;
224 const bool complexShapeFirst = true;
225 const int globalAlgoDim = 100;
227 // Pool of thread for computation
228 if(aMesh.IsParallel())
229 aMesh.InitPoolThreads();
231 SMESH_subMeshIteratorPtr smIt;
233 // Fix of Issue 22150. Due to !BLSURF->OnlyUnaryInput(), BLSURF computes edges
234 // that must be computed by Projection 1D-2D while the Projection asks to compute
236 SMESH_subMesh::compute_event computeEvent =
237 aShapeOnly ? SMESH_subMesh::COMPUTE_SUBMESH : SMESH_subMesh::COMPUTE;
238 if ( !aMesh.HasShapeToMesh() )
239 computeEvent = SMESH_subMesh::COMPUTE_NOGEOM; // if several algos and no geometry
241 TopTools_IndexedMapOfShape *allowedSubShapes = anAllowedSubShapes, allowedSub;
242 if ( aShapeOnly && !allowedSubShapes )
243 allowedSubShapes = &allowedSub;
245 if ( anUpward ) // is called from the below code in this method
247 // ===============================================
248 // Mesh all the sub-shapes starting from vertices
249 // ===============================================
251 TopAbs_ShapeEnum previousShapeType = TopAbs_VERTEX;
252 int nbThreads = aMesh.GetNbThreads();
253 auto begin = std::chrono::high_resolution_clock::now();
254 std::cout << "Running mesh with threads: " << nbThreads << " mesher: " << aMesh.GetMesherNbThreads() << std::endl;
257 smIt = shapeSM->getDependsOnIterator(includeSelf, !complexShapeFirst);
258 while ( smIt->more() )
260 SMESH_subMesh* smToCompute = smIt->next();
262 // do not mesh vertices of a pseudo shape
263 const TopoDS_Shape& shape = smToCompute->GetSubShape();
264 const TopAbs_ShapeEnum shapeType = shape.ShapeType();
265 if ( !aMesh.HasShapeToMesh() && shapeType == TopAbs_VERTEX )
267 if(shapeType==TopAbs_FACE||shapeType==TopAbs_EDGE)
268 aMesh.SetNbThreads(0);
270 aMesh.SetNbThreads(nbThreads);
271 //DEBUG std::cout << "Shape Type" << shapeType << " previous" << previousShapeType << std::endl;
272 if ((aMesh.IsParallel()||nbThreads!=0) && shapeType != previousShapeType) {
273 // Waiting for all threads for the previous type to end
276 std::string file_name;
277 switch(previousShapeType){
279 file_name = "Mesh2D.med";
282 file_name = "Mesh1D.med";
285 file_name = "Mesh0D.med";
294 fs::path mesh_file = fs::path(aMesh.tmp_folder) / fs::path(file_name);
295 // TODO: change mesh name
296 exportMesh(mesh_file.string(), aMesh, "Maillage_1");
299 //Resetting threaded pool info
300 previousShapeType = shapeType;
303 // check for preview dimension limitations
304 if ( aShapesId && GetShapeDim( shapeType ) > (int)aDim )
306 // clear compute state not to show previous compute errors
307 // if preview invoked less dimension less than previous
308 smToCompute->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
311 if(aMesh.IsParallel())
313 boost::asio::post(*(aMesh._pool), std::bind(compute_function, 1, smToCompute, computeEvent,
314 shapeSM, aShapeOnly, allowedSubShapes,
317 auto begin2 = std::chrono::high_resolution_clock::now();
319 compute_function(1 ,smToCompute, computeEvent,
320 shapeSM, aShapeOnly, allowedSubShapes,
323 if (smToCompute->GetComputeState() == SMESH_subMesh::FAILED_TO_COMPUTE &&
324 ( shapeType != TopAbs_EDGE || !SMESH_Algo::isDegenerated( TopoDS::Edge( shape ))))
326 else if ( aShapesId )
327 aShapesId->insert( smToCompute->GetId() );
331 // TODO: Check error handling in parallel mode
332 if(aMesh.IsParallel()){
333 // Waiting for the thread for Solids to finish
337 aMesh.GetMeshDS()->Modified();
338 auto end = std::chrono::high_resolution_clock::now();
339 auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
340 std::cout << "Time for All: " << elapsed.count()*1e-9 << std::endl;
342 // Pool of thread for computation
343 if(aMesh.IsParallel())
344 aMesh.DeletePoolThreads();
350 // ================================================================
351 // Apply algos that do NOT require discretized boundaries
352 // ("all-dimensional") and do NOT support sub-meshes, starting from
353 // the most complex shapes and collect sub-meshes with algos that
354 // DO support sub-meshes
355 // ================================================================
356 auto begin = std::chrono::high_resolution_clock::now();
357 list< SMESH_subMesh* > smWithAlgoSupportingSubmeshes[4]; // for each dim
359 // map to sort sm with same dim algos according to dim of
360 // the shape the algo assigned to (issue 0021217).
361 // Other issues influenced the algo applying order:
362 // 21406, 21556, 21893, 20206
363 multimap< int, SMESH_subMesh* > shDim2sm;
364 multimap< int, SMESH_subMesh* >::reverse_iterator shDim2smIt;
365 TopoDS_Shape algoShape;
366 int prevShapeDim = -1, aShapeDim;
368 smIt = shapeSM->getDependsOnIterator(includeSelf, complexShapeFirst);
369 while ( smIt->more() )
371 SMESH_subMesh* smToCompute = smIt->next();
372 if ( smToCompute->GetComputeState() != SMESH_subMesh::READY_TO_COMPUTE )
375 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
376 aShapeDim = GetShapeDim( aSubShape );
377 if ( aShapeDim < 1 ) break;
379 // check for preview dimension limitations
380 if ( aShapesId && aShapeDim > (int)aDim )
383 SMESH_Algo* algo = GetAlgo( smToCompute, &algoShape );
384 if ( algo && !algo->NeedDiscreteBoundary() )
386 if ( algo->SupportSubmeshes() )
388 // reload sub-meshes from shDim2sm into smWithAlgoSupportingSubmeshes
389 // so that more local algos to go first
390 if ( prevShapeDim != aShapeDim )
392 prevShapeDim = aShapeDim;
393 for ( shDim2smIt = shDim2sm.rbegin(); shDim2smIt != shDim2sm.rend(); ++shDim2smIt )
394 if ( shDim2smIt->first == globalAlgoDim )
395 smWithAlgoSupportingSubmeshes[ aShapeDim ].push_back( shDim2smIt->second );
397 smWithAlgoSupportingSubmeshes[ aShapeDim ].push_front( shDim2smIt->second );
400 // add smToCompute to shDim2sm map
401 if ( algoShape.IsSame( aMesh.GetShapeToMesh() ))
403 aShapeDim = globalAlgoDim; // to compute last
407 aShapeDim = GetShapeDim( algoShape );
408 if ( algoShape.ShapeType() == TopAbs_COMPOUND )
410 TopoDS_Iterator it( algoShape );
411 aShapeDim += GetShapeDim( it.Value() );
414 shDim2sm.insert( make_pair( aShapeDim, smToCompute ));
416 else // Compute w/o support of sub-meshes
418 if (_compute_canceled)
420 smToCompute->SetAllowedSubShapes( fillAllowed( shapeSM, aShapeOnly, allowedSubShapes ));
421 setCurrentSubMesh( smToCompute );
422 smToCompute->ComputeStateEngine( computeEvent );
423 setCurrentSubMesh( nullptr );
424 smToCompute->SetAllowedSubShapes( nullptr );
426 aShapesId->insert( smToCompute->GetId() );
430 // reload sub-meshes from shDim2sm into smWithAlgoSupportingSubmeshes
431 for ( shDim2smIt = shDim2sm.rbegin(); shDim2smIt != shDim2sm.rend(); ++shDim2smIt )
432 if ( shDim2smIt->first == globalAlgoDim )
433 smWithAlgoSupportingSubmeshes[3].push_back( shDim2smIt->second );
435 smWithAlgoSupportingSubmeshes[0].push_front( shDim2smIt->second );
437 // ======================================================
438 // Apply all-dimensional algorithms supporing sub-meshes
439 // ======================================================
441 std::vector< SMESH_subMesh* > smVec;
442 for ( aShapeDim = 0; aShapeDim < 4; ++aShapeDim )
443 smVec.insert( smVec.end(),
444 smWithAlgoSupportingSubmeshes[aShapeDim].begin(),
445 smWithAlgoSupportingSubmeshes[aShapeDim].end() );
447 // gather sub-shapes with local uni-dimensional algos (bos #29143)
448 // ----------------------------------------------------------------
449 TopTools_MapOfShape uniDimAlgoShapes;
450 if ( !smVec.empty() )
452 ShapeToHypothesis::Iterator s2hyps( aMesh.GetMeshDS()->GetHypotheses() );
453 for ( ; s2hyps.More(); s2hyps.Next() )
455 const TopoDS_Shape& s = s2hyps.Key();
456 if ( s.IsSame( aMesh.GetShapeToMesh() ))
458 for ( auto & hyp : s2hyps.Value() )
460 if ( const SMESH_Algo* algo = dynamic_cast< const SMESH_Algo*>( hyp ))
461 if ( algo->NeedDiscreteBoundary() )
463 TopAbs_ShapeEnum sType;
464 switch ( algo->GetDim() ) {
465 case 3: sType = TopAbs_SOLID; break;
466 case 2: sType = TopAbs_FACE; break;
467 default: sType = TopAbs_EDGE; break;
469 for ( TopExp_Explorer ex( s2hyps.Key(), sType ); ex.More(); ex.Next() )
470 uniDimAlgoShapes.Add( ex.Current() );
477 // ------------------------------------------------
478 // sort list of sub-meshes according to mesh order
479 // ------------------------------------------------
480 aMesh.SortByMeshOrder( smVec );
482 // ------------------------------------------------------------
483 // compute sub-meshes with local uni-dimensional algos under
484 // sub-meshes with all-dimensional algos
485 // ------------------------------------------------------------
486 // start from lower shapes
487 for ( size_t i = 0; i < smVec.size(); ++i )
490 if ( sm->GetComputeState() != SMESH_subMesh::READY_TO_COMPUTE)
493 const TopAbs_ShapeEnum shapeType = sm->GetSubShape().ShapeType();
495 if ( !uniDimAlgoShapes.IsEmpty() )
497 // get a shape the algo is assigned to
498 if ( !GetAlgo( sm, & algoShape ))
499 continue; // strange...
501 // look for more local algos
502 if ( SMESH_subMesh* algoSM = aMesh.GetSubMesh( algoShape ))
503 smIt = algoSM->getDependsOnIterator(!includeSelf, !complexShapeFirst);
505 smIt = sm->getDependsOnIterator(!includeSelf, !complexShapeFirst);
507 while ( smIt->more() )
509 SMESH_subMesh* smToCompute = smIt->next();
511 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
512 const int aShapeDim = GetShapeDim( aSubShape );
513 if ( aShapeDim < 1 || aSubShape.ShapeType() <= shapeType )
515 if ( !uniDimAlgoShapes.Contains( aSubShape ))
516 continue; // [bos #29143] aMesh.GetHypothesis() is too long
518 // check for preview dimension limitations
519 if ( aShapesId && GetShapeDim( aSubShape.ShapeType() ) > (int)aDim )
522 SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
524 .And( SMESH_HypoFilter::IsApplicableTo( aSubShape ))
525 .And( SMESH_HypoFilter::IsMoreLocalThan( algoShape, aMesh ));
527 if ( SMESH_Algo* subAlgo = (SMESH_Algo*) aMesh.GetHypothesis( smToCompute, filter, true))
529 if ( ! subAlgo->NeedDiscreteBoundary() ) continue;
530 TopTools_IndexedMapOfShape* localAllowed = allowedSubShapes;
531 if ( localAllowed && localAllowed->IsEmpty() )
532 localAllowed = 0; // prevent fillAllowed() with aSubShape
534 SMESH_Hypothesis::Hypothesis_Status status;
535 if ( subAlgo->CheckHypothesis( aMesh, aSubShape, status ))
536 // mesh a lower smToCompute starting from vertices
537 Compute( aMesh, aSubShape, aFlags | SHAPE_ONLY_UPWARD, aDim, aShapesId, localAllowed );
541 // --------------------------------
542 // apply the all-dimensional algo
543 // --------------------------------
545 if (_compute_canceled)
547 // check for preview dimension limitations
548 if ( aShapesId && GetShapeDim( shapeType ) > (int)aDim )
550 sm->SetAllowedSubShapes( fillAllowed( shapeSM, aShapeOnly, allowedSubShapes ));
551 setCurrentSubMesh( sm );
552 auto begin = std::chrono::high_resolution_clock::now();
553 sm->ComputeStateEngine( computeEvent );
554 auto end = std::chrono::high_resolution_clock::now();
555 auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
556 std::cout << "Time for seq:alldim:compute: " << elapsed.count()*1e-9 << std::endl;
558 setCurrentSubMesh( NULL );
559 sm->SetAllowedSubShapes( nullptr );
561 aShapesId->insert( sm->GetId() );
566 // -----------------------------------------------
567 // mesh the rest sub-shapes starting from vertices
568 // -----------------------------------------------
569 ret = Compute( aMesh, aShape, aFlags | UPWARD, aDim, aShapesId, allowedSubShapes );
570 auto end = std::chrono::high_resolution_clock::now();
571 auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
572 std::cout << "Time for All: " << elapsed.count()*1e-9 << std::endl;
578 // fix quadratic mesh by bending iternal links near concave boundary
579 if ( aCompactMesh && // a final compute
580 aShape.IsSame( aMesh.GetShapeToMesh() ) &&
581 !aShapesId && // not preview
582 ret ) // everything is OK
584 SMESH_MesherHelper aHelper( aMesh );
585 if ( aHelper.IsQuadraticMesh() != SMESH_MesherHelper::LINEAR )
587 aHelper.FixQuadraticElements( shapeSM->GetComputeError() );
593 aMesh.GetMeshDS()->Modified();
594 aMesh.GetMeshDS()->CompactMesh();
599 //=============================================================================
601 * Prepare Compute a mesh
603 //=============================================================================
604 void SMESH_Gen::PrepareCompute(SMESH_Mesh & /*aMesh*/,
605 const TopoDS_Shape & /*aShape*/)
607 _compute_canceled = false;
608 resetCurrentSubMesh();
611 //=============================================================================
613 * Cancel Compute a mesh
615 //=============================================================================
616 void SMESH_Gen::CancelCompute(SMESH_Mesh & /*aMesh*/,
617 const TopoDS_Shape & /*aShape*/)
619 _compute_canceled = true;
620 if ( const SMESH_subMesh* sm = GetCurrentSubMesh() )
622 const_cast< SMESH_subMesh* >( sm )->ComputeStateEngine( SMESH_subMesh::COMPUTE_CANCELED );
624 resetCurrentSubMesh();
627 //================================================================================
629 * \brief Returns a sub-mesh being currently computed
631 //================================================================================
633 const SMESH_subMesh* SMESH_Gen::GetCurrentSubMesh() const
635 return _sm_current.empty() ? 0 : _sm_current.back();
638 //================================================================================
640 * \brief Sets a sub-mesh being currently computed.
642 * An algorithm can call Compute() for a sub-shape, hence we keep a stack of sub-meshes
644 //================================================================================
646 void SMESH_Gen::setCurrentSubMesh(SMESH_subMesh* sm)
649 _sm_current.push_back( sm );
651 else if ( !_sm_current.empty() )
652 _sm_current.pop_back();
655 void SMESH_Gen::resetCurrentSubMesh()
660 //=============================================================================
664 //=============================================================================
666 bool SMESH_Gen::Evaluate(SMESH_Mesh & aMesh,
667 const TopoDS_Shape & aShape,
668 MapShapeNbElems& aResMap,
670 TSetOfInt* aShapesId)
674 SMESH_subMesh *sm = aMesh.GetSubMesh(aShape);
676 const bool includeSelf = true;
677 const bool complexShapeFirst = true;
678 SMESH_subMeshIteratorPtr smIt;
680 if ( anUpward ) { // is called from below code here
681 // -----------------------------------------------
682 // mesh all the sub-shapes starting from vertices
683 // -----------------------------------------------
684 smIt = sm->getDependsOnIterator(includeSelf, !complexShapeFirst);
685 while ( smIt->more() ) {
686 SMESH_subMesh* smToCompute = smIt->next();
688 // do not mesh vertices of a pseudo shape
689 const TopAbs_ShapeEnum shapeType = smToCompute->GetSubShape().ShapeType();
690 //if ( !aMesh.HasShapeToMesh() && shapeType == TopAbs_VERTEX )
692 if ( !aMesh.HasShapeToMesh() ) {
693 if( shapeType == TopAbs_VERTEX || shapeType == TopAbs_WIRE ||
694 shapeType == TopAbs_SHELL )
698 smToCompute->Evaluate(aResMap);
700 aShapesId->insert( smToCompute->GetId() );
705 // -----------------------------------------------------------------
706 // apply algos that DO NOT require Discreteized boundaries and DO NOT
707 // support sub-meshes, starting from the most complex shapes
708 // and collect sub-meshes with algos that DO support sub-meshes
709 // -----------------------------------------------------------------
710 list< SMESH_subMesh* > smWithAlgoSupportingSubmeshes;
711 smIt = sm->getDependsOnIterator(includeSelf, complexShapeFirst);
712 while ( smIt->more() ) {
713 SMESH_subMesh* smToCompute = smIt->next();
714 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
715 const int aShapeDim = GetShapeDim( aSubShape );
716 if ( aShapeDim < 1 ) break;
718 SMESH_Algo* algo = GetAlgo( smToCompute );
719 if ( algo && !algo->NeedDiscreteBoundary() ) {
720 if ( algo->SupportSubmeshes() ) {
721 smWithAlgoSupportingSubmeshes.push_front( smToCompute );
724 smToCompute->Evaluate(aResMap);
726 aShapesId->insert( smToCompute->GetId() );
731 // ------------------------------------------------------------
732 // sort list of meshes according to mesh order
733 // ------------------------------------------------------------
734 std::vector< SMESH_subMesh* > smVec( smWithAlgoSupportingSubmeshes.begin(),
735 smWithAlgoSupportingSubmeshes.end() );
736 aMesh.SortByMeshOrder( smVec );
738 // ------------------------------------------------------------
739 // compute sub-meshes under shapes with algos that DO NOT require
740 // Discreteized boundaries and DO support sub-meshes
741 // ------------------------------------------------------------
742 // start from lower shapes
743 for ( size_t i = 0; i < smVec.size(); ++i )
747 // get a shape the algo is assigned to
748 TopoDS_Shape algoShape;
749 if ( !GetAlgo( sm, & algoShape ))
750 continue; // strange...
752 // look for more local algos
753 smIt = sm->getDependsOnIterator(!includeSelf, !complexShapeFirst);
754 while ( smIt->more() ) {
755 SMESH_subMesh* smToCompute = smIt->next();
757 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
758 const int aShapeDim = GetShapeDim( aSubShape );
759 if ( aShapeDim < 1 ) continue;
761 SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
763 .And( SMESH_HypoFilter::IsApplicableTo( aSubShape ))
764 .And( SMESH_HypoFilter::IsMoreLocalThan( algoShape, aMesh ));
766 if ( SMESH_Algo* subAlgo = (SMESH_Algo*) aMesh.GetHypothesis( smToCompute, filter, true ))
768 if ( ! subAlgo->NeedDiscreteBoundary() ) continue;
769 SMESH_Hypothesis::Hypothesis_Status status;
770 if ( subAlgo->CheckHypothesis( aMesh, aSubShape, status ))
771 // mesh a lower smToCompute starting from vertices
772 Evaluate( aMesh, aSubShape, aResMap, /*anUpward=*/true, aShapesId );
776 // ----------------------------------------------------------
777 // apply the algos that do not require Discreteized boundaries
778 // ----------------------------------------------------------
779 for ( size_t i = 0; i < smVec.size(); ++i )
782 sm->Evaluate(aResMap);
784 aShapesId->insert( sm->GetId() );
787 // -----------------------------------------------
788 // mesh the rest sub-shapes starting from vertices
789 // -----------------------------------------------
790 ret = Evaluate( aMesh, aShape, aResMap, /*anUpward=*/true, aShapesId );
797 //=======================================================================
798 //function : checkConformIgnoredAlgos
800 //=======================================================================
802 static bool checkConformIgnoredAlgos(SMESH_Mesh& aMesh,
803 SMESH_subMesh* aSubMesh,
804 const SMESH_Algo* aGlobIgnoAlgo,
805 const SMESH_Algo* aLocIgnoAlgo,
807 set<SMESH_subMesh*>& aCheckedMap,
808 list< SMESH_Gen::TAlgoStateError > & theErrors)
811 if ( aSubMesh->GetSubShape().ShapeType() == TopAbs_VERTEX)
817 const list<const SMESHDS_Hypothesis*>& listHyp =
818 aMesh.GetMeshDS()->GetHypothesis( aSubMesh->GetSubShape() );
819 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
820 for ( ; it != listHyp.end(); it++)
822 const SMESHDS_Hypothesis * aHyp = *it;
823 if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
826 const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
829 if ( aLocIgnoAlgo ) // algo is hidden by a local algo of upper dim
831 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
832 theErrors.back().Set( SMESH_Hypothesis::HYP_HIDDEN_ALGO, algo, false );
833 INFOS( "Local <" << algo->GetName() << "> is hidden by local <"
834 << aLocIgnoAlgo->GetName() << ">");
838 bool isGlobal = (aMesh.IsMainShape( aSubMesh->GetSubShape() ));
839 int dim = algo->GetDim();
840 int aMaxGlobIgnoDim = ( aGlobIgnoAlgo ? aGlobIgnoAlgo->GetDim() : -1 );
841 bool isNeededDim = ( aGlobIgnoAlgo ? aGlobIgnoAlgo->NeedLowerHyps( dim ) : false );
843 if (( dim < aMaxGlobIgnoDim && !isNeededDim ) &&
844 ( isGlobal || !aGlobIgnoAlgo->SupportSubmeshes() ))
846 // algo is hidden by a global algo
847 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
848 theErrors.back().Set( SMESH_Hypothesis::HYP_HIDDEN_ALGO, algo, true );
849 INFOS( ( isGlobal ? "Global" : "Local" )
850 << " <" << algo->GetName() << "> is hidden by global <"
851 << aGlobIgnoAlgo->GetName() << ">");
853 else if ( !algo->NeedDiscreteBoundary() && !isGlobal)
855 // local algo is not hidden and hides algos on sub-shapes
856 if (checkConform && !aSubMesh->IsConform( algo ))
859 checkConform = false; // no more check conformity
860 INFOS( "ERROR: Local <" << algo->GetName() <<
861 "> would produce not conform mesh: "
862 "<Not Conform Mesh Allowed> hypothesis is missing");
863 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
864 theErrors.back().Set( SMESH_Hypothesis::HYP_NOTCONFORM, algo, false );
867 // sub-algos will be hidden by a local <algo> if <algo> does not support sub-meshes
868 if ( algo->SupportSubmeshes() )
870 SMESH_subMeshIteratorPtr revItSub =
871 aSubMesh->getDependsOnIterator( /*includeSelf=*/false, /*complexShapeFirst=*/true);
872 bool checkConform2 = false;
873 while ( revItSub->more() )
875 SMESH_subMesh* sm = revItSub->next();
876 checkConformIgnoredAlgos (aMesh, sm, aGlobIgnoAlgo,
877 algo, checkConform2, aCheckedMap, theErrors);
878 aCheckedMap.insert( sm );
887 //=======================================================================
888 //function : checkMissing
889 //purpose : notify on missing hypothesis
890 // Return false if algo or hipothesis is missing
891 //=======================================================================
893 static bool checkMissing(SMESH_Gen* aGen,
895 SMESH_subMesh* aSubMesh,
896 const int aTopAlgoDim,
898 const bool checkNoAlgo,
899 set<SMESH_subMesh*>& aCheckedMap,
900 list< SMESH_Gen::TAlgoStateError > & theErrors)
902 switch ( aSubMesh->GetSubShape().ShapeType() )
906 case TopAbs_SOLID: break; // check this sub-mesh, it can be meshed
908 return true; // not meshable sub-mesh
910 if ( aCheckedMap.count( aSubMesh ))
914 SMESH_Algo* algo = 0;
916 switch (aSubMesh->GetAlgoState())
918 case SMESH_subMesh::NO_ALGO: {
921 // should there be any algo?
922 int shapeDim = SMESH_Gen::GetShapeDim( aSubMesh->GetSubShape() );
923 if (aTopAlgoDim > shapeDim)
925 MESSAGE( "ERROR: " << shapeDim << "D algorithm is missing" );
927 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
928 theErrors.back().Set( SMESH_Hypothesis::HYP_MISSING, shapeDim, true );
933 case SMESH_subMesh::MISSING_HYP: {
934 // notify if an algo missing hyp is attached to aSubMesh
935 algo = aSubMesh->GetAlgo();
937 bool IsGlobalHypothesis = aGen->IsGlobalHypothesis( algo, aMesh );
938 if (!IsGlobalHypothesis || !globalChecked[ algo->GetDim() ])
940 TAlgoStateErrorName errName = SMESH_Hypothesis::HYP_MISSING;
941 SMESH_Hypothesis::Hypothesis_Status status;
942 algo->CheckHypothesis( aMesh, aSubMesh->GetSubShape(), status );
943 if ( status == SMESH_Hypothesis::HYP_BAD_PARAMETER ) {
944 MESSAGE( "ERROR: hypothesis of " << (IsGlobalHypothesis ? "Global " : "Local ")
945 << "<" << algo->GetName() << "> has a bad parameter value");
947 } else if ( status == SMESH_Hypothesis::HYP_BAD_GEOMETRY ) {
948 MESSAGE( "ERROR: " << (IsGlobalHypothesis ? "Global " : "Local ")
949 << "<" << algo->GetName() << "> assigned to mismatching geometry");
952 MESSAGE( "ERROR: " << (IsGlobalHypothesis ? "Global " : "Local ")
953 << "<" << algo->GetName() << "> misses some hypothesis");
955 if (IsGlobalHypothesis)
956 globalChecked[ algo->GetDim() ] = true;
957 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
958 theErrors.back().Set( errName, algo, IsGlobalHypothesis );
963 case SMESH_subMesh::HYP_OK:
964 algo = aSubMesh->GetAlgo();
966 if (!algo->NeedDiscreteBoundary())
968 SMESH_subMeshIteratorPtr itsub = aSubMesh->getDependsOnIterator( /*includeSelf=*/false,
969 /*complexShapeFirst=*/false);
970 while ( itsub->more() )
971 aCheckedMap.insert( itsub->next() );
977 // do not check under algo that hides sub-algos or
978 // re-start checking NO_ALGO state
980 bool isTopLocalAlgo =
981 ( aTopAlgoDim <= algo->GetDim() && !aGen->IsGlobalHypothesis( algo, aMesh ));
982 if (!algo->NeedDiscreteBoundary() || isTopLocalAlgo)
984 bool checkNoAlgo2 = ( algo->NeedDiscreteBoundary() );
985 SMESH_subMeshIteratorPtr itsub = aSubMesh->getDependsOnIterator( /*includeSelf=*/false,
986 /*complexShapeFirst=*/true);
987 while ( itsub->more() )
989 // sub-meshes should not be checked further more
990 SMESH_subMesh* sm = itsub->next();
994 //check algo on sub-meshes
995 int aTopAlgoDim2 = algo->GetDim();
996 if (!checkMissing (aGen, aMesh, sm, aTopAlgoDim2,
997 globalChecked, checkNoAlgo2, aCheckedMap, theErrors))
1000 if (sm->GetAlgoState() == SMESH_subMesh::NO_ALGO )
1001 checkNoAlgo2 = false;
1004 aCheckedMap.insert( sm );
1010 //=======================================================================
1011 //function : CheckAlgoState
1012 //purpose : notify on bad state of attached algos, return false
1013 // if Compute() would fail because of some algo bad state
1014 //=======================================================================
1016 bool SMESH_Gen::CheckAlgoState(SMESH_Mesh& aMesh, const TopoDS_Shape& aShape)
1018 list< TAlgoStateError > errors;
1019 return GetAlgoState( aMesh, aShape, errors );
1022 //=======================================================================
1023 //function : GetAlgoState
1024 //purpose : notify on bad state of attached algos, return false
1025 // if Compute() would fail because of some algo bad state
1026 // theErrors list contains problems description
1027 //=======================================================================
1029 bool SMESH_Gen::GetAlgoState(SMESH_Mesh& theMesh,
1030 const TopoDS_Shape& theShape,
1031 list< TAlgoStateError > & theErrors)
1034 bool hasAlgo = false;
1036 SMESH_subMesh* sm = theMesh.GetSubMesh(theShape);
1037 const SMESHDS_Mesh* meshDS = theMesh.GetMeshDS();
1038 TopoDS_Shape mainShape = meshDS->ShapeToMesh();
1040 // -----------------
1042 // -----------------
1044 const SMESH_Algo* aGlobAlgoArr[] = {0,0,0,0};
1046 const list<const SMESHDS_Hypothesis*>& listHyp = meshDS->GetHypothesis( mainShape );
1047 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
1048 for ( ; it != listHyp.end(); it++)
1050 const SMESHDS_Hypothesis * aHyp = *it;
1051 if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
1054 const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
1057 int dim = algo->GetDim();
1058 aGlobAlgoArr[ dim ] = algo;
1063 // --------------------------------------------------------
1064 // info on algos that will be ignored because of ones that
1065 // don't NeedDiscreteBoundary() attached to super-shapes,
1066 // check that a conform mesh will be produced
1067 // --------------------------------------------------------
1070 // find a global algo possibly hiding sub-algos
1072 const SMESH_Algo* aGlobIgnoAlgo = 0;
1073 for (dim = 3; dim > 0; dim--)
1075 if (aGlobAlgoArr[ dim ] &&
1076 !aGlobAlgoArr[ dim ]->NeedDiscreteBoundary() /*&&
1077 !aGlobAlgoArr[ dim ]->SupportSubmeshes()*/ )
1079 aGlobIgnoAlgo = aGlobAlgoArr[ dim ];
1084 set<SMESH_subMesh*> aCheckedSubs;
1085 bool checkConform = ( !theMesh.IsNotConformAllowed() );
1087 // loop on theShape and its sub-shapes
1088 SMESH_subMeshIteratorPtr revItSub = sm->getDependsOnIterator( /*includeSelf=*/true,
1089 /*complexShapeFirst=*/true);
1090 while ( revItSub->more() )
1092 SMESH_subMesh* smToCheck = revItSub->next();
1093 if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
1096 if ( aCheckedSubs.insert( smToCheck ).second ) // not yet checked
1097 if (!checkConformIgnoredAlgos (theMesh, smToCheck, aGlobIgnoAlgo,
1098 0, checkConform, aCheckedSubs, theErrors))
1101 if ( smToCheck->GetAlgoState() != SMESH_subMesh::NO_ALGO )
1105 // ----------------------------------------------------------------
1106 // info on missing hypothesis and find out if all needed algos are
1108 // ----------------------------------------------------------------
1110 // find max dim of global algo
1111 int aTopAlgoDim = 0;
1112 for (dim = 3; dim > 0; dim--)
1114 if (aGlobAlgoArr[ dim ])
1120 bool checkNoAlgo = theMesh.HasShapeToMesh() ? bool( aTopAlgoDim ) : false;
1121 bool globalChecked[] = { false, false, false, false };
1123 // loop on theShape and its sub-shapes
1124 aCheckedSubs.clear();
1125 revItSub = sm->getDependsOnIterator( /*includeSelf=*/true, /*complexShapeFirst=*/true);
1126 while ( revItSub->more() )
1128 SMESH_subMesh* smToCheck = revItSub->next();
1129 if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
1132 if (!checkMissing (this, theMesh, smToCheck, aTopAlgoDim,
1133 globalChecked, checkNoAlgo, aCheckedSubs, theErrors))
1136 if (smToCheck->GetAlgoState() == SMESH_subMesh::NO_ALGO )
1137 checkNoAlgo = false;
1143 theErrors.push_back( TAlgoStateError() );
1144 theErrors.back().Set( SMESH_Hypothesis::HYP_MISSING, theMesh.HasShapeToMesh() ? 1 : 3, true );
1150 //=======================================================================
1151 //function : IsGlobalHypothesis
1152 //purpose : check if theAlgo is attached to the main shape
1153 //=======================================================================
1155 bool SMESH_Gen::IsGlobalHypothesis(const SMESH_Hypothesis* theHyp, SMESH_Mesh& aMesh)
1157 SMESH_HypoFilter filter( SMESH_HypoFilter::Is( theHyp ));
1158 return aMesh.GetHypothesis( aMesh.GetMeshDS()->ShapeToMesh(), filter, false );
1161 //================================================================================
1163 * \brief Return paths to xml files of plugins
1165 //================================================================================
1167 std::vector< std::string > SMESH_Gen::GetPluginXMLPaths()
1169 // Get paths to xml files of plugins
1170 vector< string > xmlPaths;
1172 if ( const char* meshersList = getenv("SMESH_MeshersList") )
1174 string meshers = meshersList, plugin;
1175 string::size_type from = 0, pos;
1176 while ( from < meshers.size() )
1178 // cut off plugin name
1179 pos = meshers.find( env_sep, from );
1180 if ( pos != string::npos )
1181 plugin = meshers.substr( from, pos-from );
1183 plugin = meshers.substr( from ), pos = meshers.size();
1186 // get PLUGIN_ROOT_DIR path
1187 string rootDirVar, pluginSubDir = plugin;
1188 if ( plugin == "StdMeshers" )
1189 rootDirVar = "SMESH", pluginSubDir = "smesh";
1191 for ( pos = 0; pos < plugin.size(); ++pos )
1192 rootDirVar += toupper( plugin[pos] );
1193 rootDirVar += "_ROOT_DIR";
1195 const char* rootDir = getenv( rootDirVar.c_str() );
1196 if ( !rootDir || strlen(rootDir) == 0 )
1198 rootDirVar = plugin + "_ROOT_DIR"; // HexoticPLUGIN_ROOT_DIR
1199 rootDir = getenv( rootDirVar.c_str() );
1200 if ( !rootDir || strlen(rootDir) == 0 ) continue;
1203 // get a separator from rootDir
1204 for ( int i = strlen( rootDir )-1; i >= 0 && sep.empty(); --i )
1205 if ( rootDir[i] == '/' || rootDir[i] == '\\' )
1211 if (sep.empty() ) sep = "\\";
1213 if (sep.empty() ) sep = "/";
1216 // get a path to resource file
1217 string xmlPath = rootDir;
1218 if ( xmlPath[ xmlPath.size()-1 ] != sep[0] )
1220 xmlPath += "share" + sep + "salome" + sep + "resources" + sep;
1221 for ( pos = 0; pos < pluginSubDir.size(); ++pos )
1222 xmlPath += tolower( pluginSubDir[pos] );
1223 xmlPath += sep + plugin + ".xml";
1227 const wchar_t* path = Kernel_Utils::decode_s(xmlPath);
1228 SMESHUtils::ArrayDeleter<const wchar_t> deleter( path );
1230 const char* path = xmlPath.c_str();
1232 fileOK = (GetFileAttributes(path) != INVALID_FILE_ATTRIBUTES);
1235 fileOK = (access(xmlPath.c_str(), F_OK) == 0);
1238 xmlPaths.push_back( xmlPath );
1245 //=============================================================================
1247 * Finds algo to mesh a shape. Optionally returns a shape the found algo is bound to
1249 //=============================================================================
1251 SMESH_Algo *SMESH_Gen::GetAlgo(SMESH_Mesh & aMesh,
1252 const TopoDS_Shape & aShape,
1253 TopoDS_Shape* assignedTo)
1255 return GetAlgo( aMesh.GetSubMesh( aShape ), assignedTo );
1258 //=============================================================================
1260 * Finds algo to mesh a sub-mesh. Optionally returns a shape the found algo is bound to
1262 //=============================================================================
1264 SMESH_Algo *SMESH_Gen::GetAlgo(SMESH_subMesh * aSubMesh,
1265 TopoDS_Shape* assignedTo)
1267 if ( !aSubMesh ) return 0;
1269 const TopoDS_Shape & aShape = aSubMesh->GetSubShape();
1270 SMESH_Mesh& aMesh = *aSubMesh->GetFather();
1272 SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
1273 if ( aMesh.HasShapeToMesh() )
1274 filter.And( filter.IsApplicableTo( aShape ));
1276 typedef SMESH_Algo::Features AlgoData;
1278 TopoDS_Shape assignedToShape;
1280 (SMESH_Algo*) aMesh.GetHypothesis( aSubMesh, filter, true, &assignedToShape );
1283 aShape.ShapeType() == TopAbs_FACE &&
1284 !aShape.IsSame( assignedToShape ) &&
1285 SMESH_MesherHelper::NbAncestors( aShape, aMesh, TopAbs_SOLID ) > 1 )
1287 // Issue 0021559. If there is another 2D algo with different types of output
1288 // elements that can be used to mesh aShape, and 3D algos on adjacent SOLIDs
1289 // have different types of input elements, we choose a most appropriate 2D algo.
1291 // try to find a concurrent 2D algo
1292 filter.AndNot( filter.Is( algo ));
1293 TopoDS_Shape assignedToShape2;
1295 (SMESH_Algo*) aMesh.GetHypothesis( aSubMesh, filter, true, &assignedToShape2 );
1296 if ( algo2 && // algo found
1297 !assignedToShape2.IsSame( aMesh.GetShapeToMesh() ) && // algo is local
1298 ( SMESH_MesherHelper::GetGroupType( assignedToShape2 ) == // algo of the same level
1299 SMESH_MesherHelper::GetGroupType( assignedToShape )) &&
1300 aMesh.IsOrderOK( aMesh.GetSubMesh( assignedToShape2 ), // no forced order
1301 aMesh.GetSubMesh( assignedToShape )))
1303 // get algos on the adjacent SOLIDs
1304 filter.Init( filter.IsAlgo() ).And( filter.HasDim( 3 ));
1305 vector< SMESH_Algo* > algos3D;
1306 PShapeIteratorPtr solidIt = SMESH_MesherHelper::GetAncestors( aShape, aMesh,
1308 while ( const TopoDS_Shape* solid = solidIt->next() )
1309 if ( SMESH_Algo* algo3D = (SMESH_Algo*) aMesh.GetHypothesis( *solid, filter, true ))
1311 algos3D.push_back( algo3D );
1312 filter.AndNot( filter.HasName( algo3D->GetName() ));
1314 // check compatibility of algos
1315 if ( algos3D.size() > 1 )
1317 const AlgoData& algoData = algo->SMESH_Algo::GetFeatures();
1318 const AlgoData& algoData2 = algo2->SMESH_Algo::GetFeatures();
1319 const AlgoData& algoData3d0 = algos3D[0]->SMESH_Algo::GetFeatures();
1320 const AlgoData& algoData3d1 = algos3D[1]->SMESH_Algo::GetFeatures();
1321 if (( algoData2.IsCompatible( algoData3d0 ) &&
1322 algoData2.IsCompatible( algoData3d1 ))
1324 !(algoData.IsCompatible( algoData3d0 ) &&
1325 algoData.IsCompatible( algoData3d1 )))
1331 if ( assignedTo && algo )
1332 * assignedTo = assignedToShape;
1337 //=============================================================================
1339 * Returns StudyContextStruct for a study
1341 //=============================================================================
1343 StudyContextStruct *SMESH_Gen::GetStudyContext()
1345 return _studyContext;
1348 //================================================================================
1350 * \brief Return shape dimension by TopAbs_ShapeEnum
1352 //================================================================================
1354 int SMESH_Gen::GetShapeDim(const TopAbs_ShapeEnum & aShapeType)
1356 static vector<int> dim;
1359 dim.resize( TopAbs_SHAPE, -1 );
1360 dim[ TopAbs_COMPOUND ] = MeshDim_3D;
1361 dim[ TopAbs_COMPSOLID ] = MeshDim_3D;
1362 dim[ TopAbs_SOLID ] = MeshDim_3D;
1363 dim[ TopAbs_SHELL ] = MeshDim_2D;
1364 dim[ TopAbs_FACE ] = MeshDim_2D;
1365 dim[ TopAbs_WIRE ] = MeshDim_1D;
1366 dim[ TopAbs_EDGE ] = MeshDim_1D;
1367 dim[ TopAbs_VERTEX ] = MeshDim_0D;
1369 return dim[ aShapeType ];
1372 //================================================================================
1374 * \brief Return shape dimension by exploding compounds
1376 //================================================================================
1378 int SMESH_Gen::GetFlatShapeDim(const TopoDS_Shape &aShape)
1381 if ( aShape.ShapeType() == TopAbs_COMPOUND ||
1382 aShape.ShapeType() == TopAbs_COMPSOLID )
1384 TopoDS_Iterator it( aShape );
1385 aShapeDim = GetFlatShapeDim( it.Value() );
1388 aShapeDim = GetShapeDim( aShape );
1392 //=============================================================================
1394 * Generate a new id unique within this Gen
1396 //=============================================================================
1398 int SMESH_Gen::GetANewId()