Salome HOME
Copyright update 2021
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_Selection.cxx
1 // Copyright (C) 2007-2021  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, or (at your option) any later version.
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 //
27
28 // SMESH includes
29 #include "SMESHGUI_Selection.h"
30
31 #include "SMESHGUI.h"
32 #include "SMESHGUI_ComputeDlg.h"
33 #include "SMESHGUI_ConvToQuadOp.h"
34 #include "SMESHGUI_GEOMGenUtils.h"
35 #include "SMESHGUI_Utils.h"
36 #include "SMESHGUI_VTKUtils.h"
37
38 #include <SMESH_Type.h>
39 #include <SMESH_Actor.h>
40 #include <SMESH_ScalarBarActor.h>
41
42 // SALOME GUI includes
43 #include <SalomeApp_Application.h>
44 #include <LightApp_VTKSelector.h>
45 #include <SVTK_ViewWindow.h>
46
47 // IDL includes
48 #include <SALOMEconfig.h>
49 #include CORBA_CLIENT_HEADER(SMESH_Gen)
50 #include CORBA_CLIENT_HEADER(SMESH_Mesh)
51 #include CORBA_CLIENT_HEADER(SMESH_Group)
52
53 //=======================================================================
54 //function : SMESHGUI_Selection
55 //purpose  : 
56 //=======================================================================
57 SMESHGUI_Selection::SMESHGUI_Selection()
58 : LightApp_Selection()
59 {
60 }
61
62 //=======================================================================
63 //function : ~SMESHGUI_Selection
64 //purpose  : 
65 //=======================================================================
66 SMESHGUI_Selection::~SMESHGUI_Selection()
67 {
68 }
69
70 //=======================================================================
71 //function : init
72 //purpose  : 
73 //=======================================================================
74 void SMESHGUI_Selection::init( const QString& client, LightApp_SelectionMgr* mgr )
75 {
76   LightApp_Selection::init( client, mgr );
77
78   if( mgr )
79   {
80     for( int i=0, n=count(); i<n; i++ ) {
81       myTypes.append( typeName( type( entry( i ) ) ) );
82       myControls.append( controlMode( i ) );
83     }
84   }
85 }
86
87 //=======================================================================
88 //function : processOwner
89 //purpose  : 
90 //=======================================================================
91 bool SMESHGUI_Selection::processOwner( const LightApp_DataOwner* ow )
92 {
93   const LightApp_SVTKDataOwner* owner =
94     dynamic_cast<const LightApp_SVTKDataOwner*> ( ow );
95   if( owner ) {
96     myActors.append( dynamic_cast<SMESH_Actor*>( owner->GetActor() ) );
97   }
98   else if ( ow ) { // SVTK selection disabled
99     QString entry = ow->entry();
100     myActors.append( SMESH::FindActorByEntry( entry.toStdString().c_str() ));
101   }
102   else {
103     myActors.append( 0 );
104   }
105   return true;
106 }
107
108 //=======================================================================
109 //function : parameter
110 //purpose  : 
111 //=======================================================================
112 QVariant SMESHGUI_Selection::parameter( const int ind, const QString& p ) const
113 {
114   QVariant val;
115   if      ( p=="client" )               val = QVariant( LightApp_Selection::parameter( p ) );
116   else if ( p=="type" )                 val = QVariant( myTypes[ind] );
117   else if ( p=="hasActor" )             val = QVariant( getActor( ind ) != 0 );
118   else if ( p=="elemTypes" )            val = QVariant( elemTypes( ind, false ) );
119   else if ( p=="objElemTypes" )         val = QVariant( elemTypes( ind, true ) );
120   else if ( p=="isAutoColor" )          val = QVariant( isAutoColor( ind ) );
121   else if ( p=="numberOfNodes" )        val = QVariant( numberOfNodes( ind ) );
122   else if ( p=="dim" )                  val = QVariant( dim( ind ) );
123   else if ( p=="labeledTypes" )         val = QVariant( labeledTypes( ind ) );
124   else if ( p=="shrinkMode" )           val = QVariant( shrinkMode( ind ) );
125   else if ( p=="entityMode" )           val = QVariant( entityMode( ind ) );
126   else if ( p=="isNumFunctor" )         val = QVariant( isNumFunctor( ind ) );
127   else if ( p=="displayMode" )          val = QVariant( displayMode( ind ) );
128   else if ( p=="hasAlgo" )              val = QVariant( hasAlgo( ind ) );
129   else if ( p=="hasErrors" )            val = QVariant( hasErrors( ind ) );
130   else if ( p=="isComputable" )         val = QVariant( isComputable( ind ) );
131   else if ( p=="isPreComputable" )      val = QVariant( isPreComputable( ind ) );
132   else if ( p=="hasGeomReference" )     val = QVariant( hasGeomReference( ind ) );
133   else if ( p=="isEditableHyp" )        val = QVariant( isEditableHyp( ind ) );
134   else if ( p=="isImported" )           val = QVariant( isImported( ind ) );
135   else if ( p=="facesOrientationMode" ) val = QVariant( facesOrientationMode( ind ) );
136   else if ( p=="groupType" )            val = QVariant( groupType( ind ) );
137   else if ( p=="isQuadratic" )          val = QVariant( isQuadratic( ind ) );
138   else if ( p=="quadratic2DMode")       val = QVariant( quadratic2DMode( ind ) );
139   else if ( p=="isDistributionVisible") val = QVariant( isDistributionVisible( ind ) );
140   else if ( p=="isScalarBarVisible")    val = QVariant( isScalarBarVisible( ind ) );
141   else if ( p=="hasChildren")           val = QVariant( hasChildren( ind ) );
142   else if ( p=="nbChildren")            val = QVariant( nbChildren( ind ) );
143   else if ( p=="isContainer")           val = QVariant( isContainer( ind ) );
144   else if ( p=="guiState")              val = QVariant( guiState() );
145   else if ( p=="canBreakLink")          val = QVariant( canBreakLink(ind) );
146
147   if ( val.isValid() )
148     return val;
149   else
150     return LightApp_Selection::parameter( ind, p );
151 }
152
153 //=======================================================================
154 //function : parameter
155 //purpose  :
156 //=======================================================================
157 QVariant SMESHGUI_Selection::parameter( const QString& p ) const
158 {
159   QVariant val;
160   if ( p=="controlMode" ) val = QVariant( controlMode() );
161
162   if ( val.isValid() )
163     return val;
164   else
165     return LightApp_Selection::parameter( p );
166 }
167
168 //=======================================================================
169 //function : getVtkOwner
170 //purpose  :
171 //=======================================================================
172
173 SMESH_Actor* SMESHGUI_Selection::getActor( int ind ) const
174 {
175   if( ind >= 0 && ind < count() )
176     return myActors.isEmpty() ? 0 : myActors.at( ind );
177   else
178     return 0;
179 }
180
181 //=======================================================================
182 //function : elemTypes
183 //purpose  : may return {'Elem0d' 'Edge' 'Face' 'Volume' 'BallElem'} at most
184 //=======================================================================
185
186 QList<QVariant> SMESHGUI_Selection::elemTypes( int ind, bool fromObj ) const
187 {
188   QList<QVariant> types;
189   SMESH_Actor* actor = getActor( ind );
190   if ( actor ) {
191     TVisualObjPtr object = actor->GetObject();
192     if ( object ) {
193       if ( object->GetNbEntities( SMDSAbs_Edge ))      types.append( "Edge" );
194       if ( object->GetNbEntities( SMDSAbs_Face ))      types.append( "Face" );
195       if ( object->GetNbEntities( SMDSAbs_Volume ))    types.append( "Volume" );
196       if ( object->GetNbEntities( SMDSAbs_0DElement )) types.append( "Elem0d" );
197       if ( object->GetNbEntities( SMDSAbs_Ball ))      types.append( "BallElem" );
198     }
199   }
200   else if ( fromObj )
201   {
202     if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
203     {
204       _PTR(SObject) sobj = SMESH::getStudy()->FindObjectID( entry( ind ).toUtf8().data() );
205       CORBA::Object_var obj = SMESH::SObjectToObject( sobj );
206       SMESH::SMESH_IDSource_var idSrc = SMESH::SMESH_IDSource::_narrow( obj );
207       if ( !CORBA::is_nil( idSrc ) )
208       {
209         SMESH::array_of_ElementType_var typeVar = idSrc->GetTypes();
210         for ( CORBA::ULong i = 0; i < typeVar->length(); ++i )
211           switch ( typeVar[i] ) {
212           case SMESH::EDGE:   types.append( "Edge" );     break;
213           case SMESH::FACE:   types.append( "Face" );     break;
214           case SMESH::VOLUME: types.append( "Volume" );   break;
215           case SMESH::ELEM0D: types.append( "Elem0d" );   break;
216           case SMESH::BALL:   types.append( "BallElem" ); break;
217           case SMESH::ALL:
218           case SMESH::NODE:
219           case SMESH::NB_ELEMENT_TYPES: break;
220           }
221       }
222     }
223   }
224   return types;
225 }
226
227 //=======================================================================
228 //function : labeledTypes
229 //purpose  : may return {'Point' 'Cell'} at most
230 //=======================================================================
231
232 QList<QVariant> SMESHGUI_Selection::labeledTypes( int ind ) const
233 {
234   QList<QVariant> types;
235   SMESH_Actor* actor = getActor( ind );
236   if ( actor ) {
237     if ( actor->GetPointsLabeled()) types.append( "Point" );
238     if ( actor->GetCellsLabeled())  types.append( "Cell" );
239   }
240   return types;
241 }
242
243 //=======================================================================
244 //function : displayMode
245 //purpose  : return SMESH_Actor::EReperesent
246 //=======================================================================
247
248 QString SMESHGUI_Selection::displayMode( int ind ) const
249 {
250   SMESH_Actor* actor = getActor( ind );
251   if ( actor ) {
252     switch( actor->GetRepresentation() ) {
253     case SMESH_Actor::eEdge:    return "eEdge";
254     case SMESH_Actor::eSurface: return "eSurface";
255     case SMESH_Actor::ePoint:   return "ePoint";
256     default: break;
257     }
258   }
259   return "Unknown";
260 }
261
262 //=======================================================================
263 //function : isQuadratic
264 //purpose  : return true if the mesh has quadratic/bi-quadratic type
265 //=======================================================================
266
267 bool SMESHGUI_Selection::isQuadratic( int ind ) const
268 {
269   _PTR(SObject) so = SMESH::getStudy()->FindObjectID( entry( ind ).toUtf8().data() );
270   if ( !so )
271     return false;
272   SMESH::SMESH_IDSource_var idSource =  SMESH::SObjectToInterface<SMESH::SMESH_IDSource>( so );
273   if ( idSource->_is_nil() )
274     return false;
275   SMESHGUI_ConvToQuadOp::MeshDestinationType meshTgtType = SMESHGUI_ConvToQuadOp::DestinationMesh( idSource );
276   if ( meshTgtType & SMESHGUI_ConvToQuadOp::Linear )
277     return true;
278   return false;
279 }
280
281 //=======================================================================
282 //function : quadratic2DMode
283 //purpose  : return SMESH_Actor::EQuadratic2DRepresentation
284 //=======================================================================
285 QString SMESHGUI_Selection::quadratic2DMode( int ind ) const
286 {
287   SMESH_Actor* actor = getActor( ind );
288   if ( actor ) {
289     switch( actor->GetQuadratic2DRepresentation() ) {
290     case SMESH_Actor::eLines: return "eLines";
291     case SMESH_Actor::eArcs:  return "eArcs";
292     default: break;
293     }
294   }
295   return "Unknown";
296 }
297
298 //=======================================================================
299 //function : isDistributionVisible
300 //purpose  : Visible/Invisible distribution of the ScalarBar Actor
301 //=======================================================================
302
303 bool SMESHGUI_Selection::isDistributionVisible(int ind) const {
304   SMESH_Actor* actor = getActor( ind );
305   return (actor && actor->GetScalarBarActor() && actor->GetScalarBarActor()->GetDistributionVisibility());
306
307
308 //=======================================================================
309 //function : isScalarBarVisible
310 //purpose  : Visible/Invisible Scalar Bar
311 //=======================================================================
312
313 bool SMESHGUI_Selection::isScalarBarVisible(int ind) const {
314   SMESH_Actor* actor = getActor( ind );
315   return (actor && actor->GetScalarBarActor() && actor->GetScalarBarActor()->GetVisibility());
316 }
317
318 //=======================================================================
319 //function : shrinkMode
320 //purpose  : return either 'IsSrunk', 'IsNotShrunk' or 'IsNotShrinkable'
321 //=======================================================================
322
323 QString SMESHGUI_Selection::shrinkMode( int ind ) const
324 {
325   SMESH_Actor* actor = getActor( ind );
326   if ( actor && actor->IsShrunkable() ) {
327     return actor->IsShrunk() ? "IsShrunk" : "IsNotShrunk";
328   }
329   return "IsNotShrinkable";
330 }
331
332 //=======================================================================
333 //function : entityMode
334 //purpose  : may return {'Elem0d' 'Edge' 'Face' 'Volume' 'BallElem' } at most
335 //=======================================================================
336
337 QList<QVariant> SMESHGUI_Selection::entityMode( int ind ) const
338 {
339   QList<QVariant> types;
340   SMESH_Actor* actor = getActor( ind );
341   if ( actor ) {
342     unsigned int aMode = actor->GetEntityMode();
343     if ( aMode & SMESH_Actor::eVolumes    ) types.append( "Volume" );
344     if ( aMode & SMESH_Actor::eFaces      ) types.append( "Face"   );
345     if ( aMode & SMESH_Actor::eEdges      ) types.append( "Edge"   );
346     if ( aMode & SMESH_Actor::e0DElements ) types.append( "Elem0d" );
347     if ( aMode & SMESH_Actor::eBallElem )   types.append( "BallElem" );
348   }
349   return types;
350 }
351
352 //=======================================================================
353 //function : controlMode
354 //purpose  : return SMESH_Actor::eControl
355 //=======================================================================
356
357 QString SMESHGUI_Selection::controlMode( int ind ) const
358 {
359   SMESH_Actor* actor = getActor( ind );
360   QString mode = "eNone";
361   if ( actor ) {
362     switch( actor->GetControlMode() ) {
363     case SMESH_Actor::eLength:                mode = "eLength";                break;
364     case SMESH_Actor::eLength2D:              mode = "eLength2D";              break;
365     case SMESH_Actor::eDeflection2D:          mode = "eDeflection2D";          break;
366     case SMESH_Actor::eFreeEdges:             mode = "eFreeEdges";             break;
367     case SMESH_Actor::eFreeNodes:             mode = "eFreeNodes";             break;
368     case SMESH_Actor::eFreeBorders:           mode = "eFreeBorders";           break;
369     case SMESH_Actor::eFreeFaces:             mode = "eFreeFaces";             break;
370     case SMESH_Actor::eMultiConnection:       mode = "eMultiConnection";       break;
371     case SMESH_Actor::eMultiConnection2D:     mode = "eMultiConnection2D";     break;
372     case SMESH_Actor::eArea:                  mode = "eArea";                  break;
373     case SMESH_Actor::eVolume3D:              mode = "eVolume3D";              break;
374     case SMESH_Actor::eMaxElementLength2D:    mode = "eMaxElementLength2D";    break;
375     case SMESH_Actor::eMaxElementLength3D:    mode = "eMaxElementLength3D";    break;
376     case SMESH_Actor::eTaper:                 mode = "eTaper";                 break;
377     case SMESH_Actor::eAspectRatio:           mode = "eAspectRatio";           break;
378     case SMESH_Actor::eAspectRatio3D:         mode = "eAspectRatio3D";         break;
379     case SMESH_Actor::eMinimumAngle:          mode = "eMinimumAngle";          break;
380     case SMESH_Actor::eWarping:               mode = "eWarping";               break;
381     case SMESH_Actor::eSkew:                  mode = "eSkew";                  break;
382     case SMESH_Actor::eBareBorderFace:        mode = "eBareBorderFace";        break;
383     case SMESH_Actor::eBareBorderVolume:      mode = "eBareBorderVolume";      break;
384     case SMESH_Actor::eOverConstrainedFace:   mode = "eOverConstrainedFace";   break;
385     case SMESH_Actor::eOverConstrainedVolume: mode = "eOverConstrainedVolume"; break;
386     case SMESH_Actor::eCoincidentNodes:       mode = "eCoincidentNodes";       break;
387     case SMESH_Actor::eCoincidentElems1D:     mode = "eCoincidentElems1D";     break;
388     case SMESH_Actor::eCoincidentElems2D:     mode = "eCoincidentElems2D";     break;
389     case SMESH_Actor::eCoincidentElems3D:     mode = "eCoincidentElems3D";     break;
390     case SMESH_Actor::eCustomControl:         mode = "eCustomControl";         break;
391     default:break;
392     }
393   }
394   return mode;
395 }
396
397 //=======================================================================
398 //function : controlMode
399 //purpose  : gets global control mode; return SMESH_Actor::eControl
400 //=======================================================================
401 QString SMESHGUI_Selection::controlMode() const
402 {
403   if( myControls.count() > 0 ) {
404     QString mode = myControls[0];
405     for( int ind = 1; ind < myControls.count(); ind++ ) {
406       if( mode != myControls[ind] )
407         return "eMixed"; // different controls used for different actors
408     }
409     return mode;
410   }
411   return "eNone";
412 }
413
414 //=======================================================================
415 //function : isNumFunctor
416 //purpose  : return true if a given actor is shown using a numeric functor
417 //=======================================================================
418
419 bool SMESHGUI_Selection::isNumFunctor( int ind ) const
420 {
421   bool result = false;
422   SMESH_Actor* actor = getActor( ind );
423   if ( actor ) {
424     switch( actor->GetControlMode() ) {
425     case SMESH_Actor::eLength:
426     case SMESH_Actor::eLength2D:
427     case SMESH_Actor::eDeflection2D:
428     case SMESH_Actor::eMultiConnection:
429     case SMESH_Actor::eMultiConnection2D:
430     case SMESH_Actor::eArea:
431     case SMESH_Actor::eVolume3D:
432     case SMESH_Actor::eMaxElementLength2D:
433     case SMESH_Actor::eMaxElementLength3D:
434     case SMESH_Actor::eTaper:
435     case SMESH_Actor::eAspectRatio:
436     case SMESH_Actor::eAspectRatio3D:
437     case SMESH_Actor::eMinimumAngle:
438     case SMESH_Actor::eWarping:
439     case SMESH_Actor::eSkew:
440       result = true;
441       break;
442     default:
443       break;
444     }
445   }
446   return result;
447 }
448
449 //=======================================================================
450 //function : facesOrientationMode
451 //purpose  :
452 //=======================================================================
453
454 QString SMESHGUI_Selection::facesOrientationMode( int ind ) const
455 {
456   SMESH_Actor* actor = getActor( ind );
457   if ( actor ) {
458     return actor->GetFacesOriented() ? "IsOriented" : "IsNotOriented";
459   }
460   return "Unknown";
461 }
462
463 //=======================================================================
464 //function : isAutoColor
465 //purpose  : 
466 //=======================================================================
467
468 bool SMESHGUI_Selection::isAutoColor( int ind ) const
469 {
470   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
471   {
472     _PTR(SObject) sobj = SMESH::getStudy()->FindObjectID( entry( ind ).toUtf8().data() );
473     CORBA::Object_var obj = SMESH::SObjectToObject( sobj );
474
475     if ( !CORBA::is_nil( obj ) ) {
476       SMESH::SMESH_Mesh_var mesh = SMESH::SMESH_Mesh::_narrow( obj );
477       if ( !CORBA::is_nil( mesh ) )
478         return mesh->GetAutoColor();
479     }
480   }
481   return false;
482 }
483
484 //=======================================================================
485 //function : numberOfNodes
486 //purpose  : this method is actually used to check if an object is empty or not
487 //=======================================================================
488
489 int SMESHGUI_Selection::numberOfNodes( int ind ) const
490 {
491   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
492   {
493     _PTR(SObject) sobj = SMESH::getStudy()->FindObjectID( entry( ind ).toUtf8().data() );
494     CORBA::Object_var obj = SMESH::SObjectToObject( sobj );
495
496     if ( !CORBA::is_nil( obj ) ) {
497       SMESH::SMESH_Mesh_var mesh = SMESH::SMESH_Mesh::_narrow( obj );
498       if ( !CORBA::is_nil( mesh ) )
499         return mesh->NbNodes();
500       SMESH::SMESH_subMesh_var aSubMeshObj = SMESH::SMESH_subMesh::_narrow( obj );
501       if ( !CORBA::is_nil( aSubMeshObj ) )
502         return aSubMeshObj->GetNumberOfNodes(true);
503       SMESH::SMESH_GroupBase_var aGroupObj = SMESH::SMESH_GroupBase::_narrow( obj );
504       if ( !CORBA::is_nil( aGroupObj ) )
505         return aGroupObj->IsEmpty() ? 0 : 1; // aGroupObj->Size();
506     }
507   }
508   return 0;
509 }
510
511 //================================================================================
512 /*!
513  * \brief return dimension of elements of the selected object
514  *
515  *  \retval int - 0 for 0D elements, -1 for an empty object (the rest as usual)
516  */
517 //================================================================================
518
519 int SMESHGUI_Selection::dim( int ind ) const
520 {
521   int dim = -1;
522   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
523   {
524     _PTR(SObject) sobj = SMESH::getStudy()->FindObjectID( entry( ind ).toUtf8().data() );
525     CORBA::Object_var obj = SMESH::SObjectToObject( sobj );
526
527     if ( !CORBA::is_nil( obj ) ) {
528       SMESH::SMESH_IDSource_var idSrc = SMESH::SMESH_IDSource::_narrow( obj );
529       if ( !CORBA::is_nil( idSrc ) )
530       {
531         SMESH::array_of_ElementType_var types = idSrc->GetTypes();
532         for ( size_t i = 0; i < types->length(); ++ i) {
533           switch ( types[i] ) {
534           case SMESH::EDGE  : dim = std::max( dim, 1 ); break;
535           case SMESH::FACE  : dim = std::max( dim, 2 ); break;
536           case SMESH::VOLUME: dim = std::max( dim, 3 ); break;
537           case SMESH::ELEM0D: dim = std::max( dim, 0 ); break;
538           case SMESH::BALL  : dim = std::max( dim, 0 ); break;
539           default: break;
540           }
541         }
542       }
543     }
544   }
545   return dim;
546 }
547
548 //=======================================================================
549 //function : hasAlgo
550 //purpose  : return true for a ready-to-compute [sub-]mesh
551 //=======================================================================
552
553 bool SMESHGUI_Selection::hasAlgo( int ind ) const
554 {
555   if ( ind >= 0 && ind < myTypes.count() && ( myTypes[ind] == "Mesh" ||
556                                               myTypes[ind].startsWith("Mesh " )))
557   {
558     QMap<int,int> modeMap;
559     _PTR(SObject) meshSO = SMESH::getStudy()->FindObjectID( entry( ind ).toStdString() );
560
561     SMESHGUI_PrecomputeOp::getAssignedAlgos( meshSO, modeMap );
562     return modeMap.size() > 0;
563   }
564   return false;
565 }
566
567
568 //=======================================================================
569 //function : hasAlgo
570 //purpose  : return true if a mesh was computed with errors
571 //=======================================================================
572
573 bool SMESHGUI_Selection::hasErrors( int ind ) const
574 {
575   if ( ind >= 0 && ind < myTypes.count() && ( myTypes[ind] == "Mesh"))
576   {
577     _PTR(SObject)       meshSO = SMESH::getStudy()->FindObjectID( entry( ind ).toStdString() );
578     CORBA::Object_var      obj = SMESH::SObjectToObject( meshSO );
579     SMESH::SMESH_Mesh_var mesh = SMESH::SMESH_Mesh::_narrow( obj );
580     if ( !CORBA::is_nil( mesh ) )
581     {
582       SMESH::SMESH_Gen_var   gen = SMESHGUI::GetSMESHGUI()->GetSMESHGen();
583       GEOM::GEOM_Object_var geom = mesh->GetShapeToMesh();
584       SMESH::compute_error_array_var compErrors = gen->GetComputeErrors( mesh, geom );
585       return compErrors->length();
586     }
587   }
588   return false;
589 }
590
591 //=======================================================================
592 //function : isComputable
593 //purpose  : Return true if a [sub-]mesh does not have "computed" icon
594 //=======================================================================
595
596 bool SMESHGUI_Selection::isComputable( int ind ) const
597 {
598   if ( ind >= 0 && ind < myTypes.count() && ( myTypes[ind] == "Mesh" ||
599                                               myTypes[ind].startsWith("Mesh " )))
600   {
601     _PTR(GenericAttribute) attr;
602     if ( _PTR(SObject) meshSO = SMESH::getStudy()->FindObjectID( entry( ind ).toStdString() ))
603       if ( meshSO->FindAttribute( attr, "AttributePixMap" ))
604       {
605         _PTR(AttributePixMap) pixmap = attr;
606         return ( pixmap->GetPixMap() != "ICON_SMESH_TREE_MESH" );
607       }
608   }
609   return false;
610 }
611
612 //=======================================================================
613 //function : isPreComputable
614 //purpose  : returns true for a mesh with algorithms
615 //=======================================================================
616
617 bool SMESHGUI_Selection::isPreComputable( int ind ) const
618 {
619   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] == "Mesh" )
620   {
621     int maxDim = dim( ind );
622     if ( maxDim < 2 ) // we can preview 1D or 2D
623     {
624       QMap<int,int> modeMap;
625       _PTR(SObject) pMesh = SMESH::getStudy()->FindObjectID( entry( ind ).toUtf8().data() );
626       SMESHGUI_PrecomputeOp::getAssignedAlgos( pMesh, modeMap );
627       if ( modeMap.size() > 1 )
628         return (( modeMap.contains( SMESH::DIM_3D )) ||
629                 ( modeMap.contains( SMESH::DIM_2D ) && maxDim < 1 ));
630     }
631   }
632   return false;
633 }
634
635 //=======================================================================
636 //function : hasGeomReference
637 //purpose  : returns true for a mesh or sub-mesh on geometry
638 //=======================================================================
639
640 bool SMESHGUI_Selection::hasGeomReference( int ind ) const
641 {
642   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
643   {
644     _PTR(SObject) so = SMESH::getStudy()->FindObjectID( entry( ind ).toUtf8().data() );
645     GEOM::GEOM_Object_var shape = SMESH::GetShapeOnMeshOrSubMesh( so );
646     return !shape->_is_nil();
647   }
648   return false;
649 }
650
651 //=======================================================================
652 //function : canBreakLink
653 //purpose  : returns true if selected object is a Shaper object and it can break link
654 //=======================================================================
655
656 bool SMESHGUI_Selection::canBreakLink( int ind ) const
657 {
658   if ( ind >= 0 && isReference(ind) ) {
659     QString aEntry = objectInfo(ind, OI_RefEntry).toString();
660     if (!aEntry.isEmpty()) {
661       _PTR(SObject) aSObject = SMESH::getStudy()->FindObjectID( aEntry.toStdString());
662       if (aSObject) {
663         _PTR(SObject) aFatherObj = aSObject->GetFather();
664         if (aFatherObj) {
665           _PTR(SComponent) aComponent = aFatherObj->GetFatherComponent();
666           if (aComponent && aComponent->ComponentDataType() == "SMESH") {
667             QString aObjEntry = entry(ind);
668             _PTR(SObject) aGeomSObject = SMESH::getStudy()->FindObjectID(aObjEntry.toStdString());
669             GEOM::GEOM_Object_var aObject = SMESH::SObjectToInterface<GEOM::GEOM_Object>(aGeomSObject);
670             if (!aObject->_is_nil())
671               return aObject->IsParametrical();
672           }
673         }
674       }
675     }
676   }
677   return false;
678 }
679
680 //=======================================================================
681 //function : isEditableHyp
682 //purpose  : 
683 //=======================================================================
684
685 bool SMESHGUI_Selection::isEditableHyp( int ind ) const
686 {
687   bool isEditable = true;
688   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] == "Hypothesis" )
689   {
690     _PTR(SObject) so = SMESH::getStudy()->FindObjectID( entry( ind ).toUtf8().data() );
691     SMESH::SMESH_Hypothesis_var hyp = SMESH::SObjectToInterface<SMESH::SMESH_Hypothesis>( so );
692     if ( !hyp->_is_nil() )
693     {
694       isEditable = hyp->HasParameters();
695     }
696   }
697   return isEditable;
698 }
699
700 //=======================================================================
701 //function : isVisible
702 //purpose  : 
703 //=======================================================================
704
705 bool SMESHGUI_Selection::isVisible( int ind ) const
706 {
707   if ( ind >= 0 && ind < myTypes.count() && myTypes[ind] != "Unknown" )
708   {
709     SMESH_Actor* actor = SMESH::FindActorByEntry( entry( ind ).toUtf8().data() );
710     if ( actor && actor->hasIO() ) {
711       if ( SVTK_ViewWindow* aViewWindow = SMESH::GetCurrentVtkView() )
712         return aViewWindow->isVisible( actor->getIO() );
713     }
714   }
715   return false;
716 }
717
718 //=======================================================================
719 //function : hasChildren
720 //purpose  : 
721 //=======================================================================
722
723 bool SMESHGUI_Selection::hasChildren( int ind ) const
724 {
725   if ( ind >= 0 )
726   {
727     _PTR(SObject) sobj = SMESH::getStudy()->FindObjectID( entry( ind ).toUtf8().data() );
728     if ( sobj ) 
729       return SMESH::getStudy()->GetUseCaseBuilder()->HasChildren( sobj );
730   }
731   return false;
732 }
733
734 //=======================================================================
735 //function : hasChildren
736 //purpose  : 
737 //=======================================================================
738
739 int SMESHGUI_Selection::nbChildren( int ind ) const
740 {
741   int nb = 0;
742   if ( ind >= 0 )
743   {
744     _PTR(Study) study = SMESH::getStudy();
745     _PTR(SObject) sobj = study->FindObjectID( entry( ind ).toUtf8().data() );
746     if ( sobj && study->GetUseCaseBuilder()->IsUseCaseNode( sobj ) ) {
747       _PTR(UseCaseIterator) it = study->GetUseCaseBuilder()->GetUseCaseIterator( sobj );
748       for ( it->Init( false ); it->More(); it->Next() ) nb++;
749     }
750   }
751   return nb;
752 }
753
754 //=======================================================================
755 //function : isContainer
756 //purpose  : 
757 //=======================================================================
758
759 bool SMESHGUI_Selection::isContainer( int ind ) const
760 {
761   return ind >= 0 && ind < myTypes.count() && myTypes[ind] == "Unknown";
762 }
763
764 //=======================================================================
765 //function : type
766 //purpose  : 
767 //=======================================================================
768
769 int SMESHGUI_Selection::type( const QString& entry )
770 {
771   int res = -1;
772   _PTR(SObject) obj = SalomeApp_Application::getStudy()->FindObjectID( entry.toUtf8().data() );
773   if ( obj ) {
774     _PTR(SObject) ref;
775     if ( obj->ReferencedObject( ref ) )
776       obj = ref;
777
778     _PTR(SObject) objFather = obj->GetFather();
779     _PTR(SComponent) objComponent = obj->GetFatherComponent();
780
781     if ( objComponent->ComponentDataType() == "SMESH" ) {
782       if ( objComponent->GetIOR() == obj->GetIOR() ) {
783         res = SMESH::COMPONENT;
784       }
785       else {
786         int aLevel = obj->Depth() - objComponent->Depth(),
787           aFTag = objFather->Tag(),
788           anOTag = obj->Tag();
789
790         switch ( aLevel )
791         {
792         case 1:
793           if ( anOTag >= SMESH::Tag_FirstMeshRoot )
794             res = SMESH::MESH;
795           break;
796         case 2:
797           switch ( aFTag )
798           {
799           case SMESH::Tag_HypothesisRoot: res = SMESH::HYPOTHESIS; break;
800           case SMESH::Tag_AlgorithmsRoot: res = SMESH::ALGORITHM;  break;
801           default: break;
802           }
803           break;
804         case 3:
805           switch ( aFTag )
806           {
807           case SMESH::Tag_SubMeshOnVertex:   res = SMESH::SUBMESH_VERTEX;   break;
808           case SMESH::Tag_SubMeshOnEdge:     res = SMESH::SUBMESH_EDGE;     break;
809           case SMESH::Tag_SubMeshOnFace:     res = SMESH::SUBMESH_FACE;     break;
810           case SMESH::Tag_SubMeshOnSolid:    res = SMESH::SUBMESH_SOLID;    break;
811           case SMESH::Tag_SubMeshOnCompound: res = SMESH::SUBMESH_COMPOUND; break;
812           default:
813             if ( aFTag >= SMESH::Tag_FirstGroup) res = SMESH::GROUP;
814             else                                 res = SMESH::SUBMESH;
815             break;
816           }
817           break;
818         }
819       }
820     }
821   }
822   return res;
823 }
824
825 //=======================================================================
826 //function : typeName
827 //purpose  : 
828 //=======================================================================
829
830 QString SMESHGUI_Selection::typeName( const int t )
831 {
832   QString res = "Unknown";
833   switch( t )
834   {
835   case SMESH::HYPOTHESIS:
836     res = "Hypothesis"; break;
837   case SMESH::ALGORITHM:
838     res = "Algorithm"; break;
839   case SMESH::MESH:
840     res = "Mesh"; break;
841   case SMESH::SUBMESH:
842     res = "SubMesh"; break;
843   case SMESH::MESHorSUBMESH:
844     res = "Mesh or submesh"; break;
845   case SMESH::SUBMESH_VERTEX:
846     res = "Mesh vertex"; break;
847   case SMESH::SUBMESH_EDGE:
848     res = "Mesh edge"; break;
849   case SMESH::SUBMESH_FACE:
850     res = "Mesh face"; break;
851   case SMESH::SUBMESH_SOLID:
852     res = "Mesh solid"; break;
853   case SMESH::SUBMESH_COMPOUND:
854     res = "Mesh compound"; break;
855   case SMESH::GROUP:
856     res = "Group"; break;
857   case SMESH::COMPONENT:
858     res = "Component"; break;
859   default:
860      break;
861   }
862   return res;
863 }
864
865 bool SMESHGUI_Selection::isImported( const int ind ) const
866 {
867   bool res = false;
868   _PTR(SObject) sobj = SMESH::getStudy()->FindObjectID( entry( ind ).toUtf8().constData() );
869   if ( sobj )
870   {
871     SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow( SMESH::SObjectToObject( sobj ) );
872     if( !aMesh->_is_nil() )
873     {
874       SMESH::MedFileInfo_var inf = aMesh->GetMEDFileInfo();
875       res = strlen( (char*)inf->fileName ) > 0;
876     }
877   }
878   return res;
879 }
880
881 //=======================================================================
882 //function : guiState
883 //purpose  : 
884 //=======================================================================
885
886 int SMESHGUI_Selection::guiState()
887 {
888   return SMESHGUI::GetSMESHGUI() ? SMESHGUI::GetSMESHGUI()->GetState() : -1;
889 }
890
891 //=======================================================================
892 //function : groupType
893 //purpose  : 
894 //=======================================================================
895
896 QString SMESHGUI_Selection::groupType( int ind ) const
897 {
898   _PTR(SObject) sobj = SMESH::getStudy()->FindObjectID( entry( ind ).toUtf8().constData() );
899   if ( sobj )
900   {
901     SMESH::SMESH_Group_var g = SMESH::SObjectToInterface<SMESH::SMESH_Group>( sobj );
902     if ( !CORBA::is_nil( g ) )
903       return "Group";
904     SMESH::SMESH_GroupOnGeom_var gog = SMESH::SObjectToInterface<SMESH::SMESH_GroupOnGeom>( sobj );
905     if( !CORBA::is_nil( gog ) )
906       return "GroupOnGeom";
907     SMESH::SMESH_GroupOnFilter_var gof = SMESH::SObjectToInterface<SMESH::SMESH_GroupOnFilter>( sobj );
908     if ( !CORBA::is_nil( gof ) )
909       return "GroupOnFilter";
910   }
911   return "";
912 }