Salome HOME
Update from local sources
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_Selection.cxx
1
2 #include "SMESHGUI_Selection.h"
3 #include "SMESHGUI_Utils.h"
4 #include "SMESHGUI_VTKUtils.h"
5 #include "SMESHGUI_MeshUtils.h"
6 #include "SMESHGUI_GEOMGenUtils.h"
7
8 #include "SMESH_Type.h"
9 #include "SMESH_Actor.h"
10
11 #include "SalomeApp_SelectionMgr.h"
12 #include "SalomeApp_Study.h"
13 #include "SalomeApp_VTKSelector.h"
14
15 #include "SUIT_Session.h"
16
17 #include "SVTK_RenderWindowInteractor.h"
18 #include "SVTK_ViewWindow.h"
19
20 #include CORBA_SERVER_HEADER(SMESH_Mesh)
21 #include CORBA_SERVER_HEADER(SMESH_Group)
22
23 //=======================================================================
24 //function : SMESHGUI_Selection
25 //purpose  : 
26 //=======================================================================
27 SMESHGUI_Selection::SMESHGUI_Selection()
28 : SalomeApp_Selection()
29 {
30 }
31
32 //=======================================================================
33 //function : ~SMESHGUI_Selection
34 //purpose  : 
35 //=======================================================================
36 SMESHGUI_Selection::~SMESHGUI_Selection()
37 {
38 }
39
40 //=======================================================================
41 //function : init
42 //purpose  : 
43 //=======================================================================
44 void SMESHGUI_Selection::init( const QString& client, SalomeApp_SelectionMgr* mgr )
45 {
46   SalomeApp_Selection::init( client, mgr );
47
48   if( mgr && study() )
49   {
50     _PTR(Study) aStudy = study()->studyDS();
51
52     SUIT_DataOwnerPtrList sel;
53     mgr->selected( sel, client );
54     myDataOwners = sel;
55     SUIT_DataOwnerPtrList::const_iterator anIt = sel.begin(),
56                                           aLast = sel.end();
57     for( ; anIt!=aLast; anIt++ )
58     {
59       SUIT_DataOwner* owner = ( SUIT_DataOwner* )( (*anIt ).get() );
60       SalomeApp_DataOwner* sowner = dynamic_cast<SalomeApp_DataOwner*>( owner );
61       if( sowner )
62         myTypes.append( typeName( type( sowner, aStudy ) ) );
63       else
64         myTypes.append( "Unknown" );
65     }
66   }
67 }
68
69 //=======================================================================
70 //function : param
71 //purpose  : 
72 //=======================================================================
73 QtxValue SMESHGUI_Selection::param( const int ind, const QString& p ) const
74 {
75   QtxValue val;
76        if ( p=="client" )        val = QtxValue( globalParam( p ) );
77   else if ( p=="type" )          val = QtxValue( myTypes[ind] );
78   else if ( p=="elemTypes" )     val = QtxValue( elemTypes( ind ) );
79   else if ( p=="numberOfNodes" ) val = QtxValue( numberOfNodes( ind ) );
80   else if ( p=="labeledTypes" )  val = QtxValue( labeledTypes( ind ) );
81   else if ( p=="shrinkMode" )    val = QtxValue( shrinkMode( ind ) );
82   else if ( p=="entityMode" )    val = QtxValue( entityMode( ind ) );
83   else if ( p=="controlMode" )   val = QtxValue( controlMode( ind ) );
84   else if ( p=="displayMode" )   val = QtxValue( displayMode( ind ) );
85   else if ( p=="isComputable" )  val = QtxValue( isComputable( ind ) );
86   else if ( p=="hasReference" )  val = QtxValue( hasReference( ind ) );
87   else if ( p=="isVisible" )     val = QtxValue( isVisible( ind ) );
88
89 //   printf( "--> param() : [%s] = %s (%s)\n", p.latin1(), val.toString().latin1(), val.typeName() );
90 //   if ( val.type() == QVariant::List )
91 //     cout << "size: " << val.toList().count() << endl;
92   return val;
93 }
94
95 //=======================================================================
96 //function : getVtkOwner
97 //purpose  : 
98 //=======================================================================
99
100 SMESH_Actor* SMESHGUI_Selection::getActor( int ind ) const
101 {
102   if ( ind >= 0 && ind < myDataOwners.count() ) {
103     const SalomeApp_SVTKDataOwner* owner = 
104       dynamic_cast<const SalomeApp_SVTKDataOwner*> ( myDataOwners[ ind ].get() );
105     if ( owner )
106       return dynamic_cast<SMESH_Actor*>( owner->GetActor() );
107   }
108   return 0;
109 }
110
111 //=======================================================================
112 //function : elemTypes
113 //purpose  : may return {'Edge' 'Face' 'Volume'} at most
114 //=======================================================================
115
116 QValueList<QVariant> SMESHGUI_Selection::elemTypes( int ind ) const
117 {
118   QValueList<QVariant> types;
119   SMESH_Actor* actor = getActor( ind );
120   if ( actor ) {
121     TVisualObjPtr object = actor->GetObject();
122     if ( object ) {
123       if ( object->GetNbEntities( SMDSAbs_Edge )) types.append( "Edge" );
124       if ( object->GetNbEntities( SMDSAbs_Face )) types.append( "Face" );
125       if ( object->GetNbEntities( SMDSAbs_Volume )) types.append( "Volume" );
126     }
127   }
128   return types;
129 }
130
131 //=======================================================================
132 //function : labeledTypes
133 //purpose  : may return {'Point' 'Cell'} at most
134 //=======================================================================
135
136 QValueList<QVariant> SMESHGUI_Selection::labeledTypes( int ind ) const
137 {
138   QValueList<QVariant> types;
139   SMESH_Actor* actor = getActor( ind );
140   if ( actor ) {
141     if ( actor->GetPointsLabeled()) types.append( "Point" );
142     if ( actor->GetCellsLabeled()) types.append( "Cell" );
143   }
144   return types;
145 }
146
147 //=======================================================================
148 //function : displayMode
149 //purpose  : return SMESH_Actor::EReperesent
150 //=======================================================================
151
152 QString SMESHGUI_Selection::displayMode( int ind ) const
153 {
154   SMESH_Actor* actor = getActor( ind );
155   if ( actor ) {
156     switch( actor->GetRepresentation() ) {
157     case SMESH_Actor::eEdge:    return "eEdge";
158     case SMESH_Actor::eSurface: return "eSurface";
159     case SMESH_Actor::ePoint:   return "ePoint";
160     default:;
161     }
162   }
163   return "Unknown";
164 }
165
166 //=======================================================================
167 //function : shrinkMode
168 //purpose  : return either 'IsSrunk', 'IsNotShrunk' or 'IsNotShrinkable'
169 //=======================================================================
170
171 QString SMESHGUI_Selection::shrinkMode( int ind ) const
172 {
173   SMESH_Actor* actor = getActor( ind );
174   if ( actor && actor->IsShrunkable() ) {
175     if ( actor->IsShrunk() )
176       return "IsShrunk";
177     return "IsNotShrunk";
178   }
179   return "IsNotShrinkable";
180 }
181
182 //=======================================================================
183 //function : entityMode
184 //purpose  : may return {'Edge' 'Face' 'Volume'} at most
185 //=======================================================================
186
187 QValueList<QVariant> SMESHGUI_Selection::entityMode( int ind ) const
188 {
189   QValueList<QVariant> types;
190   SMESH_Actor* actor = getActor( ind );
191   if ( actor ) {
192     unsigned int aMode = actor->GetEntityMode();
193     if ( aMode & SMESH_Actor::eVolumes) types.append( "Volume");
194     if ( aMode & SMESH_Actor::eFaces  ) types.append( "Face"  );
195     if ( aMode & SMESH_Actor::eEdges  ) types.append( "Edge"  );
196   }
197   return types;
198 }
199
200 //=======================================================================
201 //function : controlMode
202 //purpose  : return SMESH_Actor::eControl
203 //=======================================================================
204
205 QString SMESHGUI_Selection::controlMode( int ind ) const
206 {
207   SMESH_Actor* actor = getActor( ind );
208   if ( actor ) {
209     switch( actor->GetControlMode() ) {
210     case SMESH_Actor::eLength:            return "eLength";
211     case SMESH_Actor::eLength2D:          return "eLength2D";
212     case SMESH_Actor::eFreeEdges:         return "eFreeEdges";
213     case SMESH_Actor::eFreeBorders:       return "eFreeBorders";
214     case SMESH_Actor::eMultiConnection:   return "eMultiConnection";
215     case SMESH_Actor::eMultiConnection2D: return "eMultiConnection2D";
216     case SMESH_Actor::eArea:              return "eArea";
217     case SMESH_Actor::eTaper:             return "eTaper";
218     case SMESH_Actor::eAspectRatio:       return "eAspectRatio";
219     case SMESH_Actor::eAspectRatio3D:     return "eAspectRatio3D";
220     case SMESH_Actor::eMinimumAngle:      return "eMinimumAngle";
221     case SMESH_Actor::eWarping:           return "eWarping";
222     case SMESH_Actor::eSkew:              return "eSkew";
223     default:;
224     }
225   }
226   return "eNone";
227 }
228
229 //=======================================================================
230 //function : numberOfNodes
231 //purpose  : 
232 //=======================================================================
233
234 int SMESHGUI_Selection::numberOfNodes( int ind ) const
235 {
236   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
237   {
238     CORBA::Object_var obj =
239       SMESH::DataOwnerToObject( static_cast<SalomeApp_DataOwner*>( myDataOwners[ ind ].get() ));
240     if ( ! CORBA::is_nil( obj )) {
241       SMESH::SMESH_Mesh_var mesh = SMESH::SMESH_Mesh::_narrow( obj );
242       if ( ! mesh->_is_nil() )
243         return mesh->NbNodes();
244       SMESH::SMESH_subMesh_var aSubMeshObj = SMESH::SMESH_subMesh::_narrow( obj );
245       if ( !aSubMeshObj->_is_nil() )
246         return aSubMeshObj->GetNumberOfNodes(true);
247       SMESH::SMESH_GroupBase_var aGroupObj = SMESH::SMESH_GroupBase::_narrow( obj );
248       if ( !aGroupObj->_is_nil() )
249         return aGroupObj->Size();
250     }
251   }
252   return 0;
253 }
254
255 //=======================================================================
256 //function : isComputable
257 //purpose  : 
258 //=======================================================================
259
260 QVariant SMESHGUI_Selection::isComputable( int ind ) const
261 {
262   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
263   {
264     Handle(SALOME_InteractiveObject) io =
265       static_cast<SalomeApp_DataOwner*>( myDataOwners[ ind ].get() )->IO();
266     if ( !io.IsNull() ) {
267       SMESH::SMESH_Mesh_var mesh = SMESH::GetMeshByIO(io) ; // m,sm,gr->m
268       if ( !mesh->_is_nil() ) {
269         _PTR(SObject) so = SMESH::FindSObject( mesh );
270         if ( so ) {
271           GEOM::GEOM_Object_var shape = SMESH::GetShapeOnMeshOrSubMesh( so );
272           return QVariant( !shape->_is_nil(), 0 );
273         }
274       }
275     }
276   }
277   return QVariant( false, 0 );
278 }
279
280 //=======================================================================
281 //function : hasReference
282 //purpose  : 
283 //=======================================================================
284
285 QVariant SMESHGUI_Selection::hasReference( int ind ) const
286 {
287   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
288   {
289     Handle(SALOME_InteractiveObject) io =
290       static_cast<SalomeApp_DataOwner*>( myDataOwners[ ind ].get() )->IO();
291     if ( !io.IsNull() )
292       return QVariant( io->hasReference(), 0 );
293   }
294   return QVariant( false, 0 );
295 }
296
297 //=======================================================================
298 //function : isVisible
299 //purpose  : 
300 //=======================================================================
301
302 QVariant SMESHGUI_Selection::isVisible( int ind ) const
303 {
304   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
305   {
306     QString entry = static_cast<SalomeApp_DataOwner*>( myDataOwners[ ind ].get() )->entry();
307     SMESH_Actor* actor = SMESH::FindActorByEntry( entry.latin1() );
308     if ( actor && actor->hasIO() ) {
309       SVTK_RenderWindowInteractor* renderInter = SMESH::GetCurrentVtkView()->getRWInteractor();
310       return QVariant( renderInter->isVisible( actor->getIO() ), 0 );
311     }
312   }
313   return QVariant( false, 0 );
314 }
315
316
317 //=======================================================================
318 //function : type
319 //purpose  : 
320 //=======================================================================
321
322 int SMESHGUI_Selection::type( SalomeApp_DataOwner* owner,
323                               _PTR(Study) study )
324 {
325   QString entry = owner->entry();
326
327   _PTR(SObject) obj (study->FindObjectID(entry.latin1()));
328   if( !obj )
329     return -1;
330
331   _PTR(SObject) objFather = obj->GetFather();
332   _PTR(SComponent) objComponent = obj->GetFatherComponent();
333
334   int aLevel = obj->Depth() - objComponent->Depth(),
335       aFTag = objFather->Tag(),
336       anOTag = obj->Tag(),
337       res = -1;
338
339   switch( aLevel )
340   {
341   case 1:
342     if( anOTag>=3 )
343       res = MESH;
344     break;
345   case 2:
346     switch( aFTag )
347     {
348     case 1:
349       res = HYPOTHESIS;
350       break;
351     case 2:
352       res = ALGORITHM;
353       break;
354     }
355     break;
356   case 3:
357     switch( aFTag )
358     {
359     case 4:
360       res = SUBMESH_VERTEX;
361       break;
362     case 5:
363       res = SUBMESH_EDGE;
364       break;
365     case 7:
366       res = SUBMESH_FACE;
367       break;
368     case 9:
369       res = SUBMESH_SOLID;
370       break;
371     case 10:
372       res = SUBMESH_COMPOUND;
373       break;
374     }
375     if( aFTag>10 )
376       res = GROUP;
377
378     break;
379   }
380
381   return res;
382 }
383
384 //=======================================================================
385 //function : typeName
386 //purpose  : 
387 //=======================================================================
388
389 QString SMESHGUI_Selection::typeName( const int t )
390 {
391   switch( t )
392   {
393   case HYPOTHESIS:
394     return "Hypothesis";
395   case ALGORITHM:
396     return "Algorithm";
397   case MESH:
398     return "Mesh";
399   case SUBMESH:
400     return "SubMesh";
401   case MESHorSUBMESH:
402     return "Mesh or submesh";
403   case SUBMESH_VERTEX:
404     return "Mesh vertex";
405   case SUBMESH_EDGE:
406     return "Mesh edge";
407   case SUBMESH_FACE:
408     return "Mesh face";
409   case SUBMESH_SOLID:
410     return "Mesh solid";
411   case SUBMESH_COMPOUND:
412     return "Mesh compound";
413   case GROUP:
414     return "Group";
415   default:
416     return "Unknown";
417   }
418 }