1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // SMESH 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_BallElement.hxx"
32 #include "SMDS_Mesh.hxx"
33 #include "SMDS_MeshCell.hxx"
34 #include "SMDS_PolyhedralVolumeOfNodes.hxx"
35 #include "SMESH_Actor.h"
36 #include "SMESH_ControlsDef.hxx"
38 #include <SalomeApp_Application.h>
39 #include <VTKViewer_ExtractUnstructuredGrid.h>
40 #include <VTKViewer_CellLocationsArray.h>
42 #include CORBA_SERVER_HEADER(SMESH_Gen)
43 #include CORBA_SERVER_HEADER(SALOME_Exception)
46 #include <vtkIdList.h>
47 #include <vtkCellArray.h>
48 #include <vtkUnsignedCharArray.h>
49 #include <vtkCellData.h>
50 #include <vtkUnstructuredGrid.h>
57 #include "utilities.h"
62 #define EXCEPTION(TYPE, MSG) {\
63 std::ostringstream aStream;\
64 aStream<<__FILE__<<"["<<__LINE__<<"]::"<<MSG;\
65 throw TYPE(aStream.str());\
70 static int MYDEBUG = 1;
71 static int MYDEBUGWITHFILES = 0;//1;
73 static int MYDEBUG = 0;
74 static int MYDEBUGWITHFILES = 0;
79 Class : SMESH_VisualObjDef
80 Description : Base class for all mesh objects to be visuilised
83 //=================================================================================
84 // function : getCellType
85 // purpose : Get type of VTK cell
86 //=================================================================================
87 // static inline vtkIdType getCellType( const SMDSAbs_ElementType theType,
88 // const bool thePoly,
89 // const int theNbNodes )
93 // case SMDSAbs_0DElement: return VTK_VERTEX;
95 // case SMDSAbs_Ball: return VTK_POLY_VERTEX;
98 // if( theNbNodes == 2 ) return VTK_LINE;
99 // else if ( theNbNodes == 3 ) return VTK_QUADRATIC_EDGE;
100 // else return VTK_EMPTY_CELL;
102 // case SMDSAbs_Face :
103 // if (thePoly && theNbNodes>2 ) return VTK_POLYGON;
104 // else if ( theNbNodes == 3 ) return VTK_TRIANGLE;
105 // else if ( theNbNodes == 4 ) return VTK_QUAD;
106 // else if ( theNbNodes == 6 ) return VTK_QUADRATIC_TRIANGLE;
107 // else if ( theNbNodes == 8 ) return VTK_QUADRATIC_QUAD;
108 // else if ( theNbNodes == 9 ) return VTK_BIQUADRATIC_QUAD;
109 // else if ( theNbNodes == 7 ) return VTK_BIQUADRATIC_TRIANGLE;
110 // else return VTK_EMPTY_CELL;
112 // case SMDSAbs_Volume:
113 // if (thePoly && theNbNodes>3 ) return VTK_POLYHEDRON; //VTK_CONVEX_POINT_SET;
114 // else if ( theNbNodes == 4 ) return VTK_TETRA;
115 // else if ( theNbNodes == 5 ) return VTK_PYRAMID;
116 // else if ( theNbNodes == 6 ) return VTK_WEDGE;
117 // else if ( theNbNodes == 8 ) return VTK_HEXAHEDRON;
118 // else if ( theNbNodes == 12 ) return VTK_HEXAGONAL_PRISM;
119 // else if ( theNbNodes == 10 ) return VTK_QUADRATIC_TETRA;
120 // else if ( theNbNodes == 20 ) return VTK_QUADRATIC_HEXAHEDRON;
121 // else if ( theNbNodes == 27 ) return VTK_TRIQUADRATIC_HEXAHEDRON;
122 // else if ( theNbNodes == 15 ) return VTK_QUADRATIC_WEDGE;
123 // else if ( theNbNodes == 13 ) return VTK_QUADRATIC_PYRAMID; //VTK_CONVEX_POINT_SET;
124 // else return VTK_EMPTY_CELL;
126 // default: return VTK_EMPTY_CELL;
130 //=================================================================================
131 // functions : SMESH_VisualObjDef
132 // purpose : Constructor
133 //=================================================================================
134 SMESH_VisualObjDef::SMESH_VisualObjDef()
136 MESSAGE("---------------------------------------------SMESH_VisualObjDef::SMESH_VisualObjDef");
137 myGrid = vtkUnstructuredGrid::New();
139 ClearEntitiesFlags();
140 SMESH::GetEntitiesFromObject(NULL);
142 SMESH_VisualObjDef::~SMESH_VisualObjDef()
144 MESSAGE("---------------------------------------------SMESH_VisualObjDef::~SMESH_VisualObjDef");
146 MESSAGE( "~SMESH_MeshObj - myGrid->GetReferenceCount() = " << myGrid->GetReferenceCount() );
150 //=================================================================================
151 // functions : GetNodeObjId, GetNodeVTKId, GetElemObjId, GetElemVTKId
152 // purpose : Methods for retrieving VTK IDs by SMDS IDs and vice versa
153 //=================================================================================
154 vtkIdType SMESH_VisualObjDef::GetNodeObjId( int theVTKID )
158 TMapOfIds::const_iterator i = myVTK2SMDSNodes.find(theVTKID);
159 return i == myVTK2SMDSNodes.end() ? -1 : i->second;
161 const SMDS_MeshNode* aNode = 0;
162 if( this->GetMesh() )
163 aNode = this->GetMesh()->FindNodeVtk( theVTKID );
165 return aNode ? aNode->GetID() : -1;
168 vtkIdType SMESH_VisualObjDef::GetNodeVTKId( int theObjID )
172 TMapOfIds::const_iterator i = mySMDS2VTKNodes.find(theObjID);
173 return i == mySMDS2VTKNodes.end() ? -1 : i->second;
176 const SMDS_MeshNode* aNode = 0;
177 if( this->GetMesh() ) {
178 aNode = this->GetMesh()->FindNode(theObjID);
180 return aNode ? aNode->getVtkId() : -1;
183 vtkIdType SMESH_VisualObjDef::GetElemObjId( int theVTKID )
187 TMapOfIds::const_iterator i = myVTK2SMDSElems.find(theVTKID);
188 return i == myVTK2SMDSElems.end() ? -1 : i->second;
190 return this->GetMesh()->fromVtkToSmds(theVTKID);
193 vtkIdType SMESH_VisualObjDef::GetElemVTKId( int theObjID )
197 TMapOfIds::const_iterator i = mySMDS2VTKElems.find(theObjID);
198 return i == mySMDS2VTKElems.end() ? -1 : i->second;
201 const SMDS_MeshElement* e = 0;
202 if ( this->GetMesh() )
203 e = this->GetMesh()->FindElement(theObjID);
205 return e ? e->getVtkId() : -1;
208 //=================================================================================
209 // function : SMESH_VisualObjDef::createPoints
210 // purpose : Create points from nodes
211 //=================================================================================
212 /*! fills a vtkPoints structure for a submesh.
213 * fills a std::list of SMDS_MeshElements*, then extract the points.
214 * fills also conversion id maps between SMDS and VTK.
216 void SMESH_VisualObjDef::createPoints( vtkPoints* thePoints )
218 if ( thePoints == 0 )
222 vtkIdType nbNodes = GetEntities( SMDSAbs_Node, aNodes );
223 thePoints->SetNumberOfPoints( nbNodes );
227 TEntityList::const_iterator anIter;
228 for ( anIter = aNodes.begin(); anIter != aNodes.end(); ++anIter )
230 const SMDS_MeshNode* aNode = ( const SMDS_MeshNode* )(*anIter);
233 thePoints->SetPoint( nbPoints, aNode->X(), aNode->Y(), aNode->Z() );
234 int anId = aNode->GetID();
235 mySMDS2VTKNodes.insert( TMapOfIds::value_type( anId, nbPoints ) );
236 myVTK2SMDSNodes.insert( TMapOfIds::value_type( nbPoints, anId ) );
241 if ( nbPoints != nbNodes )
242 thePoints->SetNumberOfPoints( nbPoints );
245 //=================================================================================
246 // function : buildPrs
247 // purpose : create VTK cells( fill unstructured grid )
248 //=================================================================================
249 void SMESH_VisualObjDef::buildPrs(bool buildGrid)
251 MESSAGE("----------------------------------------------------------SMESH_VisualObjDef::buildPrs " << buildGrid);
257 mySMDS2VTKNodes.clear();
258 myVTK2SMDSNodes.clear();
259 mySMDS2VTKElems.clear();
260 myVTK2SMDSElems.clear();
269 mySMDS2VTKNodes.clear();
270 myVTK2SMDSNodes.clear();
271 mySMDS2VTKElems.clear();
272 myVTK2SMDSElems.clear();
274 myGrid->SetPoints( 0 );
275 myGrid->SetCells( 0, 0, 0, 0, 0 );
282 if (!GetMesh()->isCompacted())
284 MESSAGE("*** buildPrs ==> compactMesh!");
285 GetMesh()->compactMesh();
287 vtkUnstructuredGrid *theGrid = GetMesh()->getGrid();
288 updateEntitiesFlags();
289 myGrid->ShallowCopy(theGrid);
290 //MESSAGE(myGrid->GetReferenceCount());
291 //MESSAGE( "Update - myGrid->GetNumberOfCells() = "<<myGrid->GetNumberOfCells() );
292 //MESSAGE( "Update - myGrid->GetNumberOfPoints() = "<<myGrid->GetNumberOfPoints() );
293 if( MYDEBUGWITHFILES ) {
294 SMESH::WriteUnstructuredGrid( myGrid,"myPrs.vtu" );
299 //=================================================================================
300 // function : buildNodePrs
301 // purpose : create VTK cells for nodes
302 //=================================================================================
304 void SMESH_VisualObjDef::buildNodePrs()
306 // PAL16631: without swap, bad_alloc is not thrown but hung up and crash instead,
307 // so check remaining memory size for safety
308 SMDS_Mesh::CheckMemory(); // PAL16631
309 vtkPoints* aPoints = vtkPoints::New();
310 createPoints( aPoints );
311 SMDS_Mesh::CheckMemory();
312 myGrid->SetPoints( aPoints );
315 myGrid->SetCells( 0, 0, 0, 0, 0 );
318 //=================================================================================
319 // function : buildElemPrs
320 // purpose : Create VTK cells for elements
321 //=================================================================================
324 typedef std::vector<const SMDS_MeshElement*> TConnect;
326 int GetConnect(const SMDS_ElemIteratorPtr& theNodesIter,
327 TConnect& theConnect)
330 for(; theNodesIter->more();)
331 theConnect.push_back(theNodesIter->next());
332 return theConnect.size();
336 void SetId(vtkIdList *theIdList,
337 const SMESH_VisualObjDef::TMapOfIds& theSMDS2VTKNodes,
338 const TConnect& theConnect,
342 theIdList->SetId(thePosition,theSMDS2VTKNodes.find(theConnect[theId]->GetID())->second);
348 void SMESH_VisualObjDef::buildElemPrs()
352 vtkPoints* aPoints = vtkPoints::New();
353 createPoints( aPoints );
354 myGrid->SetPoints( aPoints );
358 MESSAGE("Update - myGrid->GetNumberOfPoints() = "<<myGrid->GetNumberOfPoints());
360 // Calculate cells size
362 const int nbTypes = 5;
363 static SMDSAbs_ElementType aTypes[ nbTypes ] =
364 { SMDSAbs_Edge, SMDSAbs_Face, SMDSAbs_Volume, SMDSAbs_Ball, SMDSAbs_0DElement };
367 map<SMDSAbs_ElementType,int> nbEnts;
368 map<SMDSAbs_ElementType,TEntityList> anEnts;
370 vtkIdType aNbCells = 0;
372 for ( int i = 0; i < nbTypes; i++ )
374 nbEnts[ aTypes[ i ] ] = GetEntities( aTypes[ i ], anEnts[ aTypes[ i ] ] );
375 aNbCells += nbEnts[ aTypes [ i ]];
377 // PAL16631: without swap, bad_alloc is not thrown but hung up and crash instead,
378 // so check remaining memory size for safety
379 SMDS_Mesh::CheckMemory(); // PAL16631
381 vtkIdType aCellsSize = 2 * nbEnts[ SMDSAbs_0DElement ] + 3 * nbEnts[ SMDSAbs_Edge ];
382 aCellsSize += 2 * nbEnts[ SMDSAbs_Ball ];
384 for ( int i = 1; i <= 2; i++ ) // iterate through faces and volumes
386 if ( nbEnts[ aTypes[ i ] ] )
388 const TEntityList& aList = anEnts[ aTypes[ i ] ];
389 TEntityList::const_iterator anIter;
390 for ( anIter = aList.begin(); anIter != aList.end(); ++anIter ) {
391 if((*anIter)->GetEntityType() != SMDSEntity_Polyhedra &&
392 (*anIter)->GetEntityType() != SMDSEntity_Quad_Polyhedra) {
393 aCellsSize += (*anIter)->NbNodes() + 1;
395 // Special case for the VTK_POLYHEDRON:
396 // itsinput cellArray is of special format.
397 // [nCellFaces, nFace0Pts, i, j, k, nFace1Pts, i, j, k, ...]
399 if( const SMDS_VtkVolume* ph = dynamic_cast<const SMDS_VtkVolume*>(*anIter) ) {
400 int nbFaces = ph->NbFaces();
401 aCellsSize += (1 + ph->NbFaces());
402 for( int i = 1; i <= nbFaces; i++ ) {
403 aCellsSize += ph->NbFaceNodes(i);
411 MESSAGE( "Update - aNbCells = "<<aNbCells<<"; aCellsSize = "<<aCellsSize );
415 vtkCellArray* aConnectivity = vtkCellArray::New();
416 aConnectivity->Allocate( aCellsSize, 0 );
418 SMDS_Mesh::CheckMemory(); // PAL16631
420 vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
421 aCellTypesArray->SetNumberOfComponents( 1 );
422 aCellTypesArray->Allocate( aNbCells * aCellTypesArray->GetNumberOfComponents() );
424 SMDS_Mesh::CheckMemory(); // PAL16631
426 vtkIdList *anIdList = vtkIdList::New();
430 aConnect.reserve(VTK_CELL_SIZE);
432 SMDS_Mesh::CheckMemory(); // PAL16631
433 bool hasBalls = nbEnts[ SMDSAbs_Ball ] > 0;
434 vtkDataArray* aScalars = 0;
436 aScalars = vtkDataArray::CreateDataArray(VTK_DOUBLE);
437 aScalars->SetNumberOfComponents(1);
438 aScalars->SetNumberOfTuples(aNbCells);
440 for ( int i = 0; i < nbTypes; i++ ) // iterate through all types of elements
442 if ( nbEnts[ aTypes[ i ] ] > 0 ) {
444 const SMDSAbs_ElementType& aType = aTypes[ i ];
445 const TEntityList& aList = anEnts[ aType ];
446 TEntityList::const_iterator anIter;
447 for ( anIter = aList.begin(); anIter != aList.end(); ++anIter )
449 const SMDS_MeshElement* anElem = *anIter;
451 vtkIdType aNbNodes = anElem->NbNodes();
452 anIdList->SetNumberOfIds( aNbNodes );
453 const vtkIdType vtkElemType = SMDS_MeshCell::toVtkType( anElem->GetEntityType() );
455 int anId = anElem->GetID();
457 mySMDS2VTKElems.insert( TMapOfIds::value_type( anId, iElem ) );
458 myVTK2SMDSElems.insert( TMapOfIds::value_type( iElem, anId ) );
460 SMDS_ElemIteratorPtr aNodesIter = anElem->nodesIterator();
462 // Convertions connectivities from SMDS to VTK
464 if (aType == SMDSAbs_Volume && anElem->IsPoly() && aNbNodes > 3) { // POLYEDRE
466 if ( const SMDS_VtkVolume* ph = dynamic_cast<const SMDS_VtkVolume*>(anElem) ) {
467 int nbFaces = ph->NbFaces();
468 anIdList->InsertNextId(nbFaces);
469 for( int i = 1; i <= nbFaces; i++ ) {
470 anIdList->InsertNextId(ph->NbFaceNodes(i));
471 for(int j = 1; j <= ph->NbFaceNodes(i); j++) {
472 const SMDS_MeshNode* n = ph->GetFaceNode(i,j);
474 anIdList->InsertNextId(mySMDS2VTKNodes[n->GetID()]);
481 const std::vector<int>& aConnectivities =
482 SMDS_MeshCell::toVtkOrder( VTKCellType( vtkElemType ));
483 if (aConnectivities.size() > 0) {
485 GetConnect(aNodesIter,aConnect);
486 for (vtkIdType aNodeId = 0; aNodeId < aNbNodes; aNodeId++)
487 SetId(anIdList,mySMDS2VTKNodes,aConnect,aNodeId,aConnectivities[aNodeId]);
490 for( vtkIdType aNodeId = 0; aNodesIter->more(); aNodeId++ ){
491 const SMDS_MeshElement* aNode = aNodesIter->next();
492 anIdList->SetId( aNodeId, mySMDS2VTKNodes[aNode->GetID()] );
497 vtkIdType aCurId = aConnectivity->InsertNextCell( anIdList );
498 aCellTypesArray->InsertNextValue( vtkElemType );
500 //Store diameters of the balls
503 if(aType == SMDSAbs_Ball) {
504 if (const SMDS_BallElement* ball = dynamic_cast<const SMDS_BallElement*>(anElem) ) {
505 aDiam = ball->GetDiameter();
508 aScalars->SetTuple(aCurId,&aDiam);
514 SMDS_Mesh::CheckMemory(); // PAL16631
517 // Insert cells in grid
519 VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
520 aCellLocationsArray->SetNumberOfComponents( 1 );
521 aCellLocationsArray->SetNumberOfTuples( aNbCells );
523 SMDS_Mesh::CheckMemory(); // PAL16631
525 aConnectivity->InitTraversal();
526 for( vtkIdType idType = 0, *pts, npts; aConnectivity->GetNextCell( npts, pts ); idType++ )
527 aCellLocationsArray->SetValue( idType, aConnectivity->GetTraversalLocation( npts ) );
529 myGrid->SetCells( aCellTypesArray, aCellLocationsArray,aConnectivity );
530 myGrid->GetCellData()->SetScalars(aScalars);
532 aCellLocationsArray->Delete();
533 aCellTypesArray->Delete();
534 aConnectivity->Delete();
537 SMDS_Mesh::CheckMemory(); // PAL16631
540 //=================================================================================
541 // function : GetEdgeNodes
542 // purpose : Retrieve ids of nodes from edge of elements ( edge is numbered from 0 )
543 //=================================================================================
544 bool SMESH_VisualObjDef::GetEdgeNodes( const int theElemId,
545 const int theEdgeNum,
547 int& theNodeId2 ) const
549 const SMDS_Mesh* aMesh = GetMesh();
553 const SMDS_MeshElement* anElem = aMesh->FindElement( theElemId );
557 int nbNodes = anElem->NbCornerNodes();
559 if (( theEdgeNum < 0 || theEdgeNum > 3 ) ||
560 ( nbNodes != 3 && nbNodes != 4 ) ||
561 ( theEdgeNum >= nbNodes ))
564 theNodeId1 = anElem->GetNode( theEdgeNum )->GetID();
565 theNodeId2 = anElem->GetNode(( theEdgeNum + 1 ) % nbNodes )->GetID();
570 vtkUnstructuredGrid* SMESH_VisualObjDef::GetUnstructuredGrid()
572 if ( !myLocalGrid && !GetMesh()->isCompacted() )
574 GetMesh()->compactMesh();
575 updateEntitiesFlags();
576 vtkUnstructuredGrid *theGrid = GetMesh()->getGrid();
577 myGrid->ShallowCopy(theGrid);
583 //=================================================================================
584 // function : IsValid
585 // purpose : Return true if there are some entities
586 //=================================================================================
587 bool SMESH_VisualObjDef::IsValid() const
589 return ( GetNbEntities(SMDSAbs_0DElement) > 0 ||
590 GetNbEntities(SMDSAbs_Ball ) > 0 ||
591 GetNbEntities(SMDSAbs_Edge ) > 0 ||
592 GetNbEntities(SMDSAbs_Face ) > 0 ||
593 GetNbEntities(SMDSAbs_Volume ) > 0 ||
594 GetNbEntities(SMDSAbs_Node ) > 0 );
597 //=================================================================================
598 // function : updateEntitiesFlags
599 // purpose : Update entities flags
600 //=================================================================================
601 void SMESH_VisualObjDef::updateEntitiesFlags()
603 unsigned int tmp = myEntitiesState;
604 ClearEntitiesFlags();
606 map<SMDSAbs_ElementType,int> entities = SMESH::GetEntitiesFromObject(this);
609 if( myEntitiesCache[SMDSAbs_0DElement] != 0 ||
610 myEntitiesCache[SMDSAbs_0DElement] >= entities[SMDSAbs_0DElement] )
611 myEntitiesState &= ~SMESH_Actor::e0DElements;
613 if( myEntitiesCache[SMDSAbs_Ball] != 0 ||
614 myEntitiesCache[SMDSAbs_Ball] >= entities[SMDSAbs_Ball] )
615 myEntitiesState &= ~SMESH_Actor::eBallElem;
617 if( myEntitiesCache[SMDSAbs_Edge] != 0 ||
618 myEntitiesCache[SMDSAbs_Edge] >= entities[SMDSAbs_Edge] )
619 myEntitiesState &= ~SMESH_Actor::eEdges;
621 if( myEntitiesCache[SMDSAbs_Face] != 0 ||
622 myEntitiesCache[SMDSAbs_Face] >= entities[SMDSAbs_Face] )
623 myEntitiesState &= ~SMESH_Actor::eFaces;
625 if( myEntitiesCache[SMDSAbs_Volume] != 0 ||
626 myEntitiesCache[SMDSAbs_Volume] >= entities[SMDSAbs_Volume] )
627 myEntitiesState &= ~SMESH_Actor::eVolumes;
629 if( tmp != myEntitiesState ) {
630 myEntitiesFlag = true;
633 myEntitiesCache = entities;
636 //=================================================================================
637 // function : ClearEntitiesFlags
638 // purpose : Clear the entities flags
639 //=================================================================================
640 void SMESH_VisualObjDef::ClearEntitiesFlags()
642 myEntitiesState = SMESH_Actor::eAllEntity;
643 myEntitiesFlag = false;
646 //=================================================================================
647 // function : GetEntitiesFlag
648 // purpose : Return the entities flag
649 //=================================================================================
650 bool SMESH_VisualObjDef::GetEntitiesFlag()
652 return myEntitiesFlag;
655 //=================================================================================
656 // function : GetEntitiesState
657 // purpose : Return the entities state
658 //=================================================================================
659 unsigned int SMESH_VisualObjDef::GetEntitiesState()
661 return myEntitiesState;
665 Class : SMESH_MeshObj
666 Description : Class for visualisation of mesh
669 //=================================================================================
670 // function : SMESH_MeshObj
671 // purpose : Constructor
672 //=================================================================================
673 SMESH_MeshObj::SMESH_MeshObj(SMESH::SMESH_Mesh_ptr theMesh):
674 myClient(SalomeApp_Application::orb(),theMesh)
678 MESSAGE("SMESH_MeshObj - this = "<<this<<"; theMesh->_is_nil() = "<<theMesh->_is_nil());
681 //=================================================================================
682 // function : ~SMESH_MeshObj
683 // purpose : Destructor
684 //=================================================================================
685 SMESH_MeshObj::~SMESH_MeshObj()
688 MESSAGE("SMESH_MeshObj - this = "<<this<<"\n");
690 myEmptyGrid->Delete();
693 //=================================================================================
695 // purpose : Update mesh and fill grid with new values if necessary
696 //=================================================================================
697 bool SMESH_MeshObj::Update( int theIsClear )
699 // Update SMDS_Mesh on client part
700 MESSAGE("SMESH_MeshObj::Update " << this);
701 if ( myClient.Update(theIsClear) || GetUnstructuredGrid()->GetNumberOfPoints()==0) {
703 buildPrs(); // Fill unstructured grid
709 bool SMESH_MeshObj::NulData()
711 MESSAGE ("SMESH_MeshObj::NulData() ==================================================================================");
714 myEmptyGrid = SMDS_UnstructuredGrid::New();
715 myEmptyGrid->Initialize();
716 myEmptyGrid->Allocate();
717 vtkPoints* points = vtkPoints::New();
718 points->SetNumberOfPoints(0);
719 myEmptyGrid->SetPoints( points );
721 myEmptyGrid->BuildLinks();
723 myGrid->ShallowCopy(myEmptyGrid);
726 //=================================================================================
727 // function : GetElemDimension
728 // purpose : Get dimension of element
729 //=================================================================================
730 int SMESH_MeshObj::GetElemDimension( const int theObjId )
732 const SMDS_MeshElement* anElem = myClient->FindElement( theObjId );
736 int aType = anElem->GetType();
739 case SMDSAbs_0DElement : return 0;
740 case SMDSAbs_Ball : return 0;
741 case SMDSAbs_Edge : return 1;
742 case SMDSAbs_Face : return 2;
743 case SMDSAbs_Volume: return 3;
748 //=================================================================================
749 // function : GetEntities
750 // purpose : Get entities of specified type. Return number of entities
751 //=================================================================================
752 int SMESH_MeshObj::GetNbEntities( const SMDSAbs_ElementType theType) const
758 return myClient->NbNodes();
761 case SMDSAbs_0DElement:
763 return myClient->Nb0DElements();
767 return myClient->NbBalls();
772 return myClient->NbEdges();
777 return myClient->NbFaces();
782 return myClient->NbVolumes();
791 int SMESH_MeshObj::GetEntities( const SMDSAbs_ElementType theType, TEntityList& theObjs ) const
799 SMDS_NodeIteratorPtr anIter = myClient->nodesIterator();
800 while ( anIter->more() ) theObjs.push_back( anIter->next() );
803 case SMDSAbs_0DElement:
805 SMDS_ElemIteratorPtr anIter = myClient->elementsIterator(SMDSAbs_0DElement);
806 while ( anIter->more() ) theObjs.push_back( anIter->next() );
811 SMDS_ElemIteratorPtr anIter = myClient->elementGeomIterator(SMDSGeom_BALL);
812 while ( anIter->more() ) theObjs.push_back( anIter->next() );
817 SMDS_EdgeIteratorPtr anIter = myClient->edgesIterator();
818 while ( anIter->more() ) theObjs.push_back( anIter->next() );
823 SMDS_FaceIteratorPtr anIter = myClient->facesIterator();
824 while ( anIter->more() ) theObjs.push_back( anIter->next() );
829 SMDS_VolumeIteratorPtr anIter = myClient->volumesIterator();
830 while ( anIter->more() ) theObjs.push_back( anIter->next() );
837 return theObjs.size();
840 //=================================================================================
841 // function : UpdateFunctor
842 // purpose : Update functor in accordance with current mesh
843 //=================================================================================
844 void SMESH_MeshObj::UpdateFunctor( const SMESH::Controls::FunctorPtr& theFunctor )
846 theFunctor->SetMesh( GetMesh() );
849 //=================================================================================
850 // function : IsNodePrs
851 // purpose : Return true if node presentation is used
852 //=================================================================================
853 bool SMESH_MeshObj::IsNodePrs() const
855 return myClient->Nb0DElements() + myClient->NbEdges() + myClient->NbFaces() + myClient->NbVolumes() + myClient->NbBalls() == 0 ;
860 Class : SMESH_SubMeshObj
861 Description : Base class for visualisation of submeshes and groups
864 //=================================================================================
865 // function : SMESH_SubMeshObj
866 // purpose : Constructor
867 //=================================================================================
868 SMESH_SubMeshObj::SMESH_SubMeshObj( SMESH_MeshObj* theMeshObj )
870 if ( MYDEBUG ) MESSAGE( "SMESH_SubMeshObj - theMeshObj = " << theMeshObj );
872 myMeshObj = theMeshObj;
875 SMESH_SubMeshObj::~SMESH_SubMeshObj()
879 //=================================================================================
880 // function : GetElemDimension
881 // purpose : Get dimension of element
882 //=================================================================================
883 int SMESH_SubMeshObj::GetElemDimension( const int theObjId )
885 return myMeshObj == 0 ? 0 : myMeshObj->GetElemDimension( theObjId );
888 //=================================================================================
889 // function : UpdateFunctor
890 // purpose : Update functor in accordance with current mesh
891 //=================================================================================
893 void SMESH_SubMeshObj::UpdateFunctor( const SMESH::Controls::FunctorPtr& theFunctor )
895 theFunctor->SetMesh( myMeshObj->GetMesh() );
898 //=================================================================================
900 // purpose : Update mesh object and fill grid with new values
901 //=================================================================================
902 bool SMESH_SubMeshObj::Update( int theIsClear )
904 MESSAGE("SMESH_SubMeshObj::Update " << this)
905 bool changed = myMeshObj->Update( theIsClear );
912 Class : SMESH_GroupObj
913 Description : Class for visualisation of groups
916 //=================================================================================
917 // function : SMESH_GroupObj
918 // purpose : Constructor
919 //=================================================================================
920 SMESH_GroupObj::SMESH_GroupObj( SMESH::SMESH_GroupBase_ptr theGroup,
921 SMESH_MeshObj* theMeshObj )
922 : SMESH_SubMeshObj( theMeshObj ),
923 myGroupServer( SMESH::SMESH_GroupBase::_duplicate(theGroup) )
925 if ( MYDEBUG ) MESSAGE("SMESH_GroupObj - theGroup->_is_nil() = "<<theGroup->_is_nil());
926 myGroupServer->Register();
929 SMESH_GroupObj::~SMESH_GroupObj()
931 if ( MYDEBUG ) MESSAGE("~SMESH_GroupObj");
932 myGroupServer->UnRegister();
935 //=================================================================================
936 // function : IsNodePrs
937 // purpose : Return true if node presentation is used
938 //=================================================================================
939 bool SMESH_GroupObj::IsNodePrs() const
941 return myGroupServer->GetType() == SMESH::NODE;
944 //=================================================================================
945 // function : GetElementType
946 // purpose : Return type of elements of group
947 //=================================================================================
948 SMDSAbs_ElementType SMESH_GroupObj::GetElementType() const
950 return SMDSAbs_ElementType(myGroupServer->GetType());
953 //=================================================================================
954 // function : getNodesFromElems
955 // purpose : Retrieve nodes from elements
956 //=================================================================================
957 static int getNodesFromElems( SMESH::long_array_var& theElemIds,
958 const SMDS_Mesh* theMesh,
959 std::list<const SMDS_MeshElement*>& theResList )
961 set<const SMDS_MeshElement*> aNodeSet;
963 for ( CORBA::Long i = 0, n = theElemIds->length(); i < n; i++ )
965 const SMDS_MeshElement* anElem = theMesh->FindElement( theElemIds[ i ] );
968 SMDS_ElemIteratorPtr anIter = anElem->nodesIterator();
969 while ( anIter->more() )
971 const SMDS_MeshElement* aNode = anIter->next();
973 aNodeSet.insert( aNode );
978 set<const SMDS_MeshElement*>::const_iterator anIter;
979 for ( anIter = aNodeSet.begin(); anIter != aNodeSet.end(); ++anIter )
980 theResList.push_back( *anIter );
982 return theResList.size();
985 //=================================================================================
986 // function : getPointers
987 // purpose : Get std::list<const SMDS_MeshElement*> from list of IDs
988 //=================================================================================
989 static int getPointers( const SMDSAbs_ElementType theRequestType,
990 SMESH::long_array_var& theElemIds,
991 const SMDS_Mesh* theMesh,
992 std::list<const SMDS_MeshElement*>& theResList )
994 for ( CORBA::Long i = 0, n = theElemIds->length(); i < n; i++ )
996 const SMDS_MeshElement* anElem = theRequestType == SMDSAbs_Node
997 ? theMesh->FindNode( theElemIds[ i ] ) : theMesh->FindElement( theElemIds[ i ] );
1000 theResList.push_back( anElem );
1003 return theResList.size();
1007 //=================================================================================
1008 // function : GetEntities
1009 // purpose : Get entities of specified type. Return number of entities
1010 //=================================================================================
1011 int SMESH_GroupObj::GetNbEntities( const SMDSAbs_ElementType theType) const
1013 if(SMDSAbs_ElementType(myGroupServer->GetType()) == theType) {
1014 return myGroupServer->Size();
1016 if ( theType == SMDSAbs_Node ) {
1017 return myGroupServer->GetNumberOfNodes();
1022 int SMESH_GroupObj::GetEntities( const SMDSAbs_ElementType theType, TEntityList& theResList ) const
1025 SMDS_Mesh* aMesh = myMeshObj->GetMesh();
1030 SMDSAbs_ElementType aGrpType = SMDSAbs_ElementType(myGroupServer->GetType());
1031 if ( aGrpType != theType && theType != SMDSAbs_Node )
1034 SMESH::long_array_var anIds = myGroupServer->GetListOfID();
1035 if ( anIds->length() == 0 )
1038 if ( aGrpType == theType )
1039 return getPointers( theType, anIds, aMesh, theResList );
1040 else if ( theType == SMDSAbs_Node )
1041 return getNodesFromElems( anIds, aMesh, theResList );
1049 Class : SMESH_subMeshObj
1050 Description : Class for visualisation of submeshes
1053 //=================================================================================
1054 // function : SMESH_subMeshObj
1055 // purpose : Constructor
1056 //=================================================================================
1057 SMESH_subMeshObj::SMESH_subMeshObj( SMESH::SMESH_subMesh_ptr theSubMesh,
1058 SMESH_MeshObj* theMeshObj )
1059 : SMESH_SubMeshObj( theMeshObj ),
1060 mySubMeshServer( SMESH::SMESH_subMesh::_duplicate( theSubMesh ) )
1062 if ( MYDEBUG ) MESSAGE( "SMESH_subMeshObj - theSubMesh->_is_nil() = " << theSubMesh->_is_nil() );
1064 mySubMeshServer->Register();
1067 SMESH_subMeshObj::~SMESH_subMeshObj()
1069 if ( MYDEBUG ) MESSAGE( "~SMESH_subMeshObj" );
1070 mySubMeshServer->UnRegister();
1073 //=================================================================================
1074 // function : GetEntities
1075 // purpose : Get entities of specified type. Return number of entities
1076 //=================================================================================
1077 int SMESH_subMeshObj::GetNbEntities( const SMDSAbs_ElementType theType) const
1083 return mySubMeshServer->GetNumberOfNodes( /*all=*/true );
1087 case SMDSAbs_0DElement:
1090 case SMDSAbs_Volume:
1092 SMESH::long_array_var anIds =
1093 mySubMeshServer->GetElementsByType( SMESH::ElementType(theType) );
1094 return anIds->length();
1102 int SMESH_subMeshObj::GetEntities( const SMDSAbs_ElementType theType, TEntityList& theResList ) const
1106 SMDS_Mesh* aMesh = myMeshObj->GetMesh();
1110 bool isNodal = IsNodePrs();
1114 if ( theType == SMDSAbs_Node )
1116 SMESH::long_array_var anIds = mySubMeshServer->GetNodesId();
1117 return getPointers( SMDSAbs_Node, anIds, aMesh, theResList );
1122 if ( theType == SMDSAbs_Node )
1124 SMESH::long_array_var anIds = mySubMeshServer->GetElementsId();
1125 return getNodesFromElems( anIds, aMesh, theResList );
1129 SMESH::long_array_var anIds =
1130 mySubMeshServer->GetElementsByType( SMESH::ElementType(theType) );
1131 return getPointers( theType, anIds, aMesh, theResList );
1138 //=================================================================================
1139 // function : IsNodePrs
1140 // purpose : Return true if node presentation is used
1141 //=================================================================================
1142 bool SMESH_subMeshObj::IsNodePrs() const
1144 return mySubMeshServer->GetNumberOfElements() == 0;