Salome HOME
follow modif for IPAL21558 (In Group creation dialog, selection of sub-shapes in...
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_Selection.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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 // SMESH SMESHGUI_Selection
23 // File   : SMESHGUI_Selection.cxx
24 // Author : Alexander SOLOVYOV, Open CASCADE S.A.S.
25 // SMESH includes
26 //
27 #include "SMESHGUI_Selection.h"
28
29 #include "SMESHGUI_Utils.h"
30 #include "SMESHGUI_VTKUtils.h"
31 #include "SMESHGUI_GEOMGenUtils.h"
32 #include "SMESHGUI_ComputeDlg.h"
33
34 #include <SMESH_Type.h>
35 #include <SMESH_Actor.h>
36
37 // SALOME GUI includes
38 #include <SalomeApp_Study.h>
39 #include <LightApp_VTKSelector.h>
40 #include <SVTK_ViewWindow.h>
41
42 // IDL includes
43 #include <SALOMEconfig.h>
44 #include CORBA_CLIENT_HEADER(SMESH_Gen)
45 #include CORBA_CLIENT_HEADER(SMESH_Mesh)
46 #include CORBA_CLIENT_HEADER(SMESH_Group)
47
48 //=======================================================================
49 //function : SMESHGUI_Selection
50 //purpose  : 
51 //=======================================================================
52 SMESHGUI_Selection::SMESHGUI_Selection()
53 : LightApp_Selection()
54 {
55 }
56
57 //=======================================================================
58 //function : ~SMESHGUI_Selection
59 //purpose  : 
60 //=======================================================================
61 SMESHGUI_Selection::~SMESHGUI_Selection()
62 {
63 }
64
65 //=======================================================================
66 //function : init
67 //purpose  : 
68 //=======================================================================
69 void SMESHGUI_Selection::init( const QString& client, LightApp_SelectionMgr* mgr )
70 {
71   LightApp_Selection::init( client, mgr );
72
73   if( mgr && study() )
74   {
75     SalomeApp_Study* aSStudy = dynamic_cast<SalomeApp_Study*>(study());
76     if (!aSStudy)
77       return;
78     _PTR(Study) aStudy = aSStudy->studyDS();
79
80     for( int i=0, n=count(); i<n; i++ )
81       myTypes.append( typeName( type( entry( i ), aStudy ) ) );
82   }
83 }
84
85 //=======================================================================
86 //function : processOwner
87 //purpose  : 
88 //=======================================================================
89 bool SMESHGUI_Selection::processOwner( const LightApp_DataOwner* ow )
90 {
91   const LightApp_SVTKDataOwner* owner =
92     dynamic_cast<const LightApp_SVTKDataOwner*> ( ow );
93   if( owner )
94     myActors.append( dynamic_cast<SMESH_Actor*>( owner->GetActor() ) );
95   else
96     myActors.append( 0 );
97   return true;
98 }
99
100 //=======================================================================
101 //function : parameter
102 //purpose  : 
103 //=======================================================================
104 QVariant SMESHGUI_Selection::parameter( const int ind, const QString& p ) const
105 {
106   QVariant val;
107   if      ( p=="client" )        val = QVariant( LightApp_Selection::parameter( p ) );
108   else if ( p=="type" )          val = QVariant( myTypes[ind] );
109   else if ( p=="elemTypes" )     val = QVariant( elemTypes( ind ) );
110   else if ( p=="isAutoColor" )   val = QVariant( isAutoColor( ind ) );
111   else if ( p=="numberOfNodes" ) val = QVariant( numberOfNodes( ind ) );
112   else if ( p=="labeledTypes" )  val = QVariant( labeledTypes( ind ) );
113   else if ( p=="shrinkMode" )    val = QVariant( shrinkMode( ind ) );
114   else if ( p=="entityMode" )    val = QVariant( entityMode( ind ) );
115   else if ( p=="controlMode" )   val = QVariant( controlMode( ind ) );
116   else if ( p=="displayMode" )   val = QVariant( displayMode( ind ) );
117   else if ( p=="isComputable" )  val = QVariant( isComputable( ind ) );
118   else if ( p=="isPreComputable" )  val = QVariant( isPreComputable( ind ) );
119   else if ( p=="hasReference" )  val = QVariant( hasReference( ind ) );
120   else if ( p=="isImported" )    val = QVariant( isImported( ind ) );
121   else if ( p=="facesOrientationMode" ) val = QVariant( facesOrientationMode( ind ) );
122   else if ( p=="groupType" )     val = QVariant( groupType( ind ) );
123   else if ( p=="quadratic2DMode") val =  QVariant(quadratic2DMode(ind));
124
125   if( val.isValid() )
126     return val;
127   else
128     return LightApp_Selection::parameter( ind, p );
129 }
130
131 //=======================================================================
132 //function : getVtkOwner
133 //purpose  : 
134 //=======================================================================
135
136 SMESH_Actor* SMESHGUI_Selection::getActor( int ind ) const
137 {
138   if( ind >= 0 && ind < count() )
139     return myActors.isEmpty() ? 0 : myActors.at( ind );
140   else
141     return 0;
142 }
143
144 //=======================================================================
145 //function : elemTypes
146 //purpose  : may return {'Elem0d' 'Edge' 'Face' 'Volume'} at most
147 //=======================================================================
148
149 QList<QVariant> SMESHGUI_Selection::elemTypes( int ind ) const
150 {
151   QList<QVariant> types;
152   SMESH_Actor* actor = getActor( ind );
153   if ( actor ) {
154     TVisualObjPtr object = actor->GetObject();
155     if ( object ) {
156       if ( object->GetNbEntities( SMDSAbs_0DElement )) types.append( "Elem0d" );
157       if ( object->GetNbEntities( SMDSAbs_Edge )) types.append( "Edge" );
158       if ( object->GetNbEntities( SMDSAbs_Face )) types.append( "Face" );
159       if ( object->GetNbEntities( SMDSAbs_Volume )) types.append( "Volume" );
160     }
161   }
162   return types;
163 }
164
165 //=======================================================================
166 //function : labeledTypes
167 //purpose  : may return {'Point' 'Cell'} at most
168 //=======================================================================
169
170 QList<QVariant> SMESHGUI_Selection::labeledTypes( int ind ) const
171 {
172   QList<QVariant> types;
173   SMESH_Actor* actor = getActor( ind );
174   if ( actor ) {
175     if ( actor->GetPointsLabeled()) types.append( "Point" );
176     if ( actor->GetCellsLabeled()) types.append( "Cell" );
177   }
178   return types;
179 }
180
181 //=======================================================================
182 //function : displayMode
183 //purpose  : return SMESH_Actor::EReperesent
184 //=======================================================================
185
186 QString SMESHGUI_Selection::displayMode( int ind ) const
187 {
188   SMESH_Actor* actor = getActor( ind );
189   if ( actor ) {
190     switch( actor->GetRepresentation() ) {
191     case SMESH_Actor::eEdge:    return "eEdge";
192     case SMESH_Actor::eSurface: return "eSurface";
193     case SMESH_Actor::ePoint:   return "ePoint";
194     default: break;
195     }
196   }
197   return "Unknown";
198 }
199
200
201 //=======================================================================
202 //function : quadratic2DMode
203 //purpose  : return SMESH_Actor::EQuadratic2DRepresentation
204 //=======================================================================
205 QString SMESHGUI_Selection::quadratic2DMode( int ind ) const
206 {
207   SMESH_Actor* actor = getActor( ind );
208   if ( actor ) {
209     switch( actor->GetQuadratic2DRepresentation() ) {
210     case SMESH_Actor::eLines:    return "eLines";
211     case SMESH_Actor::eArcs: return "eArcs";
212     default: break;
213     }
214   }
215   return "Unknown";
216 }
217
218 //=======================================================================
219 //function : shrinkMode
220 //purpose  : return either 'IsSrunk', 'IsNotShrunk' or 'IsNotShrinkable'
221 //=======================================================================
222
223 QString SMESHGUI_Selection::shrinkMode( int ind ) const
224 {
225   SMESH_Actor* actor = getActor( ind );
226   if ( actor && actor->IsShrunkable() ) {
227     if ( actor->IsShrunk() )
228       return "IsShrunk";
229     return "IsNotShrunk";
230   }
231   return "IsNotShrinkable";
232 }
233
234 //=======================================================================
235 //function : entityMode
236 //purpose  : may return {'Elem0d' 'Edge' 'Face' 'Volume'} at most
237 //=======================================================================
238
239 QList<QVariant> SMESHGUI_Selection::entityMode( int ind ) const
240 {
241   QList<QVariant> types;
242   SMESH_Actor* actor = getActor( ind );
243   if ( actor ) {
244     unsigned int aMode = actor->GetEntityMode();
245     if ( aMode & SMESH_Actor::eVolumes    ) types.append( "Volume" );
246     if ( aMode & SMESH_Actor::eFaces      ) types.append( "Face"   );
247     if ( aMode & SMESH_Actor::eEdges      ) types.append( "Edge"   );
248     if ( aMode & SMESH_Actor::e0DElements ) types.append( "Elem0d" );
249   }
250   return types;
251 }
252
253 //=======================================================================
254 //function : controlMode
255 //purpose  : return SMESH_Actor::eControl
256 //=======================================================================
257
258 QString SMESHGUI_Selection::controlMode( int ind ) const
259 {
260   SMESH_Actor* actor = getActor( ind );
261   if ( actor ) {
262     switch( actor->GetControlMode() ) {
263     case SMESH_Actor::eLength:            return "eLength";
264     case SMESH_Actor::eLength2D:          return "eLength2D";
265     case SMESH_Actor::eFreeEdges:         return "eFreeEdges";
266     case SMESH_Actor::eFreeNodes:         return "eFreeNodes";
267     case SMESH_Actor::eFreeBorders:       return "eFreeBorders";
268     case SMESH_Actor::eFreeFaces:         return "eFreeFaces";
269     case SMESH_Actor::eMultiConnection:   return "eMultiConnection";
270     case SMESH_Actor::eMultiConnection2D: return "eMultiConnection2D";
271     case SMESH_Actor::eArea:              return "eArea";
272     case SMESH_Actor::eVolume3D:          return "eVolume3D";
273     case SMESH_Actor::eTaper:             return "eTaper";
274     case SMESH_Actor::eAspectRatio:       return "eAspectRatio";
275     case SMESH_Actor::eAspectRatio3D:     return "eAspectRatio3D";
276     case SMESH_Actor::eMinimumAngle:      return "eMinimumAngle";
277     case SMESH_Actor::eWarping:           return "eWarping";
278     case SMESH_Actor::eSkew:              return "eSkew";
279     default:;
280     }
281   }
282   return "eNone";
283 }
284
285 //=======================================================================
286 //function : facesOrientationMode
287 //purpose  : 
288 //=======================================================================
289
290 QString SMESHGUI_Selection::facesOrientationMode( int ind ) const
291 {
292   SMESH_Actor* actor = getActor( ind );
293   if ( actor ) {
294     if ( actor->GetFacesOriented() )
295       return "IsOriented";
296     return "IsNotOriented";
297   }
298   return "Unknown";
299 }
300
301 //=======================================================================
302 //function : isAutoColor
303 //purpose  : 
304 //=======================================================================
305
306 bool SMESHGUI_Selection::isAutoColor( int ind ) const
307 {
308   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
309   {
310     _PTR(SObject) sobj = SMESH::GetActiveStudyDocument()->FindObjectID( entry( ind ).toLatin1().data() );
311     CORBA::Object_var obj = SMESH::SObjectToObject( sobj, SMESH::GetActiveStudyDocument() );
312
313     if ( ! CORBA::is_nil( obj )) {
314       SMESH::SMESH_Mesh_var mesh = SMESH::SMESH_Mesh::_narrow( obj );
315       if ( ! mesh->_is_nil() )
316         return mesh->GetAutoColor();
317     }
318   }
319   return false;
320 }
321
322 //=======================================================================
323 //function : numberOfNodes
324 //purpose  : 
325 //=======================================================================
326
327 int SMESHGUI_Selection::numberOfNodes( int ind ) const
328 {
329   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
330   {
331     _PTR(SObject) sobj = SMESH::GetActiveStudyDocument()->FindObjectID( entry( ind ).toLatin1().data() );
332     CORBA::Object_var obj = SMESH::SObjectToObject( sobj, SMESH::GetActiveStudyDocument() );
333
334     if ( ! CORBA::is_nil( obj )) {
335       SMESH::SMESH_Mesh_var mesh = SMESH::SMESH_Mesh::_narrow( obj );
336       if ( ! mesh->_is_nil() )
337         return mesh->NbNodes();
338       SMESH::SMESH_subMesh_var aSubMeshObj = SMESH::SMESH_subMesh::_narrow( obj );
339       if ( !aSubMeshObj->_is_nil() )
340         return aSubMeshObj->GetNumberOfNodes(true);
341       SMESH::SMESH_GroupBase_var aGroupObj = SMESH::SMESH_GroupBase::_narrow( obj );
342       if ( !aGroupObj->_is_nil() )
343         return aGroupObj->Size();
344     }
345   }
346   return 0;
347 }
348
349 //=======================================================================
350 //function : isComputable
351 //purpose  : 
352 //=======================================================================
353
354 QVariant SMESHGUI_Selection::isComputable( int ind ) const
355 {
356   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
357   {
358 /*    Handle(SALOME_InteractiveObject) io =
359       static_cast<LightApp_DataOwner*>( myDataOwners[ ind ].get() )->IO();
360     if ( !io.IsNull() ) {
361       SMESH::SMESH_Mesh_var mesh = SMESH::GetMeshByIO(io); // m,sm,gr->m
362       if ( !mesh->_is_nil() ) {*/
363         _PTR(SObject) so = SMESH::GetActiveStudyDocument()->FindObjectID( entry( ind ).toLatin1().data() );
364         //FindSObject( mesh );
365         if ( so ) {
366           CORBA::Object_var obj = SMESH::SObjectToObject(so, SMESH::GetActiveStudyDocument());
367           if(!CORBA::is_nil(obj)){
368             SMESH::SMESH_Mesh_var mesh = SMESH::SMESH_Mesh::_narrow( obj );
369             if (!mesh->_is_nil()){
370               if(mesh->HasShapeToMesh()) {
371                 GEOM::GEOM_Object_var shape = SMESH::GetShapeOnMeshOrSubMesh( so );
372                 return QVariant( !shape->_is_nil() );
373               }
374               else
375               {
376                 return QVariant(!mesh->NbFaces()==0);
377               }
378             }
379             else
380             {
381               GEOM::GEOM_Object_var shape = SMESH::GetShapeOnMeshOrSubMesh( so );
382               return QVariant( !shape->_is_nil() );
383             }
384           }
385         }
386 //      }
387 //    }
388   }
389   return QVariant( false );
390 }
391
392 //=======================================================================
393 //function : isPreComputable
394 //purpose  : 
395 //=======================================================================
396
397 QVariant SMESHGUI_Selection::isPreComputable( int ind ) const
398 {
399   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
400   {
401     QMap<int,int> modeMap;
402     _PTR(SObject) pMesh = SMESH::GetActiveStudyDocument()->FindObjectID( entry( ind ).toLatin1().data() );
403     SMESHGUI_PrecomputeOp::getAssignedAlgos( pMesh, modeMap );
404     return QVariant( modeMap.size() > 1 );
405   }
406   return QVariant( false );
407 }
408
409 //=======================================================================
410 //function : hasReference
411 //purpose  : 
412 //=======================================================================
413
414 QVariant SMESHGUI_Selection::hasReference( int ind ) const
415 {
416   return QVariant( isReference( ind ) );
417 }
418
419 //=======================================================================
420 //function : isVisible
421 //purpose  : 
422 //=======================================================================
423
424 QVariant SMESHGUI_Selection::isVisible( int ind ) const
425 {
426   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
427   {
428     QString ent = entry( ind );
429     SMESH_Actor* actor = SMESH::FindActorByEntry( ent.toLatin1().data() );
430     if ( actor && actor->hasIO() ) {
431       if(SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView())
432         return QVariant( aViewWindow->isVisible( actor->getIO() ) );
433     }
434   }
435   return QVariant( false );
436 }
437
438 //=======================================================================
439 //function : type
440 //purpose  : 
441 //=======================================================================
442
443 int SMESHGUI_Selection::type( const QString& entry, _PTR(Study) study )
444 {
445   _PTR(SObject) obj (study->FindObjectID(entry.toLatin1().data()));
446   if( !obj )
447     return -1;
448
449   _PTR(SObject) ref;
450   if( obj->ReferencedObject( ref ) )
451     obj = ref;
452
453   _PTR(SObject) objFather = obj->GetFather();
454   _PTR(SComponent) objComponent = obj->GetFatherComponent();
455
456   if( objComponent->ComponentDataType()!="SMESH" )
457     return -1;
458
459   if( objComponent->GetIOR()==obj->GetIOR() )
460     return COMPONENT;
461
462   int aLevel = obj->Depth() - objComponent->Depth(),
463       aFTag = objFather->Tag(),
464       anOTag = obj->Tag(),
465       res = -1;
466
467   switch (aLevel)
468   {
469   case 1:
470     if (anOTag >= SMESH::Tag_FirstMeshRoot)
471       res = MESH;
472     break;
473   case 2:
474     switch (aFTag)
475     {
476     case SMESH::Tag_HypothesisRoot:
477       res = HYPOTHESIS;
478       break;
479     case SMESH::Tag_AlgorithmsRoot:
480       res = ALGORITHM;
481       break;
482     }
483     break;
484   case 3:
485     switch (aFTag)
486     {
487     case SMESH::Tag_SubMeshOnVertex:
488       res = SUBMESH_VERTEX;
489       break;
490     case SMESH::Tag_SubMeshOnEdge:
491       res = SUBMESH_EDGE;
492       break;
493     case SMESH::Tag_SubMeshOnFace:
494       res = SUBMESH_FACE;
495       break;
496     case SMESH::Tag_SubMeshOnSolid:
497       res = SUBMESH_SOLID;
498       break;
499     case SMESH::Tag_SubMeshOnCompound:
500       res = SUBMESH_COMPOUND;
501       break;
502     default:
503       if (aFTag >= SMESH::Tag_FirstGroup)
504         res = GROUP;
505       else
506         res = SUBMESH;
507     }
508     break;
509   }
510
511   return res;
512 }
513
514 //=======================================================================
515 //function : typeName
516 //purpose  : 
517 //=======================================================================
518
519 QString SMESHGUI_Selection::typeName( const int t )
520 {
521   switch( t )
522   {
523   case HYPOTHESIS:
524     return "Hypothesis";
525   case ALGORITHM:
526     return "Algorithm";
527   case MESH:
528     return "Mesh";
529   case SUBMESH:
530     return "SubMesh";
531   case MESHorSUBMESH:
532     return "Mesh or submesh";
533   case SUBMESH_VERTEX:
534     return "Mesh vertex";
535   case SUBMESH_EDGE:
536     return "Mesh edge";
537   case SUBMESH_FACE:
538     return "Mesh face";
539   case SUBMESH_SOLID:
540     return "Mesh solid";
541   case SUBMESH_COMPOUND:
542     return "Mesh compound";
543   case GROUP:
544     return "Group";
545   case COMPONENT:
546     return "Component";
547   default:
548     return "Unknown";
549   }
550 }
551
552 bool SMESHGUI_Selection::isImported( const int ind ) const
553 {
554   QString e = entry( ind );
555   _PTR(SObject) SO = SMESH::GetActiveStudyDocument()->FindObjectID( e.toLatin1().constData() );
556   bool res = false;
557   if( SO )
558   {
559     SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow( SMESH::SObjectToObject( SO ) );
560     if( !aMesh->_is_nil() )
561     {
562       SALOME_MED::MedFileInfo* inf = aMesh->GetMEDFileInfo();
563       res = strlen( (char*)inf->fileName ) > 0;
564     }
565   }
566   return res;
567 }
568
569 //=======================================================================
570 //function : groupType
571 //purpose  : 
572 //=======================================================================
573
574 QString SMESHGUI_Selection::groupType( int ind ) const
575 {
576   QString e = entry( ind );
577   _PTR(SObject) SO = SMESH::GetActiveStudyDocument()->FindObjectID( e.toLatin1().constData() );
578   QString type;
579   if( SO )
580   {
581     CORBA::Object_var obj = SMESH::SObjectToObject( SO );
582   
583     SMESH::SMESH_Group_var aGroup = SMESH::SMESH_Group::_narrow( obj );
584     SMESH::SMESH_GroupOnGeom_var aGroupOnGeom = SMESH::SMESH_GroupOnGeom::_narrow( obj );
585     if( !aGroup->_is_nil() )
586       type = QString( "Group" );
587     else if ( !aGroupOnGeom->_is_nil() )
588       type = QString( "GroupOnGeom" );
589   }
590   return type;
591 }
592