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