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