Salome HOME
7cbde8fb1d4c8ea1f9023d7b9cab727495fc7b02
[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_CLIENT_HEADER(SMESH_Gen)
40 #include CORBA_CLIENT_HEADER(SMESH_Mesh)
41 #include CORBA_CLIENT_HEADER(SMESH_Group)
42
43 //=======================================================================
44 //function : SMESHGUI_Selection
45 //purpose  : 
46 //=======================================================================
47 SMESHGUI_Selection::SMESHGUI_Selection()
48 : LightApp_Selection()
49 {
50 }
51
52 //=======================================================================
53 //function : ~SMESHGUI_Selection
54 //purpose  : 
55 //=======================================================================
56 SMESHGUI_Selection::~SMESHGUI_Selection()
57 {
58 }
59
60 //=======================================================================
61 //function : init
62 //purpose  : 
63 //=======================================================================
64 void SMESHGUI_Selection::init( const QString& client, LightApp_SelectionMgr* mgr )
65 {
66   LightApp_Selection::init( client, mgr );
67
68   if( mgr && study() )
69   {
70     SalomeApp_Study* aSStudy = dynamic_cast<SalomeApp_Study*>(study());
71     if (!aSStudy)
72       return;
73     _PTR(Study) aStudy = aSStudy->studyDS();
74
75     for( int i=0, n=count(); i<n; i++ )
76       myTypes.append( typeName( type( entry( i ), aStudy ) ) );
77   }
78 }
79
80 //=======================================================================
81 //function : processOwner
82 //purpose  : 
83 //=======================================================================
84 void SMESHGUI_Selection::processOwner( const LightApp_DataOwner* ow )
85 {
86   const LightApp_SVTKDataOwner* owner =
87     dynamic_cast<const LightApp_SVTKDataOwner*> ( ow );
88   if( owner )
89     myActors.append( dynamic_cast<SMESH_Actor*>( owner->GetActor() ) );
90   else
91     myActors.append( 0 );
92 }
93
94 //=======================================================================
95 //function : param
96 //purpose  : 
97 //=======================================================================
98 QtxValue SMESHGUI_Selection::param( const int ind, const QString& p ) const
99 {
100   QtxValue val;
101        if ( p=="client" )        val = QtxValue( globalParam( p ) );
102   else if ( p=="type" )          val = QtxValue( myTypes[ind] );
103   else if ( p=="elemTypes" )     val = QtxValue( elemTypes( ind ) );
104   else if ( p=="isAutoColor" )   val = QtxValue( isAutoColor( ind ) );
105   else if ( p=="numberOfNodes" ) val = QtxValue( numberOfNodes( ind ) );
106   else if ( p=="labeledTypes" )  val = QtxValue( labeledTypes( ind ) );
107   else if ( p=="shrinkMode" )    val = QtxValue( shrinkMode( ind ) );
108   else if ( p=="entityMode" )    val = QtxValue( entityMode( ind ) );
109   else if ( p=="controlMode" )   val = QtxValue( controlMode( ind ) );
110   else if ( p=="displayMode" )   val = QtxValue( displayMode( ind ) );
111   else if ( p=="isComputable" )  val = QtxValue( isComputable( ind ) );
112   else if ( p=="hasReference" )  val = QtxValue( hasReference( ind ) );
113   else if( p=="isImported" )     val = QtxValue( isImported( ind ) );
114
115 //  else if ( p=="isVisible" )     val = QtxValue( isVisible( ind ) );
116
117        // printf( "--> param() : [%s] = %s (%s)\n", p.latin1(), val.toString().latin1(), val.typeName() );
118   //if ( val.type() == QVariant::List )
119   //cout << "size: " << val.toList().count() << endl;
120
121   if( val.isValid() )
122     return val;
123   else
124     return LightApp_Selection::param( ind, p );
125 }
126
127 //=======================================================================
128 //function : getVtkOwner
129 //purpose  : 
130 //=======================================================================
131
132 SMESH_Actor* SMESHGUI_Selection::getActor( int ind ) const
133 {
134   if( ind >= 0 && ind < count() )
135     return ((QPtrList<SMESH_Actor>&)myActors).at( ind );
136   else
137     return 0;
138 }
139
140 //=======================================================================
141 //function : elemTypes
142 //purpose  : may return {'Edge' 'Face' 'Volume'} at most
143 //=======================================================================
144
145 QValueList<QVariant> SMESHGUI_Selection::elemTypes( int ind ) const
146 {
147   QValueList<QVariant> types;
148   SMESH_Actor* actor = getActor( ind );
149   if ( actor ) {
150     TVisualObjPtr object = actor->GetObject();
151     if ( object ) {
152       if ( object->GetNbEntities( SMDSAbs_Edge )) types.append( "Edge" );
153       if ( object->GetNbEntities( SMDSAbs_Face )) types.append( "Face" );
154       if ( object->GetNbEntities( SMDSAbs_Volume )) types.append( "Volume" );
155     }
156   }
157   return types;
158 }
159
160 //=======================================================================
161 //function : labeledTypes
162 //purpose  : may return {'Point' 'Cell'} at most
163 //=======================================================================
164
165 QValueList<QVariant> SMESHGUI_Selection::labeledTypes( int ind ) const
166 {
167   QValueList<QVariant> types;
168   SMESH_Actor* actor = getActor( ind );
169   if ( actor ) {
170     if ( actor->GetPointsLabeled()) types.append( "Point" );
171     if ( actor->GetCellsLabeled()) types.append( "Cell" );
172   }
173   return types;
174 }
175
176 //=======================================================================
177 //function : displayMode
178 //purpose  : return SMESH_Actor::EReperesent
179 //=======================================================================
180
181 QString SMESHGUI_Selection::displayMode( int ind ) const
182 {
183   SMESH_Actor* actor = getActor( ind );
184   if ( actor ) {
185     switch( actor->GetRepresentation() ) {
186     case SMESH_Actor::eEdge:    return "eEdge";
187     case SMESH_Actor::eSurface: return "eSurface";
188     case SMESH_Actor::ePoint:   return "ePoint";
189     default:;
190     }
191   }
192   return "Unknown";
193 }
194
195 //=======================================================================
196 //function : shrinkMode
197 //purpose  : return either 'IsSrunk', 'IsNotShrunk' or 'IsNotShrinkable'
198 //=======================================================================
199
200 QString SMESHGUI_Selection::shrinkMode( int ind ) const
201 {
202   SMESH_Actor* actor = getActor( ind );
203   if ( actor && actor->IsShrunkable() ) {
204     if ( actor->IsShrunk() )
205       return "IsShrunk";
206     return "IsNotShrunk";
207   }
208   return "IsNotShrinkable";
209 }
210
211 //=======================================================================
212 //function : entityMode
213 //purpose  : may return {'Edge' 'Face' 'Volume'} at most
214 //=======================================================================
215
216 QValueList<QVariant> SMESHGUI_Selection::entityMode( int ind ) const
217 {
218   QValueList<QVariant> types;
219   SMESH_Actor* actor = getActor( ind );
220   if ( actor ) {
221     unsigned int aMode = actor->GetEntityMode();
222     if ( aMode & SMESH_Actor::eVolumes) types.append( "Volume");
223     if ( aMode & SMESH_Actor::eFaces  ) types.append( "Face"  );
224     if ( aMode & SMESH_Actor::eEdges  ) types.append( "Edge"  );
225   }
226   return types;
227 }
228
229 //=======================================================================
230 //function : controlMode
231 //purpose  : return SMESH_Actor::eControl
232 //=======================================================================
233
234 QString SMESHGUI_Selection::controlMode( int ind ) const
235 {
236   SMESH_Actor* actor = getActor( ind );
237   if ( actor ) {
238     switch( actor->GetControlMode() ) {
239     case SMESH_Actor::eLength:            return "eLength";
240     case SMESH_Actor::eLength2D:          return "eLength2D";
241     case SMESH_Actor::eFreeEdges:         return "eFreeEdges";
242     case SMESH_Actor::eFreeBorders:       return "eFreeBorders";
243     case SMESH_Actor::eMultiConnection:   return "eMultiConnection";
244     case SMESH_Actor::eMultiConnection2D: return "eMultiConnection2D";
245     case SMESH_Actor::eArea:              return "eArea";
246     case SMESH_Actor::eVolume3D:          return "eVolume3D";
247     case SMESH_Actor::eTaper:             return "eTaper";
248     case SMESH_Actor::eAspectRatio:       return "eAspectRatio";
249     case SMESH_Actor::eAspectRatio3D:     return "eAspectRatio3D";
250     case SMESH_Actor::eMinimumAngle:      return "eMinimumAngle";
251     case SMESH_Actor::eWarping:           return "eWarping";
252     case SMESH_Actor::eSkew:              return "eSkew";
253     default:;
254     }
255   }
256   return "eNone";
257 }
258
259 //=======================================================================
260 //function : isAutoColor
261 //purpose  : 
262 //=======================================================================
263
264 bool SMESHGUI_Selection::isAutoColor( int ind ) const
265 {
266   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
267   {
268     _PTR(SObject) sobj = SMESH::GetActiveStudyDocument()->FindObjectID( entry( ind ).latin1() );
269     CORBA::Object_var obj = SMESH::SObjectToObject( sobj, SMESH::GetActiveStudyDocument() );
270
271     if ( ! CORBA::is_nil( obj )) {
272       SMESH::SMESH_Mesh_var mesh = SMESH::SMESH_Mesh::_narrow( obj );
273       if ( ! mesh->_is_nil() )
274         return mesh->GetAutoColor();
275     }
276   }
277   return false;
278 }
279
280 //=======================================================================
281 //function : numberOfNodes
282 //purpose  : 
283 //=======================================================================
284
285 int SMESHGUI_Selection::numberOfNodes( int ind ) const
286 {
287   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
288   {
289     _PTR(SObject) sobj = SMESH::GetActiveStudyDocument()->FindObjectID( entry( ind ).latin1() );
290     CORBA::Object_var obj = SMESH::SObjectToObject( sobj, SMESH::GetActiveStudyDocument() );
291
292     if ( ! CORBA::is_nil( obj )) {
293       SMESH::SMESH_Mesh_var mesh = SMESH::SMESH_Mesh::_narrow( obj );
294       if ( ! mesh->_is_nil() )
295         return mesh->NbNodes();
296       SMESH::SMESH_subMesh_var aSubMeshObj = SMESH::SMESH_subMesh::_narrow( obj );
297       if ( !aSubMeshObj->_is_nil() )
298         return aSubMeshObj->GetNumberOfNodes(true);
299       SMESH::SMESH_GroupBase_var aGroupObj = SMESH::SMESH_GroupBase::_narrow( obj );
300       if ( !aGroupObj->_is_nil() )
301         return aGroupObj->Size();
302     }
303   }
304   return 0;
305 }
306
307 //=======================================================================
308 //function : isComputable
309 //purpose  : 
310 //=======================================================================
311
312 QVariant SMESHGUI_Selection::isComputable( int ind ) const
313 {
314   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
315   {
316 /*    Handle(SALOME_InteractiveObject) io =
317       static_cast<LightApp_DataOwner*>( myDataOwners[ ind ].get() )->IO();
318     if ( !io.IsNull() ) {
319       SMESH::SMESH_Mesh_var mesh = SMESH::GetMeshByIO(io); // m,sm,gr->m
320       if ( !mesh->_is_nil() ) {*/
321         _PTR(SObject) so = SMESH::GetActiveStudyDocument()->FindObjectID( entry( ind ).latin1() );
322         //FindSObject( mesh );
323         if ( so ) {
324           CORBA::Object_var obj = SMESH::SObjectToObject(so, SMESH::GetActiveStudyDocument());
325           if(!CORBA::is_nil(obj)){
326             SMESH::SMESH_Mesh_var mesh = SMESH::SMESH_Mesh::_narrow( obj );
327             if (!mesh->_is_nil()){
328               if(mesh->HasShapeToMesh()) {
329                 GEOM::GEOM_Object_var shape = SMESH::GetShapeOnMeshOrSubMesh( so );
330                 return QVariant( !shape->_is_nil(), 0 );
331               }
332               else
333               {
334                 return QVariant(!mesh->NbFaces()==0, 0);
335               }
336             }
337             else
338             {
339               GEOM::GEOM_Object_var shape = SMESH::GetShapeOnMeshOrSubMesh( so );
340               return QVariant( !shape->_is_nil(), 0 );
341             }
342           }
343         }
344 //      }
345 //    }
346   }
347   return QVariant( false, 0 );
348 }
349
350 //=======================================================================
351 //function : hasReference
352 //purpose  : 
353 //=======================================================================
354
355 QVariant SMESHGUI_Selection::hasReference( int ind ) const
356 {
357   return QVariant( isReference( ind ), 0 );
358 }
359
360 //=======================================================================
361 //function : isVisible
362 //purpose  : 
363 //=======================================================================
364
365 QVariant SMESHGUI_Selection::isVisible( int ind ) const
366 {
367   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
368   {
369     QString ent = entry( ind );
370     SMESH_Actor* actor = SMESH::FindActorByEntry( ent.latin1() );
371     if ( actor && actor->hasIO() ) {
372       if(SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView())
373         return QVariant( aViewWindow->isVisible( actor->getIO() ), 0 );
374     }
375   }
376   return QVariant( false, 0 );
377 }
378
379 //=======================================================================
380 //function : type
381 //purpose  : 
382 //=======================================================================
383
384 int SMESHGUI_Selection::type( const QString& entry, _PTR(Study) study )
385 {
386   _PTR(SObject) obj (study->FindObjectID(entry.latin1()));
387   if( !obj )
388     return -1;
389
390   _PTR(SObject) ref;
391   if( obj->ReferencedObject( ref ) )
392     obj = ref;
393
394   _PTR(SObject) objFather = obj->GetFather();
395   _PTR(SComponent) objComponent = obj->GetFatherComponent();
396
397   if( objComponent->ComponentDataType()!="SMESH" )
398     return -1;
399
400   if( objComponent->GetIOR()==obj->GetIOR() )
401     return COMPONENT;
402
403   int aLevel = obj->Depth() - objComponent->Depth(),
404       aFTag = objFather->Tag(),
405       anOTag = obj->Tag(),
406       res = -1;
407
408   switch (aLevel)
409   {
410   case 1:
411     if (anOTag >= SMESH::Tag_FirstMeshRoot)
412       res = MESH;
413     break;
414   case 2:
415     switch (aFTag)
416     {
417     case SMESH::Tag_HypothesisRoot:
418       res = HYPOTHESIS;
419       break;
420     case SMESH::Tag_AlgorithmsRoot:
421       res = ALGORITHM;
422       break;
423     }
424     break;
425   case 3:
426     switch (aFTag)
427     {
428     case SMESH::Tag_SubMeshOnVertex:
429       res = SUBMESH_VERTEX;
430       break;
431     case SMESH::Tag_SubMeshOnEdge:
432       res = SUBMESH_EDGE;
433       break;
434     case SMESH::Tag_SubMeshOnFace:
435       res = SUBMESH_FACE;
436       break;
437     case SMESH::Tag_SubMeshOnSolid:
438       res = SUBMESH_SOLID;
439       break;
440     case SMESH::Tag_SubMeshOnCompound:
441       res = SUBMESH_COMPOUND;
442       break;
443     default:
444       if (aFTag >= SMESH::Tag_FirstGroup)
445         res = GROUP;
446       else
447         res = SUBMESH;
448     }
449     break;
450   }
451
452   return res;
453 }
454
455 //=======================================================================
456 //function : typeName
457 //purpose  : 
458 //=======================================================================
459
460 QString SMESHGUI_Selection::typeName( const int t )
461 {
462   switch( t )
463   {
464   case HYPOTHESIS:
465     return "Hypothesis";
466   case ALGORITHM:
467     return "Algorithm";
468   case MESH:
469     return "Mesh";
470   case SUBMESH:
471     return "SubMesh";
472   case MESHorSUBMESH:
473     return "Mesh or submesh";
474   case SUBMESH_VERTEX:
475     return "Mesh vertex";
476   case SUBMESH_EDGE:
477     return "Mesh edge";
478   case SUBMESH_FACE:
479     return "Mesh face";
480   case SUBMESH_SOLID:
481     return "Mesh solid";
482   case SUBMESH_COMPOUND:
483     return "Mesh compound";
484   case GROUP:
485     return "Group";
486   case COMPONENT:
487     return "Component";
488   default:
489     return "Unknown";
490   }
491 }
492
493 bool SMESHGUI_Selection::isImported( const int ind ) const
494 {
495   QString e = entry( ind );
496   _PTR(SObject) SO = SMESH::GetActiveStudyDocument()->FindObjectID( e );
497   bool res = false;
498   if( SO )
499   {
500     SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow( SMESH::SObjectToObject( SO ) );
501     if( !aMesh->_is_nil() )
502     {
503       SALOME_MED::MedFileInfo* inf = aMesh->GetMEDFileInfo();
504       res = strlen( (char*)inf->fileName ) > 0;
505     }
506   }
507   return res;
508 }