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>
52 //=============================================================================
56 //=============================================================================
58 SMESH_Gen::SMESH_Gen()
60 MESSAGE("SMESH_Gen::SMESH_Gen");
63 _segmentation = _nbSegments = 10;
64 SMDS_Mesh::_meshList.clear();
65 MESSAGE(SMDS_Mesh::_meshList.size());
66 _counters = new counters(100);
67 #ifdef WITH_SMESH_CANCEL_COMPUTE
68 _compute_canceled = false;
73 //=============================================================================
77 //=============================================================================
79 SMESH_Gen::~SMESH_Gen()
81 MESSAGE("SMESH_Gen::~SMESH_Gen");
84 //=============================================================================
86 * Creates a mesh in a study.
87 * if (theIsEmbeddedMode) { mesh modification commands are not logged }
89 //=============================================================================
91 SMESH_Mesh* SMESH_Gen::CreateMesh(int theStudyId, bool theIsEmbeddedMode)
92 throw(SALOME_Exception)
94 Unexpect aCatch(SalomeException);
95 MESSAGE("SMESH_Gen::CreateMesh");
97 // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
98 StudyContextStruct *aStudyContext = GetStudyContext(theStudyId);
100 // create a new SMESH_mesh object
101 SMESH_Mesh *aMesh = new SMESH_Mesh(_localId++,
105 aStudyContext->myDocument);
106 aStudyContext->mapMesh[_localId-1] = aMesh;
111 //=============================================================================
115 //=============================================================================
117 bool SMESH_Gen::Compute(SMESH_Mesh & aMesh,
118 const TopoDS_Shape & aShape,
120 const ::MeshDimension aDim,
121 TSetOfInt* aShapesId)
123 MESSAGE("SMESH_Gen::Compute");
128 SMESH_subMesh *sm = aMesh.GetSubMesh(aShape);
130 const bool includeSelf = true;
131 const bool complexShapeFirst = true;
132 const int globalAlgoDim = 100;
134 SMESH_subMeshIteratorPtr smIt;
136 if ( anUpward ) // is called from below code here
138 // -----------------------------------------------
139 // mesh all the sub-shapes starting from vertices
140 // -----------------------------------------------
141 smIt = sm->getDependsOnIterator(includeSelf, !complexShapeFirst);
142 while ( smIt->more() )
144 SMESH_subMesh* smToCompute = smIt->next();
146 // do not mesh vertices of a pseudo shape
147 const TopAbs_ShapeEnum aShType = smToCompute->GetSubShape().ShapeType();
148 if ( !aMesh.HasShapeToMesh() && aShType == TopAbs_VERTEX )
151 // check for preview dimension limitations
152 if ( aShapesId && GetShapeDim( aShType ) > (int)aDim )
154 // clear compute state to not show previous compute errors
155 // if preview invoked less dimension less than previous
156 smToCompute->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
160 if (smToCompute->GetComputeState() == SMESH_subMesh::READY_TO_COMPUTE)
162 #ifdef WITH_SMESH_CANCEL_COMPUTE
163 if (_compute_canceled)
165 _sm_current = smToCompute;
167 smToCompute->ComputeStateEngine( SMESH_subMesh::COMPUTE );
168 #ifdef WITH_SMESH_CANCEL_COMPUTE
173 // we check all the submeshes here and detect if any of them failed to compute
174 if (smToCompute->GetComputeState() == SMESH_subMesh::FAILED_TO_COMPUTE)
176 else if ( aShapesId )
177 aShapesId->insert( smToCompute->GetId() );
179 //aMesh.GetMeshDS()->Modified();
184 // -----------------------------------------------------------------
185 // apply algos that DO NOT require Discreteized boundaries and DO NOT
186 // support submeshes, starting from the most complex shapes
187 // and collect submeshes with algos that DO support submeshes
188 // -----------------------------------------------------------------
189 list< SMESH_subMesh* > smWithAlgoSupportingSubmeshes;
191 // map to sort sm with same dim algos according to dim of
192 // the shape the algo assigned to (issue 0021217)
193 multimap< int, SMESH_subMesh* > shDim2sm;
194 multimap< int, SMESH_subMesh* >::reverse_iterator shDim2smIt;
195 TopoDS_Shape algoShape;
196 int prevShapeDim = -1;
198 smIt = sm->getDependsOnIterator(includeSelf, complexShapeFirst);
199 while ( smIt->more() )
201 SMESH_subMesh* smToCompute = smIt->next();
202 if ( smToCompute->GetComputeState() != SMESH_subMesh::READY_TO_COMPUTE )
205 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
206 int aShapeDim = GetShapeDim( aSubShape );
207 if ( aShapeDim < 1 ) break;
209 // check for preview dimension limitations
210 if ( aShapesId && aShapeDim > (int)aDim )
213 SMESH_Algo* algo = GetAlgo( aMesh, aSubShape, &algoShape );
214 if ( algo && !algo->NeedDiscreteBoundary() )
216 if ( algo->SupportSubmeshes() )
218 // reload sub-meshes from shDim2sm into smWithAlgoSupportingSubmeshes
219 // so that more local algos to go first
220 if ( prevShapeDim != aShapeDim )
222 prevShapeDim = aShapeDim;
223 for ( shDim2smIt = shDim2sm.rbegin(); shDim2smIt != shDim2sm.rend(); ++shDim2smIt )
224 if ( shDim2smIt->first == globalAlgoDim )
225 smWithAlgoSupportingSubmeshes.push_back( shDim2smIt->second );
227 smWithAlgoSupportingSubmeshes.push_front( shDim2smIt->second );
230 // add smToCompute to shDim2sm map
231 if ( algoShape.IsSame( aMesh.GetShapeToMesh() ))
233 aShapeDim = globalAlgoDim; // to compute last
237 aShapeDim = GetShapeDim( algoShape );
238 if ( algoShape.ShapeType() == TopAbs_COMPOUND )
240 TopoDS_Iterator it( algoShape );
241 aShapeDim += GetShapeDim( it.Value() );
244 shDim2sm.insert( make_pair( aShapeDim, smToCompute ));
248 #ifdef WITH_SMESH_CANCEL_COMPUTE
249 if (_compute_canceled)
251 _sm_current = smToCompute;
253 smToCompute->ComputeStateEngine( SMESH_subMesh::COMPUTE );
254 #ifdef WITH_SMESH_CANCEL_COMPUTE
258 aShapesId->insert( smToCompute->GetId() );
262 // reload sub-meshes from shDim2sm into smWithAlgoSupportingSubmeshes
263 for ( shDim2smIt = shDim2sm.rbegin(); shDim2smIt != shDim2sm.rend(); ++shDim2smIt )
264 if ( shDim2smIt->first == globalAlgoDim )
265 smWithAlgoSupportingSubmeshes.push_back( shDim2smIt->second );
267 smWithAlgoSupportingSubmeshes.push_front( shDim2smIt->second );
269 // ------------------------------------------------------------
270 // sort list of submeshes according to mesh order
271 // ------------------------------------------------------------
272 aMesh.SortByMeshOrder( smWithAlgoSupportingSubmeshes );
274 // ------------------------------------------------------------
275 // compute submeshes under shapes with algos that DO NOT require
276 // Discreteized boundaries and DO support submeshes
277 // ------------------------------------------------------------
278 list< SMESH_subMesh* >::iterator subIt, subEnd;
279 subIt = smWithAlgoSupportingSubmeshes.begin();
280 subEnd = smWithAlgoSupportingSubmeshes.end();
281 // start from lower shapes
282 for ( ; subIt != subEnd; ++subIt )
286 // get a shape the algo is assigned to
287 if ( !GetAlgo( aMesh, sm->GetSubShape(), & algoShape ))
288 continue; // strange...
290 // look for more local algos
291 smIt = sm->getDependsOnIterator(!includeSelf, !complexShapeFirst);
292 while ( smIt->more() )
294 SMESH_subMesh* smToCompute = smIt->next();
296 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
297 const int aShapeDim = GetShapeDim( aSubShape );
298 //if ( aSubShape.ShapeType() == TopAbs_VERTEX ) continue;
299 if ( aShapeDim < 1 ) continue;
301 // check for preview dimension limitations
302 if ( aShapesId && GetShapeDim( aSubShape.ShapeType() ) > (int)aDim )
305 SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
307 .And( SMESH_HypoFilter::IsApplicableTo( aSubShape ))
308 .And( SMESH_HypoFilter::IsMoreLocalThan( algoShape, aMesh ));
310 if ( SMESH_Algo* subAlgo = (SMESH_Algo*) aMesh.GetHypothesis( aSubShape, filter, true )) {
311 SMESH_Hypothesis::Hypothesis_Status status;
312 if ( subAlgo->CheckHypothesis( aMesh, aSubShape, status ))
313 // mesh a lower smToCompute starting from vertices
314 Compute( aMesh, aSubShape, /*anUpward=*/true, aDim, aShapesId );
318 // ----------------------------------------------------------
319 // apply the algos that do not require Discreteized boundaries
320 // ----------------------------------------------------------
321 for ( subIt = smWithAlgoSupportingSubmeshes.begin(); subIt != subEnd; ++subIt )
324 if ( sm->GetComputeState() == SMESH_subMesh::READY_TO_COMPUTE)
326 const TopAbs_ShapeEnum aShType = sm->GetSubShape().ShapeType();
327 // check for preview dimension limitations
328 if ( aShapesId && GetShapeDim( aShType ) > (int)aDim )
331 #ifdef WITH_SMESH_CANCEL_COMPUTE
332 if (_compute_canceled)
336 sm->ComputeStateEngine( SMESH_subMesh::COMPUTE );
337 #ifdef WITH_SMESH_CANCEL_COMPUTE
341 aShapesId->insert( sm->GetId() );
344 // -----------------------------------------------
345 // mesh the rest sub-shapes starting from vertices
346 // -----------------------------------------------
347 ret = Compute( aMesh, aShape, /*anUpward=*/true, aDim, aShapesId );
350 MESSAGE( "VSR - SMESH_Gen::Compute() finished, OK = " << ret);
353 SMESHDS_Mesh *myMesh = aMesh.GetMeshDS();
354 myMesh->adjustStructure();
355 MESSAGE("*** compactMesh after compute");
356 myMesh->compactMesh();
357 //myMesh->adjustStructure();
358 list<int> listind = myMesh->SubMeshIndices();
359 list<int>::iterator it = listind.begin();
361 for(; it != listind.end(); ++it)
363 ::SMESHDS_SubMesh *subMesh = myMesh->MeshElements(*it);
364 total += subMesh->getSize();
366 MESSAGE("total elements and nodes in submesh sets:" << total);
367 MESSAGE("Number of node objects " << SMDS_MeshNode::nbNodes);
368 MESSAGE("Number of cell objects " << SMDS_MeshCell::nbCells);
369 //myMesh->dumpGrid();
370 //aMesh.GetMeshDS()->Modified();
372 // fix quadratic mesh by bending iternal links near concave boundary
373 if ( aShape.IsSame( aMesh.GetShapeToMesh() ) &&
374 !aShapesId ) // not preview
376 SMESH_MesherHelper aHelper( aMesh );
377 if ( aHelper.IsQuadraticMesh() != SMESH_MesherHelper::LINEAR )
378 aHelper.FixQuadraticElements();
384 #ifdef WITH_SMESH_CANCEL_COMPUTE
385 //=============================================================================
387 * Prepare Compute a mesh
389 //=============================================================================
390 void SMESH_Gen::PrepareCompute(SMESH_Mesh & aMesh,
391 const TopoDS_Shape & aShape)
393 _compute_canceled = false;
396 //=============================================================================
398 * Cancel Compute a mesh
400 //=============================================================================
401 void SMESH_Gen::CancelCompute(SMESH_Mesh & aMesh,
402 const TopoDS_Shape & aShape)
404 _compute_canceled = true;
407 _sm_current->ComputeStateEngine( SMESH_subMesh::COMPUTE_CANCELED );
412 //=============================================================================
416 //=============================================================================
418 bool SMESH_Gen::Evaluate(SMESH_Mesh & aMesh,
419 const TopoDS_Shape & aShape,
420 MapShapeNbElems& aResMap,
422 TSetOfInt* aShapesId)
424 MESSAGE("SMESH_Gen::Evaluate");
428 SMESH_subMesh *sm = aMesh.GetSubMesh(aShape);
430 const bool includeSelf = true;
431 const bool complexShapeFirst = true;
432 SMESH_subMeshIteratorPtr smIt;
434 if ( anUpward ) { // is called from below code here
435 // -----------------------------------------------
436 // mesh all the sub-shapes starting from vertices
437 // -----------------------------------------------
438 smIt = sm->getDependsOnIterator(includeSelf, !complexShapeFirst);
439 while ( smIt->more() ) {
440 SMESH_subMesh* smToCompute = smIt->next();
442 // do not mesh vertices of a pseudo shape
443 const TopAbs_ShapeEnum aShType = smToCompute->GetSubShape().ShapeType();
444 //if ( !aMesh.HasShapeToMesh() && aShType == TopAbs_VERTEX )
446 if ( !aMesh.HasShapeToMesh() ) {
447 if( aShType == TopAbs_VERTEX || aShType == TopAbs_WIRE ||
448 aShType == TopAbs_SHELL )
452 smToCompute->Evaluate(aResMap);
454 aShapesId->insert( smToCompute->GetId() );
459 // -----------------------------------------------------------------
460 // apply algos that DO NOT require Discreteized boundaries and DO NOT
461 // support submeshes, starting from the most complex shapes
462 // and collect submeshes with algos that DO support submeshes
463 // -----------------------------------------------------------------
464 list< SMESH_subMesh* > smWithAlgoSupportingSubmeshes;
465 smIt = sm->getDependsOnIterator(includeSelf, complexShapeFirst);
466 while ( smIt->more() ) {
467 SMESH_subMesh* smToCompute = smIt->next();
468 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
469 const int aShapeDim = GetShapeDim( aSubShape );
470 if ( aShapeDim < 1 ) break;
472 SMESH_Algo* algo = GetAlgo( aMesh, aSubShape );
473 if ( algo && !algo->NeedDiscreteBoundary() ) {
474 if ( algo->SupportSubmeshes() ) {
475 smWithAlgoSupportingSubmeshes.push_front( smToCompute );
478 smToCompute->Evaluate(aResMap);
480 aShapesId->insert( smToCompute->GetId() );
485 // ------------------------------------------------------------
486 // sort list of meshes according to mesh order
487 // ------------------------------------------------------------
488 aMesh.SortByMeshOrder( smWithAlgoSupportingSubmeshes );
490 // ------------------------------------------------------------
491 // compute submeshes under shapes with algos that DO NOT require
492 // Discreteized boundaries and DO support submeshes
493 // ------------------------------------------------------------
494 list< SMESH_subMesh* >::iterator subIt, subEnd;
495 subIt = smWithAlgoSupportingSubmeshes.begin();
496 subEnd = smWithAlgoSupportingSubmeshes.end();
497 // start from lower shapes
498 for ( ; subIt != subEnd; ++subIt ) {
501 // get a shape the algo is assigned to
502 TopoDS_Shape algoShape;
503 if ( !GetAlgo( aMesh, sm->GetSubShape(), & algoShape ))
504 continue; // strange...
506 // look for more local algos
507 smIt = sm->getDependsOnIterator(!includeSelf, !complexShapeFirst);
508 while ( smIt->more() ) {
509 SMESH_subMesh* smToCompute = smIt->next();
511 const TopoDS_Shape& aSubShape = smToCompute->GetSubShape();
512 const int aShapeDim = GetShapeDim( aSubShape );
513 if ( aShapeDim < 1 ) continue;
515 //const TopAbs_ShapeEnum aShType = smToCompute->GetSubShape().ShapeType();
517 SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
519 .And( SMESH_HypoFilter::IsApplicableTo( aSubShape ))
520 .And( SMESH_HypoFilter::IsMoreLocalThan( algoShape, aMesh ));
522 if ( SMESH_Algo* subAlgo = (SMESH_Algo*) aMesh.GetHypothesis( aSubShape, filter, true )) {
523 SMESH_Hypothesis::Hypothesis_Status status;
524 if ( subAlgo->CheckHypothesis( aMesh, aSubShape, status ))
525 // mesh a lower smToCompute starting from vertices
526 Evaluate( aMesh, aSubShape, aResMap, /*anUpward=*/true, aShapesId );
530 // ----------------------------------------------------------
531 // apply the algos that do not require Discreteized boundaries
532 // ----------------------------------------------------------
533 for ( subIt = smWithAlgoSupportingSubmeshes.begin(); subIt != subEnd; ++subIt )
536 sm->Evaluate(aResMap);
538 aShapesId->insert( sm->GetId() );
541 // -----------------------------------------------
542 // mesh the rest sub-shapes starting from vertices
543 // -----------------------------------------------
544 ret = Evaluate( aMesh, aShape, aResMap, /*anUpward=*/true, aShapesId );
547 MESSAGE( "VSR - SMESH_Gen::Evaluate() finished, OK = " << ret);
552 //=======================================================================
553 //function : checkConformIgnoredAlgos
555 //=======================================================================
557 static bool checkConformIgnoredAlgos(SMESH_Mesh& aMesh,
558 SMESH_subMesh* aSubMesh,
559 const SMESH_Algo* aGlobIgnoAlgo,
560 const SMESH_Algo* aLocIgnoAlgo,
562 set<SMESH_subMesh*>& aCheckedMap,
563 list< SMESH_Gen::TAlgoStateError > & theErrors)
566 if ( aSubMesh->GetSubShape().ShapeType() == TopAbs_VERTEX)
572 const list<const SMESHDS_Hypothesis*>& listHyp =
573 aMesh.GetMeshDS()->GetHypothesis( aSubMesh->GetSubShape() );
574 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
575 for ( ; it != listHyp.end(); it++)
577 const SMESHDS_Hypothesis * aHyp = *it;
578 if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
581 const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
584 if ( aLocIgnoAlgo ) // algo is hidden by a local algo of upper dim
586 INFOS( "Local <" << algo->GetName() << "> is hidden by local <"
587 << aLocIgnoAlgo->GetName() << ">");
591 bool isGlobal = (aMesh.IsMainShape( aSubMesh->GetSubShape() ));
592 int dim = algo->GetDim();
593 int aMaxGlobIgnoDim = ( aGlobIgnoAlgo ? aGlobIgnoAlgo->GetDim() : -1 );
595 if ( dim < aMaxGlobIgnoDim )
597 // algo is hidden by a global algo
598 INFOS( ( isGlobal ? "Global" : "Local" )
599 << " <" << algo->GetName() << "> is hidden by global <"
600 << aGlobIgnoAlgo->GetName() << ">");
602 else if ( !algo->NeedDiscreteBoundary() && !isGlobal)
604 // local algo is not hidden and hides algos on sub-shapes
605 if (checkConform && !aSubMesh->IsConform( algo ))
608 checkConform = false; // no more check conformity
609 INFOS( "ERROR: Local <" << algo->GetName() <<
610 "> would produce not conform mesh: "
611 "<Not Conform Mesh Allowed> hypotesis is missing");
612 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
613 theErrors.back().Set( SMESH_Hypothesis::HYP_NOTCONFORM, algo, false );
616 // sub-algos will be hidden by a local <algo>
617 SMESH_subMeshIteratorPtr revItSub =
618 aSubMesh->getDependsOnIterator( /*includeSelf=*/false, /*complexShapeFirst=*/true);
619 bool checkConform2 = false;
620 while ( revItSub->more() )
622 SMESH_subMesh* sm = revItSub->next();
623 checkConformIgnoredAlgos (aMesh, sm, aGlobIgnoAlgo,
624 algo, checkConform2, aCheckedMap, theErrors);
625 aCheckedMap.insert( sm );
634 //=======================================================================
635 //function : checkMissing
636 //purpose : notify on missing hypothesis
637 // Return false if algo or hipothesis is missing
638 //=======================================================================
640 static bool checkMissing(SMESH_Gen* aGen,
642 SMESH_subMesh* aSubMesh,
643 const int aTopAlgoDim,
645 const bool checkNoAlgo,
646 set<SMESH_subMesh*>& aCheckedMap,
647 list< SMESH_Gen::TAlgoStateError > & theErrors)
649 if ( aSubMesh->GetSubShape().ShapeType() == TopAbs_VERTEX ||
650 aCheckedMap.count( aSubMesh ))
653 //MESSAGE("=====checkMissing");
656 SMESH_Algo* algo = 0;
658 switch (aSubMesh->GetAlgoState())
660 case SMESH_subMesh::NO_ALGO: {
663 // should there be any algo?
664 int shapeDim = SMESH_Gen::GetShapeDim( aSubMesh->GetSubShape() );
665 if (aTopAlgoDim > shapeDim)
667 MESSAGE( "ERROR: " << shapeDim << "D algorithm is missing" );
669 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
670 theErrors.back().Set( SMESH_Hypothesis::HYP_MISSING, shapeDim, true );
675 case SMESH_subMesh::MISSING_HYP: {
676 // notify if an algo missing hyp is attached to aSubMesh
677 algo = aGen->GetAlgo( aMesh, aSubMesh->GetSubShape() );
679 bool IsGlobalHypothesis = aGen->IsGlobalHypothesis( algo, aMesh );
680 if (!IsGlobalHypothesis || !globalChecked[ algo->GetDim() ])
682 TAlgoStateErrorName errName = SMESH_Hypothesis::HYP_MISSING;
683 SMESH_Hypothesis::Hypothesis_Status status;
684 algo->CheckHypothesis( aMesh, aSubMesh->GetSubShape(), status );
685 if ( status == SMESH_Hypothesis::HYP_BAD_PARAMETER ) {
686 MESSAGE( "ERROR: hypothesis of " << (IsGlobalHypothesis ? "Global " : "Local ")
687 << "<" << algo->GetName() << "> has a bad parameter value");
689 } else if ( status == SMESH_Hypothesis::HYP_BAD_GEOMETRY ) {
690 MESSAGE( "ERROR: " << (IsGlobalHypothesis ? "Global " : "Local ")
691 << "<" << algo->GetName() << "> assigned to mismatching geometry");
694 MESSAGE( "ERROR: " << (IsGlobalHypothesis ? "Global " : "Local ")
695 << "<" << algo->GetName() << "> misses some hypothesis");
697 if (IsGlobalHypothesis)
698 globalChecked[ algo->GetDim() ] = true;
699 theErrors.push_back( SMESH_Gen::TAlgoStateError() );
700 theErrors.back().Set( errName, algo, IsGlobalHypothesis );
705 case SMESH_subMesh::HYP_OK:
706 algo = aSubMesh->GetAlgo();
708 if (!algo->NeedDiscreteBoundary())
710 SMESH_subMeshIteratorPtr itsub = aSubMesh->getDependsOnIterator( /*includeSelf=*/false,
711 /*complexShapeFirst=*/false);
712 while ( itsub->more() )
713 aCheckedMap.insert( itsub->next() );
719 // do not check under algo that hides sub-algos or
720 // re-start checking NO_ALGO state
722 bool isTopLocalAlgo =
723 ( aTopAlgoDim <= algo->GetDim() && !aGen->IsGlobalHypothesis( algo, aMesh ));
724 if (!algo->NeedDiscreteBoundary() || isTopLocalAlgo)
726 bool checkNoAlgo2 = ( algo->NeedDiscreteBoundary() );
727 SMESH_subMeshIteratorPtr itsub = aSubMesh->getDependsOnIterator( /*includeSelf=*/false,
728 /*complexShapeFirst=*/false);
729 while ( itsub->more() )
731 // sub-meshes should not be checked further more
732 SMESH_subMesh* sm = itsub->next();
736 //check algo on sub-meshes
737 int aTopAlgoDim2 = algo->GetDim();
738 if (!checkMissing (aGen, aMesh, sm, aTopAlgoDim2,
739 globalChecked, checkNoAlgo2, aCheckedMap, theErrors))
742 if (sm->GetAlgoState() == SMESH_subMesh::NO_ALGO )
743 checkNoAlgo2 = false;
746 aCheckedMap.insert( sm );
752 //=======================================================================
753 //function : CheckAlgoState
754 //purpose : notify on bad state of attached algos, return false
755 // if Compute() would fail because of some algo bad state
756 //=======================================================================
758 bool SMESH_Gen::CheckAlgoState(SMESH_Mesh& aMesh, const TopoDS_Shape& aShape)
760 list< TAlgoStateError > errors;
761 return GetAlgoState( aMesh, aShape, errors );
764 //=======================================================================
765 //function : GetAlgoState
766 //purpose : notify on bad state of attached algos, return false
767 // if Compute() would fail because of some algo bad state
768 // theErrors list contains problems description
769 //=======================================================================
771 bool SMESH_Gen::GetAlgoState(SMESH_Mesh& theMesh,
772 const TopoDS_Shape& theShape,
773 list< TAlgoStateError > & theErrors)
775 //MESSAGE("SMESH_Gen::CheckAlgoState");
778 bool hasAlgo = false;
780 SMESH_subMesh* sm = theMesh.GetSubMesh(theShape);
781 const SMESHDS_Mesh* meshDS = theMesh.GetMeshDS();
782 TopoDS_Shape mainShape = meshDS->ShapeToMesh();
788 const SMESH_Algo* aGlobAlgoArr[] = {0,0,0,0};
790 const list<const SMESHDS_Hypothesis*>& listHyp = meshDS->GetHypothesis( mainShape );
791 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
792 for ( ; it != listHyp.end(); it++)
794 const SMESHDS_Hypothesis * aHyp = *it;
795 if (aHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
798 const SMESH_Algo* algo = dynamic_cast<const SMESH_Algo*> (aHyp);
801 int dim = algo->GetDim();
802 aGlobAlgoArr[ dim ] = algo;
807 // --------------------------------------------------------
808 // info on algos that will be ignored because of ones that
809 // don't NeedDiscreteBoundary() attached to super-shapes,
810 // check that a conform mesh will be produced
811 // --------------------------------------------------------
814 // find a global algo possibly hiding sub-algos
816 const SMESH_Algo* aGlobIgnoAlgo = 0;
817 for (dim = 3; dim > 0; dim--)
819 if (aGlobAlgoArr[ dim ] &&
820 !aGlobAlgoArr[ dim ]->NeedDiscreteBoundary())
822 aGlobIgnoAlgo = aGlobAlgoArr[ dim ];
827 set<SMESH_subMesh*> aCheckedSubs;
828 bool checkConform = ( !theMesh.IsNotConformAllowed() );
830 // loop on theShape and its sub-shapes
831 SMESH_subMeshIteratorPtr revItSub = sm->getDependsOnIterator( /*includeSelf=*/true,
832 /*complexShapeFirst=*/true);
833 while ( revItSub->more() )
835 SMESH_subMesh* smToCheck = revItSub->next();
836 if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
839 if ( aCheckedSubs.insert( smToCheck ).second ) // not yet checked
840 if (!checkConformIgnoredAlgos (theMesh, smToCheck, aGlobIgnoAlgo,
841 0, checkConform, aCheckedSubs, theErrors))
844 if ( smToCheck->GetAlgoState() != SMESH_subMesh::NO_ALGO )
848 // ----------------------------------------------------------------
849 // info on missing hypothesis and find out if all needed algos are
851 // ----------------------------------------------------------------
853 //MESSAGE( "---info on missing hypothesis and find out if all needed algos are");
855 // find max dim of global algo
857 for (dim = 3; dim > 0; dim--)
859 if (aGlobAlgoArr[ dim ])
865 bool checkNoAlgo = theMesh.HasShapeToMesh() ? bool( aTopAlgoDim ) : false;
866 bool globalChecked[] = { false, false, false, false };
868 // loop on theShape and its sub-shapes
869 aCheckedSubs.clear();
870 revItSub = sm->getDependsOnIterator( /*includeSelf=*/true, /*complexShapeFirst=*/true);
871 while ( revItSub->more() )
873 SMESH_subMesh* smToCheck = revItSub->next();
874 if ( smToCheck->GetSubShape().ShapeType() == TopAbs_VERTEX)
877 if ( aCheckedSubs.insert( smToCheck ).second ) // not yet checked
878 if (!checkMissing (this, theMesh, smToCheck, aTopAlgoDim,
879 globalChecked, checkNoAlgo, aCheckedSubs, theErrors))
882 if (smToCheck->GetAlgoState() == SMESH_subMesh::NO_ALGO )
889 INFOS( "None algorithm attached" );
890 theErrors.push_back( TAlgoStateError() );
891 theErrors.back().Set( SMESH_Hypothesis::HYP_MISSING, 1, true );
897 //=======================================================================
898 //function : IsGlobalHypothesis
899 //purpose : check if theAlgo is attached to the main shape
900 //=======================================================================
902 bool SMESH_Gen::IsGlobalHypothesis(const SMESH_Hypothesis* theHyp, SMESH_Mesh& aMesh)
904 SMESH_HypoFilter filter( SMESH_HypoFilter::Is( theHyp ));
905 return aMesh.GetHypothesis( aMesh.GetMeshDS()->ShapeToMesh(), filter, false );
908 //================================================================================
910 * \brief Return paths to xml files of plugins
912 //================================================================================
914 std::vector< std::string > SMESH_Gen::GetPluginXMLPaths()
916 // Get paths to xml files of plugins
917 vector< string > xmlPaths;
919 if ( const char* meshersList = getenv("SMESH_MeshersList") )
921 string meshers = meshersList, plugin;
922 string::size_type from = 0, pos;
923 while ( from < meshers.size() )
925 // cut off plugin name
926 pos = meshers.find( ':', from );
927 if ( pos != string::npos )
928 plugin = meshers.substr( from, pos-from );
930 plugin = meshers.substr( from ), pos = meshers.size();
933 // get PLUGIN_ROOT_DIR path
934 string rootDirVar, pluginSubDir = plugin;
935 if ( plugin == "StdMeshers" )
936 rootDirVar = "SMESH", pluginSubDir = "smesh";
938 for ( pos = 0; pos < plugin.size(); ++pos )
939 rootDirVar += toupper( plugin[pos] );
940 rootDirVar += "_ROOT_DIR";
942 const char* rootDir = getenv( rootDirVar.c_str() );
943 if ( !rootDir || strlen(rootDir) == 0 )
945 rootDirVar = plugin + "_ROOT_DIR"; // HexoticPLUGIN_ROOT_DIR
946 rootDir = getenv( rootDirVar.c_str() );
947 if ( !rootDir || strlen(rootDir) == 0 ) continue;
950 // get a separator from rootDir
951 for ( pos = strlen( rootDir )-1; pos >= 0 && sep.empty(); --pos )
952 if ( rootDir[pos] == '/' || rootDir[pos] == '\\' )
958 if (sep.empty() ) sep = "\\";
960 if (sep.empty() ) sep = "/";
963 // get a path to resource file
964 string xmlPath = rootDir;
965 if ( xmlPath[ xmlPath.size()-1 ] != sep[0] )
967 xmlPath += "share" + sep + "salome" + sep + "resources" + sep;
968 for ( pos = 0; pos < pluginSubDir.size(); ++pos )
969 xmlPath += tolower( pluginSubDir[pos] );
970 xmlPath += sep + plugin + ".xml";
973 fileOK = (GetFileAttributes(xmlPath.c_str()) != INVALID_FILE_ATTRIBUTES);
975 fileOK = (access(xmlPath.c_str(), F_OK) == 0);
978 xmlPaths.push_back( xmlPath );
985 //=======================================================================
986 namespace // Access to type of input and output of an algorithm
987 //=======================================================================
992 set<SMDSAbs_GeometryType> _inElemTypes; // acceptable types of input mesh element
993 set<SMDSAbs_GeometryType> _outElemTypes; // produced types of mesh elements
995 bool IsCompatible( const AlgoData& algo2 ) const
997 if ( _dim > algo2._dim ) return algo2.IsCompatible( *this );
998 // algo2 is of highter dimension
999 if ( _outElemTypes.empty() || algo2._inElemTypes.empty() )
1001 bool compatible = true;
1002 set<SMDSAbs_GeometryType>::const_iterator myOutType = _outElemTypes.begin();
1003 for ( ; myOutType != _outElemTypes.end() && compatible; ++myOutType )
1004 compatible = algo2._inElemTypes.count( *myOutType );
1009 //================================================================================
1011 * \brief Return AlgoData of the algorithm
1013 //================================================================================
1015 const AlgoData& getAlgoData( const SMESH_Algo* algo )
1017 static map< string, AlgoData > theDataByName;
1018 if ( theDataByName.empty() )
1020 // Read Plugin.xml files
1021 vector< string > xmlPaths = SMESH_Gen::GetPluginXMLPaths();
1022 LDOMParser xmlParser;
1023 for ( size_t i = 0; i < xmlPaths.size(); ++i )
1025 bool error = xmlParser.parse( xmlPaths[i].c_str() );
1028 TCollection_AsciiString data;
1029 INFOS( xmlParser.GetError(data) );
1032 // <algorithm type="Regular_1D"
1035 // output="QUAD,TRIA">
1037 LDOM_Document xmlDoc = xmlParser.getDocument();
1038 LDOM_NodeList algoNodeList = xmlDoc.getElementsByTagName( "algorithm" );
1039 for ( int i = 0; i < algoNodeList.getLength(); ++i )
1041 LDOM_Node algoNode = algoNodeList.item( i );
1042 LDOM_Element& algoElem = (LDOM_Element&) algoNode;
1043 TCollection_AsciiString algoType = algoElem.getAttribute("type");
1044 TCollection_AsciiString input = algoElem.getAttribute("input");
1045 TCollection_AsciiString output = algoElem.getAttribute("output");
1046 TCollection_AsciiString dim = algoElem.getAttribute("dim");
1047 AlgoData & data = theDataByName[ algoType.ToCString() ];
1048 data._dim = dim.IntegerValue();
1049 for ( int isInput = 0; isInput < 2; ++isInput )
1051 TCollection_AsciiString& typeStr = isInput ? input : output;
1052 set<SMDSAbs_GeometryType>& typeSet = isInput ? data._inElemTypes : data._outElemTypes;
1054 while ( beg <= typeStr.Length() )
1056 while ( beg < typeStr.Length() && !isalpha( typeStr.Value( beg ) ))
1059 while ( end < typeStr.Length() && isalpha( typeStr.Value( end + 1 ) ))
1063 TCollection_AsciiString typeName = typeStr.SubString( beg, end );
1064 if ( typeName == "EDGE" ) typeSet.insert( SMDSGeom_EDGE );
1065 else if ( typeName == "TRIA" ) typeSet.insert( SMDSGeom_TRIANGLE );
1066 else if ( typeName == "QUAD" ) typeSet.insert( SMDSGeom_QUADRANGLE );
1074 return theDataByName[ algo->GetName() ];
1078 //=============================================================================
1080 * Finds algo to mesh a shape. Optionally returns a shape the found algo is bound to
1082 //=============================================================================
1084 SMESH_Algo *SMESH_Gen::GetAlgo(SMESH_Mesh & aMesh,
1085 const TopoDS_Shape & aShape,
1086 TopoDS_Shape* assignedTo)
1088 SMESH_HypoFilter filter( SMESH_HypoFilter::IsAlgo() );
1089 filter.And( filter.IsApplicableTo( aShape ));
1091 TopoDS_Shape assignedToShape;
1093 (SMESH_Algo*) aMesh.GetHypothesis( aShape, filter, true, &assignedToShape );
1096 aShape.ShapeType() == TopAbs_FACE &&
1097 !aShape.IsSame( assignedToShape ) &&
1098 SMESH_MesherHelper::NbAncestors( aShape, aMesh, TopAbs_SOLID ) > 1 )
1100 // Issue 0021559. If there is another 2D algo with different types of output
1101 // elements that can be used to mesh aShape, and 3D algos on adjacent SOLIDs
1102 // have different types of input elements, we choose a most appropriate 2D algo.
1104 // try to find a concurrent 2D algo
1105 filter.AndNot( filter.Is( algo ));
1106 TopoDS_Shape assignedToShape2;
1108 (SMESH_Algo*) aMesh.GetHypothesis( aShape, filter, true, &assignedToShape2 );
1110 assignedToShape2.ShapeType() == assignedToShape.ShapeType() &&
1111 aMesh.IsOrderOK( aMesh.GetSubMesh( assignedToShape2 ),
1112 aMesh.GetSubMesh( assignedToShape )))
1114 // get algos on the adjacent SOLIDs
1115 filter.Init( filter.IsAlgo() ).And( filter.HasDim( 3 ));
1116 vector< SMESH_Algo* > algos3D;
1117 PShapeIteratorPtr solidIt = SMESH_MesherHelper::GetAncestors( aShape, aMesh,
1119 while ( const TopoDS_Shape* solid = solidIt->next() )
1120 if ( SMESH_Algo* algo3D = (SMESH_Algo*) aMesh.GetHypothesis( *solid, filter, true ))
1122 algos3D.push_back( algo3D );
1123 filter.AndNot( filter.Is( algo3D ));
1125 // check compatibility of algos
1126 if ( algos3D.size() > 1 )
1128 const AlgoData& algoData = getAlgoData( algo );
1129 const AlgoData& algoData2 = getAlgoData( algo2 );
1130 const AlgoData& algoData3d0 = getAlgoData( algos3D[0] );
1131 const AlgoData& algoData3d1 = getAlgoData( algos3D[1] );
1132 if (( algoData2.IsCompatible( algoData3d0 ) &&
1133 algoData2.IsCompatible( algoData3d1 ))
1135 !(algoData.IsCompatible( algoData3d0 ) &&
1136 algoData.IsCompatible( algoData3d1 )))
1142 if ( assignedTo && algo )
1143 * assignedTo = assignedToShape;
1148 //=============================================================================
1150 * Returns StudyContextStruct for a study
1152 //=============================================================================
1154 StudyContextStruct *SMESH_Gen::GetStudyContext(int studyId)
1156 // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
1158 if (_mapStudyContext.find(studyId) == _mapStudyContext.end())
1160 _mapStudyContext[studyId] = new StudyContextStruct;
1161 _mapStudyContext[studyId]->myDocument = new SMESHDS_Document(studyId);
1163 StudyContextStruct *myStudyContext = _mapStudyContext[studyId];
1164 return myStudyContext;
1167 //================================================================================
1169 * \brief Return shape dimension by TopAbs_ShapeEnum
1171 //================================================================================
1173 int SMESH_Gen::GetShapeDim(const TopAbs_ShapeEnum & aShapeType)
1175 static vector<int> dim;
1178 dim.resize( TopAbs_SHAPE, -1 );
1179 dim[ TopAbs_COMPOUND ] = MeshDim_3D;
1180 dim[ TopAbs_COMPSOLID ] = MeshDim_3D;
1181 dim[ TopAbs_SOLID ] = MeshDim_3D;
1182 dim[ TopAbs_SHELL ] = MeshDim_2D;
1183 dim[ TopAbs_FACE ] = MeshDim_2D;
1184 dim[ TopAbs_WIRE ] = MeshDim_1D;
1185 dim[ TopAbs_EDGE ] = MeshDim_1D;
1186 dim[ TopAbs_VERTEX ] = MeshDim_0D;
1188 return dim[ aShapeType ];
1191 //=============================================================================
1193 * Genarate a new id unique withing this Gen
1195 //=============================================================================
1197 int SMESH_Gen::GetANewId()