Salome HOME
a885c49b9fc54837b52778d1329f3984ae9d6b31
[modules/smesh.git] / src / OBJECT / SMESH_Object.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  SMESH OBJECT : interactive object for SMESH visualization
23 //  File   : SMESH_Grid.cxx
24 //  Author : Nicolas REJNERI
25 //  Module : SMESH
26 //
27 #include "SMESH_ObjectDef.h"
28 #include "SMESH_ActorUtils.h"
29
30 #include "SMDS_Mesh.hxx"
31 #include "SMDS_PolyhedralVolumeOfNodes.hxx"
32 #include "SMESH_Actor.h"
33 #include "SMESH_ControlsDef.hxx"
34 #include "SalomeApp_Application.h"
35 #include "VTKViewer_ExtractUnstructuredGrid.h"
36 #include "VTKViewer_CellLocationsArray.h"
37
38 #include CORBA_SERVER_HEADER(SMESH_Gen)
39 #include CORBA_SERVER_HEADER(SALOME_Exception)
40
41 #include <vtkCell.h>
42 #include <vtkIdList.h>
43 #include <vtkCellArray.h>
44 #include <vtkUnsignedCharArray.h>
45
46 #include <vtkUnstructuredGrid.h>
47
48 #include <memory>
49 #include <sstream>      
50 #include <stdexcept>
51 #include <set>
52
53 #include "utilities.h"
54
55 using namespace std;
56
57 #ifndef EXCEPTION
58 #define EXCEPTION(TYPE, MSG) {\
59   std::ostringstream aStream;\
60   aStream<<__FILE__<<"["<<__LINE__<<"]::"<<MSG;\
61   throw TYPE(aStream.str());\
62 }
63 #endif
64
65 #ifdef _DEBUG_
66 static int MYDEBUG = 0;
67 static int MYDEBUGWITHFILES = 0;
68 #else
69 static int MYDEBUG = 0;
70 static int MYDEBUGWITHFILES = 0;
71 #endif
72
73
74 /*
75   Class       : SMESH_VisualObjDef
76   Description : Base class for all mesh objects to be visuilised
77 */
78
79 //=================================================================================
80 // function : getCellType
81 // purpose  : Get type of VTK cell
82 //=================================================================================
83 static inline vtkIdType getCellType( const SMDSAbs_ElementType theType,
84                                      const bool thePoly,
85                                      const int theNbNodes )
86 {
87   switch( theType )
88   {
89     case SMDSAbs_Edge: 
90       if( theNbNodes == 2 )         return VTK_LINE;
91       else if ( theNbNodes == 3 )   return VTK_QUADRATIC_EDGE;
92       else return VTK_EMPTY_CELL;
93
94     case SMDSAbs_Face  :
95       if (thePoly && theNbNodes>2 ) return VTK_POLYGON;
96       else if ( theNbNodes == 3 )   return VTK_TRIANGLE;
97       else if ( theNbNodes == 4 )   return VTK_QUAD;
98       else if ( theNbNodes == 6 )   return VTK_QUADRATIC_TRIANGLE;
99       else if ( theNbNodes == 8 )   return VTK_QUADRATIC_QUAD;
100       else return VTK_EMPTY_CELL;
101       
102     case SMDSAbs_Volume:
103       if (thePoly && theNbNodes>3 ) return VTK_CONVEX_POINT_SET;
104       else if ( theNbNodes == 4 )   return VTK_TETRA;
105       else if ( theNbNodes == 5 )   return VTK_PYRAMID;
106       else if ( theNbNodes == 6 )   return VTK_WEDGE;
107       else if ( theNbNodes == 8 )   return VTK_HEXAHEDRON;
108       else if ( theNbNodes == 10 )  {
109         return VTK_QUADRATIC_TETRA;
110       }
111       else if ( theNbNodes == 20 )  {
112         return VTK_QUADRATIC_HEXAHEDRON;
113       }
114       else if ( theNbNodes == 15 )  {
115         return VTK_QUADRATIC_WEDGE;
116       }
117       else if ( theNbNodes==13 )  {
118         return VTK_CONVEX_POINT_SET;
119       }
120       else return VTK_EMPTY_CELL;
121
122     default: return VTK_EMPTY_CELL;
123   }
124 }
125
126 //=================================================================================
127 // functions : SMESH_VisualObjDef
128 // purpose   : Constructor
129 //=================================================================================
130 SMESH_VisualObjDef::SMESH_VisualObjDef()
131 {
132   myGrid = vtkUnstructuredGrid::New();
133 }
134 SMESH_VisualObjDef::~SMESH_VisualObjDef()
135 {
136   if ( MYDEBUG )
137     MESSAGE( "~SMESH_MeshObj - myGrid->GetReferenceCount() = " << myGrid->GetReferenceCount() );
138   myGrid->Delete();
139 }
140
141 //=================================================================================
142 // functions : GetNodeObjId, GetNodeVTKId, GetElemObjId, GetElemVTKId
143 // purpose   : Methods for retrieving VTK IDs by SMDS IDs and  vice versa
144 //=================================================================================
145 vtkIdType SMESH_VisualObjDef::GetNodeObjId( int theVTKID )
146 {
147   return myVTK2SMDSNodes.find(theVTKID) == myVTK2SMDSNodes.end() ? -1 : myVTK2SMDSNodes[theVTKID];
148 }
149
150 vtkIdType SMESH_VisualObjDef::GetNodeVTKId( int theObjID )
151 {
152   return mySMDS2VTKNodes.find(theObjID) == mySMDS2VTKNodes.end() ? -1 : mySMDS2VTKNodes[theObjID];
153 }
154
155 vtkIdType SMESH_VisualObjDef::GetElemObjId( int theVTKID )
156 {
157   return myVTK2SMDSElems.find(theVTKID) == myVTK2SMDSElems.end() ? -1 : myVTK2SMDSElems[theVTKID];
158 }
159
160 vtkIdType SMESH_VisualObjDef::GetElemVTKId( int theObjID )
161 {
162   return mySMDS2VTKElems.find(theObjID) == mySMDS2VTKElems.end() ? -1 : mySMDS2VTKElems[theObjID];
163 }
164
165 //=================================================================================
166 // function : SMESH_VisualObjDef::createPoints
167 // purpose  : Create points from nodes
168 //=================================================================================
169 void SMESH_VisualObjDef::createPoints( vtkPoints* thePoints )
170 {
171   if ( thePoints == 0 )
172     return;
173
174   TEntityList aNodes;
175   vtkIdType nbNodes = GetEntities( SMDSAbs_Node, aNodes );
176   thePoints->SetNumberOfPoints( nbNodes );
177   
178   int nbPoints = 0;
179
180   TEntityList::const_iterator anIter;
181   for ( anIter = aNodes.begin(); anIter != aNodes.end(); ++anIter )
182   {
183     const SMDS_MeshNode* aNode = ( const SMDS_MeshNode* )(*anIter);
184     if ( aNode != 0 )
185     {
186       thePoints->SetPoint( nbPoints, aNode->X(), aNode->Y(), aNode->Z() );
187       int anId = aNode->GetID();
188       mySMDS2VTKNodes.insert( TMapOfIds::value_type( anId, nbPoints ) );
189       myVTK2SMDSNodes.insert( TMapOfIds::value_type( nbPoints, anId ) );
190       nbPoints++;
191     }
192   }
193
194   if ( nbPoints != nbNodes )
195     thePoints->SetNumberOfPoints( nbPoints );
196 }
197
198 //=================================================================================
199 // function : buildPrs
200 // purpose  : create VTK cells( fill unstructured grid )
201 //=================================================================================
202 void SMESH_VisualObjDef::buildPrs()
203 {
204   try 
205   {
206     mySMDS2VTKNodes.clear();
207     myVTK2SMDSNodes.clear();
208     mySMDS2VTKElems.clear();
209     myVTK2SMDSElems.clear();
210     
211     if ( IsNodePrs() )
212       buildNodePrs();
213     else
214       buildElemPrs();
215   }
216   catch(...)
217   {
218     mySMDS2VTKNodes.clear();
219     myVTK2SMDSNodes.clear();
220     mySMDS2VTKElems.clear();
221     myVTK2SMDSElems.clear();
222
223     myGrid->SetPoints( 0 );
224     myGrid->SetCells( 0, 0, 0 );
225     throw;
226   }
227   
228   if( MYDEBUG ) MESSAGE( "Update - myGrid->GetNumberOfCells() = "<<myGrid->GetNumberOfCells() );
229   if( MYDEBUGWITHFILES ) SMESH::WriteUnstructuredGrid( myGrid,"/tmp/buildPrs" );
230 }
231
232 //=================================================================================
233 // function : buildNodePrs
234 // purpose  : create VTK cells for nodes
235 //=================================================================================
236 void SMESH_VisualObjDef::buildNodePrs()
237 {
238   // PAL16631: without swap, bad_alloc is not thrown but hung up and crash instead,
239   // so check remaining memory size for safety
240   SMDS_Mesh::CheckMemory(); // PAL16631
241   vtkPoints* aPoints = vtkPoints::New();
242   createPoints( aPoints );
243   SMDS_Mesh::CheckMemory();
244   myGrid->SetPoints( aPoints );
245   aPoints->Delete();
246
247   myGrid->SetCells( 0, 0, 0 );
248 }
249
250 //=================================================================================
251 // function : buildElemPrs
252 // purpose  : Create VTK cells for elements
253 //=================================================================================
254
255 namespace{
256   typedef std::vector<const SMDS_MeshElement*> TConnect;
257
258   int GetConnect(const SMDS_ElemIteratorPtr& theNodesIter, 
259                  TConnect& theConnect)
260   {
261     theConnect.clear();
262     for(; theNodesIter->more();)
263       theConnect.push_back(theNodesIter->next());
264     return theConnect.size();
265   }
266   
267   inline 
268   void SetId(vtkIdList *theIdList, 
269              const SMESH_VisualObjDef::TMapOfIds& theSMDS2VTKNodes, 
270              const TConnect& theConnect, 
271              int thePosition,
272              int theId)
273   {
274     theIdList->SetId(thePosition,theSMDS2VTKNodes.find(theConnect[theId]->GetID())->second);
275   }
276
277 }
278
279
280 void SMESH_VisualObjDef::buildElemPrs()
281 {
282   // Create points
283   
284   vtkPoints* aPoints = vtkPoints::New();
285   createPoints( aPoints );
286   myGrid->SetPoints( aPoints );
287   aPoints->Delete();
288     
289   if ( MYDEBUG )
290     MESSAGE("Update - myGrid->GetNumberOfPoints() = "<<myGrid->GetNumberOfPoints());
291
292   // Calculate cells size
293
294   static SMDSAbs_ElementType aTypes[ 3 ] = { SMDSAbs_Edge, SMDSAbs_Face, SMDSAbs_Volume };
295
296   // get entity data
297   map<SMDSAbs_ElementType,int> nbEnts;
298   map<SMDSAbs_ElementType,TEntityList> anEnts;
299
300   for ( int i = 0; i <= 2; i++ )
301     nbEnts[ aTypes[ i ] ] = GetEntities( aTypes[ i ], anEnts[ aTypes[ i ] ] );
302
303   // PAL16631: without swap, bad_alloc is not thrown but hung up and crash instead,
304   // so check remaining memory size for safety
305   SMDS_Mesh::CheckMemory(); // PAL16631
306
307   vtkIdType aCellsSize =  3 * nbEnts[ SMDSAbs_Edge ];
308
309   for ( int i = 1; i <= 2; i++ ) // iterate through faces and volumes
310   {
311     if ( nbEnts[ aTypes[ i ] ] )
312     {
313       const TEntityList& aList = anEnts[ aTypes[ i ] ];
314       TEntityList::const_iterator anIter;
315       for ( anIter = aList.begin(); anIter != aList.end(); ++anIter )
316         aCellsSize += (*anIter)->NbNodes() + 1;
317     }
318   }
319
320   vtkIdType aNbCells = nbEnts[ SMDSAbs_Edge ] + nbEnts[ SMDSAbs_Face ] + nbEnts[ SMDSAbs_Volume ];
321   
322   if ( MYDEBUG )
323     MESSAGE( "Update - aNbCells = "<<aNbCells<<"; aCellsSize = "<<aCellsSize );
324
325   // Create cells
326   
327   vtkCellArray* aConnectivity = vtkCellArray::New();
328   aConnectivity->Allocate( aCellsSize, 0 );
329   
330   SMDS_Mesh::CheckMemory(); // PAL16631
331
332   vtkUnsignedCharArray* aCellTypesArray = vtkUnsignedCharArray::New();
333   aCellTypesArray->SetNumberOfComponents( 1 );
334   aCellTypesArray->Allocate( aNbCells * aCellTypesArray->GetNumberOfComponents() );
335   
336   SMDS_Mesh::CheckMemory(); // PAL16631
337
338   vtkIdList *anIdList = vtkIdList::New();
339   vtkIdType iElem = 0;
340
341   TConnect aConnect;
342   aConnect.reserve(VTK_CELL_SIZE);
343
344   SMDS_Mesh::CheckMemory(); // PAL16631
345
346   for ( int i = 0; i <= 2; i++ ) // iterate through edges, faces and volumes
347   {
348     if( nbEnts[ aTypes[ i ] ] > 0 )
349     {
350       const SMDSAbs_ElementType& aType = aTypes[ i ];
351       const TEntityList& aList = anEnts[ aType ];
352       TEntityList::const_iterator anIter;
353       for ( anIter = aList.begin(); anIter != aList.end(); ++anIter )
354       {
355         const SMDS_MeshElement* anElem = *anIter;
356         
357         vtkIdType aNbNodes = anElem->NbNodes();
358         anIdList->SetNumberOfIds( aNbNodes );
359
360         int anId = anElem->GetID();
361
362         mySMDS2VTKElems.insert( TMapOfIds::value_type( anId, iElem ) );
363         myVTK2SMDSElems.insert( TMapOfIds::value_type( iElem, anId ) );
364
365         SMDS_ElemIteratorPtr aNodesIter = anElem->nodesIterator();
366         switch(aType){
367         case SMDSAbs_Volume:{
368           aConnect.clear();
369           std::vector<int> aConnectivities;
370           // Convertions connectivities from SMDS to VTK
371           if (anElem->IsPoly() && aNbNodes > 3) { // POLYEDRE
372
373             if ( const SMDS_PolyhedralVolumeOfNodes* ph =
374                  dynamic_cast<const SMDS_PolyhedralVolumeOfNodes*> (anElem))
375             {
376               aNbNodes = GetConnect(ph->uniqueNodesIterator(),aConnect);
377               anIdList->SetNumberOfIds( aNbNodes );
378             }
379             for (int k = 0; k < aNbNodes; k++)
380               aConnectivities.push_back(k);
381
382           } else if (aNbNodes == 4) {
383             static int anIds[] = {0,2,1,3};
384             for (int k = 0; k < aNbNodes; k++) aConnectivities.push_back(anIds[k]);
385
386           } else if (aNbNodes == 5) {
387             static int anIds[] = {0,3,2,1,4};
388             for (int k = 0; k < aNbNodes; k++) aConnectivities.push_back(anIds[k]);
389
390           } else if (aNbNodes == 6) {
391             static int anIds[] = {0,1,2,3,4,5};
392             for (int k = 0; k < aNbNodes; k++) aConnectivities.push_back(anIds[k]);
393
394           }
395           else if (aNbNodes == 8) {
396             static int anIds[] = {0,3,2,1,4,7,6,5};
397             for (int k = 0; k < aNbNodes; k++) aConnectivities.push_back(anIds[k]);
398
399           }
400           else if (aNbNodes == 10) {
401             static int anIds[] = {0,2,1,3,6,5,4,7,9,8};
402             for (int k = 0; k < aNbNodes; k++) aConnectivities.push_back(anIds[k]);
403           }
404           else if (aNbNodes == 13) {
405             static int anIds[] = {0,3,2,1,4,8,7,6,5,9,12,11,10};
406             for (int k = 0; k < aNbNodes; k++) aConnectivities.push_back(anIds[k]);
407           }
408           else if (aNbNodes == 15) {
409             //static int anIds[] = {0,2,1,3,5,4,8,7,6,11,10,9,12,14,13};
410             static int anIds[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14};
411             for (int k = 0; k < aNbNodes; k++) aConnectivities.push_back(anIds[k]);
412             //for (int k = 0; k < aNbNodes; k++) {
413             //  int nn = aConnectivities[k];
414             //  const SMDS_MeshNode* N = static_cast<const SMDS_MeshNode*> (aConnect[nn]);
415             //  cout<<"k="<<k<<"  N("<<N->X()<<","<<N->Y()<<","<<N->Z()<<")"<<endl;
416             //}
417           }
418           else if (aNbNodes == 20) {
419             static int anIds[] = {0,3,2,1,4,7,6,5,11,10,9,8,15,14,13,12,16,19,18,17};
420             for (int k = 0; k < aNbNodes; k++) aConnectivities.push_back(anIds[k]);
421           }
422           else {
423           }
424
425           if ( aConnect.empty() )
426             GetConnect(aNodesIter,aConnect);
427
428           if (aConnectivities.size() > 0) {
429             for (vtkIdType aNodeId = 0; aNodeId < aNbNodes; aNodeId++)
430               SetId(anIdList,mySMDS2VTKNodes,aConnect,aNodeId,aConnectivities[aNodeId]);
431           }
432           break;
433         }
434         default:
435           for( vtkIdType aNodeId = 0; aNodesIter->more(); aNodeId++ ){
436             const SMDS_MeshElement* aNode = aNodesIter->next();
437             anIdList->SetId( aNodeId, mySMDS2VTKNodes[aNode->GetID()] );
438           }
439         }
440
441         aConnectivity->InsertNextCell( anIdList );
442         aCellTypesArray->InsertNextValue( getCellType( aType, anElem->IsPoly(), aNbNodes ) );
443
444         iElem++;
445       }
446     }
447     SMDS_Mesh::CheckMemory(); // PAL16631
448   }
449
450   // Insert cells in grid
451   
452   VTKViewer_CellLocationsArray* aCellLocationsArray = VTKViewer_CellLocationsArray::New();
453   aCellLocationsArray->SetNumberOfComponents( 1 );
454   aCellLocationsArray->SetNumberOfTuples( aNbCells );
455   
456   SMDS_Mesh::CheckMemory(); // PAL16631
457
458   aConnectivity->InitTraversal();
459   for( vtkIdType idType = 0, *pts, npts; aConnectivity->GetNextCell( npts, pts ); idType++ )
460     aCellLocationsArray->SetValue( idType, aConnectivity->GetTraversalLocation( npts ) );
461
462   myGrid->SetCells( aCellTypesArray, aCellLocationsArray,aConnectivity );
463   
464   aCellLocationsArray->Delete();
465   aCellTypesArray->Delete();
466   aConnectivity->Delete();
467   anIdList->Delete();
468
469   SMDS_Mesh::CheckMemory(); // PAL16631
470 }
471
472 //=================================================================================
473 // function : GetEdgeNodes
474 // purpose  : Retrieve ids of nodes from edge of elements ( edge is numbered from 1 )
475 //=================================================================================
476 bool SMESH_VisualObjDef::GetEdgeNodes( const int theElemId,
477                                        const int theEdgeNum,
478                                        int&      theNodeId1,
479                                        int&      theNodeId2 ) const
480 {
481   const SMDS_Mesh* aMesh = GetMesh();
482   if ( aMesh == 0 )
483     return false;
484     
485   const SMDS_MeshElement* anElem = aMesh->FindElement( theElemId );
486   if ( anElem == 0 )
487     return false;
488     
489   int nbNodes = anElem->NbNodes();
490
491   if ( theEdgeNum < 0 || theEdgeNum > 3 || nbNodes != 3 && nbNodes != 4 || theEdgeNum > nbNodes )
492     return false;
493
494   vector<int> anIds( nbNodes );
495   SMDS_ElemIteratorPtr anIter = anElem->nodesIterator();
496   int i = 0;
497   while( anIter->more() )
498     anIds[ i++ ] = anIter->next()->GetID();
499
500   if ( theEdgeNum < nbNodes - 1 )
501   {
502     theNodeId1 = anIds[ theEdgeNum ];
503     theNodeId2 = anIds[ theEdgeNum + 1 ];
504   }
505   else
506   {
507     theNodeId1 = anIds[ nbNodes - 1 ];
508     theNodeId2 = anIds[ 0 ];
509   }
510
511   return true;
512 }
513
514 /*
515   Class       : SMESH_MeshObj
516   Description : Class for visualisation of mesh
517 */
518
519 //=================================================================================
520 // function : SMESH_MeshObj
521 // purpose  : Constructor
522 //=================================================================================
523 SMESH_MeshObj::SMESH_MeshObj(SMESH::SMESH_Mesh_ptr theMesh):
524   myClient(SalomeApp_Application::orb(),theMesh)
525 {
526   if ( MYDEBUG ) 
527     MESSAGE("SMESH_MeshObj - this = "<<this<<"; theMesh->_is_nil() = "<<theMesh->_is_nil());
528 }
529
530 //=================================================================================
531 // function : ~SMESH_MeshObj
532 // purpose  : Destructor
533 //=================================================================================
534 SMESH_MeshObj::~SMESH_MeshObj()
535 {
536   if ( MYDEBUG ) 
537     MESSAGE("SMESH_MeshObj - this = "<<this<<"\n");
538 }
539
540 //=================================================================================
541 // function : Update
542 // purpose  : Update mesh and fill grid with new values if necessary 
543 //=================================================================================
544 bool SMESH_MeshObj::Update( int theIsClear )
545 {
546   // Update SMDS_Mesh on client part
547   if ( myClient.Update(theIsClear) || GetUnstructuredGrid()->GetNumberOfPoints()==0) {
548     buildPrs();  // Fill unstructured grid
549     return true;
550   }
551   return false;
552 }
553
554 //=================================================================================
555 // function : GetElemDimension
556 // purpose  : Get dimension of element
557 //=================================================================================
558 int SMESH_MeshObj::GetElemDimension( const int theObjId )
559 {
560   const SMDS_MeshElement* anElem = myClient->FindElement( theObjId );
561   if ( anElem == 0 )
562     return 0;
563
564   int aType = anElem->GetType();
565   switch ( aType )
566   {
567     case SMDSAbs_Edge  : return 1;
568     case SMDSAbs_Face  : return 2;
569     case SMDSAbs_Volume: return 3;
570     default            : return 0;
571   }
572 }
573
574 //=================================================================================
575 // function : GetEntities
576 // purpose  : Get entities of specified type. Return number of entities
577 //=================================================================================
578 int SMESH_MeshObj::GetNbEntities( const SMDSAbs_ElementType theType) const
579 {
580   switch ( theType )
581   {
582     case SMDSAbs_Node:
583     {
584       return myClient->NbNodes();
585     }
586     break;
587     case SMDSAbs_Edge:
588     {
589       return myClient->NbEdges();
590     }
591     break;
592     case SMDSAbs_Face:
593     {
594       return myClient->NbFaces();
595     }
596     break;
597     case SMDSAbs_Volume:
598     {
599       return myClient->NbVolumes();
600     }
601     break;
602     default:
603       return 0;
604     break;
605   }
606 }
607
608 int SMESH_MeshObj::GetEntities( const SMDSAbs_ElementType theType, TEntityList& theObjs ) const
609 {
610   theObjs.clear();
611
612   switch ( theType )
613   {
614     case SMDSAbs_Node:
615     {
616       SMDS_NodeIteratorPtr anIter = myClient->nodesIterator();
617       while ( anIter->more() ) theObjs.push_back( anIter->next() );
618     }
619     break;
620     case SMDSAbs_Edge:
621     {
622       SMDS_EdgeIteratorPtr anIter = myClient->edgesIterator();
623       while ( anIter->more() ) theObjs.push_back( anIter->next() );
624     }
625     break;
626     case SMDSAbs_Face:
627     {
628       SMDS_FaceIteratorPtr anIter = myClient->facesIterator();
629       while ( anIter->more() ) theObjs.push_back( anIter->next() );
630     }
631     break;
632     case SMDSAbs_Volume:
633     {
634       SMDS_VolumeIteratorPtr anIter = myClient->volumesIterator();
635       while ( anIter->more() ) theObjs.push_back( anIter->next() );
636     }
637     break;
638     default:
639     break;
640   }
641
642   return theObjs.size();
643 }
644
645 //=================================================================================
646 // function : UpdateFunctor
647 // purpose  : Update functor in accordance with current mesh
648 //=================================================================================
649 void SMESH_MeshObj::UpdateFunctor( const SMESH::Controls::FunctorPtr& theFunctor )
650 {
651   theFunctor->SetMesh( GetMesh() );
652 }
653
654 //=================================================================================
655 // function : IsNodePrs
656 // purpose  : Return true if node presentation is used
657 //=================================================================================
658 bool SMESH_MeshObj::IsNodePrs() const
659 {
660   return myClient->NbEdges() == 0 &&myClient->NbFaces() == 0 && myClient->NbVolumes() == 0 ;
661 }
662
663
664 /*
665   Class       : SMESH_SubMeshObj
666   Description : Base class for visualisation of submeshes and groups
667 */
668
669 //=================================================================================
670 // function : SMESH_SubMeshObj
671 // purpose  : Constructor
672 //=================================================================================
673 SMESH_SubMeshObj::SMESH_SubMeshObj( SMESH_MeshObj* theMeshObj )
674 {
675   if ( MYDEBUG ) MESSAGE( "SMESH_SubMeshObj - theMeshObj = " << theMeshObj );
676   
677   myMeshObj = theMeshObj;
678 }
679
680 SMESH_SubMeshObj::~SMESH_SubMeshObj()
681 {
682 }
683
684 //=================================================================================
685 // function : GetElemDimension
686 // purpose  : Get dimension of element
687 //=================================================================================
688 int SMESH_SubMeshObj::GetElemDimension( const int theObjId )
689 {
690   return myMeshObj == 0 ? 0 : myMeshObj->GetElemDimension( theObjId );
691 }
692
693 //=================================================================================
694 // function : UpdateFunctor
695 // purpose  : Update functor in accordance with current mesh
696 //=================================================================================
697 void SMESH_SubMeshObj::UpdateFunctor( const SMESH::Controls::FunctorPtr& theFunctor )
698 {
699   theFunctor->SetMesh( myMeshObj->GetMesh() );
700 }
701
702 //=================================================================================
703 // function : Update
704 // purpose  : Update mesh object and fill grid with new values 
705 //=================================================================================
706 bool SMESH_SubMeshObj::Update( int theIsClear )
707 {
708   bool changed = myMeshObj->Update( theIsClear );
709   buildPrs();
710   return changed;
711 }
712
713
714 /*
715   Class       : SMESH_GroupObj
716   Description : Class for visualisation of groups
717 */
718
719 //=================================================================================
720 // function : SMESH_GroupObj
721 // purpose  : Constructor
722 //=================================================================================
723 SMESH_GroupObj::SMESH_GroupObj( SMESH::SMESH_GroupBase_ptr theGroup, 
724                                 SMESH_MeshObj*             theMeshObj )
725 : SMESH_SubMeshObj( theMeshObj ),
726   myGroupServer( SMESH::SMESH_GroupBase::_duplicate(theGroup) )
727 {
728   if ( MYDEBUG ) MESSAGE("SMESH_GroupObj - theGroup->_is_nil() = "<<theGroup->_is_nil());
729   myGroupServer->Register();
730 }
731
732 SMESH_GroupObj::~SMESH_GroupObj()
733 {
734   if ( MYDEBUG ) MESSAGE("~SMESH_GroupObj");
735   myGroupServer->Destroy();
736 }
737
738 //=================================================================================
739 // function : IsNodePrs
740 // purpose  : Return true if node presentation is used
741 //=================================================================================
742 bool SMESH_GroupObj::IsNodePrs() const
743 {
744   return myGroupServer->GetType() == SMESH::NODE;
745 }
746
747 //=================================================================================
748 // function : getNodesFromElems
749 // purpose  : Retrieve nodes from elements
750 //=================================================================================
751 static int getNodesFromElems( SMESH::long_array_var&              theElemIds,
752                               const SMDS_Mesh*                    theMesh,
753                               std::list<const SMDS_MeshElement*>& theResList )
754 {
755   set<const SMDS_MeshElement*> aNodeSet;
756
757   for ( CORBA::Long i = 0, n = theElemIds->length(); i < n; i++ )
758   {
759     const SMDS_MeshElement* anElem = theMesh->FindElement( theElemIds[ i ] );
760     if ( anElem != 0 )
761     {
762       SMDS_ElemIteratorPtr anIter = anElem->nodesIterator();
763       while ( anIter->more() )
764       {
765         const SMDS_MeshElement* aNode = anIter->next();
766         if ( aNode != 0 )
767           aNodeSet.insert( aNode );
768       }
769     }
770   }
771
772   set<const SMDS_MeshElement*>::const_iterator anIter;
773   for ( anIter = aNodeSet.begin(); anIter != aNodeSet.end(); ++anIter )
774     theResList.push_back( *anIter );
775
776   return theResList.size();    
777 }
778
779 //=================================================================================
780 // function : getPointers
781 // purpose  : Get std::list<const SMDS_MeshElement*> from list of IDs
782 //=================================================================================
783 static int getPointers( const SMDSAbs_ElementType            theRequestType,
784                         SMESH::long_array_var&              theElemIds,
785                         const SMDS_Mesh*                    theMesh,
786                         std::list<const SMDS_MeshElement*>& theResList )
787 {
788   for ( CORBA::Long i = 0, n = theElemIds->length(); i < n; i++ )
789   {
790     const SMDS_MeshElement* anElem = theRequestType == SMDSAbs_Node
791       ? theMesh->FindNode( theElemIds[ i ] ) : theMesh->FindElement( theElemIds[ i ] );
792
793     if ( anElem != 0 )
794       theResList.push_back( anElem );
795   }
796
797   return theResList.size();
798 }
799
800
801 //=================================================================================
802 // function : GetEntities
803 // purpose  : Get entities of specified type. Return number of entities
804 //=================================================================================
805 int SMESH_GroupObj::GetNbEntities( const SMDSAbs_ElementType theType) const
806 {
807   if(SMDSAbs_ElementType(myGroupServer->GetType()) == theType){
808     return myGroupServer->Size();
809   }
810   return 0;
811 }
812
813 int SMESH_GroupObj::GetEntities( const SMDSAbs_ElementType theType, TEntityList& theResList ) const
814 {
815   theResList.clear();
816   SMDS_Mesh* aMesh = myMeshObj->GetMesh();
817   
818   if ( myGroupServer->Size() == 0 || aMesh == 0 )
819     return 0;
820
821   SMDSAbs_ElementType aGrpType = SMDSAbs_ElementType(myGroupServer->GetType());
822   SMESH::long_array_var anIds = myGroupServer->GetListOfID();
823
824   if ( aGrpType == theType )
825     return getPointers( theType, anIds, aMesh, theResList );
826   else if ( theType == SMDSAbs_Node )
827     return getNodesFromElems( anIds, aMesh, theResList );
828   else
829     return 0;
830 }    
831
832
833
834 /*
835   Class       : SMESH_subMeshObj
836   Description : Class for visualisation of submeshes
837 */
838
839 //=================================================================================
840 // function : SMESH_subMeshObj
841 // purpose  : Constructor
842 //=================================================================================
843 SMESH_subMeshObj::SMESH_subMeshObj( SMESH::SMESH_subMesh_ptr theSubMesh,
844                                     SMESH_MeshObj*           theMeshObj )
845 : SMESH_SubMeshObj( theMeshObj ),
846   mySubMeshServer( SMESH::SMESH_subMesh::_duplicate( theSubMesh ) )
847 {
848   if ( MYDEBUG ) MESSAGE( "SMESH_subMeshObj - theSubMesh->_is_nil() = " << theSubMesh->_is_nil() );
849   
850   mySubMeshServer->Register();
851 }
852
853 SMESH_subMeshObj::~SMESH_subMeshObj()
854 {
855   if ( MYDEBUG ) MESSAGE( "~SMESH_subMeshObj" );
856   mySubMeshServer->Destroy();
857 }
858
859 //=================================================================================
860 // function : GetEntities
861 // purpose  : Get entities of specified type. Return number of entities
862 //=================================================================================
863 int SMESH_subMeshObj::GetNbEntities( const SMDSAbs_ElementType theType) const
864 {
865   switch ( theType )
866   {
867     case SMDSAbs_Node:
868     {
869       return mySubMeshServer->GetNumberOfNodes( false );
870     }
871     break;
872     case SMDSAbs_Edge:
873     case SMDSAbs_Face:
874     case SMDSAbs_Volume:
875     {
876       SMESH::long_array_var anIds = 
877         mySubMeshServer->GetElementsByType( SMESH::ElementType(theType) );
878       return anIds->length();
879     }
880     default:
881       return 0;
882     break;
883   }
884 }
885
886 int SMESH_subMeshObj::GetEntities( const SMDSAbs_ElementType theType, TEntityList& theResList ) const
887 {
888   theResList.clear();
889
890   SMDS_Mesh* aMesh = myMeshObj->GetMesh();
891   if ( aMesh == 0 )
892     return 0;
893
894   bool isNodal = IsNodePrs();
895
896   if ( isNodal )
897   {
898     if ( theType == SMDSAbs_Node )
899     {
900       SMESH::long_array_var anIds = mySubMeshServer->GetNodesId();
901       return getPointers( SMDSAbs_Node, anIds, aMesh, theResList );
902     }
903   }
904   else
905   {
906     if ( theType == SMDSAbs_Node )
907     {
908       SMESH::long_array_var anIds = mySubMeshServer->GetElementsId();
909       return getNodesFromElems( anIds, aMesh, theResList );
910     }
911     else
912     {
913       SMESH::long_array_var anIds = 
914         mySubMeshServer->GetElementsByType( SMESH::ElementType(theType) );
915       return getPointers( theType, anIds, aMesh, theResList );
916     }
917   }
918
919   return 0;
920 }
921
922 //=================================================================================
923 // function : IsNodePrs
924 // purpose  : Return true if node presentation is used
925 //=================================================================================
926 bool SMESH_subMeshObj::IsNodePrs() const
927 {
928   return mySubMeshServer->GetNumberOfElements() == 0;
929 }