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 OBJECT : interactive object for SMESH visualization
24 // File : SMESH_Grid.cxx
25 // Author : Nicolas REJNERI
28 #include "SMESH_ObjectDef.h"
29 #include "SMESH_ActorUtils.h"
31 #include "SMDS_Mesh.hxx"
32 #include "SMDS_PolyhedralVolumeOfNodes.hxx"
33 #include "SMESH_Actor.h"
34 #include "SMESH_ControlsDef.hxx"
35 #include "SalomeApp_Application.h"
36 #include "VTKViewer_ExtractUnstructuredGrid.h"
37 #include "VTKViewer_CellLocationsArray.h"
39 #include CORBA_SERVER_HEADER(SMESH_Gen)
40 #include CORBA_SERVER_HEADER(SALOME_Exception)
43 #include <vtkIdList.h>
44 #include <vtkCellArray.h>
45 #include <vtkUnsignedCharArray.h>
47 #include <vtkUnstructuredGrid.h>
54 #include "utilities.h"
59 #define EXCEPTION(TYPE, MSG) {\
60 std::ostringstream aStream;\
61 aStream<<__FILE__<<"["<<__LINE__<<"]::"<<MSG;\
62 throw TYPE(aStream.str());\
67 static int MYDEBUG = 1;
68 static int MYDEBUGWITHFILES = 0;//1;
70 static int MYDEBUG = 0;
71 static int MYDEBUGWITHFILES = 0;
76 Class : SMESH_VisualObjDef
77 Description : Base class for all mesh objects to be visuilised
80 //=================================================================================
81 // function : getCellType
82 // purpose : Get type of VTK cell
83 //=================================================================================
84 static inline vtkIdType getCellType( const SMDSAbs_ElementType theType,
86 const int theNbNodes )
90 case SMDSAbs_0DElement:
94 if( theNbNodes == 2 ) return VTK_LINE;
95 else if ( theNbNodes == 3 ) return VTK_QUADRATIC_EDGE;
96 else return VTK_EMPTY_CELL;
99 if (thePoly && theNbNodes>2 ) return VTK_POLYGON;
100 else if ( theNbNodes == 3 ) return VTK_TRIANGLE;
101 else if ( theNbNodes == 4 ) return VTK_QUAD;
102 else if ( theNbNodes == 6 ) return VTK_QUADRATIC_TRIANGLE;
103 else if ( theNbNodes == 8 ) return VTK_QUADRATIC_QUAD;
104 else if ( theNbNodes == 9 ) return VTK_BIQUADRATIC_QUAD;
105 else return VTK_EMPTY_CELL;
108 if (thePoly && theNbNodes>3 ) return VTK_POLYHEDRON; //VTK_CONVEX_POINT_SET;
109 else if ( theNbNodes == 4 ) return VTK_TETRA;
110 else if ( theNbNodes == 5 ) return VTK_PYRAMID;
111 else if ( theNbNodes == 6 ) return VTK_WEDGE;
112 else if ( theNbNodes == 8 ) return VTK_HEXAHEDRON;
113 else if ( theNbNodes == 12 ) return VTK_HEXAGONAL_PRISM;
114 else if ( theNbNodes == 10 ) return VTK_QUADRATIC_TETRA;
115 else if ( theNbNodes == 20 ) return VTK_QUADRATIC_HEXAHEDRON;
116 else if ( theNbNodes == 27 ) return VTK_TRIQUADRATIC_HEXAHEDRON;
117 else if ( theNbNodes == 15 ) return VTK_QUADRATIC_WEDGE;
118 else if ( theNbNodes == 13 ) return VTK_QUADRATIC_PYRAMID; //VTK_CONVEX_POINT_SET;
119 else return VTK_EMPTY_CELL;
121 default: return VTK_EMPTY_CELL;
125 //=================================================================================
126 // functions : SMESH_VisualObjDef
127 // purpose : Constructor
128 //=================================================================================
129 SMESH_VisualObjDef::SMESH_VisualObjDef()
131 MESSAGE("---------------------------------------------SMESH_VisualObjDef::SMESH_VisualObjDef");
132 myGrid = vtkUnstructuredGrid::New();
134 ClearEntitiesFlags();
135 SMESH::GetEntitiesFromObject(NULL);
137 SMESH_VisualObjDef::~SMESH_VisualObjDef()
139 MESSAGE("---------------------------------------------SMESH_VisualObjDef::~SMESH_VisualObjDef");
141 MESSAGE( "~SMESH_MeshObj - myGrid->GetReferenceCount() = " << myGrid->GetReferenceCount() );
145 //=================================================================================
146 // functions : GetNodeObjId, GetNodeVTKId, GetElemObjId, GetElemVTKId
147 // purpose : Methods for retrieving VTK IDs by SMDS IDs and vice versa
148 //=================================================================================
149 vtkIdType SMESH_VisualObjDef::GetNodeObjId( int theVTKID )
153 TMapOfIds::const_iterator i = myVTK2SMDSNodes.find(theVTKID);
154 return i == myVTK2SMDSNodes.end() ? -1 : i->second;
156 return this->GetMesh()->FindNodeVtk(theVTKID)->GetID();
159 vtkIdType SMESH_VisualObjDef::GetNodeVTKId( int theObjID )
163 TMapOfIds::const_iterator i = mySMDS2VTKNodes.find(theObjID);
164 return i == mySMDS2VTKNodes.end() ? -1 : i->second;
167 const SMDS_MeshNode* aNode = 0;
168 if( this->GetMesh() ) {
169 aNode = this->GetMesh()->FindNode(theObjID);
171 return aNode ? aNode->getVtkId() : -1;
174 vtkIdType SMESH_VisualObjDef::GetElemObjId( int theVTKID )
178 TMapOfIds::const_iterator i = myVTK2SMDSElems.find(theVTKID);
179 return i == myVTK2SMDSElems.end() ? -1 : i->second;
181 return this->GetMesh()->fromVtkToSmds(theVTKID);
184 vtkIdType SMESH_VisualObjDef::GetElemVTKId( int theObjID )
188 TMapOfIds::const_iterator i = mySMDS2VTKElems.find(theObjID);
189 return i == mySMDS2VTKElems.end() ? -1 : i->second;
191 return this->GetMesh()->FindElement(theObjID)->getVtkId();
192 //return this->GetMesh()->fromSmdsToVtk(theObjID);
195 //=================================================================================
196 // function : SMESH_VisualObjDef::createPoints
197 // purpose : Create points from nodes
198 //=================================================================================
199 /*! fills a vtkPoints structure for a submesh.
200 * fills a std::list of SMDS_MeshElements*, then extract the points.
201 * fills also conversion id maps between SMDS and VTK.
203 void SMESH_VisualObjDef::createPoints( vtkPoints* thePoints )
205 if ( thePoints == 0 )
209 vtkIdType nbNodes = GetEntities( SMDSAbs_Node, aNodes );
210 thePoints->SetNumberOfPoints( nbNodes );
214 TEntityList::const_iterator anIter;
215 for ( anIter = aNodes.begin(); anIter != aNodes.end(); ++anIter )
217 const SMDS_MeshNode* aNode = ( const SMDS_MeshNode* )(*anIter);
220 thePoints->SetPoint( nbPoints, aNode->X(), aNode->Y(), aNode->Z() );
221 int anId = aNode->GetID();
222 mySMDS2VTKNodes.insert( TMapOfIds::value_type( anId, nbPoints ) );
223 myVTK2SMDSNodes.insert( TMapOfIds::value_type( nbPoints, anId ) );
228 if ( nbPoints != nbNodes )
229 thePoints->SetNumberOfPoints( nbPoints );
232 //=================================================================================
233 // function : buildPrs
234 // purpose : create VTK cells( fill unstructured grid )
235 //=================================================================================
236 void SMESH_VisualObjDef::buildPrs(bool buildGrid)
238 MESSAGE("----------------------------------------------------------SMESH_VisualObjDef::buildPrs " << buildGrid);
244 mySMDS2VTKNodes.clear();
245 myVTK2SMDSNodes.clear();
246 mySMDS2VTKElems.clear();
247 myVTK2SMDSElems.clear();
256 mySMDS2VTKNodes.clear();
257 myVTK2SMDSNodes.clear();
258 mySMDS2VTKElems.clear();
259 myVTK2SMDSElems.clear();
261 myGrid->SetPoints( 0 );
262 myGrid->SetCells( 0, 0, 0, 0, 0 );
269 if (!GetMesh()->isCompacted())
271 MESSAGE("*** buildPrs ==> compactMesh!");
272 GetMesh()->compactMesh();
274 vtkUnstructuredGrid *theGrid = GetMesh()->getGrid();
275 updateEntitiesFlags();
276 myGrid->ShallowCopy(theGrid);
277 //MESSAGE(myGrid->GetReferenceCount());
278 //MESSAGE( "Update - myGrid->GetNumberOfCells() = "<<myGrid->GetNumberOfCells() );
279 //MESSAGE( "Update - myGrid->GetNumberOfPoints() = "<<myGrid->GetNumberOfPoints() );
280 if( MYDEBUGWITHFILES ) SMESH::WriteUnstructuredGrid( myGrid,"buildPrs.vtu" );
284 //=================================================================================
285 // function : buildNodePrs
286 // purpose : create VTK cells for nodes
287 //=================================================================================
289 void SMESH_VisualObjDef::buildNodePrs()
291 // PAL16631: without swap, bad_alloc is not thrown but hung up and crash instead,
292 // so check remaining memory size for safety
293 SMDS_Mesh::CheckMemory(); // PAL16631
294 vtkPoints* aPoints = vtkPoints::New();
295 createPoints( aPoints );
296 SMDS_Mesh::CheckMemory();
297 myGrid->SetPoints( aPoints );
300 myGrid->SetCells( 0, 0, 0, 0, 0 );
303 //=================================================================================
304 // function : buildElemPrs
305 // purpose : Create VTK cells for elements
306 //=================================================================================
309 typedef std::vector<const SMDS_MeshElement*> TConnect;
311 int GetConnect(const SMDS_ElemIteratorPtr& theNodesIter,
312 TConnect& theConnect)
315 for(; theNodesIter->more();)
316 theConnect.push_back(theNodesIter->next());
317 return theConnect.size();
321 void SetId(vtkIdList *theIdList,
322 const SMESH_VisualObjDef::TMapOfIds& theSMDS2VTKNodes,
323 const TConnect& theConnect,
327 theIdList->SetId(thePosition,theSMDS2VTKNodes.find(theConnect[theId]->GetID())->second);
333 void SMESH_VisualObjDef::buildElemPrs()
337 vtkPoints* aPoints = vtkPoints::New();
338 createPoints( aPoints );
339 myGrid->SetPoints( aPoints );
343 MESSAGE("Update - myGrid->GetNumberOfPoints() = "<<myGrid->GetNumberOfPoints());
345 // Calculate cells size
347 static SMDSAbs_ElementType aTypes[ 4 ] =
348 { SMDSAbs_0DElement, SMDSAbs_Edge, SMDSAbs_Face, SMDSAbs_Volume };
351 map<SMDSAbs_ElementType,int> nbEnts;
352 map<SMDSAbs_ElementType,TEntityList> anEnts;
354 for ( int i = 0; i <= 3; i++ )
355 nbEnts[ aTypes[ i ] ] = GetEntities( aTypes[ i ], anEnts[ aTypes[ i ] ] );
357 // PAL16631: without swap, bad_alloc is not thrown but hung up and crash instead,
358 // so check remaining memory size for safety
359 SMDS_Mesh::CheckMemory(); // PAL16631
361 vtkIdType aCellsSize = 2 * nbEnts[ SMDSAbs_0DElement ] + 3 * nbEnts[ SMDSAbs_Edge ];
363 for ( int i = 2; i <= 3; i++ ) // iterate through faces and volumes
365 if ( nbEnts[ aTypes[ i ] ] )
367 const TEntityList& aList = anEnts[ aTypes[ i ] ];
368 TEntityList::const_iterator anIter;
369 for ( anIter = aList.begin(); anIter != aList.end(); ++anIter ) {
370 if((*anIter)->GetEntityType() != SMDSEntity_Polyhedra &&
371 (*anIter)->GetEntityType() != SMDSEntity_Quad_Polyhedra) {
372 aCellsSize += (*anIter)->NbNodes() + 1;
374 // Special case for the VTK_POLYHEDRON:
375 // itsinput cellArray is of special format.
376 // [nCellFaces, nFace0Pts, i, j, k, nFace1Pts, i, j, k, ...]
378 if( const SMDS_VtkVolume* ph = dynamic_cast<const SMDS_VtkVolume*>(*anIter) ) {
379 int nbFaces = ph->NbFaces();
380 aCellsSize += (1 + ph->NbFaces());
381 for( int i = 1; i <= nbFaces; i++ ) {
382 aCellsSize += ph->NbFaceNodes(i);
390 vtkIdType aNbCells = nbEnts[ SMDSAbs_0DElement ] + nbEnts[ SMDSAbs_Edge ] +
391 nbEnts[ SMDSAbs_Face ] + nbEnts[ SMDSAbs_Volume ];
394 MESSAGE( "Update - aNbCells = "<<aNbCells<<"; aCellsSize = "<<aCellsSize );
398 vtkCellArray* aConnectivity = vtkCellArray::New();
399 aConnectivity->Allocate( aCellsSize, 0 );
401 SMDS_Mesh::CheckMemory(); // PAL16631
403 vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
404 aCellTypesArray->SetNumberOfComponents( 1 );
405 aCellTypesArray->Allocate( aNbCells * aCellTypesArray->GetNumberOfComponents() );
407 SMDS_Mesh::CheckMemory(); // PAL16631
409 vtkIdList *anIdList = vtkIdList::New();
413 aConnect.reserve(VTK_CELL_SIZE);
415 SMDS_Mesh::CheckMemory(); // PAL16631
417 for ( int i = 0; i <= 3; i++ ) // iterate through 0d elements, edges, faces and volumes
419 if ( nbEnts[ aTypes[ i ] ] > 0 ) {
421 const SMDSAbs_ElementType& aType = aTypes[ i ];
422 const TEntityList& aList = anEnts[ aType ];
423 TEntityList::const_iterator anIter;
424 for ( anIter = aList.begin(); anIter != aList.end(); ++anIter )
426 const SMDS_MeshElement* anElem = *anIter;
428 vtkIdType aNbNodes = anElem->NbNodes();
429 anIdList->SetNumberOfIds( aNbNodes );
430 const vtkIdType vtkElemType = getCellType( aType, anElem->IsPoly(), aNbNodes );
432 int anId = anElem->GetID();
434 mySMDS2VTKElems.insert( TMapOfIds::value_type( anId, iElem ) );
435 myVTK2SMDSElems.insert( TMapOfIds::value_type( iElem, anId ) );
437 SMDS_ElemIteratorPtr aNodesIter = anElem->nodesIterator();
439 // Convertions connectivities from SMDS to VTK
441 if (aType == SMDSAbs_Volume && anElem->IsPoly() && aNbNodes > 3) { // POLYEDRE
443 if ( const SMDS_VtkVolume* ph = dynamic_cast<const SMDS_VtkVolume*>(anElem) ) {
444 int nbFaces = ph->NbFaces();
445 anIdList->InsertNextId(nbFaces);
446 for( int i = 1; i <= nbFaces; i++ ) {
447 anIdList->InsertNextId(ph->NbFaceNodes(i));
448 for(int j = 1; j <= ph->NbFaceNodes(i); j++) {
449 const SMDS_MeshNode* n = ph->GetFaceNode(i,j);
451 anIdList->InsertNextId(mySMDS2VTKNodes[n->GetID()]);
458 const std::vector<int>& aConnectivities =
459 SMDS_MeshCell::toVtkOrder( VTKCellType( vtkElemType ));
460 if (aConnectivities.size() > 0) {
462 GetConnect(aNodesIter,aConnect);
463 for (vtkIdType aNodeId = 0; aNodeId < aNbNodes; aNodeId++)
464 SetId(anIdList,mySMDS2VTKNodes,aConnect,aNodeId,aConnectivities[aNodeId]);
467 for( vtkIdType aNodeId = 0; aNodesIter->more(); aNodeId++ ){
468 const SMDS_MeshElement* aNode = aNodesIter->next();
469 anIdList->SetId( aNodeId, mySMDS2VTKNodes[aNode->GetID()] );
474 aConnectivity->InsertNextCell( anIdList );
475 aCellTypesArray->InsertNextValue( vtkElemType );
480 SMDS_Mesh::CheckMemory(); // PAL16631
483 // Insert cells in grid
485 VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
486 aCellLocationsArray->SetNumberOfComponents( 1 );
487 aCellLocationsArray->SetNumberOfTuples( aNbCells );
489 SMDS_Mesh::CheckMemory(); // PAL16631
491 aConnectivity->InitTraversal();
492 for( vtkIdType idType = 0, *pts, npts; aConnectivity->GetNextCell( npts, pts ); idType++ )
493 aCellLocationsArray->SetValue( idType, aConnectivity->GetTraversalLocation( npts ) );
495 myGrid->SetCells( aCellTypesArray, aCellLocationsArray,aConnectivity );
497 aCellLocationsArray->Delete();
498 aCellTypesArray->Delete();
499 aConnectivity->Delete();
502 SMDS_Mesh::CheckMemory(); // PAL16631
505 //=================================================================================
506 // function : GetEdgeNodes
507 // purpose : Retrieve ids of nodes from edge of elements ( edge is numbered from 1 )
508 //=================================================================================
509 bool SMESH_VisualObjDef::GetEdgeNodes( const int theElemId,
510 const int theEdgeNum,
512 int& theNodeId2 ) const
514 const SMDS_Mesh* aMesh = GetMesh();
518 const SMDS_MeshElement* anElem = aMesh->FindElement( theElemId );
522 int nbNodes = anElem->NbNodes();
524 if ( theEdgeNum < 0 || theEdgeNum > 3 || (nbNodes != 3 && nbNodes != 4) || theEdgeNum > nbNodes )
527 vector<int> anIds( nbNodes );
528 SMDS_ElemIteratorPtr anIter = anElem->nodesIterator();
530 while( anIter->more() )
531 anIds[ i++ ] = anIter->next()->GetID();
533 if ( theEdgeNum < nbNodes - 1 )
535 theNodeId1 = anIds[ theEdgeNum ];
536 theNodeId2 = anIds[ theEdgeNum + 1 ];
540 theNodeId1 = anIds[ nbNodes - 1 ];
541 theNodeId2 = anIds[ 0 ];
547 vtkUnstructuredGrid* SMESH_VisualObjDef::GetUnstructuredGrid()
549 if ( !myLocalGrid && !GetMesh()->isCompacted() )
551 GetMesh()->compactMesh();
552 updateEntitiesFlags();
553 vtkUnstructuredGrid *theGrid = GetMesh()->getGrid();
554 myGrid->ShallowCopy(theGrid);
560 //=================================================================================
561 // function : IsValid
562 // purpose : Return true if there are some entities
563 //=================================================================================
564 bool SMESH_VisualObjDef::IsValid() const
566 //MESSAGE("SMESH_VisualObjDef::IsValid");
567 return GetNbEntities(SMDSAbs_Node) > 0 ||
568 GetNbEntities(SMDSAbs_0DElement) > 0 ||
569 GetNbEntities(SMDSAbs_Edge) > 0 ||
570 GetNbEntities(SMDSAbs_Face) > 0 ||
571 GetNbEntities(SMDSAbs_Volume) > 0 ;
574 //=================================================================================
575 // function : updateEntitiesFlags
576 // purpose : Update entities flags
577 //=================================================================================
578 void SMESH_VisualObjDef::updateEntitiesFlags() {
580 unsigned int tmp = myEntitiesState;
581 ClearEntitiesFlags();
583 map<SMDSAbs_ElementType,int> entities = SMESH::GetEntitiesFromObject(this);
586 if( myEntitiesCache[SMDSAbs_0DElement] != 0 || myEntitiesCache[SMDSAbs_0DElement] >= entities[SMDSAbs_0DElement] )
587 myEntitiesState &= ~SMESH_Actor::e0DElements;
589 if( myEntitiesCache[SMDSAbs_Edge] != 0 || myEntitiesCache[SMDSAbs_Edge] >= entities[SMDSAbs_Edge] )
590 myEntitiesState &= ~SMESH_Actor::eEdges;
592 if( myEntitiesCache[SMDSAbs_Face] != 0 || myEntitiesCache[SMDSAbs_Face] >= entities[SMDSAbs_Face] )
593 myEntitiesState &= ~SMESH_Actor::eFaces;
595 if( myEntitiesCache[SMDSAbs_Volume] != 0 || myEntitiesCache[SMDSAbs_Volume] >= entities[SMDSAbs_Volume] )
596 myEntitiesState &= ~SMESH_Actor::eVolumes;
598 if( tmp != myEntitiesState ) {
599 myEntitiesFlag = true;
602 myEntitiesCache = entities;
605 //=================================================================================
606 // function : ClearEntitiesFlags
607 // purpose : Clear the entities flags
608 //=================================================================================
609 void SMESH_VisualObjDef::ClearEntitiesFlags() {
610 myEntitiesState = SMESH_Actor::eAllEntity;
611 myEntitiesFlag = false;
614 //=================================================================================
615 // function : GetEntitiesFlag
616 // purpose : Return the entities flag
617 //=================================================================================
618 bool SMESH_VisualObjDef::GetEntitiesFlag() {
619 return myEntitiesFlag;
622 //=================================================================================
623 // function : GetEntitiesState
624 // purpose : Return the entities state
625 //=================================================================================
626 unsigned int SMESH_VisualObjDef::GetEntitiesState() {
627 return myEntitiesState;
631 Class : SMESH_MeshObj
632 Description : Class for visualisation of mesh
635 //=================================================================================
636 // function : SMESH_MeshObj
637 // purpose : Constructor
638 //=================================================================================
639 SMESH_MeshObj::SMESH_MeshObj(SMESH::SMESH_Mesh_ptr theMesh):
640 myClient(SalomeApp_Application::orb(),theMesh)
644 MESSAGE("SMESH_MeshObj - this = "<<this<<"; theMesh->_is_nil() = "<<theMesh->_is_nil());
647 //=================================================================================
648 // function : ~SMESH_MeshObj
649 // purpose : Destructor
650 //=================================================================================
651 SMESH_MeshObj::~SMESH_MeshObj()
654 MESSAGE("SMESH_MeshObj - this = "<<this<<"\n");
657 //=================================================================================
659 // purpose : Update mesh and fill grid with new values if necessary
660 //=================================================================================
661 bool SMESH_MeshObj::Update( int theIsClear )
663 // Update SMDS_Mesh on client part
664 MESSAGE("SMESH_MeshObj::Update " << this);
665 if ( myClient.Update(theIsClear) || GetUnstructuredGrid()->GetNumberOfPoints()==0) {
667 buildPrs(); // Fill unstructured grid
673 bool SMESH_MeshObj::NulData()
675 MESSAGE ("SMESH_MeshObj::NulData() ==================================================================================");
678 myEmptyGrid = SMDS_UnstructuredGrid::New();
679 myEmptyGrid->Initialize();
680 myEmptyGrid->Allocate();
681 vtkPoints* points = vtkPoints::New();
682 points->SetNumberOfPoints(0);
683 myEmptyGrid->SetPoints( points );
685 myEmptyGrid->BuildLinks();
687 myGrid->ShallowCopy(myEmptyGrid);
690 //=================================================================================
691 // function : GetElemDimension
692 // purpose : Get dimension of element
693 //=================================================================================
694 int SMESH_MeshObj::GetElemDimension( const int theObjId )
696 const SMDS_MeshElement* anElem = myClient->FindElement( theObjId );
700 int aType = anElem->GetType();
703 case SMDSAbs_0DElement : return 0;
704 case SMDSAbs_Edge : return 1;
705 case SMDSAbs_Face : return 2;
706 case SMDSAbs_Volume: return 3;
711 //=================================================================================
712 // function : GetEntities
713 // purpose : Get entities of specified type. Return number of entities
714 //=================================================================================
715 int SMESH_MeshObj::GetNbEntities( const SMDSAbs_ElementType theType) const
721 return myClient->NbNodes();
724 case SMDSAbs_0DElement:
726 return myClient->Nb0DElements();
731 return myClient->NbEdges();
736 return myClient->NbFaces();
741 return myClient->NbVolumes();
750 int SMESH_MeshObj::GetEntities( const SMDSAbs_ElementType theType, TEntityList& theObjs ) const
758 SMDS_NodeIteratorPtr anIter = myClient->nodesIterator();
759 while ( anIter->more() ) theObjs.push_back( anIter->next() );
762 case SMDSAbs_0DElement:
764 SMDS_0DElementIteratorPtr anIter = myClient->elements0dIterator();
765 while ( anIter->more() ) theObjs.push_back( anIter->next() );
770 SMDS_EdgeIteratorPtr anIter = myClient->edgesIterator();
771 while ( anIter->more() ) theObjs.push_back( anIter->next() );
776 SMDS_FaceIteratorPtr anIter = myClient->facesIterator();
777 while ( anIter->more() ) theObjs.push_back( anIter->next() );
782 SMDS_VolumeIteratorPtr anIter = myClient->volumesIterator();
783 while ( anIter->more() ) theObjs.push_back( anIter->next() );
790 return theObjs.size();
793 //=================================================================================
794 // function : UpdateFunctor
795 // purpose : Update functor in accordance with current mesh
796 //=================================================================================
797 void SMESH_MeshObj::UpdateFunctor( const SMESH::Controls::FunctorPtr& theFunctor )
799 theFunctor->SetMesh( GetMesh() );
802 //=================================================================================
803 // function : IsNodePrs
804 // purpose : Return true if node presentation is used
805 //=================================================================================
806 bool SMESH_MeshObj::IsNodePrs() const
808 return myClient->Nb0DElements() == 0 && myClient->NbEdges() == 0 && myClient->NbFaces() == 0 && myClient->NbVolumes() == 0 ;
813 Class : SMESH_SubMeshObj
814 Description : Base class for visualisation of submeshes and groups
817 //=================================================================================
818 // function : SMESH_SubMeshObj
819 // purpose : Constructor
820 //=================================================================================
821 SMESH_SubMeshObj::SMESH_SubMeshObj( SMESH_MeshObj* theMeshObj )
823 if ( MYDEBUG ) MESSAGE( "SMESH_SubMeshObj - theMeshObj = " << theMeshObj );
825 myMeshObj = theMeshObj;
828 SMESH_SubMeshObj::~SMESH_SubMeshObj()
832 //=================================================================================
833 // function : GetElemDimension
834 // purpose : Get dimension of element
835 //=================================================================================
836 int SMESH_SubMeshObj::GetElemDimension( const int theObjId )
838 return myMeshObj == 0 ? 0 : myMeshObj->GetElemDimension( theObjId );
841 //=================================================================================
842 // function : UpdateFunctor
843 // purpose : Update functor in accordance with current mesh
844 //=================================================================================
846 void SMESH_SubMeshObj::UpdateFunctor( const SMESH::Controls::FunctorPtr& theFunctor )
848 theFunctor->SetMesh( myMeshObj->GetMesh() );
851 //=================================================================================
853 // purpose : Update mesh object and fill grid with new values
854 //=================================================================================
855 bool SMESH_SubMeshObj::Update( int theIsClear )
857 MESSAGE("SMESH_SubMeshObj::Update " << this)
858 bool changed = myMeshObj->Update( theIsClear );
865 Class : SMESH_GroupObj
866 Description : Class for visualisation of groups
869 //=================================================================================
870 // function : SMESH_GroupObj
871 // purpose : Constructor
872 //=================================================================================
873 SMESH_GroupObj::SMESH_GroupObj( SMESH::SMESH_GroupBase_ptr theGroup,
874 SMESH_MeshObj* theMeshObj )
875 : SMESH_SubMeshObj( theMeshObj ),
876 myGroupServer( SMESH::SMESH_GroupBase::_duplicate(theGroup) )
878 if ( MYDEBUG ) MESSAGE("SMESH_GroupObj - theGroup->_is_nil() = "<<theGroup->_is_nil());
879 myGroupServer->Register();
882 SMESH_GroupObj::~SMESH_GroupObj()
884 if ( MYDEBUG ) MESSAGE("~SMESH_GroupObj");
885 myGroupServer->UnRegister();
888 //=================================================================================
889 // function : IsNodePrs
890 // purpose : Return true if node presentation is used
891 //=================================================================================
892 bool SMESH_GroupObj::IsNodePrs() const
894 return myGroupServer->GetType() == SMESH::NODE;
897 //=================================================================================
898 // function : GetElementType
899 // purpose : Return type of elements of group
900 //=================================================================================
901 SMDSAbs_ElementType SMESH_GroupObj::GetElementType() const
903 return SMDSAbs_ElementType(myGroupServer->GetType());
906 //=================================================================================
907 // function : getNodesFromElems
908 // purpose : Retrieve nodes from elements
909 //=================================================================================
910 static int getNodesFromElems( SMESH::long_array_var& theElemIds,
911 const SMDS_Mesh* theMesh,
912 std::list<const SMDS_MeshElement*>& theResList )
914 set<const SMDS_MeshElement*> aNodeSet;
916 for ( CORBA::Long i = 0, n = theElemIds->length(); i < n; i++ )
918 const SMDS_MeshElement* anElem = theMesh->FindElement( theElemIds[ i ] );
921 SMDS_ElemIteratorPtr anIter = anElem->nodesIterator();
922 while ( anIter->more() )
924 const SMDS_MeshElement* aNode = anIter->next();
926 aNodeSet.insert( aNode );
931 set<const SMDS_MeshElement*>::const_iterator anIter;
932 for ( anIter = aNodeSet.begin(); anIter != aNodeSet.end(); ++anIter )
933 theResList.push_back( *anIter );
935 return theResList.size();
938 //=================================================================================
939 // function : getPointers
940 // purpose : Get std::list<const SMDS_MeshElement*> from list of IDs
941 //=================================================================================
942 static int getPointers( const SMDSAbs_ElementType theRequestType,
943 SMESH::long_array_var& theElemIds,
944 const SMDS_Mesh* theMesh,
945 std::list<const SMDS_MeshElement*>& theResList )
947 for ( CORBA::Long i = 0, n = theElemIds->length(); i < n; i++ )
949 const SMDS_MeshElement* anElem = theRequestType == SMDSAbs_Node
950 ? theMesh->FindNode( theElemIds[ i ] ) : theMesh->FindElement( theElemIds[ i ] );
953 theResList.push_back( anElem );
956 return theResList.size();
960 //=================================================================================
961 // function : GetEntities
962 // purpose : Get entities of specified type. Return number of entities
963 //=================================================================================
964 int SMESH_GroupObj::GetNbEntities( const SMDSAbs_ElementType theType) const
966 if(SMDSAbs_ElementType(myGroupServer->GetType()) == theType) {
967 return myGroupServer->Size();
969 if ( theType == SMDSAbs_Node ) {
970 return myGroupServer->GetNumberOfNodes();
975 int SMESH_GroupObj::GetEntities( const SMDSAbs_ElementType theType, TEntityList& theResList ) const
978 SMDS_Mesh* aMesh = myMeshObj->GetMesh();
980 if ( myGroupServer->Size() == 0 || aMesh == 0 )
983 SMDSAbs_ElementType aGrpType = SMDSAbs_ElementType(myGroupServer->GetType());
984 SMESH::long_array_var anIds = myGroupServer->GetListOfID();
986 if ( aGrpType == theType )
987 return getPointers( theType, anIds, aMesh, theResList );
988 else if ( theType == SMDSAbs_Node )
989 return getNodesFromElems( anIds, aMesh, theResList );
997 Class : SMESH_subMeshObj
998 Description : Class for visualisation of submeshes
1001 //=================================================================================
1002 // function : SMESH_subMeshObj
1003 // purpose : Constructor
1004 //=================================================================================
1005 SMESH_subMeshObj::SMESH_subMeshObj( SMESH::SMESH_subMesh_ptr theSubMesh,
1006 SMESH_MeshObj* theMeshObj )
1007 : SMESH_SubMeshObj( theMeshObj ),
1008 mySubMeshServer( SMESH::SMESH_subMesh::_duplicate( theSubMesh ) )
1010 if ( MYDEBUG ) MESSAGE( "SMESH_subMeshObj - theSubMesh->_is_nil() = " << theSubMesh->_is_nil() );
1012 mySubMeshServer->Register();
1015 SMESH_subMeshObj::~SMESH_subMeshObj()
1017 if ( MYDEBUG ) MESSAGE( "~SMESH_subMeshObj" );
1018 mySubMeshServer->UnRegister();
1021 //=================================================================================
1022 // function : GetEntities
1023 // purpose : Get entities of specified type. Return number of entities
1024 //=================================================================================
1025 int SMESH_subMeshObj::GetNbEntities( const SMDSAbs_ElementType theType) const
1031 return mySubMeshServer->GetNumberOfNodes( /*all=*/true );
1034 case SMDSAbs_0DElement:
1037 case SMDSAbs_Volume:
1039 SMESH::long_array_var anIds =
1040 mySubMeshServer->GetElementsByType( SMESH::ElementType(theType) );
1041 return anIds->length();
1049 int SMESH_subMeshObj::GetEntities( const SMDSAbs_ElementType theType, TEntityList& theResList ) const
1053 SMDS_Mesh* aMesh = myMeshObj->GetMesh();
1057 bool isNodal = IsNodePrs();
1061 if ( theType == SMDSAbs_Node )
1063 SMESH::long_array_var anIds = mySubMeshServer->GetNodesId();
1064 return getPointers( SMDSAbs_Node, anIds, aMesh, theResList );
1069 if ( theType == SMDSAbs_Node )
1071 SMESH::long_array_var anIds = mySubMeshServer->GetElementsId();
1072 return getNodesFromElems( anIds, aMesh, theResList );
1076 SMESH::long_array_var anIds =
1077 mySubMeshServer->GetElementsByType( SMESH::ElementType(theType) );
1078 return getPointers( theType, anIds, aMesh, theResList );
1085 //=================================================================================
1086 // function : IsNodePrs
1087 // purpose : Return true if node presentation is used
1088 //=================================================================================
1089 bool SMESH_subMeshObj::IsNodePrs() const
1091 return mySubMeshServer->GetNumberOfElements() == 0;