Salome HOME
33f01403f46fb1df5a0e5ee31d98d3b923a7f1d4
[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::eVolume3D:          return "eVolume3D";
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     SalomeApp_DataOwner* owner = dynamic_cast<SalomeApp_DataOwner*>( myDataOwners[ ind ].operator->() );
295     if( owner )
296     {
297       _PTR(SObject) obj ( study()->studyDS()->FindObjectID( owner->entry().latin1() ) ), ref;
298       return QVariant( obj->ReferencedObject( ref ), 0 );
299     }
300   }
301   return QVariant( false, 0 );
302 }
303
304 //=======================================================================
305 //function : isVisible
306 //purpose  : 
307 //=======================================================================
308
309 QVariant SMESHGUI_Selection::isVisible( int ind ) const
310 {
311   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
312   {
313     QString entry = static_cast<SalomeApp_DataOwner*>( myDataOwners[ ind ].get() )->entry();
314     SMESH_Actor* actor = SMESH::FindActorByEntry( entry.latin1() );
315     if ( actor && actor->hasIO() ) {
316       SVTK_RenderWindowInteractor* renderInter = SMESH::GetCurrentVtkView()->getRWInteractor();
317       return QVariant( renderInter->isVisible( actor->getIO() ), 0 );
318     }
319   }
320   return QVariant( false, 0 );
321 }
322
323
324 //=======================================================================
325 //function : type
326 //purpose  :
327 //=======================================================================
328 int SMESHGUI_Selection::type( SalomeApp_DataOwner* owner, _PTR(Study) study )
329 {
330   return type( owner->entry(), study );
331 }
332
333 //=======================================================================
334 //function : type
335 //purpose  : 
336 //=======================================================================
337
338 int SMESHGUI_Selection::type( const QString& entry, _PTR(Study) study )
339 {
340   _PTR(SObject) obj (study->FindObjectID(entry.latin1()));
341   if( !obj )
342     return -1;
343
344   _PTR(SObject) ref;
345   if( obj->ReferencedObject( ref ) )
346     obj = ref;
347
348   _PTR(SObject) objFather = obj->GetFather();
349   _PTR(SComponent) objComponent = obj->GetFatherComponent();
350
351   if( objComponent->ComponentDataType()!="SMESH" )
352     return -1;
353
354   if( objComponent->GetIOR()==obj->GetIOR() )
355     return COMPONENT;
356
357   int aLevel = obj->Depth() - objComponent->Depth(),
358       aFTag = objFather->Tag(),
359       anOTag = obj->Tag(),
360       res = -1;
361
362   switch( aLevel )
363   {
364   case 1:
365     if( anOTag>=3 )
366       res = MESH;
367     break;
368   case 2:
369     switch( aFTag )
370     {
371     case 1:
372       res = HYPOTHESIS;
373       break;
374     case 2:
375       res = ALGORITHM;
376       break;
377     }
378     break;
379   case 3:
380     switch( aFTag )
381     {
382     case 4:
383       res = SUBMESH_VERTEX;
384       break;
385     case 5:
386       res = SUBMESH_EDGE;
387       break;
388     case 7:
389       res = SUBMESH_FACE;
390       break;
391     case 9:
392       res = SUBMESH_SOLID;
393       break;
394     case 10:
395       res = SUBMESH_COMPOUND;
396       break;
397     }
398     if( aFTag>10 )
399       res = GROUP;
400
401     break;
402   }
403
404   return res;
405 }
406
407 //=======================================================================
408 //function : typeName
409 //purpose  : 
410 //=======================================================================
411
412 QString SMESHGUI_Selection::typeName( const int t )
413 {
414   switch( t )
415   {
416   case HYPOTHESIS:
417     return "Hypothesis";
418   case ALGORITHM:
419     return "Algorithm";
420   case MESH:
421     return "Mesh";
422   case SUBMESH:
423     return "SubMesh";
424   case MESHorSUBMESH:
425     return "Mesh or submesh";
426   case SUBMESH_VERTEX:
427     return "Mesh vertex";
428   case SUBMESH_EDGE:
429     return "Mesh edge";
430   case SUBMESH_FACE:
431     return "Mesh face";
432   case SUBMESH_SOLID:
433     return "Mesh solid";
434   case SUBMESH_COMPOUND:
435     return "Mesh compound";
436   case GROUP:
437     return "Group";
438   case COMPONENT:
439     return "Component";
440   default:
441     return "Unknown";
442   }
443 }