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