Salome HOME
IPAL9193 (Jun 16)
[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_DataOwner* owner = 
104       dynamic_cast<const SalomeApp_DataOwner*> ( myDataOwners[ ind ].get() );
105     if ( owner )
106       {
107         Handle( SALOME_InteractiveObject ) anObj = owner->IO();
108         QString entry = anObj->getEntry();
109         return dynamic_cast<SMESH_Actor*>( SMESH::FindActorByEntry( entry ) );
110       }
111       //return dynamic_cast<SMESH_Actor*>( owner->GetActor() );
112   }
113   return 0;
114 }
115
116 //=======================================================================
117 //function : elemTypes
118 //purpose  : may return {'Edge' 'Face' 'Volume'} at most
119 //=======================================================================
120
121 QValueList<QVariant> SMESHGUI_Selection::elemTypes( int ind ) const
122 {
123   QValueList<QVariant> types;
124   SMESH_Actor* actor = getActor( ind );
125   if ( actor ) {
126     TVisualObjPtr object = actor->GetObject();
127     if ( object ) {
128       if ( object->GetNbEntities( SMDSAbs_Edge )) types.append( "Edge" );
129       if ( object->GetNbEntities( SMDSAbs_Face )) types.append( "Face" );
130       if ( object->GetNbEntities( SMDSAbs_Volume )) types.append( "Volume" );
131     }
132   }
133   return types;
134 }
135
136 //=======================================================================
137 //function : labeledTypes
138 //purpose  : may return {'Point' 'Cell'} at most
139 //=======================================================================
140
141 QValueList<QVariant> SMESHGUI_Selection::labeledTypes( int ind ) const
142 {
143   QValueList<QVariant> types;
144   SMESH_Actor* actor = getActor( ind );
145   if ( actor ) {
146     if ( actor->GetPointsLabeled()) types.append( "Point" );
147     if ( actor->GetCellsLabeled()) types.append( "Cell" );
148   }
149   return types;
150 }
151
152 //=======================================================================
153 //function : displayMode
154 //purpose  : return SMESH_Actor::EReperesent
155 //=======================================================================
156
157 QString SMESHGUI_Selection::displayMode( int ind ) const
158 {
159   SMESH_Actor* actor = getActor( ind );
160   if ( actor ) {
161     switch( actor->GetRepresentation() ) {
162     case SMESH_Actor::eEdge:    return "eEdge";
163     case SMESH_Actor::eSurface: return "eSurface";
164     case SMESH_Actor::ePoint:   return "ePoint";
165     default:;
166     }
167   }
168   return "Unknown";
169 }
170
171 //=======================================================================
172 //function : shrinkMode
173 //purpose  : return either 'IsSrunk', 'IsNotShrunk' or 'IsNotShrinkable'
174 //=======================================================================
175
176 QString SMESHGUI_Selection::shrinkMode( int ind ) const
177 {
178   SMESH_Actor* actor = getActor( ind );
179   if ( actor && actor->IsShrunkable() ) {
180     if ( actor->IsShrunk() )
181       return "IsShrunk";
182     return "IsNotShrunk";
183   }
184   return "IsNotShrinkable";
185 }
186
187 //=======================================================================
188 //function : entityMode
189 //purpose  : may return {'Edge' 'Face' 'Volume'} at most
190 //=======================================================================
191
192 QValueList<QVariant> SMESHGUI_Selection::entityMode( int ind ) const
193 {
194   QValueList<QVariant> types;
195   SMESH_Actor* actor = getActor( ind );
196   if ( actor ) {
197     unsigned int aMode = actor->GetEntityMode();
198     if ( aMode & SMESH_Actor::eVolumes) types.append( "Volume");
199     if ( aMode & SMESH_Actor::eFaces  ) types.append( "Face"  );
200     if ( aMode & SMESH_Actor::eEdges  ) types.append( "Edge"  );
201   }
202   return types;
203 }
204
205 //=======================================================================
206 //function : controlMode
207 //purpose  : return SMESH_Actor::eControl
208 //=======================================================================
209
210 QString SMESHGUI_Selection::controlMode( int ind ) const
211 {
212   SMESH_Actor* actor = getActor( ind );
213   if ( actor ) {
214     switch( actor->GetControlMode() ) {
215     case SMESH_Actor::eLength:            return "eLength";
216     case SMESH_Actor::eLength2D:          return "eLength2D";
217     case SMESH_Actor::eFreeEdges:         return "eFreeEdges";
218     case SMESH_Actor::eFreeBorders:       return "eFreeBorders";
219     case SMESH_Actor::eMultiConnection:   return "eMultiConnection";
220     case SMESH_Actor::eMultiConnection2D: return "eMultiConnection2D";
221     case SMESH_Actor::eArea:              return "eArea";
222     case SMESH_Actor::eTaper:             return "eTaper";
223     case SMESH_Actor::eAspectRatio:       return "eAspectRatio";
224     case SMESH_Actor::eAspectRatio3D:     return "eAspectRatio3D";
225     case SMESH_Actor::eMinimumAngle:      return "eMinimumAngle";
226     case SMESH_Actor::eWarping:           return "eWarping";
227     case SMESH_Actor::eSkew:              return "eSkew";
228     default:;
229     }
230   }
231   return "eNone";
232 }
233
234 //=======================================================================
235 //function : numberOfNodes
236 //purpose  : 
237 //=======================================================================
238
239 int SMESHGUI_Selection::numberOfNodes( int ind ) const
240 {
241   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
242   {
243     CORBA::Object_var obj =
244       SMESH::DataOwnerToObject( static_cast<SalomeApp_DataOwner*>( myDataOwners[ ind ].get() ));
245     if ( ! CORBA::is_nil( obj )) {
246       SMESH::SMESH_Mesh_var mesh = SMESH::SMESH_Mesh::_narrow( obj );
247       if ( ! mesh->_is_nil() )
248         return mesh->NbNodes();
249       SMESH::SMESH_subMesh_var aSubMeshObj = SMESH::SMESH_subMesh::_narrow( obj );
250       if ( !aSubMeshObj->_is_nil() )
251         return aSubMeshObj->GetNumberOfNodes(true);
252       SMESH::SMESH_GroupBase_var aGroupObj = SMESH::SMESH_GroupBase::_narrow( obj );
253       if ( !aGroupObj->_is_nil() )
254         return aGroupObj->Size();
255     }
256   }
257   return 0;
258 }
259
260 //=======================================================================
261 //function : isComputable
262 //purpose  : 
263 //=======================================================================
264
265 QVariant SMESHGUI_Selection::isComputable( int ind ) const
266 {
267   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
268   {
269     Handle(SALOME_InteractiveObject) io =
270       static_cast<SalomeApp_DataOwner*>( myDataOwners[ ind ].get() )->IO();
271     if ( !io.IsNull() ) {
272       SMESH::SMESH_Mesh_var mesh = SMESH::GetMeshByIO(io) ; // m,sm,gr->m
273       if ( !mesh->_is_nil() ) {
274         _PTR(SObject) so = SMESH::FindSObject( mesh );
275         if ( so ) {
276           GEOM::GEOM_Object_var shape = SMESH::GetShapeOnMeshOrSubMesh( so );
277           return QVariant( !shape->_is_nil(), 0 );
278         }
279       }
280     }
281   }
282   return QVariant( false, 0 );
283 }
284
285 //=======================================================================
286 //function : hasReference
287 //purpose  : 
288 //=======================================================================
289
290 QVariant SMESHGUI_Selection::hasReference( int ind ) const
291 {
292   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
293   {
294     Handle(SALOME_InteractiveObject) io =
295       static_cast<SalomeApp_DataOwner*>( myDataOwners[ ind ].get() )->IO();
296     if ( !io.IsNull() )
297       return QVariant( io->hasReference(), 0 );
298   }
299   return QVariant( false, 0 );
300 }
301
302 //=======================================================================
303 //function : isVisible
304 //purpose  : 
305 //=======================================================================
306
307 QVariant SMESHGUI_Selection::isVisible( int ind ) const
308 {
309   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
310   {
311     QString entry = static_cast<SalomeApp_DataOwner*>( myDataOwners[ ind ].get() )->entry();
312     SMESH_Actor* actor = SMESH::FindActorByEntry( entry.latin1() );
313     if ( actor && actor->hasIO() ) {
314       SVTK_RenderWindowInteractor* renderInter = SMESH::GetCurrentVtkView()->getRWInteractor();
315       return QVariant( renderInter->isVisible( actor->getIO() ), 0 );
316     }
317   }
318   return QVariant( false, 0 );
319 }
320
321
322 //=======================================================================
323 //function : type
324 //purpose  : 
325 //=======================================================================
326
327 int SMESHGUI_Selection::type( SalomeApp_DataOwner* owner,
328                               _PTR(Study) study )
329 {
330   QString entry = owner->entry();
331
332   _PTR(SObject) obj (study->FindObjectID(entry.latin1()));
333   if( !obj )
334     return -1;
335
336   _PTR(SObject) objFather = obj->GetFather();
337   _PTR(SComponent) objComponent = obj->GetFatherComponent();
338
339   int aLevel = obj->Depth() - objComponent->Depth(),
340       aFTag = objFather->Tag(),
341       anOTag = obj->Tag(),
342       res = -1;
343
344   switch( aLevel )
345   {
346   case 1:
347     if( anOTag>=3 )
348       res = MESH;
349     break;
350   case 2:
351     switch( aFTag )
352     {
353     case 1:
354       res = HYPOTHESIS;
355       break;
356     case 2:
357       res = ALGORITHM;
358       break;
359     }
360     break;
361   case 3:
362     switch( aFTag )
363     {
364     case 4:
365       res = SUBMESH_VERTEX;
366       break;
367     case 5:
368       res = SUBMESH_EDGE;
369       break;
370     case 7:
371       res = SUBMESH_FACE;
372       break;
373     case 9:
374       res = SUBMESH_SOLID;
375       break;
376     case 10:
377       res = SUBMESH_COMPOUND;
378       break;
379     }
380     if( aFTag>10 )
381       res = GROUP;
382
383     break;
384   }
385
386   return res;
387 }
388
389 //=======================================================================
390 //function : typeName
391 //purpose  : 
392 //=======================================================================
393
394 QString SMESHGUI_Selection::typeName( const int t )
395 {
396   switch( t )
397   {
398   case HYPOTHESIS:
399     return "Hypothesis";
400   case ALGORITHM:
401     return "Algorithm";
402   case MESH:
403     return "Mesh";
404   case SUBMESH:
405     return "SubMesh";
406   case MESHorSUBMESH:
407     return "Mesh or submesh";
408   case SUBMESH_VERTEX:
409     return "Mesh vertex";
410   case SUBMESH_EDGE:
411     return "Mesh edge";
412   case SUBMESH_FACE:
413     return "Mesh face";
414   case SUBMESH_SOLID:
415     return "Mesh solid";
416   case SUBMESH_COMPOUND:
417     return "Mesh compound";
418   case GROUP:
419     return "Group";
420   default:
421     return "Unknown";
422   }
423 }