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