Salome HOME
SMH: Preparation version 3.0.0 - merge (HEAD+POLYWORK)
[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
28 SMESHGUI_Selection::SMESHGUI_Selection( const QString&          client,
29                                         SalomeApp_SelectionMgr* mgr )
30 {
31   myPopupClient = client;
32   
33   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
34     (SUIT_Session::session()->activeApplication()->activeStudy());
35
36   if( mgr && appStudy )
37   {
38     _PTR(Study) study = appStudy->studyDS();
39
40     SUIT_DataOwnerPtrList sel;
41     mgr->selected( sel, client );
42     myDataOwners = sel;
43     SUIT_DataOwnerPtrList::const_iterator anIt = sel.begin(),
44                                           aLast = sel.end();
45     for( ; anIt!=aLast; anIt++ )
46     {
47       SUIT_DataOwner* owner = ( SUIT_DataOwner* )( (*anIt ).get() );
48       SalomeApp_DataOwner* sowner = dynamic_cast<SalomeApp_DataOwner*>( owner );
49       if( sowner )
50         myTypes.append( typeName( type( sowner, study ) ) );
51       else
52         myTypes.append( "Unknown" );
53     }
54   }
55 }
56
57 SMESHGUI_Selection::~SMESHGUI_Selection()
58 {
59 }
60
61 //=======================================================================
62 //function : count
63 //purpose  : 
64 //=======================================================================
65
66 int SMESHGUI_Selection::count() const
67 {
68   return myTypes.count();
69 }
70
71 //=======================================================================
72 //function : param
73 //purpose  : 
74 //=======================================================================
75
76 QtxValue SMESHGUI_Selection::param( const int ind, const QString& p ) const
77 {
78   QtxValue val;
79   if      ( p=="client" )        val = QtxValue( myPopupClient );
80   else if ( p=="type" )          val = QtxValue( myTypes[ind] );
81   else if ( p=="elemTypes" )     val = QtxValue( elemTypes( ind ) );
82   else if ( p=="numberOfNodes" ) val = QtxValue( numberOfNodes( ind ) );
83   else if ( p=="labeledTypes" )  val = QtxValue( labeledTypes( ind ) );
84   else if ( p=="shrinkMode" )    val = QtxValue( shrinkMode( ind ) );
85   else if ( p=="entityMode" )    val = QtxValue( entityMode( ind ) );
86   else if ( p=="controlMode" )   val = QtxValue( controlMode( ind ) );
87   else if ( p=="displayMode" )   val = QtxValue( displayMode( ind ) );
88   else if ( p=="isComputable" )  val = QtxValue( isComputable( ind ) );
89   else if ( p=="hasReference" )  val = QtxValue( hasReference( ind ) );
90   else if ( p=="isVisible" )     val = QtxValue( isVisible( ind ) );
91
92   printf( "--> param() : [%s] = %s (%s)\n", p.latin1(), val.toString().latin1(), val.typeName() );
93   if ( val.type() == QVariant::List )
94     cout << "size: " << val.toList().count() << endl;
95   return val;
96 }
97
98 //=======================================================================
99 //function : getVtkOwner
100 //purpose  : 
101 //=======================================================================
102
103 SMESH_Actor* SMESHGUI_Selection::getActor( int ind ) const
104 {
105   if ( ind >= 0 && ind < myDataOwners.count() ) {
106     const SalomeApp_SVTKDataOwner* owner = 
107       dynamic_cast<const SalomeApp_SVTKDataOwner*> ( myDataOwners[ ind ].get() );
108     if ( owner )
109       return dynamic_cast<SMESH_Actor*>( owner->GetActor() );
110   }
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::eTaper:             return "eTaper";
221     case SMESH_Actor::eAspectRatio:       return "eAspectRatio";
222     case SMESH_Actor::eAspectRatio3D:     return "eAspectRatio3D";
223     case SMESH_Actor::eMinimumAngle:      return "eMinimumAngle";
224     case SMESH_Actor::eWarping:           return "eWarping";
225     case SMESH_Actor::eSkew:              return "eSkew";
226     default:;
227     }
228   }
229   return "eNone";
230 }
231
232 //=======================================================================
233 //function : numberOfNodes
234 //purpose  : 
235 //=======================================================================
236
237 int SMESHGUI_Selection::numberOfNodes( int ind ) const
238 {
239   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
240   {
241     CORBA::Object_var obj =
242       SMESH::DataOwnerToObject( static_cast<SalomeApp_DataOwner*>( myDataOwners[ ind ].get() ));
243     if ( ! CORBA::is_nil( obj )) {
244       SMESH::SMESH_Mesh_var mesh = SMESH::SMESH_Mesh::_narrow( obj );
245       if ( ! mesh->_is_nil() )
246         return mesh->NbNodes();
247       SMESH::SMESH_subMesh_var aSubMeshObj = SMESH::SMESH_subMesh::_narrow( obj );
248       if ( !aSubMeshObj->_is_nil() )
249         return aSubMeshObj->GetNumberOfNodes(true);
250       SMESH::SMESH_GroupBase_var aGroupObj = SMESH::SMESH_GroupBase::_narrow( obj );
251       if ( !aGroupObj->_is_nil() )
252         return aGroupObj->Size();
253     }
254   }
255   return 0;
256 }
257
258 //=======================================================================
259 //function : isComputable
260 //purpose  : 
261 //=======================================================================
262
263 QVariant SMESHGUI_Selection::isComputable( int ind ) const
264 {
265   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
266   {
267     Handle(SALOME_InteractiveObject) io =
268       static_cast<SalomeApp_DataOwner*>( myDataOwners[ ind ].get() )->IO();
269     if ( !io.IsNull() ) {
270       SMESH::SMESH_Mesh_var mesh = SMESH::GetMeshByIO(io) ; // m,sm,gr->m
271       if ( !mesh->_is_nil() ) {
272         _PTR(SObject) so = SMESH::FindSObject( mesh );
273         if ( so ) {
274           GEOM::GEOM_Object_var shape = SMESH::GetShapeOnMeshOrSubMesh( so );
275           return QVariant( !shape->_is_nil(), 0 );
276         }
277       }
278     }
279   }
280   return QVariant( false, 0 );
281 }
282
283 //=======================================================================
284 //function : hasReference
285 //purpose  : 
286 //=======================================================================
287
288 QVariant SMESHGUI_Selection::hasReference( int ind ) const
289 {
290   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
291   {
292     Handle(SALOME_InteractiveObject) io =
293       static_cast<SalomeApp_DataOwner*>( myDataOwners[ ind ].get() )->IO();
294     if ( !io.IsNull() )
295       return QVariant( io->hasReference(), 0 );
296   }
297   return QVariant( false, 0 );
298 }
299
300 //=======================================================================
301 //function : isVisible
302 //purpose  : 
303 //=======================================================================
304
305 QVariant SMESHGUI_Selection::isVisible( int ind ) const
306 {
307   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
308   {
309     QString entry = static_cast<SalomeApp_DataOwner*>( myDataOwners[ ind ].get() )->entry();
310     SMESH_Actor* actor = SMESH::FindActorByEntry( entry.latin1() );
311     if ( actor && actor->hasIO() ) {
312       SVTK_RenderWindowInteractor* renderInter = SMESH::GetCurrentVtkView()->getRWInteractor();
313       return QVariant( renderInter->isVisible( actor->getIO() ), 0 );
314     }
315   }
316   return QVariant( false, 0 );
317 }
318
319
320 //=======================================================================
321 //function : type
322 //purpose  : 
323 //=======================================================================
324
325 int SMESHGUI_Selection::type( SalomeApp_DataOwner* owner,
326                               _PTR(Study) study )
327 {
328   QString entry = owner->entry();
329
330   _PTR(SObject) obj (study->FindObjectID(entry.latin1()));
331   if( !obj )
332     return -1;
333
334   _PTR(SObject) objFather = obj->GetFather();
335   _PTR(SComponent) objComponent = obj->GetFatherComponent();
336
337   int aLevel = obj->Depth() - objComponent->Depth(),
338       aFTag = objFather->Tag(),
339       anOTag = obj->Tag(),
340       res = -1;
341
342   switch( aLevel )
343   {
344   case 1:
345     if( anOTag>=3 )
346       res = MESH;
347     break;
348   case 2:
349     switch( aFTag )
350     {
351     case 1:
352       res = HYPOTHESIS;
353       break;
354     case 2:
355       res = ALGORITHM;
356       break;
357     }
358     break;
359   case 3:
360     switch( aFTag )
361     {
362     case 4:
363       res = SUBMESH_VERTEX;
364       break;
365     case 5:
366       res = SUBMESH_EDGE;
367       break;
368     case 7:
369       res = SUBMESH_FACE;
370       break;
371     case 9:
372       res = SUBMESH_SOLID;
373       break;
374     case 10:
375       res = SUBMESH_COMPOUND;
376       break;
377     }
378     if( aFTag>10 )
379       res = GROUP;
380
381     break;
382   }
383
384   return res;
385 }
386
387 //=======================================================================
388 //function : typeName
389 //purpose  : 
390 //=======================================================================
391
392 QString SMESHGUI_Selection::typeName( const int t )
393 {
394   switch( t )
395   {
396   case HYPOTHESIS:
397     return "Hypothesis";
398   case ALGORITHM:
399     return "Algorithm";
400   case MESH:
401     return "Mesh";
402   case SUBMESH:
403     return "SubMesh";
404   case MESHorSUBMESH:
405     return "Mesh or submesh";
406   case SUBMESH_VERTEX:
407     return "Mesh vertex";
408   case SUBMESH_EDGE:
409     return "Mesh edge";
410   case SUBMESH_FACE:
411     return "Mesh face";
412   case SUBMESH_SOLID:
413     return "Mesh solid";
414   case SUBMESH_COMPOUND:
415     return "Mesh compound";
416   case GROUP:
417     return "Group";
418   default:
419     return "Unknown";
420   }
421 }