1 // Copyright (C) 2007-2016 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 : implementaion of SMESH idl descriptions
24 // File : SMESH_Gen.cxx
25 // Author : Paul RASCLE, EDF
31 #include "SMESH_Gen.hxx"
33 #include "SMDS_Mesh.hxx"
34 #include "SMDS_MeshElement.hxx"
35 #include "SMDS_MeshNode.hxx"
36 #include "SMESHDS_Document.hxx"
37 #include "SMESH_HypoFilter.hxx"
38 #include "SMESH_Mesh.hxx"
39 #include "SMESH_MesherHelper.hxx"
40 #include "SMESH_subMesh.hxx"
42 #include "utilities.h"
44 #include "Utils_ExceptHandlers.hxx"
46 #include <TopoDS_Iterator.hxx>
57 //#include <vtkDebugLeaks.h>
60 //=============================================================================
64 //=============================================================================
66 SMESH_Gen::SMESH_Gen()
70 _segmentation = _nbSegments = 10;
71 SMDS_Mesh::_meshList.clear();
72 _compute_canceled = false;
73 //vtkDebugLeaks::SetExitError(0);
76 //=============================================================================
80 //=============================================================================
82 SMESH_Gen::~SMESH_Gen()
84 std::map < int, StudyContextStruct * >::iterator i_sc = _mapStudyContext.begin();
85 for ( ; i_sc != _mapStudyContext.end(); ++i_sc )
87 delete i_sc->second->myDocument;
92 //=============================================================================
94 * Creates a mesh in a study.
95 * if (theIsEmbeddedMode) { mesh modification commands are not logged }
97 //=============================================================================
99 SMESH_Mesh* SMESH_Gen::CreateMesh(int theStudyId, bool theIsEmbeddedMode)
100 throw(SALOME_Exception)
102 Unexpect aCatch(SalomeException);
104 // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
105 StudyContextStruct *aStudyContext = GetStudyContext(theStudyId);
107 // create a new SMESH_mesh object
108 SMESH_Mesh *aMesh = new SMESH_Mesh(_localId++,
112 aStudyContext->myDocument);
113 aStudyContext->mapMesh[_localId-1] = aMesh;
118 //=============================================================================
122 //=============================================================================
124 bool SMESH_Gen::Compute(SMESH_Mesh & aMesh,
125 const TopoDS_Shape & aShape,
126 const int aFlags /*= COMPACT_MESH*/,
127 const ::MeshDimension aDim /*=::MeshDim_3D*/,
128 TSetOfInt* aShapesId /*=0*/)
132 const bool aShapeOnly = aFlags & SHAPE_ONLY;
133 const bool anUpward = aFlags & UPWARD;
134 const bool aCompactMesh = aFlags & COMPACT_MESH;
138 SMESH_subMesh *sm = aMesh.GetSubMesh(aShape);
140 const bool includeSelf = true;
141 const bool complexShapeFirst = true;
142 const int globalAlgoDim = 100;
144 SMESH_subMeshIteratorPtr smIt;
146 // Fix of Issue 22150. Due to !BLSURF->OnlyUnaryInput(), BLSURF computes edges
147 // that must be computed by Projection 1D-2D while the Projection asks to compute
149 SMESH_subMesh::compute_event computeEvent =
150 aShapeOnly ? SMESH_subMesh::COMPUTE_SUBMESH : SMESH_subMesh::COMPUTE;
151 if ( !aMesh.HasShapeToMesh() )
152 computeEvent = SMESH_subMesh::COMPUTE_NOGEOM; // if several algos and no geometry
154 if ( anUpward ) // is called from the below code in this method
156 // ===============================================
157 // Mesh all the sub-shapes starting from vertices
158 // ===============================================
160 smIt = sm->getDependsOnIterator(includeSelf, !complexShapeFirst);
161 while ( smIt->more() )
163 SMESH_subMesh* smToCompute = smIt->next();
165 // do not mesh vertices of a pseudo shape
166 const TopoDS_Shape& shape = smToCompute->GetSubShape();
167 const TopAbs_ShapeEnum shapeType = shape.ShapeType();
168 if ( !aMesh.HasShapeToMesh() && shapeType == TopAbs_VERTEX )
171 // check for preview dimension limitations
172 if ( aShapesId && GetShapeDim( shapeType ) > (int)aDim )
174 // clear compute state not to show previous compute errors
175 // if preview invoked less dimension less than previous
176 smToCompute->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
180 if (smToCompute->GetComputeState() == SMESH_subMesh::READY_TO_COMPUTE)
182 if (_compute_canceled)
184 setCurrentSubMesh( smToCompute );
185 smToCompute->ComputeStateEngine( computeEvent );
186 setCurrentSubMesh( NULL );
189 // we check all the sub-meshes here and detect if any of them failed to compute
190 if (smToCompute->GetComputeState() == SMESH_subMesh::FAILED_TO_COMPUTE &&
191 ( shapeType != TopAbs_EDGE || !SMESH_Algo::isDegenerated( TopoDS::Edge( shape ))))
193 else if ( aShapesId )
194 aShapesId->insert( smToCompute->GetId() );
196 //aMesh.GetMeshDS()->Modified();
201 // ================================================================
202 // Apply algos that do NOT require discreteized boundaries
203 // ("all-dimensional") and do NOT support sub-meshes, starting from
204 // the most complex shapes and collect sub-meshes with algos that
205 // DO support sub-meshes
206 // ================================================================
208 list< SMESH_subMesh* > smWithAlgoSupportingSubmeshes[4]; // for each dim
210 // map to sort sm with same dim algos according to dim of
211 // the shape the algo assigned to (issue 0021217).
212 // Other issues influenced the algo applying order:
213 // 21406, 21556, 21893, 20206
214 multimap< int, SMESH_subMesh* > shDim2sm;
215 multimap< int, SMESH_subMesh* >::reverse_iterator shDim2smIt;
216 TopoDS_Shape algoShape;
217 int prevShapeDim = -1, aShapeDim;
219 smIt = sm->getDependsOnIterator(includeSelf, complexShapeFirst);
220 while ( smIt->more() )
222 SMESH_subMesh* smToCompute = smIt->next();
223 if ( smToCompute->GetComputeState() != SMESH_subMesh::READY_TO_COMPUTE )
226 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
227 aShapeDim = GetShapeDim( aSubShape );
228 if ( aShapeDim < 1 ) break;
230 // check for preview dimension limitations
231 if ( aShapesId && aShapeDim > (int)aDim )
234 SMESH_Algo* algo = GetAlgo( smToCompute, &algoShape );
235 if ( algo && !algo->NeedDiscreteBoundary() )
237 if ( algo->SupportSubmeshes() )
239 // reload sub-meshes from shDim2sm into smWithAlgoSupportingSubmeshes
240 // so that more local algos to go first
241 if ( prevShapeDim != aShapeDim )
243 prevShapeDim = aShapeDim;
244 for ( shDim2smIt = shDim2sm.rbegin(); shDim2smIt != shDim2sm.rend(); ++shDim2smIt )
245 if ( shDim2smIt->first == globalAlgoDim )
246 smWithAlgoSupportingSubmeshes[ aShapeDim ].push_back( shDim2smIt->second );
248 smWithAlgoSupportingSubmeshes[ aShapeDim ].push_front( shDim2smIt->second );
251 // add smToCompute to shDim2sm map
252 if ( algoShape.IsSame( aMesh.GetShapeToMesh() ))
254 aShapeDim = globalAlgoDim; // to compute last
258 aShapeDim = GetShapeDim( algoShape );
259 if ( algoShape.ShapeType() == TopAbs_COMPOUND )
261 TopoDS_Iterator it( algoShape );
262 aShapeDim += GetShapeDim( it.Value() );
265 shDim2sm.insert( make_pair( aShapeDim, smToCompute ));
267 else // Compute w/o support of sub-meshes
269 if (_compute_canceled)
271 setCurrentSubMesh( smToCompute );
272 smToCompute->ComputeStateEngine( computeEvent );
273 setCurrentSubMesh( NULL );
275 aShapesId->insert( smToCompute->GetId() );
279 // reload sub-meshes from shDim2sm into smWithAlgoSupportingSubmeshes
280 for ( shDim2smIt = shDim2sm.rbegin(); shDim2smIt != shDim2sm.rend(); ++shDim2smIt )
281 if ( shDim2smIt->first == globalAlgoDim )
282 smWithAlgoSupportingSubmeshes[3].push_back( shDim2smIt->second );
284 smWithAlgoSupportingSubmeshes[0].push_front( shDim2smIt->second );
286 // ======================================================
287 // Apply all-dimensional algorithms supporing sub-meshes
288 // ======================================================
290 std::vector< SMESH_subMesh* > smVec;
291 for ( aShapeDim = 0; aShapeDim < 4; ++aShapeDim )
293 // ------------------------------------------------
294 // sort list of sub-meshes according to mesh order
295 // ------------------------------------------------
296 smVec.assign( smWithAlgoSupportingSubmeshes[ aShapeDim ].begin(),
297 smWithAlgoSupportingSubmeshes[ aShapeDim ].end() );
298 aMesh.SortByMeshOrder( smVec );
300 // ------------------------------------------------------------
301 // compute sub-meshes with local uni-dimensional algos under
302 // sub-meshes with all-dimensional algos
303 // ------------------------------------------------------------
304 // start from lower shapes
305 for ( size_t i = 0; i < smVec.size(); ++i )
309 // get a shape the algo is assigned to
310 if ( !GetAlgo( sm, & algoShape ))
311 continue; // strange...
313 // look for more local algos
314 smIt = sm->getDependsOnIterator(!includeSelf, !complexShapeFirst);
315 while ( smIt->more() )
317 SMESH_subMesh* smToCompute = smIt->next();
319 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
320 const int aShapeDim = GetShapeDim( aSubShape );
321 //if ( aSubShape.ShapeType() == TopAbs_VERTEX ) continue;
322 if ( aShapeDim < 1 ) continue;
324 // check for preview dimension limitations
325 if ( aShapesId && GetShapeDim( aSubShape.ShapeType() ) > (int)aDim )
328 SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
330 .And( SMESH_HypoFilter::IsApplicableTo( aSubShape ))
331 .And( SMESH_HypoFilter::IsMoreLocalThan( algoShape, aMesh ));
333 if ( SMESH_Algo* subAlgo = (SMESH_Algo*) aMesh.GetHypothesis( smToCompute, filter, true))
335 if ( ! subAlgo->NeedDiscreteBoundary() ) continue;
336 SMESH_Hypothesis::Hypothesis_Status status;
337 if ( subAlgo->CheckHypothesis( aMesh, aSubShape, status ))
338 // mesh a lower smToCompute starting from vertices
339 Compute( aMesh, aSubShape, aFlags | SHAPE_ONLY_UPWARD, aDim, aShapesId );
340 // Compute( aMesh, aSubShape, aShapeOnly, /*anUpward=*/true, aDim, aShapesId );
344 // --------------------------------
345 // apply the all-dimensional algos
346 // --------------------------------
347 for ( size_t i = 0; i < smVec.size(); ++i )
350 if ( sm->GetComputeState() == SMESH_subMesh::READY_TO_COMPUTE)
352 const TopAbs_ShapeEnum shapeType = sm->GetSubShape().ShapeType();
353 // check for preview dimension limitations
354 if ( aShapesId && GetShapeDim( shapeType ) > (int)aDim )
357 if (_compute_canceled)
359 setCurrentSubMesh( sm );
360 sm->ComputeStateEngine( computeEvent );
361 setCurrentSubMesh( NULL );
363 aShapesId->insert( sm->GetId() );
366 } // loop on shape dimensions
368 // -----------------------------------------------
369 // mesh the rest sub-shapes starting from vertices
370 // -----------------------------------------------
371 ret = Compute( aMesh, aShape, aFlags | UPWARD, aDim, aShapesId );
376 // fix quadratic mesh by bending iternal links near concave boundary
377 if ( aCompactMesh && // a final compute
378 aShape.IsSame( aMesh.GetShapeToMesh() ) &&
379 !aShapesId && // not preview
380 ret ) // everything is OK
382 SMESH_MesherHelper aHelper( aMesh );
383 if ( aHelper.IsQuadraticMesh() != SMESH_MesherHelper::LINEAR )
385 aHelper.FixQuadraticElements( sm->GetComputeError() );
390 aMesh.GetMeshDS()->compactMesh();
395 //=============================================================================
397 * Prepare Compute a mesh
399 //=============================================================================
400 void SMESH_Gen::PrepareCompute(SMESH_Mesh & aMesh,
401 const TopoDS_Shape & aShape)
403 _compute_canceled = false;
404 resetCurrentSubMesh();
407 //=============================================================================
409 * Cancel Compute a mesh
411 //=============================================================================
412 void SMESH_Gen::CancelCompute(SMESH_Mesh & aMesh,
413 const TopoDS_Shape & aShape)
415 _compute_canceled = true;
416 if ( const SMESH_subMesh* sm = GetCurrentSubMesh() )
418 const_cast< SMESH_subMesh* >( sm )->ComputeStateEngine( SMESH_subMesh::COMPUTE_CANCELED );
420 resetCurrentSubMesh();
423 //================================================================================
425 * \brief Returns a sub-mesh being currently computed
427 //================================================================================
429 const SMESH_subMesh* SMESH_Gen::GetCurrentSubMesh() const
431 return _sm_current.empty() ? 0 : _sm_current.back();
434 //================================================================================
436 * \brief Sets a sub-mesh being currently computed.
438 * An algorithm can call Compute() for a sub-shape, hence we keep a stack of sub-meshes
440 //================================================================================
442 void SMESH_Gen::setCurrentSubMesh(SMESH_subMesh* sm)
445 _sm_current.push_back( sm );
447 else if ( !_sm_current.empty() )
448 _sm_current.pop_back();
451 void SMESH_Gen::resetCurrentSubMesh()
456 //=============================================================================
460 //=============================================================================
462 bool SMESH_Gen::Evaluate(SMESH_Mesh & aMesh,
463 const TopoDS_Shape & aShape,
464 MapShapeNbElems& aResMap,
466 TSetOfInt* aShapesId)
470 SMESH_subMesh *sm = aMesh.GetSubMesh(aShape);
472 const bool includeSelf = true;
473 const bool complexShapeFirst = true;
474 SMESH_subMeshIteratorPtr smIt;
476 if ( anUpward ) { // is called from below code here
477 // -----------------------------------------------
478 // mesh all the sub-shapes starting from vertices
479 // -----------------------------------------------
480 smIt = sm->getDependsOnIterator(includeSelf, !complexShapeFirst);
481 while ( smIt->more() ) {
482 SMESH_subMesh* smToCompute = smIt->next();
484 // do not mesh vertices of a pseudo shape
485 const TopAbs_ShapeEnum shapeType = smToCompute->GetSubShape().ShapeType();
486 //if ( !aMesh.HasShapeToMesh() && shapeType == TopAbs_VERTEX )
488 if ( !aMesh.HasShapeToMesh() ) {
489 if( shapeType == TopAbs_VERTEX || shapeType == TopAbs_WIRE ||
490 shapeType == TopAbs_SHELL )
494 smToCompute->Evaluate(aResMap);
496 aShapesId->insert( smToCompute->GetId() );
501 // -----------------------------------------------------------------
502 // apply algos that DO NOT require Discreteized boundaries and DO NOT
503 // support sub-meshes, starting from the most complex shapes
504 // and collect sub-meshes with algos that DO support sub-meshes
505 // -----------------------------------------------------------------
506 list< SMESH_subMesh* > smWithAlgoSupportingSubmeshes;
507 smIt = sm->getDependsOnIterator(includeSelf, complexShapeFirst);
508 while ( smIt->more() ) {
509 SMESH_subMesh* smToCompute = smIt->next();
510 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
511 const int aShapeDim = GetShapeDim( aSubShape );
512 if ( aShapeDim < 1 ) break;
514 SMESH_Algo* algo = GetAlgo( smToCompute );
515 if ( algo && !algo->NeedDiscreteBoundary() ) {
516 if ( algo->SupportSubmeshes() ) {
517 smWithAlgoSupportingSubmeshes.push_front( smToCompute );
520 smToCompute->Evaluate(aResMap);
522 aShapesId->insert( smToCompute->GetId() );
527 // ------------------------------------------------------------
528 // sort list of meshes according to mesh order
529 // ------------------------------------------------------------
530 std::vector< SMESH_subMesh* > smVec( smWithAlgoSupportingSubmeshes.begin(),
531 smWithAlgoSupportingSubmeshes.end() );
532 aMesh.SortByMeshOrder( smVec );
534 // ------------------------------------------------------------
535 // compute sub-meshes under shapes with algos that DO NOT require
536 // Discreteized boundaries and DO support sub-meshes
537 // ------------------------------------------------------------
538 // start from lower shapes
539 for ( size_t i = 0; i < smVec.size(); ++i )
543 // get a shape the algo is assigned to
544 TopoDS_Shape algoShape;
545 if ( !GetAlgo( sm, & algoShape ))
546 continue; // strange...
548 // look for more local algos
549 smIt = sm->getDependsOnIterator(!includeSelf, !complexShapeFirst);
550 while ( smIt->more() ) {
551 SMESH_subMesh* smToCompute = smIt->next();
553 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
554 const int aShapeDim = GetShapeDim( aSubShape );
555 if ( aShapeDim < 1 ) continue;
557 SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
559 .And( SMESH_HypoFilter::IsApplicableTo( aSubShape ))
560 .And( SMESH_HypoFilter::IsMoreLocalThan( algoShape, aMesh ));
562 if ( SMESH_Algo* subAlgo = (SMESH_Algo*) aMesh.GetHypothesis( smToCompute, filter, true ))
564 if ( ! subAlgo->NeedDiscreteBoundary() ) continue;
565 SMESH_Hypothesis::Hypothesis_Status status;
566 if ( subAlgo->CheckHypothesis( aMesh, aSubShape, status ))
567 // mesh a lower smToCompute starting from vertices
568 Evaluate( aMesh, aSubShape, aResMap, /*anUpward=*/true, aShapesId );
572 // ----------------------------------------------------------
573 // apply the algos that do not require Discreteized boundaries
574 // ----------------------------------------------------------
575 for ( size_t i = 0; i < smVec.size(); ++i )
578 sm->Evaluate(aResMap);
580 aShapesId->insert( sm->GetId() );
583 // -----------------------------------------------
584 // mesh the rest sub-shapes starting from vertices
585 // -----------------------------------------------
586 ret = Evaluate( aMesh, aShape, aResMap, /*anUpward=*/true, aShapesId );
589 MESSAGE( "VSR - SMESH_Gen::Evaluate() finished, OK = " << ret);
594 //=======================================================================
595 //function : checkConformIgnoredAlgos
597 //=======================================================================
599 static bool checkConformIgnoredAlgos(SMESH_Mesh& aMesh,
600 SMESH_subMesh* aSubMesh,
601 const SMESH_Algo* aGlobIgnoAlgo,
602 const SMESH_Algo* aLocIgnoAlgo,
604 set<SMESH_subMesh*>& aCheckedMap,
605 list< SMESH_Gen::TAlgoStateError > & theErrors)
608 if ( aSubMesh->GetSubShape().ShapeType() == TopAbs_VERTEX)
614 const list<const SMESHDS_Hypothesis*>& listHyp =
615 aMesh.GetMeshDS()->GetHypothesis( aSubMesh->GetSubShape() );
616 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
617 for ( ; it != listHyp.end(); it++)
619 const SMESHDS_Hypothesis * aHyp = *it;
620 if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
623 const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
626 if ( aLocIgnoAlgo ) // algo is hidden by a local algo of upper dim
628 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
629 theErrors.back().Set( SMESH_Hypothesis::HYP_HIDDEN_ALGO, algo, false );
630 INFOS( "Local <" << algo->GetName() << "> is hidden by local <"
631 << aLocIgnoAlgo->GetName() << ">");
635 bool isGlobal = (aMesh.IsMainShape( aSubMesh->GetSubShape() ));
636 int dim = algo->GetDim();
637 int aMaxGlobIgnoDim = ( aGlobIgnoAlgo ? aGlobIgnoAlgo->GetDim() : -1 );
638 bool isNeededDim = ( aGlobIgnoAlgo ? aGlobIgnoAlgo->NeedLowerHyps( dim ) : false );
640 if (( dim < aMaxGlobIgnoDim && !isNeededDim ) &&
641 ( isGlobal || !aGlobIgnoAlgo->SupportSubmeshes() ))
643 // algo is hidden by a global algo
644 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
645 theErrors.back().Set( SMESH_Hypothesis::HYP_HIDDEN_ALGO, algo, true );
646 INFOS( ( isGlobal ? "Global" : "Local" )
647 << " <" << algo->GetName() << "> is hidden by global <"
648 << aGlobIgnoAlgo->GetName() << ">");
650 else if ( !algo->NeedDiscreteBoundary() && !isGlobal)
652 // local algo is not hidden and hides algos on sub-shapes
653 if (checkConform && !aSubMesh->IsConform( algo ))
656 checkConform = false; // no more check conformity
657 INFOS( "ERROR: Local <" << algo->GetName() <<
658 "> would produce not conform mesh: "
659 "<Not Conform Mesh Allowed> hypotesis is missing");
660 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
661 theErrors.back().Set( SMESH_Hypothesis::HYP_NOTCONFORM, algo, false );
664 // sub-algos will be hidden by a local <algo> if <algo> does not support sub-meshes
665 if ( algo->SupportSubmeshes() )
667 SMESH_subMeshIteratorPtr revItSub =
668 aSubMesh->getDependsOnIterator( /*includeSelf=*/false, /*complexShapeFirst=*/true);
669 bool checkConform2 = false;
670 while ( revItSub->more() )
672 SMESH_subMesh* sm = revItSub->next();
673 checkConformIgnoredAlgos (aMesh, sm, aGlobIgnoAlgo,
674 algo, checkConform2, aCheckedMap, theErrors);
675 aCheckedMap.insert( sm );
684 //=======================================================================
685 //function : checkMissing
686 //purpose : notify on missing hypothesis
687 // Return false if algo or hipothesis is missing
688 //=======================================================================
690 static bool checkMissing(SMESH_Gen* aGen,
692 SMESH_subMesh* aSubMesh,
693 const int aTopAlgoDim,
695 const bool checkNoAlgo,
696 set<SMESH_subMesh*>& aCheckedMap,
697 list< SMESH_Gen::TAlgoStateError > & theErrors)
699 switch ( aSubMesh->GetSubShape().ShapeType() )
703 case TopAbs_SOLID: break; // check this sub-mesh, it can be meshed
705 return true; // not meshable sub-mesh
707 if ( aCheckedMap.count( aSubMesh ))
711 SMESH_Algo* algo = 0;
713 switch (aSubMesh->GetAlgoState())
715 case SMESH_subMesh::NO_ALGO: {
718 // should there be any algo?
719 int shapeDim = SMESH_Gen::GetShapeDim( aSubMesh->GetSubShape() );
720 if (aTopAlgoDim > shapeDim)
722 MESSAGE( "ERROR: " << shapeDim << "D algorithm is missing" );
724 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
725 theErrors.back().Set( SMESH_Hypothesis::HYP_MISSING, shapeDim, true );
730 case SMESH_subMesh::MISSING_HYP: {
731 // notify if an algo missing hyp is attached to aSubMesh
732 algo = aSubMesh->GetAlgo();
734 bool IsGlobalHypothesis = aGen->IsGlobalHypothesis( algo, aMesh );
735 if (!IsGlobalHypothesis || !globalChecked[ algo->GetDim() ])
737 TAlgoStateErrorName errName = SMESH_Hypothesis::HYP_MISSING;
738 SMESH_Hypothesis::Hypothesis_Status status;
739 algo->CheckHypothesis( aMesh, aSubMesh->GetSubShape(), status );
740 if ( status == SMESH_Hypothesis::HYP_BAD_PARAMETER ) {
741 MESSAGE( "ERROR: hypothesis of " << (IsGlobalHypothesis ? "Global " : "Local ")
742 << "<" << algo->GetName() << "> has a bad parameter value");
744 } else if ( status == SMESH_Hypothesis::HYP_BAD_GEOMETRY ) {
745 MESSAGE( "ERROR: " << (IsGlobalHypothesis ? "Global " : "Local ")
746 << "<" << algo->GetName() << "> assigned to mismatching geometry");
749 MESSAGE( "ERROR: " << (IsGlobalHypothesis ? "Global " : "Local ")
750 << "<" << algo->GetName() << "> misses some hypothesis");
752 if (IsGlobalHypothesis)
753 globalChecked[ algo->GetDim() ] = true;
754 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
755 theErrors.back().Set( errName, algo, IsGlobalHypothesis );
760 case SMESH_subMesh::HYP_OK:
761 algo = aSubMesh->GetAlgo();
763 if (!algo->NeedDiscreteBoundary())
765 SMESH_subMeshIteratorPtr itsub = aSubMesh->getDependsOnIterator( /*includeSelf=*/false,
766 /*complexShapeFirst=*/false);
767 while ( itsub->more() )
768 aCheckedMap.insert( itsub->next() );
774 // do not check under algo that hides sub-algos or
775 // re-start checking NO_ALGO state
777 bool isTopLocalAlgo =
778 ( aTopAlgoDim <= algo->GetDim() && !aGen->IsGlobalHypothesis( algo, aMesh ));
779 if (!algo->NeedDiscreteBoundary() || isTopLocalAlgo)
781 bool checkNoAlgo2 = ( algo->NeedDiscreteBoundary() );
782 SMESH_subMeshIteratorPtr itsub = aSubMesh->getDependsOnIterator( /*includeSelf=*/false,
783 /*complexShapeFirst=*/true);
784 while ( itsub->more() )
786 // sub-meshes should not be checked further more
787 SMESH_subMesh* sm = itsub->next();
791 //check algo on sub-meshes
792 int aTopAlgoDim2 = algo->GetDim();
793 if (!checkMissing (aGen, aMesh, sm, aTopAlgoDim2,
794 globalChecked, checkNoAlgo2, aCheckedMap, theErrors))
797 if (sm->GetAlgoState() == SMESH_subMesh::NO_ALGO )
798 checkNoAlgo2 = false;
801 aCheckedMap.insert( sm );
807 //=======================================================================
808 //function : CheckAlgoState
809 //purpose : notify on bad state of attached algos, return false
810 // if Compute() would fail because of some algo bad state
811 //=======================================================================
813 bool SMESH_Gen::CheckAlgoState(SMESH_Mesh& aMesh, const TopoDS_Shape& aShape)
815 list< TAlgoStateError > errors;
816 return GetAlgoState( aMesh, aShape, errors );
819 //=======================================================================
820 //function : GetAlgoState
821 //purpose : notify on bad state of attached algos, return false
822 // if Compute() would fail because of some algo bad state
823 // theErrors list contains problems description
824 //=======================================================================
826 bool SMESH_Gen::GetAlgoState(SMESH_Mesh& theMesh,
827 const TopoDS_Shape& theShape,
828 list< TAlgoStateError > & theErrors)
831 bool hasAlgo = false;
833 SMESH_subMesh* sm = theMesh.GetSubMesh(theShape);
834 const SMESHDS_Mesh* meshDS = theMesh.GetMeshDS();
835 TopoDS_Shape mainShape = meshDS->ShapeToMesh();
841 const SMESH_Algo* aGlobAlgoArr[] = {0,0,0,0};
843 const list<const SMESHDS_Hypothesis*>& listHyp = meshDS->GetHypothesis( mainShape );
844 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
845 for ( ; it != listHyp.end(); it++)
847 const SMESHDS_Hypothesis * aHyp = *it;
848 if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
851 const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
854 int dim = algo->GetDim();
855 aGlobAlgoArr[ dim ] = algo;
860 // --------------------------------------------------------
861 // info on algos that will be ignored because of ones that
862 // don't NeedDiscreteBoundary() attached to super-shapes,
863 // check that a conform mesh will be produced
864 // --------------------------------------------------------
867 // find a global algo possibly hiding sub-algos
869 const SMESH_Algo* aGlobIgnoAlgo = 0;
870 for (dim = 3; dim > 0; dim--)
872 if (aGlobAlgoArr[ dim ] &&
873 !aGlobAlgoArr[ dim ]->NeedDiscreteBoundary() /*&&
874 !aGlobAlgoArr[ dim ]->SupportSubmeshes()*/ )
876 aGlobIgnoAlgo = aGlobAlgoArr[ dim ];
881 set<SMESH_subMesh*> aCheckedSubs;
882 bool checkConform = ( !theMesh.IsNotConformAllowed() );
884 // loop on theShape and its sub-shapes
885 SMESH_subMeshIteratorPtr revItSub = sm->getDependsOnIterator( /*includeSelf=*/true,
886 /*complexShapeFirst=*/true);
887 while ( revItSub->more() )
889 SMESH_subMesh* smToCheck = revItSub->next();
890 if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
893 if ( aCheckedSubs.insert( smToCheck ).second ) // not yet checked
894 if (!checkConformIgnoredAlgos (theMesh, smToCheck, aGlobIgnoAlgo,
895 0, checkConform, aCheckedSubs, theErrors))
898 if ( smToCheck->GetAlgoState() != SMESH_subMesh::NO_ALGO )
902 // ----------------------------------------------------------------
903 // info on missing hypothesis and find out if all needed algos are
905 // ----------------------------------------------------------------
907 // find max dim of global algo
909 for (dim = 3; dim > 0; dim--)
911 if (aGlobAlgoArr[ dim ])
917 bool checkNoAlgo = theMesh.HasShapeToMesh() ? bool( aTopAlgoDim ) : false;
918 bool globalChecked[] = { false, false, false, false };
920 // loop on theShape and its sub-shapes
921 aCheckedSubs.clear();
922 revItSub = sm->getDependsOnIterator( /*includeSelf=*/true, /*complexShapeFirst=*/true);
923 while ( revItSub->more() )
925 SMESH_subMesh* smToCheck = revItSub->next();
926 if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
929 if (!checkMissing (this, theMesh, smToCheck, aTopAlgoDim,
930 globalChecked, checkNoAlgo, aCheckedSubs, theErrors))
933 if (smToCheck->GetAlgoState() == SMESH_subMesh::NO_ALGO )
940 theErrors.push_back( TAlgoStateError() );
941 theErrors.back().Set( SMESH_Hypothesis::HYP_MISSING, theMesh.HasShapeToMesh() ? 1 : 3, true );
947 //=======================================================================
948 //function : IsGlobalHypothesis
949 //purpose : check if theAlgo is attached to the main shape
950 //=======================================================================
952 bool SMESH_Gen::IsGlobalHypothesis(const SMESH_Hypothesis* theHyp, SMESH_Mesh& aMesh)
954 SMESH_HypoFilter filter( SMESH_HypoFilter::Is( theHyp ));
955 return aMesh.GetHypothesis( aMesh.GetMeshDS()->ShapeToMesh(), filter, false );
958 //================================================================================
960 * \brief Return paths to xml files of plugins
962 //================================================================================
964 std::vector< std::string > SMESH_Gen::GetPluginXMLPaths()
966 // Get paths to xml files of plugins
967 vector< string > xmlPaths;
969 if ( const char* meshersList = getenv("SMESH_MeshersList") )
971 string meshers = meshersList, plugin;
972 string::size_type from = 0, pos;
973 while ( from < meshers.size() )
975 // cut off plugin name
976 pos = meshers.find( ':', from );
977 if ( pos != string::npos )
978 plugin = meshers.substr( from, pos-from );
980 plugin = meshers.substr( from ), pos = meshers.size();
983 // get PLUGIN_ROOT_DIR path
984 string rootDirVar, pluginSubDir = plugin;
985 if ( plugin == "StdMeshers" )
986 rootDirVar = "SMESH", pluginSubDir = "smesh";
988 for ( pos = 0; pos < plugin.size(); ++pos )
989 rootDirVar += toupper( plugin[pos] );
990 rootDirVar += "_ROOT_DIR";
992 const char* rootDir = getenv( rootDirVar.c_str() );
993 if ( !rootDir || strlen(rootDir) == 0 )
995 rootDirVar = plugin + "_ROOT_DIR"; // HexoticPLUGIN_ROOT_DIR
996 rootDir = getenv( rootDirVar.c_str() );
997 if ( !rootDir || strlen(rootDir) == 0 ) continue;
1000 // get a separator from rootDir
1001 for ( pos = strlen( rootDir )-1; pos >= 0 && sep.empty(); --pos )
1002 if ( rootDir[pos] == '/' || rootDir[pos] == '\\' )
1008 if (sep.empty() ) sep = "\\";
1010 if (sep.empty() ) sep = "/";
1013 // get a path to resource file
1014 string xmlPath = rootDir;
1015 if ( xmlPath[ xmlPath.size()-1 ] != sep[0] )
1017 xmlPath += "share" + sep + "salome" + sep + "resources" + sep;
1018 for ( pos = 0; pos < pluginSubDir.size(); ++pos )
1019 xmlPath += tolower( pluginSubDir[pos] );
1020 xmlPath += sep + plugin + ".xml";
1023 fileOK = (GetFileAttributes(xmlPath.c_str()) != INVALID_FILE_ATTRIBUTES);
1025 fileOK = (access(xmlPath.c_str(), F_OK) == 0);
1028 xmlPaths.push_back( xmlPath );
1035 //=============================================================================
1037 * Finds algo to mesh a shape. Optionally returns a shape the found algo is bound to
1039 //=============================================================================
1041 SMESH_Algo *SMESH_Gen::GetAlgo(SMESH_Mesh & aMesh,
1042 const TopoDS_Shape & aShape,
1043 TopoDS_Shape* assignedTo)
1045 return GetAlgo( aMesh.GetSubMesh( aShape ), assignedTo );
1048 //=============================================================================
1050 * Finds algo to mesh a sub-mesh. Optionally returns a shape the found algo is bound to
1052 //=============================================================================
1054 SMESH_Algo *SMESH_Gen::GetAlgo(SMESH_subMesh * aSubMesh,
1055 TopoDS_Shape* assignedTo)
1057 if ( !aSubMesh ) return 0;
1059 const TopoDS_Shape & aShape = aSubMesh->GetSubShape();
1060 SMESH_Mesh& aMesh = *aSubMesh->GetFather();
1062 SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
1063 if ( aMesh.HasShapeToMesh() )
1064 filter.And( filter.IsApplicableTo( aShape ));
1066 typedef SMESH_Algo::Features AlgoData;
1068 TopoDS_Shape assignedToShape;
1070 (SMESH_Algo*) aMesh.GetHypothesis( aSubMesh, filter, true, &assignedToShape );
1073 aShape.ShapeType() == TopAbs_FACE &&
1074 !aShape.IsSame( assignedToShape ) &&
1075 SMESH_MesherHelper::NbAncestors( aShape, aMesh, TopAbs_SOLID ) > 1 )
1077 // Issue 0021559. If there is another 2D algo with different types of output
1078 // elements that can be used to mesh aShape, and 3D algos on adjacent SOLIDs
1079 // have different types of input elements, we choose a most appropriate 2D algo.
1081 // try to find a concurrent 2D algo
1082 filter.AndNot( filter.Is( algo ));
1083 TopoDS_Shape assignedToShape2;
1085 (SMESH_Algo*) aMesh.GetHypothesis( aSubMesh, filter, true, &assignedToShape2 );
1086 if ( algo2 && // algo found
1087 !assignedToShape2.IsSame( aMesh.GetShapeToMesh() ) && // algo is local
1088 ( SMESH_MesherHelper::GetGroupType( assignedToShape2 ) == // algo of the same level
1089 SMESH_MesherHelper::GetGroupType( assignedToShape )) &&
1090 aMesh.IsOrderOK( aMesh.GetSubMesh( assignedToShape2 ), // no forced order
1091 aMesh.GetSubMesh( assignedToShape )))
1093 // get algos on the adjacent SOLIDs
1094 filter.Init( filter.IsAlgo() ).And( filter.HasDim( 3 ));
1095 vector< SMESH_Algo* > algos3D;
1096 PShapeIteratorPtr solidIt = SMESH_MesherHelper::GetAncestors( aShape, aMesh,
1098 while ( const TopoDS_Shape* solid = solidIt->next() )
1099 if ( SMESH_Algo* algo3D = (SMESH_Algo*) aMesh.GetHypothesis( *solid, filter, true ))
1101 algos3D.push_back( algo3D );
1102 filter.AndNot( filter.HasName( algo3D->GetName() ));
1104 // check compatibility of algos
1105 if ( algos3D.size() > 1 )
1107 const AlgoData& algoData = algo->SMESH_Algo::GetFeatures();
1108 const AlgoData& algoData2 = algo2->SMESH_Algo::GetFeatures();
1109 const AlgoData& algoData3d0 = algos3D[0]->SMESH_Algo::GetFeatures();
1110 const AlgoData& algoData3d1 = algos3D[1]->SMESH_Algo::GetFeatures();
1111 if (( algoData2.IsCompatible( algoData3d0 ) &&
1112 algoData2.IsCompatible( algoData3d1 ))
1114 !(algoData.IsCompatible( algoData3d0 ) &&
1115 algoData.IsCompatible( algoData3d1 )))
1121 if ( assignedTo && algo )
1122 * assignedTo = assignedToShape;
1127 //=============================================================================
1129 * Returns StudyContextStruct for a study
1131 //=============================================================================
1133 StudyContextStruct *SMESH_Gen::GetStudyContext(int studyId)
1135 // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
1137 if (_mapStudyContext.find(studyId) == _mapStudyContext.end())
1139 _mapStudyContext[studyId] = new StudyContextStruct;
1140 _mapStudyContext[studyId]->myDocument = new SMESHDS_Document(studyId);
1142 StudyContextStruct *myStudyContext = _mapStudyContext[studyId];
1143 return myStudyContext;
1146 //================================================================================
1148 * \brief Return shape dimension by TopAbs_ShapeEnum
1150 //================================================================================
1152 int SMESH_Gen::GetShapeDim(const TopAbs_ShapeEnum & aShapeType)
1154 static vector<int> dim;
1157 dim.resize( TopAbs_SHAPE, -1 );
1158 dim[ TopAbs_COMPOUND ] = MeshDim_3D;
1159 dim[ TopAbs_COMPSOLID ] = MeshDim_3D;
1160 dim[ TopAbs_SOLID ] = MeshDim_3D;
1161 dim[ TopAbs_SHELL ] = MeshDim_2D;
1162 dim[ TopAbs_FACE ] = MeshDim_2D;
1163 dim[ TopAbs_WIRE ] = MeshDim_1D;
1164 dim[ TopAbs_EDGE ] = MeshDim_1D;
1165 dim[ TopAbs_VERTEX ] = MeshDim_0D;
1167 return dim[ aShapeType ];
1170 //=============================================================================
1172 * Genarate a new id unique withing this Gen
1174 //=============================================================================
1176 int SMESH_Gen::GetANewId()