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 "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>
56 #include <Basics_Utils.hxx>
59 #include <boost/filesystem.hpp>
60 namespace fs = boost::filesystem;
62 // Environment variable separator
71 // a structure used to nullify SMESH_Gen field of SMESH_Hypothesis,
72 // which is needed for SMESH_Hypothesis not deleted before ~SMESH_Gen()
73 struct _Hyp : public SMESH_Hypothesis
81 //================================================================================
83 * \brief Fill a map of shapes with all sub-shape of a sub-mesh
85 //================================================================================
87 TopTools_IndexedMapOfShape * fillAllowed( SMESH_subMesh* sm,
89 TopTools_IndexedMapOfShape * allowedSub )
91 if ( !toFill || !allowedSub )
95 if ( allowedSub->IsEmpty() )
97 allowedSub->ReSize( sm->DependsOn().size() + 1 );
98 allowedSub->Add( sm->GetSubShape() );
99 for ( const auto & key_sm : sm->DependsOn() )
100 allowedSub->Add( key_sm.second->GetSubShape() );
106 //=============================================================================
110 //=============================================================================
112 SMESH_Gen::SMESH_Gen()
114 _studyContext = new StudyContextStruct;
115 _studyContext->myDocument = new SMESHDS_Document();
118 _segmentation = _nbSegments = 10;
119 _compute_canceled = false;
122 //=============================================================================
126 //=============================================================================
128 SMESH_Gen::~SMESH_Gen()
130 std::map < int, SMESH_Hypothesis * >::iterator i_hyp = _studyContext->mapHypothesis.begin();
131 for ( ; i_hyp != _studyContext->mapHypothesis.end(); ++i_hyp )
133 if ( _Hyp* h = static_cast< _Hyp*>( i_hyp->second ))
136 delete _studyContext->myDocument;
137 delete _studyContext;
140 //=============================================================================
142 * Creates a mesh in a study.
143 * if (theIsEmbeddedMode) { mesh modification commands are not logged }
145 //=============================================================================
147 SMESH_Mesh* SMESH_Gen::CreateMesh(bool theIsEmbeddedMode)
149 Unexpect aCatch(SalomeException);
151 // create a new SMESH_mesh object
152 SMESH_Mesh *aMesh = new SMESH_Mesh(_localId++,
155 _studyContext->myDocument);
156 _studyContext->mapMesh[_localId-1] = aMesh;
161 //=============================================================================
163 * Parallel compute of a submesh
164 * This function is used to pass to thread_pool
166 //=============================================================================
167 const std::function<void(int,
169 SMESH_subMesh::compute_event,
172 TopTools_IndexedMapOfShape *,
174 parallel_compute([&] (int id,
176 SMESH_subMesh::compute_event event,
177 SMESH_subMesh *shapeSM,
179 TopTools_IndexedMapOfShape *allowedSubShapes,
180 TSetOfInt* aShapesId) -> void
182 if (sm->GetComputeState() == SMESH_subMesh::READY_TO_COMPUTE)
184 sm->SetAllowedSubShapes( fillAllowed( shapeSM, aShapeOnly, allowedSubShapes ));
185 //setCurrentSubMesh( sm );
186 sm->ComputeStateEngine(event);
187 //setCurrentSubMesh( nullptr );
188 sm->SetAllowedSubShapes( nullptr );
192 aShapesId->insert( sm->GetId() );
196 //=============================================================================
200 //=============================================================================
202 bool SMESH_Gen::Compute(SMESH_Mesh & aMesh,
203 const TopoDS_Shape & aShape,
204 const int aFlags /*= COMPACT_MESH*/,
205 const ::MeshDimension aDim /*=::MeshDim_3D*/,
206 TSetOfInt* aShapesId /*=0*/,
207 TopTools_IndexedMapOfShape* anAllowedSubShapes/*=0*/)
211 const bool aShapeOnly = aFlags & SHAPE_ONLY;
212 const bool anUpward = aFlags & UPWARD;
213 const bool aCompactMesh = aFlags & COMPACT_MESH;
217 SMESH_subMesh *sm, *shapeSM = aMesh.GetSubMesh(aShape);
219 const bool includeSelf = true;
220 const bool complexShapeFirst = true;
221 const int globalAlgoDim = 100;
223 // Pool of thread for computation
225 _pool = new ctpl::thread_pool(2);
228 SMESH_subMeshIteratorPtr smIt;
230 // Fix of Issue 22150. Due to !BLSURF->OnlyUnaryInput(), BLSURF computes edges
231 // that must be computed by Projection 1D-2D while the Projection asks to compute
233 SMESH_subMesh::compute_event computeEvent =
234 aShapeOnly ? SMESH_subMesh::COMPUTE_SUBMESH : SMESH_subMesh::COMPUTE;
235 if ( !aMesh.HasShapeToMesh() )
236 computeEvent = SMESH_subMesh::COMPUTE_NOGEOM; // if several algos and no geometry
238 TopTools_IndexedMapOfShape *allowedSubShapes = anAllowedSubShapes, allowedSub;
239 if ( aShapeOnly && !allowedSubShapes )
240 allowedSubShapes = &allowedSub;
242 if ( anUpward ) // is called from the below code in this method
244 // ===============================================
245 // Mesh all the sub-shapes starting from vertices
246 // ===============================================
248 TopAbs_ShapeEnum previousShapeType = TopAbs_VERTEX;
249 smIt = shapeSM->getDependsOnIterator(includeSelf, !complexShapeFirst);
250 std::vector<std::future<void>> pending;
251 while ( smIt->more() )
253 SMESH_subMesh* smToCompute = smIt->next();
255 // do not mesh vertices of a pseudo shape
256 const TopoDS_Shape& shape = smToCompute->GetSubShape();
257 const TopAbs_ShapeEnum shapeType = shape.ShapeType();
258 if ( !aMesh.HasShapeToMesh() && shapeType == TopAbs_VERTEX )
261 //DEBUG std::cout << "Shape Type" << shapeType << " previous" << previousShapeType << std::endl;
262 if (shapeType != previousShapeType) {
263 // Waiting for all thread for the previous type to end
264 for(auto it =std::begin(pending); it != std::end(pending); ++it){
265 std::cout << "Waiting" << std::endl;
268 std::string file_name;
269 switch(previousShapeType){
271 file_name = "Mesh2D.med";
274 file_name = "Mesh1D.med";
277 file_name = "Mesh0D.med";
284 fs::path mesh_file = fs::path(aMesh.tmp_folder) / fs::path(file_name);
285 // TODO: change mesh name
286 export_mesh(mesh_file.string(), aMesh, "Maillage_1");
289 //Resetting threaded pool info
290 previousShapeType = shapeType;
294 // check for preview dimension limitations
295 if ( aShapesId && GetShapeDim( shapeType ) > (int)aDim )
297 // clear compute state not to show previous compute errors
298 // if preview invoked less dimension less than previous
299 smToCompute->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
302 pending.push_back(_pool->push(parallel_compute, smToCompute, computeEvent,
303 shapeSM, aShapeOnly, allowedSubShapes,
305 //DEBUG std::cout << "Launched " << smToCompute << " shape type " << shapeType << std::endl;
310 for(auto it =std::begin(pending); it != std::end(pending); ++it){
314 //aMesh.GetMeshDS()->Modified();
319 // ================================================================
320 // Apply algos that do NOT require discreteized boundaries
321 // ("all-dimensional") and do NOT support sub-meshes, starting from
322 // the most complex shapes and collect sub-meshes with algos that
323 // DO support sub-meshes
324 // ================================================================
326 list< SMESH_subMesh* > smWithAlgoSupportingSubmeshes[4]; // for each dim
328 // map to sort sm with same dim algos according to dim of
329 // the shape the algo assigned to (issue 0021217).
330 // Other issues influenced the algo applying order:
331 // 21406, 21556, 21893, 20206
332 multimap< int, SMESH_subMesh* > shDim2sm;
333 multimap< int, SMESH_subMesh* >::reverse_iterator shDim2smIt;
334 TopoDS_Shape algoShape;
335 int prevShapeDim = -1, aShapeDim;
337 smIt = shapeSM->getDependsOnIterator(includeSelf, complexShapeFirst);
338 while ( smIt->more() )
340 SMESH_subMesh* smToCompute = smIt->next();
341 if ( smToCompute->GetComputeState() != SMESH_subMesh::READY_TO_COMPUTE )
344 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
345 aShapeDim = GetShapeDim( aSubShape );
346 if ( aShapeDim < 1 ) break;
348 // check for preview dimension limitations
349 if ( aShapesId && aShapeDim > (int)aDim )
352 SMESH_Algo* algo = GetAlgo( smToCompute, &algoShape );
353 if ( algo && !algo->NeedDiscreteBoundary() )
355 if ( algo->SupportSubmeshes() )
357 // reload sub-meshes from shDim2sm into smWithAlgoSupportingSubmeshes
358 // so that more local algos to go first
359 if ( prevShapeDim != aShapeDim )
361 prevShapeDim = aShapeDim;
362 for ( shDim2smIt = shDim2sm.rbegin(); shDim2smIt != shDim2sm.rend(); ++shDim2smIt )
363 if ( shDim2smIt->first == globalAlgoDim )
364 smWithAlgoSupportingSubmeshes[ aShapeDim ].push_back( shDim2smIt->second );
366 smWithAlgoSupportingSubmeshes[ aShapeDim ].push_front( shDim2smIt->second );
369 // add smToCompute to shDim2sm map
370 if ( algoShape.IsSame( aMesh.GetShapeToMesh() ))
372 aShapeDim = globalAlgoDim; // to compute last
376 aShapeDim = GetShapeDim( algoShape );
377 if ( algoShape.ShapeType() == TopAbs_COMPOUND )
379 TopoDS_Iterator it( algoShape );
380 aShapeDim += GetShapeDim( it.Value() );
383 shDim2sm.insert( make_pair( aShapeDim, smToCompute ));
385 else // Compute w/o support of sub-meshes
387 if (_compute_canceled)
389 smToCompute->SetAllowedSubShapes( fillAllowed( shapeSM, aShapeOnly, allowedSubShapes ));
390 setCurrentSubMesh( smToCompute );
391 smToCompute->ComputeStateEngine( computeEvent );
392 setCurrentSubMesh( nullptr );
393 smToCompute->SetAllowedSubShapes( nullptr );
395 aShapesId->insert( smToCompute->GetId() );
399 // reload sub-meshes from shDim2sm into smWithAlgoSupportingSubmeshes
400 for ( shDim2smIt = shDim2sm.rbegin(); shDim2smIt != shDim2sm.rend(); ++shDim2smIt )
401 if ( shDim2smIt->first == globalAlgoDim )
402 smWithAlgoSupportingSubmeshes[3].push_back( shDim2smIt->second );
404 smWithAlgoSupportingSubmeshes[0].push_front( shDim2smIt->second );
406 // ======================================================
407 // Apply all-dimensional algorithms supporing sub-meshes
408 // ======================================================
410 std::vector< SMESH_subMesh* > smVec;
411 for ( aShapeDim = 0; aShapeDim < 4; ++aShapeDim )
412 smVec.insert( smVec.end(),
413 smWithAlgoSupportingSubmeshes[aShapeDim].begin(),
414 smWithAlgoSupportingSubmeshes[aShapeDim].end() );
416 // gather sub-shapes with local uni-dimensional algos (bos #29143)
417 // ----------------------------------------------------------------
418 TopTools_MapOfShape uniDimAlgoShapes;
419 if ( !smVec.empty() )
421 ShapeToHypothesis::Iterator s2hyps( aMesh.GetMeshDS()->GetHypotheses() );
422 for ( ; s2hyps.More(); s2hyps.Next() )
424 const TopoDS_Shape& s = s2hyps.Key();
425 if ( s.IsSame( aMesh.GetShapeToMesh() ))
427 for ( auto & hyp : s2hyps.Value() )
429 if ( const SMESH_Algo* algo = dynamic_cast< const SMESH_Algo*>( hyp ))
430 if ( algo->NeedDiscreteBoundary() )
432 TopAbs_ShapeEnum sType;
433 switch ( algo->GetDim() ) {
434 case 3: sType = TopAbs_SOLID; break;
435 case 2: sType = TopAbs_FACE; break;
436 default: sType = TopAbs_EDGE; break;
438 for ( TopExp_Explorer ex( s2hyps.Key(), sType ); ex.More(); ex.Next() )
439 uniDimAlgoShapes.Add( ex.Current() );
446 // ------------------------------------------------
447 // sort list of sub-meshes according to mesh order
448 // ------------------------------------------------
449 aMesh.SortByMeshOrder( smVec );
451 // ------------------------------------------------------------
452 // compute sub-meshes with local uni-dimensional algos under
453 // sub-meshes with all-dimensional algos
454 // ------------------------------------------------------------
455 // start from lower shapes
456 for ( size_t i = 0; i < smVec.size(); ++i )
459 if ( sm->GetComputeState() != SMESH_subMesh::READY_TO_COMPUTE)
462 const TopAbs_ShapeEnum shapeType = sm->GetSubShape().ShapeType();
464 if ( !uniDimAlgoShapes.IsEmpty() )
466 // get a shape the algo is assigned to
467 if ( !GetAlgo( sm, & algoShape ))
468 continue; // strange...
470 // look for more local algos
471 if ( SMESH_subMesh* algoSM = aMesh.GetSubMesh( algoShape ))
472 smIt = algoSM->getDependsOnIterator(!includeSelf, !complexShapeFirst);
474 smIt = sm->getDependsOnIterator(!includeSelf, !complexShapeFirst);
476 while ( smIt->more() )
478 SMESH_subMesh* smToCompute = smIt->next();
480 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
481 const int aShapeDim = GetShapeDim( aSubShape );
482 if ( aShapeDim < 1 || aSubShape.ShapeType() <= shapeType )
484 if ( !uniDimAlgoShapes.Contains( aSubShape ))
485 continue; // [bos #29143] aMesh.GetHypothesis() is too long
487 // check for preview dimension limitations
488 if ( aShapesId && GetShapeDim( aSubShape.ShapeType() ) > (int)aDim )
491 SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
493 .And( SMESH_HypoFilter::IsApplicableTo( aSubShape ))
494 .And( SMESH_HypoFilter::IsMoreLocalThan( algoShape, aMesh ));
496 if ( SMESH_Algo* subAlgo = (SMESH_Algo*) aMesh.GetHypothesis( smToCompute, filter, true))
498 if ( ! subAlgo->NeedDiscreteBoundary() ) continue;
499 TopTools_IndexedMapOfShape* localAllowed = allowedSubShapes;
500 if ( localAllowed && localAllowed->IsEmpty() )
501 localAllowed = 0; // prevent fillAllowed() with aSubShape
503 SMESH_Hypothesis::Hypothesis_Status status;
504 if ( subAlgo->CheckHypothesis( aMesh, aSubShape, status ))
505 // mesh a lower smToCompute starting from vertices
506 Compute( aMesh, aSubShape, aFlags | SHAPE_ONLY_UPWARD, aDim, aShapesId, localAllowed );
510 // --------------------------------
511 // apply the all-dimensional algo
512 // --------------------------------
514 if (_compute_canceled)
516 // check for preview dimension limitations
517 if ( aShapesId && GetShapeDim( shapeType ) > (int)aDim )
519 sm->SetAllowedSubShapes( fillAllowed( shapeSM, aShapeOnly, allowedSubShapes ));
520 setCurrentSubMesh( sm );
521 sm->ComputeStateEngine( computeEvent );
522 setCurrentSubMesh( NULL );
523 sm->SetAllowedSubShapes( nullptr );
525 aShapesId->insert( sm->GetId() );
530 // -----------------------------------------------
531 // mesh the rest sub-shapes starting from vertices
532 // -----------------------------------------------
533 ret = Compute( aMesh, aShape, aFlags | UPWARD, aDim, aShapesId, allowedSubShapes );
538 // fix quadratic mesh by bending iternal links near concave boundary
539 if ( aCompactMesh && // a final compute
540 aShape.IsSame( aMesh.GetShapeToMesh() ) &&
541 !aShapesId && // not preview
542 ret ) // everything is OK
544 SMESH_MesherHelper aHelper( aMesh );
545 if ( aHelper.IsQuadraticMesh() != SMESH_MesherHelper::LINEAR )
547 aHelper.FixQuadraticElements( shapeSM->GetComputeError() );
553 aMesh.GetMeshDS()->Modified();
554 aMesh.GetMeshDS()->CompactMesh();
559 //=============================================================================
561 * Prepare Compute a mesh
563 //=============================================================================
564 void SMESH_Gen::PrepareCompute(SMESH_Mesh & /*aMesh*/,
565 const TopoDS_Shape & /*aShape*/)
567 _compute_canceled = false;
568 resetCurrentSubMesh();
571 //=============================================================================
573 * Cancel Compute a mesh
575 //=============================================================================
576 void SMESH_Gen::CancelCompute(SMESH_Mesh & /*aMesh*/,
577 const TopoDS_Shape & /*aShape*/)
579 _compute_canceled = true;
580 if ( const SMESH_subMesh* sm = GetCurrentSubMesh() )
582 const_cast< SMESH_subMesh* >( sm )->ComputeStateEngine( SMESH_subMesh::COMPUTE_CANCELED );
584 resetCurrentSubMesh();
587 //================================================================================
589 * \brief Returns a sub-mesh being currently computed
591 //================================================================================
593 const SMESH_subMesh* SMESH_Gen::GetCurrentSubMesh() const
595 return _sm_current.empty() ? 0 : _sm_current.back();
598 //================================================================================
600 * \brief Sets a sub-mesh being currently computed.
602 * An algorithm can call Compute() for a sub-shape, hence we keep a stack of sub-meshes
604 //================================================================================
606 void SMESH_Gen::setCurrentSubMesh(SMESH_subMesh* sm)
609 _sm_current.push_back( sm );
611 else if ( !_sm_current.empty() )
612 _sm_current.pop_back();
615 void SMESH_Gen::resetCurrentSubMesh()
620 //=============================================================================
624 //=============================================================================
626 bool SMESH_Gen::Evaluate(SMESH_Mesh & aMesh,
627 const TopoDS_Shape & aShape,
628 MapShapeNbElems& aResMap,
630 TSetOfInt* aShapesId)
634 SMESH_subMesh *sm = aMesh.GetSubMesh(aShape);
636 const bool includeSelf = true;
637 const bool complexShapeFirst = true;
638 SMESH_subMeshIteratorPtr smIt;
640 if ( anUpward ) { // is called from below code here
641 // -----------------------------------------------
642 // mesh all the sub-shapes starting from vertices
643 // -----------------------------------------------
644 smIt = sm->getDependsOnIterator(includeSelf, !complexShapeFirst);
645 while ( smIt->more() ) {
646 SMESH_subMesh* smToCompute = smIt->next();
648 // do not mesh vertices of a pseudo shape
649 const TopAbs_ShapeEnum shapeType = smToCompute->GetSubShape().ShapeType();
650 //if ( !aMesh.HasShapeToMesh() && shapeType == TopAbs_VERTEX )
652 if ( !aMesh.HasShapeToMesh() ) {
653 if( shapeType == TopAbs_VERTEX || shapeType == TopAbs_WIRE ||
654 shapeType == TopAbs_SHELL )
658 smToCompute->Evaluate(aResMap);
660 aShapesId->insert( smToCompute->GetId() );
665 // -----------------------------------------------------------------
666 // apply algos that DO NOT require Discreteized boundaries and DO NOT
667 // support sub-meshes, starting from the most complex shapes
668 // and collect sub-meshes with algos that DO support sub-meshes
669 // -----------------------------------------------------------------
670 list< SMESH_subMesh* > smWithAlgoSupportingSubmeshes;
671 smIt = sm->getDependsOnIterator(includeSelf, complexShapeFirst);
672 while ( smIt->more() ) {
673 SMESH_subMesh* smToCompute = smIt->next();
674 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
675 const int aShapeDim = GetShapeDim( aSubShape );
676 if ( aShapeDim < 1 ) break;
678 SMESH_Algo* algo = GetAlgo( smToCompute );
679 if ( algo && !algo->NeedDiscreteBoundary() ) {
680 if ( algo->SupportSubmeshes() ) {
681 smWithAlgoSupportingSubmeshes.push_front( smToCompute );
684 smToCompute->Evaluate(aResMap);
686 aShapesId->insert( smToCompute->GetId() );
691 // ------------------------------------------------------------
692 // sort list of meshes according to mesh order
693 // ------------------------------------------------------------
694 std::vector< SMESH_subMesh* > smVec( smWithAlgoSupportingSubmeshes.begin(),
695 smWithAlgoSupportingSubmeshes.end() );
696 aMesh.SortByMeshOrder( smVec );
698 // ------------------------------------------------------------
699 // compute sub-meshes under shapes with algos that DO NOT require
700 // Discreteized boundaries and DO support sub-meshes
701 // ------------------------------------------------------------
702 // start from lower shapes
703 for ( size_t i = 0; i < smVec.size(); ++i )
707 // get a shape the algo is assigned to
708 TopoDS_Shape algoShape;
709 if ( !GetAlgo( sm, & algoShape ))
710 continue; // strange...
712 // look for more local algos
713 smIt = sm->getDependsOnIterator(!includeSelf, !complexShapeFirst);
714 while ( smIt->more() ) {
715 SMESH_subMesh* smToCompute = smIt->next();
717 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
718 const int aShapeDim = GetShapeDim( aSubShape );
719 if ( aShapeDim < 1 ) continue;
721 SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
723 .And( SMESH_HypoFilter::IsApplicableTo( aSubShape ))
724 .And( SMESH_HypoFilter::IsMoreLocalThan( algoShape, aMesh ));
726 if ( SMESH_Algo* subAlgo = (SMESH_Algo*) aMesh.GetHypothesis( smToCompute, filter, true ))
728 if ( ! subAlgo->NeedDiscreteBoundary() ) continue;
729 SMESH_Hypothesis::Hypothesis_Status status;
730 if ( subAlgo->CheckHypothesis( aMesh, aSubShape, status ))
731 // mesh a lower smToCompute starting from vertices
732 Evaluate( aMesh, aSubShape, aResMap, /*anUpward=*/true, aShapesId );
736 // ----------------------------------------------------------
737 // apply the algos that do not require Discreteized boundaries
738 // ----------------------------------------------------------
739 for ( size_t i = 0; i < smVec.size(); ++i )
742 sm->Evaluate(aResMap);
744 aShapesId->insert( sm->GetId() );
747 // -----------------------------------------------
748 // mesh the rest sub-shapes starting from vertices
749 // -----------------------------------------------
750 ret = Evaluate( aMesh, aShape, aResMap, /*anUpward=*/true, aShapesId );
757 //=======================================================================
758 //function : checkConformIgnoredAlgos
760 //=======================================================================
762 static bool checkConformIgnoredAlgos(SMESH_Mesh& aMesh,
763 SMESH_subMesh* aSubMesh,
764 const SMESH_Algo* aGlobIgnoAlgo,
765 const SMESH_Algo* aLocIgnoAlgo,
767 set<SMESH_subMesh*>& aCheckedMap,
768 list< SMESH_Gen::TAlgoStateError > & theErrors)
771 if ( aSubMesh->GetSubShape().ShapeType() == TopAbs_VERTEX)
777 const list<const SMESHDS_Hypothesis*>& listHyp =
778 aMesh.GetMeshDS()->GetHypothesis( aSubMesh->GetSubShape() );
779 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
780 for ( ; it != listHyp.end(); it++)
782 const SMESHDS_Hypothesis * aHyp = *it;
783 if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
786 const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
789 if ( aLocIgnoAlgo ) // algo is hidden by a local algo of upper dim
791 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
792 theErrors.back().Set( SMESH_Hypothesis::HYP_HIDDEN_ALGO, algo, false );
793 INFOS( "Local <" << algo->GetName() << "> is hidden by local <"
794 << aLocIgnoAlgo->GetName() << ">");
798 bool isGlobal = (aMesh.IsMainShape( aSubMesh->GetSubShape() ));
799 int dim = algo->GetDim();
800 int aMaxGlobIgnoDim = ( aGlobIgnoAlgo ? aGlobIgnoAlgo->GetDim() : -1 );
801 bool isNeededDim = ( aGlobIgnoAlgo ? aGlobIgnoAlgo->NeedLowerHyps( dim ) : false );
803 if (( dim < aMaxGlobIgnoDim && !isNeededDim ) &&
804 ( isGlobal || !aGlobIgnoAlgo->SupportSubmeshes() ))
806 // algo is hidden by a global algo
807 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
808 theErrors.back().Set( SMESH_Hypothesis::HYP_HIDDEN_ALGO, algo, true );
809 INFOS( ( isGlobal ? "Global" : "Local" )
810 << " <" << algo->GetName() << "> is hidden by global <"
811 << aGlobIgnoAlgo->GetName() << ">");
813 else if ( !algo->NeedDiscreteBoundary() && !isGlobal)
815 // local algo is not hidden and hides algos on sub-shapes
816 if (checkConform && !aSubMesh->IsConform( algo ))
819 checkConform = false; // no more check conformity
820 INFOS( "ERROR: Local <" << algo->GetName() <<
821 "> would produce not conform mesh: "
822 "<Not Conform Mesh Allowed> hypothesis is missing");
823 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
824 theErrors.back().Set( SMESH_Hypothesis::HYP_NOTCONFORM, algo, false );
827 // sub-algos will be hidden by a local <algo> if <algo> does not support sub-meshes
828 if ( algo->SupportSubmeshes() )
830 SMESH_subMeshIteratorPtr revItSub =
831 aSubMesh->getDependsOnIterator( /*includeSelf=*/false, /*complexShapeFirst=*/true);
832 bool checkConform2 = false;
833 while ( revItSub->more() )
835 SMESH_subMesh* sm = revItSub->next();
836 checkConformIgnoredAlgos (aMesh, sm, aGlobIgnoAlgo,
837 algo, checkConform2, aCheckedMap, theErrors);
838 aCheckedMap.insert( sm );
847 //=======================================================================
848 //function : checkMissing
849 //purpose : notify on missing hypothesis
850 // Return false if algo or hipothesis is missing
851 //=======================================================================
853 static bool checkMissing(SMESH_Gen* aGen,
855 SMESH_subMesh* aSubMesh,
856 const int aTopAlgoDim,
858 const bool checkNoAlgo,
859 set<SMESH_subMesh*>& aCheckedMap,
860 list< SMESH_Gen::TAlgoStateError > & theErrors)
862 switch ( aSubMesh->GetSubShape().ShapeType() )
866 case TopAbs_SOLID: break; // check this sub-mesh, it can be meshed
868 return true; // not meshable sub-mesh
870 if ( aCheckedMap.count( aSubMesh ))
874 SMESH_Algo* algo = 0;
876 switch (aSubMesh->GetAlgoState())
878 case SMESH_subMesh::NO_ALGO: {
881 // should there be any algo?
882 int shapeDim = SMESH_Gen::GetShapeDim( aSubMesh->GetSubShape() );
883 if (aTopAlgoDim > shapeDim)
885 MESSAGE( "ERROR: " << shapeDim << "D algorithm is missing" );
887 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
888 theErrors.back().Set( SMESH_Hypothesis::HYP_MISSING, shapeDim, true );
893 case SMESH_subMesh::MISSING_HYP: {
894 // notify if an algo missing hyp is attached to aSubMesh
895 algo = aSubMesh->GetAlgo();
897 bool IsGlobalHypothesis = aGen->IsGlobalHypothesis( algo, aMesh );
898 if (!IsGlobalHypothesis || !globalChecked[ algo->GetDim() ])
900 TAlgoStateErrorName errName = SMESH_Hypothesis::HYP_MISSING;
901 SMESH_Hypothesis::Hypothesis_Status status;
902 algo->CheckHypothesis( aMesh, aSubMesh->GetSubShape(), status );
903 if ( status == SMESH_Hypothesis::HYP_BAD_PARAMETER ) {
904 MESSAGE( "ERROR: hypothesis of " << (IsGlobalHypothesis ? "Global " : "Local ")
905 << "<" << algo->GetName() << "> has a bad parameter value");
907 } else if ( status == SMESH_Hypothesis::HYP_BAD_GEOMETRY ) {
908 MESSAGE( "ERROR: " << (IsGlobalHypothesis ? "Global " : "Local ")
909 << "<" << algo->GetName() << "> assigned to mismatching geometry");
912 MESSAGE( "ERROR: " << (IsGlobalHypothesis ? "Global " : "Local ")
913 << "<" << algo->GetName() << "> misses some hypothesis");
915 if (IsGlobalHypothesis)
916 globalChecked[ algo->GetDim() ] = true;
917 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
918 theErrors.back().Set( errName, algo, IsGlobalHypothesis );
923 case SMESH_subMesh::HYP_OK:
924 algo = aSubMesh->GetAlgo();
926 if (!algo->NeedDiscreteBoundary())
928 SMESH_subMeshIteratorPtr itsub = aSubMesh->getDependsOnIterator( /*includeSelf=*/false,
929 /*complexShapeFirst=*/false);
930 while ( itsub->more() )
931 aCheckedMap.insert( itsub->next() );
937 // do not check under algo that hides sub-algos or
938 // re-start checking NO_ALGO state
940 bool isTopLocalAlgo =
941 ( aTopAlgoDim <= algo->GetDim() && !aGen->IsGlobalHypothesis( algo, aMesh ));
942 if (!algo->NeedDiscreteBoundary() || isTopLocalAlgo)
944 bool checkNoAlgo2 = ( algo->NeedDiscreteBoundary() );
945 SMESH_subMeshIteratorPtr itsub = aSubMesh->getDependsOnIterator( /*includeSelf=*/false,
946 /*complexShapeFirst=*/true);
947 while ( itsub->more() )
949 // sub-meshes should not be checked further more
950 SMESH_subMesh* sm = itsub->next();
954 //check algo on sub-meshes
955 int aTopAlgoDim2 = algo->GetDim();
956 if (!checkMissing (aGen, aMesh, sm, aTopAlgoDim2,
957 globalChecked, checkNoAlgo2, aCheckedMap, theErrors))
960 if (sm->GetAlgoState() == SMESH_subMesh::NO_ALGO )
961 checkNoAlgo2 = false;
964 aCheckedMap.insert( sm );
970 //=======================================================================
971 //function : CheckAlgoState
972 //purpose : notify on bad state of attached algos, return false
973 // if Compute() would fail because of some algo bad state
974 //=======================================================================
976 bool SMESH_Gen::CheckAlgoState(SMESH_Mesh& aMesh, const TopoDS_Shape& aShape)
978 list< TAlgoStateError > errors;
979 return GetAlgoState( aMesh, aShape, errors );
982 //=======================================================================
983 //function : GetAlgoState
984 //purpose : notify on bad state of attached algos, return false
985 // if Compute() would fail because of some algo bad state
986 // theErrors list contains problems description
987 //=======================================================================
989 bool SMESH_Gen::GetAlgoState(SMESH_Mesh& theMesh,
990 const TopoDS_Shape& theShape,
991 list< TAlgoStateError > & theErrors)
994 bool hasAlgo = false;
996 SMESH_subMesh* sm = theMesh.GetSubMesh(theShape);
997 const SMESHDS_Mesh* meshDS = theMesh.GetMeshDS();
998 TopoDS_Shape mainShape = meshDS->ShapeToMesh();
1000 // -----------------
1002 // -----------------
1004 const SMESH_Algo* aGlobAlgoArr[] = {0,0,0,0};
1006 const list<const SMESHDS_Hypothesis*>& listHyp = meshDS->GetHypothesis( mainShape );
1007 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
1008 for ( ; it != listHyp.end(); it++)
1010 const SMESHDS_Hypothesis * aHyp = *it;
1011 if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
1014 const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
1017 int dim = algo->GetDim();
1018 aGlobAlgoArr[ dim ] = algo;
1023 // --------------------------------------------------------
1024 // info on algos that will be ignored because of ones that
1025 // don't NeedDiscreteBoundary() attached to super-shapes,
1026 // check that a conform mesh will be produced
1027 // --------------------------------------------------------
1030 // find a global algo possibly hiding sub-algos
1032 const SMESH_Algo* aGlobIgnoAlgo = 0;
1033 for (dim = 3; dim > 0; dim--)
1035 if (aGlobAlgoArr[ dim ] &&
1036 !aGlobAlgoArr[ dim ]->NeedDiscreteBoundary() /*&&
1037 !aGlobAlgoArr[ dim ]->SupportSubmeshes()*/ )
1039 aGlobIgnoAlgo = aGlobAlgoArr[ dim ];
1044 set<SMESH_subMesh*> aCheckedSubs;
1045 bool checkConform = ( !theMesh.IsNotConformAllowed() );
1047 // loop on theShape and its sub-shapes
1048 SMESH_subMeshIteratorPtr revItSub = sm->getDependsOnIterator( /*includeSelf=*/true,
1049 /*complexShapeFirst=*/true);
1050 while ( revItSub->more() )
1052 SMESH_subMesh* smToCheck = revItSub->next();
1053 if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
1056 if ( aCheckedSubs.insert( smToCheck ).second ) // not yet checked
1057 if (!checkConformIgnoredAlgos (theMesh, smToCheck, aGlobIgnoAlgo,
1058 0, checkConform, aCheckedSubs, theErrors))
1061 if ( smToCheck->GetAlgoState() != SMESH_subMesh::NO_ALGO )
1065 // ----------------------------------------------------------------
1066 // info on missing hypothesis and find out if all needed algos are
1068 // ----------------------------------------------------------------
1070 // find max dim of global algo
1071 int aTopAlgoDim = 0;
1072 for (dim = 3; dim > 0; dim--)
1074 if (aGlobAlgoArr[ dim ])
1080 bool checkNoAlgo = theMesh.HasShapeToMesh() ? bool( aTopAlgoDim ) : false;
1081 bool globalChecked[] = { false, false, false, false };
1083 // loop on theShape and its sub-shapes
1084 aCheckedSubs.clear();
1085 revItSub = sm->getDependsOnIterator( /*includeSelf=*/true, /*complexShapeFirst=*/true);
1086 while ( revItSub->more() )
1088 SMESH_subMesh* smToCheck = revItSub->next();
1089 if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
1092 if (!checkMissing (this, theMesh, smToCheck, aTopAlgoDim,
1093 globalChecked, checkNoAlgo, aCheckedSubs, theErrors))
1096 if (smToCheck->GetAlgoState() == SMESH_subMesh::NO_ALGO )
1097 checkNoAlgo = false;
1103 theErrors.push_back( TAlgoStateError() );
1104 theErrors.back().Set( SMESH_Hypothesis::HYP_MISSING, theMesh.HasShapeToMesh() ? 1 : 3, true );
1110 //=======================================================================
1111 //function : IsGlobalHypothesis
1112 //purpose : check if theAlgo is attached to the main shape
1113 //=======================================================================
1115 bool SMESH_Gen::IsGlobalHypothesis(const SMESH_Hypothesis* theHyp, SMESH_Mesh& aMesh)
1117 SMESH_HypoFilter filter( SMESH_HypoFilter::Is( theHyp ));
1118 return aMesh.GetHypothesis( aMesh.GetMeshDS()->ShapeToMesh(), filter, false );
1121 //================================================================================
1123 * \brief Return paths to xml files of plugins
1125 //================================================================================
1127 std::vector< std::string > SMESH_Gen::GetPluginXMLPaths()
1129 // Get paths to xml files of plugins
1130 vector< string > xmlPaths;
1132 if ( const char* meshersList = getenv("SMESH_MeshersList") )
1134 string meshers = meshersList, plugin;
1135 string::size_type from = 0, pos;
1136 while ( from < meshers.size() )
1138 // cut off plugin name
1139 pos = meshers.find( env_sep, from );
1140 if ( pos != string::npos )
1141 plugin = meshers.substr( from, pos-from );
1143 plugin = meshers.substr( from ), pos = meshers.size();
1146 // get PLUGIN_ROOT_DIR path
1147 string rootDirVar, pluginSubDir = plugin;
1148 if ( plugin == "StdMeshers" )
1149 rootDirVar = "SMESH", pluginSubDir = "smesh";
1151 for ( pos = 0; pos < plugin.size(); ++pos )
1152 rootDirVar += toupper( plugin[pos] );
1153 rootDirVar += "_ROOT_DIR";
1155 const char* rootDir = getenv( rootDirVar.c_str() );
1156 if ( !rootDir || strlen(rootDir) == 0 )
1158 rootDirVar = plugin + "_ROOT_DIR"; // HexoticPLUGIN_ROOT_DIR
1159 rootDir = getenv( rootDirVar.c_str() );
1160 if ( !rootDir || strlen(rootDir) == 0 ) continue;
1163 // get a separator from rootDir
1164 for ( int i = strlen( rootDir )-1; i >= 0 && sep.empty(); --i )
1165 if ( rootDir[i] == '/' || rootDir[i] == '\\' )
1171 if (sep.empty() ) sep = "\\";
1173 if (sep.empty() ) sep = "/";
1176 // get a path to resource file
1177 string xmlPath = rootDir;
1178 if ( xmlPath[ xmlPath.size()-1 ] != sep[0] )
1180 xmlPath += "share" + sep + "salome" + sep + "resources" + sep;
1181 for ( pos = 0; pos < pluginSubDir.size(); ++pos )
1182 xmlPath += tolower( pluginSubDir[pos] );
1183 xmlPath += sep + plugin + ".xml";
1187 const wchar_t* path = Kernel_Utils::decode_s(xmlPath);
1188 SMESHUtils::ArrayDeleter<const wchar_t> deleter( path );
1190 const char* path = xmlPath.c_str();
1192 fileOK = (GetFileAttributes(path) != INVALID_FILE_ATTRIBUTES);
1195 fileOK = (access(xmlPath.c_str(), F_OK) == 0);
1198 xmlPaths.push_back( xmlPath );
1205 //=============================================================================
1207 * Finds algo to mesh a shape. Optionally returns a shape the found algo is bound to
1209 //=============================================================================
1211 SMESH_Algo *SMESH_Gen::GetAlgo(SMESH_Mesh & aMesh,
1212 const TopoDS_Shape & aShape,
1213 TopoDS_Shape* assignedTo)
1215 return GetAlgo( aMesh.GetSubMesh( aShape ), assignedTo );
1218 //=============================================================================
1220 * Finds algo to mesh a sub-mesh. Optionally returns a shape the found algo is bound to
1222 //=============================================================================
1224 SMESH_Algo *SMESH_Gen::GetAlgo(SMESH_subMesh * aSubMesh,
1225 TopoDS_Shape* assignedTo)
1227 if ( !aSubMesh ) return 0;
1229 const TopoDS_Shape & aShape = aSubMesh->GetSubShape();
1230 SMESH_Mesh& aMesh = *aSubMesh->GetFather();
1232 SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
1233 if ( aMesh.HasShapeToMesh() )
1234 filter.And( filter.IsApplicableTo( aShape ));
1236 typedef SMESH_Algo::Features AlgoData;
1238 TopoDS_Shape assignedToShape;
1240 (SMESH_Algo*) aMesh.GetHypothesis( aSubMesh, filter, true, &assignedToShape );
1243 aShape.ShapeType() == TopAbs_FACE &&
1244 !aShape.IsSame( assignedToShape ) &&
1245 SMESH_MesherHelper::NbAncestors( aShape, aMesh, TopAbs_SOLID ) > 1 )
1247 // Issue 0021559. If there is another 2D algo with different types of output
1248 // elements that can be used to mesh aShape, and 3D algos on adjacent SOLIDs
1249 // have different types of input elements, we choose a most appropriate 2D algo.
1251 // try to find a concurrent 2D algo
1252 filter.AndNot( filter.Is( algo ));
1253 TopoDS_Shape assignedToShape2;
1255 (SMESH_Algo*) aMesh.GetHypothesis( aSubMesh, filter, true, &assignedToShape2 );
1256 if ( algo2 && // algo found
1257 !assignedToShape2.IsSame( aMesh.GetShapeToMesh() ) && // algo is local
1258 ( SMESH_MesherHelper::GetGroupType( assignedToShape2 ) == // algo of the same level
1259 SMESH_MesherHelper::GetGroupType( assignedToShape )) &&
1260 aMesh.IsOrderOK( aMesh.GetSubMesh( assignedToShape2 ), // no forced order
1261 aMesh.GetSubMesh( assignedToShape )))
1263 // get algos on the adjacent SOLIDs
1264 filter.Init( filter.IsAlgo() ).And( filter.HasDim( 3 ));
1265 vector< SMESH_Algo* > algos3D;
1266 PShapeIteratorPtr solidIt = SMESH_MesherHelper::GetAncestors( aShape, aMesh,
1268 while ( const TopoDS_Shape* solid = solidIt->next() )
1269 if ( SMESH_Algo* algo3D = (SMESH_Algo*) aMesh.GetHypothesis( *solid, filter, true ))
1271 algos3D.push_back( algo3D );
1272 filter.AndNot( filter.HasName( algo3D->GetName() ));
1274 // check compatibility of algos
1275 if ( algos3D.size() > 1 )
1277 const AlgoData& algoData = algo->SMESH_Algo::GetFeatures();
1278 const AlgoData& algoData2 = algo2->SMESH_Algo::GetFeatures();
1279 const AlgoData& algoData3d0 = algos3D[0]->SMESH_Algo::GetFeatures();
1280 const AlgoData& algoData3d1 = algos3D[1]->SMESH_Algo::GetFeatures();
1281 if (( algoData2.IsCompatible( algoData3d0 ) &&
1282 algoData2.IsCompatible( algoData3d1 ))
1284 !(algoData.IsCompatible( algoData3d0 ) &&
1285 algoData.IsCompatible( algoData3d1 )))
1291 if ( assignedTo && algo )
1292 * assignedTo = assignedToShape;
1297 //=============================================================================
1299 * Returns StudyContextStruct for a study
1301 //=============================================================================
1303 StudyContextStruct *SMESH_Gen::GetStudyContext()
1305 return _studyContext;
1308 //================================================================================
1310 * \brief Return shape dimension by TopAbs_ShapeEnum
1312 //================================================================================
1314 int SMESH_Gen::GetShapeDim(const TopAbs_ShapeEnum & aShapeType)
1316 static vector<int> dim;
1319 dim.resize( TopAbs_SHAPE, -1 );
1320 dim[ TopAbs_COMPOUND ] = MeshDim_3D;
1321 dim[ TopAbs_COMPSOLID ] = MeshDim_3D;
1322 dim[ TopAbs_SOLID ] = MeshDim_3D;
1323 dim[ TopAbs_SHELL ] = MeshDim_2D;
1324 dim[ TopAbs_FACE ] = MeshDim_2D;
1325 dim[ TopAbs_WIRE ] = MeshDim_1D;
1326 dim[ TopAbs_EDGE ] = MeshDim_1D;
1327 dim[ TopAbs_VERTEX ] = MeshDim_0D;
1329 return dim[ aShapeType ];
1332 //================================================================================
1334 * \brief Return shape dimension by exploding compounds
1336 //================================================================================
1338 int SMESH_Gen::GetFlatShapeDim(const TopoDS_Shape &aShape)
1341 if ( aShape.ShapeType() == TopAbs_COMPOUND ||
1342 aShape.ShapeType() == TopAbs_COMPSOLID )
1344 TopoDS_Iterator it( aShape );
1345 aShapeDim = GetFlatShapeDim( it.Value() );
1348 aShapeDim = GetShapeDim( aShape );
1352 //=============================================================================
1354 * Generate a new id unique within this Gen
1356 //=============================================================================
1358 int SMESH_Gen::GetANewId()