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 // File : SMESH_Mesh.cxx
24 // Author : Paul RASCLE, EDF
27 #include "SMESH_Mesh.hxx"
28 #include "SMESH_MesherHelper.hxx"
29 #include "SMDS_MeshVolume.hxx"
30 #include "SMDS_SetIterator.hxx"
31 #include "SMESHDS_Document.hxx"
32 #include "SMESHDS_Group.hxx"
33 #include "SMESHDS_GroupOnGeom.hxx"
34 #include "SMESHDS_Script.hxx"
35 #include "SMESHDS_TSubMeshHolder.hxx"
36 #include "SMESH_Gen.hxx"
37 #include "SMESH_Group.hxx"
38 #include "SMESH_HypoFilter.hxx"
39 #include "SMESH_Hypothesis.hxx"
40 #include "SMESH_subMesh.hxx"
42 #include "utilities.h"
44 #include "DriverDAT_W_SMDS_Mesh.h"
45 #include "DriverGMF_Read.hxx"
46 #include "DriverGMF_Write.hxx"
47 #include "DriverMED_R_SMESHDS_Mesh.h"
48 #include "DriverMED_W_SMESHDS_Mesh.h"
49 #include "DriverSTL_R_SMDS_Mesh.h"
50 #include "DriverSTL_W_SMDS_Mesh.h"
51 #include "DriverUNV_R_SMDS_Mesh.h"
52 #include "DriverUNV_W_SMDS_Mesh.h"
54 #include "DriverCGNS_Read.hxx"
55 #include "DriverCGNS_Write.hxx"
58 #include <GEOMUtils.hxx>
60 #undef _Precision_HeaderFile
61 #include <BRepBndLib.hxx>
62 #include <BRepPrimAPI_MakeBox.hxx>
63 #include <Bnd_Box.hxx>
64 #include <TColStd_MapOfInteger.hxx>
66 #include <TopExp_Explorer.hxx>
67 #include <TopTools_ListIteratorOfListOfShape.hxx>
68 #include <TopTools_ListOfShape.hxx>
69 #include <TopTools_MapOfShape.hxx>
70 #include <TopoDS_Iterator.hxx>
72 #include "SMESH_TryCatch.hxx" // include after OCCT headers!
74 #include "Utils_ExceptHandlers.hxx"
77 #include <boost/thread/thread.hpp>
78 #include <boost/bind.hpp>
85 // maximum stored group name length in MED file
86 #define MAX_MED_GROUP_NAME_LENGTH 80
89 static int MYDEBUG = 0;
91 static int MYDEBUG = 0;
94 #define cSMESH_Hyp(h) static_cast<const SMESH_Hypothesis*>(h)
96 typedef SMESH_HypoFilter THypType;
98 class SMESH_Mesh::SubMeshHolder : public SMESHDS_TSubMeshHolder< SMESH_subMesh >
102 //=============================================================================
106 //=============================================================================
108 SMESH_Mesh::SMESH_Mesh(int theLocalId,
110 bool theIsEmbeddedMode,
111 SMESHDS_Document* theDocument):
112 _groupId( 0 ), _nbSubShapes( 0 )
114 if(MYDEBUG) MESSAGE("SMESH_Mesh::SMESH_Mesh(int localId)");
117 _myDocument = theDocument;
118 _myMeshDS = theDocument->NewMesh(theIsEmbeddedMode,theLocalId);
119 _isShapeToMesh = false;
120 _isAutoColor = false;
122 _shapeDiagonal = 0.0;
124 _myMeshDS->ShapeToMesh( PseudoShape() );
125 _subMeshHolder = new SubMeshHolder;
128 //================================================================================
130 * \brief Constructor of SMESH_Mesh being a base of some descendant class
132 //================================================================================
134 SMESH_Mesh::SMESH_Mesh():
138 _isShapeToMesh( false ),
142 _isAutoColor( false ),
143 _isModified( false ),
144 _shapeDiagonal( 0.0 ),
147 _subMeshHolder = new SubMeshHolder;
153 void deleteMeshDS(SMESHDS_Mesh* meshDS)
155 //cout << "deleteMeshDS( " << meshDS << endl;
159 static void* deleteMeshDS(void* meshDS)
161 //cout << "deleteMeshDS( " << meshDS << endl;
162 SMESHDS_Mesh* m = (SMESHDS_Mesh*)meshDS;
171 //=============================================================================
175 //=============================================================================
177 SMESH_Mesh::~SMESH_Mesh()
179 if(MYDEBUG) MESSAGE("SMESH_Mesh::~SMESH_Mesh");
181 // avoid usual removal of elements while processing RemoveHypothesis( algo ) event
182 SMESHDS_SubMeshIteratorPtr smIt = _myMeshDS->SubMeshes();
183 while ( smIt->more() )
184 const_cast<SMESHDS_SubMesh*>( smIt->next() )->Clear();
186 // issue 0020340: EDF 1022 SMESH : Crash with FindNodeClosestTo in a second new study
187 // Notify event listeners at least that something happens
188 if ( SMESH_subMesh * sm = GetSubMeshContaining(1))
189 sm->ComputeStateEngine( SMESH_subMesh::MESH_ENTITY_REMOVED );
192 map < int, SMESH_Group * >::iterator itg;
193 for (itg = _mapGroup.begin(); itg != _mapGroup.end(); itg++) {
194 SMESH_Group *aGroup = (*itg).second;
200 delete _subMeshHolder;
202 if ( _callUp) delete _callUp;
205 // remove self from studyContext
208 StudyContextStruct * studyContext = _gen->GetStudyContext();
209 studyContext->mapMesh.erase( _id );
212 _myDocument->RemoveMesh( _id );
216 // delete _myMeshDS, in a thread in order not to block closing a study with large meshes
218 boost::thread aThread(boost::bind( & deleteMeshDS, _myMeshDS ));
221 int result=pthread_create(&thread, NULL, deleteMeshDS, (void*)_myMeshDS);
226 //================================================================================
228 * \brief Return true if a mesh with given id exists
230 //================================================================================
232 bool SMESH_Mesh::MeshExists( int meshId ) const
234 return _myDocument ? bool( _myDocument->GetMesh( meshId )) : false;
237 //================================================================================
239 * \brief Return a mesh by id
241 //================================================================================
243 SMESH_Mesh* SMESH_Mesh::FindMesh( int meshId ) const
246 return (SMESH_Mesh*) this;
248 if ( StudyContextStruct *aStudyContext = _gen->GetStudyContext())
250 std::map < int, SMESH_Mesh * >::iterator i_m = aStudyContext->mapMesh.find( meshId );
251 if ( i_m != aStudyContext->mapMesh.end() )
257 //=============================================================================
259 * \brief Set geometry to be meshed
261 //=============================================================================
263 void SMESH_Mesh::ShapeToMesh(const TopoDS_Shape & aShape)
265 if(MYDEBUG) MESSAGE("SMESH_Mesh::ShapeToMesh");
267 if ( !aShape.IsNull() && _isShapeToMesh ) {
268 if ( aShape.ShapeType() != TopAbs_COMPOUND && // group contents is allowed to change
269 _myMeshDS->ShapeToMesh().ShapeType() != TopAbs_COMPOUND )
270 throw SALOME_Exception(LOCALIZED ("a shape to mesh has already been defined"));
272 // clear current data
273 if ( !_myMeshDS->ShapeToMesh().IsNull() )
275 // removal of a shape to mesh, delete objects referring to sub-shapes:
277 _subMeshHolder->DeleteAll();
278 // - groups on geometry
279 map <int, SMESH_Group *>::iterator i_gr = _mapGroup.begin();
280 while ( i_gr != _mapGroup.end() ) {
281 if ( dynamic_cast<SMESHDS_GroupOnGeom*>( i_gr->second->GetGroupDS() )) {
282 _myMeshDS->RemoveGroup( i_gr->second->GetGroupDS() );
284 _mapGroup.erase( i_gr++ );
289 _mapAncestors.Clear();
292 TopoDS_Shape aNullShape;
293 _myMeshDS->ShapeToMesh( aNullShape );
295 _shapeDiagonal = 0.0;
298 // set a new geometry
299 if ( !aShape.IsNull() )
301 _myMeshDS->ShapeToMesh(aShape);
302 _isShapeToMesh = true;
303 _nbSubShapes = _myMeshDS->MaxShapeIndex();
305 // fill map of ancestors
306 fillAncestorsMap(aShape);
310 _isShapeToMesh = false;
311 _shapeDiagonal = 0.0;
312 _myMeshDS->ShapeToMesh( PseudoShape() );
317 //=======================================================================
319 * \brief Return geometry to be meshed. (It may be a PseudoShape()!)
321 //=======================================================================
323 TopoDS_Shape SMESH_Mesh::GetShapeToMesh() const
325 return _myMeshDS->ShapeToMesh();
328 //=======================================================================
330 * \brief Return a solid which is returned by GetShapeToMesh() if
331 * a real geometry to be meshed was not set
333 //=======================================================================
335 const TopoDS_Solid& SMESH_Mesh::PseudoShape()
337 static TopoDS_Solid aSolid;
338 if ( aSolid.IsNull() )
340 aSolid = BRepPrimAPI_MakeBox(1,1,1);
345 //=======================================================================
347 * \brief Return diagonal size of bounding box of a shape
349 //=======================================================================
351 double SMESH_Mesh::GetShapeDiagonalSize(const TopoDS_Shape & aShape)
353 if ( !aShape.IsNull() ) {
355 // avoid too long waiting on large shapes. PreciseBoundingBox() was added
356 // to assure same result which else depends on presence of triangulation (IPAL52557).
357 const int maxNbFaces = 4000;
359 for ( TopExp_Explorer f( aShape, TopAbs_FACE ); f.More() && nbFaces < maxNbFaces; f.Next() )
361 bool isPrecise = false;
362 if ( nbFaces < maxNbFaces )
364 GEOMUtils::PreciseBoundingBox( aShape, Box );
372 BRepBndLib::Add( aShape, Box );
375 return sqrt( Box.SquareExtent() );
380 //=======================================================================
382 * \brief Return diagonal size of bounding box of shape to mesh
384 //=======================================================================
386 double SMESH_Mesh::GetShapeDiagonalSize() const
388 if ( _shapeDiagonal == 0. && _isShapeToMesh )
389 const_cast<SMESH_Mesh*>(this)->_shapeDiagonal = GetShapeDiagonalSize( GetShapeToMesh() );
391 return _shapeDiagonal;
394 //================================================================================
396 * \brief Load mesh from study file
398 //================================================================================
400 void SMESH_Mesh::Load()
406 //=======================================================================
408 * \brief Remove all nodes and elements
410 //=======================================================================
412 void SMESH_Mesh::Clear()
414 if ( HasShapeToMesh() ) // remove all nodes and elements
417 _myMeshDS->ClearMesh();
419 // update compute state of submeshes
420 if ( SMESH_subMesh *sm = GetSubMeshContaining( GetShapeToMesh() ) )
422 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
423 sm->ComputeSubMeshStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
424 sm->ComputeStateEngine( SMESH_subMesh::CLEAN ); // for event listeners (issue 0020918)
425 sm->ComputeSubMeshStateEngine( SMESH_subMesh::CLEAN );
428 else // remove only nodes/elements computed by algorithms
430 if ( SMESH_subMesh *sm = GetSubMeshContaining( GetShapeToMesh() ) )
432 sm->ComputeStateEngine( SMESH_subMesh::CLEAN );
433 sm->ComputeSubMeshStateEngine( SMESH_subMesh::CLEAN );
434 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
435 sm->ComputeSubMeshStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
438 GetMeshDS()->Modified();
442 //=======================================================================
444 * \brief Remove all nodes and elements of indicated shape
446 //=======================================================================
448 void SMESH_Mesh::ClearSubMesh(const int theShapeId)
450 // clear sub-meshes; get ready to re-compute as a side-effect
451 if ( SMESH_subMesh *sm = GetSubMeshContaining( theShapeId ) )
453 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(/*includeSelf=*/true,
454 /*complexShapeFirst=*/false);
455 while ( smIt->more() )
458 TopAbs_ShapeEnum shapeType = sm->GetSubShape().ShapeType();
459 if ( shapeType == TopAbs_VERTEX || shapeType < TopAbs_SOLID )
460 // all other shapes depends on vertices so they are already cleaned
461 sm->ComputeStateEngine( SMESH_subMesh::CLEAN );
462 // to recompute even if failed
463 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
468 //=======================================================================
469 //function : UNVToMesh
471 //=======================================================================
473 int SMESH_Mesh::UNVToMesh(const char* theFileName)
475 if ( _isShapeToMesh )
476 throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
477 _isShapeToMesh = false;
479 DriverUNV_R_SMDS_Mesh myReader;
480 myReader.SetMesh(_myMeshDS);
481 myReader.SetFile(theFileName);
482 myReader.SetMeshId(-1);
485 TGroupNamesMap& aGroupNames = myReader.GetGroupNamesMap();
486 TGroupNamesMap::iterator gr2names;
487 int anId = 1 + ( _mapGroup.empty() ? 0 : _mapGroup.rbegin()->first );
488 for ( gr2names = aGroupNames.begin(); gr2names != aGroupNames.end(); ++gr2names )
490 SMDS_MeshGroup* aGroup = gr2names->first;
491 const std::string& aName = gr2names->second;
492 SMESHDS_Group* aGroupDS = new SMESHDS_Group( anId++, _myMeshDS, aGroup->GetType() );
493 aGroupDS->SMDSGroup() = std::move( *aGroup );
494 aGroupDS->SetStoreName( aName.c_str() );
495 AddGroup( aGroupDS );
501 //=======================================================================
502 //function : MEDToMesh
504 //=======================================================================
506 int SMESH_Mesh::MEDToMesh(const char* theFileName, const char* theMeshName)
508 if ( _isShapeToMesh )
509 throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
510 _isShapeToMesh = false;
512 DriverMED_R_SMESHDS_Mesh myReader;
513 myReader.SetMesh(_myMeshDS);
514 myReader.SetMeshId(-1);
515 myReader.SetFile(theFileName);
516 myReader.SetMeshName(theMeshName);
517 Driver_Mesh::Status status = myReader.Perform();
519 SMESH_ComputeErrorPtr er = myReader.GetError();
520 if ( er && !er->IsOK() ) cout << er->myComment << endl;
523 // Reading groups (sub-meshes are out of scope of MED import functionality)
524 list<TNameAndType> aGroupNames = myReader.GetGroupNamesAndTypes();
526 list<TNameAndType>::iterator name_type = aGroupNames.begin();
527 for ( ; name_type != aGroupNames.end(); name_type++ )
529 SMESH_Group* aGroup = AddGroup( name_type->second, name_type->first.c_str(), anId );
531 SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( aGroup->GetGroupDS() );
533 aGroupDS->SetStoreName( name_type->first.c_str() );
534 myReader.GetGroup( aGroupDS );
539 _myMeshDS->Modified();
540 _myMeshDS->CompactMesh();
545 //=======================================================================
546 //function : STLToMesh
548 //=======================================================================
550 std::string SMESH_Mesh::STLToMesh(const char* theFileName)
553 throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
554 _isShapeToMesh = false;
556 DriverSTL_R_SMDS_Mesh myReader;
557 myReader.SetMesh(_myMeshDS);
558 myReader.SetFile(theFileName);
559 myReader.SetMeshId(-1);
562 return myReader.GetName();
565 //================================================================================
567 * \brief Reads the given mesh from the CGNS file
568 * \param theFileName - name of the file
569 * \retval int - Driver_Mesh::Status
571 //================================================================================
573 int SMESH_Mesh::CGNSToMesh(const char* theFileName,
574 const int theMeshIndex,
575 std::string& theMeshName)
577 int res = Driver_Mesh::DRS_FAIL;
580 DriverCGNS_Read myReader;
581 myReader.SetMesh(_myMeshDS);
582 myReader.SetFile(theFileName);
583 myReader.SetMeshId(theMeshIndex);
584 res = myReader.Perform();
585 theMeshName = myReader.GetMeshName();
594 //================================================================================
596 * \brief Fill its data by reading a GMF file
598 //================================================================================
600 SMESH_ComputeErrorPtr SMESH_Mesh::GMFToMesh(const char* theFileName,
601 bool theMakeRequiredGroups)
603 DriverGMF_Read myReader;
604 myReader.SetMesh(_myMeshDS);
605 myReader.SetFile(theFileName);
606 myReader.SetMakeRequiredGroups( theMakeRequiredGroups );
608 //theMeshName = myReader.GetMeshName();
613 return myReader.GetError();
616 //=============================================================================
620 //=============================================================================
622 SMESH_Hypothesis::Hypothesis_Status
623 SMESH_Mesh::AddHypothesis(const TopoDS_Shape & aSubShape,
625 std::string* anError ) throw(SALOME_Exception)
627 Unexpect aCatch(SalomeException);
628 if(MYDEBUG) MESSAGE("SMESH_Mesh::AddHypothesis");
633 SMESH_subMesh *subMesh = GetSubMesh(aSubShape);
634 if ( !subMesh || !subMesh->GetId())
635 return SMESH_Hypothesis::HYP_BAD_SUBSHAPE;
637 SMESH_Hypothesis *anHyp = GetHypothesis( anHypId );
639 throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
641 bool isGlobalHyp = IsMainShape( aSubShape );
643 // NotConformAllowed can be only global
646 // NOTE: this is not a correct way to check a name of hypothesis,
647 // there should be an attribute of hypothesis saying that it can/can't
649 string hypName = anHyp->GetName();
650 if ( hypName == "NotConformAllowed" )
652 if(MYDEBUG) MESSAGE( "Hypothesis <NotConformAllowed> can be only global" );
653 return SMESH_Hypothesis::HYP_INCOMPATIBLE;
659 bool isAlgo = ( anHyp->GetType() != SMESHDS_Hypothesis::PARAM_ALGO );
660 SMESH_subMesh::algo_event event = isAlgo ? SMESH_subMesh::ADD_ALGO : SMESH_subMesh::ADD_HYP;
662 SMESH_Hypothesis::Hypothesis_Status ret = subMesh->AlgoStateEngine(event, anHyp);
664 if ( anError && SMESH_Hypothesis::IsStatusFatal(ret) && subMesh->GetComputeError() )
665 *anError = subMesh->GetComputeError()->myComment;
668 if ( !SMESH_Hypothesis::IsStatusFatal(ret) &&
669 anHyp->GetDim() <= SMESH_Gen::GetShapeDim(aSubShape)) // is added on father
671 event = isAlgo ? SMESH_subMesh::ADD_FATHER_ALGO : SMESH_subMesh::ADD_FATHER_HYP;
673 SMESH_Hypothesis::Hypothesis_Status ret2 =
674 subMesh->SubMeshesAlgoStateEngine(event, anHyp, /*exitOnFatal=*/true);
678 if ( SMESH_Hypothesis::IsStatusFatal( ret ))
680 if ( anError && subMesh->GetComputeError() )
681 *anError = subMesh->GetComputeError()->myComment;
683 event = isAlgo ? SMESH_subMesh::REMOVE_ALGO : SMESH_subMesh::REMOVE_HYP;
684 subMesh->AlgoStateEngine(event, anHyp);
688 // check concurrent hypotheses on ancestors
689 if (ret < SMESH_Hypothesis::HYP_CONCURRENT && !isGlobalHyp )
691 SMESH_subMeshIteratorPtr smIt = subMesh->getDependsOnIterator(false,false);
692 while ( smIt->more() ) {
693 SMESH_subMesh* sm = smIt->next();
694 if ( sm->IsApplicableHypothesis( anHyp )) {
695 ret2 = sm->CheckConcurrentHypothesis( anHyp->GetType() );
704 HasModificationsToDiscard(); // to reset _isModified flag if a mesh becomes empty
705 GetMeshDS()->Modified();
707 if(MYDEBUG) subMesh->DumpAlgoState(true);
708 if(MYDEBUG) SCRUTE(ret);
712 //=============================================================================
716 //=============================================================================
718 SMESH_Hypothesis::Hypothesis_Status
719 SMESH_Mesh::RemoveHypothesis(const TopoDS_Shape & aSubShape,
720 int anHypId) throw( SALOME_Exception )
722 Unexpect aCatch(SalomeException);
723 if(MYDEBUG) MESSAGE("SMESH_Mesh::RemoveHypothesis");
725 StudyContextStruct *sc = _gen->GetStudyContext();
726 if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
727 throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
729 SMESH_Hypothesis *anHyp = sc->mapHypothesis[anHypId];
730 if(MYDEBUG) { SCRUTE(anHyp->GetType()); }
734 bool isAlgo = ( !anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO );
735 SMESH_subMesh::algo_event event = isAlgo ? SMESH_subMesh::REMOVE_ALGO : SMESH_subMesh::REMOVE_HYP;
737 SMESH_subMesh *subMesh = GetSubMesh(aSubShape);
739 SMESH_Hypothesis::Hypothesis_Status ret = subMesh->AlgoStateEngine(event, anHyp);
741 // there may appear concurrent hyps that were covered by the removed hyp
742 if (ret < SMESH_Hypothesis::HYP_CONCURRENT &&
743 subMesh->IsApplicableHypothesis( anHyp ) &&
744 subMesh->CheckConcurrentHypothesis( anHyp->GetType() ) != SMESH_Hypothesis::HYP_OK)
745 ret = SMESH_Hypothesis::HYP_CONCURRENT;
748 if (!SMESH_Hypothesis::IsStatusFatal(ret) &&
749 anHyp->GetDim() <= SMESH_Gen::GetShapeDim(aSubShape)) // is removed from father
751 event = isAlgo ? SMESH_subMesh::REMOVE_FATHER_ALGO : SMESH_subMesh::REMOVE_FATHER_HYP;
753 SMESH_Hypothesis::Hypothesis_Status ret2 =
754 subMesh->SubMeshesAlgoStateEngine(event, anHyp);
755 if (ret2 > ret) // more severe
758 // check concurrent hypotheses on ancestors
759 if (ret < SMESH_Hypothesis::HYP_CONCURRENT && !IsMainShape( aSubShape ) )
761 SMESH_subMeshIteratorPtr smIt = subMesh->getDependsOnIterator(false,false);
762 while ( smIt->more() ) {
763 SMESH_subMesh* sm = smIt->next();
764 if ( sm->IsApplicableHypothesis( anHyp )) {
765 ret2 = sm->CheckConcurrentHypothesis( anHyp->GetType() );
775 HasModificationsToDiscard(); // to reset _isModified flag if mesh become empty
776 GetMeshDS()->Modified();
778 if(MYDEBUG) subMesh->DumpAlgoState(true);
779 if(MYDEBUG) SCRUTE(ret);
783 //=============================================================================
787 //=============================================================================
789 const list<const SMESHDS_Hypothesis*>&
790 SMESH_Mesh::GetHypothesisList(const TopoDS_Shape & aSubShape) const
791 throw(SALOME_Exception)
793 return _myMeshDS->GetHypothesis(aSubShape);
796 //=======================================================================
798 * \brief Return the hypothesis assigned to the shape
799 * \param aSubShape - the shape to check
800 * \param aFilter - the hypothesis filter
801 * \param andAncestors - flag to check hypos assigned to ancestors of the shape
802 * \param assignedTo - to return the shape the found hypo is assigned to
803 * \retval SMESH_Hypothesis* - the first hypo passed through aFilter
805 //=======================================================================
807 const SMESH_Hypothesis * SMESH_Mesh::GetHypothesis(const TopoDS_Shape & aSubShape,
808 const SMESH_HypoFilter& aFilter,
809 const bool andAncestors,
810 TopoDS_Shape* assignedTo) const
812 return GetHypothesis( const_cast< SMESH_Mesh* >(this)->GetSubMesh( aSubShape ),
813 aFilter, andAncestors, assignedTo );
816 //=======================================================================
818 * \brief Return the hypothesis assigned to the shape of a sub-mesh
819 * \param aSubMesh - the sub-mesh to check
820 * \param aFilter - the hypothesis filter
821 * \param andAncestors - flag to check hypos assigned to ancestors of the shape
822 * \param assignedTo - to return the shape the found hypo is assigned to
823 * \retval SMESH_Hypothesis* - the first hypo passed through aFilter
825 //=======================================================================
827 const SMESH_Hypothesis * SMESH_Mesh::GetHypothesis(const SMESH_subMesh * aSubMesh,
828 const SMESH_HypoFilter& aFilter,
829 const bool andAncestors,
830 TopoDS_Shape* assignedTo) const
832 if ( !aSubMesh ) return 0;
835 const TopoDS_Shape & aSubShape = aSubMesh->GetSubShape();
836 const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(aSubShape);
837 list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
838 for ( ; hyp != hypList.end(); hyp++ ) {
839 const SMESH_Hypothesis * h = cSMESH_Hyp( *hyp );
840 if ( aFilter.IsOk( h, aSubShape)) {
841 if ( assignedTo ) *assignedTo = aSubShape;
848 // user sorted submeshes of ancestors, according to stored submesh priority
849 std::vector< SMESH_subMesh * > & ancestors =
850 const_cast< std::vector< SMESH_subMesh * > & > ( aSubMesh->GetAncestors() );
851 SortByMeshOrder( ancestors );
853 vector<SMESH_subMesh*>::const_iterator smIt = ancestors.begin();
854 for ( ; smIt != ancestors.end(); smIt++ )
856 const TopoDS_Shape& curSh = (*smIt)->GetSubShape();
857 const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(curSh);
858 list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
859 for ( ; hyp != hypList.end(); hyp++ ) {
860 const SMESH_Hypothesis * h = cSMESH_Hyp( *hyp );
861 if (aFilter.IsOk( h, curSh )) {
862 if ( assignedTo ) *assignedTo = curSh;
871 //================================================================================
873 * \brief Return hypotheses assigned to the shape
874 * \param aSubShape - the shape to check
875 * \param aFilter - the hypothesis filter
876 * \param aHypList - the list of the found hypotheses
877 * \param andAncestors - flag to check hypos assigned to ancestors of the shape
878 * \retval int - number of unique hypos in aHypList
880 //================================================================================
882 int SMESH_Mesh::GetHypotheses(const TopoDS_Shape & aSubShape,
883 const SMESH_HypoFilter& aFilter,
884 list <const SMESHDS_Hypothesis * >& aHypList,
885 const bool andAncestors,
886 list< TopoDS_Shape > * assignedTo/*=0*/) const
888 return GetHypotheses( const_cast< SMESH_Mesh* >(this)->GetSubMesh( aSubShape ),
889 aFilter, aHypList, andAncestors, assignedTo );
892 //================================================================================
894 * \brief Return hypotheses assigned to the shape of a sub-mesh
895 * \param aSubShape - the sub-mesh to check
896 * \param aFilter - the hypothesis filter
897 * \param aHypList - the list of the found hypotheses
898 * \param andAncestors - flag to check hypos assigned to ancestors of the shape
899 * \retval int - number of unique hypos in aHypList
901 //================================================================================
903 int SMESH_Mesh::GetHypotheses(const SMESH_subMesh * aSubMesh,
904 const SMESH_HypoFilter& aFilter,
905 list <const SMESHDS_Hypothesis * >& aHypList,
906 const bool andAncestors,
907 list< TopoDS_Shape > * assignedTo/*=0*/) const
909 if ( !aSubMesh ) return 0;
911 set<string> hypTypes; // to exclude same type hypos from the result list
914 // only one main hypothesis is allowed
915 bool mainHypFound = false;
918 list<const SMESHDS_Hypothesis*>::const_iterator hyp;
919 for ( hyp = aHypList.begin(); hyp != aHypList.end(); hyp++ ) {
920 if ( hypTypes.insert( (*hyp)->GetName() ).second )
922 if ( !cSMESH_Hyp(*hyp)->IsAuxiliary() )
926 // get hypos from aSubShape
928 const TopoDS_Shape & aSubShape = aSubMesh->GetSubShape();
929 const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(aSubShape);
930 for ( hyp = hypList.begin(); hyp != hypList.end(); hyp++ )
932 const SMESH_Hypothesis* h = cSMESH_Hyp( *hyp );
933 if (( aFilter.IsOk( h, aSubShape )) &&
934 ( h->IsAuxiliary() || !mainHypFound ) &&
935 ( h->IsAuxiliary() || hypTypes.insert( h->GetName() ).second ))
937 aHypList.push_back( *hyp );
939 if ( !h->IsAuxiliary() )
941 if ( assignedTo ) assignedTo->push_back( aSubShape );
946 // get hypos from ancestors of aSubShape
949 // user sorted submeshes of ancestors, according to stored submesh priority
950 std::vector< SMESH_subMesh * > & ancestors =
951 const_cast< std::vector< SMESH_subMesh * > & > ( aSubMesh->GetAncestors() );
952 SortByMeshOrder( ancestors );
954 vector<SMESH_subMesh*>::const_iterator smIt = ancestors.begin();
955 for ( ; smIt != ancestors.end(); smIt++ )
957 const TopoDS_Shape& curSh = (*smIt)->GetSubShape();
958 const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(curSh);
959 for ( hyp = hypList.begin(); hyp != hypList.end(); hyp++ )
961 const SMESH_Hypothesis* h = cSMESH_Hyp( *hyp );
962 if (( aFilter.IsOk( h, curSh )) &&
963 ( h->IsAuxiliary() || !mainHypFound ) &&
964 ( h->IsAuxiliary() || hypTypes.insert( h->GetName() ).second ))
966 aHypList.push_back( *hyp );
968 if ( !h->IsAuxiliary() )
970 if ( assignedTo ) assignedTo->push_back( curSh );
978 //================================================================================
980 * \brief Return a hypothesis by its ID
982 //================================================================================
984 SMESH_Hypothesis * SMESH_Mesh::GetHypothesis(const int anHypId) const
986 StudyContextStruct *sc = _gen->GetStudyContext();
987 if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
990 SMESH_Hypothesis *anHyp = sc->mapHypothesis[anHypId];
994 //=============================================================================
998 //=============================================================================
1000 const list<SMESHDS_Command*> & SMESH_Mesh::GetLog() throw(SALOME_Exception)
1002 Unexpect aCatch(SalomeException);
1003 return _myMeshDS->GetScript()->GetCommands();
1006 //=============================================================================
1010 //=============================================================================
1011 void SMESH_Mesh::ClearLog() throw(SALOME_Exception)
1013 Unexpect aCatch(SalomeException);
1014 _myMeshDS->GetScript()->Clear();
1017 //=============================================================================
1019 * Get or Create the SMESH_subMesh object implementation
1021 //=============================================================================
1023 SMESH_subMesh * SMESH_Mesh::GetSubMesh(const TopoDS_Shape & aSubShape)
1024 throw(SALOME_Exception)
1026 int index = _myMeshDS->ShapeToIndex(aSubShape);
1027 if ( !index && aSubShape.IsNull() )
1030 // for submeshes on GEOM Group
1031 if (( !index || index > _nbSubShapes ) && aSubShape.ShapeType() == TopAbs_COMPOUND )
1033 TopoDS_Iterator it( aSubShape );
1036 index = _myMeshDS->AddCompoundSubmesh( aSubShape, it.Value().ShapeType() );
1037 // fill map of Ancestors
1038 while ( _nbSubShapes < index )
1039 fillAncestorsMap( _myMeshDS->IndexToShape( ++_nbSubShapes ));
1043 // return NULL; // neither sub-shape nor a group
1045 SMESH_subMesh* aSubMesh = _subMeshHolder->Get( index );
1048 aSubMesh = new SMESH_subMesh(index, this, _myMeshDS, aSubShape);
1049 _subMeshHolder->Add( index, aSubMesh );
1051 // include non-computable sub-meshes in SMESH_subMesh::_ancestors of sub-submeshes
1052 switch ( aSubShape.ShapeType() ) {
1053 case TopAbs_COMPOUND:
1056 for ( TopoDS_Iterator subIt( aSubShape ); subIt.More(); subIt.Next() )
1058 SMESH_subMesh* sm = GetSubMesh( subIt.Value() );
1059 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(/*inclideSelf=*/true);
1060 while ( smIt->more() )
1061 smIt->next()->ClearAncestors();
1069 //=============================================================================
1071 * Get the SMESH_subMesh object implementation. Don't create it, return null
1072 * if it does not exist.
1074 //=============================================================================
1076 SMESH_subMesh *SMESH_Mesh::GetSubMeshContaining(const TopoDS_Shape & aSubShape) const
1077 throw(SALOME_Exception)
1079 int index = _myMeshDS->ShapeToIndex(aSubShape);
1080 return GetSubMeshContaining( index );
1083 //=============================================================================
1085 * Get the SMESH_subMesh object implementation. Don't create it, return null
1086 * if it does not exist.
1088 //=============================================================================
1090 SMESH_subMesh *SMESH_Mesh::GetSubMeshContaining(const int aShapeID) const
1091 throw(SALOME_Exception)
1093 SMESH_subMesh *aSubMesh = _subMeshHolder->Get( aShapeID );
1098 //================================================================================
1100 * \brief Return sub-meshes of groups containing the given sub-shape
1102 //================================================================================
1104 list<SMESH_subMesh*>
1105 SMESH_Mesh::GetGroupSubMeshesContaining(const TopoDS_Shape & aSubShape) const
1106 throw(SALOME_Exception)
1108 list<SMESH_subMesh*> found;
1110 SMESH_subMesh * subMesh = GetSubMeshContaining(aSubShape);
1114 // sub-meshes of groups have max IDs, so search from the map end
1115 SMESH_subMeshIteratorPtr smIt( _subMeshHolder->GetIterator( /*reverse=*/true ) );
1116 while ( smIt->more() ) {
1117 SMESH_subMesh* sm = smIt->next();
1118 SMESHDS_SubMesh * ds = sm->GetSubMeshDS();
1119 if ( ds && ds->IsComplexSubmesh() ) {
1120 if ( SMESH_MesherHelper::IsSubShape( aSubShape, sm->GetSubShape() ))
1122 found.push_back( sm );
1126 break; // the rest sub-meshes are not those of groups
1130 if ( found.empty() ) // maybe the main shape is a COMPOUND (issue 0021530)
1132 if ( SMESH_subMesh * mainSM = GetSubMeshContaining(1) )
1133 if ( mainSM->GetSubShape().ShapeType() == TopAbs_COMPOUND )
1135 TopoDS_Iterator it( mainSM->GetSubShape() );
1136 if ( it.Value().ShapeType() == aSubShape.ShapeType() &&
1137 SMESH_MesherHelper::IsSubShape( aSubShape, mainSM->GetSubShape() ))
1138 found.push_back( mainSM );
1141 else // issue 0023068
1143 if ( SMESH_subMesh * mainSM = GetSubMeshContaining(1) )
1144 if ( mainSM->GetSubShape().ShapeType() == TopAbs_COMPOUND )
1145 found.push_back( mainSM );
1149 //=======================================================================
1150 //function : IsUsedHypothesis
1151 //purpose : Return True if anHyp is used to mesh aSubShape
1152 //=======================================================================
1154 bool SMESH_Mesh::IsUsedHypothesis(SMESHDS_Hypothesis * anHyp,
1155 const SMESH_subMesh* aSubMesh)
1157 SMESH_Hypothesis* hyp = static_cast<SMESH_Hypothesis*>(anHyp);
1159 // check if anHyp can be used to mesh aSubMesh
1160 if ( !aSubMesh || !aSubMesh->IsApplicableHypothesis( hyp ))
1163 SMESH_Algo *algo = aSubMesh->GetAlgo();
1166 if (anHyp->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
1167 return ( anHyp == algo );
1169 // algorithm parameter
1172 // look trough hypotheses used by algo
1173 const SMESH_HypoFilter* hypoKind;
1174 if (( hypoKind = algo->GetCompatibleHypoFilter( !hyp->IsAuxiliary() ))) {
1175 list <const SMESHDS_Hypothesis * > usedHyps;
1176 if ( GetHypotheses( aSubMesh, *hypoKind, usedHyps, true ))
1177 return ( find( usedHyps.begin(), usedHyps.end(), anHyp ) != usedHyps.end() );
1184 //=======================================================================
1185 //function : NotifySubMeshesHypothesisModification
1186 //purpose : Say all submeshes using theChangedHyp that it has been modified
1187 //=======================================================================
1189 void SMESH_Mesh::NotifySubMeshesHypothesisModification(const SMESH_Hypothesis* hyp)
1191 Unexpect aCatch(SalomeException);
1193 if ( !GetMeshDS()->IsUsedHypothesis( hyp ))
1197 _callUp->HypothesisModified();
1200 const SMESH_HypoFilter* compatibleHypoKind;
1201 list <const SMESHDS_Hypothesis * > usedHyps;
1202 vector< SMESH_subMesh* > smToNotify;
1203 bool allMeshedEdgesNotified = true;
1205 SMESH_subMeshIteratorPtr smIt( _subMeshHolder->GetIterator() );
1206 while ( smIt->more() )
1208 SMESH_subMesh* aSubMesh = smIt->next();
1209 bool toNotify = false;
1211 // if aSubMesh meshing depends on hyp,
1212 // we call aSubMesh->AlgoStateEngine( MODIF_HYP, hyp ) that causes either
1213 // 1) clearing already computed aSubMesh or
1214 // 2) changing algo_state from MISSING_HYP to HYP_OK when parameters of hyp becomes valid,
1215 // other possible changes are not interesting. (IPAL0052457 - assigning hyp performance pb)
1216 if ( aSubMesh->GetComputeState() == SMESH_subMesh::COMPUTE_OK ||
1217 aSubMesh->GetComputeState() == SMESH_subMesh::FAILED_TO_COMPUTE ||
1218 aSubMesh->GetAlgoState() == SMESH_subMesh::MISSING_HYP ||
1219 hyp->DataDependOnParams() )
1221 const TopoDS_Shape & aSubShape = aSubMesh->GetSubShape();
1223 if (( aSubMesh->IsApplicableHypothesis( hyp )) &&
1224 ( algo = aSubMesh->GetAlgo() ) &&
1225 ( compatibleHypoKind = algo->GetCompatibleHypoFilter( !hyp->IsAuxiliary() )) &&
1226 ( compatibleHypoKind->IsOk( hyp, aSubShape )))
1228 // check if hyp is used by algo
1230 toNotify = ( GetHypotheses( aSubMesh, *compatibleHypoKind, usedHyps, true ) &&
1231 std::find( usedHyps.begin(), usedHyps.end(), hyp ) != usedHyps.end() );
1236 smToNotify.push_back( aSubMesh );
1237 if ( aSubMesh->GetAlgoState() == SMESH_subMesh::MISSING_HYP )
1238 allMeshedEdgesNotified = false; // update of algo state needed, not mesh clearing
1242 if ( !aSubMesh->IsEmpty() &&
1243 aSubMesh->GetSubShape().ShapeType() == TopAbs_EDGE )
1244 allMeshedEdgesNotified = false;
1247 if ( smToNotify.empty() )
1250 // if all meshed EDGEs will be notified then the notification is equivalent
1251 // to the whole mesh clearing, which is usually faster
1252 if ( allMeshedEdgesNotified && NbNodes() > 0 )
1258 // notify in reverse order to avoid filling the pool of IDs
1259 for ( int i = smToNotify.size()-1; i >= 0; --i )
1260 smToNotify[i]->AlgoStateEngine(SMESH_subMesh::MODIF_HYP,
1261 const_cast< SMESH_Hypothesis*>( hyp ));
1263 HasModificationsToDiscard(); // to reset _isModified flag if mesh becomes empty
1264 GetMeshDS()->Modified();
1267 //=============================================================================
1269 * Auto color functionality
1271 //=============================================================================
1272 void SMESH_Mesh::SetAutoColor(bool theAutoColor) throw(SALOME_Exception)
1274 Unexpect aCatch(SalomeException);
1275 _isAutoColor = theAutoColor;
1278 bool SMESH_Mesh::GetAutoColor() throw(SALOME_Exception)
1280 Unexpect aCatch(SalomeException);
1281 return _isAutoColor;
1284 //=======================================================================
1285 //function : SetIsModified
1286 //purpose : Set the flag meaning that the mesh has been edited "manually"
1287 //=======================================================================
1289 void SMESH_Mesh::SetIsModified(bool isModified)
1291 _isModified = isModified;
1294 // check if mesh becomes empty as result of modification
1295 HasModificationsToDiscard();
1298 //=======================================================================
1299 //function : HasModificationsToDiscard
1300 //purpose : Return true if the mesh has been edited since a total re-compute
1301 // and those modifications may prevent successful partial re-compute.
1302 // As a side effect reset _isModified flag if mesh is empty
1304 //=======================================================================
1306 bool SMESH_Mesh::HasModificationsToDiscard() const
1308 if ( ! _isModified )
1311 // return true if the next Compute() will be partial and
1312 // existing but changed elements may prevent successful re-compute
1313 bool hasComputed = false, hasNotComputed = false;
1314 SMESH_subMeshIteratorPtr smIt( _subMeshHolder->GetIterator() );
1315 while ( smIt->more() )
1317 const SMESH_subMesh* aSubMesh = smIt->next();
1318 switch ( aSubMesh->GetSubShape().ShapeType() )
1323 if ( aSubMesh->IsMeshComputed() )
1326 hasNotComputed = true;
1327 if ( hasComputed && hasNotComputed)
1333 if ( NbNodes() < 1 )
1334 const_cast<SMESH_Mesh*>(this)->_isModified = false;
1339 //=============================================================================
1341 * \brief Return true if all sub-meshes are computed OK - to update an icon
1343 //=============================================================================
1345 bool SMESH_Mesh::IsComputedOK()
1347 if ( NbNodes() == 0 )
1350 if ( !HasShapeToMesh() )
1353 if ( SMESH_subMesh* mainSM = GetSubMeshContaining( 1 ))
1355 SMESH_subMeshIteratorPtr smIt = mainSM->getDependsOnIterator(/*includeSelf=*/true);
1356 while ( smIt->more() )
1358 const SMESH_subMesh* sm = smIt->next();
1359 if ( !sm->IsAlwaysComputed() )
1360 switch ( sm->GetComputeState() )
1362 case SMESH_subMesh::NOT_READY:
1363 case SMESH_subMesh::COMPUTE_OK:
1365 case SMESH_subMesh::FAILED_TO_COMPUTE:
1366 case SMESH_subMesh::READY_TO_COMPUTE:
1374 //================================================================================
1376 * \brief Check if any groups of the same type have equal names
1378 //================================================================================
1380 bool SMESH_Mesh::HasDuplicatedGroupNamesMED()
1382 // Corrected for Mantis issue 0020028
1383 map< SMDSAbs_ElementType, set<string> > aGroupNames;
1384 for ( map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ )
1386 SMESH_Group* aGroup = it->second;
1387 SMDSAbs_ElementType aType = aGroup->GetGroupDS()->GetType();
1388 string aGroupName = aGroup->GetName();
1389 aGroupName.resize( MAX_MED_GROUP_NAME_LENGTH );
1390 if ( !aGroupNames[aType].insert(aGroupName).second )
1397 //================================================================================
1399 * \brief Export the mesh to a med file
1400 * \param [in] file - name of the MED file
1401 * \param [in] theMeshName - name of this mesh
1402 * \param [in] theAutoGroups - boolean parameter for creating/not creating
1403 * the groups Group_On_All_Nodes, Group_On_All_Faces, ... ;
1404 * the typical use is auto_groups=false.
1405 * \param [in] meshPart - mesh data to export
1406 * \param [in] theAutoDimension - if \c true, a space dimension of a MED mesh can be either
1407 * - 1D if all mesh nodes lie on OX coordinate axis, or
1408 * - 2D if all mesh nodes lie on XOY coordinate plane, or
1409 * - 3D in the rest cases.
1410 * If \a theAutoDimension is \c false, the space dimension is always 3.
1411 * \param [in] theAddODOnVertices - to create 0D elements on all vertices
1412 * \param [in] theAllElemsToGroup - to make every element to belong to any group (PAL23413)
1413 * \return int - mesh index in the file
1415 //================================================================================
1417 void SMESH_Mesh::ExportMED(const char * file,
1418 const char* theMeshName,
1420 const SMESHDS_Mesh* meshPart,
1421 bool theAutoDimension,
1422 bool theAddODOnVertices,
1423 bool theAllElemsToGroup)
1424 throw(SALOME_Exception)
1426 //MESSAGE("MED_VERSION:"<< theVersion);
1429 DriverMED_W_SMESHDS_Mesh myWriter;
1430 myWriter.SetFile ( file );
1431 myWriter.SetMesh ( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS );
1432 myWriter.SetAutoDimension( theAutoDimension );
1433 myWriter.AddODOnVertices ( theAddODOnVertices );
1435 myWriter.SetMeshId ( _id );
1437 myWriter.SetMeshId ( -1 );
1438 myWriter.SetMeshName ( theMeshName );
1441 if ( theAutoGroups ) {
1442 myWriter.AddGroupOfNodes();
1443 myWriter.AddGroupOfEdges();
1444 myWriter.AddGroupOfFaces();
1445 myWriter.AddGroupOfVolumes();
1446 myWriter.AddGroupOf0DElems();
1447 myWriter.AddGroupOfBalls();
1449 if ( theAllElemsToGroup )
1450 myWriter.AddAllToGroup();
1452 // Pass groups to writer. Provide unique group names.
1453 //set<string> aGroupNames; // Corrected for Mantis issue 0020028
1456 map< SMDSAbs_ElementType, set<string> > aGroupNames;
1458 int maxNbIter = 10000; // to guarantee cycle finish
1459 for ( map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ ) {
1460 SMESH_Group* aGroup = it->second;
1461 SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
1463 SMDSAbs_ElementType aType = aGroupDS->GetType();
1464 string aGroupName0 = aGroup->GetName();
1465 aGroupName0.resize(MAX_MED_GROUP_NAME_LENGTH);
1466 string aGroupName = aGroupName0;
1467 for (int i = 1; !aGroupNames[aType].insert(aGroupName).second && i < maxNbIter; i++) {
1468 sprintf(&aString[0], "GR_%d_%s", i, aGroupName0.c_str());
1469 aGroupName = aString;
1470 aGroupName.resize(MAX_MED_GROUP_NAME_LENGTH);
1472 aGroupDS->SetStoreName( aGroupName.c_str() );
1473 myWriter.AddGroup( aGroupDS );
1480 SMESH_CATCH( SMESH::throwSalomeEx );
1483 //================================================================================
1485 * \brief Export the mesh to a SAUV file
1487 //================================================================================
1489 void SMESH_Mesh::ExportSAUV(const char *file,
1490 const char* theMeshName,
1492 throw(SALOME_Exception)
1494 std::string medfilename(file);
1495 medfilename += ".med";
1498 cmd = "%PYTHONBIN% ";
1503 cmd += "from medutilities import my_remove ; my_remove(r'" + medfilename + "')";
1505 system(cmd.c_str());
1506 ExportMED(medfilename.c_str(), theMeshName, theAutoGroups,
1507 /*meshPart=*/NULL, /*theAutoDimension=*/false, /*theAddODOnVertices=*/false,
1508 /*theAllElemsToGroup=*/true ); // theAllElemsToGroup is for PAL0023413
1510 cmd = "%PYTHONBIN% ";
1515 cmd += "from medutilities import convert ; convert(r'" + medfilename + "', 'MED', 'GIBI', 1, r'" + file + "')";
1517 system(cmd.c_str());
1519 cmd = "%PYTHONBIN% ";
1524 cmd += "from medutilities import my_remove ; my_remove(r'" + medfilename + "')";
1526 system(cmd.c_str());
1529 //================================================================================
1531 * \brief Export the mesh to a DAT file
1533 //================================================================================
1535 void SMESH_Mesh::ExportDAT(const char * file,
1536 const SMESHDS_Mesh* meshPart) throw(SALOME_Exception)
1538 Unexpect aCatch(SalomeException);
1539 DriverDAT_W_SMDS_Mesh myWriter;
1540 myWriter.SetFile( file );
1541 myWriter.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS );
1542 myWriter.SetMeshId(_id);
1546 //================================================================================
1548 * \brief Export the mesh to an UNV file
1550 //================================================================================
1552 void SMESH_Mesh::ExportUNV(const char * file,
1553 const SMESHDS_Mesh* meshPart) throw(SALOME_Exception)
1555 Unexpect aCatch(SalomeException);
1556 DriverUNV_W_SMDS_Mesh myWriter;
1557 myWriter.SetFile( file );
1558 myWriter.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS );
1559 myWriter.SetMeshId(_id);
1560 // myWriter.SetGroups(_mapGroup);
1562 // pass group names to SMESHDS
1565 for ( map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ ) {
1566 SMESH_Group* aGroup = it->second;
1567 SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
1569 string aGroupName = aGroup->GetName();
1570 aGroupDS->SetStoreName( aGroupName.c_str() );
1571 myWriter.AddGroup( aGroupDS );
1578 //================================================================================
1580 * \brief Export the mesh to an STL file
1582 //================================================================================
1584 void SMESH_Mesh::ExportSTL(const char * file,
1587 const SMESHDS_Mesh* meshPart) throw(SALOME_Exception)
1589 Unexpect aCatch(SalomeException);
1590 DriverSTL_W_SMDS_Mesh myWriter;
1591 myWriter.SetFile( file );
1592 myWriter.SetIsAscii( isascii );
1593 myWriter.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS);
1594 myWriter.SetMeshId(_id);
1595 if ( name ) myWriter.SetName( name );
1599 //================================================================================
1601 * \brief Export the mesh to the CGNS file
1603 //================================================================================
1605 void SMESH_Mesh::ExportCGNS(const char * file,
1606 const SMESHDS_Mesh* meshDS,
1607 const char * meshName,
1608 const bool groupElemsByType)
1610 int res = Driver_Mesh::DRS_FAIL;
1612 // pass group names to SMESHDS
1613 for ( map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ ) {
1614 SMESH_Group* group = it->second;
1615 SMESHDS_GroupBase* groupDS = group->GetGroupDS();
1617 string groupName = group->GetName();
1618 groupDS->SetStoreName( groupName.c_str() );
1623 DriverCGNS_Write myWriter;
1624 myWriter.SetFile( file );
1625 myWriter.SetMesh( const_cast<SMESHDS_Mesh*>( meshDS ));
1626 myWriter.SetMeshName( SMESH_Comment("Mesh_") << meshDS->GetPersistentId());
1627 if ( meshName && meshName[0] )
1628 myWriter.SetMeshName( meshName );
1629 myWriter.SetElementsByType( groupElemsByType );
1630 res = myWriter.Perform();
1631 if ( res != Driver_Mesh::DRS_OK )
1633 SMESH_ComputeErrorPtr err = myWriter.GetError();
1634 if ( err && !err->IsOK() && !err->myComment.empty() )
1635 throw SALOME_Exception(("Export failed: " + err->myComment ).c_str() );
1639 if ( res != Driver_Mesh::DRS_OK )
1640 throw SALOME_Exception("Export failed");
1643 //================================================================================
1645 * \brief Export the mesh to a GMF file
1647 //================================================================================
1649 void SMESH_Mesh::ExportGMF(const char * file,
1650 const SMESHDS_Mesh* meshDS,
1651 bool withRequiredGroups)
1653 DriverGMF_Write myWriter;
1654 myWriter.SetFile( file );
1655 myWriter.SetMesh( const_cast<SMESHDS_Mesh*>( meshDS ));
1656 myWriter.SetExportRequiredGroups( withRequiredGroups );
1661 //================================================================================
1663 * \brief Return a ratio of "compute cost" of computed sub-meshes to the whole
1666 //================================================================================
1668 double SMESH_Mesh::GetComputeProgress() const
1670 double totalCost = 1e-100, computedCost = 0;
1671 const SMESH_subMesh* curSM = _gen->GetCurrentSubMesh();
1673 // get progress of a current algo
1674 TColStd_MapOfInteger currentSubIds;
1676 if ( SMESH_Algo* algo = curSM->GetAlgo() )
1678 int algoNotDoneCost = 0, algoDoneCost = 0;
1679 const std::vector<SMESH_subMesh*>& smToCompute = algo->SubMeshesToCompute();
1680 for ( size_t i = 0; i < smToCompute.size(); ++i )
1682 if ( smToCompute[i]->IsEmpty() || smToCompute.size() == 1 )
1683 algoNotDoneCost += smToCompute[i]->GetComputeCost();
1685 algoDoneCost += smToCompute[i]->GetComputeCost();
1686 currentSubIds.Add( smToCompute[i]->GetId() );
1692 rate = algo->GetProgress();
1696 cerr << "Exception in " << algo->GetName() << "::GetProgress()" << endl;
1699 if ( 0. < rate && rate < 1.001 )
1701 computedCost += rate * ( algoDoneCost + algoNotDoneCost );
1705 rate = algo->GetProgressByTic();
1706 computedCost += algoDoneCost + rate * algoNotDoneCost;
1708 // cout << "rate: "<<rate << " algoNotDoneCost: " << algoNotDoneCost << endl;
1711 // get cost of already treated sub-meshes
1712 if ( SMESH_subMesh* mainSM = GetSubMeshContaining( 1 ))
1714 SMESH_subMeshIteratorPtr smIt = mainSM->getDependsOnIterator(/*includeSelf=*/true);
1715 while ( smIt->more() )
1717 const SMESH_subMesh* sm = smIt->next();
1718 const int smCost = sm->GetComputeCost();
1719 totalCost += smCost;
1720 if ( !currentSubIds.Contains( sm->GetId() ) )
1722 if (( !sm->IsEmpty() ) ||
1723 ( sm->GetComputeState() == SMESH_subMesh::FAILED_TO_COMPUTE &&
1724 !sm->DependsOn( curSM ) ))
1725 computedCost += smCost;
1729 // cout << "Total: " << totalCost
1730 // << " computed: " << computedCost << " progress: " << computedCost / totalCost
1731 // << " nbElems: " << GetMeshDS()->GetMeshInfo().NbElements() << endl;
1732 return computedCost / totalCost;
1735 //================================================================================
1737 * \brief Return number of nodes in the mesh
1739 //================================================================================
1741 int SMESH_Mesh::NbNodes() const throw(SALOME_Exception)
1743 Unexpect aCatch(SalomeException);
1744 return _myMeshDS->NbNodes();
1747 //================================================================================
1749 * \brief Return number of edges of given order in the mesh
1751 //================================================================================
1753 int SMESH_Mesh::Nb0DElements() const throw(SALOME_Exception)
1755 Unexpect aCatch(SalomeException);
1756 return _myMeshDS->GetMeshInfo().Nb0DElements();
1759 //================================================================================
1761 * \brief Return number of edges of given order in the mesh
1763 //================================================================================
1765 int SMESH_Mesh::NbEdges(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1767 Unexpect aCatch(SalomeException);
1768 return _myMeshDS->GetMeshInfo().NbEdges(order);
1771 //================================================================================
1773 * \brief Return number of faces of given order in the mesh
1775 //================================================================================
1777 int SMESH_Mesh::NbFaces(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1779 Unexpect aCatch(SalomeException);
1780 return _myMeshDS->GetMeshInfo().NbFaces(order);
1783 //================================================================================
1785 * \brief Return the number of faces in the mesh
1787 //================================================================================
1789 int SMESH_Mesh::NbTriangles(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1791 Unexpect aCatch(SalomeException);
1792 return _myMeshDS->GetMeshInfo().NbTriangles(order);
1795 //================================================================================
1797 * \brief Return number of biquadratic triangles in the mesh
1799 //================================================================================
1801 int SMESH_Mesh::NbBiQuadTriangles() const throw(SALOME_Exception)
1803 Unexpect aCatch(SalomeException);
1804 return _myMeshDS->GetMeshInfo().NbBiQuadTriangles();
1807 //================================================================================
1809 * \brief Return the number nodes faces in the mesh
1811 //================================================================================
1813 int SMESH_Mesh::NbQuadrangles(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1815 Unexpect aCatch(SalomeException);
1816 return _myMeshDS->GetMeshInfo().NbQuadrangles(order);
1819 //================================================================================
1821 * \brief Return number of biquadratic quadrangles in the mesh
1823 //================================================================================
1825 int SMESH_Mesh::NbBiQuadQuadrangles() const throw(SALOME_Exception)
1827 Unexpect aCatch(SalomeException);
1828 return _myMeshDS->GetMeshInfo().NbBiQuadQuadrangles();
1831 //================================================================================
1833 * \brief Return the number of polygonal faces in the mesh
1835 //================================================================================
1837 int SMESH_Mesh::NbPolygons(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1839 Unexpect aCatch(SalomeException);
1840 return _myMeshDS->GetMeshInfo().NbPolygons(order);
1843 //================================================================================
1845 * \brief Return number of volumes of given order in the mesh
1847 //================================================================================
1849 int SMESH_Mesh::NbVolumes(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1851 Unexpect aCatch(SalomeException);
1852 return _myMeshDS->GetMeshInfo().NbVolumes(order);
1855 //================================================================================
1857 * \brief Return number of tetrahedrons of given order in the mesh
1859 //================================================================================
1861 int SMESH_Mesh::NbTetras(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1863 Unexpect aCatch(SalomeException);
1864 return _myMeshDS->GetMeshInfo().NbTetras(order);
1867 //================================================================================
1869 * \brief Return number of hexahedrons of given order in the mesh
1871 //================================================================================
1873 int SMESH_Mesh::NbHexas(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1875 Unexpect aCatch(SalomeException);
1876 return _myMeshDS->GetMeshInfo().NbHexas(order);
1879 //================================================================================
1881 * \brief Return number of triquadratic hexahedrons in the mesh
1883 //================================================================================
1885 int SMESH_Mesh::NbTriQuadraticHexas() const throw(SALOME_Exception)
1887 Unexpect aCatch(SalomeException);
1888 return _myMeshDS->GetMeshInfo().NbTriQuadHexas();
1891 //================================================================================
1893 * \brief Return number of pyramids of given order in the mesh
1895 //================================================================================
1897 int SMESH_Mesh::NbPyramids(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1899 Unexpect aCatch(SalomeException);
1900 return _myMeshDS->GetMeshInfo().NbPyramids(order);
1903 //================================================================================
1905 * \brief Return number of prisms (penthahedrons) of given order in the mesh
1907 //================================================================================
1909 int SMESH_Mesh::NbPrisms(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1911 Unexpect aCatch(SalomeException);
1912 return _myMeshDS->GetMeshInfo().NbPrisms(order);
1915 int SMESH_Mesh::NbQuadPrisms() const throw (SALOME_Exception)
1917 Unexpect aCatch(SalomeException);
1918 return _myMeshDS->GetMeshInfo().NbQuadPrisms();
1921 int SMESH_Mesh::NbBiQuadPrisms() const throw (SALOME_Exception)
1923 Unexpect aCatch(SalomeException);
1924 return _myMeshDS->GetMeshInfo().NbBiQuadPrisms();
1928 //================================================================================
1930 * \brief Return number of hexagonal prisms in the mesh
1932 //================================================================================
1934 int SMESH_Mesh::NbHexagonalPrisms() const throw(SALOME_Exception)
1936 Unexpect aCatch(SalomeException);
1937 return _myMeshDS->GetMeshInfo().NbHexPrisms();
1940 //================================================================================
1942 * \brief Return number of polyhedrons in the mesh
1944 //================================================================================
1946 int SMESH_Mesh::NbPolyhedrons() const throw(SALOME_Exception)
1948 Unexpect aCatch(SalomeException);
1949 return _myMeshDS->GetMeshInfo().NbPolyhedrons();
1952 //================================================================================
1954 * \brief Return number of ball elements in the mesh
1956 //================================================================================
1958 int SMESH_Mesh::NbBalls() const throw(SALOME_Exception)
1960 Unexpect aCatch(SalomeException);
1961 return _myMeshDS->GetMeshInfo().NbBalls();
1964 //================================================================================
1966 * \brief Return number of submeshes in the mesh
1968 //================================================================================
1970 int SMESH_Mesh::NbSubMesh() const throw(SALOME_Exception)
1972 Unexpect aCatch(SalomeException);
1973 return _myMeshDS->NbSubMesh();
1976 //================================================================================
1978 * \brief Returns number of meshes in the Study, that is supposed to be
1979 * equal to SMESHDS_Document::NbMeshes()
1981 //================================================================================
1983 int SMESH_Mesh::NbMeshes() const // nb meshes in the Study
1985 return _myDocument->NbMeshes();
1988 //=======================================================================
1989 //function : IsNotConformAllowed
1990 //purpose : check if a hypothesis allowing notconform mesh is present
1991 //=======================================================================
1993 bool SMESH_Mesh::IsNotConformAllowed() const
1995 if(MYDEBUG) MESSAGE("SMESH_Mesh::IsNotConformAllowed");
1997 static SMESH_HypoFilter filter( SMESH_HypoFilter::HasName( "NotConformAllowed" ));
1998 return GetHypothesis( _myMeshDS->ShapeToMesh(), filter, false );
2001 //=======================================================================
2002 //function : IsMainShape
2004 //=======================================================================
2006 bool SMESH_Mesh::IsMainShape(const TopoDS_Shape& theShape) const
2008 return theShape.IsSame(_myMeshDS->ShapeToMesh() );
2011 //=============================================================================
2015 //=============================================================================
2017 SMESH_Group* SMESH_Mesh::AddGroup (const SMDSAbs_ElementType theType,
2018 const char* theName,
2020 const TopoDS_Shape& theShape,
2021 const SMESH_PredicatePtr& thePredicate)
2023 if (_mapGroup.count(_groupId))
2026 SMESH_Group* aGroup = new SMESH_Group (theId, this, theType, theName, theShape, thePredicate);
2027 GetMeshDS()->AddGroup( aGroup->GetGroupDS() );
2028 _mapGroup[_groupId++] = aGroup;
2032 //================================================================================
2034 * \brief Creates a group based on an existing SMESHDS group. Group ID should be unique
2036 //================================================================================
2038 SMESH_Group* SMESH_Mesh::AddGroup (SMESHDS_GroupBase* groupDS) throw(SALOME_Exception)
2041 throw SALOME_Exception(LOCALIZED ("SMESH_Mesh::AddGroup(): NULL SMESHDS_GroupBase"));
2043 map <int, SMESH_Group*>::iterator i_g = _mapGroup.find( groupDS->GetID() );
2044 if ( i_g != _mapGroup.end() && i_g->second )
2046 if ( i_g->second->GetGroupDS() == groupDS )
2049 throw SALOME_Exception(LOCALIZED ("SMESH_Mesh::AddGroup() wrong ID of SMESHDS_GroupBase"));
2051 SMESH_Group* aGroup = new SMESH_Group (groupDS);
2052 _mapGroup[ groupDS->GetID() ] = aGroup;
2053 GetMeshDS()->AddGroup( aGroup->GetGroupDS() );
2055 _groupId = 1 + _mapGroup.rbegin()->first;
2061 //================================================================================
2063 * \brief Creates SMESH_Groups for not wrapped SMESHDS_Groups
2064 * \retval bool - true if new SMESH_Groups have been created
2067 //================================================================================
2069 bool SMESH_Mesh::SynchronizeGroups()
2071 const size_t nbGroups = _mapGroup.size();
2072 const set<SMESHDS_GroupBase*>& groups = _myMeshDS->GetGroups();
2073 set<SMESHDS_GroupBase*>::const_iterator gIt = groups.begin();
2074 for ( ; gIt != groups.end(); ++gIt )
2076 SMESHDS_GroupBase* groupDS = (SMESHDS_GroupBase*) *gIt;
2077 _groupId = groupDS->GetID();
2078 if ( !_mapGroup.count( _groupId ))
2079 _mapGroup[_groupId] = new SMESH_Group( groupDS );
2081 if ( !_mapGroup.empty() )
2082 _groupId = _mapGroup.rbegin()->first + 1;
2084 return nbGroups < _mapGroup.size();
2087 //================================================================================
2089 * \brief Return iterator on all existing groups
2091 //================================================================================
2093 SMESH_Mesh::GroupIteratorPtr SMESH_Mesh::GetGroups() const
2095 typedef map <int, SMESH_Group *> TMap;
2096 return GroupIteratorPtr( new SMDS_mapIterator<TMap>( _mapGroup ));
2099 //=============================================================================
2101 * \brief Return a group by ID
2103 //=============================================================================
2105 SMESH_Group* SMESH_Mesh::GetGroup (const int theGroupID)
2107 if (_mapGroup.find(theGroupID) == _mapGroup.end())
2109 return _mapGroup[theGroupID];
2113 //=============================================================================
2115 * \brief Return IDs of all groups
2117 //=============================================================================
2119 list<int> SMESH_Mesh::GetGroupIds() const
2122 for ( map<int, SMESH_Group*>::const_iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ )
2123 anIds.push_back( it->first );
2128 //================================================================================
2130 * \brief Set a caller of methods at level of CORBA API implementation.
2131 * The set upCaller will be deleted by SMESH_Mesh
2133 //================================================================================
2135 void SMESH_Mesh::SetCallUp( TCallUp* upCaller )
2137 if ( _callUp ) delete _callUp;
2141 //=============================================================================
2145 //=============================================================================
2147 bool SMESH_Mesh::RemoveGroup( const int theGroupID )
2149 if (_mapGroup.find(theGroupID) == _mapGroup.end())
2151 GetMeshDS()->RemoveGroup( _mapGroup[theGroupID]->GetGroupDS() );
2152 delete _mapGroup[theGroupID];
2153 _mapGroup.erase (theGroupID);
2155 _callUp->RemoveGroup( theGroupID );
2159 //=======================================================================
2160 //function : GetAncestors
2161 //purpose : return list of ancestors of theSubShape in the order
2162 // that lower dimension shapes come first.
2163 //=======================================================================
2165 const TopTools_ListOfShape& SMESH_Mesh::GetAncestors(const TopoDS_Shape& theS) const
2167 if ( _mapAncestors.Contains( theS ) )
2168 return _mapAncestors.FindFromKey( theS );
2170 static TopTools_ListOfShape emptyList;
2174 //=======================================================================
2176 //purpose : dumps contents of mesh to stream [ debug purposes ]
2177 //=======================================================================
2179 ostream& SMESH_Mesh::Dump(ostream& save)
2182 save << "========================== Dump contents of mesh ==========================" << endl << endl;
2183 save << ++clause << ") Total number of nodes: \t" << NbNodes() << endl;
2184 save << ++clause << ") Total number of edges: \t" << NbEdges() << endl;
2185 save << ++clause << ") Total number of faces: \t" << NbFaces() << endl;
2186 save << ++clause << ") Total number of polygons: \t" << NbPolygons() << endl;
2187 save << ++clause << ") Total number of volumes: \t" << NbVolumes() << endl;
2188 save << ++clause << ") Total number of polyhedrons:\t" << NbPolyhedrons() << endl << endl;
2189 for ( int isQuadratic = 0; isQuadratic < 2; ++isQuadratic )
2191 string orderStr = isQuadratic ? "quadratic" : "linear";
2192 SMDSAbs_ElementOrder order = isQuadratic ? ORDER_QUADRATIC : ORDER_LINEAR;
2194 save << ++clause << ") Total number of " << orderStr << " edges:\t" << NbEdges(order) << endl;
2195 save << ++clause << ") Total number of " << orderStr << " faces:\t" << NbFaces(order) << endl;
2196 if ( NbFaces(order) > 0 ) {
2197 int nb3 = NbTriangles(order);
2198 int nb4 = NbQuadrangles(order);
2199 save << clause << ".1) Number of " << orderStr << " triangles: \t" << nb3 << endl;
2200 save << clause << ".2) Number of " << orderStr << " quadrangles:\t" << nb4 << endl;
2201 if ( nb3 + nb4 != NbFaces(order) ) {
2202 map<int,int> myFaceMap;
2203 SMDS_FaceIteratorPtr itFaces=_myMeshDS->facesIterator();
2204 while( itFaces->more( ) ) {
2205 int nbNodes = itFaces->next()->NbNodes();
2206 if ( myFaceMap.find( nbNodes ) == myFaceMap.end() )
2207 myFaceMap[ nbNodes ] = 0;
2208 myFaceMap[ nbNodes ] = myFaceMap[ nbNodes ] + 1;
2210 save << clause << ".3) Faces in detail: " << endl;
2211 map <int,int>::iterator itF;
2212 for (itF = myFaceMap.begin(); itF != myFaceMap.end(); itF++)
2213 save << "--> nb nodes: " << itF->first << " - nb elements:\t" << itF->second << endl;
2216 save << ++clause << ") Total number of " << orderStr << " volumes:\t" << NbVolumes(order) << endl;
2217 if ( NbVolumes(order) > 0 ) {
2218 int nb8 = NbHexas(order);
2219 int nb4 = NbTetras(order);
2220 int nb5 = NbPyramids(order);
2221 int nb6 = NbPrisms(order);
2222 save << clause << ".1) Number of " << orderStr << " hexahedrons: \t" << nb8 << endl;
2223 save << clause << ".2) Number of " << orderStr << " tetrahedrons:\t" << nb4 << endl;
2224 save << clause << ".3) Number of " << orderStr << " prisms: \t" << nb6 << endl;
2225 save << clause << ".4) Number of " << orderStr << " pyramids: \t" << nb5 << endl;
2226 if ( nb8 + nb4 + nb5 + nb6 != NbVolumes(order) ) {
2227 map<int,int> myVolumesMap;
2228 SMDS_VolumeIteratorPtr itVolumes=_myMeshDS->volumesIterator();
2229 while( itVolumes->more( ) ) {
2230 int nbNodes = itVolumes->next()->NbNodes();
2231 if ( myVolumesMap.find( nbNodes ) == myVolumesMap.end() )
2232 myVolumesMap[ nbNodes ] = 0;
2233 myVolumesMap[ nbNodes ] = myVolumesMap[ nbNodes ] + 1;
2235 save << clause << ".5) Volumes in detail: " << endl;
2236 map <int,int>::iterator itV;
2237 for (itV = myVolumesMap.begin(); itV != myVolumesMap.end(); itV++)
2238 save << "--> nb nodes: " << itV->first << " - nb elements:\t" << itV->second << endl;
2243 save << "===========================================================================" << endl;
2247 //=======================================================================
2248 //function : GetElementType
2249 //purpose : Returns type of mesh element with certain id
2250 //=======================================================================
2252 SMDSAbs_ElementType SMESH_Mesh::GetElementType( const int id, const bool iselem )
2254 return _myMeshDS->GetElementType( id, iselem );
2257 //=============================================================================
2259 * \brief Convert group on geometry into standalone group
2261 //=============================================================================
2263 SMESH_Group* SMESH_Mesh::ConvertToStandalone ( int theGroupID )
2265 SMESH_Group* aGroup = 0;
2266 map < int, SMESH_Group * >::iterator itg = _mapGroup.find( theGroupID );
2267 if ( itg == _mapGroup.end() )
2270 SMESH_Group* anOldGrp = (*itg).second;
2271 if ( !anOldGrp || !anOldGrp->GetGroupDS() )
2273 SMESHDS_GroupBase* anOldGrpDS = anOldGrp->GetGroupDS();
2275 // create new standalone group
2276 aGroup = new SMESH_Group (theGroupID, this, anOldGrpDS->GetType(), anOldGrp->GetName() );
2277 _mapGroup[theGroupID] = aGroup;
2279 SMESHDS_Group* aNewGrpDS = dynamic_cast<SMESHDS_Group*>( aGroup->GetGroupDS() );
2280 GetMeshDS()->RemoveGroup( anOldGrpDS );
2281 GetMeshDS()->AddGroup( aNewGrpDS );
2283 // add elements (or nodes) into new created group
2284 SMDS_ElemIteratorPtr anItr = anOldGrpDS->GetElements();
2285 while ( anItr->more() )
2286 aNewGrpDS->Add( (anItr->next())->GetID() );
2289 aNewGrpDS->SetColor( anOldGrpDS->GetColor() );
2297 //=============================================================================
2299 * \brief remove submesh order from Mesh
2301 //=============================================================================
2303 void SMESH_Mesh::ClearMeshOrder()
2305 _mySubMeshOrder.clear();
2308 //=============================================================================
2310 * \brief remove submesh order from Mesh
2312 //=============================================================================
2314 void SMESH_Mesh::SetMeshOrder(const TListOfListOfInt& theOrder )
2316 _mySubMeshOrder = theOrder;
2319 //=============================================================================
2321 * \brief return submesh order if any
2323 //=============================================================================
2325 const TListOfListOfInt& SMESH_Mesh::GetMeshOrder() const
2327 return _mySubMeshOrder;
2330 //=============================================================================
2332 * \brief fill _mapAncestors
2334 //=============================================================================
2336 void SMESH_Mesh::fillAncestorsMap(const TopoDS_Shape& theShape)
2338 int desType, ancType;
2339 if ( !theShape.IsSame( GetShapeToMesh()) && theShape.ShapeType() == TopAbs_COMPOUND )
2341 // a geom group is added. Insert it into lists of ancestors before
2342 // the first ancestor more complex than group members
2343 TopoDS_Iterator subIt( theShape );
2344 if ( !subIt.More() ) return;
2345 int memberType = subIt.Value().ShapeType();
2346 for ( desType = TopAbs_VERTEX; desType >= memberType; desType-- )
2347 for (TopExp_Explorer des( theShape, TopAbs_ShapeEnum( desType )); des.More(); des.Next())
2349 if ( !_mapAncestors.Contains( des.Current() )) continue;// issue 0020982
2350 TopTools_ListOfShape& ancList = _mapAncestors.ChangeFromKey( des.Current() );
2351 TopTools_ListIteratorOfListOfShape ancIt (ancList);
2352 while ( ancIt.More() && ancIt.Value().ShapeType() >= memberType )
2354 if ( ancIt.More() ) ancList.InsertBefore( theShape, ancIt );
2355 else ancList.Append( theShape );
2358 else // else added for 52457: Addition of hypotheses is 8 time longer than meshing
2360 for ( desType = TopAbs_VERTEX; desType > TopAbs_COMPOUND; desType-- )
2361 for ( ancType = desType - 1; ancType >= TopAbs_COMPOUND; ancType-- )
2362 TopExp::MapShapesAndAncestors ( theShape,
2363 (TopAbs_ShapeEnum) desType,
2364 (TopAbs_ShapeEnum) ancType,
2367 // visit COMPOUNDs inside a COMPOUND that are not reachable by TopExp_Explorer
2368 if ( theShape.ShapeType() == TopAbs_COMPOUND )
2370 TopoDS_Iterator sIt(theShape);
2371 if ( sIt.More() && sIt.Value().ShapeType() == TopAbs_COMPOUND )
2372 for ( ; sIt.More(); sIt.Next() )
2373 if ( sIt.Value().ShapeType() == TopAbs_COMPOUND )
2374 fillAncestorsMap( sIt.Value() );
2378 //=============================================================================
2380 * \brief sort submeshes according to stored mesh order
2381 * \param theListToSort in out list to be sorted
2382 * \return FALSE if nothing sorted
2384 //=============================================================================
2386 bool SMESH_Mesh::SortByMeshOrder(std::vector<SMESH_subMesh*>& theListToSort) const
2388 if ( !_mySubMeshOrder.size() || theListToSort.size() < 2)
2392 vector<SMESH_subMesh*> onlyOrderedList, smVec;
2394 // collect all ordered submeshes in one list as pointers
2395 // and get their positions within theListToSort
2396 typedef vector<SMESH_subMesh*>::iterator TPosInList;
2397 map< int, TPosInList > sortedPos;
2398 TPosInList smBeg = theListToSort.begin(), smEnd = theListToSort.end();
2399 TListOfListOfInt::const_iterator listIdsIt = _mySubMeshOrder.begin();
2400 for( ; listIdsIt != _mySubMeshOrder.end(); listIdsIt++)
2402 const TListOfInt& listOfId = *listIdsIt;
2403 // convert sm ids to sm's
2405 TListOfInt::const_iterator idIt = listOfId.begin();
2406 for ( ; idIt != listOfId.end(); idIt++ )
2408 if ( SMESH_subMesh * sm = GetSubMeshContaining( *idIt ))
2410 smVec.push_back( sm );
2411 if ( sm->GetSubMeshDS() && sm->GetSubMeshDS()->IsComplexSubmesh() )
2413 SMESHDS_SubMeshIteratorPtr smdsIt = sm->GetSubMeshDS()->GetSubMeshIterator();
2414 while ( smdsIt->more() )
2416 const SMESHDS_SubMesh* smDS = smdsIt->next();
2417 if (( sm = GetSubMeshContaining( smDS->GetID() )))
2418 smVec.push_back( sm );
2423 // find smVec items in theListToSort
2424 for ( size_t i = 0; i < smVec.size(); ++i )
2426 TPosInList smPos = find( smBeg, smEnd, smVec[i] );
2427 if ( smPos != smEnd ) {
2428 onlyOrderedList.push_back( smVec[i] );
2429 sortedPos[ distance( smBeg, smPos )] = smPos;
2433 if (onlyOrderedList.size() < 2)
2437 vector<SMESH_subMesh*>::iterator onlyBIt = onlyOrderedList.begin();
2438 vector<SMESH_subMesh*>::iterator onlyEIt = onlyOrderedList.end();
2440 // iterate on ordered sub-meshes and insert them in detected positions
2441 map< int, TPosInList >::iterator i_pos = sortedPos.begin();
2442 for ( ; onlyBIt != onlyEIt; ++onlyBIt, ++i_pos )
2443 *(i_pos->second) = *onlyBIt;
2448 //================================================================================
2450 * \brief Return true if given order of sub-meshes is OK
2452 //================================================================================
2454 bool SMESH_Mesh::IsOrderOK( const SMESH_subMesh* smBefore,
2455 const SMESH_subMesh* smAfter ) const
2457 TListOfListOfInt::const_iterator listIdsIt = _mySubMeshOrder.begin();
2458 for( ; listIdsIt != _mySubMeshOrder.end(); listIdsIt++)
2460 const TListOfInt& listOfId = *listIdsIt;
2461 int iB = -1, iA = -1, i = 0;
2462 for ( TListOfInt::const_iterator id = listOfId.begin(); id != listOfId.end(); ++id, ++i )
2464 if ( *id == smBefore->GetId() )
2470 else if ( *id == smAfter->GetId() )
2478 return true; // no order imposed to given sub-meshes
2481 //=============================================================================
2483 * \brief sort submeshes according to stored mesh order
2484 * \param theListToSort in out list to be sorted
2485 * \return FALSE if nothing sorted
2487 //=============================================================================
2489 void SMESH_Mesh::getAncestorsSubMeshes (const TopoDS_Shape& theSubShape,
2490 std::vector< SMESH_subMesh* >& theSubMeshes) const
2492 theSubMeshes.clear();
2493 TopTools_ListIteratorOfListOfShape it( GetAncestors( theSubShape ));
2494 for (; it.More(); it.Next() )
2495 if ( SMESH_subMesh* sm = GetSubMeshContaining( it.Value() ))
2496 theSubMeshes.push_back(sm);
2498 // sort submeshes according to stored mesh order
2499 SortByMeshOrder( theSubMeshes );