Salome HOME
Join modifications from branch BR_DEBUG_3_2_0b1
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_Selection.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20
21 #include "SMESHGUI_Selection.h"
22 #include "SMESHGUI_Utils.h"
23 #include "SMESHGUI_VTKUtils.h"
24 #include "SMESHGUI_MeshUtils.h"
25 #include "SMESHGUI_GEOMGenUtils.h"
26
27 #include "SMESH_Type.h"
28 #include "SMESH_Actor.h"
29
30 #include "LightApp_SelectionMgr.h"
31 #include "SalomeApp_Study.h"
32 #include "LightApp_VTKSelector.h"
33
34 #include "SUIT_Session.h"
35
36 #include "SVTK_RenderWindowInteractor.h"
37 #include "SVTK_ViewWindow.h"
38
39 #include CORBA_SERVER_HEADER(SMESH_Mesh)
40 #include CORBA_SERVER_HEADER(SMESH_Group)
41
42 //=======================================================================
43 //function : SMESHGUI_Selection
44 //purpose  : 
45 //=======================================================================
46 SMESHGUI_Selection::SMESHGUI_Selection()
47 : LightApp_Selection()
48 {
49 }
50
51 //=======================================================================
52 //function : ~SMESHGUI_Selection
53 //purpose  : 
54 //=======================================================================
55 SMESHGUI_Selection::~SMESHGUI_Selection()
56 {
57 }
58
59 //=======================================================================
60 //function : init
61 //purpose  : 
62 //=======================================================================
63 void SMESHGUI_Selection::init( const QString& client, LightApp_SelectionMgr* mgr )
64 {
65   LightApp_Selection::init( client, mgr );
66
67   if( mgr && study() )
68   {
69     SalomeApp_Study* aSStudy = dynamic_cast<SalomeApp_Study*>(study());
70     if (!aSStudy)
71       return;
72     _PTR(Study) aStudy = aSStudy->studyDS();
73
74     for( int i=0, n=count(); i<n; i++ )
75       myTypes.append( typeName( type( entry( i ), aStudy ) ) );
76   }
77 }
78
79 //=======================================================================
80 //function : processOwner
81 //purpose  : 
82 //=======================================================================
83 void SMESHGUI_Selection::processOwner( const LightApp_DataOwner* ow )
84 {
85   const LightApp_SVTKDataOwner* owner = 
86     dynamic_cast<const LightApp_SVTKDataOwner*> ( ow );
87   if( owner )
88     myActors.append( dynamic_cast<SMESH_Actor*>( owner->GetActor() ) );
89   else
90     myActors.append( 0 );
91 }
92
93 //=======================================================================
94 //function : param
95 //purpose  : 
96 //=======================================================================
97 QtxValue SMESHGUI_Selection::param( const int ind, const QString& p ) const
98 {
99   QtxValue val;
100        if ( p=="client" )        val = QtxValue( globalParam( p ) );
101   else if ( p=="type" )          val = QtxValue( myTypes[ind] );
102   else if ( p=="elemTypes" )     val = QtxValue( elemTypes( ind ) );
103   else if ( p=="numberOfNodes" ) val = QtxValue( numberOfNodes( ind ) );
104   else if ( p=="labeledTypes" )  val = QtxValue( labeledTypes( ind ) );
105   else if ( p=="shrinkMode" )    val = QtxValue( shrinkMode( ind ) );
106   else if ( p=="entityMode" )    val = QtxValue( entityMode( ind ) );
107   else if ( p=="controlMode" )   val = QtxValue( controlMode( ind ) );
108   else if ( p=="displayMode" )   val = QtxValue( displayMode( ind ) );
109   else if ( p=="isComputable" )  val = QtxValue( isComputable( ind ) );
110   else if ( p=="hasReference" )  val = QtxValue( hasReference( ind ) );
111 //  else if ( p=="isVisible" )     val = QtxValue( isVisible( ind ) );
112
113        // printf( "--> param() : [%s] = %s (%s)\n", p.latin1(), val.toString().latin1(), val.typeName() );
114   //if ( val.type() == QVariant::List )
115   //cout << "size: " << val.toList().count() << endl;
116
117   if( val.isValid() )
118     return val;
119   else
120     return LightApp_Selection::param( ind, p );
121 }
122
123 //=======================================================================
124 //function : getVtkOwner
125 //purpose  : 
126 //=======================================================================
127
128 SMESH_Actor* SMESHGUI_Selection::getActor( int ind ) const
129 {
130   if( ind >= 0 && ind < count() )
131     return ((QPtrList<SMESH_Actor>&)myActors).at( ind );
132   else
133     return 0;
134 }
135
136 //=======================================================================
137 //function : elemTypes
138 //purpose  : may return {'Edge' 'Face' 'Volume'} at most
139 //=======================================================================
140
141 QValueList<QVariant> SMESHGUI_Selection::elemTypes( int ind ) const
142 {
143   QValueList<QVariant> types;
144   SMESH_Actor* actor = getActor( ind );
145   if ( actor ) {
146     TVisualObjPtr object = actor->GetObject();
147     if ( object ) {
148       if ( object->GetNbEntities( SMDSAbs_Edge )) types.append( "Edge" );
149       if ( object->GetNbEntities( SMDSAbs_Face )) types.append( "Face" );
150       if ( object->GetNbEntities( SMDSAbs_Volume )) types.append( "Volume" );
151     }
152   }
153   return types;
154 }
155
156 //=======================================================================
157 //function : labeledTypes
158 //purpose  : may return {'Point' 'Cell'} at most
159 //=======================================================================
160
161 QValueList<QVariant> SMESHGUI_Selection::labeledTypes( int ind ) const
162 {
163   QValueList<QVariant> types;
164   SMESH_Actor* actor = getActor( ind );
165   if ( actor ) {
166     if ( actor->GetPointsLabeled()) types.append( "Point" );
167     if ( actor->GetCellsLabeled()) types.append( "Cell" );
168   }
169   return types;
170 }
171
172 //=======================================================================
173 //function : displayMode
174 //purpose  : return SMESH_Actor::EReperesent
175 //=======================================================================
176
177 QString SMESHGUI_Selection::displayMode( int ind ) const
178 {
179   SMESH_Actor* actor = getActor( ind );
180   if ( actor ) {
181     switch( actor->GetRepresentation() ) {
182     case SMESH_Actor::eEdge:    return "eEdge";
183     case SMESH_Actor::eSurface: return "eSurface";
184     case SMESH_Actor::ePoint:   return "ePoint";
185     default:;
186     }
187   }
188   return "Unknown";
189 }
190
191 //=======================================================================
192 //function : shrinkMode
193 //purpose  : return either 'IsSrunk', 'IsNotShrunk' or 'IsNotShrinkable'
194 //=======================================================================
195
196 QString SMESHGUI_Selection::shrinkMode( int ind ) const
197 {
198   SMESH_Actor* actor = getActor( ind );
199   if ( actor && actor->IsShrunkable() ) {
200     if ( actor->IsShrunk() )
201       return "IsShrunk";
202     return "IsNotShrunk";
203   }
204   return "IsNotShrinkable";
205 }
206
207 //=======================================================================
208 //function : entityMode
209 //purpose  : may return {'Edge' 'Face' 'Volume'} at most
210 //=======================================================================
211
212 QValueList<QVariant> SMESHGUI_Selection::entityMode( int ind ) const
213 {
214   QValueList<QVariant> types;
215   SMESH_Actor* actor = getActor( ind );
216   if ( actor ) {
217     unsigned int aMode = actor->GetEntityMode();
218     if ( aMode & SMESH_Actor::eVolumes) types.append( "Volume");
219     if ( aMode & SMESH_Actor::eFaces  ) types.append( "Face"  );
220     if ( aMode & SMESH_Actor::eEdges  ) types.append( "Edge"  );
221   }
222   return types;
223 }
224
225 //=======================================================================
226 //function : controlMode
227 //purpose  : return SMESH_Actor::eControl
228 //=======================================================================
229
230 QString SMESHGUI_Selection::controlMode( int ind ) const
231 {
232   SMESH_Actor* actor = getActor( ind );
233   if ( actor ) {
234     switch( actor->GetControlMode() ) {
235     case SMESH_Actor::eLength:            return "eLength";
236     case SMESH_Actor::eLength2D:          return "eLength2D";
237     case SMESH_Actor::eFreeEdges:         return "eFreeEdges";
238     case SMESH_Actor::eFreeBorders:       return "eFreeBorders";
239     case SMESH_Actor::eMultiConnection:   return "eMultiConnection";
240     case SMESH_Actor::eMultiConnection2D: return "eMultiConnection2D";
241     case SMESH_Actor::eArea:              return "eArea";
242     case SMESH_Actor::eVolume3D:          return "eVolume3D";
243     case SMESH_Actor::eTaper:             return "eTaper";
244     case SMESH_Actor::eAspectRatio:       return "eAspectRatio";
245     case SMESH_Actor::eAspectRatio3D:     return "eAspectRatio3D";
246     case SMESH_Actor::eMinimumAngle:      return "eMinimumAngle";
247     case SMESH_Actor::eWarping:           return "eWarping";
248     case SMESH_Actor::eSkew:              return "eSkew";
249     default:;
250     }
251   }
252   return "eNone";
253 }
254
255 //=======================================================================
256 //function : numberOfNodes
257 //purpose  : 
258 //=======================================================================
259
260 int SMESHGUI_Selection::numberOfNodes( int ind ) const
261 {
262   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
263   {
264     _PTR(SObject) sobj = SMESH::GetActiveStudyDocument()->FindObjectID( entry( ind ).latin1() );
265     CORBA::Object_var obj = SMESH::SObjectToObject( sobj, SMESH::GetActiveStudyDocument() );
266
267     if ( ! CORBA::is_nil( obj )) {
268       SMESH::SMESH_Mesh_var mesh = SMESH::SMESH_Mesh::_narrow( obj );
269       if ( ! mesh->_is_nil() )
270         return mesh->NbNodes();
271       SMESH::SMESH_subMesh_var aSubMeshObj = SMESH::SMESH_subMesh::_narrow( obj );
272       if ( !aSubMeshObj->_is_nil() )
273         return aSubMeshObj->GetNumberOfNodes(true);
274       SMESH::SMESH_GroupBase_var aGroupObj = SMESH::SMESH_GroupBase::_narrow( obj );
275       if ( !aGroupObj->_is_nil() )
276         return aGroupObj->Size();
277     }
278   }
279   return 0;
280 }
281
282 //=======================================================================
283 //function : isComputable
284 //purpose  : 
285 //=======================================================================
286
287 QVariant SMESHGUI_Selection::isComputable( int ind ) const
288 {
289   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
290   {
291 /*    Handle(SALOME_InteractiveObject) io =
292       static_cast<LightApp_DataOwner*>( myDataOwners[ ind ].get() )->IO();
293     if ( !io.IsNull() ) {
294       SMESH::SMESH_Mesh_var mesh = SMESH::GetMeshByIO(io) ; // m,sm,gr->m
295       if ( !mesh->_is_nil() ) {*/
296         _PTR(SObject) so = SMESH::GetActiveStudyDocument()->FindObjectID( entry( ind ).latin1() );
297         //FindSObject( mesh );
298         if ( so ) {
299           GEOM::GEOM_Object_var shape = SMESH::GetShapeOnMeshOrSubMesh( so );
300           return QVariant( !shape->_is_nil(), 0 );
301         }
302 //      }
303 //    }
304   }
305   return QVariant( false, 0 );
306 }
307
308 //=======================================================================
309 //function : hasReference
310 //purpose  : 
311 //=======================================================================
312
313 QVariant SMESHGUI_Selection::hasReference( int ind ) const
314 {
315   return QVariant( isReference( ind ), 0 );
316 }
317
318 //=======================================================================
319 //function : isVisible
320 //purpose  : 
321 //=======================================================================
322
323 QVariant SMESHGUI_Selection::isVisible( int ind ) const
324 {
325   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
326   {
327     QString ent = entry( ind );
328     SMESH_Actor* actor = SMESH::FindActorByEntry( ent.latin1() );
329     if ( actor && actor->hasIO() ) {
330       if(SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView())
331         return QVariant( aViewWindow->isVisible( actor->getIO() ), 0 );
332     }
333   }
334   return QVariant( false, 0 );
335 }
336
337 //=======================================================================
338 //function : type
339 //purpose  : 
340 //=======================================================================
341
342 int SMESHGUI_Selection::type( const QString& entry, _PTR(Study) study )
343 {
344   _PTR(SObject) obj (study->FindObjectID(entry.latin1()));
345   if( !obj )
346     return -1;
347
348   _PTR(SObject) ref;
349   if( obj->ReferencedObject( ref ) )
350     obj = ref;
351
352   _PTR(SObject) objFather = obj->GetFather();
353   _PTR(SComponent) objComponent = obj->GetFatherComponent();
354
355   if( objComponent->ComponentDataType()!="SMESH" )
356     return -1;
357
358   if( objComponent->GetIOR()==obj->GetIOR() )
359     return COMPONENT;
360
361   int aLevel = obj->Depth() - objComponent->Depth(),
362       aFTag = objFather->Tag(),
363       anOTag = obj->Tag(),
364       res = -1;
365
366   switch( aLevel )
367   {
368   case 1:
369     if( anOTag>=3 )
370       res = MESH;
371     break;
372   case 2:
373     switch( aFTag )
374     {
375     case 1:
376       res = HYPOTHESIS;
377       break;
378     case 2:
379       res = ALGORITHM;
380       break;
381     }
382     break;
383   case 3:
384     switch( aFTag )
385     {
386     case 4:
387       res = SUBMESH_VERTEX;
388       break;
389     case 5:
390       res = SUBMESH_EDGE;
391       break;
392     case 7:
393       res = SUBMESH_FACE;
394       break;
395     case 9:
396       res = SUBMESH_SOLID;
397       break;
398     case 10:
399       res = SUBMESH_COMPOUND;
400       break;
401     }
402     if( aFTag>10 )
403       res = GROUP;
404     else
405       res = SUBMESH;
406
407     break;
408   }
409
410   return res;
411 }
412
413 //=======================================================================
414 //function : typeName
415 //purpose  : 
416 //=======================================================================
417
418 QString SMESHGUI_Selection::typeName( const int t )
419 {
420   switch( t )
421   {
422   case HYPOTHESIS:
423     return "Hypothesis";
424   case ALGORITHM:
425     return "Algorithm";
426   case MESH:
427     return "Mesh";
428   case SUBMESH:
429     return "SubMesh";
430   case MESHorSUBMESH:
431     return "Mesh or submesh";
432   case SUBMESH_VERTEX:
433     return "Mesh vertex";
434   case SUBMESH_EDGE:
435     return "Mesh edge";
436   case SUBMESH_FACE:
437     return "Mesh face";
438   case SUBMESH_SOLID:
439     return "Mesh solid";
440   case SUBMESH_COMPOUND:
441     return "Mesh compound";
442   case GROUP:
443     return "Group";
444   case COMPONENT:
445     return "Component";
446   default:
447     return "Unknown";
448   }
449 }