Salome HOME
CoTech 86: Turn ON parallel tesselation of presentations by default
[modules/geom.git] / src / GEOMGUI / GEOM_Displayer.cxx
1 // Copyright (C) 2007-2016  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 // GEOM GEOMGUI : GUI for Geometry component
24 // File   : GEOM_Displayer.cxx
25 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
26
27 #include "GEOM_Displayer.h"
28 #include "GEOMGUI_DimensionProperty.h"
29 #include "GeometryGUI.h"
30
31 #include <GEOM_Constants.h>
32 #include <GEOM_TypeFilter.h>
33 #include <GEOM_EdgeFilter.h>
34 #include <GEOM_FaceFilter.h>
35 #include <GEOM_CompoundFilter.h>
36 #include <GEOM_PreviewFilter.h>
37 #include <GEOM_LogicalFilter.h>
38 #include <GEOM_OCCFilter.h>
39
40 #include <GEOM_Actor.h>
41 #include <GEOM_AISDimension.hxx>
42 #include <GEOM_TopWireframeShape.hxx>
43 #include <GEOM_AISVector.hxx>
44 #include <GEOM_AISTrihedron.hxx>
45 #include <GEOM_VTKTrihedron.hxx>
46 #include <GEOM_VTKPropertyMaterial.hxx>
47
48 #include <GEOMUtils.hxx>
49
50 #include <Material_Model.h>
51
52 #include <SUIT_Desktop.h>
53 #include <SUIT_ViewWindow.h>
54 #include <SUIT_Session.h>
55 #include <SUIT_ViewManager.h>
56 #include <SUIT_ResourceMgr.h>
57
58 #include <Basics_OCCTVersion.hxx>
59
60 #include <SalomeApp_Study.h>
61 #include <SalomeApp_Application.h>
62 #include <LightApp_SelectionMgr.h>
63 #include <LightApp_DataObject.h>
64 #include <SalomeApp_TypeFilter.h>
65 #include <SalomeApp_Tools.h>
66 #include "utilities.h"
67
68 #include <SALOME_ListIO.hxx>
69 #include <SALOME_Prs.h>
70 #include "utilities.h"
71
72 #include <SOCC_Prs.h>
73 #include <SOCC_ViewModel.h>
74
75 #include <SVTK_Prs.h>
76 #include <SVTK_ViewModel.h>
77
78 #include <OCCViewer_ViewWindow.h>
79 #include <OCCViewer_ViewPort3d.h>
80 #include <OCCViewer_Utilities.h>
81
82 // OCCT Includes
83 #include <AIS_Dimension.hxx>
84 #include <AIS_LengthDimension.hxx>
85 #include <AIS_DiameterDimension.hxx>
86 #include <AIS_AngleDimension.hxx>
87 #include <AIS_ListIteratorOfListOfInteractive.hxx>
88 #include <Aspect_PolygonOffsetMode.hxx>
89 #if OCC_VERSION_MAJOR < 7
90   #include <Aspect_ColorScale.hxx>
91 #endif
92 #include <Prs3d_IsoAspect.hxx>
93 #include <Prs3d_PointAspect.hxx>
94 #include <StdSelect_TypeOfEdge.hxx>
95 #include <StdSelect_TypeOfFace.hxx>
96 #include <StdSelect_DisplayMode.hxx>
97 #include <TopoDS_Face.hxx>
98 #include <BRep_Tool.hxx>
99 #include <Geom_Plane.hxx>
100 #include <Geom_Axis2Placement.hxx>
101 #include <Graphic3d_AspectFillArea3d.hxx>
102 #include <gp_Pln.hxx>
103 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
104 #include <TopoDS_Iterator.hxx>
105 #include <Graphic3d_AspectMarker3d.hxx>
106 #include <TopTools_MapOfShape.hxx>
107 #include <TopTools_ListOfShape.hxx>
108 #include <TopTools_ListIteratorOfListOfShape.hxx>
109 #include <TopoDS.hxx>
110 #include <NCollection_DataMap.hxx>
111 #include <NCollection_Map.hxx>
112
113 #include <Prs3d_ShadingAspect.hxx>
114
115 #include <BRepMesh_IncrementalMesh.hxx>
116
117 // VTK Includes
118 #include <vtkActorCollection.h>
119 #include <vtkProperty.h>
120
121 // CORBA Headers
122 #include CORBA_CLIENT_HEADER(SALOMEDS_Attributes)
123
124 #include <GEOMImpl_Types.hxx>
125
126 #include <TColStd_HArray1OfByte.hxx>
127
128 // If the next macro is defined, autocolor feature works for all sub-shapes;
129 // if it is undefined, autocolor feature works for groups only
130 #define GENERAL_AUTOCOLOR
131 // Below macro, when uncommented, switches on simplified (more performant) algorithm
132 // of auto-color picking up
133 #define SIMPLE_AUTOCOLOR
134
135 // Hard-coded value of shape deflection coefficient for VTK viewer
136 const double VTK_MIN_DEFLECTION = 0.001;
137 // If the next macro is defined, the deflection coefficient for VTK presentation
138 // is limited by VTK_MIN_DEFLECTION
139 //#define LIMIT_DEFLECTION_FOR_VTK
140
141 // Pixmap caching support
142 namespace
143 {
144   typedef NCollection_Map<Handle(GEOM_AISShape)>                    SetOfAISShapes;
145   typedef NCollection_DataMap<Handle(Image_PixMap), SetOfAISShapes> PixmapUsageMap;
146   typedef QMap<QString, Handle(Image_PixMap)>                       PixmapCacheMap;
147
148   static inline PixmapUsageMap& getPixmapUsageMap()
149   {
150     static PixmapUsageMap aMap;
151     return aMap;
152   }
153
154   static inline PixmapCacheMap& getPixmapCacheMap()
155   {
156     static PixmapCacheMap aMap;
157     return aMap;
158   }
159
160   //===========================================================================
161   // Function : getDefaultTexture
162   // Purpose  : Get default texture
163   //===========================================================================
164   static inline Handle(Image_PixMap) getDefaultTexture()
165   {
166     static Handle(Image_PixMap) aPixmap;
167     if ( aPixmap.IsNull() ) {
168       QPixmap px(":images/default_texture.png");
169       if ( !px.isNull() )
170         aPixmap = OCCViewer_Utilities::imageToPixmap( px.toImage() );
171     }
172     return aPixmap;
173   }
174
175   //===========================================================================
176   // Function : cacheTextureFor
177   // Purpose  : Load and cache image for the specified presentation.
178   //===========================================================================
179   static inline Handle(Image_PixMap) cacheTextureFor( const QString& thePath,
180                                                       const Handle(GEOM_AISShape)& theShape )
181   {
182     if ( thePath.isEmpty() )
183       return NULL;
184
185     PixmapUsageMap& aPixmapUsersMap = getPixmapUsageMap();
186     PixmapCacheMap& aPixmapCacheMap = getPixmapCacheMap();
187
188     Handle(Image_PixMap) aPixmap = aPixmapCacheMap.value( thePath, NULL );
189     if ( !aPixmap.IsNull() ) {
190       // point that the texture is used by the presentation
191       if ( !aPixmapUsersMap.IsBound( aPixmap ) )
192         aPixmapUsersMap.Bind( aPixmap, SetOfAISShapes() );
193
194       aPixmapUsersMap.ChangeFind( aPixmap ).Add( theShape );
195
196       return aPixmap;
197     }
198
199     // convert texture to compatible image format
200     QImage anImage = QImage( thePath ).convertToFormat( QImage::Format_ARGB32 );
201     if ( anImage.isNull() )
202       return NULL;
203
204     aPixmap = OCCViewer_Utilities::imageToPixmap( anImage );
205
206     aPixmapCacheMap.insert( thePath, aPixmap );
207
208     if ( !aPixmapUsersMap.IsBound( aPixmap ) )
209       aPixmapUsersMap.Bind( aPixmap, SetOfAISShapes() );
210
211     aPixmapUsersMap.ChangeFind( aPixmap ).Add( theShape );
212
213     return aPixmap;
214   }
215
216   //===========================================================================
217   // Function : releaseTextures
218   // Purpose  : Releases cached textures found for the specified presentation.
219   //===========================================================================
220   static inline void releaseTextures( const SALOME_OCCPrs* thePrs )
221   {
222     const SOCC_Prs* anOccPrs = dynamic_cast<const SOCC_Prs*>( thePrs );
223
224     AIS_ListOfInteractive aListOfIO;
225
226     anOccPrs->GetObjects( aListOfIO );
227
228     AIS_ListIteratorOfListOfInteractive aIterateIO( aListOfIO );
229
230     PixmapUsageMap& aPixmapUsersMap = getPixmapUsageMap();
231     PixmapCacheMap& aPixmapCacheMap = getPixmapCacheMap();
232
233     for ( ; aIterateIO.More(); aIterateIO.Next() )
234     {
235       Handle(GEOM_AISShape) aAISShape =
236         Handle(GEOM_AISShape)::DownCast( aIterateIO.Value() );
237
238       if ( aAISShape.IsNull() )
239         continue;
240
241 #ifdef USE_TEXTURED_SHAPE
242       const Handle(Image_PixMap)& aPixmap = aAISShape->TexturePixMap();
243       if ( aPixmap.IsNull() )
244         continue;
245
246       if ( !aPixmapUsersMap.IsBound( aPixmap ) )
247         continue;
248
249       SetOfAISShapes& aUsersShapes = aPixmapUsersMap.ChangeFind( aPixmap );
250
251       aUsersShapes.Remove( aAISShape );
252
253       if ( aUsersShapes.IsEmpty() ) {
254         aPixmapUsersMap.UnBind( aPixmap );
255         aPixmapCacheMap.remove( aPixmapCacheMap.key( aPixmap ) );
256       }
257 #endif
258     }
259   }
260
261   uint randomize( uint size )
262   {
263     static bool initialized = false;
264     if ( !initialized ) {
265       qsrand( QDateTime::currentDateTime().toTime_t() );
266       initialized = true;
267     }
268     uint v = qrand();
269     v = uint( (double)( v ) / RAND_MAX * size );
270     v = qMax( uint(0), qMin ( v, size-1 ) );
271     return v;
272   }
273 } // namespace
274
275 //================================================================
276 // Function : getActiveStudy
277 // Purpose  : Get active study, returns 0 if no open study frame
278 //================================================================
279 static inline SalomeApp_Study* getActiveStudy()
280 {
281   SUIT_Session* session = SUIT_Session::session();
282   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
283   if ( app )
284     return ( SalomeApp_Study* )app->activeStudy();
285   return 0;
286 }
287
288 static inline int getViewManagerId( SALOME_View* theViewFrame) {
289   SUIT_ViewModel* aModel = dynamic_cast<SUIT_ViewModel*>(theViewFrame);
290   SUIT_ViewManager* aViewMgr = 0;
291   if (aModel != 0)
292     aViewMgr = aModel->getViewManager();
293   return ((aViewMgr == 0) ? -1 :aViewMgr->getGlobalId());
294 }
295
296 //================================================================
297 // Function : getTopAbsMode
298 // Purpose  : Get TopAbs_ShapeEnum value corresponding to the
299 //            one from GEOMImpl_Types.h
300 //================================================================
301 static inline int getTopAbsMode( const int implType )
302 {
303   switch ( implType )
304   {
305     case GEOM_COMPOUND  : return TopAbs_COMPOUND;
306     case GEOM_SOLID     : return TopAbs_SOLID;
307     case GEOM_SHELL     : return TopAbs_SHELL;
308     case GEOM_FACE      : return TopAbs_FACE;
309     case GEOM_WIRE      : return TopAbs_WIRE;
310     case GEOM_EDGE      : return TopAbs_EDGE;
311     case GEOM_POINT     : return TopAbs_VERTEX;
312     default             : return -1;
313   }
314 }
315
316 int GEOM_Displayer::getMinMaxShapeType( const TopoDS_Shape& shape, bool ismin )
317 {
318   if ( shape.IsNull() )
319     return TopAbs_SHAPE;
320
321   int ret = shape.ShapeType();
322
323   if ( shape.ShapeType() == TopAbs_COMPOUND || shape.ShapeType() == TopAbs_COMPSOLID ) {
324     TopoDS_Iterator it(shape, Standard_True, Standard_False);
325     for (; it.More(); it.Next()) {
326       TopoDS_Shape sub_shape = it.Value();
327       if ( sub_shape.IsNull() ) continue;
328       int stype = getMinMaxShapeType( sub_shape, ismin );
329       if ( stype == TopAbs_SHAPE ) continue;
330       if ( ismin && stype > ret )
331         ret = stype;
332       else if ( !ismin && ( ret < TopAbs_SOLID || stype < ret ) )
333         ret = stype;
334     }
335   }
336
337   return ret;
338 }
339
340 bool GEOM_Displayer::isCompoundOfVertices( const TopoDS_Shape& theShape )
341 {
342   return theShape.ShapeType() == TopAbs_COMPOUND && getMinMaxShapeType( theShape, false ) == TopAbs_VERTEX;
343 }
344
345 //================================================================
346 // Function : getFilter
347 // Purpose  : Get filter corresponding to the type of object
348 //            from GEOMImpl_Types.h
349 //================================================================
350 SUIT_SelectionFilter* GEOM_Displayer::getFilter( const int theMode )
351 {
352   SUIT_SelectionFilter* aFilter;
353
354   int aTopAbsMode = getTopAbsMode( theMode );
355   if ( aTopAbsMode != -1 )
356     aFilter = new GEOM_TypeFilter( getStudy(), aTopAbsMode, true ); //@ aFilter = new GEOM_TypeFilter( ( TopAbs_ShapeEnum )aTopAbsMode );
357   else
358     switch ( theMode )
359       {
360       case GEOM_LINE      : aFilter = new GEOM_EdgeFilter( getStudy(), StdSelect_Line ); break;
361       case GEOM_CIRCLE    : aFilter = new GEOM_EdgeFilter( getStudy(), StdSelect_Circle ); break;
362
363       case GEOM_PLANE     : aFilter = new GEOM_FaceFilter( getStudy(), StdSelect_Plane ); break;
364       case GEOM_CYLINDER  : aFilter = new GEOM_FaceFilter( getStudy(), StdSelect_Cylinder ); break;
365       case GEOM_SPHERE    : aFilter = new GEOM_FaceFilter( getStudy(), StdSelect_Sphere ); break;
366       case GEOM_TORUS     : aFilter = new GEOM_FaceFilter( getStudy(), StdSelect_Torus ); break;
367       case GEOM_REVOLUTION: aFilter = new GEOM_FaceFilter( getStudy(), StdSelect_Revol ); break;
368       case GEOM_CONE      : aFilter = new GEOM_FaceFilter( getStudy(), StdSelect_Cone ); break;
369
370       case GEOM_PREVIEW   : aFilter = new GEOM_PreviewFilter( getStudy() ); break;
371
372       case GEOM_ALLSHAPES : aFilter = new GEOM_SelectionFilter(getStudy(), true ); break;
373       case GEOM_ALLGEOM   : aFilter = new SalomeApp_TypeFilter( getStudy(), "GEOM" ); break;
374
375       default             : aFilter = new GEOM_TypeFilter( getStudy(), theMode ); break;
376       }
377
378   return aFilter;
379 }
380
381 //================================================================
382 // Function : getComplexFilter
383 // Purpose  : Get compound filter corresponding to the type of
384 //            object from GEOMImpl_Types.h
385 //================================================================
386 SUIT_SelectionFilter* GEOM_Displayer::getComplexFilter( const QList<int>* aSubShapes)
387 {
388   GEOM_CompoundFilter* aFilter;
389
390   if(aSubShapes != NULL ) {
391     aFilter = new GEOM_CompoundFilter(getStudy());
392     QList<int> aTopAbsTypes;
393     QList<int>::const_iterator it;
394     for(it = aSubShapes->constBegin(); it != aSubShapes->constEnd(); ++it ) {
395       int topAbsMode = getTopAbsMode(*it);
396       if(topAbsMode != -1 )
397         aTopAbsTypes.append(topAbsMode);
398     }
399     aFilter->addSubTypes(aTopAbsTypes);
400   }
401
402   return aFilter;
403 }
404
405 //================================================================
406 // Function : getEntry
407 // Purpose  :
408 //================================================================
409 static std::string getEntry( GEOM::GEOM_BaseObject_ptr object )
410 {
411   SUIT_Session* session = SUIT_Session::session();
412   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
413   if ( app )
414   {
415     CORBA::String_var IOR = app->orb()->object_to_string( object );
416     if ( strcmp(IOR.in(), "") != 0 )
417     {
418       SalomeApp_Study* study = ( SalomeApp_Study* )app->activeStudy();
419       _PTR(SObject) SO ( study->studyDS()->FindObjectIOR( std::string(IOR) ) );
420       if ( SO )
421         return SO->GetID();
422     }
423   }
424   return "";
425 }
426
427 //================================================================
428 // Function : getName
429 // Purpose  :
430 //================================================================
431 static std::string getName( GEOM::GEOM_BaseObject_ptr object )
432 {
433   SUIT_Session* session = SUIT_Session::session();
434   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
435   if ( app )
436   {
437     CORBA::String_var IOR = app->orb()->object_to_string( object );
438     if ( strcmp(IOR.in(), "") != 0 )
439     {
440       SalomeApp_Study* study = ( SalomeApp_Study* )app->activeStudy();
441       _PTR(SObject) aSObj ( study->studyDS()->FindObjectIOR( std::string(IOR) ) );
442
443       _PTR(GenericAttribute) anAttr;
444
445       if ( aSObj && aSObj->FindAttribute( anAttr, "AttributeName") )
446       {
447         _PTR(AttributeName) aNameAttr( anAttr );
448         return aNameAttr->Value();
449       }
450     }
451   }
452
453   return "";
454 }
455
456 //=================================================================
457 /*!
458  *  GEOM_Displayer::GEOM_Displayer
459  *  Constructor
460  */
461 //=================================================================
462 GEOM_Displayer::GEOM_Displayer( SalomeApp_Study* st )
463 {
464   if( st )
465     myApp = dynamic_cast<SalomeApp_Application*>( st->application() );
466   else
467     myApp = 0;
468
469   /* Shading Color */
470   SUIT_Session* session = SUIT_Session::session();
471   SUIT_ResourceMgr* resMgr = session->resourceMgr();
472
473   QColor col = resMgr->colorValue( "Geometry", "shading_color", QColor( 255, 0, 0 ) );
474   myShadingColor = SalomeApp_Tools::color( col );
475
476   myDisplayMode = resMgr->integerValue("Geometry", "display_mode", 0);
477   myHasDisplayMode = false;
478
479   int aType = resMgr->integerValue("Geometry", "type_of_marker", (int)Aspect_TOM_PLUS);
480   
481   myTypeOfMarker = (Aspect_TypeOfMarker)(std::min((int)Aspect_TOM_RING3, std::max((int)Aspect_TOM_POINT, aType)));
482   myScaleOfMarker = (resMgr->integerValue("Geometry", "marker_scale", 1)-(int)GEOM::MS_10)*0.5 + 1.0;
483   myScaleOfMarker = std::min(7.0, std::max(1., myScaleOfMarker));
484
485   // Next properties provide a way to customize displaying of presentations;
486   // for instance, this is useful for preview
487   myColor = -1;
488   myTexture = "";
489   myNbIsos = -1;
490   myWidth = -1;
491   myTransparency = -1;
492   myType = -1;
493   myIsosColor = -1;
494   myIsosWidth = -1;
495
496   // This parameter is used for activisation/deactivisation (selection) of objects to be displayed
497   myToActivate = true;
498
499   // Activate parallel vizualisation by default.
500   // It can be switched OFF via the environment variable:
501   //     export PARALLEL_VISU=0
502   Standard_Boolean parallel_visu = Standard_True;
503   char* parallel_visu_env = getenv("PARALLEL_VISU");
504   if (parallel_visu_env && atoi(parallel_visu_env) == 0)
505     parallel_visu = Standard_False;
506   BRepMesh_IncrementalMesh::SetParallelDefault(parallel_visu);
507
508   myViewFrame = 0;
509
510 #if OCC_VERSION_MAJOR >= 7
511   myColorScale = new AIS_ColorScale;
512   myColorScale->SetZLayer (Graphic3d_ZLayerId_TopOSD);
513   myColorScale->SetTransformPersistence (Graphic3d_TMF_2d, gp_Pnt (-1,-1,0));
514 #endif
515
516   myFieldDataType = GEOM::FDT_Double;
517   myFieldDimension = 0;
518   myFieldStepRangeMin = 0;
519   myFieldStepRangeMax = 0;
520 }
521
522 //=================================================================
523 /*!
524  *  GEOM_Displayer::~GEOM_Displayer
525  *  Destructor
526  */
527 //=================================================================
528 GEOM_Displayer::~GEOM_Displayer()
529 {
530 }
531
532 //=================================================================
533 /*!
534  *  GEOM_Displayer::Display
535  *  Display interactive object in the current viewer
536  */
537 //=================================================================
538 void GEOM_Displayer::Display( const Handle(SALOME_InteractiveObject)& theIO,
539                              const bool updateViewer,
540                              SALOME_View* theViewFrame )
541 {
542   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
543   if ( vf )
544   {
545     SALOME_Prs* prs = buildPresentation( theIO->getEntry(), vf );
546
547     if ( prs )
548     {
549       vf->BeforeDisplay( this, prs );
550       vf->Display( this, prs );
551       vf->AfterDisplay( this, prs );
552
553       if ( updateViewer )
554         vf->Repaint();
555
556       delete prs;  // delete presentation because displayer is its owner
557     }
558   }
559 }
560
561 //=================================================================
562 /*!
563  *  GEOM_Displayer::Display
564  *  This overloaded Display() method can be useful for operations
565  *  not using dialog boxes.
566  */
567 //=================================================================
568 void GEOM_Displayer::Display( GEOM::GEOM_BaseObject_ptr theObj, const bool updateViewer )
569 {
570   if ( theObj->_is_nil() )
571     return;
572
573   std::string entry = getEntry( theObj );
574   if ( entry != "" ) {
575     Display(new SALOME_InteractiveObject(entry.c_str(), "GEOM", getName(theObj).c_str()),
576             updateViewer);
577   }
578 }
579
580 //=================================================================
581 /*!
582  *  GEOM_Displayer::Erase
583  *  Erase interactive object in the current viewer
584  */
585 //=================================================================
586 void GEOM_Displayer::Erase( const Handle(SALOME_InteractiveObject)& theIO,
587                             const bool forced,
588                             const bool updateViewer,
589                             SALOME_View* theViewFrame )
590 {
591   if ( theIO.IsNull() )
592     return;
593
594   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
595
596   if ( vf ) {
597     SALOME_Prs* prs = vf->CreatePrs( theIO->getEntry() );
598     if ( prs ) {
599       vf->BeforeErase( this, prs );
600       vf->Erase( this, prs, forced );
601       vf->AfterErase( this, prs );
602       if ( updateViewer )
603         vf->Repaint();
604       delete prs;  // delete presentation because displayer is its owner
605     }
606   }
607 }
608
609 //=================================================================
610 /*!
611  *  GEOM_Displayer::Erase
612  *  Erase geometry object in the current viewer
613  */
614 //=================================================================
615 void GEOM_Displayer::Erase( GEOM::GEOM_BaseObject_ptr theObj,
616                             const bool forced,
617                             const bool updateViewer,
618                             SALOME_View* theViewFrame)
619 {
620   std::string entry = getEntry( theObj );
621   if ( entry != "" )
622   {
623     Erase(new SALOME_InteractiveObject(entry.c_str(), "GEOM", getName(theObj).c_str()),
624           forced, updateViewer, theViewFrame);
625   }
626 }
627
628 //=================================================================
629 /*!
630  *  GEOM_Displayer::Redisplay
631  *  Redisplay (erase and then display again) interactive object
632  *  in the current viewer
633  */
634 //=================================================================
635 void GEOM_Displayer::Redisplay( const Handle(SALOME_InteractiveObject)& theIO,
636                                 const bool updateViewer,
637                                 const bool checkActiveViewer )
638 {
639   // Remove the object permanently (<forced> == true)
640   SUIT_Session* ses = SUIT_Session::session();
641   SUIT_Application* app = ses->activeApplication();
642   if ( app )
643   {
644     SUIT_Desktop* desk = app->desktop();
645     QList<SUIT_ViewWindow*> wnds = desk->windows();
646     SUIT_ViewWindow* wnd;
647     QListIterator<SUIT_ViewWindow*> it( wnds );
648     while ( it.hasNext() && (wnd = it.next()) )
649     {
650       SUIT_ViewManager* vman = wnd->getViewManager();
651       if ( vman )
652       {
653         SUIT_ViewModel* vmodel = vman->getViewModel();
654         if ( vmodel )
655         {
656           SALOME_View* view = dynamic_cast<SALOME_View*>(vmodel);
657           if ( view )
658           {
659             if ( view->isVisible( theIO ) || ( checkActiveViewer && view == GetActiveView() ) )
660             {
661               Redisplay( theIO, updateViewer, view );
662             }
663           }
664         }
665       }
666     }
667   }
668 }
669
670 //=================================================================
671 /*!
672  *  GEOM_Displayer::Redisplay
673  *  Redisplay (erase and then display again) interactive object
674  *  in the specified view
675  */
676 //=================================================================
677 void GEOM_Displayer::Redisplay( const Handle(SALOME_InteractiveObject)& theIO,
678                                 const bool theUpdateViewer,
679                                 SALOME_View* theViewFrame )
680 {
681   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
682   if ( !vf )
683   {
684     return;
685   }
686
687   Erase( theIO, true, false, theViewFrame );
688   Display( theIO, theUpdateViewer, theViewFrame );
689 }
690
691 //=================================================================
692 /*!
693  *  GEOM_Displayer::Display
694  *  Calls Display() method for each object in the given list
695  */
696 //=================================================================
697 void GEOM_Displayer::Display( const SALOME_ListIO& theIOList, const bool updateViewer )
698 {
699   SALOME_ListIteratorOfListIO Iter( theIOList );
700   for ( ; Iter.More(); Iter.Next() ) {
701     Display( Iter.Value(), false );
702   }
703   if ( updateViewer )
704     UpdateViewer();
705 }
706
707 void GEOM_Displayer::UpdateVisibility( SALOME_View* v, const SALOME_Prs* p, bool on )
708 {
709   SalomeApp_Study* aStudy = getStudy();
710   int vId = -1;
711   if ( v ) vId = getViewManagerId( v );
712
713   if ( p ) {
714     QString entry = p->GetEntry();
715     if ( !entry.isEmpty() ) {
716       if ( vId != -1 )
717         aStudy->setObjectProperty( vId, entry, GEOM::propertyName( GEOM::Visibility ), on );
718       setVisibilityState( entry, on ? Qtx::ShownState : Qtx::HiddenState );
719     }
720   }
721   else {
722     if ( vId != -1 ) {
723       aStudy->setObjectProperty( vId, GEOM::propertyName( GEOM::Visibility ), on );
724     }
725   }
726 }
727
728 Quantity_Color GEOM_Displayer::qColorFromResources( const QString& property, const QColor& defColor )
729 {
730   // VSR: this method can be improved in future:
731   // to improve performance, the default values from resource manager should be cached in the displayer
732   return SalomeApp_Tools::color( SUIT_Session::session()->resourceMgr()->colorValue( "Geometry", property, defColor ) );
733 }
734
735 QColor GEOM_Displayer::colorFromResources( const QString& property, const QColor& defColor )
736 {
737   // VSR: this method can be improved in future:
738   // to improve performance, the default values from resource manager should be cached in the displayer
739   return SUIT_Session::session()->resourceMgr()->colorValue( "Geometry", property, defColor );
740 }
741
742 void GEOM_Displayer::updateShapeProperties( const Handle(GEOM_AISShape)& AISShape, bool create )
743 {
744   // check that shape is not null
745   if ( AISShape.IsNull() ) return;
746   
747   // check that study is active
748   SalomeApp_Study* study = getStudy();
749   if ( !study ) return;
750
751   if ( myShape.ShapeType() != TopAbs_VERTEX && // fix pb with not displayed points
752        !TopoDS_Iterator(myShape).More() )
753     return; // NPAL15983 (Bug when displaying empty groups)
754
755   // set interactive object
756
757   Handle( SALOME_InteractiveObject ) anIO;
758
759   if ( !myIO.IsNull() ) {
760     AISShape->setIO( myIO );
761     AISShape->SetOwner( myIO );
762     anIO = myIO;
763   }
764   else if ( !myName.empty() ) {
765     // workaround to allow selection of temporary objects
766     static int tempId = 0;
767     anIO = new SALOME_InteractiveObject( QString( "TEMP_%1" ).arg( tempId++ ).toLatin1().data(), "GEOM", myName.c_str() );
768     AISShape->setIO( anIO );
769     AISShape->SetOwner( anIO );
770   }
771
772   // flag:  only vertex or compound of vertices is processed (specific handling)
773   bool onlyVertex = myShape.ShapeType() == TopAbs_VERTEX || isCompoundOfVertices( myShape );
774   // presentation study entry (empty for temporary objects like preview)
775   QString entry = !anIO.IsNull() ? QString( anIO->getEntry() ) : QString();
776   // flag: temporary object
777   bool isTemporary = entry.isEmpty() || entry.startsWith( "TEMP_" );
778   // currently active view window's ID (-1 if no active view)
779   int aMgrId = !anIO.IsNull() ? getViewManagerId( myViewFrame ) : -1;
780
781   // get presentation properties
782   PropMap propMap = getObjectProperties( study, entry, myViewFrame );
783
784   // Temporary staff: vertex must be infinite for correct visualization
785   AISShape->SetInfiniteState( myShape.Infinite() ); // || myShape.ShapeType() == TopAbs_VERTEX // VSR: 05/04/2010: Fix 20668 (Fit All for points & lines)
786
787   // set material
788   Material_Model material;
789   // if predefined color isn't set in displayer(via GEOM_Displayer::SetColor() function)
790   if( !HasColor() )
791     material.fromProperties( propMap.value( GEOM::propertyName( GEOM::Material ) ).toString() );
792   // - set front material properties
793   AISShape->SetCurrentFacingModel( Aspect_TOFM_FRONT_SIDE );
794   AISShape->SetMaterial( material.getMaterialOCCAspect( true ) );
795   // - set back material properties
796   AISShape->SetCurrentFacingModel( Aspect_TOFM_BACK_SIDE );
797   AISShape->SetMaterial( material.getMaterialOCCAspect( false ) );
798   // - switch to default (both sides) facing mode
799   AISShape->SetCurrentFacingModel( Aspect_TOFM_BOTH_SIDE );
800
801   // set colors
802
803   // - shading color
804   if ( HasColor()  ) {
805     // predefined color, manually set to displayer via GEOM_Displayer::SetColor() function;
806     // we set it to the shape not taking into account material properties
807     AISShape->SetShadingColor( (Quantity_NameOfColor)GetColor() );
808   }
809   else if ( !material.isPhysical() ) {
810     // shading color from properties is used only for non-physical materials
811     AISShape->SetShadingColor( SalomeApp_Tools::color( propMap.value( GEOM::propertyName( GEOM::ShadingColor ) ).value<QColor>() ) );
812   }
813
814   // - wireframe color
815   Handle(Prs3d_LineAspect) anAspect = AISShape->Attributes()->LineAspect();
816   anAspect->SetColor( HasColor() ? (Quantity_NameOfColor)GetColor() : 
817                       SalomeApp_Tools::color( propMap.value( GEOM::propertyName( GEOM::WireframeColor ) ).value<QColor>() ) );
818   AISShape->Attributes()->SetLineAspect( anAspect );
819   
820   // - unfree boundaries color
821   anAspect = AISShape->Attributes()->UnFreeBoundaryAspect();
822   anAspect->SetColor( HasColor() ? (Quantity_NameOfColor)GetColor() : 
823                       SalomeApp_Tools::color( propMap.value( GEOM::propertyName( GEOM::WireframeColor ) ).value<QColor>() ) );
824   AISShape->Attributes()->SetUnFreeBoundaryAspect( anAspect );
825   
826   // - free boundaries color
827   anAspect = AISShape->Attributes()->FreeBoundaryAspect();
828   anAspect->SetColor( HasColor() ? (Quantity_NameOfColor)GetColor() : 
829                       SalomeApp_Tools::color( propMap.value( GEOM::propertyName( GEOM::FreeBndColor ) ).value<QColor>() ) );
830   AISShape->Attributes()->SetFreeBoundaryAspect( anAspect );
831   
832   // - standalone edges color
833   anAspect = AISShape->Attributes()->WireAspect();
834   anAspect->SetColor( HasColor() ? (Quantity_NameOfColor)GetColor() : 
835                       SalomeApp_Tools::color( propMap.value( GEOM::propertyName( GEOM::LineColor ) ).value<QColor>() ) );
836   AISShape->Attributes()->SetWireAspect( anAspect );
837   
838   // - color for edges in shading+edges mode
839   AISShape->SetEdgesInShadingColor( SalomeApp_Tools::color( propMap.value( GEOM::propertyName( GEOM::OutlineColor ) ).value<QColor>() ) );
840
841   // - color of labels (textual fields and shape name)
842   AISShape->SetLabelColor( qColorFromResources( "label_color", QColor( 255, 255, 255 ) ) );
843
844   // set display mode
845   AISShape->SetDisplayMode( HasDisplayMode() ? 
846                             // predefined display mode, manually set to displayer via GEOM_Displayer::SetDisplayMode() function 
847                             GetDisplayMode() :
848                             // display mode from properties
849                             propMap.value( GEOM::propertyName( GEOM::DisplayMode ) ).toInt() );
850
851   // - face boundaries color and line width
852   anAspect = AISShape->Attributes()->FaceBoundaryAspect();
853   anAspect->SetColor( SalomeApp_Tools::color( propMap.value( GEOM::propertyName( GEOM::OutlineColor ) ).value<QColor>() ) );
854   anAspect->SetWidth( HasWidth() ? GetWidth() : propMap.value( GEOM::propertyName( GEOM::LineWidth ) ).toInt() );
855   AISShape->Attributes()->SetFaceBoundaryAspect( anAspect );
856
857   // set display vectors flag
858   AISShape->SetDisplayVectors( propMap.value( GEOM::propertyName( GEOM::EdgesDirection ) ).toBool() );
859
860   // set display vertices flag
861   bool isVerticesMode = propMap.value( GEOM::propertyName( GEOM::Vertices ) ).toBool();
862   AISShape->SetDisplayVertices( isVerticesMode );
863
864   // set display name flag
865   bool isNameMode = propMap.value( GEOM::propertyName( GEOM::ShowName ) ).toBool();
866   AISShape->SetDisplayName( isNameMode );
867
868   // set transparency
869   if( HasTransparency() ) {
870     AISShape->SetTransparency( GetTransparency() );
871   } else {
872     AISShape->SetTransparency( propMap.value( GEOM::propertyName( GEOM::Transparency ) ).toDouble() );
873   }
874
875   // set iso properties
876   int uIsos = propMap.value( GEOM::propertyName( GEOM::NbIsos ) ).toString().split( GEOM::subSectionSeparator() )[0].toInt();
877   int vIsos = propMap.value( GEOM::propertyName( GEOM::NbIsos ) ).toString().split( GEOM::subSectionSeparator() )[1].toInt();
878   Quantity_Color isosColor = SalomeApp_Tools::color( propMap.value( GEOM::propertyName( GEOM::IsosColor ) ).value<QColor>() );
879   int isosWidth = propMap.value( GEOM::propertyName( GEOM::IsosWidth ) ).toInt();
880   Handle(Prs3d_IsoAspect) uIsoAspect = AISShape->Attributes()->UIsoAspect();
881   Handle(Prs3d_IsoAspect) vIsoAspect = AISShape->Attributes()->VIsoAspect();
882
883   if ( HasIsosColor() ) {
884     uIsoAspect->SetColor( (Quantity_NameOfColor)GetIsosColor() );
885     vIsoAspect->SetColor( (Quantity_NameOfColor)GetIsosColor() );
886   }
887   else {
888     uIsoAspect->SetColor( isosColor );
889     vIsoAspect->SetColor( isosColor );
890   }
891
892   if ( HasIsosWidth() ) {
893     uIsoAspect->SetWidth( GetIsosWidth() );
894     vIsoAspect->SetWidth( GetIsosWidth() );
895   }
896   else {
897     uIsoAspect->SetWidth( isosWidth );
898     vIsoAspect->SetWidth( isosWidth );
899   }
900   
901   if ( HasNbIsos() ) {
902     uIsoAspect->SetNumber( GetNbIsos() );
903     vIsoAspect->SetNumber( GetNbIsos() );
904   }
905   else {
906     uIsoAspect->SetNumber( uIsos );
907     vIsoAspect->SetNumber( vIsos );
908   }
909
910   AISShape->Attributes()->SetUIsoAspect( uIsoAspect );
911   AISShape->Attributes()->SetVIsoAspect( vIsoAspect );
912
913   // set deflection coefficient
914   // ... to avoid to small values of the coefficient, its lower value is limited 
915   AISShape->SetOwnDeviationCoefficient( qMax( propMap.value( GEOM::propertyName( GEOM::Deflection ) ).toDouble(), GEOM::minDeflection() ) );
916
917   // set texture
918   QString aImagePath;
919   if ( HasTexture() ) {
920     // predefined display texture, manually set to displayer via GEOM_Displayer::SetTexture() function 
921     aImagePath = GetTexture().c_str();
922     if ( ! entry.isEmpty() ) {
923       // check that study is active
924       SalomeApp_Study* study = getActiveStudy();
925       if ( study ) {
926         // Store the texture in object properties for next displays
927         study->setObjectProperty( aMgrId, entry, GEOM::propertyName( GEOM::Texture ), QString( GetTexture().c_str() ) );
928         study->setObjectProperty( aMgrId, entry, GEOM::propertyName( GEOM::DisplayMode ), 3 );
929         
930         // Update propeties map
931         propMap = getObjectProperties( study, entry, myViewFrame );
932       }
933     }
934   }
935   else {
936     aImagePath = propMap.value( GEOM::propertyName( GEOM::Texture ) ).toString();
937   }
938
939 #ifdef USE_TEXTURED_SHAPE
940   Handle(Image_PixMap) aPixmap;
941   if ( !aImagePath.isEmpty() )
942     aPixmap = cacheTextureFor( aImagePath, AISShape );
943   else
944     aPixmap = getDefaultTexture();
945
946   // apply image to shape
947   if ( !aPixmap.IsNull() ) {
948     AISShape->SetTexturePixMap( aPixmap );
949     AISShape->SetTextureMapOn();
950     AISShape->DisableTextureModulate();
951   }
952   else {
953     AISShape->SetTextureMapOff();
954   }
955 #endif
956
957   // set line width
958   AISShape->SetWidth( HasWidth() ?
959                       // predefined line width, manually set to displayer via GEOM_Displayer::SetLineWidth() function 
960                       GetWidth() :
961                       // libe width from properties
962                       propMap.value( GEOM::propertyName( GEOM::LineWidth ) ).toInt() );
963
964   // set top-level flag
965   AISShape->setTopLevel( propMap.value( GEOM::propertyName( GEOM::TopLevel ) ).toBool() );
966
967   // set point marker (for vertex / compound of vertices only)
968   if ( onlyVertex || isVerticesMode ) {
969     QStringList aList = propMap.value( GEOM::propertyName( GEOM::PointMarker ) ).toString().split( GEOM::subSectionSeparator() );
970     if ( aList.size() == 2 ) {
971       // standard marker string contains "TypeOfMarker:ScaleOfMarker"
972       int aTypeOfMarker = aList[0].toInt();
973       double aScaleOfMarker = (aList[1].toInt() + 1) * 0.5;
974       Handle(Prs3d_PointAspect) anAspect = AISShape->Attributes()->PointAspect();
975       anAspect->SetScale( aScaleOfMarker );
976       anAspect->SetTypeOfMarker( (Aspect_TypeOfMarker)( aTypeOfMarker-1 ) );
977       anAspect->SetColor( HasColor() ? 
978                           // predefined color, manually set to displayer via GEOM_Displayer::SetColor() function
979                           (Quantity_NameOfColor)GetColor() : 
980                           // color from properties
981                           SalomeApp_Tools::color( propMap.value( GEOM::propertyName( GEOM::PointColor ) ).value<QColor>() ) );
982       AISShape->Attributes()->SetPointAspect( anAspect );
983     }
984     else if ( aList.size() == 1 ) {
985       // custom marker string contains "IdOfTexture"
986       int textureId = aList[0].toInt();
987       Standard_Integer aWidth, aHeight;
988       Handle(TColStd_HArray1OfByte) aTexture =
989         GeometryGUI::getTexture( study, textureId, aWidth, aHeight );
990       if ( !aTexture.IsNull() ) {
991         Handle(Prs3d_PointAspect) aTextureAspect =
992           new Prs3d_PointAspect( HasColor() ? 
993                                  // predefined color, manually set to displayer via GEOM_Displayer::SetColor() function
994                                  (Quantity_NameOfColor)GetColor() : 
995                                  // color from properties 
996                                  SalomeApp_Tools::color( propMap.value( GEOM::propertyName( GEOM::PointColor ) ).value<QColor>() ),
997                                  aWidth, aHeight,
998                                  aTexture );
999         AISShape->Attributes()->SetPointAspect( aTextureAspect );
1000       }
1001     }
1002   }
1003
1004   // set field step data
1005   AISShape->setFieldStepInfo( myFieldDataType,
1006                               myFieldDimension,
1007                               myFieldStepData,
1008                               myFieldStepName,
1009                               myFieldStepRangeMin,
1010                               myFieldStepRangeMax );
1011
1012   if ( create && !isTemporary && aMgrId != -1 ) {
1013     // set properties to the study
1014     study->setObjectProperties( aMgrId, entry, propMap );
1015   }
1016
1017   // AISShape->SetName(???); ??? necessary to set name ???
1018 }
1019
1020 void GEOM_Displayer::updateActorProperties( GEOM_Actor* actor, bool create )
1021 {
1022   // check that actor is not null
1023   if ( !actor ) return;
1024   
1025   // check that study is active
1026   SalomeApp_Study* study = getStudy();
1027   if ( !study ) return;
1028
1029   // set interactive object
1030
1031   Handle( SALOME_InteractiveObject ) anIO;
1032
1033   if ( !myIO.IsNull() ) {
1034     actor->setIO( myIO );
1035     anIO = myIO;
1036   }
1037   else if ( !myName.empty() ) {
1038     // workaround to allow selection of temporary objects
1039     static int tempId = 0;
1040     anIO = new SALOME_InteractiveObject( QString( "TEMP_VTK_%1" ).arg( tempId++ ).toLatin1().data(), "GEOM", myName.c_str() );
1041     actor->setIO( anIO );
1042   }
1043
1044   // presentation study entry (empty for temporary objects like preview)
1045   QString entry = !anIO.IsNull() ? QString( anIO->getEntry() ) : QString();
1046   // flag: temporary object
1047   bool isTemporary = entry.isEmpty() || entry.startsWith( "TEMP_" );
1048   // currently active view window's ID (-1 if no active view)
1049   int aMgrId = !anIO.IsNull() ? getViewManagerId( myViewFrame ) : -1;
1050
1051   // get presentation properties
1052   PropMap propMap = getObjectProperties( study, entry, myViewFrame );
1053   QColor c;
1054
1055   /////////////////////////////////////////////////////////////////////////
1056   // VSR: for VTK viewer currently deflection coefficient is hardcoded
1057   //      due to performance problem
1058   // actor->SetShape(myShape,aDefPropMap.value(GEOM::propertyName( GEOM::Deflection )).toDouble(),myType == GEOM_VECTOR);
1059   /////////////////////////////////////////////////////////////////////////
1060   if ( !actor->getTopo().IsSame( myShape ) )
1061 #ifdef LIMIT_DEFLECTION_FOR_VTK
1062     actor->SetShape( myShape, VTK_MIN_DEFLECTION, myType == GEOM_VECTOR );
1063 #else
1064     actor->SetShape( myShape, qMax( propMap.value( GEOM::propertyName( GEOM::Deflection ) ).toDouble(), GEOM::minDeflection() ), myType == GEOM_VECTOR );
1065 #endif
1066
1067   // set material
1068   Material_Model material;
1069   material.fromProperties( propMap.value( GEOM::propertyName( GEOM::Material ) ).toString() );
1070   std::vector<vtkProperty*> mprops;
1071   mprops.push_back( material.getMaterialVTKProperty( true ) );
1072   mprops.push_back( material.getMaterialVTKProperty( false) );
1073   actor->SetMaterial( mprops );
1074
1075   // set iso-lines properties
1076
1077   // - set number of iso-lines
1078   int nbIsos[2]= { 1, 1 };
1079   if ( HasNbIsos() ) {
1080     nbIsos[0] = GetNbIsos();
1081     nbIsos[1] = GetNbIsos();
1082   }
1083   else {
1084     QStringList isos = propMap.value( GEOM::propertyName( GEOM::NbIsos ) ).toString().split( GEOM::subSectionSeparator() );
1085     nbIsos[0] = isos[0].toInt();
1086     nbIsos[1] = isos[1].toInt();
1087   }
1088   actor->SetNbIsos( nbIsos );
1089
1090   // - set iso-lines width
1091   actor->SetIsosWidth( HasIsosWidth() ? GetIsosWidth() : propMap.value( GEOM::propertyName( GEOM::IsosWidth ) ).toInt() );
1092
1093   // - set iso-lines color
1094   if ( HasIsosColor() )
1095     c = SalomeApp_Tools::color( Quantity_Color((Quantity_NameOfColor)GetIsosColor()) );
1096   else
1097     c = propMap.value( GEOM::propertyName( GEOM::IsosColor ) ).value<QColor>();
1098   actor->SetIsosColor( c.redF(), c.greenF(), c.blueF() );
1099
1100   // set colors
1101
1102   if ( HasColor()  ) {
1103     // - same color for all sub-actors
1104     Quantity_Color aColor( (Quantity_NameOfColor)GetColor() );
1105     actor->SetColor( aColor.Red(), aColor.Green(), aColor.Blue() );
1106   }
1107   else {
1108     // shading color (for non-physical materials)
1109     if ( !material.isPhysical() ) {
1110       c = propMap.value( GEOM::propertyName( GEOM::ShadingColor ) ).value<QColor>();
1111       actor->GetFrontMaterial()->SetColor( c.redF(), c.greenF(), c.blueF() );
1112       actor->GetBackMaterial()->SetColor( c.redF(), c.greenF(), c.blueF() );
1113     }
1114
1115     // - standalone edge color
1116     c = propMap.value( GEOM::propertyName( GEOM::LineColor ) ).value<QColor>();
1117     actor->SetIsolatedEdgeColor( c.redF(), c.greenF(), c.blueF() );
1118
1119     c = propMap.value( GEOM::propertyName( GEOM::WireframeColor ) ).value<QColor>();
1120     // - shared edges color ???
1121     actor->SetSharedEdgeColor( c.redF(), c.greenF(), c.blueF() );
1122
1123     c = propMap.value( GEOM::propertyName( GEOM::FreeBndColor ) ).value<QColor>();
1124     // - free edges color ???
1125     actor->SetFreeEdgeColor( c.redF(), c.greenF(), c.blueF() );
1126
1127     // - point color
1128     c = propMap.value( GEOM::propertyName( GEOM::PointColor ) ).value<QColor>();
1129     actor->SetPointColor( c.redF(), c.greenF(), c.blueF() );
1130   }
1131
1132   // - color for edges in shading+edges mode
1133   c = propMap.value( GEOM::propertyName( GEOM::OutlineColor ) ).value<QColor>();
1134   actor->SetEdgesInShadingColor( c.redF(), c.greenF(), c.blueF() );
1135
1136   // - color of labels (shape name)
1137   c = colorFromResources( "label_color", QColor( 255, 255, 255 ) );
1138   actor->SetLabelColor( c.redF(), c.greenF(), c.blueF() );
1139
1140   // set opacity
1141   if( HasTransparency() ) {
1142     actor->SetOpacity( 1.0 - GetTransparency() );
1143   } else {
1144     actor->SetOpacity( 1.0 - propMap.value( GEOM::propertyName( GEOM::Transparency ) ).toDouble() );
1145   }
1146
1147   // set line width
1148   actor->SetWidth( HasWidth() ?
1149                    // predefined line width, manually set to displayer via GEOM_Displayer::SetLineWidth() function 
1150                    GetWidth() :
1151                    // libe width from properties
1152                    propMap.value( GEOM::propertyName( GEOM::LineWidth ) ).toInt() );
1153   
1154   // set display vectors flag
1155   actor->SetVectorMode( propMap.value( GEOM::propertyName( GEOM::EdgesDirection ) ).toBool() );
1156
1157   // set display vertices flag
1158   actor->SetVerticesMode( propMap.value( GEOM::propertyName( GEOM::Vertices ) ).toBool() );
1159
1160   // set display name flag
1161   actor->SetNameMode( propMap.value( GEOM::propertyName( GEOM::ShowName ) ).toBool() );
1162
1163   // set display mode
1164   int displayMode = HasDisplayMode() ? 
1165     // predefined display mode, manually set to displayer via GEOM_Displayer::SetDisplayMode() function 
1166     GetDisplayMode() :
1167     // display mode from properties
1168     propMap.value( GEOM::propertyName( GEOM::DisplayMode ) ).toInt();
1169
1170   // specific processing of 'shading with edges' mode, as VTK provides only the following standard display modes:
1171   // Points - 0, Wireframe - 1, Surface - 2, Insideframe - 3, SurfaceWithEdges - 4
1172   // GEOM actor allows alternative display modes (see VTKViewer::Representation enum) and enum in GEOM_Actor:
1173   // eWireframe - 0, eShading - 1, eShadingWithEdges - 3
1174
1175   if ( displayMode == 2 )
1176       // this is 'Shading with edges' mode => we have to do the correct mapping to EDisplayMode
1177       // enum in GEOM_Actor (and further to VTKViewer::Representation enum)
1178     displayMode++;
1179   actor->setDisplayMode( displayMode );
1180
1181   if ( myToActivate )
1182     actor->PickableOn();
1183   else
1184     actor->PickableOff();
1185
1186   if ( create && !isTemporary && aMgrId != -1 ) {
1187     // set properties to the study
1188     study->setObjectProperties( aMgrId, entry, propMap );
1189   }
1190 }
1191
1192 //=================================================================
1193 /*!
1194  *  GEOM_Displayer::updateDimensions
1195  *  Creates or renews dimension presentation for the IO.
1196  */
1197 //=================================================================
1198 void GEOM_Displayer::updateDimensions( const Handle(SALOME_InteractiveObject)& theIO,
1199                                        SALOME_OCCPrs* thePrs,
1200                                        const gp_Ax3& theShapeLCS )
1201 {
1202   SalomeApp_Study* aStudy = getStudy();
1203   if ( !aStudy )
1204   {
1205     return;
1206   }
1207
1208   if ( theIO.IsNull() )
1209   {
1210     return;
1211   }
1212
1213   SOCC_Prs* anOccPrs = dynamic_cast<SOCC_Prs*>( thePrs );
1214
1215   AIS_ListOfInteractive aListOfIO;
1216
1217   anOccPrs->GetObjects( aListOfIO );
1218
1219   AIS_ListIteratorOfListOfInteractive aIterateIO( aListOfIO );
1220
1221   // remove outdated presentations of dimensions
1222   for ( ; aIterateIO.More(); aIterateIO.Next() )
1223   {
1224     const Handle(AIS_InteractiveObject)& anIO = aIterateIO.Value();
1225     if ( !anIO->IsKind( STANDARD_TYPE( AIS_Dimension ) ) )
1226     {
1227       continue;
1228     }
1229
1230     aListOfIO.Remove( aIterateIO );
1231
1232     if ( !aIterateIO.More() )
1233     {
1234       break;
1235     }
1236   }
1237
1238   // prepare dimension styling
1239   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
1240
1241   QColor  aQColor       = aResMgr->colorValue  ( "Geometry", "dimensions_color", QColor( 0, 255, 0 ) );
1242   int     aLineWidth    = aResMgr->integerValue( "Geometry", "dimensions_line_width", 1 );
1243   QFont   aFont         = aResMgr->fontValue   ( "Geometry", "dimensions_font", QFont("Y14.5M-2009", 14) );
1244   double  anArrowLength = aResMgr->doubleValue ( "Geometry", "dimensions_arrow_length", 5 );
1245   bool    isUnitsShown  = aResMgr->booleanValue( "Geometry", "dimensions_show_units", false );
1246   QString aUnitsLength  = aResMgr->stringValue ( "Geometry", "dimensions_length_units", "m" );
1247   QString aUnitsAngle   = aResMgr->stringValue ( "Geometry", "dimensions_angle_units", "deg" );
1248   bool    aUseText3d    = aResMgr->booleanValue( "Geometry", "dimensions_use_text3d", false );
1249
1250   // restore dimension presentation from saved attribute or property data
1251   AIS_ListOfInteractive aRestoredDimensions;
1252
1253   QVariant aProperty = aStudy->getObjectProperty( GEOM::sharedPropertiesId(),
1254                                                   theIO->getEntry(),
1255                                                   GEOM::propertyName( GEOM::Dimensions ),
1256                                                   QVariant() );
1257
1258   GEOMGUI_DimensionProperty aRecords;
1259
1260   if ( aProperty.isValid() && aProperty.canConvert<GEOMGUI_DimensionProperty>() )
1261   {
1262     aRecords = aProperty.value<GEOMGUI_DimensionProperty>();
1263   }
1264   else
1265   {
1266     aRecords.LoadFromAttribute( getStudy(), theIO->getEntry() );
1267   }
1268   
1269   // create up-to-date dimension presentations
1270   for ( int aPrsIt = 0; aPrsIt < aRecords.GetNumber(); ++aPrsIt )
1271   {
1272     if ( !aRecords.IsVisible( aPrsIt ) )
1273     {
1274       continue;
1275     }
1276
1277     // init dimension by type
1278     Handle(AIS_Dimension) aPrs;
1279     switch( aRecords.GetType( aPrsIt ) )
1280     {
1281       case GEOMGUI_DimensionProperty::DimensionType_Length :
1282       {
1283         Handle(GEOM_AISLength) aLength = new GEOM_AISLength( aPrsIt );
1284         aRecords.GetRecord( aPrsIt )->AsLength()->Update( aLength, theShapeLCS );
1285         aPrs = aLength;
1286         break;
1287       }
1288
1289       case GEOMGUI_DimensionProperty::DimensionType_Diameter :
1290       {
1291         Handle(GEOM_AISDiameter) aDiam = new GEOM_AISDiameter( aPrsIt );
1292         aRecords.GetRecord( aPrsIt )->AsDiameter()->Update( aDiam, theShapeLCS );
1293         aPrs = aDiam;
1294         break;
1295       }
1296
1297       case GEOMGUI_DimensionProperty::DimensionType_Angle :
1298       {
1299         Handle(GEOM_AISAngle) anAng = new GEOM_AISAngle( aPrsIt );
1300         aRecords.GetRecord( aPrsIt )->AsAngle()->Update( anAng, theShapeLCS );
1301         aPrs = anAng;
1302         break;
1303       }
1304     }
1305
1306     aPrs->SetOwner( theIO );
1307
1308     Quantity_Color aColor( aQColor.redF(), aQColor.greenF(), aQColor.blueF(), Quantity_TOC_RGB );
1309
1310     Handle(Prs3d_DimensionAspect) aStyle = new Prs3d_DimensionAspect();
1311
1312     aStyle->SetCommonColor( aColor );
1313     aStyle->MakeUnitsDisplayed( (Standard_Boolean) isUnitsShown );
1314     aStyle->MakeText3d( aUseText3d );
1315     aStyle->MakeTextShaded( Standard_True );
1316     int fsize = aFont.pixelSize() != -1 ? aFont.pixelSize() : aFont.pointSize();
1317     aStyle->SetExtensionSize( fsize * 0.5 );
1318     aStyle->TextAspect()->SetFont( aFont.family().toLatin1().data() );
1319     aStyle->TextAspect()->SetHeight( fsize );
1320     aStyle->ArrowAspect()->SetLength( anArrowLength );
1321     aStyle->LineAspect()->SetWidth( aLineWidth );
1322     aStyle->SetTextHorizontalPosition( aPrs->DimensionAspect()->TextHorizontalPosition() );
1323     aStyle->SetTextVerticalPosition( aPrs->DimensionAspect()->TextVerticalPosition() );
1324     aStyle->SetArrowOrientation( aPrs->DimensionAspect()->ArrowOrientation() );
1325     aPrs->SetDimensionAspect( aStyle );
1326     aPrs->SetPolygonOffsets( Aspect_POM_Fill, -1.0, -1.0 );
1327     aPrs->Attributes()->SetDimLengthDisplayUnits( aUnitsLength.toLatin1().data() );
1328     aPrs->Attributes()->SetDimAngleDisplayUnits( aUnitsAngle.toLatin1().data() );
1329
1330     if ( aPrs->IsKind( STANDARD_TYPE(AIS_AngleDimension) ) )
1331     {
1332       // show degree symbol for dimension instead of label "deg"
1333       if ( aUnitsAngle == "deg" )
1334       {
1335         aPrs->SetSpecialSymbol(0xB0);
1336         aPrs->SetDisplaySpecialSymbol( isUnitsShown ? AIS_DSS_After : AIS_DSS_No );
1337         aStyle->MakeUnitsDisplayed(Standard_False);
1338       }
1339       else
1340       {
1341         aPrs->SetDisplaySpecialSymbol(AIS_DSS_No);
1342         aStyle->MakeUnitsDisplayed( (Standard_Boolean) isUnitsShown );
1343       }
1344     }
1345     else
1346     {
1347       aStyle->MakeUnitsDisplayed( (Standard_Boolean) isUnitsShown );
1348     }
1349
1350     aListOfIO.Append( aPrs );
1351   }
1352
1353   // update presentation
1354   anOccPrs->Clear();
1355
1356   for ( aIterateIO.Initialize( aListOfIO ); aIterateIO.More(); aIterateIO.Next() )
1357   {
1358     anOccPrs->AddObject( aIterateIO.Value() );
1359   }
1360 }
1361
1362 //=================================================================
1363 /*!
1364  *  GEOM_Displayer::Erase
1365  *  Calls Erase() method for each object in the given list
1366  */
1367 //=================================================================
1368 void GEOM_Displayer::Erase( const SALOME_ListIO& theIOList,
1369                             const bool forced,
1370                             const bool updateViewer )
1371 {
1372   SALOME_ListIteratorOfListIO Iter( theIOList );
1373   for ( ; Iter.More(); Iter.Next() )
1374     Erase( Iter.Value(), forced, false );
1375
1376   if ( updateViewer )
1377     UpdateViewer();
1378 }
1379
1380 //=================================================================
1381 /*!
1382  *  GEOM_Displayer::Redisplay
1383  *  Calls Redisplay() method for each object in the given list
1384  */
1385 //=================================================================
1386 void GEOM_Displayer::Redisplay( const SALOME_ListIO& theIOList,
1387                                 const bool updateViewer,
1388                                 const bool checkActiveViewer )
1389 {
1390   SALOME_ListIteratorOfListIO Iter( theIOList );
1391   for ( ; Iter.More(); Iter.Next() )
1392     Redisplay( Iter.Value(), false, checkActiveViewer );
1393
1394   if ( updateViewer )
1395     UpdateViewer();
1396 }
1397
1398 //=================================================================
1399 /*!
1400  *  GEOM_Displayer::Redisplay
1401  *  Calls Redisplay() method for each object in the given list
1402  */
1403 //=================================================================
1404 void GEOM_Displayer::Redisplay( const SALOME_ListIO& theIOList,
1405                                 const bool theUpdateViewer,
1406                                 SALOME_View* theViewFrame )
1407 {
1408   SALOME_ListIteratorOfListIO anIter( theIOList );
1409   for ( ; anIter.More(); anIter.Next() )
1410   {
1411     Redisplay( anIter.Value(), false, theViewFrame );
1412   }
1413
1414   if ( theUpdateViewer )
1415   {
1416     UpdateViewer();
1417   }
1418 }
1419
1420 //=================================================================
1421 /*!
1422  *  GEOM_Displayer::Update
1423  *  Update OCC presentaion
1424  *  [ Reimplemented from SALOME_Displayer ]
1425  */
1426 //=================================================================
1427 void GEOM_Displayer::Update( SALOME_OCCPrs* prs )
1428 {
1429   SOCC_Prs* occPrs = dynamic_cast<SOCC_Prs*>( prs );
1430   SalomeApp_Study* study = getStudy();
1431
1432   if ( !occPrs || myShape.IsNull() || !study )
1433     return;
1434
1435   if ( myType == GEOM_MARKER && myShape.ShapeType() == TopAbs_FACE )
1436   {
1437     // 
1438     // specific processing for local coordinate system presentation
1439     // 
1440
1441     TopoDS_Face aFace = TopoDS::Face( myShape );
1442     Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast( BRep_Tool::Surface( aFace ) );
1443     if ( !aPlane.IsNull() )
1444     {
1445       gp_Ax3 aPos = aPlane->Pln().Position();
1446       Handle(Geom_Axis2Placement) aPlc = new Geom_Axis2Placement( aPos.Ax2() );
1447
1448       Handle(GEOM_AISTrihedron) aTrh;
1449
1450       if ( occPrs->IsNull() )
1451       {
1452         // new presentation is being created
1453         aTrh = new GEOM_AISTrihedron( aPlc );
1454         occPrs->AddObject( aTrh );
1455       }
1456       else
1457       {
1458         // presentation is being updated
1459         AIS_ListOfInteractive aList;
1460         occPrs->GetObjects( aList );
1461         AIS_ListIteratorOfListOfInteractive anIter( aList );
1462         for ( ; anIter.More() && aTrh.IsNull(); anIter.Next() ) {
1463           aTrh = Handle(GEOM_AISTrihedron)::DownCast( anIter.Value() );
1464         }
1465       }
1466         
1467       if ( !aTrh.IsNull() ) {
1468         // predefined color, manually set to displayer via GEOM_Displayer::SetColor() function
1469         if ( HasColor() )
1470           aTrh->SetColor( (Quantity_NameOfColor)GetColor() );
1471         // predefined line width, manually set to displayer via GEOM_Displayer::SetLineWidth() function 
1472         if ( HasWidth() )
1473           aTrh->SetWidth( GetWidth() );
1474         
1475         if ( !myIO.IsNull() )
1476         {
1477           aTrh->setIO( myIO );
1478           aTrh->SetOwner( myIO );
1479         }
1480         aTrh->SetComponent( aPlc );
1481         aTrh->SetToUpdate();
1482       }
1483       occPrs->SetToActivate( ToActivate() );
1484     }
1485   }
1486   else
1487   {
1488     // 
1489     // processing for usual geometry presentation
1490     // 
1491
1492     // if presentation is empty we try to create new one
1493     if ( occPrs->IsNull() )
1494     {
1495       // create presentation (specific for vectors)
1496       Handle(GEOM_AISShape) AISShape = ( myType == GEOM_VECTOR ) ? new GEOM_AISVector( myShape, "" )
1497                                                                  : new GEOM_AISShape ( myShape, "" );
1498
1499       if( myType == GEOM_FIELD_STEP )
1500         AISShape->SetHilightMode( GEOM_AISShape::CustomHighlight );
1501       // update shape properties
1502       updateShapeProperties( AISShape, true );
1503
1504       // add shape to the presentation
1505       occPrs->AddObject( AISShape );
1506
1507       // In accordance with ToActivate() value object will be activated/deactivated
1508       // when it will be displayed
1509       occPrs->SetToActivate( ToActivate() );
1510
1511       if ( AISShape->isTopLevel() && GEOM_AISShape::topLevelDisplayMode() == GEOM_AISShape::TopShowAdditionalWActor ) {
1512         // 21671: EDF 1829 GEOM : Bring to front selected objects (continuation):
1513
1514         // create additional wireframe shape
1515         Handle(GEOM_TopWireframeShape) aWirePrs = new GEOM_TopWireframeShape(myShape);
1516         aWirePrs->SetWidth(AISShape->Width());
1517         if ( !myIO.IsNull() ) {
1518           aWirePrs->setIO( myIO );
1519           aWirePrs->SetOwner( myIO );
1520         }
1521
1522         // add shape to the presentation
1523         occPrs->AddObject( aWirePrs );
1524       }
1525     }
1526     // if presentation is found -> set again shape for it
1527     else
1528     {
1529       AIS_ListOfInteractive IOList;
1530       occPrs->GetObjects( IOList );
1531       AIS_ListIteratorOfListOfInteractive Iter( IOList );
1532       for ( ; Iter.More(); Iter.Next() )
1533       {
1534         Handle(GEOM_AISShape) AISShape = Handle(GEOM_AISShape)::DownCast( Iter.Value() );
1535         if ( AISShape.IsNull() )
1536           continue;
1537
1538         // re-set shape (it might be changed)
1539         if ( AISShape->Shape() != myShape )
1540           AISShape->Set( myShape );
1541
1542         // update shape properties
1543         updateShapeProperties( AISShape, false );
1544
1545         // force updating
1546         AISShape->UpdateSelection();
1547         AISShape->SetToUpdate();
1548       }
1549     }
1550
1551     updateDimensions( myIO, occPrs, gp_Ax3().Transformed( myShape.Location().Transformation() ) );
1552   }
1553 }
1554
1555 //=================================================================
1556 /*!
1557  *  GEOM_Displayer::Update
1558  *  Update VTK presentaion
1559  *  [ Reimplemented from SALOME_Displayer ]
1560  */
1561 //=================================================================
1562 void GEOM_Displayer::Update( SALOME_VTKPrs* prs )
1563 {
1564   SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
1565   SalomeApp_Study* study = getStudy();
1566
1567   if ( !vtkPrs || myShape.IsNull() || !study )
1568     return;
1569
1570   if ( myType == GEOM_MARKER && myShape.ShapeType() == TopAbs_FACE )
1571   {
1572     // 
1573     // specific processing for local coordinate system presentation
1574     // 
1575
1576     TopoDS_Face aFace = TopoDS::Face( myShape );
1577     Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast( BRep_Tool::Surface( aFace ) );
1578     if ( !aPlane.IsNull() ) {
1579       gp_Ax3 aPos = aPlane->Pln().Position();
1580       Handle(Geom_Axis2Placement) aPlc = new Geom_Axis2Placement( aPos.Ax2() );
1581
1582       GEOM_VTKTrihedron* aTrh = 0;
1583
1584       if ( vtkPrs->IsNull() ) {
1585         // new presentation is being created
1586         aTrh = GEOM_VTKTrihedron::New();
1587         vtkPrs->AddObject( aTrh );
1588       }
1589       else {
1590         // presentation is being updated
1591         vtkActorCollection* actors = vtkPrs->GetObjects();
1592         if ( actors ) {
1593           actors->InitTraversal();
1594           vtkActor* a = actors->GetNextActor();
1595           while ( a && !aTrh ) {
1596             aTrh = GEOM_VTKTrihedron::SafeDownCast( a );
1597             a = actors->GetNextActor();
1598           }
1599         }
1600       }
1601       
1602       if ( aTrh ) {
1603         // predefined color, manually set to displayer via GEOM_Displayer::SetColor() function
1604         if ( HasColor() ) {
1605           Quantity_Color aColor( (Quantity_NameOfColor)GetColor() );
1606           aTrh->SetColor( aColor.Red(), aColor.Green(), aColor.Blue() );
1607         }
1608 #ifdef VTK_TRIHEDRON_WIDTH
1609         // 
1610         // VSR: currently isn't supported
1611         //
1612         // predefined line width, manually set to displayer via GEOM_Displayer::SetLineWidth() function 
1613         if ( HasWidth() )
1614           aTrh->SetWidth( GetWidth() );
1615 #endif
1616
1617         if ( !myIO.IsNull() )
1618           aTrh->setIO( myIO );
1619
1620         aTrh->SetPlacement( aPlc );
1621       }
1622     }
1623   }
1624   else
1625   {
1626     // 
1627     // processing for usual geometry presentation
1628     // 
1629
1630     // if presentation is empty we try to create new one
1631     if ( vtkPrs->IsNull() )
1632     {
1633       // create an actor
1634       GEOM_Actor* actor = GEOM_Actor::New();
1635       // update actor properties
1636       updateActorProperties( actor, true );
1637       // add actor to the presentation
1638       vtkPrs->AddObject( actor );
1639     }
1640     else {
1641       // presentation is being updated
1642       vtkActorCollection* actors = vtkPrs->GetObjects();
1643       if ( actors ) {
1644         actors->InitTraversal();
1645         vtkActor* a = actors->GetNextActor();
1646         while ( a ) {
1647           GEOM_Actor* actor = GEOM_Actor::SafeDownCast( a );
1648           if ( actor ) {
1649             // update actor properties
1650             updateActorProperties( actor, false );
1651             a = actors->GetNextActor();
1652           }
1653         }
1654       }
1655     }
1656   }
1657 }
1658
1659 //=================================================================
1660 /*!
1661  *  GEOM_Displayer::BuildPrs
1662  *  Build presentation accordint to the current viewer type
1663  */
1664 //=================================================================
1665 SALOME_Prs* GEOM_Displayer::BuildPrs( GEOM::GEOM_Object_ptr theObj )
1666 {
1667   if ( theObj->_is_nil() )
1668     return 0;
1669
1670   myViewFrame = GetActiveView();
1671   if ( myViewFrame == 0 )
1672     return 0;
1673
1674   SALOME_Prs* aPrs = myViewFrame->CreatePrs();
1675   if ( aPrs == 0 )
1676     return 0;
1677
1678   internalReset();
1679   setShape( GEOM_Client::get_client().GetShape( GeometryGUI::GetGeomGen(), theObj ) );
1680   myType = theObj->GetType();
1681
1682   // Update presentation
1683   UpdatePrs( aPrs );
1684
1685   return aPrs;
1686 }
1687
1688 //=================================================================
1689 /*!
1690  *  GEOM_Displayer::BuildPrs
1691  *  Build presentation accordint to the current viewer type
1692  */
1693 //=================================================================
1694 SALOME_Prs* GEOM_Displayer::BuildPrs( const TopoDS_Shape& theShape )
1695 {
1696   myViewFrame = GetActiveView();
1697   if ( theShape.IsNull() || myViewFrame == 0 )
1698     return 0;
1699
1700   SALOME_Prs* aPrs = myViewFrame->CreatePrs();
1701   if ( aPrs == 0 )
1702     return 0;
1703
1704   internalReset();
1705   setShape( theShape );
1706   myType = -1;
1707
1708   UpdatePrs( aPrs );
1709
1710   return aPrs;
1711 }
1712
1713 //=================================================================
1714 /*!
1715  *  GEOM_Displayer::buildPresentation
1716  *  Builds/finds object's presentation for the current viewer
1717  *  Calls corresponding Update() method by means of double dispatch
1718  *  [ internal ]
1719  */
1720 //=================================================================
1721 SALOME_Prs* GEOM_Displayer::buildPresentation( const QString& entry,
1722                                                SALOME_View* theViewFrame )
1723 {
1724   SALOME_Prs* prs = 0;
1725   internalReset();
1726
1727   myViewFrame = theViewFrame ? theViewFrame : GetActiveView();
1728
1729   if ( myViewFrame )
1730   {
1731     prs = LightApp_Displayer::buildPresentation( entry, theViewFrame );
1732     if ( prs )
1733     {
1734       Handle( SALOME_InteractiveObject ) theIO = new SALOME_InteractiveObject();
1735       theIO->setEntry( entry.toLatin1().constData() );
1736       if ( !theIO.IsNull() )
1737       {
1738         // set interactive object
1739         setIO( theIO );
1740         //  Find SOBject (because shape should be published previously)
1741         SUIT_Session* session = SUIT_Session::session();
1742         SUIT_Application* app = session->activeApplication();
1743         if ( app )
1744         {
1745           SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
1746           if ( study )
1747           {
1748             _PTR(SObject) SO ( study->studyDS()->FindObjectID( theIO->getEntry() ) );
1749             if ( SO )
1750             {
1751               // get CORBA reference to data object
1752               CORBA::Object_var object = GeometryGUI::ClientSObjectToObject(SO);
1753               if ( !CORBA::is_nil( object ) )
1754               {
1755                 // downcast to GEOM base object
1756                 GEOM::GEOM_BaseObject_var GeomBaseObject = GEOM::GEOM_BaseObject::_narrow( object );
1757                 if ( !GeomBaseObject->_is_nil() )
1758                 {
1759                   myType = GeomBaseObject->GetType();
1760
1761                   // downcast to GEOM object
1762                   GEOM::GEOM_Object_var GeomObject = GEOM::GEOM_Object::_narrow( GeomBaseObject );
1763                   if ( myType == GEOM_FIELD_STEP )
1764                   {
1765                     // get the GEOM object from the field's shape
1766                     GEOM::GEOM_FieldStep_var GeomFieldStep = GEOM::GEOM_FieldStep::_narrow( GeomBaseObject );
1767                     if ( !GeomFieldStep->_is_nil() )
1768                     {
1769                       GEOM::GEOM_Field_var GeomField = GeomFieldStep->GetField();
1770                       if ( !GeomField->_is_nil() )
1771                         GeomObject = GeomField->GetShape();
1772                     }
1773
1774                     // read the field step information
1775                     readFieldStepInfo( GeomFieldStep );
1776                   }
1777
1778                   if ( !GeomObject->_is_nil() )
1779                   {
1780                     theIO->setName( GeomObject->GetName() );
1781                     // finally set shape
1782                     setShape( GEOM_Client::get_client().GetShape( GeometryGUI::GetGeomGen(), GeomObject ) );
1783                   }
1784                 }
1785               }
1786             }
1787           }
1788         }
1789       }
1790       UpdatePrs( prs );  // Update presentation by using of the double dispatch
1791     }
1792   }
1793   return prs;
1794 }
1795
1796 //=================================================================
1797 /*!
1798  *  GEOM_Displayer::buildSubshapePresentation
1799  *  Builds/finds object's presentation for the current viewer
1800  *  Calls corresponding Update() method by means of double dispatch
1801  *  For not published objects (for Mantis issue 0020435)
1802  */
1803 //=================================================================
1804 SALOME_Prs* GEOM_Displayer::buildSubshapePresentation(const TopoDS_Shape& aShape,
1805                                                       const QString& entry,
1806                                                       SALOME_View* theViewFrame)
1807 {
1808   SALOME_Prs* prs = 0;
1809   internalReset();
1810
1811   myViewFrame = theViewFrame ? theViewFrame : GetActiveView();
1812
1813   if (myViewFrame)
1814   {
1815     prs = LightApp_Displayer::buildPresentation(entry, theViewFrame);
1816     if (prs)
1817     {
1818       Handle(SALOME_InteractiveObject) theIO = new SALOME_InteractiveObject();
1819       theIO->setEntry(entry.toLatin1().constData());
1820       if (!theIO.IsNull())
1821       {
1822         // set interactive object
1823         setIO(theIO);
1824         // finally set shape
1825         setShape(aShape);
1826         myType = GEOM_SUBSHAPE;
1827       }
1828       UpdatePrs(prs);  // Update presentation by using of the double dispatch
1829     }
1830   }
1831   return prs;
1832 }
1833
1834 //=================================================================
1835 /*!
1836  *  GEOM_Displayer::internalReset
1837  *  Resets internal data
1838  *  [internal]
1839  */
1840 //=================================================================
1841 void GEOM_Displayer::internalReset()
1842 {
1843   myIO.Nullify();
1844   myShape.Nullify();
1845
1846   myFieldDataType = GEOM::FDT_Double;
1847   myFieldDimension = 0;
1848   myFieldStepData.clear();
1849   myFieldStepName.Clear();
1850   myFieldStepRangeMin = 0;
1851   myFieldStepRangeMax = 0;
1852 }
1853
1854 //=================================================================
1855 /*!
1856  *  GEOM_Displayer::LocalSelection
1857  *  Activate selection of CAD shapes with activisation of selection
1858  *  of their sub-shapes (with opened local context for OCC viewer)
1859  */
1860 //=================================================================
1861 void GEOM_Displayer::LocalSelection( const Handle(SALOME_InteractiveObject)& theIO, const std::list<int> modes )
1862 {
1863   SUIT_Session* session = SUIT_Session::session();
1864   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
1865   if ( !app )
1866     return;
1867
1868   LightApp_SelectionMgr* sm = app->selectionMgr();
1869
1870   // remove all filters from selection
1871   sm->clearFilters();
1872
1873   SALOME_View* vf = GetActiveView();
1874   if ( vf ) {
1875     if (!theIO.IsNull() && !vf->isVisible(theIO))
1876       Display(theIO);
1877     SALOME_Prs* prs = vf->CreatePrs( theIO.IsNull() ? 0 : theIO->getEntry() );
1878     vf->LocalSelection( prs, modes );
1879     delete prs;  // delete presentation because displayer is its owner
1880   }
1881 }
1882
1883 //=================================================================
1884 /*!
1885  *  GEOM_Displayer::LocalSelection
1886  *  Activate selection of CAD shapes with activisation of selection
1887  *  of their sub-shapes (with opened local context for OCC viewer)
1888  */
1889 //=================================================================
1890 void GEOM_Displayer::LocalSelection( const Handle(SALOME_InteractiveObject)& theIO, const int theMode )
1891 {
1892   std::list<int> modes;
1893   modes.push_back( theMode );
1894   LocalSelection( theIO, modes );
1895 }
1896
1897 //=================================================================
1898 /*!
1899  *  GEOM_Displayer::globalSelection
1900  *  Activate selection of CAD shapes without activisation of selection
1901  *  of their sub-shapes (without opened local context for OCC viewer)
1902  */
1903 //=================================================================
1904 void GEOM_Displayer::GlobalSelection( const int theMode, const bool update )
1905 {
1906   TColStd_MapOfInteger aModes;
1907   aModes.Add( theMode );
1908   GlobalSelection( aModes, update );
1909 }
1910
1911 //=================================================================
1912 /*!
1913  *  GEOM_Displayer::globalSelection
1914  *  Activate selection of CAD shapes without activisation of selection
1915  *  of their sub-shapes (without opened local context for OCC viewer)
1916  */
1917 //=================================================================
1918 void GEOM_Displayer::GlobalSelection( const TColStd_MapOfInteger& theModes,
1919                                       const bool update, const QList<int>* theSubShapes )
1920 {
1921   SUIT_Session* session = SUIT_Session::session();
1922   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
1923   if ( !app )
1924     return;
1925
1926   SALOME_View* vf = GetActiveView();
1927   if ( vf == 0 )
1928     return;
1929
1930   // Close local context
1931   vf->GlobalSelection( update );
1932
1933   // Set selection filters in accordance with current mode
1934   LightApp_SelectionMgr* sm = app->selectionMgr();
1935   if ( !sm )
1936     return;
1937
1938   // Remove from selection temporary objects if necessary
1939   if ( !theModes.Contains( GEOM_PREVIEW ) )
1940     clearTemporary( sm );
1941
1942   //@ aSel->ClearIndex();
1943
1944   sm->clearFilters();
1945
1946   // Remove filters from AIS_InteractiveContext
1947   Handle(AIS_InteractiveContext) ic;
1948   SOCC_Viewer* viewer = dynamic_cast<SOCC_Viewer*>( vf );
1949   if ( viewer )
1950     {
1951       ic = viewer->getAISContext();
1952       if ( !ic.IsNull() )
1953         ic->RemoveFilters();
1954     }
1955
1956   if ( theModes.Contains( GEOM_ALLOBJECTS ) )
1957     return;
1958
1959   SUIT_SelectionFilter* aFilter;
1960   if ( theModes.Extent() == 1 )
1961     {
1962       int aMode = TColStd_MapIteratorOfMapOfInteger( theModes ).Key();
1963
1964       if ( aMode == GEOM_COMPOUNDFILTER )
1965         aFilter = getComplexFilter( theSubShapes );
1966       else
1967         aFilter = getFilter( aMode );
1968     }
1969   else if ( theModes.Extent() > 1 )
1970     {
1971       TColStd_MapOfInteger aTopAbsModes;
1972       TColStd_MapIteratorOfMapOfInteger anIter( theModes );
1973       QList<SUIT_SelectionFilter*> aListOfFilters;
1974       for ( ; anIter.More(); anIter.Next() )
1975         {
1976           SUIT_SelectionFilter* aFilter;
1977           int aMode = anIter.Key();
1978           if ( aMode == GEOM_COMPOUNDFILTER )
1979             aFilter = getComplexFilter( theSubShapes );
1980           else
1981             aFilter = getFilter( aMode );
1982
1983           if ( aFilter )
1984             aListOfFilters.append( aFilter );
1985         }
1986
1987       aFilter = new GEOM_LogicalFilter( aListOfFilters, GEOM_LogicalFilter::LO_OR );
1988     }
1989   else
1990     return;
1991
1992   if ( aFilter )
1993     {
1994       sm->installFilter( aFilter );
1995       if ( !ic.IsNull() )
1996         {
1997           Handle(GEOM_OCCFilter) anOCCFilter = new GEOM_OCCFilter( sm );
1998           ic->AddFilter( anOCCFilter );
1999         }
2000     }
2001 }
2002
2003 //=================================================================
2004 /*!
2005  *  GEOM_Displayer::LocalSelection
2006  *  Activate selection of CAD shapes with activisation of selection
2007  *  of their sub-shapes (with opened local context for OCC viewer)
2008  */
2009 //=================================================================
2010 void GEOM_Displayer::LocalSelection( const SALOME_ListIO& theIOList, const std::list<int> modes )
2011 {
2012   SALOME_ListIteratorOfListIO Iter( theIOList );
2013   for ( ; Iter.More(); Iter.Next() )
2014     LocalSelection( Iter.Value(), modes );
2015 }
2016
2017 //=================================================================
2018 /*!
2019  *  GEOM_Displayer::LocalSelection
2020  *  Activate selection of CAD shapes with activisation of selection
2021  *  of their sub-shapes (with opened local context for OCC viewer)
2022  */
2023 //=================================================================
2024 void GEOM_Displayer::LocalSelection( const SALOME_ListIO& theIOList, const int theMode )
2025 {
2026   std::list<int> modes;
2027   modes.push_back( theMode );
2028   LocalSelection( theIOList, modes );
2029 }
2030
2031 //=================================================================
2032 /*!
2033  *  GEOM_Displayer::BeforeDisplay
2034  *  Called before displaying of pars. Close local context
2035  *  [ Reimplemented from SALOME_Displayer ]
2036  */
2037 //=================================================================
2038 void GEOM_Displayer::BeforeDisplay( SALOME_View* v, const SALOME_OCCPrs* )
2039 {
2040   SOCC_Viewer* vf = dynamic_cast<SOCC_Viewer*>( v );
2041   if ( vf )
2042   {
2043     Handle(AIS_InteractiveContext) ic = vf->getAISContext();
2044     if ( !ic.IsNull() )
2045     {
2046       if ( ic->HasOpenedContext() )
2047       ic->CloseAllContexts();
2048     }
2049   }
2050 }
2051
2052 void GEOM_Displayer::AfterDisplay( SALOME_View* v, const SALOME_OCCPrs* p )
2053 {
2054   UpdateColorScale(false,false);
2055 }
2056
2057 void GEOM_Displayer::BeforeErase( SALOME_View* v, const SALOME_OCCPrs* p )
2058 {
2059   LightApp_Displayer::BeforeErase( v, p );
2060   releaseTextures( p );
2061 }
2062
2063 void GEOM_Displayer::AfterErase( SALOME_View* v, const SALOME_OCCPrs* p )
2064 {
2065   LightApp_Displayer::AfterErase( v, p );
2066   UpdateColorScale(false,false);
2067 }
2068
2069 //=================================================================
2070 /*!
2071  *  GEOM_Displayer::SetColor
2072  *  Set color for shape displaying. If it is equal -1 then default color is used.
2073  *  Available values are from Quantity_NameOfColor enumeration
2074  */
2075 //=================================================================
2076 void GEOM_Displayer::SetColor( const int color )
2077 {
2078   if ( color == -1 )
2079     UnsetColor();
2080   else
2081   {
2082     myColor = color;
2083     myShadingColor = Quantity_Color( (Quantity_NameOfColor)color );
2084   }
2085 }
2086
2087 int GEOM_Displayer::GetColor() const
2088 {
2089   return myColor;
2090 }
2091
2092 bool GEOM_Displayer::HasColor() const
2093 {
2094   return myColor != -1;
2095 }
2096
2097 void GEOM_Displayer::UnsetColor()
2098 {
2099   myColor = -1;
2100
2101   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
2102   QColor col = resMgr->colorValue( "Geometry", "shading_color", QColor( 255, 0, 0 ) );
2103   myShadingColor = SalomeApp_Tools::color( col );
2104 }
2105
2106 //=================================================================
2107 /*!
2108  *  GEOM_Displayer::SetTransparency
2109  *  Set transparency for shape displaying.
2110  */
2111 //=================================================================
2112 double GEOM_Displayer::SetTransparency( const double transparency )
2113 {
2114   double prevTransparency = myTransparency;
2115   myTransparency = transparency;
2116   return prevTransparency;
2117 }
2118
2119 //=================================================================
2120 /*!
2121  *  GEOM_Displayer::GetTransparency
2122  *  Get transparency for shape displaying.
2123  */
2124 //=================================================================
2125 double GEOM_Displayer::GetTransparency() const
2126 {
2127   return myTransparency;
2128 }
2129
2130 //=================================================================
2131 /*!
2132  *  GEOM_Displayer::HasTransparency
2133  *  Check if transparency for shape displaying is set.
2134  */
2135 //=================================================================
2136 bool GEOM_Displayer::HasTransparency() const
2137 {
2138   return myTransparency >= 0;
2139 }
2140
2141 //=================================================================
2142 /*!
2143  *  GEOM_Displayer::UnsetTransparency
2144  *  Unset transparency for shape displaying.
2145  */
2146 //=================================================================
2147 double GEOM_Displayer::UnsetTransparency()
2148 {
2149   return SetTransparency( -1 );
2150 }
2151
2152 //=================================================================
2153 /*!
2154  *  GEOM_Displayer::SetTexture
2155  */
2156 //=================================================================
2157 void GEOM_Displayer::SetTexture( const std::string& texureFileName )
2158 {
2159   myTexture = texureFileName;
2160 }
2161
2162 bool GEOM_Displayer::HasTexture() const
2163 {
2164   return myTexture != "";
2165 }
2166
2167 std::string GEOM_Displayer::GetTexture() const
2168 {
2169   return myTexture;
2170 }
2171
2172 //=================================================================
2173 /*!
2174  *  GEOM_Displayer::SetWidth
2175  *  Set width of shape displaying. If it is equal -1 then default width is used.
2176  */
2177 //=================================================================
2178 void GEOM_Displayer::SetWidth( const double width )
2179 {
2180   myWidth = width;
2181 }
2182
2183 double GEOM_Displayer::GetWidth() const
2184 {
2185   return myWidth;
2186 }
2187
2188 bool GEOM_Displayer::HasWidth() const
2189 {
2190   return myWidth != -1;
2191 }
2192
2193 void GEOM_Displayer::UnsetWidth()
2194 {
2195   myWidth = -1;
2196 }
2197
2198 int GEOM_Displayer::GetIsosWidth() const
2199 {
2200   return myIsosWidth;
2201 }
2202
2203 void GEOM_Displayer::SetIsosWidth(const int width)
2204 {
2205   myIsosWidth = width;
2206 }
2207
2208 bool GEOM_Displayer::HasIsosWidth() const
2209 {
2210   return myIsosWidth != -1;
2211 }
2212
2213 int GEOM_Displayer::SetNbIsos( const int nbIsos )
2214 {
2215   int prevNbIsos = myNbIsos;
2216   myNbIsos = nbIsos;
2217   return prevNbIsos;
2218 }
2219
2220 int GEOM_Displayer::UnsetNbIsos()
2221 {
2222   return SetNbIsos( -1 );
2223 }
2224
2225 int GEOM_Displayer::GetNbIsos() const
2226 {
2227   return myNbIsos;
2228 }
2229
2230 bool GEOM_Displayer::HasNbIsos() const
2231 {
2232   return myNbIsos >= 0;
2233 }
2234
2235 int GEOM_Displayer::SetIsosColor( const int color )
2236 {
2237   int prevColor = myIsosColor;
2238   myIsosColor = color;
2239   return prevColor;
2240 }
2241
2242 int GEOM_Displayer::GetIsosColor() const
2243 {
2244   return myIsosColor;
2245 }
2246
2247 bool GEOM_Displayer::HasIsosColor() const
2248 {
2249   return myIsosColor != -1;
2250 }
2251
2252 int GEOM_Displayer::UnsetIsosColor()
2253 {
2254   return SetIsosColor( -1 );
2255 }
2256
2257 //=================================================================
2258 /*!
2259  *  GEOM_Displayer::SetToActivate
2260  *  This method is used for activisation/deactivisation of objects to be displayed
2261  */
2262 //=================================================================
2263 void GEOM_Displayer::SetToActivate( const bool toActivate )
2264 {
2265   myToActivate = toActivate;
2266 }
2267 bool GEOM_Displayer::ToActivate() const
2268 {
2269   return myToActivate;
2270 }
2271
2272 //=================================================================
2273 /*!
2274  *  GEOM_Displayer::clearTemporary
2275  *  Removes from selection temporary objects
2276  */
2277 //=================================================================
2278 void GEOM_Displayer::clearTemporary( LightApp_SelectionMgr* theSelMgr )
2279 {
2280   SALOME_ListIO selected, toSelect;
2281   theSelMgr->selectedObjects( selected );
2282
2283   for (  SALOME_ListIteratorOfListIO it( selected ) ; it.More(); it.Next() ) {
2284     Handle(SALOME_InteractiveObject) io = it.Value();
2285     if ( !io.IsNull() && io->hasEntry() && strncmp( io->getEntry(), "TEMP_", 5 ) != 0 )
2286       toSelect.Append( it.Value() );
2287   }
2288
2289   theSelMgr->setSelectedObjects( toSelect, true );
2290 }
2291
2292 void GEOM_Displayer::SetName( const char* theName )
2293 {
2294   myName = theName;
2295 }
2296
2297 void GEOM_Displayer::UnsetName()
2298 {
2299   myName = "";
2300 }
2301
2302 SalomeApp_Study* GEOM_Displayer::getStudy() const
2303 {
2304   return dynamic_cast<SalomeApp_Study*>( myApp->activeStudy() );
2305 }
2306
2307 void GEOM_Displayer::setIO( const Handle(SALOME_InteractiveObject)& theIO )
2308 {
2309   myIO = theIO;
2310 }
2311
2312 void GEOM_Displayer::setShape( const TopoDS_Shape& theShape )
2313 {
2314   myShape = theShape;
2315 }
2316
2317 void GEOM_Displayer::setFieldStepInfo( const GEOM::field_data_type theFieldDataType,
2318                                        const int theFieldDimension,
2319                                        const QList<QVariant>& theFieldStepData,
2320                                        const TCollection_AsciiString& theFieldStepName,
2321                                        const double theFieldStepRangeMin,
2322                                        const double theFieldStepRangeMax )
2323 {
2324   myFieldDataType = theFieldDataType;
2325   myFieldDimension = theFieldDimension;
2326   myFieldStepData = theFieldStepData;
2327   myFieldStepName = theFieldStepName;
2328   myFieldStepRangeMin = theFieldStepRangeMin;
2329   myFieldStepRangeMax = theFieldStepRangeMax;
2330 }
2331
2332 bool GEOM_Displayer::canBeDisplayed( const QString& entry, const QString& viewer_type ) const
2333 {
2334   _PTR(SObject) anObj = getStudy()->studyDS()->FindObjectID( (const char*)entry.toLatin1() );
2335   GEOM::GEOM_Object_var aGeomObj = GEOM::GEOM_Object::_narrow(GeometryGUI::ClientSObjectToObject(anObj)); // enable displaying of GEOM objects
2336   GEOM::GEOM_FieldStep_var aFieldStepObj = GEOM::GEOM_FieldStep::_narrow(GeometryGUI::ClientSObjectToObject(anObj)); // enable displaying of GEOM field steps
2337   GEOM::GEOM_Gen_var aCompObj = GEOM::GEOM_Gen::_narrow(GeometryGUI::ClientSObjectToObject(anObj)); // enable displaying of whole GEOM component
2338   return ( !CORBA::is_nil( aGeomObj ) || !CORBA::is_nil( aFieldStepObj ) || !CORBA::is_nil( aCompObj ) ) &&
2339          (viewer_type == SOCC_Viewer::Type() || viewer_type == SVTK_Viewer::Type());
2340 }
2341
2342 int GEOM_Displayer::SetDisplayMode( const int theMode )
2343 {
2344   int aPrevMode = myDisplayMode;
2345   if ( theMode != -1 ) {
2346     myDisplayMode = theMode;
2347     myHasDisplayMode = true;
2348   }
2349   else {
2350     UnsetDisplayMode();
2351   }
2352   return aPrevMode;
2353 }
2354
2355 int GEOM_Displayer::GetDisplayMode() const
2356 {
2357   return myDisplayMode;
2358 }
2359
2360 int GEOM_Displayer::UnsetDisplayMode()
2361 {
2362   int aPrevMode = myDisplayMode;
2363   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
2364   myDisplayMode = resMgr->integerValue( "Geometry", "display_mode", 0 );
2365   myHasDisplayMode = false;
2366   return aPrevMode;
2367 }
2368
2369 bool GEOM_Displayer::HasDisplayMode() const
2370 {
2371   return myHasDisplayMode;
2372 }
2373
2374 SALOMEDS::Color GEOM_Displayer::getPredefinedUniqueColor()
2375 {
2376   static QList<QColor> colors;
2377
2378   if ( colors.isEmpty() ) {
2379
2380     for (int s = 0; s < 2 ; s++)
2381     {
2382       for (int v = 100; v >= 40; v = v - 20)
2383       {
2384         for (int h = 0; h < 359 ; h = h + 60)
2385         {
2386           colors.append(QColor::fromHsv(h, 255 - s * 127, v * 255 / 100));
2387         }
2388       }
2389     }
2390   }
2391
2392   static int currentColor = randomize( colors.size() );
2393
2394   SALOMEDS::Color color;
2395   color.R = (double)colors[currentColor].red()   / 255.0;
2396   color.G = (double)colors[currentColor].green() / 255.0;
2397   color.B = (double)colors[currentColor].blue()  / 255.0;
2398
2399   currentColor = (currentColor+1) % colors.count();
2400
2401   return color;
2402 }
2403
2404 SALOMEDS::Color GEOM_Displayer::getUniqueColor( const QList<SALOMEDS::Color>& theReservedColors )
2405 {
2406   int aHue = -1;
2407   int aTolerance = 64;
2408   int anIterations = 0;
2409   int aPeriod = 5;
2410
2411   while( 1 )
2412   {
2413     anIterations++;
2414     if( anIterations % aPeriod == 0 )
2415     {
2416       aTolerance /= 2;
2417       if( aTolerance < 1 )
2418         break;
2419     }
2420     //std::cout << "Iteration N" << anIterations << " (tolerance=" << aTolerance << ")"<< std::endl;
2421
2422     aHue = (int)( 360.0 * rand() / RAND_MAX );
2423     //std::cout << "Hue = " << aHue << std::endl;
2424
2425     //std::cout << "Auto colors : ";
2426     bool ok = true;
2427     QList<SALOMEDS::Color>::const_iterator it = theReservedColors.constBegin();
2428     QList<SALOMEDS::Color>::const_iterator itEnd = theReservedColors.constEnd();
2429     for( ; it != itEnd; ++it )
2430     {
2431       SALOMEDS::Color anAutoColor = *it;
2432       QColor aQColor( (int)( anAutoColor.R * 255.0 ), (int)( anAutoColor.G * 255.0 ), (int)( anAutoColor.B * 255.0 ) );
2433
2434       int h, s, v;
2435       aQColor.getHsv( &h, &s, &v );
2436       //std::cout << h << " ";
2437       if( abs( h - aHue ) < aTolerance )
2438       {
2439         ok = false;
2440         //std::cout << "break (diff = " << abs( h - aHue ) << ")";
2441         break;
2442       }
2443     }
2444     //std::cout << std::endl;
2445
2446     if( ok )
2447       break;
2448   }
2449
2450   //std::cout << "Hue of the returned color = " << aHue << std::endl;
2451   QColor aColor;
2452   aColor.setHsv( aHue, 255, 255 );
2453
2454   SALOMEDS::Color aSColor;
2455   aSColor.R = (double)aColor.red() / 255.0;
2456   aSColor.G = (double)aColor.green() / 255.0;
2457   aSColor.B = (double)aColor.blue() / 255.0;
2458
2459   return aSColor;
2460 }
2461
2462 PropMap GEOM_Displayer::getObjectProperties( SalomeApp_Study* study,
2463                                              const QString& entry,
2464                                              SALOME_View* view )
2465 {
2466   // get default properties for the explicitly specified default view type
2467   PropMap propMap = GEOM_Displayer::getDefaultPropertyMap();
2468
2469   if ( study && view ) {
2470     SUIT_ViewModel* viewModel = dynamic_cast<SUIT_ViewModel*>( view );
2471     SUIT_ViewManager* viewMgr = ( viewModel != 0 ) ? viewModel->getViewManager() : 0;
2472     int viewId = ( viewMgr != 0 ) ? viewMgr->getGlobalId() : -1;
2473   
2474     if ( viewModel && viewId != -1 ) {
2475       // get properties from the study
2476       PropMap storedMap = study->getObjectProperties( viewId, entry );
2477       // overwrite default properties from stored ones (that are specified)
2478       for ( int prop = GEOM::Visibility; prop <= GEOM::LastProperty; prop++ ) {
2479         if ( storedMap.contains( GEOM::propertyName( (GEOM::Property)prop ) ) )
2480           propMap.insert( GEOM::propertyName( (GEOM::Property)prop ), 
2481                           storedMap.value( GEOM::propertyName( (GEOM::Property)prop ) ) );
2482       }
2483       // ... specific processing for color
2484       // ... current implementation is to use same stored color for all aspects
2485       // ... (TODO) possible future improvements about free boundaries, standalone edges etc colors can be here
2486       if ( storedMap.contains( GEOM::propertyName( GEOM::Color ) ) ) {
2487         propMap.insert( GEOM::propertyName( GEOM::ShadingColor ),   storedMap.value( GEOM::propertyName( GEOM::Color ) ) );
2488         propMap.insert( GEOM::propertyName( GEOM::WireframeColor ), storedMap.value( GEOM::propertyName( GEOM::Color ) ) );
2489         propMap.insert( GEOM::propertyName( GEOM::LineColor ),      storedMap.value( GEOM::propertyName( GEOM::Color ) ) );
2490         propMap.insert( GEOM::propertyName( GEOM::FreeBndColor ),   storedMap.value( GEOM::propertyName( GEOM::Color ) ) );
2491         propMap.insert( GEOM::propertyName( GEOM::PointColor ),     storedMap.value( GEOM::propertyName( GEOM::Color ) ) );
2492       }
2493
2494       if ( !entry.isEmpty() ) {
2495         // get CORBA reference to geom object
2496         _PTR(SObject) SO( study->studyDS()->FindObjectID( entry.toStdString() ) );
2497         if ( SO ) {
2498           CORBA::Object_var object = GeometryGUI::ClientSObjectToObject( SO );
2499           if ( !CORBA::is_nil( object ) ) {
2500             GEOM::GEOM_Object_var geomObject = GEOM::GEOM_Object::_narrow( object );
2501             if ( !CORBA::is_nil( geomObject ) ) { // to check
2502               // check that geom object has color properly set
2503               bool hasColor = false;
2504               SALOMEDS::Color aSColor = getColor( geomObject, hasColor );
2505               // set color from geometry object (only once, if it is not yet set in GUI)
2506               // current implementation is to use same color for all aspects
2507               // (TODO) possible future improvements about free boundaries, standalone edges etc colors can be here
2508               if ( hasColor && !storedMap.contains( GEOM::propertyName( GEOM::Color ) ) ) {
2509                 QColor objColor = QColor::fromRgbF( aSColor.R, aSColor.G, aSColor.B );
2510                 propMap.insert( GEOM::propertyName( GEOM::ShadingColor ),   objColor );
2511                 propMap.insert( GEOM::propertyName( GEOM::WireframeColor ), objColor );
2512                 propMap.insert( GEOM::propertyName( GEOM::LineColor ),      objColor );
2513                 propMap.insert( GEOM::propertyName( GEOM::FreeBndColor ),   objColor );
2514                 propMap.insert( GEOM::propertyName( GEOM::PointColor ),     objColor );
2515               }
2516               // check that object has point marker properly set
2517               GEOM::marker_type mType = geomObject->GetMarkerType();
2518               GEOM::marker_size mSize = geomObject->GetMarkerSize();
2519               int mTextureId = geomObject->GetMarkerTexture();
2520               bool hasMarker = ( mType > GEOM::MT_NONE && mType < GEOM::MT_USER && mSize > GEOM::MS_NONE && mSize <= GEOM::MS_70 ) || 
2521                                ( mType == GEOM::MT_USER && mTextureId > 0 );
2522               // set point marker from geometry object (only once, if it is not yet set in GUI)
2523               if ( hasMarker && !storedMap.contains( GEOM::propertyName( GEOM::PointMarker ) ) ) {
2524                 if ( mType > GEOM::MT_NONE && mType < GEOM::MT_USER ) {
2525                   // standard type
2526                   propMap.insert( GEOM::propertyName( GEOM::PointMarker ),
2527                                   QString( "%1%2%3" ).arg( (int)mType ).arg( GEOM::subSectionSeparator() ).arg( (int)mSize ) );
2528                 }
2529                 else if ( mType == GEOM::MT_USER ) {
2530                   // custom texture
2531                   propMap.insert( GEOM::propertyName( GEOM::PointMarker ), QString::number( mTextureId ) );
2532                 }
2533               }
2534             }
2535           }
2536         }
2537       }
2538     }
2539   }
2540   return propMap;
2541 }
2542
2543 PropMap GEOM_Displayer::getDefaultPropertyMap()
2544 {
2545   PropMap propMap;
2546
2547   // get resource manager
2548   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
2549
2550   // fill in the properties map with default values
2551
2552   // - visibility (false by default)
2553   propMap.insert( GEOM::propertyName( GEOM::Visibility ), false );
2554
2555   // - nb isos (take default value from preferences)
2556   propMap.insert( GEOM::propertyName( GEOM::NbIsos ),
2557                   QString( "%1%2%3" ).
2558                   arg( resMgr->integerValue( "Geometry", "iso_number_u", 1 ) ).
2559                   arg( GEOM::subSectionSeparator() ).
2560                   arg( resMgr->integerValue( "Geometry", "iso_number_v", 1 ) ) );
2561
2562   // - transparency (opacity = 1-transparency)
2563   propMap.insert( GEOM::propertyName( GEOM::Transparency ),
2564                   resMgr->integerValue( "Geometry", "transparency", 0 ) / 100. );
2565
2566   // - display mode (take default value from preferences)
2567   propMap.insert( GEOM::propertyName( GEOM::DisplayMode ),
2568                   resMgr->integerValue( "Geometry", "display_mode", 0 ) );
2569
2570   // - show edges direction flag (false by default)
2571   propMap.insert( GEOM::propertyName( GEOM::EdgesDirection ), false );
2572
2573   // - show vertices flag (false by default)
2574   propMap.insert( GEOM::propertyName( GEOM::Vertices ), false );
2575
2576   // - show name flag (false by default)
2577   propMap.insert( GEOM::propertyName( GEOM::ShowName ), false );
2578
2579   // - shading color (take default value from preferences)
2580   propMap.insert( GEOM::propertyName( GEOM::ShadingColor ),
2581                   colorFromResources( "shading_color", QColor( 255, 255, 0 ) ) );
2582
2583   // - wireframe color (take default value from preferences)
2584   propMap.insert( GEOM::propertyName( GEOM::WireframeColor ),
2585                   colorFromResources( "wireframe_color", QColor( 255, 255, 0 ) ) );
2586
2587   // - standalone edges color (take default value from preferences)
2588   propMap.insert( GEOM::propertyName( GEOM::LineColor ),
2589                   colorFromResources( "line_color", QColor( 255, 0, 0 ) ) );
2590
2591   // - free boundaries color (take default value from preferences)
2592   propMap.insert( GEOM::propertyName( GEOM::FreeBndColor ),
2593                   colorFromResources( "free_bound_color", QColor( 0, 255, 0 ) ) );
2594
2595   // - points color (take default value from preferences)
2596   propMap.insert( GEOM::propertyName( GEOM::PointColor ),
2597                   colorFromResources( "point_color", QColor( 255, 255, 0 ) ) );
2598
2599   // - isos color (take default value from preferences)
2600   propMap.insert( GEOM::propertyName( GEOM::IsosColor ),
2601                   colorFromResources( "isos_color", QColor( 200, 200, 200 ) ) );
2602
2603   // - outlines color (take default value from preferences)
2604   propMap.insert( GEOM::propertyName( GEOM::OutlineColor ),
2605                   colorFromResources( "edges_in_shading_color", QColor( 180, 180, 180 ) ) );
2606
2607   // - deflection coefficient (take default value from preferences)
2608   propMap.insert( GEOM::propertyName( GEOM::Deflection ),
2609                   resMgr->doubleValue( "Geometry", "deflection_coeff", 0.001 ) );
2610
2611   // - material (take default value from preferences)
2612   Material_Model material;
2613   material.fromResources( resMgr->stringValue( "Geometry", "material", "Plastic" ) );
2614   propMap.insert( GEOM::propertyName( GEOM::Material ), material.toProperties() );
2615
2616   // - edge width (take default value from preferences)
2617   propMap.insert( GEOM::propertyName( GEOM::LineWidth ),
2618                   resMgr->integerValue( "Geometry", "edge_width", 1 ) );
2619
2620   // - isos width (take default value from preferences)
2621   propMap.insert( GEOM::propertyName( GEOM::IsosWidth ),
2622                   resMgr->integerValue( "Geometry", "isolines_width", 1 ) );
2623
2624   // - point marker (take default value from preferences)
2625   propMap.insert( GEOM::propertyName( GEOM::PointMarker ),
2626                   QString( "%1%2%3" ).
2627                   arg( resMgr->integerValue( "Geometry", "type_of_marker", 1 ) + 1 ).
2628                   arg( GEOM::subSectionSeparator() ).
2629                   arg( resMgr->integerValue( "Geometry", "marker_scale", 1 ) ) );
2630
2631   // - top-level flag (false by default)
2632   propMap.insert( GEOM::propertyName( GEOM::TopLevel ), false );
2633
2634   return propMap;
2635 }
2636
2637 SALOMEDS::Color GEOM_Displayer::getColor(GEOM::GEOM_Object_var theGeomObject, bool& hasColor) {
2638   SALOMEDS::Color aSColor;
2639   hasColor = false;
2640
2641   SUIT_Session* session = SUIT_Session::session();
2642   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
2643
2644   if ( app && !theGeomObject->_is_nil()) {
2645     SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
2646
2647     if ( study ) {
2648       _PTR(Study) aStudy = study->studyDS();
2649       aSColor = theGeomObject->GetColor();
2650       hasColor = aSColor.R >= 0 && aSColor.G >= 0 && aSColor.B >= 0;
2651       if ( !hasColor ) {
2652 #ifdef GENERAL_AUTOCOLOR // auto-color for all sub-shapes
2653         bool general_autocolor = true;
2654 #else                    // auto-color for groups only
2655         bool general_autocolor = false;
2656 #endif                   // GENERAL_AUTOCOLOR
2657         if ( general_autocolor || theGeomObject->GetType() == GEOM_GROUP ) {
2658           GEOM::GEOM_Object_var aMainObject = theGeomObject->GetMainShape();
2659           if ( !CORBA::is_nil( aMainObject ) && aMainObject->GetAutoColor() ) {
2660 #ifdef SIMPLE_AUTOCOLOR  // simplified algorithm for auto-colors
2661             aSColor = getPredefinedUniqueColor();
2662             hasColor = true;
2663 #else                    // old algorithm  for auto-colors
2664             QList<SALOMEDS::Color> aReservedColors;
2665             CORBA::String_var IOR = app->orb()->object_to_string( aMainObject );
2666             _PTR(SObject) aMainSObject( aStudy->FindObjectIOR( IOR.in() ) );
2667             if ( aMainSObject ) {
2668               _PTR(ChildIterator) it( aStudy->NewChildIterator( aMainSObject ) );
2669               for ( ; it->More(); it->Next() ) {
2670                 _PTR(SObject) aChildSObject( it->Value() );
2671                 GEOM::GEOM_Object_var aChildObject =
2672                   GEOM::GEOM_Object::_narrow(GeometryGUI::ClientSObjectToObject(aChildSObject));
2673                 if ( CORBA::is_nil( aChildObject ) )
2674                   continue;
2675
2676                 SALOMEDS::Color aReservedColor = aChildObject->GetColor();
2677                 if ( aReservedColor.R >= 0 && aReservedColor.G >= 0 && aReservedColor.B >= 0 )
2678                   aReservedColors.append( aReservedColor );
2679               }
2680             }
2681             aSColor = getUniqueColor( aReservedColors );
2682             hasColor = true;
2683 #endif                   // SIMPLE_AUTOCOLOR
2684           }
2685         }
2686       }
2687     }
2688   }
2689   return aSColor;
2690 }
2691
2692
2693 void GEOM_Displayer::EraseWithChildren(const Handle(SALOME_InteractiveObject)& theIO,
2694                                        const bool eraseOnlyChildren) {
2695   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
2696   if ( !app )
2697     return;
2698
2699   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
2700   if ( !appStudy )
2701     return;
2702
2703   LightApp_DataObject* parent = appStudy->findObjectByEntry(theIO->getEntry());
2704
2705   if( !parent)
2706     return;
2707
2708   // Erase from all views
2709   QList<SALOME_View*> views;
2710   SALOME_View* view;
2711   ViewManagerList vmans = app->viewManagers();
2712   SUIT_ViewManager* vman;
2713   foreach ( vman, vmans ) {
2714     SUIT_ViewModel* vmod = vman->getViewModel();
2715     view = dynamic_cast<SALOME_View*> ( vmod );
2716     if ( view )
2717       views.append( view );
2718   }
2719
2720   if( views.count() == 0 )
2721     return;
2722
2723   //Erase childrens w/o update views
2724   DataObjectList listObj = parent->children( true );
2725   SUIT_DataObject* obj;
2726   foreach( obj, listObj ) {
2727     LightApp_DataObject* l_obj = dynamic_cast<LightApp_DataObject*>(obj);
2728     if(l_obj)
2729       foreach ( view, views ) {
2730       Handle(SALOME_InteractiveObject) anIO =
2731         new SALOME_InteractiveObject(qPrintable(l_obj->entry()), "GEOM", "");
2732       Erase(anIO, false, false, view);
2733     }
2734   }
2735
2736   //Erase parent with view update or repaint views
2737   foreach ( view, views ) {
2738     if(!eraseOnlyChildren)
2739       Erase(theIO, false, true, view);
2740     else
2741       view->Repaint();
2742   }
2743 }
2744
2745 void GEOM_Displayer::readFieldStepInfo( GEOM::GEOM_FieldStep_var theGeomFieldStep )
2746 {
2747   if( theGeomFieldStep->_is_nil() )
2748     return;
2749
2750   GEOM::GEOM_Field_var aGeomField = theGeomFieldStep->GetField();
2751   if( aGeomField->_is_nil() )
2752     return;
2753
2754   GEOM::GEOM_Object_var aGeomFieldShape = aGeomField->GetShape();
2755   if( aGeomFieldShape->_is_nil() )
2756     return;
2757
2758   TCollection_AsciiString aFieldStepName( theGeomFieldStep->GetName() );
2759   TCollection_AsciiString aFieldName( aGeomField->GetName() );
2760   TCollection_AsciiString aShapeName( aGeomFieldShape->GetName() );
2761
2762   aFieldStepName = aShapeName + "\n" + aFieldName + "\n" + aFieldStepName;
2763
2764   GEOM::field_data_type aFieldDataType = aGeomField->GetDataType();
2765
2766   int aFieldDimension = aGeomField->GetDimension();
2767
2768   GEOM::string_array_var aFieldComponents = aGeomField->GetComponents();
2769   int aFieldNbComponents = aFieldComponents->length();
2770
2771   QList<QVariant> aFieldStepData;
2772   if( aFieldDataType == GEOM::FDT_Bool )
2773   {
2774     GEOM::GEOM_BoolFieldStep_var aGeomBoolFieldStep = GEOM::GEOM_BoolFieldStep::_narrow( theGeomFieldStep );
2775     if ( !aGeomBoolFieldStep->_is_nil() )
2776     {
2777       GEOM::short_array_var aValues = aGeomBoolFieldStep->GetValues();
2778       for( size_t i = 0, n = aValues->length(); i < n; i++ )
2779         aFieldStepData << (bool)aValues[i];
2780     }
2781   }
2782   else if( aFieldDataType == GEOM::FDT_Int )
2783   {
2784     GEOM::GEOM_IntFieldStep_var aGeomIntFieldStep = GEOM::GEOM_IntFieldStep::_narrow( theGeomFieldStep );
2785     if ( !aGeomIntFieldStep->_is_nil() )
2786     {
2787       GEOM::ListOfLong_var aValues = aGeomIntFieldStep->GetValues();
2788       for( size_t i = 0, n = aValues->length(); i < n; i++ )
2789         aFieldStepData << (qlonglong)aValues[i];
2790     }
2791   }
2792   else if( aFieldDataType == GEOM::FDT_Double )
2793   {
2794     GEOM::GEOM_DoubleFieldStep_var aGeomDoubleFieldStep = GEOM::GEOM_DoubleFieldStep::_narrow( theGeomFieldStep );
2795     if ( !aGeomDoubleFieldStep->_is_nil() )
2796     {
2797       GEOM::ListOfDouble_var aValues = aGeomDoubleFieldStep->GetValues();
2798       for( size_t i = 0, n = aValues->length(); i < n; i++ )
2799         aFieldStepData << (double)aValues[i];
2800     }
2801   }
2802   else if( aFieldDataType == GEOM::FDT_String )
2803   {
2804     GEOM::GEOM_StringFieldStep_var aGeomStringFieldStep = GEOM::GEOM_StringFieldStep::_narrow( theGeomFieldStep );
2805     if ( !aGeomStringFieldStep->_is_nil() )
2806     {
2807       GEOM::string_array_var aValues = aGeomStringFieldStep->GetValues();
2808       for( size_t i = 0, n = aValues->length(); i < n; i++ )
2809         aFieldStepData << QString( aValues[i] );
2810     }
2811   }
2812
2813   double aFieldStepRangeMin = 0, aFieldStepRangeMax = 0;
2814   aFieldStepData = groupFieldData( aFieldStepData,
2815                                    aFieldNbComponents,
2816                                    aFieldDataType == GEOM::FDT_String,
2817                                    aFieldStepRangeMin,
2818                                    aFieldStepRangeMax );
2819
2820   setFieldStepInfo( aFieldDataType,
2821                     aFieldDimension,
2822                     aFieldStepData,
2823                     aFieldStepName,
2824                     aFieldStepRangeMin,
2825                     aFieldStepRangeMax );
2826 }
2827
2828 QList<QVariant> GEOM_Displayer::groupFieldData( const QList<QVariant>& theFieldStepData,
2829                                                 const int theFieldNbComponents,
2830                                                 const bool theIsString,
2831                                                 double& theFieldStepRangeMin,
2832                                                 double& theFieldStepRangeMax )
2833 {
2834   QList<QVariant> aResultList;
2835   theFieldStepRangeMin = 0;
2836   theFieldStepRangeMax = 0;
2837
2838   if( theFieldStepData.isEmpty() || theFieldNbComponents < 1 )
2839     return aResultList;
2840
2841   int aNbSubShapes = theFieldStepData.count() / theFieldNbComponents;
2842
2843   QList<QVariant> aGroupedList;
2844
2845   bool anIsBoolean = false;
2846   for( int aSubShape = 0; aSubShape < aNbSubShapes; aSubShape++ )
2847   {
2848     double aNorm = 0;
2849     QStringList aStringList;
2850
2851     int aBaseIndex = aSubShape * theFieldNbComponents;
2852     for( int aComponent = 0; aComponent < theFieldNbComponents; aComponent++ )
2853     {
2854       int anIndex = aComponent + aBaseIndex;
2855
2856       const QVariant& aVariant = theFieldStepData[ anIndex ];
2857       if( theIsString )
2858       {
2859         if( aVariant.type() == QVariant::String )
2860           aStringList << aVariant.toString();
2861       }
2862       else
2863       {
2864         double aValue = 0;
2865         if( aVariant.type() == QVariant::Bool )
2866         {
2867           aValue = aVariant.toBool() ? 1.0 : 0.0;
2868           aNorm += aValue;
2869           anIsBoolean = true;
2870         }
2871         else
2872         {
2873           if( aVariant.type() == QVariant::LongLong )
2874             aValue = double( aVariant.toLongLong() );
2875           else if( aVariant.type() == QVariant::Double )
2876             aValue = aVariant.toDouble();
2877           aNorm += aValue * aValue;
2878         }
2879       }
2880     }
2881
2882     if( theIsString )
2883       aGroupedList << aStringList.join( "\n" );
2884     else
2885     {
2886       if( anIsBoolean )
2887         aNorm /= theFieldNbComponents;
2888       else
2889         aNorm = pow( aNorm, 0.5 );
2890
2891       if( aGroupedList.isEmpty() )
2892         theFieldStepRangeMin = theFieldStepRangeMax = aNorm;
2893       else
2894       {
2895         theFieldStepRangeMin = Min( theFieldStepRangeMin, aNorm );
2896         theFieldStepRangeMax = Max( theFieldStepRangeMax, aNorm );
2897       }
2898
2899       aGroupedList << aNorm;
2900     }
2901   }
2902
2903   if( anIsBoolean )
2904   {
2905     theFieldStepRangeMin = 0.0;
2906     theFieldStepRangeMax = 1.0;
2907   }
2908
2909   SUIT_Session* session = SUIT_Session::session();
2910   SUIT_ResourceMgr* resMgr = session->resourceMgr();
2911   Standard_Integer aNbIntervals = resMgr->integerValue( "Geometry", "scalar_bar_nb_intervals", 20 );
2912
2913   QListIterator<QVariant> anIter( aGroupedList );
2914   while( anIter.hasNext() )
2915   {
2916     const QVariant& aVariant = anIter.next();
2917     if( theIsString )
2918       aResultList << aVariant;
2919     else
2920     {
2921       QColor aQColor;
2922       Quantity_Color aColor;
2923       if( FindColor( aVariant.toDouble(), theFieldStepRangeMin, theFieldStepRangeMax, anIsBoolean ? 2 : aNbIntervals, aColor ) )
2924         aQColor = QColor::fromRgbF( aColor.Red(), aColor.Green(), aColor.Blue() );
2925       aResultList << aQColor;
2926     }
2927   }
2928   return aResultList;
2929 }
2930
2931 // Note: the method is copied from AIS_ColorScale class
2932 Standard_Integer GEOM_Displayer::HueFromValue( const Standard_Integer aValue,
2933                                                const Standard_Integer aMin,
2934                                                const Standard_Integer aMax )
2935 {
2936   Standard_Integer minLimit( 0 ), maxLimit( 230 );
2937
2938   Standard_Integer aHue = maxLimit;
2939   if ( aMin != aMax )
2940     aHue = (Standard_Integer)( maxLimit - ( maxLimit - minLimit ) * ( aValue - aMin ) / ( aMax - aMin ) );
2941
2942   aHue = Min( Max( minLimit, aHue ), maxLimit );
2943
2944   return aHue;
2945 }
2946
2947 // Note: the method is copied from AIS_ColorScale class
2948 Standard_Boolean GEOM_Displayer::FindColor( const Standard_Real aValue, 
2949                                             const Standard_Real aMin,
2950                                             const Standard_Real aMax,
2951                                             const Standard_Integer ColorsCount,
2952                                             Quantity_Color& aColor )
2953 {
2954   if( aValue<aMin || aValue>aMax || aMax<aMin )
2955     return Standard_False;
2956
2957   else
2958   {
2959     Standard_Real IntervNumber = 0;
2960     if( aValue<aMin )
2961       IntervNumber = 0;
2962     else if( aValue>aMax )
2963       IntervNumber = ColorsCount-1;
2964     else if( Abs( aMax-aMin ) > Precision::Approximation() )
2965       IntervNumber = Floor( Standard_Real( ColorsCount ) * ( aValue - aMin ) / ( aMax - aMin ) ); // 'Ceiling' replaced with 'Floor'
2966
2967     Standard_Integer Interv = Standard_Integer( IntervNumber );
2968
2969     aColor = Quantity_Color( HueFromValue( Interv, 0, ColorsCount - 1 ), 1.0, 1.0, Quantity_TOC_HLS );
2970
2971     return Standard_True;
2972   } 
2973 }
2974
2975 void GEOM_Displayer::UpdateColorScale( const bool theIsRedisplayFieldSteps, const bool updateViewer ) 
2976 {
2977   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( myApp->activeStudy() );
2978   if( !aStudy )
2979     return;
2980
2981   SOCC_Viewer* aViewModel = dynamic_cast<SOCC_Viewer*>( GetActiveView() );
2982   if( !aViewModel )
2983     return;
2984
2985 #if OCC_VERSION_MAJOR < 7
2986   Handle(V3d_Viewer) aViewer = aViewModel->getViewer3d();
2987   if( aViewer.IsNull() )
2988     return;
2989
2990   aViewer->InitActiveViews();
2991   if( !aViewer->MoreActiveViews() )
2992     return;
2993
2994   Handle(V3d_View) aView = aViewer->ActiveView();
2995   if( aView.IsNull() )
2996     return;
2997 #endif
2998
2999   Standard_Boolean anIsDisplayColorScale = Standard_False;
3000   TCollection_AsciiString aColorScaleTitle;
3001   Standard_Real aColorScaleMin = 0, aColorScaleMax = 0;
3002   Standard_Boolean anIsBoolean = Standard_False;
3003
3004   SALOME_ListIO aSelectedObjects;
3005   myApp->selectionMgr()->selectedObjects( aSelectedObjects );
3006   if( aSelectedObjects.Extent() == 1 )
3007   {
3008     Handle(SALOME_InteractiveObject) anIO = aSelectedObjects.First();
3009     if( !anIO.IsNull() )
3010     {
3011       SOCC_Prs* aPrs = dynamic_cast<SOCC_Prs*>( aViewModel->CreatePrs( anIO->getEntry() ) );
3012       if( aPrs )
3013       {
3014         AIS_ListOfInteractive aList;
3015         aPrs->GetObjects( aList );
3016         AIS_ListIteratorOfListOfInteractive anIter( aList );
3017         for( ; anIter.More(); anIter.Next() )
3018         {
3019           Handle(GEOM_AISShape) aShape = Handle(GEOM_AISShape)::DownCast( anIter.Value() );
3020           if( !aShape.IsNull() )
3021           {
3022             GEOM::field_data_type aFieldDataType;
3023             int aFieldDimension;
3024             QList<QVariant> aFieldStepData;
3025             TCollection_AsciiString aFieldStepName;
3026             double aFieldStepRangeMin, aFieldStepRangeMax;
3027             aShape->getFieldStepInfo( aFieldDataType,
3028                                       aFieldDimension,
3029                                       aFieldStepData,
3030                                       aFieldStepName,
3031                                       aFieldStepRangeMin,
3032                                       aFieldStepRangeMax );
3033             if( !aFieldStepData.isEmpty() && aFieldDataType != GEOM::FDT_String )
3034             {
3035               anIsDisplayColorScale = Standard_True;
3036               aColorScaleTitle = aFieldStepName;
3037               aColorScaleMin = aFieldStepRangeMin;
3038               aColorScaleMax = aFieldStepRangeMax;
3039               anIsBoolean = aFieldDataType == GEOM::FDT_Bool;
3040             }
3041           }
3042         }
3043       }
3044     }
3045   }
3046
3047   if( anIsDisplayColorScale )
3048   {
3049     SUIT_Session* session = SUIT_Session::session();
3050     SUIT_ResourceMgr* resMgr = session->resourceMgr();
3051
3052     Standard_Real anXPos = resMgr->doubleValue( "Geometry", "scalar_bar_x_position", 0.05 );
3053     Standard_Real anYPos = resMgr->doubleValue( "Geometry", "scalar_bar_y_position", 0.1 );
3054     Standard_Real aWidth = resMgr->doubleValue( "Geometry", "scalar_bar_width", 0.2 );
3055     Standard_Real aHeight = resMgr->doubleValue( "Geometry", "scalar_bar_height", 0.5 );
3056     Standard_Integer aTextHeight = resMgr->integerValue( "Geometry", "scalar_bar_text_height", 14 );
3057     Standard_Integer aNbIntervals = resMgr->integerValue( "Geometry", "scalar_bar_nb_intervals", 20 );
3058
3059 #if OCC_VERSION_MAJOR < 7
3060     Handle(Aspect_ColorScale) myColorScale = aView->ColorScale();
3061     if( !myColorScale.IsNull() )
3062     {
3063 #endif
3064
3065     myColorScale->SetXPosition( anXPos );
3066     myColorScale->SetYPosition( anYPos );
3067     myColorScale->SetWidth( aWidth );
3068     myColorScale->SetHeight( aHeight );
3069
3070     myColorScale->SetTextHeight( aTextHeight );
3071     myColorScale->SetNumberOfIntervals( anIsBoolean ? 2 : aNbIntervals );
3072
3073     myColorScale->SetTitle( aColorScaleTitle );
3074     myColorScale->SetRange( aColorScaleMin, aColorScaleMax );
3075
3076 #if OCC_VERSION_MAJOR < 7
3077     }
3078     if( !aView->ColorScaleIsDisplayed() )
3079       aView->ColorScaleDisplay();
3080   }
3081   else
3082     if( aView->ColorScaleIsDisplayed() )
3083       aView->ColorScaleErase();
3084 #else
3085     if( !aViewModel->getAISContext()->IsDisplayed( myColorScale ) )
3086       aViewModel->getAISContext()->Display( myColorScale );
3087   }
3088   else
3089     if( aViewModel->getAISContext()->IsDisplayed( myColorScale ) )
3090       aViewModel->getAISContext()->Erase( myColorScale );
3091 #endif
3092
3093   if( theIsRedisplayFieldSteps )
3094   {
3095     _PTR(Study) aStudyDS = aStudy->studyDS();
3096     QList<SUIT_ViewManager*> vmList;
3097     myApp->viewManagers( vmList );
3098     for( QList<SUIT_ViewManager*>::Iterator vmIt = vmList.begin(); vmIt != vmList.end(); vmIt++ )
3099     {
3100       if( SUIT_ViewManager* aViewManager = *vmIt )
3101       {
3102         const ObjMap& anObjects = aStudy->getObjectProperties( aViewManager->getGlobalId() );
3103         for( ObjMap::ConstIterator objIt = anObjects.begin(); objIt != anObjects.end(); objIt++ )
3104         {
3105           _PTR(SObject) aSObj( aStudyDS->FindObjectID( objIt.key().toLatin1().constData() ) );
3106           if( aSObj )
3107           {
3108             CORBA::Object_var anObject = GeometryGUI::ClientSObjectToObject( aSObj );
3109             if( !CORBA::is_nil( anObject ) )
3110             {
3111               GEOM::GEOM_FieldStep_var aFieldStep = GEOM::GEOM_FieldStep::_narrow( anObject );
3112               if( !aFieldStep->_is_nil() )
3113               {
3114                 CORBA::String_var aStepEntry = aFieldStep->GetStudyEntry();
3115                 Handle(SALOME_InteractiveObject) aStepIO =
3116                   new SALOME_InteractiveObject( aStepEntry.in(), "GEOM", "TEMP_IO" );
3117                 Redisplay( aStepIO, false, false );
3118               }
3119             }
3120           }
3121         }
3122       }
3123     }
3124   }
3125   if(updateViewer)
3126     UpdateViewer();
3127 }