Salome HOME
IMP19942 - Convert group on geometry into group of elements
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_Selection.cxx
1 // SMESH SMESHGUI_Selection
2 //
3 // Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 // File   : SMESHGUI_Selection.cxx
23 // Author : Alexander SOLOVYOV, Open CASCADE S.A.S.
24 //
25
26 // SMESH includes
27 #include "SMESHGUI_Selection.h"
28
29 #include "SMESHGUI_Utils.h"
30 #include "SMESHGUI_VTKUtils.h"
31 #include "SMESHGUI_GEOMGenUtils.h"
32
33 #include <SMESH_Type.h>
34 #include <SMESH_Actor.h>
35
36 // SALOME GUI includes
37 #include <SalomeApp_Study.h>
38 #include <LightApp_VTKSelector.h>
39 #include <SVTK_ViewWindow.h>
40
41 // IDL includes
42 #include <SALOMEconfig.h>
43 #include CORBA_CLIENT_HEADER(SMESH_Gen)
44 #include CORBA_CLIENT_HEADER(SMESH_Mesh)
45 #include CORBA_CLIENT_HEADER(SMESH_Group)
46
47 //=======================================================================
48 //function : SMESHGUI_Selection
49 //purpose  : 
50 //=======================================================================
51 SMESHGUI_Selection::SMESHGUI_Selection()
52 : LightApp_Selection()
53 {
54 }
55
56 //=======================================================================
57 //function : ~SMESHGUI_Selection
58 //purpose  : 
59 //=======================================================================
60 SMESHGUI_Selection::~SMESHGUI_Selection()
61 {
62 }
63
64 //=======================================================================
65 //function : init
66 //purpose  : 
67 //=======================================================================
68 void SMESHGUI_Selection::init( const QString& client, LightApp_SelectionMgr* mgr )
69 {
70   LightApp_Selection::init( client, mgr );
71
72   if( mgr && study() )
73   {
74     SalomeApp_Study* aSStudy = dynamic_cast<SalomeApp_Study*>(study());
75     if (!aSStudy)
76       return;
77     _PTR(Study) aStudy = aSStudy->studyDS();
78
79     for( int i=0, n=count(); i<n; i++ )
80       myTypes.append( typeName( type( entry( i ), aStudy ) ) );
81   }
82 }
83
84 //=======================================================================
85 //function : processOwner
86 //purpose  : 
87 //=======================================================================
88 void SMESHGUI_Selection::processOwner( const LightApp_DataOwner* ow )
89 {
90   const LightApp_SVTKDataOwner* owner =
91     dynamic_cast<const LightApp_SVTKDataOwner*> ( ow );
92   if( owner )
93     myActors.append( dynamic_cast<SMESH_Actor*>( owner->GetActor() ) );
94   else
95     myActors.append( 0 );
96 }
97
98 //=======================================================================
99 //function : parameter
100 //purpose  : 
101 //=======================================================================
102 QVariant SMESHGUI_Selection::parameter( const int ind, const QString& p ) const
103 {
104   QVariant val;
105   if      ( p=="client" )        val = QVariant( LightApp_Selection::parameter( p ) );
106   else if ( p=="type" )          val = QVariant( myTypes[ind] );
107   else if ( p=="elemTypes" )     val = QVariant( elemTypes( ind ) );
108   else if ( p=="isAutoColor" )   val = QVariant( isAutoColor( ind ) );
109   else if ( p=="numberOfNodes" ) val = QVariant( numberOfNodes( ind ) );
110   else if ( p=="labeledTypes" )  val = QVariant( labeledTypes( ind ) );
111   else if ( p=="shrinkMode" )    val = QVariant( shrinkMode( ind ) );
112   else if ( p=="entityMode" )    val = QVariant( entityMode( ind ) );
113   else if ( p=="controlMode" )   val = QVariant( controlMode( ind ) );
114   else if ( p=="displayMode" )   val = QVariant( displayMode( ind ) );
115   else if ( p=="isComputable" )  val = QVariant( isComputable( ind ) );
116   else if ( p=="hasReference" )  val = QVariant( hasReference( ind ) );
117   else if ( p=="isImported" )    val = QVariant( isImported( ind ) );
118   else if ( p=="facesOrientationMode" ) val = QVariant( facesOrientationMode( ind ) );
119   else if ( p=="groupType" )     val = QVariant( groupType( ind ) );
120
121   if( val.isValid() )
122     return val;
123   else
124     return LightApp_Selection::parameter( 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 myActors.isEmpty() ? 0 : 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 QList<QVariant> SMESHGUI_Selection::elemTypes( int ind ) const
146 {
147   QList<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 QList<QVariant> SMESHGUI_Selection::labeledTypes( int ind ) const
166 {
167   QList<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: break;
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 QList<QVariant> SMESHGUI_Selection::entityMode( int ind ) const
217 {
218   QList<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 : facesOrientationMode
261 //purpose  : 
262 //=======================================================================
263
264 QString SMESHGUI_Selection::facesOrientationMode( int ind ) const
265 {
266   SMESH_Actor* actor = getActor( ind );
267   if ( actor ) {
268     if ( actor->GetFacesOriented() )
269       return "IsOriented";
270     return "IsNotOriented";
271   }
272   return "Unknown";
273 }
274
275 //=======================================================================
276 //function : isAutoColor
277 //purpose  : 
278 //=======================================================================
279
280 bool SMESHGUI_Selection::isAutoColor( int ind ) const
281 {
282   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
283   {
284     _PTR(SObject) sobj = SMESH::GetActiveStudyDocument()->FindObjectID( entry( ind ).toLatin1().data() );
285     CORBA::Object_var obj = SMESH::SObjectToObject( sobj, SMESH::GetActiveStudyDocument() );
286
287     if ( ! CORBA::is_nil( obj )) {
288       SMESH::SMESH_Mesh_var mesh = SMESH::SMESH_Mesh::_narrow( obj );
289       if ( ! mesh->_is_nil() )
290         return mesh->GetAutoColor();
291     }
292   }
293   return false;
294 }
295
296 //=======================================================================
297 //function : numberOfNodes
298 //purpose  : 
299 //=======================================================================
300
301 int SMESHGUI_Selection::numberOfNodes( int ind ) const
302 {
303   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
304   {
305     _PTR(SObject) sobj = SMESH::GetActiveStudyDocument()->FindObjectID( entry( ind ).toLatin1().data() );
306     CORBA::Object_var obj = SMESH::SObjectToObject( sobj, SMESH::GetActiveStudyDocument() );
307
308     if ( ! CORBA::is_nil( obj )) {
309       SMESH::SMESH_Mesh_var mesh = SMESH::SMESH_Mesh::_narrow( obj );
310       if ( ! mesh->_is_nil() )
311         return mesh->NbNodes();
312       SMESH::SMESH_subMesh_var aSubMeshObj = SMESH::SMESH_subMesh::_narrow( obj );
313       if ( !aSubMeshObj->_is_nil() )
314         return aSubMeshObj->GetNumberOfNodes(true);
315       SMESH::SMESH_GroupBase_var aGroupObj = SMESH::SMESH_GroupBase::_narrow( obj );
316       if ( !aGroupObj->_is_nil() )
317         return aGroupObj->Size();
318     }
319   }
320   return 0;
321 }
322
323 //=======================================================================
324 //function : isComputable
325 //purpose  : 
326 //=======================================================================
327
328 QVariant SMESHGUI_Selection::isComputable( int ind ) const
329 {
330   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
331   {
332 /*    Handle(SALOME_InteractiveObject) io =
333       static_cast<LightApp_DataOwner*>( myDataOwners[ ind ].get() )->IO();
334     if ( !io.IsNull() ) {
335       SMESH::SMESH_Mesh_var mesh = SMESH::GetMeshByIO(io); // m,sm,gr->m
336       if ( !mesh->_is_nil() ) {*/
337         _PTR(SObject) so = SMESH::GetActiveStudyDocument()->FindObjectID( entry( ind ).toLatin1().data() );
338         //FindSObject( mesh );
339         if ( so ) {
340           CORBA::Object_var obj = SMESH::SObjectToObject(so, SMESH::GetActiveStudyDocument());
341           if(!CORBA::is_nil(obj)){
342             SMESH::SMESH_Mesh_var mesh = SMESH::SMESH_Mesh::_narrow( obj );
343             if (!mesh->_is_nil()){
344               if(mesh->HasShapeToMesh()) {
345                 GEOM::GEOM_Object_var shape = SMESH::GetShapeOnMeshOrSubMesh( so );
346                 return QVariant( !shape->_is_nil() );
347               }
348               else
349               {
350                 return QVariant(!mesh->NbFaces()==0);
351               }
352             }
353             else
354             {
355               GEOM::GEOM_Object_var shape = SMESH::GetShapeOnMeshOrSubMesh( so );
356               return QVariant( !shape->_is_nil() );
357             }
358           }
359         }
360 //      }
361 //    }
362   }
363   return QVariant( false );
364 }
365
366 //=======================================================================
367 //function : hasReference
368 //purpose  : 
369 //=======================================================================
370
371 QVariant SMESHGUI_Selection::hasReference( int ind ) const
372 {
373   return QVariant( isReference( ind ) );
374 }
375
376 //=======================================================================
377 //function : isVisible
378 //purpose  : 
379 //=======================================================================
380
381 QVariant SMESHGUI_Selection::isVisible( int ind ) const
382 {
383   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
384   {
385     QString ent = entry( ind );
386     SMESH_Actor* actor = SMESH::FindActorByEntry( ent.toLatin1().data() );
387     if ( actor && actor->hasIO() ) {
388       if(SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView())
389         return QVariant( aViewWindow->isVisible( actor->getIO() ) );
390     }
391   }
392   return QVariant( false );
393 }
394
395 //=======================================================================
396 //function : type
397 //purpose  : 
398 //=======================================================================
399
400 int SMESHGUI_Selection::type( const QString& entry, _PTR(Study) study )
401 {
402   _PTR(SObject) obj (study->FindObjectID(entry.toLatin1().data()));
403   if( !obj )
404     return -1;
405
406   _PTR(SObject) ref;
407   if( obj->ReferencedObject( ref ) )
408     obj = ref;
409
410   _PTR(SObject) objFather = obj->GetFather();
411   _PTR(SComponent) objComponent = obj->GetFatherComponent();
412
413   if( objComponent->ComponentDataType()!="SMESH" )
414     return -1;
415
416   if( objComponent->GetIOR()==obj->GetIOR() )
417     return COMPONENT;
418
419   int aLevel = obj->Depth() - objComponent->Depth(),
420       aFTag = objFather->Tag(),
421       anOTag = obj->Tag(),
422       res = -1;
423
424   switch (aLevel)
425   {
426   case 1:
427     if (anOTag >= SMESH::Tag_FirstMeshRoot)
428       res = MESH;
429     break;
430   case 2:
431     switch (aFTag)
432     {
433     case SMESH::Tag_HypothesisRoot:
434       res = HYPOTHESIS;
435       break;
436     case SMESH::Tag_AlgorithmsRoot:
437       res = ALGORITHM;
438       break;
439     }
440     break;
441   case 3:
442     switch (aFTag)
443     {
444     case SMESH::Tag_SubMeshOnVertex:
445       res = SUBMESH_VERTEX;
446       break;
447     case SMESH::Tag_SubMeshOnEdge:
448       res = SUBMESH_EDGE;
449       break;
450     case SMESH::Tag_SubMeshOnFace:
451       res = SUBMESH_FACE;
452       break;
453     case SMESH::Tag_SubMeshOnSolid:
454       res = SUBMESH_SOLID;
455       break;
456     case SMESH::Tag_SubMeshOnCompound:
457       res = SUBMESH_COMPOUND;
458       break;
459     default:
460       if (aFTag >= SMESH::Tag_FirstGroup)
461         res = GROUP;
462       else
463         res = SUBMESH;
464     }
465     break;
466   }
467
468   return res;
469 }
470
471 //=======================================================================
472 //function : typeName
473 //purpose  : 
474 //=======================================================================
475
476 QString SMESHGUI_Selection::typeName( const int t )
477 {
478   switch( t )
479   {
480   case HYPOTHESIS:
481     return "Hypothesis";
482   case ALGORITHM:
483     return "Algorithm";
484   case MESH:
485     return "Mesh";
486   case SUBMESH:
487     return "SubMesh";
488   case MESHorSUBMESH:
489     return "Mesh or submesh";
490   case SUBMESH_VERTEX:
491     return "Mesh vertex";
492   case SUBMESH_EDGE:
493     return "Mesh edge";
494   case SUBMESH_FACE:
495     return "Mesh face";
496   case SUBMESH_SOLID:
497     return "Mesh solid";
498   case SUBMESH_COMPOUND:
499     return "Mesh compound";
500   case GROUP:
501     return "Group";
502   case COMPONENT:
503     return "Component";
504   default:
505     return "Unknown";
506   }
507 }
508
509 bool SMESHGUI_Selection::isImported( const int ind ) const
510 {
511   QString e = entry( ind );
512   _PTR(SObject) SO = SMESH::GetActiveStudyDocument()->FindObjectID( e.toLatin1().constData() );
513   bool res = false;
514   /*
515   if( SO )
516   {
517     SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow( SMESH::SObjectToObject( SO ) );
518     if( !aMesh->_is_nil() )
519     {
520       SALOME_MED::MedFileInfo* inf = aMesh->GetMEDFileInfo();
521       res = strlen( (char*)inf->fileName ) > 0;
522     }
523   }
524   */
525   return res;
526 }
527
528 //=======================================================================
529 //function : groupType
530 //purpose  : 
531 //=======================================================================
532
533 QString SMESHGUI_Selection::groupType( int ind ) const
534 {
535   QString e = entry( ind );
536   _PTR(SObject) SO = SMESH::GetActiveStudyDocument()->FindObjectID( e.toLatin1().constData() );
537   QString type;
538   if( SO )
539   {
540     CORBA::Object_var obj = SMESH::SObjectToObject( SO );
541   
542     SMESH::SMESH_Group_var aGroup = SMESH::SMESH_Group::_narrow( obj );
543     SMESH::SMESH_GroupOnGeom_var aGroupOnGeom = SMESH::SMESH_GroupOnGeom::_narrow( obj );
544     if( !aGroup->_is_nil() )
545       type = QString( "Group" );
546     else if ( !aGroupOnGeom->_is_nil() )
547       type = QString( "GroupOnGeom" );
548   }
549   return type;
550 }