Salome HOME
1b281d27f2fbe22edd697f66034c1d1b247bd84f
[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     for( int i=0, n=count(); i<n; i++ )
53       myTypes.append( typeName( type( entry( i ), aStudy ) ) );
54   }
55 }
56
57 //=======================================================================
58 //function : processOwner
59 //purpose  : 
60 //=======================================================================
61 void SMESHGUI_Selection::processOwner( const SalomeApp_DataOwner* ow )
62 {
63   const SalomeApp_SVTKDataOwner* owner = 
64     dynamic_cast<const SalomeApp_SVTKDataOwner*> ( ow );
65   if( owner )
66     myActors.append( dynamic_cast<SMESH_Actor*>( owner->GetActor() ) );
67   else
68     myActors.append( 0 );
69 }
70
71 //=======================================================================
72 //function : param
73 //purpose  : 
74 //=======================================================================
75 QtxValue SMESHGUI_Selection::param( const int ind, const QString& p ) const
76 {
77   QtxValue val;
78        if ( p=="client" )        val = QtxValue( globalParam( p ) );
79   else if ( p=="type" )          val = QtxValue( myTypes[ind] );
80   else if ( p=="elemTypes" )     val = QtxValue( elemTypes( ind ) );
81   else if ( p=="numberOfNodes" ) val = QtxValue( numberOfNodes( ind ) );
82   else if ( p=="labeledTypes" )  val = QtxValue( labeledTypes( ind ) );
83   else if ( p=="shrinkMode" )    val = QtxValue( shrinkMode( ind ) );
84   else if ( p=="entityMode" )    val = QtxValue( entityMode( ind ) );
85   else if ( p=="controlMode" )   val = QtxValue( controlMode( ind ) );
86   else if ( p=="displayMode" )   val = QtxValue( displayMode( ind ) );
87   else if ( p=="isComputable" )  val = QtxValue( isComputable( ind ) );
88   else if ( p=="hasReference" )  val = QtxValue( hasReference( ind ) );
89 //  else if ( p=="isVisible" )     val = QtxValue( isVisible( ind ) );
90
91   // printf( "--> param() : [%s] = %s (%s)\n", p.latin1(), val.toString().latin1(), val.typeName() );
92   //if ( val.type() == QVariant::List )
93   //cout << "size: " << val.toList().count() << endl;
94
95   if( val.isValid() )
96     return val;
97   else
98     return SalomeApp_Selection::param( ind, p );
99 }
100
101 //=======================================================================
102 //function : getVtkOwner
103 //purpose  : 
104 //=======================================================================
105
106 SMESH_Actor* SMESHGUI_Selection::getActor( int ind ) const
107 {
108   if( ind >= 0 && ind < count() )
109     return ((QPtrList<SMESH_Actor>&)myActors).at( ind );
110   else
111     return 0;
112 }
113
114 //=======================================================================
115 //function : elemTypes
116 //purpose  : may return {'Edge' 'Face' 'Volume'} at most
117 //=======================================================================
118
119 QValueList<QVariant> SMESHGUI_Selection::elemTypes( int ind ) const
120 {
121   QValueList<QVariant> types;
122   SMESH_Actor* actor = getActor( ind );
123   if ( actor ) {
124     TVisualObjPtr object = actor->GetObject();
125     if ( object ) {
126       if ( object->GetNbEntities( SMDSAbs_Edge )) types.append( "Edge" );
127       if ( object->GetNbEntities( SMDSAbs_Face )) types.append( "Face" );
128       if ( object->GetNbEntities( SMDSAbs_Volume )) types.append( "Volume" );
129     }
130   }
131   return types;
132 }
133
134 //=======================================================================
135 //function : labeledTypes
136 //purpose  : may return {'Point' 'Cell'} at most
137 //=======================================================================
138
139 QValueList<QVariant> SMESHGUI_Selection::labeledTypes( int ind ) const
140 {
141   QValueList<QVariant> types;
142   SMESH_Actor* actor = getActor( ind );
143   if ( actor ) {
144     if ( actor->GetPointsLabeled()) types.append( "Point" );
145     if ( actor->GetCellsLabeled()) types.append( "Cell" );
146   }
147   return types;
148 }
149
150 //=======================================================================
151 //function : displayMode
152 //purpose  : return SMESH_Actor::EReperesent
153 //=======================================================================
154
155 QString SMESHGUI_Selection::displayMode( int ind ) const
156 {
157   SMESH_Actor* actor = getActor( ind );
158   if ( actor ) {
159     switch( actor->GetRepresentation() ) {
160     case SMESH_Actor::eEdge:    return "eEdge";
161     case SMESH_Actor::eSurface: return "eSurface";
162     case SMESH_Actor::ePoint:   return "ePoint";
163     default:;
164     }
165   }
166   return "Unknown";
167 }
168
169 //=======================================================================
170 //function : shrinkMode
171 //purpose  : return either 'IsSrunk', 'IsNotShrunk' or 'IsNotShrinkable'
172 //=======================================================================
173
174 QString SMESHGUI_Selection::shrinkMode( int ind ) const
175 {
176   SMESH_Actor* actor = getActor( ind );
177   if ( actor && actor->IsShrunkable() ) {
178     if ( actor->IsShrunk() )
179       return "IsShrunk";
180     return "IsNotShrunk";
181   }
182   return "IsNotShrinkable";
183 }
184
185 //=======================================================================
186 //function : entityMode
187 //purpose  : may return {'Edge' 'Face' 'Volume'} at most
188 //=======================================================================
189
190 QValueList<QVariant> SMESHGUI_Selection::entityMode( int ind ) const
191 {
192   QValueList<QVariant> types;
193   SMESH_Actor* actor = getActor( ind );
194   if ( actor ) {
195     unsigned int aMode = actor->GetEntityMode();
196     if ( aMode & SMESH_Actor::eVolumes) types.append( "Volume");
197     if ( aMode & SMESH_Actor::eFaces  ) types.append( "Face"  );
198     if ( aMode & SMESH_Actor::eEdges  ) types.append( "Edge"  );
199   }
200   return types;
201 }
202
203 //=======================================================================
204 //function : controlMode
205 //purpose  : return SMESH_Actor::eControl
206 //=======================================================================
207
208 QString SMESHGUI_Selection::controlMode( int ind ) const
209 {
210   SMESH_Actor* actor = getActor( ind );
211   if ( actor ) {
212     switch( actor->GetControlMode() ) {
213     case SMESH_Actor::eLength:            return "eLength";
214     case SMESH_Actor::eLength2D:          return "eLength2D";
215     case SMESH_Actor::eFreeEdges:         return "eFreeEdges";
216     case SMESH_Actor::eFreeBorders:       return "eFreeBorders";
217     case SMESH_Actor::eMultiConnection:   return "eMultiConnection";
218     case SMESH_Actor::eMultiConnection2D: return "eMultiConnection2D";
219     case SMESH_Actor::eArea:              return "eArea";
220     case SMESH_Actor::eVolume3D:          return "eVolume3D";
221     case SMESH_Actor::eTaper:             return "eTaper";
222     case SMESH_Actor::eAspectRatio:       return "eAspectRatio";
223     case SMESH_Actor::eAspectRatio3D:     return "eAspectRatio3D";
224     case SMESH_Actor::eMinimumAngle:      return "eMinimumAngle";
225     case SMESH_Actor::eWarping:           return "eWarping";
226     case SMESH_Actor::eSkew:              return "eSkew";
227     default:;
228     }
229   }
230   return "eNone";
231 }
232
233 //=======================================================================
234 //function : numberOfNodes
235 //purpose  : 
236 //=======================================================================
237
238 int SMESHGUI_Selection::numberOfNodes( int ind ) const
239 {
240   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
241   {
242     _PTR(SObject) sobj = SMESH::GetActiveStudyDocument()->FindObjectID( entry( ind ).latin1() );
243     CORBA::Object_var obj = SMESH::SObjectToObject( sobj, SMESH::GetActiveStudyDocument() );
244
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::GetActiveStudyDocument()->FindObjectID( entry( ind ) );
275         //FindSObject( mesh );
276         if ( so ) {
277           GEOM::GEOM_Object_var shape = SMESH::GetShapeOnMeshOrSubMesh( so );
278           return QVariant( !shape->_is_nil(), 0 );
279         }
280 //      }
281 //    }
282   }
283   return QVariant( false, 0 );
284 }
285
286 //=======================================================================
287 //function : hasReference
288 //purpose  : 
289 //=======================================================================
290
291 QVariant SMESHGUI_Selection::hasReference( int ind ) const
292 {
293   return QVariant( isReference( ind ), 0 );
294 }
295
296 //=======================================================================
297 //function : isVisible
298 //purpose  : 
299 //=======================================================================
300
301 QVariant SMESHGUI_Selection::isVisible( int ind ) const
302 {
303   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
304   {
305     QString ent = entry( ind );
306     SMESH_Actor* actor = SMESH::FindActorByEntry( ent.latin1() );
307     if ( actor && actor->hasIO() ) {
308       SVTK_RenderWindowInteractor* renderInter = SMESH::GetCurrentVtkView()->getRWInteractor();
309       return QVariant( renderInter->isVisible( actor->getIO() ), 0 );
310     }
311   }
312   return QVariant( false, 0 );
313 }
314
315 //=======================================================================
316 //function : type
317 //purpose  : 
318 //=======================================================================
319
320 int SMESHGUI_Selection::type( const QString& entry, _PTR(Study) study )
321 {
322   _PTR(SObject) obj (study->FindObjectID(entry.latin1()));
323   if( !obj )
324     return -1;
325
326   _PTR(SObject) ref;
327   if( obj->ReferencedObject( ref ) )
328     obj = ref;
329
330   _PTR(SObject) objFather = obj->GetFather();
331   _PTR(SComponent) objComponent = obj->GetFatherComponent();
332
333   if( objComponent->ComponentDataType()!="SMESH" )
334     return -1;
335
336   if( objComponent->GetIOR()==obj->GetIOR() )
337     return COMPONENT;
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   case COMPONENT:
421     return "Component";
422   default:
423     return "Unknown";
424   }
425 }