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