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 // 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 = 0;
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 if ( MYDEBUG ) MESSAGE("-------------------------------SMESH_VisualObjDef::SMESH_VisualObjDef");
137 myGrid = vtkUnstructuredGrid::New();
139 ClearEntitiesFlags();
140 SMESH::GetEntitiesFromObject(NULL);
142 SMESH_VisualObjDef::~SMESH_VisualObjDef()
144 if ( MYDEBUG ) MESSAGE("--------------------------------SMESH_VisualObjDef::~SMESH_VisualObjDef");
145 if ( MYDEBUG ) MESSAGE( "myGrid->GetReferenceCount() = " << myGrid->GetReferenceCount() );
149 //=================================================================================
150 // functions : GetNodeObjId, GetNodeVTKId, GetElemObjId, GetElemVTKId
151 // purpose : Methods for retrieving VTK IDs by SMDS IDs and vice versa
152 //=================================================================================
153 vtkIdType SMESH_VisualObjDef::GetNodeObjId( int theVTKID )
157 TMapOfIds::const_iterator i = myVTK2SMDSNodes.find(theVTKID);
158 return i == myVTK2SMDSNodes.end() ? -1 : i->second;
160 const SMDS_MeshNode* aNode = 0;
161 if( this->GetMesh() )
162 aNode = this->GetMesh()->FindNodeVtk( theVTKID );
164 return aNode ? aNode->GetID() : -1;
167 vtkIdType SMESH_VisualObjDef::GetNodeVTKId( int theObjID )
171 TMapOfIds::const_iterator i = mySMDS2VTKNodes.find(theObjID);
172 return i == mySMDS2VTKNodes.end() ? -1 : i->second;
175 const SMDS_MeshNode* aNode = 0;
176 if( this->GetMesh() ) {
177 aNode = this->GetMesh()->FindNode(theObjID);
179 return aNode ? aNode->getVtkId() : -1;
182 vtkIdType SMESH_VisualObjDef::GetElemObjId( int theVTKID )
186 TMapOfIds::const_iterator i = myVTK2SMDSElems.find(theVTKID);
187 return i == myVTK2SMDSElems.end() ? -1 : i->second;
189 return this->GetMesh()->fromVtkToSmds(theVTKID);
192 vtkIdType SMESH_VisualObjDef::GetElemVTKId( int theObjID )
196 TMapOfIds::const_iterator i = mySMDS2VTKElems.find(theObjID);
197 return i == mySMDS2VTKElems.end() ? -1 : i->second;
200 const SMDS_MeshElement* e = 0;
201 if ( this->GetMesh() )
202 e = this->GetMesh()->FindElement(theObjID);
204 return e ? e->getVtkId() : -1;
207 //=================================================================================
208 // function : SMESH_VisualObjDef::createPoints
209 // purpose : Create points from nodes
210 //=================================================================================
211 /*! fills a vtkPoints structure for a submesh.
212 * fills a std::list of SMDS_MeshElements*, then extract the points.
213 * fills also conversion id maps between SMDS and VTK.
215 void SMESH_VisualObjDef::createPoints( vtkPoints* thePoints )
217 if ( thePoints == 0 )
221 vtkIdType nbNodes = GetEntities( SMDSAbs_Node, aNodes );
222 thePoints->SetNumberOfPoints( nbNodes );
226 TEntityList::const_iterator anIter;
227 for ( anIter = aNodes.begin(); anIter != aNodes.end(); ++anIter )
229 const SMDS_MeshNode* aNode = ( const SMDS_MeshNode* )(*anIter);
232 thePoints->SetPoint( nbPoints, aNode->X(), aNode->Y(), aNode->Z() );
233 int anId = aNode->GetID();
234 mySMDS2VTKNodes.insert( mySMDS2VTKNodes.end(), std::make_pair( anId, nbPoints ));
235 myVTK2SMDSNodes.insert( myVTK2SMDSNodes.end(), std::make_pair( nbPoints, anId ));
240 if ( nbPoints != nbNodes )
241 thePoints->SetNumberOfPoints( nbPoints );
244 //=================================================================================
245 // function : buildPrs
246 // purpose : create VTK cells( fill unstructured grid )
247 //=================================================================================
248 void SMESH_VisualObjDef::buildPrs(bool buildGrid)
250 if ( MYDEBUG ) MESSAGE("---------------------------SMESH_VisualObjDef::buildPrs " << buildGrid);
256 mySMDS2VTKNodes.clear();
257 myVTK2SMDSNodes.clear();
258 mySMDS2VTKElems.clear();
259 myVTK2SMDSElems.clear();
268 mySMDS2VTKNodes.clear();
269 myVTK2SMDSNodes.clear();
270 mySMDS2VTKElems.clear();
271 myVTK2SMDSElems.clear();
273 myGrid->SetPoints( 0 );
274 myGrid->SetCells( 0, 0, 0, 0, 0 );
281 if (!GetMesh()->isCompacted())
283 if ( MYDEBUG ) MESSAGE("*** buildPrs ==> compactMesh!");
284 GetMesh()->compactMesh();
286 vtkUnstructuredGrid *theGrid = GetMesh()->getGrid();
287 updateEntitiesFlags();
288 myGrid->ShallowCopy(theGrid);
289 //MESSAGE(myGrid->GetReferenceCount());
290 //MESSAGE( "Update - myGrid->GetNumberOfCells() = "<<myGrid->GetNumberOfCells() );
291 //MESSAGE( "Update - myGrid->GetNumberOfPoints() = "<<myGrid->GetNumberOfPoints() );
292 if( MYDEBUGWITHFILES ) {
293 SMESH::WriteUnstructuredGrid( myGrid,"myPrs.vtu" );
298 //=================================================================================
299 // function : buildNodePrs
300 // purpose : create VTK cells for nodes
301 //=================================================================================
303 void SMESH_VisualObjDef::buildNodePrs()
305 // PAL16631: without swap, bad_alloc is not thrown but hung up and crash instead,
306 // so check remaining memory size for safety
307 SMDS_Mesh::CheckMemory(); // PAL16631
308 vtkPoints* aPoints = vtkPoints::New();
309 createPoints( aPoints );
310 SMDS_Mesh::CheckMemory();
311 myGrid->SetPoints( aPoints );
314 myGrid->SetCells( 0, 0, 0, 0, 0 );
317 //=================================================================================
318 // function : buildElemPrs
319 // purpose : Create VTK cells for elements
320 //=================================================================================
323 typedef std::vector<const SMDS_MeshElement*> TConnect;
325 int GetConnect(const SMDS_ElemIteratorPtr& theNodesIter,
326 TConnect& theConnect)
329 for(; theNodesIter->more();)
330 theConnect.push_back(theNodesIter->next());
331 return theConnect.size();
335 void SetId(vtkIdList *theIdList,
336 const SMESH_VisualObjDef::TMapOfIds& theSMDS2VTKNodes,
337 const TConnect& theConnect,
341 theIdList->SetId(thePosition,theSMDS2VTKNodes.find(theConnect[theId]->GetID())->second);
347 void SMESH_VisualObjDef::buildElemPrs()
351 vtkPoints* aPoints = vtkPoints::New();
352 createPoints( aPoints );
353 myGrid->SetPoints( aPoints );
357 MESSAGE("Update - myGrid->GetNumberOfPoints() = "<<myGrid->GetNumberOfPoints());
359 // Calculate cells size
361 const int nbTypes = 5;
362 static SMDSAbs_ElementType aTypes[ nbTypes ] =
363 { SMDSAbs_Edge, SMDSAbs_Face, SMDSAbs_Volume, SMDSAbs_Ball, SMDSAbs_0DElement };
366 map<SMDSAbs_ElementType,int> nbEnts;
367 map<SMDSAbs_ElementType,TEntityList> anEnts;
369 vtkIdType aNbCells = 0;
371 for ( int i = 0; i < nbTypes; i++ )
373 nbEnts[ aTypes[ i ] ] = GetEntities( aTypes[ i ], anEnts[ aTypes[ i ] ] );
374 aNbCells += nbEnts[ aTypes [ i ]];
376 // PAL16631: without swap, bad_alloc is not thrown but hung up and crash instead,
377 // so check remaining memory size for safety
378 SMDS_Mesh::CheckMemory(); // PAL16631
380 vtkIdType aCellsSize = 2 * nbEnts[ SMDSAbs_0DElement ] + 3 * nbEnts[ SMDSAbs_Edge ];
381 aCellsSize += 2 * nbEnts[ SMDSAbs_Ball ];
383 for ( int i = 1; i <= 2; i++ ) // iterate through faces and volumes
385 if ( nbEnts[ aTypes[ i ] ] )
387 const TEntityList& aList = anEnts[ aTypes[ i ] ];
388 TEntityList::const_iterator anIter;
389 for ( anIter = aList.begin(); anIter != aList.end(); ++anIter ) {
390 if((*anIter)->GetEntityType() != SMDSEntity_Polyhedra &&
391 (*anIter)->GetEntityType() != SMDSEntity_Quad_Polyhedra) {
392 aCellsSize += (*anIter)->NbNodes() + 1;
394 // Special case for the VTK_POLYHEDRON:
395 // itsinput cellArray is of special format.
396 // [nCellFaces, nFace0Pts, i, j, k, nFace1Pts, i, j, k, ...]
398 if( const SMDS_VtkVolume* ph = dynamic_cast<const SMDS_VtkVolume*>(*anIter) ) {
399 int nbFaces = ph->NbFaces();
400 aCellsSize += (1 + ph->NbFaces());
401 for( int i = 1; i <= nbFaces; i++ ) {
402 aCellsSize += ph->NbFaceNodes(i);
410 MESSAGE( "Update - aNbCells = "<<aNbCells<<"; aCellsSize = "<<aCellsSize );
414 vtkCellArray* aConnectivity = vtkCellArray::New();
415 aConnectivity->Allocate( aCellsSize, 0 );
417 SMDS_Mesh::CheckMemory(); // PAL16631
419 vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
420 aCellTypesArray->SetNumberOfComponents( 1 );
421 aCellTypesArray->Allocate( aNbCells * aCellTypesArray->GetNumberOfComponents() );
423 SMDS_Mesh::CheckMemory(); // PAL16631
425 vtkIdList *anIdList = vtkIdList::New();
429 aConnect.reserve(VTK_CELL_SIZE);
431 SMDS_Mesh::CheckMemory(); // PAL16631
432 bool hasBalls = nbEnts[ SMDSAbs_Ball ] > 0;
433 vtkDataArray* aScalars = 0;
435 aScalars = vtkDataArray::CreateDataArray(VTK_DOUBLE);
436 aScalars->SetNumberOfComponents(1);
437 aScalars->SetNumberOfTuples(aNbCells);
439 for ( int i = 0; i < nbTypes; i++ ) // iterate through all types of elements
441 if ( nbEnts[ aTypes[ i ] ] > 0 ) {
443 const SMDSAbs_ElementType& aType = aTypes[ i ];
444 const TEntityList& aList = anEnts[ aType ];
445 TEntityList::const_iterator anIter;
446 for ( anIter = aList.begin(); anIter != aList.end(); ++anIter )
448 const SMDS_MeshElement* anElem = *anIter;
450 vtkIdType aNbNodes = anElem->NbNodes();
451 anIdList->SetNumberOfIds( aNbNodes );
452 const vtkIdType vtkElemType = SMDS_MeshCell::toVtkType( anElem->GetEntityType() );
454 int anId = anElem->GetID();
456 mySMDS2VTKElems.insert( mySMDS2VTKElems.end(), std::make_pair( anId, iElem ));
457 myVTK2SMDSElems.insert( myVTK2SMDSElems.end(), std::make_pair( iElem, anId ));
459 SMDS_ElemIteratorPtr aNodesIter = anElem->nodesIterator();
461 // Convertions connectivities from SMDS to VTK
463 if (aType == SMDSAbs_Volume && anElem->IsPoly() && aNbNodes > 3) { // POLYEDRE
465 if ( const SMDS_VtkVolume* ph = dynamic_cast<const SMDS_VtkVolume*>(anElem) ) {
466 int nbFaces = ph->NbFaces();
467 anIdList->InsertNextId(nbFaces);
468 for( int i = 1; i <= nbFaces; i++ ) {
469 anIdList->InsertNextId(ph->NbFaceNodes(i));
470 for(int j = 1; j <= ph->NbFaceNodes(i); j++) {
471 const SMDS_MeshNode* n = ph->GetFaceNode(i,j);
473 anIdList->InsertNextId(mySMDS2VTKNodes[n->GetID()]);
480 const std::vector<int>& aConnectivities =
481 SMDS_MeshCell::toVtkOrder( VTKCellType( vtkElemType ));
482 if (aConnectivities.size() > 0) {
484 GetConnect(aNodesIter,aConnect);
485 for (vtkIdType aNodeId = 0; aNodeId < aNbNodes; aNodeId++)
486 SetId(anIdList,mySMDS2VTKNodes,aConnect,aNodeId,aConnectivities[aNodeId]);
489 for( vtkIdType aNodeId = 0; aNodesIter->more(); aNodeId++ ){
490 const SMDS_MeshElement* aNode = aNodesIter->next();
491 anIdList->SetId( aNodeId, mySMDS2VTKNodes[aNode->GetID()] );
496 vtkIdType aCurId = aConnectivity->InsertNextCell( anIdList );
497 aCellTypesArray->InsertNextValue( vtkElemType );
499 //Store diameters of the balls
502 if(aType == SMDSAbs_Ball) {
503 if (const SMDS_BallElement* ball = dynamic_cast<const SMDS_BallElement*>(anElem) ) {
504 aDiam = ball->GetDiameter();
507 aScalars->SetTuple(aCurId,&aDiam);
513 SMDS_Mesh::CheckMemory(); // PAL16631
516 // Insert cells in grid
518 VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
519 aCellLocationsArray->SetNumberOfComponents( 1 );
520 aCellLocationsArray->SetNumberOfTuples( aNbCells );
522 SMDS_Mesh::CheckMemory(); // PAL16631
524 aConnectivity->InitTraversal();
525 for( vtkIdType idType = 0, *pts, npts; aConnectivity->GetNextCell( npts, pts ); idType++ )
526 aCellLocationsArray->SetValue( idType, aConnectivity->GetTraversalLocation( npts ) );
528 myGrid->SetCells( aCellTypesArray, aCellLocationsArray,aConnectivity );
529 myGrid->GetCellData()->SetScalars(aScalars);
531 aCellLocationsArray->Delete();
532 aCellTypesArray->Delete();
533 aConnectivity->Delete();
536 SMDS_Mesh::CheckMemory(); // PAL16631
539 //=================================================================================
540 // function : GetEdgeNodes
541 // purpose : Retrieve ids of nodes from edge of elements ( edge is numbered from 0 )
542 //=================================================================================
543 bool SMESH_VisualObjDef::GetEdgeNodes( const int theElemId,
544 const int theEdgeNum,
546 int& theNodeId2 ) const
548 const SMDS_Mesh* aMesh = GetMesh();
552 const SMDS_MeshElement* anElem = aMesh->FindElement( theElemId );
556 int nbNodes = anElem->NbCornerNodes();
558 if (( theEdgeNum < 0 || theEdgeNum > 3 ) ||
559 ( nbNodes != 3 && nbNodes != 4 ) ||
560 ( theEdgeNum >= nbNodes ))
563 theNodeId1 = anElem->GetNode( theEdgeNum )->GetID();
564 theNodeId2 = anElem->GetNode(( theEdgeNum + 1 ) % nbNodes )->GetID();
569 vtkUnstructuredGrid* SMESH_VisualObjDef::GetUnstructuredGrid()
571 if ( !myLocalGrid && !GetMesh()->isCompacted() )
573 GetMesh()->compactMesh();
574 updateEntitiesFlags();
575 vtkUnstructuredGrid *theGrid = GetMesh()->getGrid();
576 myGrid->ShallowCopy(theGrid);
582 //=================================================================================
583 // function : IsValid
584 // purpose : Return true if there are some entities
585 //=================================================================================
586 bool SMESH_VisualObjDef::IsValid() const
588 return ( GetNbEntities(SMDSAbs_0DElement) > 0 ||
589 GetNbEntities(SMDSAbs_Ball ) > 0 ||
590 GetNbEntities(SMDSAbs_Edge ) > 0 ||
591 GetNbEntities(SMDSAbs_Face ) > 0 ||
592 GetNbEntities(SMDSAbs_Volume ) > 0 ||
593 GetNbEntities(SMDSAbs_Node ) > 0 );
596 //=================================================================================
597 // function : updateEntitiesFlags
598 // purpose : Update entities flags
599 //=================================================================================
600 void SMESH_VisualObjDef::updateEntitiesFlags()
602 unsigned int tmp = myEntitiesState;
603 ClearEntitiesFlags();
605 map<SMDSAbs_ElementType,int> entities = SMESH::GetEntitiesFromObject(this);
608 if( myEntitiesCache[SMDSAbs_0DElement] != 0 ||
609 myEntitiesCache[SMDSAbs_0DElement] >= entities[SMDSAbs_0DElement] )
610 myEntitiesState &= ~SMESH_Actor::e0DElements;
612 if( myEntitiesCache[SMDSAbs_Ball] != 0 ||
613 myEntitiesCache[SMDSAbs_Ball] >= entities[SMDSAbs_Ball] )
614 myEntitiesState &= ~SMESH_Actor::eBallElem;
616 if( myEntitiesCache[SMDSAbs_Edge] != 0 ||
617 myEntitiesCache[SMDSAbs_Edge] >= entities[SMDSAbs_Edge] )
618 myEntitiesState &= ~SMESH_Actor::eEdges;
620 if( myEntitiesCache[SMDSAbs_Face] != 0 ||
621 myEntitiesCache[SMDSAbs_Face] >= entities[SMDSAbs_Face] )
622 myEntitiesState &= ~SMESH_Actor::eFaces;
624 if( myEntitiesCache[SMDSAbs_Volume] != 0 ||
625 myEntitiesCache[SMDSAbs_Volume] >= entities[SMDSAbs_Volume] )
626 myEntitiesState &= ~SMESH_Actor::eVolumes;
628 if( tmp != myEntitiesState ) {
629 myEntitiesFlag = true;
632 myEntitiesCache = entities;
635 //=================================================================================
636 // function : ClearEntitiesFlags
637 // purpose : Clear the entities flags
638 //=================================================================================
639 void SMESH_VisualObjDef::ClearEntitiesFlags()
641 myEntitiesState = SMESH_Actor::eAllEntity;
642 myEntitiesFlag = false;
645 //=================================================================================
646 // function : GetEntitiesFlag
647 // purpose : Return the entities flag
648 //=================================================================================
649 bool SMESH_VisualObjDef::GetEntitiesFlag()
651 return myEntitiesFlag;
654 //=================================================================================
655 // function : GetEntitiesState
656 // purpose : Return the entities state
657 //=================================================================================
658 unsigned int SMESH_VisualObjDef::GetEntitiesState()
660 return myEntitiesState;
664 Class : SMESH_MeshObj
665 Description : Class for visualisation of mesh
668 //=================================================================================
669 // function : SMESH_MeshObj
670 // purpose : Constructor
671 //=================================================================================
672 SMESH_MeshObj::SMESH_MeshObj(SMESH::SMESH_Mesh_ptr theMesh):
673 myClient(SalomeApp_Application::orb(),theMesh)
677 MESSAGE("SMESH_MeshObj - this = "<<this<<"; theMesh->_is_nil() = "<<theMesh->_is_nil());
680 //=================================================================================
681 // function : ~SMESH_MeshObj
682 // purpose : Destructor
683 //=================================================================================
684 SMESH_MeshObj::~SMESH_MeshObj()
687 MESSAGE("SMESH_MeshObj - this = "<<this<<"\n");
689 myEmptyGrid->Delete();
692 //=================================================================================
694 // purpose : Update mesh and fill grid with new values if necessary
695 //=================================================================================
696 bool SMESH_MeshObj::Update( int theIsClear )
698 // Update SMDS_Mesh on client part
699 if ( MYDEBUG ) MESSAGE("SMESH_MeshObj::Update " << this);
700 if ( myClient.Update(theIsClear) || GetUnstructuredGrid()->GetNumberOfPoints()==0) {
701 if ( MYDEBUG ) MESSAGE("buildPrs");
702 buildPrs(); // Fill unstructured grid
708 bool SMESH_MeshObj::NulData()
710 if ( MYDEBUG ) MESSAGE ("SMESH_MeshObj::NulData() =============================================");
713 myEmptyGrid = SMDS_UnstructuredGrid::New();
714 myEmptyGrid->Initialize();
715 myEmptyGrid->Allocate();
716 vtkPoints* points = vtkPoints::New();
717 points->SetNumberOfPoints(0);
718 myEmptyGrid->SetPoints( points );
720 myEmptyGrid->BuildLinks();
722 myGrid->ShallowCopy(myEmptyGrid);
725 //=================================================================================
726 // function : GetElemDimension
727 // purpose : Get dimension of element
728 //=================================================================================
729 int SMESH_MeshObj::GetElemDimension( const int theObjId )
731 const SMDS_MeshElement* anElem = myClient->FindElement( theObjId );
735 int aType = anElem->GetType();
738 case SMDSAbs_0DElement : return 0;
739 case SMDSAbs_Ball : return 0;
740 case SMDSAbs_Edge : return 1;
741 case SMDSAbs_Face : return 2;
742 case SMDSAbs_Volume: return 3;
747 //=================================================================================
748 // function : GetEntities
749 // purpose : Get entities of specified type. Return number of entities
750 //=================================================================================
751 int SMESH_MeshObj::GetNbEntities( const SMDSAbs_ElementType theType) const
757 return myClient->NbNodes();
760 case SMDSAbs_0DElement:
762 return myClient->Nb0DElements();
766 return myClient->NbBalls();
771 return myClient->NbEdges();
776 return myClient->NbFaces();
781 return myClient->NbVolumes();
790 int SMESH_MeshObj::GetEntities( const SMDSAbs_ElementType theType, TEntityList& theObjs ) const
798 SMDS_NodeIteratorPtr anIter = myClient->nodesIterator();
799 while ( anIter->more() ) theObjs.push_back( anIter->next() );
802 case SMDSAbs_0DElement:
804 SMDS_ElemIteratorPtr anIter = myClient->elementsIterator(SMDSAbs_0DElement);
805 while ( anIter->more() ) theObjs.push_back( anIter->next() );
810 SMDS_ElemIteratorPtr anIter = myClient->elementGeomIterator(SMDSGeom_BALL);
811 while ( anIter->more() ) theObjs.push_back( anIter->next() );
816 SMDS_EdgeIteratorPtr anIter = myClient->edgesIterator();
817 while ( anIter->more() ) theObjs.push_back( anIter->next() );
822 SMDS_FaceIteratorPtr anIter = myClient->facesIterator();
823 while ( anIter->more() ) theObjs.push_back( anIter->next() );
828 SMDS_VolumeIteratorPtr anIter = myClient->volumesIterator();
829 while ( anIter->more() ) theObjs.push_back( anIter->next() );
836 return theObjs.size();
839 //=================================================================================
840 // function : UpdateFunctor
841 // purpose : Update functor in accordance with current mesh
842 //=================================================================================
843 void SMESH_MeshObj::UpdateFunctor( const SMESH::Controls::FunctorPtr& theFunctor )
845 theFunctor->SetMesh( GetMesh() );
848 //=================================================================================
849 // function : IsNodePrs
850 // purpose : Return true if node presentation is used
851 //=================================================================================
852 bool SMESH_MeshObj::IsNodePrs() const
854 return myClient->Nb0DElements() + myClient->NbEdges() + myClient->NbFaces() + myClient->NbVolumes() + myClient->NbBalls() == 0 ;
859 Class : SMESH_SubMeshObj
860 Description : Base class for visualisation of submeshes and groups
863 //=================================================================================
864 // function : SMESH_SubMeshObj
865 // purpose : Constructor
866 //=================================================================================
867 SMESH_SubMeshObj::SMESH_SubMeshObj( SMESH_MeshObj* theMeshObj )
869 if ( MYDEBUG ) MESSAGE( "SMESH_SubMeshObj - theMeshObj = " << theMeshObj );
871 myMeshObj = theMeshObj;
874 SMESH_SubMeshObj::~SMESH_SubMeshObj()
878 //=================================================================================
879 // function : GetElemDimension
880 // purpose : Get dimension of element
881 //=================================================================================
882 int SMESH_SubMeshObj::GetElemDimension( const int theObjId )
884 return myMeshObj == 0 ? 0 : myMeshObj->GetElemDimension( theObjId );
887 //=================================================================================
888 // function : UpdateFunctor
889 // purpose : Update functor in accordance with current mesh
890 //=================================================================================
892 void SMESH_SubMeshObj::UpdateFunctor( const SMESH::Controls::FunctorPtr& theFunctor )
894 theFunctor->SetMesh( myMeshObj->GetMesh() );
897 //=================================================================================
899 // purpose : Update mesh object and fill grid with new values
900 //=================================================================================
901 bool SMESH_SubMeshObj::Update( int theIsClear )
903 if ( MYDEBUG ) MESSAGE("SMESH_SubMeshObj::Update " << this)
904 bool changed = myMeshObj->Update( theIsClear );
911 Class : SMESH_GroupObj
912 Description : Class for visualisation of groups
915 //=================================================================================
916 // function : SMESH_GroupObj
917 // purpose : Constructor
918 //=================================================================================
919 SMESH_GroupObj::SMESH_GroupObj( SMESH::SMESH_GroupBase_ptr theGroup,
920 SMESH_MeshObj* theMeshObj )
921 : SMESH_SubMeshObj( theMeshObj ),
922 myGroupServer( SMESH::SMESH_GroupBase::_duplicate(theGroup) )
924 if ( MYDEBUG ) MESSAGE("SMESH_GroupObj - theGroup->_is_nil() = "<<theGroup->_is_nil());
925 myGroupServer->Register();
928 SMESH_GroupObj::~SMESH_GroupObj()
930 if ( MYDEBUG ) MESSAGE("~SMESH_GroupObj");
931 myGroupServer->UnRegister();
934 //=================================================================================
935 // function : IsNodePrs
936 // purpose : Return true if node presentation is used
937 //=================================================================================
938 bool SMESH_GroupObj::IsNodePrs() const
940 return myGroupServer->GetType() == SMESH::NODE;
943 //=================================================================================
944 // function : GetElementType
945 // purpose : Return type of elements of group
946 //=================================================================================
947 SMDSAbs_ElementType SMESH_GroupObj::GetElementType() const
949 return SMDSAbs_ElementType(myGroupServer->GetType());
952 //=================================================================================
953 // function : getNodesFromElems
954 // purpose : Retrieve nodes from elements
955 //=================================================================================
956 static int getNodesFromElems( SMESH::long_array_var& theElemIds,
957 const SMDS_Mesh* theMesh,
958 std::list<const SMDS_MeshElement*>& theResList )
960 set<const SMDS_MeshElement*> aNodeSet;
962 for ( CORBA::Long i = 0, n = theElemIds->length(); i < n; i++ )
964 const SMDS_MeshElement* anElem = theMesh->FindElement( theElemIds[ i ] );
967 SMDS_ElemIteratorPtr anIter = anElem->nodesIterator();
968 while ( anIter->more() )
970 const SMDS_MeshElement* aNode = anIter->next();
972 aNodeSet.insert( aNode );
977 set<const SMDS_MeshElement*>::const_iterator anIter;
978 for ( anIter = aNodeSet.begin(); anIter != aNodeSet.end(); ++anIter )
979 theResList.push_back( *anIter );
981 return theResList.size();
984 //=================================================================================
985 // function : getPointers
986 // purpose : Get std::list<const SMDS_MeshElement*> from list of IDs
987 //=================================================================================
988 static int getPointers( const SMDSAbs_ElementType theRequestType,
989 SMESH::long_array_var& theElemIds,
990 const SMDS_Mesh* theMesh,
991 std::list<const SMDS_MeshElement*>& theResList )
993 for ( CORBA::Long i = 0, n = theElemIds->length(); i < n; i++ )
995 const SMDS_MeshElement* anElem = theRequestType == SMDSAbs_Node
996 ? theMesh->FindNode( theElemIds[ i ] ) : theMesh->FindElement( theElemIds[ i ] );
999 theResList.push_back( anElem );
1002 return theResList.size();
1006 //=================================================================================
1007 // function : GetEntities
1008 // purpose : Get entities of specified type. Return number of entities
1009 //=================================================================================
1010 int SMESH_GroupObj::GetNbEntities( const SMDSAbs_ElementType theType) const
1012 if(SMDSAbs_ElementType(myGroupServer->GetType()) == theType) {
1013 return myGroupServer->Size();
1015 if ( theType == SMDSAbs_Node ) {
1016 return myGroupServer->GetNumberOfNodes();
1021 int SMESH_GroupObj::GetEntities( const SMDSAbs_ElementType theType, TEntityList& theResList ) const
1024 SMDS_Mesh* aMesh = myMeshObj->GetMesh();
1029 SMDSAbs_ElementType aGrpType = SMDSAbs_ElementType(myGroupServer->GetType());
1030 if ( aGrpType != theType && theType != SMDSAbs_Node )
1033 SMESH::long_array_var anIds = myGroupServer->GetListOfID();
1034 if ( anIds->length() == 0 )
1037 if ( aGrpType == theType )
1038 return getPointers( theType, anIds, aMesh, theResList );
1039 else if ( theType == SMDSAbs_Node )
1040 return getNodesFromElems( anIds, aMesh, theResList );
1048 Class : SMESH_subMeshObj
1049 Description : Class for visualisation of submeshes
1052 //=================================================================================
1053 // function : SMESH_subMeshObj
1054 // purpose : Constructor
1055 //=================================================================================
1056 SMESH_subMeshObj::SMESH_subMeshObj( SMESH::SMESH_subMesh_ptr theSubMesh,
1057 SMESH_MeshObj* theMeshObj )
1058 : SMESH_SubMeshObj( theMeshObj ),
1059 mySubMeshServer( SMESH::SMESH_subMesh::_duplicate( theSubMesh ) )
1061 if ( MYDEBUG ) MESSAGE( "SMESH_subMeshObj - theSubMesh->_is_nil() = " << theSubMesh->_is_nil() );
1063 mySubMeshServer->Register();
1066 SMESH_subMeshObj::~SMESH_subMeshObj()
1068 if ( MYDEBUG ) MESSAGE( "~SMESH_subMeshObj" );
1069 mySubMeshServer->UnRegister();
1072 //=================================================================================
1073 // function : GetEntities
1074 // purpose : Get entities of specified type. Return number of entities
1075 //=================================================================================
1076 int SMESH_subMeshObj::GetNbEntities( const SMDSAbs_ElementType theType) const
1082 return mySubMeshServer->GetNumberOfNodes( /*all=*/true );
1086 case SMDSAbs_0DElement:
1089 case SMDSAbs_Volume:
1091 SMESH::long_array_var anIds =
1092 mySubMeshServer->GetElementsByType( SMESH::ElementType(theType) );
1093 return anIds->length();
1101 int SMESH_subMeshObj::GetEntities( const SMDSAbs_ElementType theType, TEntityList& theResList ) const
1105 SMDS_Mesh* aMesh = myMeshObj->GetMesh();
1109 bool isNodal = IsNodePrs();
1113 if ( theType == SMDSAbs_Node )
1115 SMESH::long_array_var anIds = mySubMeshServer->GetNodesId();
1116 return getPointers( SMDSAbs_Node, anIds, aMesh, theResList );
1121 if ( theType == SMDSAbs_Node )
1123 SMESH::long_array_var anIds = mySubMeshServer->GetElementsId();
1124 return getNodesFromElems( anIds, aMesh, theResList );
1128 SMESH::long_array_var anIds =
1129 mySubMeshServer->GetElementsByType( SMESH::ElementType(theType) );
1130 return getPointers( theType, anIds, aMesh, theResList );
1137 //=================================================================================
1138 // function : IsNodePrs
1139 // purpose : Return true if node presentation is used
1140 //=================================================================================
1141 bool SMESH_subMeshObj::IsNodePrs() const
1143 return mySubMeshServer->GetNumberOfElements() == 0;