1 // Copyright (C) 2007-2012 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.
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_MesherHelper.hxx"
39 #include "SMESH_subMesh.hxx"
41 #include "utilities.h"
43 #include "Utils_ExceptHandlers.hxx"
45 #include <TopoDS_Iterator.hxx>
46 #include <LDOMParser.hxx>
56 //=============================================================================
60 //=============================================================================
62 SMESH_Gen::SMESH_Gen()
64 MESSAGE("SMESH_Gen::SMESH_Gen");
67 _segmentation = _nbSegments = 10;
68 SMDS_Mesh::_meshList.clear();
69 MESSAGE(SMDS_Mesh::_meshList.size());
70 _counters = new counters(100);
71 #ifdef WITH_SMESH_CANCEL_COMPUTE
72 _compute_canceled = false;
77 //=============================================================================
81 //=============================================================================
83 SMESH_Gen::~SMESH_Gen()
85 MESSAGE("SMESH_Gen::~SMESH_Gen");
88 //=============================================================================
90 * Creates a mesh in a study.
91 * if (theIsEmbeddedMode) { mesh modification commands are not logged }
93 //=============================================================================
95 SMESH_Mesh* SMESH_Gen::CreateMesh(int theStudyId, bool theIsEmbeddedMode)
96 throw(SALOME_Exception)
98 Unexpect aCatch(SalomeException);
99 MESSAGE("SMESH_Gen::CreateMesh");
101 // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
102 StudyContextStruct *aStudyContext = GetStudyContext(theStudyId);
104 // create a new SMESH_mesh object
105 SMESH_Mesh *aMesh = new SMESH_Mesh(_localId++,
109 aStudyContext->myDocument);
110 aStudyContext->mapMesh[_localId-1] = aMesh;
115 //=============================================================================
119 //=============================================================================
121 bool SMESH_Gen::Compute(SMESH_Mesh & aMesh,
122 const TopoDS_Shape & aShape,
124 const ::MeshDimension aDim,
125 TSetOfInt* aShapesId)
127 MESSAGE("SMESH_Gen::Compute");
132 SMESH_subMesh *sm = aMesh.GetSubMesh(aShape);
134 const bool includeSelf = true;
135 const bool complexShapeFirst = true;
136 const int globalAlgoDim = 100;
138 SMESH_subMeshIteratorPtr smIt;
140 if ( anUpward ) // is called from below code here
142 // -----------------------------------------------
143 // mesh all the sub-shapes starting from vertices
144 // -----------------------------------------------
145 smIt = sm->getDependsOnIterator(includeSelf, !complexShapeFirst);
146 while ( smIt->more() )
148 SMESH_subMesh* smToCompute = smIt->next();
150 // do not mesh vertices of a pseudo shape
151 const TopAbs_ShapeEnum aShType = smToCompute->GetSubShape().ShapeType();
152 if ( !aMesh.HasShapeToMesh() && aShType == TopAbs_VERTEX )
155 // check for preview dimension limitations
156 if ( aShapesId && GetShapeDim( aShType ) > (int)aDim )
158 // clear compute state to not show previous compute errors
159 // if preview invoked less dimension less than previous
160 smToCompute->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
164 if (smToCompute->GetComputeState() == SMESH_subMesh::READY_TO_COMPUTE)
166 #ifdef WITH_SMESH_CANCEL_COMPUTE
167 if (_compute_canceled)
169 _sm_current = smToCompute;
171 smToCompute->ComputeStateEngine( SMESH_subMesh::COMPUTE );
172 #ifdef WITH_SMESH_CANCEL_COMPUTE
177 // we check all the submeshes here and detect if any of them failed to compute
178 if (smToCompute->GetComputeState() == SMESH_subMesh::FAILED_TO_COMPUTE)
180 else if ( aShapesId )
181 aShapesId->insert( smToCompute->GetId() );
183 //aMesh.GetMeshDS()->Modified();
188 // -----------------------------------------------------------------
189 // apply algos that DO NOT require Discreteized boundaries and DO NOT
190 // support submeshes, starting from the most complex shapes
191 // and collect submeshes with algos that DO support submeshes
192 // -----------------------------------------------------------------
193 list< SMESH_subMesh* > smWithAlgoSupportingSubmeshes;
195 // map to sort sm with same dim algos according to dim of
196 // the shape the algo assigned to (issue 0021217)
197 multimap< int, SMESH_subMesh* > shDim2sm;
198 multimap< int, SMESH_subMesh* >::reverse_iterator shDim2smIt;
199 TopoDS_Shape algoShape;
200 int prevShapeDim = -1;
202 smIt = sm->getDependsOnIterator(includeSelf, complexShapeFirst);
203 while ( smIt->more() )
205 SMESH_subMesh* smToCompute = smIt->next();
206 if ( smToCompute->GetComputeState() != SMESH_subMesh::READY_TO_COMPUTE )
209 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
210 int aShapeDim = GetShapeDim( aSubShape );
211 if ( aShapeDim < 1 ) break;
213 // check for preview dimension limitations
214 if ( aShapesId && aShapeDim > (int)aDim )
217 SMESH_Algo* algo = GetAlgo( aMesh, aSubShape, &algoShape );
218 if ( algo && !algo->NeedDiscreteBoundary() )
220 if ( algo->SupportSubmeshes() )
222 // reload sub-meshes from shDim2sm into smWithAlgoSupportingSubmeshes
223 // so that more local algos to go first
224 if ( prevShapeDim != aShapeDim )
226 prevShapeDim = aShapeDim;
227 for ( shDim2smIt = shDim2sm.rbegin(); shDim2smIt != shDim2sm.rend(); ++shDim2smIt )
228 if ( shDim2smIt->first == globalAlgoDim )
229 smWithAlgoSupportingSubmeshes.push_back( shDim2smIt->second );
231 smWithAlgoSupportingSubmeshes.push_front( shDim2smIt->second );
234 // add smToCompute to shDim2sm map
235 if ( algoShape.IsSame( aMesh.GetShapeToMesh() ))
237 aShapeDim = globalAlgoDim; // to compute last
241 aShapeDim = GetShapeDim( algoShape );
242 if ( algoShape.ShapeType() == TopAbs_COMPOUND )
244 TopoDS_Iterator it( algoShape );
245 aShapeDim += GetShapeDim( it.Value() );
248 shDim2sm.insert( make_pair( aShapeDim, smToCompute ));
252 #ifdef WITH_SMESH_CANCEL_COMPUTE
253 if (_compute_canceled)
255 _sm_current = smToCompute;
257 smToCompute->ComputeStateEngine( SMESH_subMesh::COMPUTE );
258 #ifdef WITH_SMESH_CANCEL_COMPUTE
262 aShapesId->insert( smToCompute->GetId() );
266 // reload sub-meshes from shDim2sm into smWithAlgoSupportingSubmeshes
267 for ( shDim2smIt = shDim2sm.rbegin(); shDim2smIt != shDim2sm.rend(); ++shDim2smIt )
268 if ( shDim2smIt->first == globalAlgoDim )
269 smWithAlgoSupportingSubmeshes.push_back( shDim2smIt->second );
271 smWithAlgoSupportingSubmeshes.push_front( shDim2smIt->second );
273 // ------------------------------------------------------------
274 // sort list of submeshes according to mesh order
275 // ------------------------------------------------------------
276 aMesh.SortByMeshOrder( smWithAlgoSupportingSubmeshes );
278 // ------------------------------------------------------------
279 // compute submeshes under shapes with algos that DO NOT require
280 // Discreteized boundaries and DO support submeshes
281 // ------------------------------------------------------------
282 list< SMESH_subMesh* >::iterator subIt, subEnd;
283 subIt = smWithAlgoSupportingSubmeshes.begin();
284 subEnd = smWithAlgoSupportingSubmeshes.end();
285 // start from lower shapes
286 for ( ; subIt != subEnd; ++subIt )
290 // get a shape the algo is assigned to
291 if ( !GetAlgo( aMesh, sm->GetSubShape(), & algoShape ))
292 continue; // strange...
294 // look for more local algos
295 smIt = sm->getDependsOnIterator(!includeSelf, !complexShapeFirst);
296 while ( smIt->more() )
298 SMESH_subMesh* smToCompute = smIt->next();
300 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
301 const int aShapeDim = GetShapeDim( aSubShape );
302 //if ( aSubShape.ShapeType() == TopAbs_VERTEX ) continue;
303 if ( aShapeDim < 1 ) continue;
305 // check for preview dimension limitations
306 if ( aShapesId && GetShapeDim( aSubShape.ShapeType() ) > (int)aDim )
309 SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
311 .And( SMESH_HypoFilter::IsApplicableTo( aSubShape ))
312 .And( SMESH_HypoFilter::IsMoreLocalThan( algoShape, aMesh ));
314 if ( SMESH_Algo* subAlgo = (SMESH_Algo*) aMesh.GetHypothesis( aSubShape, filter, true )) {
315 SMESH_Hypothesis::Hypothesis_Status status;
316 if ( subAlgo->CheckHypothesis( aMesh, aSubShape, status ))
317 // mesh a lower smToCompute starting from vertices
318 Compute( aMesh, aSubShape, /*anUpward=*/true, aDim, aShapesId );
322 // ----------------------------------------------------------
323 // apply the algos that do not require Discreteized boundaries
324 // ----------------------------------------------------------
325 for ( subIt = smWithAlgoSupportingSubmeshes.begin(); subIt != subEnd; ++subIt )
328 if ( sm->GetComputeState() == SMESH_subMesh::READY_TO_COMPUTE)
330 const TopAbs_ShapeEnum aShType = sm->GetSubShape().ShapeType();
331 // check for preview dimension limitations
332 if ( aShapesId && GetShapeDim( aShType ) > (int)aDim )
335 #ifdef WITH_SMESH_CANCEL_COMPUTE
336 if (_compute_canceled)
340 sm->ComputeStateEngine( SMESH_subMesh::COMPUTE );
341 #ifdef WITH_SMESH_CANCEL_COMPUTE
345 aShapesId->insert( sm->GetId() );
348 // -----------------------------------------------
349 // mesh the rest sub-shapes starting from vertices
350 // -----------------------------------------------
351 ret = Compute( aMesh, aShape, /*anUpward=*/true, aDim, aShapesId );
354 MESSAGE( "VSR - SMESH_Gen::Compute() finished, OK = " << ret);
357 SMESHDS_Mesh *myMesh = aMesh.GetMeshDS();
358 MESSAGE("*** compactMesh after compute");
359 myMesh->compactMesh();
361 // fix quadratic mesh by bending iternal links near concave boundary
362 if ( aShape.IsSame( aMesh.GetShapeToMesh() ) &&
363 !aShapesId ) // not preview
365 SMESH_MesherHelper aHelper( aMesh );
366 if ( aHelper.IsQuadraticMesh() != SMESH_MesherHelper::LINEAR )
368 aHelper.FixQuadraticElements( sm->GetComputeError() );
375 #ifdef WITH_SMESH_CANCEL_COMPUTE
376 //=============================================================================
378 * Prepare Compute a mesh
380 //=============================================================================
381 void SMESH_Gen::PrepareCompute(SMESH_Mesh & aMesh,
382 const TopoDS_Shape & aShape)
384 _compute_canceled = false;
387 //=============================================================================
389 * Cancel Compute a mesh
391 //=============================================================================
392 void SMESH_Gen::CancelCompute(SMESH_Mesh & aMesh,
393 const TopoDS_Shape & aShape)
395 _compute_canceled = true;
398 _sm_current->ComputeStateEngine( SMESH_subMesh::COMPUTE_CANCELED );
403 //=============================================================================
407 //=============================================================================
409 bool SMESH_Gen::Evaluate(SMESH_Mesh & aMesh,
410 const TopoDS_Shape & aShape,
411 MapShapeNbElems& aResMap,
413 TSetOfInt* aShapesId)
415 MESSAGE("SMESH_Gen::Evaluate");
419 SMESH_subMesh *sm = aMesh.GetSubMesh(aShape);
421 const bool includeSelf = true;
422 const bool complexShapeFirst = true;
423 SMESH_subMeshIteratorPtr smIt;
425 if ( anUpward ) { // is called from below code here
426 // -----------------------------------------------
427 // mesh all the sub-shapes starting from vertices
428 // -----------------------------------------------
429 smIt = sm->getDependsOnIterator(includeSelf, !complexShapeFirst);
430 while ( smIt->more() ) {
431 SMESH_subMesh* smToCompute = smIt->next();
433 // do not mesh vertices of a pseudo shape
434 const TopAbs_ShapeEnum aShType = smToCompute->GetSubShape().ShapeType();
435 //if ( !aMesh.HasShapeToMesh() && aShType == TopAbs_VERTEX )
437 if ( !aMesh.HasShapeToMesh() ) {
438 if( aShType == TopAbs_VERTEX || aShType == TopAbs_WIRE ||
439 aShType == TopAbs_SHELL )
443 smToCompute->Evaluate(aResMap);
445 aShapesId->insert( smToCompute->GetId() );
450 // -----------------------------------------------------------------
451 // apply algos that DO NOT require Discreteized boundaries and DO NOT
452 // support submeshes, starting from the most complex shapes
453 // and collect submeshes with algos that DO support submeshes
454 // -----------------------------------------------------------------
455 list< SMESH_subMesh* > smWithAlgoSupportingSubmeshes;
456 smIt = sm->getDependsOnIterator(includeSelf, complexShapeFirst);
457 while ( smIt->more() ) {
458 SMESH_subMesh* smToCompute = smIt->next();
459 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
460 const int aShapeDim = GetShapeDim( aSubShape );
461 if ( aShapeDim < 1 ) break;
463 SMESH_Algo* algo = GetAlgo( aMesh, aSubShape );
464 if ( algo && !algo->NeedDiscreteBoundary() ) {
465 if ( algo->SupportSubmeshes() ) {
466 smWithAlgoSupportingSubmeshes.push_front( smToCompute );
469 smToCompute->Evaluate(aResMap);
471 aShapesId->insert( smToCompute->GetId() );
476 // ------------------------------------------------------------
477 // sort list of meshes according to mesh order
478 // ------------------------------------------------------------
479 aMesh.SortByMeshOrder( smWithAlgoSupportingSubmeshes );
481 // ------------------------------------------------------------
482 // compute submeshes under shapes with algos that DO NOT require
483 // Discreteized boundaries and DO support submeshes
484 // ------------------------------------------------------------
485 list< SMESH_subMesh* >::iterator subIt, subEnd;
486 subIt = smWithAlgoSupportingSubmeshes.begin();
487 subEnd = smWithAlgoSupportingSubmeshes.end();
488 // start from lower shapes
489 for ( ; subIt != subEnd; ++subIt ) {
492 // get a shape the algo is assigned to
493 TopoDS_Shape algoShape;
494 if ( !GetAlgo( aMesh, sm->GetSubShape(), & algoShape ))
495 continue; // strange...
497 // look for more local algos
498 smIt = sm->getDependsOnIterator(!includeSelf, !complexShapeFirst);
499 while ( smIt->more() ) {
500 SMESH_subMesh* smToCompute = smIt->next();
502 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
503 const int aShapeDim = GetShapeDim( aSubShape );
504 if ( aShapeDim < 1 ) continue;
506 //const TopAbs_ShapeEnum aShType = smToCompute->GetSubShape().ShapeType();
508 SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
510 .And( SMESH_HypoFilter::IsApplicableTo( aSubShape ))
511 .And( SMESH_HypoFilter::IsMoreLocalThan( algoShape, aMesh ));
513 if ( SMESH_Algo* subAlgo = (SMESH_Algo*) aMesh.GetHypothesis( aSubShape, filter, true )) {
514 SMESH_Hypothesis::Hypothesis_Status status;
515 if ( subAlgo->CheckHypothesis( aMesh, aSubShape, status ))
516 // mesh a lower smToCompute starting from vertices
517 Evaluate( aMesh, aSubShape, aResMap, /*anUpward=*/true, aShapesId );
521 // ----------------------------------------------------------
522 // apply the algos that do not require Discreteized boundaries
523 // ----------------------------------------------------------
524 for ( subIt = smWithAlgoSupportingSubmeshes.begin(); subIt != subEnd; ++subIt )
527 sm->Evaluate(aResMap);
529 aShapesId->insert( sm->GetId() );
532 // -----------------------------------------------
533 // mesh the rest sub-shapes starting from vertices
534 // -----------------------------------------------
535 ret = Evaluate( aMesh, aShape, aResMap, /*anUpward=*/true, aShapesId );
538 MESSAGE( "VSR - SMESH_Gen::Evaluate() finished, OK = " << ret);
543 //=======================================================================
544 //function : checkConformIgnoredAlgos
546 //=======================================================================
548 static bool checkConformIgnoredAlgos(SMESH_Mesh& aMesh,
549 SMESH_subMesh* aSubMesh,
550 const SMESH_Algo* aGlobIgnoAlgo,
551 const SMESH_Algo* aLocIgnoAlgo,
553 set<SMESH_subMesh*>& aCheckedMap,
554 list< SMESH_Gen::TAlgoStateError > & theErrors)
557 if ( aSubMesh->GetSubShape().ShapeType() == TopAbs_VERTEX)
563 const list<const SMESHDS_Hypothesis*>& listHyp =
564 aMesh.GetMeshDS()->GetHypothesis( aSubMesh->GetSubShape() );
565 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
566 for ( ; it != listHyp.end(); it++)
568 const SMESHDS_Hypothesis * aHyp = *it;
569 if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
572 const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
575 if ( aLocIgnoAlgo ) // algo is hidden by a local algo of upper dim
577 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
578 theErrors.back().Set( SMESH_Hypothesis::HYP_HIDDEN_ALGO, algo, false );
579 INFOS( "Local <" << algo->GetName() << "> is hidden by local <"
580 << aLocIgnoAlgo->GetName() << ">");
584 bool isGlobal = (aMesh.IsMainShape( aSubMesh->GetSubShape() ));
585 int dim = algo->GetDim();
586 int aMaxGlobIgnoDim = ( aGlobIgnoAlgo ? aGlobIgnoAlgo->GetDim() : -1 );
588 if ( dim < aMaxGlobIgnoDim &&
589 ( isGlobal || !aGlobIgnoAlgo->SupportSubmeshes() ))
591 // algo is hidden by a global algo
592 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
593 theErrors.back().Set( SMESH_Hypothesis::HYP_HIDDEN_ALGO, algo, true );
594 INFOS( ( isGlobal ? "Global" : "Local" )
595 << " <" << algo->GetName() << "> is hidden by global <"
596 << aGlobIgnoAlgo->GetName() << ">");
598 else if ( !algo->NeedDiscreteBoundary() && !isGlobal)
600 // local algo is not hidden and hides algos on sub-shapes
601 if (checkConform && !aSubMesh->IsConform( algo ))
604 checkConform = false; // no more check conformity
605 INFOS( "ERROR: Local <" << algo->GetName() <<
606 "> would produce not conform mesh: "
607 "<Not Conform Mesh Allowed> hypotesis is missing");
608 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
609 theErrors.back().Set( SMESH_Hypothesis::HYP_NOTCONFORM, algo, false );
612 // sub-algos will be hidden by a local <algo>
613 SMESH_subMeshIteratorPtr revItSub =
614 aSubMesh->getDependsOnIterator( /*includeSelf=*/false, /*complexShapeFirst=*/true);
615 bool checkConform2 = false;
616 while ( revItSub->more() )
618 SMESH_subMesh* sm = revItSub->next();
619 checkConformIgnoredAlgos (aMesh, sm, aGlobIgnoAlgo,
620 algo, checkConform2, aCheckedMap, theErrors);
621 aCheckedMap.insert( sm );
630 //=======================================================================
631 //function : checkMissing
632 //purpose : notify on missing hypothesis
633 // Return false if algo or hipothesis is missing
634 //=======================================================================
636 static bool checkMissing(SMESH_Gen* aGen,
638 SMESH_subMesh* aSubMesh,
639 const int aTopAlgoDim,
641 const bool checkNoAlgo,
642 set<SMESH_subMesh*>& aCheckedMap,
643 list< SMESH_Gen::TAlgoStateError > & theErrors)
645 if ( aSubMesh->GetSubShape().ShapeType() == TopAbs_VERTEX ||
646 aCheckedMap.count( aSubMesh ))
649 //MESSAGE("=====checkMissing");
652 SMESH_Algo* algo = 0;
654 switch (aSubMesh->GetAlgoState())
656 case SMESH_subMesh::NO_ALGO: {
659 // should there be any algo?
660 int shapeDim = SMESH_Gen::GetShapeDim( aSubMesh->GetSubShape() );
661 if (aTopAlgoDim > shapeDim)
663 MESSAGE( "ERROR: " << shapeDim << "D algorithm is missing" );
665 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
666 theErrors.back().Set( SMESH_Hypothesis::HYP_MISSING, shapeDim, true );
671 case SMESH_subMesh::MISSING_HYP: {
672 // notify if an algo missing hyp is attached to aSubMesh
673 algo = aGen->GetAlgo( aMesh, aSubMesh->GetSubShape() );
675 bool IsGlobalHypothesis = aGen->IsGlobalHypothesis( algo, aMesh );
676 if (!IsGlobalHypothesis || !globalChecked[ algo->GetDim() ])
678 TAlgoStateErrorName errName = SMESH_Hypothesis::HYP_MISSING;
679 SMESH_Hypothesis::Hypothesis_Status status;
680 algo->CheckHypothesis( aMesh, aSubMesh->GetSubShape(), status );
681 if ( status == SMESH_Hypothesis::HYP_BAD_PARAMETER ) {
682 MESSAGE( "ERROR: hypothesis of " << (IsGlobalHypothesis ? "Global " : "Local ")
683 << "<" << algo->GetName() << "> has a bad parameter value");
685 } else if ( status == SMESH_Hypothesis::HYP_BAD_GEOMETRY ) {
686 MESSAGE( "ERROR: " << (IsGlobalHypothesis ? "Global " : "Local ")
687 << "<" << algo->GetName() << "> assigned to mismatching geometry");
690 MESSAGE( "ERROR: " << (IsGlobalHypothesis ? "Global " : "Local ")
691 << "<" << algo->GetName() << "> misses some hypothesis");
693 if (IsGlobalHypothesis)
694 globalChecked[ algo->GetDim() ] = true;
695 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
696 theErrors.back().Set( errName, algo, IsGlobalHypothesis );
701 case SMESH_subMesh::HYP_OK:
702 algo = aSubMesh->GetAlgo();
704 if (!algo->NeedDiscreteBoundary())
706 SMESH_subMeshIteratorPtr itsub = aSubMesh->getDependsOnIterator( /*includeSelf=*/false,
707 /*complexShapeFirst=*/false);
708 while ( itsub->more() )
709 aCheckedMap.insert( itsub->next() );
715 // do not check under algo that hides sub-algos or
716 // re-start checking NO_ALGO state
718 bool isTopLocalAlgo =
719 ( aTopAlgoDim <= algo->GetDim() && !aGen->IsGlobalHypothesis( algo, aMesh ));
720 if (!algo->NeedDiscreteBoundary() || isTopLocalAlgo)
722 bool checkNoAlgo2 = ( algo->NeedDiscreteBoundary() );
723 SMESH_subMeshIteratorPtr itsub = aSubMesh->getDependsOnIterator( /*includeSelf=*/false,
724 /*complexShapeFirst=*/false);
725 while ( itsub->more() )
727 // sub-meshes should not be checked further more
728 SMESH_subMesh* sm = itsub->next();
732 //check algo on sub-meshes
733 int aTopAlgoDim2 = algo->GetDim();
734 if (!checkMissing (aGen, aMesh, sm, aTopAlgoDim2,
735 globalChecked, checkNoAlgo2, aCheckedMap, theErrors))
738 if (sm->GetAlgoState() == SMESH_subMesh::NO_ALGO )
739 checkNoAlgo2 = false;
742 aCheckedMap.insert( sm );
748 //=======================================================================
749 //function : CheckAlgoState
750 //purpose : notify on bad state of attached algos, return false
751 // if Compute() would fail because of some algo bad state
752 //=======================================================================
754 bool SMESH_Gen::CheckAlgoState(SMESH_Mesh& aMesh, const TopoDS_Shape& aShape)
756 list< TAlgoStateError > errors;
757 return GetAlgoState( aMesh, aShape, errors );
760 //=======================================================================
761 //function : GetAlgoState
762 //purpose : notify on bad state of attached algos, return false
763 // if Compute() would fail because of some algo bad state
764 // theErrors list contains problems description
765 //=======================================================================
767 bool SMESH_Gen::GetAlgoState(SMESH_Mesh& theMesh,
768 const TopoDS_Shape& theShape,
769 list< TAlgoStateError > & theErrors)
771 //MESSAGE("SMESH_Gen::CheckAlgoState");
774 bool hasAlgo = false;
776 SMESH_subMesh* sm = theMesh.GetSubMesh(theShape);
777 const SMESHDS_Mesh* meshDS = theMesh.GetMeshDS();
778 TopoDS_Shape mainShape = meshDS->ShapeToMesh();
784 const SMESH_Algo* aGlobAlgoArr[] = {0,0,0,0};
786 const list<const SMESHDS_Hypothesis*>& listHyp = meshDS->GetHypothesis( mainShape );
787 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
788 for ( ; it != listHyp.end(); it++)
790 const SMESHDS_Hypothesis * aHyp = *it;
791 if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
794 const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
797 int dim = algo->GetDim();
798 aGlobAlgoArr[ dim ] = algo;
803 // --------------------------------------------------------
804 // info on algos that will be ignored because of ones that
805 // don't NeedDiscreteBoundary() attached to super-shapes,
806 // check that a conform mesh will be produced
807 // --------------------------------------------------------
810 // find a global algo possibly hiding sub-algos
812 const SMESH_Algo* aGlobIgnoAlgo = 0;
813 for (dim = 3; dim > 0; dim--)
815 if (aGlobAlgoArr[ dim ] &&
816 !aGlobAlgoArr[ dim ]->NeedDiscreteBoundary() /*&&
817 !aGlobAlgoArr[ dim ]->SupportSubmeshes()*/ )
819 aGlobIgnoAlgo = aGlobAlgoArr[ dim ];
824 set<SMESH_subMesh*> aCheckedSubs;
825 bool checkConform = ( !theMesh.IsNotConformAllowed() );
827 // loop on theShape and its sub-shapes
828 SMESH_subMeshIteratorPtr revItSub = sm->getDependsOnIterator( /*includeSelf=*/true,
829 /*complexShapeFirst=*/true);
830 while ( revItSub->more() )
832 SMESH_subMesh* smToCheck = revItSub->next();
833 if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
836 if ( aCheckedSubs.insert( smToCheck ).second ) // not yet checked
837 if (!checkConformIgnoredAlgos (theMesh, smToCheck, aGlobIgnoAlgo,
838 0, checkConform, aCheckedSubs, theErrors))
841 if ( smToCheck->GetAlgoState() != SMESH_subMesh::NO_ALGO )
845 // ----------------------------------------------------------------
846 // info on missing hypothesis and find out if all needed algos are
848 // ----------------------------------------------------------------
850 //MESSAGE( "---info on missing hypothesis and find out if all needed algos are");
852 // find max dim of global algo
854 for (dim = 3; dim > 0; dim--)
856 if (aGlobAlgoArr[ dim ])
862 bool checkNoAlgo = theMesh.HasShapeToMesh() ? bool( aTopAlgoDim ) : false;
863 bool globalChecked[] = { false, false, false, false };
865 // loop on theShape and its sub-shapes
866 aCheckedSubs.clear();
867 revItSub = sm->getDependsOnIterator( /*includeSelf=*/true, /*complexShapeFirst=*/true);
868 while ( revItSub->more() )
870 SMESH_subMesh* smToCheck = revItSub->next();
871 if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
874 if (!checkMissing (this, theMesh, smToCheck, aTopAlgoDim,
875 globalChecked, checkNoAlgo, aCheckedSubs, theErrors))
878 if (smToCheck->GetAlgoState() == SMESH_subMesh::NO_ALGO )
885 INFOS( "None algorithm attached" );
886 theErrors.push_back( TAlgoStateError() );
887 theErrors.back().Set( SMESH_Hypothesis::HYP_MISSING, 1, true );
893 //=======================================================================
894 //function : IsGlobalHypothesis
895 //purpose : check if theAlgo is attached to the main shape
896 //=======================================================================
898 bool SMESH_Gen::IsGlobalHypothesis(const SMESH_Hypothesis* theHyp, SMESH_Mesh& aMesh)
900 SMESH_HypoFilter filter( SMESH_HypoFilter::Is( theHyp ));
901 return aMesh.GetHypothesis( aMesh.GetMeshDS()->ShapeToMesh(), filter, false );
904 //================================================================================
906 * \brief Return paths to xml files of plugins
908 //================================================================================
910 std::vector< std::string > SMESH_Gen::GetPluginXMLPaths()
912 // Get paths to xml files of plugins
913 vector< string > xmlPaths;
915 if ( const char* meshersList = getenv("SMESH_MeshersList") )
917 string meshers = meshersList, plugin;
918 string::size_type from = 0, pos;
919 while ( from < meshers.size() )
921 // cut off plugin name
922 pos = meshers.find( ':', from );
923 if ( pos != string::npos )
924 plugin = meshers.substr( from, pos-from );
926 plugin = meshers.substr( from ), pos = meshers.size();
929 // get PLUGIN_ROOT_DIR path
930 string rootDirVar, pluginSubDir = plugin;
931 if ( plugin == "StdMeshers" )
932 rootDirVar = "SMESH", pluginSubDir = "smesh";
934 for ( pos = 0; pos < plugin.size(); ++pos )
935 rootDirVar += toupper( plugin[pos] );
936 rootDirVar += "_ROOT_DIR";
938 const char* rootDir = getenv( rootDirVar.c_str() );
939 if ( !rootDir || strlen(rootDir) == 0 )
941 rootDirVar = plugin + "_ROOT_DIR"; // HexoticPLUGIN_ROOT_DIR
942 rootDir = getenv( rootDirVar.c_str() );
943 if ( !rootDir || strlen(rootDir) == 0 ) continue;
946 // get a separator from rootDir
947 for ( pos = strlen( rootDir )-1; pos >= 0 && sep.empty(); --pos )
948 if ( rootDir[pos] == '/' || rootDir[pos] == '\\' )
954 if (sep.empty() ) sep = "\\";
956 if (sep.empty() ) sep = "/";
959 // get a path to resource file
960 string xmlPath = rootDir;
961 if ( xmlPath[ xmlPath.size()-1 ] != sep[0] )
963 xmlPath += "share" + sep + "salome" + sep + "resources" + sep;
964 for ( pos = 0; pos < pluginSubDir.size(); ++pos )
965 xmlPath += tolower( pluginSubDir[pos] );
966 xmlPath += sep + plugin + ".xml";
969 fileOK = (GetFileAttributes(xmlPath.c_str()) != INVALID_FILE_ATTRIBUTES);
971 fileOK = (access(xmlPath.c_str(), F_OK) == 0);
974 xmlPaths.push_back( xmlPath );
981 //=======================================================================
982 namespace // Access to type of input and output of an algorithm
983 //=======================================================================
988 set<SMDSAbs_GeometryType> _inElemTypes; // acceptable types of input mesh element
989 set<SMDSAbs_GeometryType> _outElemTypes; // produced types of mesh elements
991 bool IsCompatible( const AlgoData& algo2 ) const
993 if ( _dim > algo2._dim ) return algo2.IsCompatible( *this );
994 // algo2 is of highter dimension
995 if ( _outElemTypes.empty() || algo2._inElemTypes.empty() )
997 bool compatible = true;
998 set<SMDSAbs_GeometryType>::const_iterator myOutType = _outElemTypes.begin();
999 for ( ; myOutType != _outElemTypes.end() && compatible; ++myOutType )
1000 compatible = algo2._inElemTypes.count( *myOutType );
1005 //================================================================================
1007 * \brief Return AlgoData of the algorithm
1009 //================================================================================
1011 const AlgoData& getAlgoData( const SMESH_Algo* algo )
1013 static map< string, AlgoData > theDataByName;
1014 if ( theDataByName.empty() )
1016 // Read Plugin.xml files
1017 vector< string > xmlPaths = SMESH_Gen::GetPluginXMLPaths();
1018 LDOMParser xmlParser;
1019 for ( size_t iXML = 0; iXML < xmlPaths.size(); ++iXML )
1021 bool error = xmlParser.parse( xmlPaths[iXML].c_str() );
1024 TCollection_AsciiString data;
1025 INFOS( xmlParser.GetError(data) );
1028 // <algorithm type="Regular_1D"
1031 // output="QUAD,TRIA">
1033 LDOM_Document xmlDoc = xmlParser.getDocument();
1034 LDOM_NodeList algoNodeList = xmlDoc.getElementsByTagName( "algorithm" );
1035 for ( int i = 0; i < algoNodeList.getLength(); ++i )
1037 LDOM_Node algoNode = algoNodeList.item( i );
1038 LDOM_Element& algoElem = (LDOM_Element&) algoNode;
1039 TCollection_AsciiString algoType = algoElem.getAttribute("type");
1040 TCollection_AsciiString input = algoElem.getAttribute("input");
1041 TCollection_AsciiString output = algoElem.getAttribute("output");
1042 TCollection_AsciiString dim = algoElem.getAttribute("dim");
1043 if ( algoType.IsEmpty() ) continue;
1044 AlgoData & data = theDataByName[ algoType.ToCString() ];
1045 data._dim = dim.IntegerValue();
1046 for ( int isInput = 0; isInput < 2; ++isInput )
1048 TCollection_AsciiString& typeStr = isInput ? input : output;
1049 set<SMDSAbs_GeometryType>& typeSet = isInput ? data._inElemTypes : data._outElemTypes;
1051 while ( beg <= typeStr.Length() )
1053 while ( beg < typeStr.Length() && !isalpha( typeStr.Value( beg ) ))
1056 while ( end < typeStr.Length() && isalpha( typeStr.Value( end + 1 ) ))
1060 TCollection_AsciiString typeName = typeStr.SubString( beg, end );
1061 if ( typeName == "EDGE" ) typeSet.insert( SMDSGeom_EDGE );
1062 else if ( typeName == "TRIA" ) typeSet.insert( SMDSGeom_TRIANGLE );
1063 else if ( typeName == "QUAD" ) typeSet.insert( SMDSGeom_QUADRANGLE );
1071 return theDataByName[ algo->GetName() ];
1075 //=============================================================================
1077 * Finds algo to mesh a shape. Optionally returns a shape the found algo is bound to
1079 //=============================================================================
1081 SMESH_Algo *SMESH_Gen::GetAlgo(SMESH_Mesh & aMesh,
1082 const TopoDS_Shape & aShape,
1083 TopoDS_Shape* assignedTo)
1085 SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
1086 filter.And( filter.IsApplicableTo( aShape ));
1088 TopoDS_Shape assignedToShape;
1090 (SMESH_Algo*) aMesh.GetHypothesis( aShape, filter, true, &assignedToShape );
1093 aShape.ShapeType() == TopAbs_FACE &&
1094 !aShape.IsSame( assignedToShape ) &&
1095 SMESH_MesherHelper::NbAncestors( aShape, aMesh, TopAbs_SOLID ) > 1 )
1097 // Issue 0021559. If there is another 2D algo with different types of output
1098 // elements that can be used to mesh aShape, and 3D algos on adjacent SOLIDs
1099 // have different types of input elements, we choose a most appropriate 2D algo.
1101 // try to find a concurrent 2D algo
1102 filter.AndNot( filter.Is( algo ));
1103 TopoDS_Shape assignedToShape2;
1105 (SMESH_Algo*) aMesh.GetHypothesis( aShape, filter, true, &assignedToShape2 );
1106 if ( algo2 && // algo found
1107 !assignedToShape2.IsSame( aMesh.GetShapeToMesh() ) && // algo is local
1108 ( SMESH_MesherHelper::GetGroupType( assignedToShape2 ) == // algo of the same level
1109 SMESH_MesherHelper::GetGroupType( assignedToShape )) &&
1110 aMesh.IsOrderOK( aMesh.GetSubMesh( assignedToShape2 ), // no forced order
1111 aMesh.GetSubMesh( assignedToShape )))
1113 // get algos on the adjacent SOLIDs
1114 filter.Init( filter.IsAlgo() ).And( filter.HasDim( 3 ));
1115 vector< SMESH_Algo* > algos3D;
1116 PShapeIteratorPtr solidIt = SMESH_MesherHelper::GetAncestors( aShape, aMesh,
1118 while ( const TopoDS_Shape* solid = solidIt->next() )
1119 if ( SMESH_Algo* algo3D = (SMESH_Algo*) aMesh.GetHypothesis( *solid, filter, true ))
1121 algos3D.push_back( algo3D );
1122 filter.AndNot( filter.HasName( algo3D->GetName() ));
1124 // check compatibility of algos
1125 if ( algos3D.size() > 1 )
1127 const AlgoData& algoData = getAlgoData( algo );
1128 const AlgoData& algoData2 = getAlgoData( algo2 );
1129 const AlgoData& algoData3d0 = getAlgoData( algos3D[0] );
1130 const AlgoData& algoData3d1 = getAlgoData( algos3D[1] );
1131 if (( algoData2.IsCompatible( algoData3d0 ) &&
1132 algoData2.IsCompatible( algoData3d1 ))
1134 !(algoData.IsCompatible( algoData3d0 ) &&
1135 algoData.IsCompatible( algoData3d1 )))
1141 if ( assignedTo && algo )
1142 * assignedTo = assignedToShape;
1147 //=============================================================================
1149 * Returns StudyContextStruct for a study
1151 //=============================================================================
1153 StudyContextStruct *SMESH_Gen::GetStudyContext(int studyId)
1155 // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
1157 if (_mapStudyContext.find(studyId) == _mapStudyContext.end())
1159 _mapStudyContext[studyId] = new StudyContextStruct;
1160 _mapStudyContext[studyId]->myDocument = new SMESHDS_Document(studyId);
1162 StudyContextStruct *myStudyContext = _mapStudyContext[studyId];
1163 return myStudyContext;
1166 //================================================================================
1168 * \brief Return shape dimension by TopAbs_ShapeEnum
1170 //================================================================================
1172 int SMESH_Gen::GetShapeDim(const TopAbs_ShapeEnum & aShapeType)
1174 static vector<int> dim;
1177 dim.resize( TopAbs_SHAPE, -1 );
1178 dim[ TopAbs_COMPOUND ] = MeshDim_3D;
1179 dim[ TopAbs_COMPSOLID ] = MeshDim_3D;
1180 dim[ TopAbs_SOLID ] = MeshDim_3D;
1181 dim[ TopAbs_SHELL ] = MeshDim_2D;
1182 dim[ TopAbs_FACE ] = MeshDim_2D;
1183 dim[ TopAbs_WIRE ] = MeshDim_1D;
1184 dim[ TopAbs_EDGE ] = MeshDim_1D;
1185 dim[ TopAbs_VERTEX ] = MeshDim_0D;
1187 return dim[ aShapeType ];
1190 //=============================================================================
1192 * Genarate a new id unique withing this Gen
1194 //=============================================================================
1196 int SMESH_Gen::GetANewId()