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