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