Salome HOME
f917a2f0ec46c46bacfaa408209499ca4494553d
[modules/geom.git] / src / GEOMGUI / GEOM_Displayer.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // 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
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_AISShape.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 <Material_Model.h>
49
50 #include <SUIT_Desktop.h>
51 #include <SUIT_ViewWindow.h>
52 #include <SUIT_Session.h>
53 #include <SUIT_ViewManager.h>
54 #include <SUIT_ResourceMgr.h>
55
56 #include <Basics_OCCTVersion.hxx>
57
58 #include <SalomeApp_Study.h>
59 #include <SalomeApp_Application.h>
60 #include <LightApp_SelectionMgr.h>
61 #include <LightApp_DataObject.h>
62 #include <SalomeApp_TypeFilter.h>
63 #include <SalomeApp_Tools.h>
64
65 #include <SALOME_ListIteratorOfListIO.hxx>
66 #include <SALOME_ListIO.hxx>
67 #include <SALOME_Prs.h>
68
69 #include <SOCC_Prs.h>
70 #include <SOCC_ViewModel.h>
71
72 #include <SVTK_Prs.h>
73 #include <SVTK_ViewModel.h>
74
75 #include <OCCViewer_ViewWindow.h>
76 #include <OCCViewer_ViewPort3d.h>
77
78 // OCCT Includes
79 #include <AIS_Drawer.hxx>
80 #include <AIS_ListIteratorOfListOfInteractive.hxx>
81 #include <Aspect_ColorScale.hxx>
82 #include <Prs3d_IsoAspect.hxx>
83 #include <Prs3d_PointAspect.hxx>
84 #include <StdSelect_TypeOfEdge.hxx>
85 #include <StdSelect_TypeOfFace.hxx>
86 #include <StdSelect_DisplayMode.hxx>
87 #include <TopoDS_Face.hxx>
88 #include <BRep_Tool.hxx>
89 #include <Geom_Plane.hxx>
90 #include <Geom_Axis2Placement.hxx>
91 #include <Graphic3d_AspectFillArea3d.hxx>
92 #include <gp_Pln.hxx>
93 #include <TColStd_MapOfInteger.hxx>
94 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
95 #include <TopoDS_Iterator.hxx>
96 #include <Graphic3d_AspectMarker3d.hxx>
97 #include <TopTools_MapOfShape.hxx>
98 #include <TopTools_ListOfShape.hxx>
99 #include <TopTools_ListIteratorOfListOfShape.hxx>
100 #include <TopoDS.hxx>
101
102 #include <Prs3d_ShadingAspect.hxx>
103
104 #include <BRepMesh_IncrementalMesh.hxx>
105
106 // VTK Includes
107 #include <vtkActorCollection.h>
108 #include <vtkProperty.h>
109
110 // CORBA Headers
111 #include CORBA_CLIENT_HEADER(SALOMEDS_Attributes)
112
113 #include <GEOMImpl_Types.hxx>
114
115 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
116 #include <TColStd_HArray1OfByte.hxx>
117 #else
118 #include <Graphic3d_HArray1OfBytes.hxx>
119 #endif
120
121 // If the next macro is defined, autocolor feature works for all sub-shapes;
122 // if it is undefined, autocolor feature works for groups only
123 #define GENERAL_AUTOCOLOR
124 // Below macro, when uncommented, switches on simplified (more performant) algorithm
125 // of auto-color picking up
126 #define SIMPLE_AUTOCOLOR
127
128 // Hard-coded value of shape deflection coefficient for VTK viewer
129 const double VTK_MIN_DEFLECTION = 0.001;
130
131 //================================================================
132 // Function : getActiveStudy
133 // Purpose  : Get active study, returns 0 if no open study frame
134 //================================================================
135 static inline SalomeApp_Study* getActiveStudy()
136 {
137   SUIT_Session* session = SUIT_Session::session();
138   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
139   if ( app )
140     return ( SalomeApp_Study* )app->activeStudy();
141   return 0;
142 }
143
144 static inline int getViewManagerId( SALOME_View* theViewFrame) {
145   SUIT_ViewModel* aModel = dynamic_cast<SUIT_ViewModel*>(theViewFrame);
146   SUIT_ViewManager* aViewMgr = 0;
147   if (aModel != 0)
148     aViewMgr = aModel->getViewManager();
149   return ((aViewMgr == 0) ? -1 :aViewMgr->getGlobalId());
150 }
151
152 //================================================================
153 // Function : getTopAbsMode
154 // Purpose  : Get TopAbs_ShapeEnum value corresponding to the
155 //            one from GEOMImpl_Types.h
156 //================================================================
157 static inline int getTopAbsMode( const int implType )
158 {
159   switch ( implType )
160   {
161     case GEOM_COMPOUND  : return TopAbs_COMPOUND;
162     case GEOM_SOLID     : return TopAbs_SOLID;
163     case GEOM_SHELL     : return TopAbs_SHELL;
164     case GEOM_FACE      : return TopAbs_FACE;
165     case GEOM_WIRE      : return TopAbs_WIRE;
166     case GEOM_EDGE      : return TopAbs_EDGE;
167     case GEOM_POINT     : return TopAbs_VERTEX;
168     default             : return -1;
169   }
170 }
171
172 int GEOM_Displayer::getMinMaxShapeType( const TopoDS_Shape& shape, bool ismin )
173 {
174   if ( shape.IsNull() )
175     return TopAbs_SHAPE;
176
177   int ret = shape.ShapeType();
178
179   if ( shape.ShapeType() == TopAbs_COMPOUND || shape.ShapeType() == TopAbs_COMPSOLID ) {
180     TopoDS_Iterator it(shape, Standard_True, Standard_False);
181     for (; it.More(); it.Next()) {
182       TopoDS_Shape sub_shape = it.Value();
183       if ( sub_shape.IsNull() ) continue;
184       int stype = getMinMaxShapeType( sub_shape, ismin );
185       if ( stype == TopAbs_SHAPE ) continue;
186       if ( ismin && stype > ret )
187         ret = stype;
188       else if ( !ismin && ( ret < TopAbs_SOLID || stype < ret ) )
189         ret = stype;
190     }
191   }
192
193   return ret;
194 }
195
196 bool GEOM_Displayer::isCompoundOfVertices( const TopoDS_Shape& theShape )
197 {
198   return theShape.ShapeType() == TopAbs_COMPOUND && getMinMaxShapeType( theShape, false ) == TopAbs_VERTEX;
199 }
200
201 //================================================================
202 // Function : getFilter
203 // Purpose  : Get filter corresponding to the type of object
204 //            from GEOMImpl_Types.h
205 //================================================================
206 SUIT_SelectionFilter* GEOM_Displayer::getFilter( const int theMode )
207 {
208   SUIT_SelectionFilter* aFilter;
209
210   int aTopAbsMode = getTopAbsMode( theMode );
211   if ( aTopAbsMode != -1 )
212     aFilter = new GEOM_TypeFilter( getStudy(), aTopAbsMode, true ); //@ aFilter = new GEOM_TypeFilter( ( TopAbs_ShapeEnum )aTopAbsMode );
213   else
214     switch ( theMode )
215       {
216       case GEOM_LINE      : aFilter = new GEOM_EdgeFilter( getStudy(), StdSelect_Line ); break;
217       case GEOM_CIRCLE    : aFilter = new GEOM_EdgeFilter( getStudy(), StdSelect_Circle ); break;
218
219       case GEOM_PLANE     : aFilter = new GEOM_FaceFilter( getStudy(), StdSelect_Plane ); break;
220       case GEOM_CYLINDER  : aFilter = new GEOM_FaceFilter( getStudy(), StdSelect_Cylinder ); break;
221       case GEOM_SPHERE    : aFilter = new GEOM_FaceFilter( getStudy(), StdSelect_Sphere ); break;
222       case GEOM_TORUS     : aFilter = new GEOM_FaceFilter( getStudy(), StdSelect_Torus ); break;
223       case GEOM_REVOLUTION: aFilter = new GEOM_FaceFilter( getStudy(), StdSelect_Revol ); break;
224       case GEOM_CONE      : aFilter = new GEOM_FaceFilter( getStudy(), StdSelect_Cone ); break;
225
226       case GEOM_PREVIEW   : aFilter = new GEOM_PreviewFilter( getStudy() ); break;
227
228       case GEOM_ALLSHAPES : aFilter = new GEOM_SelectionFilter(getStudy(), true ); break;
229       case GEOM_ALLGEOM   : aFilter = new SalomeApp_TypeFilter( getStudy(), "GEOM" ); break;
230
231       default             : aFilter = new GEOM_TypeFilter( getStudy(), theMode ); break;
232       }
233
234   return aFilter;
235 }
236
237 //================================================================
238 // Function : getComplexFilter
239 // Purpose  : Get compound filter corresponding to the type of
240 //            object from GEOMImpl_Types.h
241 //================================================================
242 SUIT_SelectionFilter* GEOM_Displayer::getComplexFilter( const QList<int>* aSubShapes)
243 {
244   GEOM_CompoundFilter* aFilter;
245
246   if(aSubShapes != NULL ) {
247     aFilter = new GEOM_CompoundFilter(getStudy());
248     QList<int> aTopAbsTypes;
249     QList<int>::const_iterator it;
250     for(it = aSubShapes->constBegin(); it != aSubShapes->constEnd(); ++it ) {
251       int topAbsMode = getTopAbsMode(*it);
252       if(topAbsMode != -1 )
253         aTopAbsTypes.append(topAbsMode);
254     }
255     aFilter->addSubTypes(aTopAbsTypes);
256   }
257
258   return aFilter;
259 }
260
261 //================================================================
262 // Function : getEntry
263 // Purpose  :
264 //================================================================
265 static std::string getEntry( GEOM::GEOM_BaseObject_ptr object )
266 {
267   SUIT_Session* session = SUIT_Session::session();
268   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
269   if ( app )
270   {
271     CORBA::String_var IOR = app->orb()->object_to_string( object );
272     if ( strcmp(IOR.in(), "") != 0 )
273     {
274       SalomeApp_Study* study = ( SalomeApp_Study* )app->activeStudy();
275       _PTR(SObject) SO ( study->studyDS()->FindObjectIOR( std::string(IOR) ) );
276       if ( SO )
277         return SO->GetID();
278     }
279   }
280   return "";
281 }
282
283 //================================================================
284 // Function : getName
285 // Purpose  :
286 //================================================================
287 static std::string getName( GEOM::GEOM_BaseObject_ptr object )
288 {
289   SUIT_Session* session = SUIT_Session::session();
290   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
291   if ( app )
292   {
293     CORBA::String_var IOR = app->orb()->object_to_string( object );
294     if ( strcmp(IOR.in(), "") != 0 )
295     {
296       SalomeApp_Study* study = ( SalomeApp_Study* )app->activeStudy();
297       _PTR(SObject) aSObj ( study->studyDS()->FindObjectIOR( std::string(IOR) ) );
298
299       _PTR(GenericAttribute) anAttr;
300
301       if ( aSObj && aSObj->FindAttribute( anAttr, "AttributeName") )
302       {
303         _PTR(AttributeName) aNameAttr( anAttr );
304         return aNameAttr->Value();
305       }
306     }
307   }
308
309   return "";
310 }
311
312 //=================================================================
313 /*!
314  *  GEOM_Displayer::GEOM_Displayer
315  *  Constructor
316  */
317 //=================================================================
318 GEOM_Displayer::GEOM_Displayer( SalomeApp_Study* st )
319 {
320   if( st )
321     myApp = dynamic_cast<SalomeApp_Application*>( st->application() );
322   else
323     myApp = 0;
324
325   /* Shading Color */
326   SUIT_Session* session = SUIT_Session::session();
327   SUIT_ResourceMgr* resMgr = session->resourceMgr();
328
329   QColor col = resMgr->colorValue( "Geometry", "shading_color", QColor( 255, 0, 0 ) );
330   myShadingColor = SalomeApp_Tools::color( col );
331
332   myDisplayMode = resMgr->integerValue("Geometry", "display_mode", 0);
333   myHasDisplayMode = false;
334
335   int aType = resMgr->integerValue("Geometry", "type_of_marker", (int)Aspect_TOM_PLUS);
336   myWidth = resMgr->integerValue("Geometry", "edge_width", -1);
337   myIsosWidth = resMgr->integerValue("Geometry", "isolines_width", -1);
338
339   myTypeOfMarker = (Aspect_TypeOfMarker)(std::min((int)Aspect_TOM_RING3, std::max((int)Aspect_TOM_POINT, aType)));
340   myScaleOfMarker = (resMgr->integerValue("Geometry", "marker_scale", 1)-(int)GEOM::MS_10)*0.5 + 1.0;
341   myScaleOfMarker = std::min(7.0, std::max(1., myScaleOfMarker));
342
343   myColor = -1;
344   // This color is used for shape displaying. If it is equal -1 then
345   // default color is used.
346   myTexture = "";
347
348   myWidth = -1;
349   myType = -1;
350
351   myToActivate = true;
352   // This parameter is used for activisation/deactivisation of objects to be displayed
353
354   #if OCC_VERSION_LARGE > 0x06050100 // Functionnality available only in OCCT 6.5.2
355   // Activate parallel vizualisation only for testing purpose
356   // and if the corresponding env variable is set to 1
357   char* parallel_visu = getenv("PARALLEL_VISU");
358   if (parallel_visu && atoi(parallel_visu))
359   {
360     MESSAGE("Parallel visualisation on");
361     BRepMesh_IncrementalMesh::SetParallelDefault(Standard_True);
362   }
363   #endif
364
365   myViewFrame = 0;
366
367   myFieldDataType = GEOM::FDT_Double;
368   myFieldDimension = 0;
369   myFieldStepRangeMin = 0;
370   myFieldStepRangeMax = 0;
371 }
372
373 //=================================================================
374 /*!
375  *  GEOM_Displayer::~GEOM_Displayer
376  *  Destructor
377  */
378 //=================================================================
379 GEOM_Displayer::~GEOM_Displayer()
380 {
381 }
382
383 //=================================================================
384 /*!
385  *  GEOM_Displayer::Display
386  *  Display interactive object in the current viewer
387  */
388 //=================================================================
389 void GEOM_Displayer::Display( const Handle(SALOME_InteractiveObject)& theIO,
390                              const bool updateViewer,
391                              SALOME_View* theViewFrame )
392 {
393   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
394   if ( vf )
395   {
396     SALOME_Prs* prs = buildPresentation( theIO->getEntry(), vf );
397
398     if ( prs )
399     {
400       vf->BeforeDisplay( this, prs );
401       vf->Display( prs );
402       vf->AfterDisplay( this, prs );
403
404       if ( updateViewer )
405         vf->Repaint();
406
407       int aMgrId = getViewManagerId(vf);
408       SalomeApp_Study* aStudy = getStudy();
409       aStudy->setObjectProperty(aMgrId, theIO->getEntry(), GEOM::propertyName( GEOM::Visibility ), 1 );
410
411       setVisibilityState(theIO->getEntry(), Qtx::ShownState);
412
413       delete prs;  // delete presentation because displayer is its owner
414     }
415   }
416 }
417
418 //=================================================================
419 /*!
420  *  GEOM_Displayer::Display
421  *  This overloaded Display() method can be useful for operations
422  *  not using dialog boxes.
423  */
424 //=================================================================
425 void GEOM_Displayer::Display( GEOM::GEOM_BaseObject_ptr theObj, const bool updateViewer )
426 {
427   if ( theObj->_is_nil() )
428     return;
429
430   std::string entry = getEntry( theObj );
431   if ( entry != "" ) {
432     Display(new SALOME_InteractiveObject(entry.c_str(), "GEOM", getName(theObj).c_str()),
433             updateViewer);
434   }
435 }
436
437 //=================================================================
438 /*!
439  *  GEOM_Displayer::Erase
440  *  Erase interactive object in the current viewer
441  */
442 //=================================================================
443 void GEOM_Displayer::Erase( const Handle(SALOME_InteractiveObject)& theIO,
444                             const bool forced,
445                             const bool updateViewer,
446                             SALOME_View* theViewFrame )
447 {
448   if ( theIO.IsNull() )
449     return;
450
451   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
452
453   if ( vf ) {
454     SALOME_Prs* prs = vf->CreatePrs( theIO->getEntry() );
455     if ( prs ) {
456       vf->BeforeErase( this, prs );
457       vf->Erase( prs, forced );
458       vf->AfterErase( this, prs );
459       if ( updateViewer )
460         vf->Repaint();
461       delete prs;  // delete presentation because displayer is its owner
462
463       int aMgrId = getViewManagerId(vf);
464       SalomeApp_Study* aStudy = getStudy();
465       aStudy->setObjectProperty(aMgrId, theIO->getEntry(), GEOM::propertyName( GEOM::Visibility ), 0 );
466
467       setVisibilityState(theIO->getEntry(), Qtx::HiddenState);
468     }
469   }
470 }
471
472 //=================================================================
473 /*!
474  *  GEOM_Displayer::Erase
475  *  Erase geometry object in the current viewer
476  */
477 //=================================================================
478 void GEOM_Displayer::Erase( GEOM::GEOM_BaseObject_ptr theObj,
479                             const bool forced,
480                             const bool updateViewer )
481 {
482   std::string entry = getEntry( theObj );
483   if ( entry != "" )
484   {
485     Erase(new SALOME_InteractiveObject(entry.c_str(), "GEOM", getName(theObj).c_str()),
486           forced, updateViewer);
487   }
488 }
489
490 //=================================================================
491 /*!
492  *  GEOM_Displayer::Redisplay
493  *  Redisplay (erase and then display again) interactive object
494  *  in the current viewer
495  */
496 //=================================================================
497 void GEOM_Displayer::Redisplay( const Handle(SALOME_InteractiveObject)& theIO,
498                                 const bool updateViewer,
499                                 const bool checkActiveViewer )
500 {
501   // Remove the object permanently (<forced> == true)
502   SUIT_Session* ses = SUIT_Session::session();
503   SUIT_Application* app = ses->activeApplication();
504   if ( app )
505   {
506     SUIT_Desktop* desk = app->desktop();
507     QList<SUIT_ViewWindow*> wnds = desk->windows();
508     SUIT_ViewWindow* wnd;
509     QListIterator<SUIT_ViewWindow*> it( wnds );
510     while ( it.hasNext() && (wnd = it.next()) )
511     {
512       SUIT_ViewManager* vman = wnd->getViewManager();
513       if ( vman )
514       {
515         SUIT_ViewModel* vmodel = vman->getViewModel();
516         if ( vmodel )
517         {
518           SALOME_View* view = dynamic_cast<SALOME_View*>(vmodel);
519           if ( view )
520           {
521             if ( view->isVisible( theIO ) || ( checkActiveViewer && view == GetActiveView() ) )
522             {
523               Erase( theIO, true, false, view );
524               Display( theIO, updateViewer, view );
525             }
526           }
527         }
528       }
529     }
530   }
531 }
532
533 //=================================================================
534 /*!
535  *  GEOM_Displayer::Display
536  *  Calls Display() method for each object in the given list
537  */
538 //=================================================================
539 void GEOM_Displayer::Display( const SALOME_ListIO& theIOList, const bool updateViewer )
540 {
541   SALOME_ListIteratorOfListIO Iter( theIOList );
542   for ( ; Iter.More(); Iter.Next() ) {
543     Display( Iter.Value(), false );
544   }
545   if ( updateViewer )
546     UpdateViewer();
547 }
548
549 Quantity_Color GEOM_Displayer::qColorFromResources( const QString& property, const QColor& defColor )
550 {
551   // VSR: this method can be improved in future:
552   // to improve performance, the default values from resource manager should be cached in the displayer
553   return SalomeApp_Tools::color( SUIT_Session::session()->resourceMgr()->colorValue( "Geometry", property, defColor ) );
554 }
555
556 QColor GEOM_Displayer::colorFromResources( const QString& property, const QColor& defColor )
557 {
558   // VSR: this method can be improved in future:
559   // to improve performance, the default values from resource manager should be cached in the displayer
560   return SUIT_Session::session()->resourceMgr()->colorValue( "Geometry", property, defColor );
561 }
562
563 void GEOM_Displayer::updateShapeProperties( const Handle(GEOM_AISShape)& AISShape, bool create )
564 {
565   // check that shape is not null
566   if ( AISShape.IsNull() ) return;
567   
568   // check that study is active
569   SalomeApp_Study* study = getStudy();
570   if ( !study ) return;
571
572   if ( myShape.ShapeType() != TopAbs_VERTEX && // fix pb with not displayed points
573        !TopoDS_Iterator(myShape).More() )
574     return; // NPAL15983 (Bug when displaying empty groups)
575
576   // set interactive object
577
578   Handle( SALOME_InteractiveObject ) anIO;
579
580   if ( !myIO.IsNull() ) {
581     AISShape->setIO( myIO );
582     AISShape->SetOwner( myIO );
583     anIO = myIO;
584   }
585   else if ( !myName.empty() ) {
586     // workaround to allow selection of temporary objects
587     static int tempId = 0;
588     anIO = new SALOME_InteractiveObject( QString( "TEMP_%1" ).arg( tempId++ ).toLatin1().data(), "GEOM", myName.c_str() );
589     AISShape->setIO( anIO );
590     AISShape->SetOwner( anIO );
591   }
592
593   // flag:  only vertex or compound of vertices is processed (specific handling)
594   bool onlyVertex = myShape.ShapeType() == TopAbs_VERTEX || isCompoundOfVertices( myShape );
595   // presentation study entry (empty for temporary objects like preview)
596   QString entry = !anIO.IsNull() ? QString( anIO->getEntry() ) : QString();
597   // flag: temporary object
598   bool isTemporary = entry.isEmpty() || entry.startsWith( "TEMP_" );
599   // currently active view window's ID (-1 if no active view)
600   int aMgrId = !anIO.IsNull() ? getViewManagerId( myViewFrame ) : -1;
601
602   // get presentation properties
603   PropMap propMap = getObjectProperties( study, entry, myViewFrame );
604
605   // Temporary staff: vertex must be infinite for correct visualization
606   AISShape->SetInfiniteState( myShape.Infinite() ); // || myShape.ShapeType() == TopAbs_VERTEX // VSR: 05/04/2010: Fix 20668 (Fit All for points & lines)
607
608   // set material
609   Material_Model material;
610   // if predefined color isn't set in displayer(via GEOM_Displayer::SetColor() function)
611   if( !HasColor() )
612     material.fromProperties( propMap.value( GEOM::propertyName( GEOM::Material ) ).toString() );
613   // - set front material properties
614   AISShape->SetCurrentFacingModel( Aspect_TOFM_FRONT_SIDE );
615   AISShape->SetMaterial( material.getMaterialOCCAspect( true ) );
616   // - set back material properties
617   AISShape->SetCurrentFacingModel( Aspect_TOFM_BACK_SIDE );
618   AISShape->SetMaterial( material.getMaterialOCCAspect( false ) );
619   // - switch to default (both sides) facing mode
620   AISShape->SetCurrentFacingModel( Aspect_TOFM_BOTH_SIDE );
621
622   // set colors
623
624   // - shading color
625   if ( HasColor()  ) {
626     // predefined color, manually set to displayer via GEOM_Displayer::SetColor() function;
627     // we set it to the shape not taking into account material properties
628     AISShape->SetShadingColor( (Quantity_NameOfColor)GetColor() );
629   }
630   else if ( !material.isPhysical() ) {
631     // shading color from properties is used only for non-physical materials
632     AISShape->SetShadingColor( SalomeApp_Tools::color( propMap.value( GEOM::propertyName( GEOM::ShadingColor ) ).value<QColor>() ) );
633   }
634
635   // - wireframe color
636   Handle(Prs3d_LineAspect) anAspect = AISShape->Attributes()->LineAspect();
637   anAspect->SetColor( HasColor() ? (Quantity_NameOfColor)GetColor() : 
638                       SalomeApp_Tools::color( propMap.value( GEOM::propertyName( GEOM::WireframeColor ) ).value<QColor>() ) );
639   AISShape->Attributes()->SetLineAspect( anAspect );
640   
641   // - unfree boundaries color
642   anAspect = AISShape->Attributes()->UnFreeBoundaryAspect();
643   anAspect->SetColor( HasColor() ? (Quantity_NameOfColor)GetColor() : 
644                       SalomeApp_Tools::color( propMap.value( GEOM::propertyName( GEOM::WireframeColor ) ).value<QColor>() ) );
645   AISShape->Attributes()->SetUnFreeBoundaryAspect( anAspect );
646   
647   // - free boundaries color
648   anAspect = AISShape->Attributes()->FreeBoundaryAspect();
649   anAspect->SetColor( HasColor() ? (Quantity_NameOfColor)GetColor() : 
650                       SalomeApp_Tools::color( propMap.value( GEOM::propertyName( GEOM::FreeBndColor ) ).value<QColor>() ) );
651   AISShape->Attributes()->SetFreeBoundaryAspect( anAspect );
652   
653   // - standalone edges color
654   anAspect = AISShape->Attributes()->WireAspect();
655   anAspect->SetColor( HasColor() ? (Quantity_NameOfColor)GetColor() : 
656                       SalomeApp_Tools::color( propMap.value( GEOM::propertyName( GEOM::LineColor ) ).value<QColor>() ) );
657   AISShape->Attributes()->SetWireAspect( anAspect );
658   
659   // - color for edges in shading+edges mode
660   AISShape->SetEdgesInShadingColor( SalomeApp_Tools::color( propMap.value( GEOM::propertyName( GEOM::OutlineColor ) ).value<QColor>() ) );
661   
662   // ???
663   AISShape->storeBoundaryColors();
664
665   // set display mode
666   AISShape->SetDisplayMode( HasDisplayMode() ? 
667                             // predefined display mode, manually set to displayer via GEOM_Displayer::SetDisplayMode() function 
668                             GetDisplayMode() :
669                             // display mode from properties
670                             propMap.value( GEOM::propertyName( GEOM::DisplayMode ) ).toInt() );
671
672   // set display vectors flag
673   AISShape->SetDisplayVectors( propMap.value( GEOM::propertyName( GEOM::EdgesDirection ) ).toBool() );
674
675   // set transparency
676   // VSR: ??? next line is commented: transparency property is set in the AfterDisplay() function
677   //AISShape->SetTransparency( propMap.value( GEOM::propertyName( GEOM::Transparency ) ).toDouble() );
678
679   // set iso properties
680   int uIsos = propMap.value( GEOM::propertyName( GEOM::NbIsos ) ).toString().split( GEOM::subSectionSeparator() )[0].toInt();
681   int vIsos = propMap.value( GEOM::propertyName( GEOM::NbIsos ) ).toString().split( GEOM::subSectionSeparator() )[1].toInt();
682   Quantity_Color isosColor = SalomeApp_Tools::color( propMap.value( GEOM::propertyName( GEOM::IsosColor ) ).value<QColor>() );
683   int isosWidth = propMap.value( GEOM::propertyName( GEOM::IsosWidth ) ).toInt();
684   Handle(Prs3d_IsoAspect) uIsoAspect = AISShape->Attributes()->UIsoAspect();
685   Handle(Prs3d_IsoAspect) vIsoAspect = AISShape->Attributes()->VIsoAspect();
686   uIsoAspect->SetColor( isosColor );
687   uIsoAspect->SetWidth( isosWidth );
688   uIsoAspect->SetNumber( uIsos );
689   vIsoAspect->SetColor( isosColor );
690   vIsoAspect->SetWidth( isosWidth );
691   vIsoAspect->SetNumber( vIsos );
692   AISShape->Attributes()->SetUIsoAspect( uIsoAspect );
693   AISShape->Attributes()->SetVIsoAspect( vIsoAspect );
694
695   // set deflection coefficient
696   // ... to avoid to small values of the coefficient, its lower value is limited 
697   AISShape->SetOwnDeviationCoefficient( qMax( propMap.value( GEOM::propertyName( GEOM::Deflection ) ).toDouble(), GEOM::minDeflection() ) );
698
699   // set texture
700   if ( HasTexture() ) {
701     // predefined display texture, manually set to displayer via GEOM_Displayer::SetTexture() function 
702     AISShape->SetTextureFileName( TCollection_AsciiString( GetTexture().c_str() ) );
703     AISShape->SetTextureMapOn();
704     AISShape->DisableTextureModulate();
705     AISShape->SetDisplayMode( 3 );
706   }
707
708   // set line width
709   AISShape->SetWidth( HasWidth() ?
710                       // predefined line width, manually set to displayer via GEOM_Displayer::SetLineWidth() function 
711                       GetWidth() :
712                       // libe width from properties
713                       propMap.value( GEOM::propertyName( GEOM::LineWidth ) ).toInt() );
714
715   // set top-level flag
716   AISShape->setTopLevel( propMap.value( GEOM::propertyName( GEOM::TopLevel ) ).toBool() );
717
718   // set point marker (for vertex / compound of vertices only)
719   if ( onlyVertex ) {
720     QStringList aList = propMap.value( GEOM::propertyName( GEOM::PointMarker ) ).toString().split( GEOM::subSectionSeparator() );
721     if ( aList.size() == 2 ) {
722       // standard marker string contains "TypeOfMarker:ScaleOfMarker"
723       int aTypeOfMarker = aList[0].toInt();
724       double aScaleOfMarker = (aList[1].toInt() + 1) * 0.5;
725       Handle(Prs3d_PointAspect) anAspect = AISShape->Attributes()->PointAspect();
726       anAspect->SetScale( aScaleOfMarker );
727       anAspect->SetTypeOfMarker( (Aspect_TypeOfMarker)( aTypeOfMarker-1 ) );
728       anAspect->SetColor( HasColor() ? 
729                           // predefined color, manually set to displayer via GEOM_Displayer::SetColor() function
730                           (Quantity_NameOfColor)GetColor() : 
731                           // color from properties
732                           SalomeApp_Tools::color( propMap.value( GEOM::propertyName( GEOM::PointColor ) ).value<QColor>() ) );
733       AISShape->Attributes()->SetPointAspect( anAspect );
734     }
735     else if ( aList.size() == 1 ) {
736       // custom marker string contains "IdOfTexture"
737       int textureId = aList[0].toInt();
738       Standard_Integer aWidth, aHeight;
739 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
740       Handle(TColStd_HArray1OfByte) aTexture =
741 #else
742         Handle(Graphic3d_HArray1OfBytes) aTexture =
743 #endif
744         GeometryGUI::getTexture( study, textureId, aWidth, aHeight );
745       if ( !aTexture.IsNull() ) {
746 #if OCC_VERSION_LARGE > 0x06060000 // Porting to OCCT higher 6.6.0 version
747         Handle(Prs3d_PointAspect) aTextureAspect =
748           new Prs3d_PointAspect( HasColor() ? 
749                                  // predefined color, manually set to displayer via GEOM_Displayer::SetColor() function
750                                  (Quantity_NameOfColor)GetColor() : 
751                                  // color from properties 
752                                  SalomeApp_Tools::color( propMap.value( GEOM::propertyName( GEOM::PointColor ) ).value<QColor>() ),
753                                  aWidth, aHeight,
754                                  aTexture );
755 #else
756         int TextureId = 0;
757         Handle(Prs3d_PointAspect) aTextureAspect =
758           new Prs3d_PointAspect( HasColor() ? 
759                                  // predefined color, manually set to displayer via GEOM_Displayer::SetColor() function
760                                  (Quantity_NameOfColor)GetColor() : 
761                                  // color from properties 
762                                  SalomeApp_Tools::color( propMap.value( GEOM::propertyName( GEOM::PointColor ) ).value<QColor>() ), 
763                                  ++TextureId,
764                                  aWidth, aHeight,
765                                  aTexture );
766 #endif
767         AISShape->Attributes()->SetPointAspect( aTextureAspect );
768       }
769     }
770   }
771
772   // set field step data
773   AISShape->setFieldStepInfo( myFieldDataType,
774                               myFieldDimension,
775                               myFieldStepData,
776                               myFieldStepName,
777                               myFieldStepRangeMin,
778                               myFieldStepRangeMax );
779
780   if ( create && !isTemporary && aMgrId != -1 ) {
781     // set properties to the study
782     study->setObjectPropMap( aMgrId, entry, propMap );
783   }
784
785   // AISShape->SetName(???); ??? necessary to set name ???
786 }
787
788 void GEOM_Displayer::updateActorProperties( GEOM_Actor* actor, bool create )
789 {
790   // check that actor is not null
791   if ( !actor ) return;
792   
793   // check that study is active
794   SalomeApp_Study* study = getStudy();
795   if ( !study ) return;
796
797   // set interactive object
798
799   Handle( SALOME_InteractiveObject ) anIO;
800
801   if ( !myIO.IsNull() ) {
802     actor->setIO( myIO );
803     anIO = myIO;
804   }
805   else if ( !myName.empty() ) {
806     // workaround to allow selection of temporary objects
807     static int tempId = 0;
808     anIO = new SALOME_InteractiveObject( QString( "TEMP_VTK_%1" ).arg( tempId++ ).toLatin1().data(), "GEOM", myName.c_str() );
809     actor->setIO( anIO );
810   }
811
812   // presentation study entry (empty for temporary objects like preview)
813   QString entry = !anIO.IsNull() ? QString( anIO->getEntry() ) : QString();
814   // flag: temporary object
815   bool isTemporary = entry.isEmpty() || entry.startsWith( "TEMP_" );
816   // currently active view window's ID (-1 if no active view)
817   int aMgrId = !anIO.IsNull() ? getViewManagerId( myViewFrame ) : -1;
818
819   // get presentation properties
820   PropMap propMap = getObjectProperties( study, entry, myViewFrame );
821   QColor c;
822
823   /////////////////////////////////////////////////////////////////////////
824   // VSR: for VTK viewer currently deflection coefficient is hardcoded
825   //      due to performance problem
826   // actor->SetShape(myShape,aDefPropMap.value(GEOM::propertyName( GEOM::Deflection )).toDouble(),myType == GEOM_VECTOR);
827   /////////////////////////////////////////////////////////////////////////
828   if ( !actor->getTopo().IsSame( myShape ) )
829     actor->SetShape( myShape, VTK_MIN_DEFLECTION, myType == GEOM_VECTOR );
830
831   // set material
832   Material_Model material;
833   material.fromProperties( propMap.value( GEOM::propertyName( GEOM::Material ) ).toString() );
834   std::vector<vtkProperty*> mprops;
835   mprops.push_back( material.getMaterialVTKProperty( true ) );
836   mprops.push_back( material.getMaterialVTKProperty( false) );
837   actor->SetMaterial( mprops );
838
839   // set iso-lines properties
840
841   // - set number of iso-lines
842   int nbIsos[2]= { 1, 1 };
843   QStringList isos = propMap.value( GEOM::propertyName( GEOM::NbIsos ) ).toString().split( GEOM::subSectionSeparator() );
844   nbIsos[0] = isos[0].toInt();
845   nbIsos[1] = isos[1].toInt();
846   actor->SetNbIsos( nbIsos );
847
848   // - set iso-lines width
849   actor->SetIsosWidth( propMap.value( GEOM::propertyName( GEOM::IsosWidth ) ).toInt() );
850
851   // - set iso-lines color
852   c = propMap.value( GEOM::propertyName( GEOM::IsosColor ) ).value<QColor>();
853   actor->SetIsosColor( c.redF(), c.greenF(), c.blueF() );
854
855   // set colors
856
857   if ( HasColor()  ) {
858     // - same color for all sub-actors
859     Quantity_Color aColor( (Quantity_NameOfColor)GetColor() );
860     actor->SetColor( aColor.Red(), aColor.Green(), aColor.Blue() );
861   }
862   else {
863     // shading color (for non-physical materials)
864     if ( !material.isPhysical() ) {
865       c = propMap.value( GEOM::propertyName( GEOM::ShadingColor ) ).value<QColor>();
866       actor->GetFrontMaterial()->SetColor( c.redF(), c.greenF(), c.blueF() );
867       actor->GetBackMaterial()->SetColor( c.redF(), c.greenF(), c.blueF() );
868     }
869
870     // - standalone edge color
871     c = propMap.value( GEOM::propertyName( GEOM::WireframeColor ) ).value<QColor>();
872     actor->SetIsolatedEdgeColor( c.redF(), c.greenF(), c.blueF() );
873
874     c = propMap.value( GEOM::propertyName( GEOM::WireframeColor ) ).value<QColor>();
875     // - shared edges color ???
876     actor->SetSharedEdgeColor( c.redF(), c.greenF(), c.blueF() );
877
878     c = propMap.value( GEOM::propertyName( GEOM::FreeBndColor ) ).value<QColor>();
879     // - free edges color ???
880     actor->SetFreeEdgeColor( c.redF(), c.greenF(), c.blueF() );
881
882     // - point color
883     c = propMap.value( GEOM::propertyName( GEOM::PointColor ) ).value<QColor>();
884     actor->SetPointColor( c.redF(), c.greenF(), c.blueF() );
885   }
886
887   // - color for edges in shading+edges mode
888   c = propMap.value( GEOM::propertyName( GEOM::OutlineColor ) ).value<QColor>();
889   actor->SetEdgesInShadingColor( c.redF(), c.greenF(), c.blueF() );
890
891   // set opacity
892   actor->SetOpacity( 1.0 - propMap.value( GEOM::propertyName( GEOM::Transparency ) ).toDouble() );
893
894   // set line width
895   actor->SetWidth( HasWidth() ?
896                    // predefined line width, manually set to displayer via GEOM_Displayer::SetLineWidth() function 
897                    GetWidth() :
898                    // libe width from properties
899                    propMap.value( GEOM::propertyName( GEOM::LineWidth ) ).toInt() );
900   
901   // set display vectors flag
902   actor->SetVectorMode( propMap.value( GEOM::propertyName( GEOM::EdgesDirection ) ).toBool() );
903
904   // set display mode
905   int displayMode = HasDisplayMode() ? 
906     // predefined display mode, manually set to displayer via GEOM_Displayer::SetDisplayMode() function 
907     GetDisplayMode() :
908     // display mode from properties
909     propMap.value( GEOM::propertyName( GEOM::DisplayMode ) ).toInt();
910
911   // specific processing of 'shading with edges' mode, as VTK provides only the following standard display modes:
912   // Points - 0, Wireframe - 1, Surface - 2, Insideframe - 3, SurfaceWithEdges - 4
913   // GEOM actor allows alternative display modes (see VTKViewer::Representation enum) and enum in GEOM_Actor:
914   // eWireframe - 0, eShading - 1, eShadingWithEdges - 3
915
916   if ( displayMode == 2 )
917       // this is 'Shading with edges' mode => we have to do the correct mapping to EDisplayMode
918       // enum in GEOM_Actor (and further to VTKViewer::Representation enum)
919     displayMode++;
920   actor->setDisplayMode( displayMode );
921
922   if ( myToActivate )
923     actor->PickableOn();
924   else
925     actor->PickableOff();
926
927   if ( create && !isTemporary && aMgrId != -1 ) {
928     // set properties to the study
929     study->setObjectPropMap( aMgrId, entry, propMap );
930   }
931 }
932
933 //=================================================================
934 /*!
935  *  GEOM_Displayer::Erase
936  *  Calls Erase() method for each object in the given list
937  */
938 //=================================================================
939 void GEOM_Displayer::Erase( const SALOME_ListIO& theIOList,
940                             const bool forced,
941                             const bool updateViewer )
942 {
943   SALOME_ListIteratorOfListIO Iter( theIOList );
944   for ( ; Iter.More(); Iter.Next() )
945     Erase( Iter.Value(), forced, false );
946
947   if ( updateViewer )
948     UpdateViewer();
949 }
950
951 //=================================================================
952 /*!
953  *  GEOM_Displayer::Redisplay
954  *  Calls Redisplay() method for each object in the given list
955  */
956 //=================================================================
957 void GEOM_Displayer::Redisplay( const SALOME_ListIO& theIOList,
958                                 const bool updateViewer,
959                                 const bool checkActiveViewer )
960 {
961   SALOME_ListIteratorOfListIO Iter( theIOList );
962   for ( ; Iter.More(); Iter.Next() )
963     Redisplay( Iter.Value(), false, checkActiveViewer );
964
965   if ( updateViewer )
966     UpdateViewer();
967 }
968
969 //=================================================================
970 /*!
971  *  GEOM_Displayer::Update
972  *  Update OCC presentaion
973  *  [ Reimplemented from SALOME_Displayer ]
974  */
975 //=================================================================
976 void GEOM_Displayer::Update( SALOME_OCCPrs* prs )
977 {
978   SOCC_Prs* occPrs = dynamic_cast<SOCC_Prs*>( prs );
979   SalomeApp_Study* study = getStudy();
980
981   if ( !occPrs || myShape.IsNull() || !study )
982     return;
983
984   if ( myType == GEOM_MARKER && myShape.ShapeType() == TopAbs_FACE )
985   {
986     // 
987     // specific processing for local coordinate system presentation
988     // 
989
990     TopoDS_Face aFace = TopoDS::Face( myShape );
991     Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast( BRep_Tool::Surface( aFace ) );
992     if ( !aPlane.IsNull() )
993     {
994       gp_Ax3 aPos = aPlane->Pln().Position();
995       Handle(Geom_Axis2Placement) aPlc = new Geom_Axis2Placement( aPos.Ax2() );
996
997       Handle(GEOM_AISTrihedron) aTrh;
998
999       if ( occPrs->IsNull() )
1000       {
1001         // new presentation is being created
1002         aTrh = new GEOM_AISTrihedron( aPlc );
1003         occPrs->AddObject( aTrh );
1004       }
1005       else
1006       {
1007         // presentation is being updated
1008         AIS_ListOfInteractive aList;
1009         occPrs->GetObjects( aList );
1010         AIS_ListIteratorOfListOfInteractive anIter( aList );
1011         for ( ; anIter.More() && aTrh.IsNull(); anIter.Next() ) {
1012           aTrh = Handle(GEOM_AISTrihedron)::DownCast( anIter.Value() );
1013         }
1014       }
1015         
1016       if ( !aTrh.IsNull() ) {
1017         // predefined color, manually set to displayer via GEOM_Displayer::SetColor() function
1018         if ( HasColor() )
1019           aTrh->SetColor( (Quantity_NameOfColor)GetColor() );
1020         // predefined line width, manually set to displayer via GEOM_Displayer::SetLineWidth() function 
1021         if ( HasWidth() )
1022           aTrh->SetWidth( GetWidth() );
1023         
1024         if ( !myIO.IsNull() )
1025         {
1026           aTrh->setIO( myIO );
1027           aTrh->SetOwner( myIO );
1028         }
1029         aTrh->SetComponent( aPlc );
1030         aTrh->SetToUpdate();
1031       }
1032       occPrs->SetToActivate( ToActivate() );
1033     }
1034   }
1035   else
1036   {
1037     // 
1038     // processing for usual geometry presentation
1039     // 
1040
1041     // if presentation is empty we try to create new one
1042     if ( occPrs->IsNull() )
1043     {
1044       // create presentation (specific for vectors)
1045       Handle(GEOM_AISShape) AISShape = ( myType == GEOM_VECTOR ) ? new GEOM_AISVector( myShape, "" )
1046                                                                  : new GEOM_AISShape ( myShape, "" );
1047       // update shape properties
1048       updateShapeProperties( AISShape, true );
1049
1050       // add shape to the presentation
1051       occPrs->AddObject( AISShape );
1052
1053       // In accordance with ToActivate() value object will be activated/deactivated
1054       // when it will be displayed
1055       occPrs->SetToActivate( ToActivate() );
1056
1057       if ( AISShape->isTopLevel() && GEOM_AISShape::topLevelDisplayMode() == GEOM_AISShape::TopShowAdditionalWActor ) {
1058         // 21671: EDF 1829 GEOM : Bring to front selected objects (continuation):
1059
1060         // create additional wireframe shape
1061         Handle(GEOM_TopWireframeShape) aWirePrs = new GEOM_TopWireframeShape(myShape);
1062         aWirePrs->SetWidth(AISShape->Width());
1063         if ( !myIO.IsNull() ) {
1064           aWirePrs->setIO( myIO );
1065           aWirePrs->SetOwner( myIO );
1066         }
1067
1068         // add shape to the presentation
1069         occPrs->AddObject( aWirePrs );
1070       }
1071     }
1072     // if presentation is found -> set again shape for it
1073     else
1074     {
1075       AIS_ListOfInteractive IOList;
1076       occPrs->GetObjects( IOList );
1077       AIS_ListIteratorOfListOfInteractive Iter( IOList );
1078       for ( ; Iter.More(); Iter.Next() )
1079       {
1080         Handle(GEOM_AISShape) AISShape = Handle(GEOM_AISShape)::DownCast( Iter.Value() );
1081         if ( AISShape.IsNull() )
1082           continue;
1083
1084         // re-set shape (it might be changed)
1085         if ( AISShape->Shape() != myShape )
1086           AISShape->Set( myShape );
1087
1088         // update shape properties
1089         updateShapeProperties( AISShape, false );
1090
1091         // force updating
1092         AISShape->UpdateSelection();
1093         AISShape->SetToUpdate();
1094       }
1095     }
1096   }
1097 }
1098
1099 //=================================================================
1100 /*!
1101  *  GEOM_Displayer::Update
1102  *  Update VTK presentaion
1103  *  [ Reimplemented from SALOME_Displayer ]
1104  */
1105 //=================================================================
1106 void GEOM_Displayer::Update( SALOME_VTKPrs* prs )
1107 {
1108   SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
1109   SalomeApp_Study* study = getStudy();
1110
1111   if ( !vtkPrs || myShape.IsNull() || !study )
1112     return;
1113
1114   if ( myType == GEOM_MARKER && myShape.ShapeType() == TopAbs_FACE )
1115   {
1116     // 
1117     // specific processing for local coordinate system presentation
1118     // 
1119
1120     TopoDS_Face aFace = TopoDS::Face( myShape );
1121     Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast( BRep_Tool::Surface( aFace ) );
1122     if ( !aPlane.IsNull() ) {
1123       gp_Ax3 aPos = aPlane->Pln().Position();
1124       Handle(Geom_Axis2Placement) aPlc = new Geom_Axis2Placement( aPos.Ax2() );
1125
1126       GEOM_VTKTrihedron* aTrh = 0;
1127
1128       if ( vtkPrs->IsNull() ) {
1129         // new presentation is being created
1130         aTrh = GEOM_VTKTrihedron::New();
1131         vtkPrs->AddObject( aTrh );
1132       }
1133       else {
1134         // presentation is being updated
1135         vtkActorCollection* actors = vtkPrs->GetObjects();
1136         if ( actors ) {
1137           actors->InitTraversal();
1138           vtkActor* a = actors->GetNextActor();
1139           while ( a && !aTrh ) {
1140             aTrh = GEOM_VTKTrihedron::SafeDownCast( a );
1141             a = actors->GetNextActor();
1142           }
1143         }
1144       }
1145       
1146       if ( aTrh ) {
1147         // predefined color, manually set to displayer via GEOM_Displayer::SetColor() function
1148         if ( HasColor() ) {
1149           Quantity_Color aColor( (Quantity_NameOfColor)GetColor() );
1150           aTrh->SetColor( aColor.Red(), aColor.Green(), aColor.Blue() );
1151         }
1152 #ifdef VTK_TRIHEDRON_WIDTH
1153         // 
1154         // VSR: currently isn't supported
1155         //
1156         // predefined line width, manually set to displayer via GEOM_Displayer::SetLineWidth() function 
1157         if ( HasWidth() )
1158           aTrh->SetWidth( GetWidth() );
1159 #endif
1160
1161         if ( !myIO.IsNull() )
1162           aTrh->setIO( myIO );
1163
1164         aTrh->SetPlacement( aPlc );
1165       }
1166     }
1167   }
1168   else
1169   {
1170     // 
1171     // processing for usual geometry presentation
1172     // 
1173
1174     // if presentation is empty we try to create new one
1175     if ( vtkPrs->IsNull() )
1176     {
1177       // create an actor
1178       GEOM_Actor* actor = GEOM_Actor::New();
1179       // update actor properties
1180       updateActorProperties( actor, true );
1181       // add actor to the presentation
1182       vtkPrs->AddObject( actor );
1183     }
1184     else {
1185       // presentation is being updated
1186       vtkActorCollection* actors = vtkPrs->GetObjects();
1187       if ( actors ) {
1188         actors->InitTraversal();
1189         vtkActor* a = actors->GetNextActor();
1190         while ( a ) {
1191           GEOM_Actor* actor = GEOM_Actor::SafeDownCast( a );
1192           if ( actor ) {
1193             // update actor properties
1194             updateActorProperties( actor, false );
1195             a = actors->GetNextActor();
1196           }
1197         }
1198       }
1199     }
1200   }
1201 }
1202
1203 //=================================================================
1204 /*!
1205  *  GEOM_Displayer::BuildPrs
1206  *  Build presentation accordint to the current viewer type
1207  */
1208 //=================================================================
1209 SALOME_Prs* GEOM_Displayer::BuildPrs( GEOM::GEOM_Object_ptr theObj )
1210 {
1211   if ( theObj->_is_nil() )
1212     return 0;
1213
1214   myViewFrame = GetActiveView();
1215   if ( myViewFrame == 0 )
1216     return 0;
1217
1218   SALOME_Prs* aPrs = myViewFrame->CreatePrs();
1219   if ( aPrs == 0 )
1220     return 0;
1221
1222   internalReset();
1223   setShape( GEOM_Client::get_client().GetShape( GeometryGUI::GetGeomGen(), theObj ) );
1224   myType = theObj->GetType();
1225
1226   // Update presentation
1227   UpdatePrs( aPrs );
1228
1229   return aPrs;
1230 }
1231
1232 //=================================================================
1233 /*!
1234  *  GEOM_Displayer::BuildPrs
1235  *  Build presentation accordint to the current viewer type
1236  */
1237 //=================================================================
1238 SALOME_Prs* GEOM_Displayer::BuildPrs( const TopoDS_Shape& theShape )
1239 {
1240   myViewFrame = GetActiveView();
1241   if ( theShape.IsNull() || myViewFrame == 0 )
1242     return 0;
1243
1244   SALOME_Prs* aPrs = myViewFrame->CreatePrs();
1245   if ( aPrs == 0 )
1246     return 0;
1247
1248   internalReset();
1249   setShape( theShape );
1250   myType = -1;
1251
1252   UpdatePrs( aPrs );
1253
1254   return aPrs;
1255 }
1256
1257 //=================================================================
1258 /*!
1259  *  GEOM_Displayer::buildPresentation
1260  *  Builds/finds object's presentation for the current viewer
1261  *  Calls corresponding Update() method by means of double dispatch
1262  *  [ internal ]
1263  */
1264 //=================================================================
1265 SALOME_Prs* GEOM_Displayer::buildPresentation( const QString& entry,
1266                                                SALOME_View* theViewFrame )
1267 {
1268   SALOME_Prs* prs = 0;
1269   internalReset();
1270
1271   myViewFrame = theViewFrame ? theViewFrame : GetActiveView();
1272
1273   if ( myViewFrame )
1274   {
1275     prs = LightApp_Displayer::buildPresentation( entry, theViewFrame );
1276     if ( prs )
1277     {
1278       Handle( SALOME_InteractiveObject ) theIO = new SALOME_InteractiveObject();
1279       theIO->setEntry( entry.toLatin1().constData() );
1280       if ( !theIO.IsNull() )
1281       {
1282         // set interactive object
1283         setIO( theIO );
1284         //  Find SOBject (because shape should be published previously)
1285         SUIT_Session* session = SUIT_Session::session();
1286         SUIT_Application* app = session->activeApplication();
1287         if ( app )
1288         {
1289           SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
1290           if ( study )
1291           {
1292             _PTR(SObject) SO ( study->studyDS()->FindObjectID( theIO->getEntry() ) );
1293             if ( SO )
1294             {
1295               // get CORBA reference to data object
1296               CORBA::Object_var object = GeometryGUI::ClientSObjectToObject(SO);
1297               if ( !CORBA::is_nil( object ) )
1298               {
1299                 // downcast to GEOM base object
1300                 GEOM::GEOM_BaseObject_var GeomBaseObject = GEOM::GEOM_BaseObject::_narrow( object );
1301                 if ( !GeomBaseObject->_is_nil() )
1302                 {
1303                   myType = GeomBaseObject->GetType();
1304
1305                   // downcast to GEOM object
1306                   GEOM::GEOM_Object_var GeomObject = GEOM::GEOM_Object::_narrow( GeomBaseObject );
1307                   if ( myType == GEOM_FIELD_STEP )
1308                   {
1309                     // get the GEOM object from the field's shape
1310                     GEOM::GEOM_FieldStep_var GeomFieldStep = GEOM::GEOM_FieldStep::_narrow( GeomBaseObject );
1311                     if ( !GeomFieldStep->_is_nil() )
1312                     {
1313                       GEOM::GEOM_Field_var GeomField = GeomFieldStep->GetField();
1314                       if ( !GeomField->_is_nil() )
1315                         GeomObject = GeomField->GetShape();
1316                     }
1317
1318                     // read the field step information
1319                     readFieldStepInfo( GeomFieldStep );
1320                   }
1321
1322                   if ( !GeomObject->_is_nil() )
1323                   {
1324                     // finally set shape
1325                     setShape( GEOM_Client::get_client().GetShape( GeometryGUI::GetGeomGen(), GeomObject ) );
1326                   }
1327                 }
1328               }
1329             }
1330           }
1331         }
1332       }
1333       UpdatePrs( prs );  // Update presentation by using of the double dispatch
1334     }
1335   }
1336   return prs;
1337 }
1338
1339 //=================================================================
1340 /*!
1341  *  GEOM_Displayer::buildSubshapePresentation
1342  *  Builds/finds object's presentation for the current viewer
1343  *  Calls corresponding Update() method by means of double dispatch
1344  *  For not published objects (for Mantis issue 0020435)
1345  */
1346 //=================================================================
1347 SALOME_Prs* GEOM_Displayer::buildSubshapePresentation(const TopoDS_Shape& aShape,
1348                                                       const QString& entry,
1349                                                       SALOME_View* theViewFrame)
1350 {
1351   SALOME_Prs* prs = 0;
1352   internalReset();
1353
1354   myViewFrame = theViewFrame ? theViewFrame : GetActiveView();
1355
1356   if (myViewFrame)
1357   {
1358     prs = LightApp_Displayer::buildPresentation(entry, theViewFrame);
1359     if (prs)
1360     {
1361       Handle(SALOME_InteractiveObject) theIO = new SALOME_InteractiveObject();
1362       theIO->setEntry(entry.toLatin1().constData());
1363       if (!theIO.IsNull())
1364       {
1365         // set interactive object
1366         setIO(theIO);
1367         // finally set shape
1368         setShape(aShape);
1369         myType = GEOM_SUBSHAPE;
1370       }
1371       UpdatePrs(prs);  // Update presentation by using of the double dispatch
1372     }
1373   }
1374   return prs;
1375 }
1376
1377 //=================================================================
1378 /*!
1379  *  GEOM_Displayer::internalReset
1380  *  Resets internal data
1381  *  [internal]
1382  */
1383 //=================================================================
1384 void GEOM_Displayer::internalReset()
1385 {
1386   myIO.Nullify();
1387   myShape.Nullify();
1388
1389   myFieldDataType = GEOM::FDT_Double;
1390   myFieldDimension = 0;
1391   myFieldStepData.clear();
1392   myFieldStepName.Clear();
1393   myFieldStepRangeMin = 0;
1394   myFieldStepRangeMax = 0;
1395 }
1396
1397 //=================================================================
1398 /*!
1399  *  GEOM_Displayer::LocalSelection
1400  *  Activate selection of CAD shapes with activisation of selection
1401  *  of their sub-shapes (with opened local context for OCC viewer)
1402  */
1403 //=================================================================
1404 void GEOM_Displayer::LocalSelection( const Handle(SALOME_InteractiveObject)& theIO, const int theMode )
1405 {
1406   SUIT_Session* session = SUIT_Session::session();
1407   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
1408   if ( !app )
1409     return;
1410
1411   LightApp_SelectionMgr* sm = app->selectionMgr();
1412
1413   // remove all filters from selection
1414   sm->clearFilters();
1415
1416   SALOME_View* vf = GetActiveView();
1417   if ( vf ) {
1418     if (!theIO.IsNull() && !vf->isVisible(theIO))
1419       Display(theIO);
1420     SALOME_Prs* prs = vf->CreatePrs( theIO.IsNull() ? 0 : theIO->getEntry() );
1421     vf->LocalSelection( prs, theMode );
1422     delete prs;  // delete presentation because displayer is its owner
1423   }
1424 }
1425
1426 //=================================================================
1427 /*!
1428  *  GEOM_Displayer::globalSelection
1429  *  Activate selection of CAD shapes without activisation of selection
1430  *  of their sub-shapes (without opened local context for OCC viewer)
1431  */
1432 //=================================================================
1433 void GEOM_Displayer::GlobalSelection( const int theMode, const bool update )
1434 {
1435   TColStd_MapOfInteger aModes;
1436   aModes.Add( theMode );
1437   GlobalSelection( aModes, update );
1438 }
1439
1440 //=================================================================
1441 /*!
1442  *  GEOM_Displayer::globalSelection
1443  *  Activate selection of CAD shapes without activisation of selection
1444  *  of their sub-shapes (without opened local context for OCC viewer)
1445  */
1446 //=================================================================
1447 void GEOM_Displayer::GlobalSelection( const TColStd_MapOfInteger& theModes,
1448                                       const bool update, const QList<int>* theSubShapes )
1449 {
1450   SUIT_Session* session = SUIT_Session::session();
1451   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
1452   if ( !app )
1453     return;
1454
1455   SALOME_View* vf = GetActiveView();
1456   if ( vf == 0 )
1457     return;
1458
1459   // Close local context
1460   vf->GlobalSelection( update );
1461
1462   // Set selection filters in accordance with current mode
1463   LightApp_SelectionMgr* sm = app->selectionMgr();
1464   if ( !sm )
1465     return;
1466
1467   // Remove from selection temporary objects if necessary
1468   if ( !theModes.Contains( GEOM_PREVIEW ) )
1469     clearTemporary( sm );
1470
1471   //@ aSel->ClearIndex();
1472
1473   sm->clearFilters();
1474
1475   // Remove filters from AIS_InteractiveContext
1476   Handle(AIS_InteractiveContext) ic;
1477   SOCC_Viewer* viewer = dynamic_cast<SOCC_Viewer*>( vf );
1478   if ( viewer )
1479     {
1480       ic = viewer->getAISContext();
1481       if ( !ic.IsNull() )
1482         ic->RemoveFilters();
1483     }
1484
1485   if ( theModes.Contains( GEOM_ALLOBJECTS ) )
1486     return;
1487
1488   SUIT_SelectionFilter* aFilter;
1489   if ( theModes.Extent() == 1 )
1490     {
1491       int aMode = TColStd_MapIteratorOfMapOfInteger( theModes ).Key();
1492
1493       if ( aMode == GEOM_COMPOUNDFILTER )
1494         aFilter = getComplexFilter( theSubShapes );
1495       else
1496         aFilter = getFilter( aMode );
1497     }
1498   else if ( theModes.Extent() > 1 )
1499     {
1500       TColStd_MapOfInteger aTopAbsModes;
1501       TColStd_MapIteratorOfMapOfInteger anIter( theModes );
1502       QList<SUIT_SelectionFilter*> aListOfFilters;
1503       for ( ; anIter.More(); anIter.Next() )
1504         {
1505           SUIT_SelectionFilter* aFilter;
1506           int aMode = anIter.Key();
1507           if ( aMode == GEOM_COMPOUNDFILTER )
1508             aFilter = getComplexFilter( theSubShapes );
1509           else
1510             aFilter = getFilter( aMode );
1511
1512           if ( aFilter )
1513             aListOfFilters.append( aFilter );
1514         }
1515
1516       aFilter = new GEOM_LogicalFilter( aListOfFilters, GEOM_LogicalFilter::LO_OR );
1517     }
1518   else
1519     return;
1520
1521   if ( aFilter )
1522     {
1523       sm->installFilter( aFilter );
1524       if ( !ic.IsNull() )
1525         {
1526           Handle(GEOM_OCCFilter) anOCCFilter = new GEOM_OCCFilter( sm );
1527           ic->AddFilter( anOCCFilter );
1528         }
1529     }
1530 }
1531
1532 //=================================================================
1533 /*!
1534  *  GEOM_Displayer::LocalSelection
1535  *  Activate selection of CAD shapes with activisation of selection
1536  *  of their sub-shapes (with opened local context for OCC viewer)
1537  */
1538 //=================================================================
1539 void GEOM_Displayer::LocalSelection( const SALOME_ListIO& theIOList, const int theMode )
1540 {
1541   SALOME_ListIteratorOfListIO Iter( theIOList );
1542   for ( ; Iter.More(); Iter.Next() )
1543     LocalSelection( Iter.Value(), theMode );
1544 }
1545
1546 //=================================================================
1547 /*!
1548  *  GEOM_Displayer::BeforeDisplay
1549  *  Called before displaying of pars. Close local context
1550  *  [ Reimplemented from SALOME_Displayer ]
1551  */
1552 //=================================================================
1553 void GEOM_Displayer::BeforeDisplay( SALOME_View* v, const SALOME_OCCPrs* )
1554 {
1555   SOCC_Viewer* vf = dynamic_cast<SOCC_Viewer*>( v );
1556   if ( vf )
1557   {
1558     Handle(AIS_InteractiveContext) ic = vf->getAISContext();
1559     if ( !ic.IsNull() )
1560     {
1561       if ( ic->HasOpenedContext() )
1562       ic->CloseAllContexts();
1563     }
1564   }
1565 }
1566
1567 void GEOM_Displayer::AfterDisplay( SALOME_View* v, const SALOME_OCCPrs* p )
1568 {
1569   SalomeApp_Study* aStudy = getStudy();
1570   if (!aStudy) return;
1571   SOCC_Viewer* vf = dynamic_cast<SOCC_Viewer*>( v );
1572   if ( vf && !p->IsNull() ) {
1573     int aMgrId = getViewManagerId( vf );
1574     Handle(AIS_InteractiveContext) ic = vf->getAISContext();
1575     const SOCC_Prs* prs = dynamic_cast<const SOCC_Prs*>( p );
1576     if ( !ic.IsNull() && prs ) {
1577       AIS_ListOfInteractive objects;
1578       prs->GetObjects( objects );
1579       AIS_ListIteratorOfListOfInteractive it( objects );
1580       for ( ; it.More(); it.Next() ) {
1581         Handle(GEOM_AISShape) sh = Handle(GEOM_AISShape)::DownCast( it.Value() );
1582         if ( sh.IsNull() ) continue;
1583         Handle(SALOME_InteractiveObject) IO = sh->getIO();
1584         if ( IO.IsNull() ) continue;
1585         PropMap aPropMap = aStudy->getObjectPropMap( aMgrId, IO->getEntry() );
1586         if ( aPropMap.contains( GEOM::propertyName( GEOM::Transparency ) ) ) {
1587           double transparency = aPropMap.value(GEOM::propertyName( GEOM::Transparency )).toDouble();
1588           ic->SetTransparency( sh, transparency, true );
1589         }
1590       }
1591     }
1592   }
1593   UpdateColorScale();
1594 }
1595
1596 void GEOM_Displayer::AfterErase( SALOME_View* v, const SALOME_OCCPrs* p )
1597 {
1598   LightApp_Displayer::AfterErase( v, p );
1599   UpdateColorScale();
1600 }
1601
1602 //=================================================================
1603 /*!
1604  *  GEOM_Displayer::SetColor
1605  *  Set color for shape displaying. If it is equal -1 then default color is used.
1606  *  Available values are from Quantity_NameOfColor enumeration
1607  */
1608 //=================================================================
1609 void GEOM_Displayer::SetColor( const int color )
1610 {
1611   if ( color == -1 )
1612     UnsetColor();
1613   else
1614   {
1615     myColor = color;
1616     myShadingColor = Quantity_Color( (Quantity_NameOfColor)color );
1617   }
1618 }
1619
1620 int GEOM_Displayer::GetColor() const
1621 {
1622   return myColor;
1623 }
1624
1625 bool GEOM_Displayer::HasColor() const
1626 {
1627   return myColor != -1;
1628 }
1629
1630 void GEOM_Displayer::UnsetColor()
1631 {
1632   myColor = -1;
1633
1634   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
1635   QColor col = resMgr->colorValue( "Geometry", "shading_color", QColor( 255, 0, 0 ) );
1636   myShadingColor = SalomeApp_Tools::color( col );
1637 }
1638
1639 //=================================================================
1640 /*!
1641  *  GEOM_Displayer::SetTexture
1642  *  Set color for shape displaying. If it is equal -1 then default color is used.
1643  *  Available values are from Quantity_NameOfColor enumeration
1644  */
1645 //=================================================================
1646 void GEOM_Displayer::SetTexture( const std::string& texureFileName )
1647 {
1648   if(texureFileName!="")
1649   {
1650     myTexture = texureFileName;
1651   }
1652 }
1653
1654 bool GEOM_Displayer::HasTexture() const
1655 {
1656   return myTexture != "";
1657 }
1658
1659 std::string GEOM_Displayer::GetTexture() const
1660 {
1661   return myTexture;
1662 }
1663
1664 //=================================================================
1665 /*!
1666  *  GEOM_Displayer::SetWidth
1667  *  Set width of shape displaying. If it is equal -1 then default width is used.
1668  */
1669 //=================================================================
1670 void GEOM_Displayer::SetWidth( const double width )
1671 {
1672   myWidth = width;
1673 }
1674
1675 double GEOM_Displayer::GetWidth() const
1676 {
1677   return myWidth;
1678 }
1679
1680 bool GEOM_Displayer::HasWidth() const
1681 {
1682   return myWidth != -1;
1683 }
1684
1685 void GEOM_Displayer::UnsetWidth()
1686 {
1687   myWidth = -1;
1688 }
1689
1690
1691 int GEOM_Displayer::GetIsosWidth() const
1692 {
1693   return myIsosWidth;
1694 }
1695
1696 void GEOM_Displayer::SetIsosWidth(const int width)
1697 {
1698   myIsosWidth = width;
1699 }
1700
1701 bool GEOM_Displayer::HasIsosWidth() const
1702 {
1703   return myIsosWidth != -1;
1704 }
1705
1706
1707 //=================================================================
1708 /*!
1709  *  GEOM_Displayer::SetToActivate
1710  *  This method is used for activisation/deactivisation of objects to be displayed
1711  */
1712 //=================================================================
1713 void GEOM_Displayer::SetToActivate( const bool toActivate )
1714 {
1715   myToActivate = toActivate;
1716 }
1717 bool GEOM_Displayer::ToActivate() const
1718 {
1719   return myToActivate;
1720 }
1721
1722 //=================================================================
1723 /*!
1724  *  GEOM_Displayer::clearTemporary
1725  *  Removes from selection temporary objects
1726  */
1727 //=================================================================
1728 void GEOM_Displayer::clearTemporary( LightApp_SelectionMgr* theSelMgr )
1729 {
1730   SALOME_ListIO selected, toSelect;
1731   theSelMgr->selectedObjects( selected );
1732
1733   for (  SALOME_ListIteratorOfListIO it( selected ) ; it.More(); it.Next() ) {
1734     Handle(SALOME_InteractiveObject) io = it.Value();
1735     if ( !io.IsNull() && io->hasEntry() && strncmp( io->getEntry(), "TEMP_", 5 ) != 0 )
1736       toSelect.Append( it.Value() );
1737   }
1738
1739   theSelMgr->setSelectedObjects( toSelect, true );
1740 }
1741
1742 void GEOM_Displayer::SetName( const char* theName )
1743 {
1744   myName = theName;
1745 }
1746
1747 void GEOM_Displayer::UnsetName()
1748 {
1749   myName = "";
1750 }
1751
1752 SalomeApp_Study* GEOM_Displayer::getStudy() const
1753 {
1754   return dynamic_cast<SalomeApp_Study*>( myApp->activeStudy() );
1755 }
1756
1757 void GEOM_Displayer::setIO( const Handle(SALOME_InteractiveObject)& theIO )
1758 {
1759   myIO = theIO;
1760 }
1761
1762 void GEOM_Displayer::setShape( const TopoDS_Shape& theShape )
1763 {
1764   myShape = theShape;
1765 }
1766
1767 void GEOM_Displayer::setFieldStepInfo( const GEOM::field_data_type theFieldDataType,
1768                                        const int theFieldDimension,
1769                                        const QList<QVariant>& theFieldStepData,
1770                                        const TCollection_AsciiString& theFieldStepName,
1771                                        const double theFieldStepRangeMin,
1772                                        const double theFieldStepRangeMax )
1773 {
1774   myFieldDataType = theFieldDataType;
1775   myFieldDimension = theFieldDimension;
1776   myFieldStepData = theFieldStepData;
1777   myFieldStepName = theFieldStepName;
1778   myFieldStepRangeMin = theFieldStepRangeMin;
1779   myFieldStepRangeMax = theFieldStepRangeMax;
1780 }
1781
1782 bool GEOM_Displayer::canBeDisplayed( const QString& entry, const QString& viewer_type ) const
1783 {
1784   _PTR(SObject) anObj = getStudy()->studyDS()->FindObjectID( (const char*)entry.toLatin1() );
1785   GEOM::GEOM_Object_var aGeomObj = GEOM::GEOM_Object::_narrow(GeometryGUI::ClientSObjectToObject(anObj)); // enable displaying of GEOM objects
1786   GEOM::GEOM_FieldStep_var aFieldStepObj = GEOM::GEOM_FieldStep::_narrow(GeometryGUI::ClientSObjectToObject(anObj)); // enable displaying of GEOM field steps
1787   GEOM::GEOM_Gen_var aCompObj = GEOM::GEOM_Gen::_narrow(GeometryGUI::ClientSObjectToObject(anObj)); // enable displaying of whole GEOM component
1788   return ( !CORBA::is_nil( aGeomObj ) || !CORBA::is_nil( aFieldStepObj ) || !CORBA::is_nil( aCompObj ) ) &&
1789          (viewer_type == SOCC_Viewer::Type() || viewer_type == SVTK_Viewer::Type());
1790 }
1791
1792 int GEOM_Displayer::SetDisplayMode( const int theMode )
1793 {
1794   int aPrevMode = myDisplayMode;
1795   if ( theMode != -1 ) {
1796     myDisplayMode = theMode;
1797     myHasDisplayMode = true;
1798   }
1799   else {
1800     UnsetDisplayMode();
1801   }
1802   return aPrevMode;
1803 }
1804
1805 int GEOM_Displayer::GetDisplayMode() const
1806 {
1807   return myDisplayMode;
1808 }
1809
1810 int GEOM_Displayer::UnsetDisplayMode()
1811 {
1812   int aPrevMode = myDisplayMode;
1813   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
1814   myDisplayMode = resMgr->integerValue( "Geometry", "display_mode", 0 );
1815   myHasDisplayMode = false;
1816   return aPrevMode;
1817 }
1818
1819 bool GEOM_Displayer::HasDisplayMode() const
1820 {
1821   return myHasDisplayMode;
1822 }
1823
1824 SALOMEDS::Color GEOM_Displayer::getPredefinedUniqueColor()
1825 {
1826   static QList<QColor> colors;
1827
1828   if ( colors.isEmpty() ) {
1829
1830     for (int s = 0; s < 2 ; s++)
1831     {
1832       for (int v = 100; v >= 40; v = v - 20)
1833       {
1834         for (int h = 0; h < 359 ; h = h + 60)
1835         {
1836           colors.append(QColor::fromHsv(h, 255 - s * 127, v * 255 / 100));
1837         }
1838       }
1839     }
1840   }
1841
1842   static int currentColor = 0;
1843
1844   SALOMEDS::Color color;
1845   color.R = (double)colors[currentColor].red()   / 255.0;
1846   color.G = (double)colors[currentColor].green() / 255.0;
1847   color.B = (double)colors[currentColor].blue()  / 255.0;
1848
1849   currentColor = (currentColor+1) % colors.count();
1850
1851   return color;
1852 }
1853
1854 SALOMEDS::Color GEOM_Displayer::getUniqueColor( const QList<SALOMEDS::Color>& theReservedColors )
1855 {
1856   int aHue = -1;
1857   int aTolerance = 64;
1858   int anIterations = 0;
1859   int aPeriod = 5;
1860
1861   while( 1 )
1862   {
1863     anIterations++;
1864     if( anIterations % aPeriod == 0 )
1865     {
1866       aTolerance /= 2;
1867       if( aTolerance < 1 )
1868         break;
1869     }
1870     //std::cout << "Iteration N" << anIterations << " (tolerance=" << aTolerance << ")"<< std::endl;
1871
1872     aHue = (int)( 360.0 * rand() / RAND_MAX );
1873     //std::cout << "Hue = " << aHue << std::endl;
1874
1875     //std::cout << "Auto colors : ";
1876     bool ok = true;
1877     QList<SALOMEDS::Color>::const_iterator it = theReservedColors.constBegin();
1878     QList<SALOMEDS::Color>::const_iterator itEnd = theReservedColors.constEnd();
1879     for( ; it != itEnd; ++it )
1880     {
1881       SALOMEDS::Color anAutoColor = *it;
1882       QColor aQColor( (int)( anAutoColor.R * 255.0 ), (int)( anAutoColor.G * 255.0 ), (int)( anAutoColor.B * 255.0 ) );
1883
1884       int h, s, v;
1885       aQColor.getHsv( &h, &s, &v );
1886       //std::cout << h << " ";
1887       if( abs( h - aHue ) < aTolerance )
1888       {
1889         ok = false;
1890         //std::cout << "break (diff = " << abs( h - aHue ) << ")";
1891         break;
1892       }
1893     }
1894     //std::cout << std::endl;
1895
1896     if( ok )
1897       break;
1898   }
1899
1900   //std::cout << "Hue of the returned color = " << aHue << std::endl;
1901   QColor aColor;
1902   aColor.setHsv( aHue, 255, 255 );
1903
1904   SALOMEDS::Color aSColor;
1905   aSColor.R = (double)aColor.red() / 255.0;
1906   aSColor.G = (double)aColor.green() / 255.0;
1907   aSColor.B = (double)aColor.blue() / 255.0;
1908
1909   return aSColor;
1910 }
1911
1912 PropMap GEOM_Displayer::getObjectProperties( SalomeApp_Study* study,
1913                                              const QString& entry,
1914                                              SALOME_View* view )
1915 {
1916   // get default properties for the explicitly specified default view type
1917   PropMap propMap = GEOM_Displayer::getDefaultPropertyMap();
1918
1919   if ( study && view ) {
1920     SUIT_ViewModel* viewModel = dynamic_cast<SUIT_ViewModel*>( view );
1921     SUIT_ViewManager* viewMgr = ( viewModel != 0 ) ? viewModel->getViewManager() : 0;
1922     int viewId = ( viewMgr != 0 ) ? viewMgr->getGlobalId() : -1;
1923   
1924     if ( viewModel && viewId != -1 ) {
1925       // get properties from the study
1926       PropMap storedMap = study->getObjectPropMap( viewId, entry );
1927       // overwrite default properties from stored ones (that are specified)
1928       for ( int prop = GEOM::Visibility; prop <= GEOM::LastProperty; prop++ ) {
1929         if ( storedMap.contains( GEOM::propertyName( (GEOM::Property)prop ) ) )
1930           propMap.insert( GEOM::propertyName( (GEOM::Property)prop ), 
1931                           storedMap.value( GEOM::propertyName( (GEOM::Property)prop ) ) );
1932       }
1933       // ... specific processing for color
1934       // ... current implementation is to use same stored color for all aspects
1935       // ... (TODO) possible future improvements about free boundaries, standalone edges etc colors can be here
1936       if ( storedMap.contains( GEOM::propertyName( GEOM::Color ) ) ) {
1937         propMap.insert( GEOM::propertyName( GEOM::ShadingColor ),   storedMap.value( GEOM::propertyName( GEOM::Color ) ) );
1938         propMap.insert( GEOM::propertyName( GEOM::WireframeColor ), storedMap.value( GEOM::propertyName( GEOM::Color ) ) );
1939         propMap.insert( GEOM::propertyName( GEOM::LineColor ),      storedMap.value( GEOM::propertyName( GEOM::Color ) ) );
1940         propMap.insert( GEOM::propertyName( GEOM::FreeBndColor ),   storedMap.value( GEOM::propertyName( GEOM::Color ) ) );
1941         propMap.insert( GEOM::propertyName( GEOM::PointColor ),     storedMap.value( GEOM::propertyName( GEOM::Color ) ) );
1942       }
1943
1944       if ( !entry.isEmpty() ) {
1945         // get CORBA reference to geom object
1946         _PTR(SObject) SO( study->studyDS()->FindObjectID( entry.toStdString() ) );
1947         if ( SO ) {
1948           CORBA::Object_var object = GeometryGUI::ClientSObjectToObject( SO );
1949           if ( !CORBA::is_nil( object ) ) {
1950             GEOM::GEOM_Object_var geomObject = GEOM::GEOM_Object::_narrow( object );
1951             if ( !CORBA::is_nil( geomObject ) ) { // to check
1952               // check that geom object has color properly set
1953               bool hasColor = false;
1954               SALOMEDS::Color aSColor = getColor( geomObject, hasColor );
1955               // set color from geometry object (only once, if it is not yet set in GUI)
1956               // current implementation is to use same color for all aspects
1957               // (TODO) possible future improvements about free boundaries, standalone edges etc colors can be here
1958               if ( hasColor && !storedMap.contains( GEOM::propertyName( GEOM::Color ) ) ) {
1959                 QColor objColor = QColor::fromRgbF( aSColor.R, aSColor.G, aSColor.B );
1960                 propMap.insert( GEOM::propertyName( GEOM::ShadingColor ),   objColor );
1961                 propMap.insert( GEOM::propertyName( GEOM::WireframeColor ), objColor );
1962                 propMap.insert( GEOM::propertyName( GEOM::LineColor ),      objColor );
1963                 propMap.insert( GEOM::propertyName( GEOM::FreeBndColor ),   objColor );
1964                 propMap.insert( GEOM::propertyName( GEOM::PointColor ),     objColor );
1965               }
1966               // check that object has point marker properly set
1967               GEOM::marker_type mType = geomObject->GetMarkerType();
1968               GEOM::marker_size mSize = geomObject->GetMarkerSize();
1969               int mTextureId = geomObject->GetMarkerTexture();
1970               bool hasMarker = ( mType > GEOM::MT_NONE && mType < GEOM::MT_USER && mSize > GEOM::MS_NONE && mSize <= GEOM::MS_70 ) || 
1971                                ( mType == GEOM::MT_USER && mTextureId > 0 );
1972               // set point marker from geometry object (only once, if it is not yet set in GUI)
1973               if ( hasMarker && !storedMap.contains( GEOM::propertyName( GEOM::PointMarker ) ) ) {
1974                 if ( mType > GEOM::MT_NONE && mType < GEOM::MT_USER ) {
1975                   // standard type
1976                   propMap.insert( GEOM::propertyName( GEOM::PointMarker ),
1977                                   QString( "%1%2%3" ).arg( (int)mType ).arg( GEOM::subSectionSeparator() ).arg( (int)mSize ) );
1978                 }
1979                 else if ( mType == GEOM::MT_USER ) {
1980                   // custom texture
1981                   propMap.insert( GEOM::propertyName( GEOM::PointMarker ), QString::number( mTextureId ) );
1982                 }
1983               }
1984             }
1985           }
1986         }
1987       }
1988     }
1989   }
1990   return propMap;
1991 }
1992
1993 PropMap GEOM_Displayer::getDefaultPropertyMap()
1994 {
1995   PropMap propMap;
1996
1997   // get resource manager
1998   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
1999
2000   // fill in the properties map with default values
2001
2002   // - visibility (false by default)
2003   propMap.insert( GEOM::propertyName( GEOM::Visibility ), false );
2004
2005   // - nb isos (take default value from preferences)
2006   propMap.insert( GEOM::propertyName( GEOM::NbIsos ),
2007                   QString( "%1%2%3" ).
2008                   arg( resMgr->integerValue( "Geometry", "iso_number_u", 1 ) ).
2009                   arg( GEOM::subSectionSeparator() ).
2010                   arg( resMgr->integerValue( "Geometry", "iso_number_v", 1 ) ) );
2011
2012   // - transparency (opacity = 1-transparency)
2013   propMap.insert( GEOM::propertyName( GEOM::Transparency ), 0.0 );
2014
2015   // - display mode (take default value from preferences)
2016   propMap.insert( GEOM::propertyName( GEOM::DisplayMode ),
2017                   resMgr->integerValue( "Geometry", "display_mode", 0 ) );
2018
2019   // - show edges direction flag (false by default)
2020   propMap.insert( GEOM::propertyName( GEOM::EdgesDirection ), false );
2021
2022   // - shading color (take default value from preferences)
2023   propMap.insert( GEOM::propertyName( GEOM::ShadingColor ),
2024                   colorFromResources( "shading_color", QColor( 255, 255, 0 ) ) );
2025
2026   // - wireframe color (take default value from preferences)
2027   propMap.insert( GEOM::propertyName( GEOM::WireframeColor ),
2028                   colorFromResources( "wireframe_color", QColor( 255, 255, 0 ) ) );
2029
2030   // - standalone edges color (take default value from preferences)
2031   propMap.insert( GEOM::propertyName( GEOM::LineColor ),
2032                   colorFromResources( "line_color", QColor( 255, 0, 0 ) ) );
2033
2034   // - free boundaries color (take default value from preferences)
2035   propMap.insert( GEOM::propertyName( GEOM::FreeBndColor ),
2036                   colorFromResources( "free_bound_color", QColor( 0, 255, 0 ) ) );
2037
2038   // - points color (take default value from preferences)
2039   propMap.insert( GEOM::propertyName( GEOM::PointColor ),
2040                   colorFromResources( "point_color", QColor( 255, 255, 0 ) ) );
2041
2042   // - isos color (take default value from preferences)
2043   propMap.insert( GEOM::propertyName( GEOM::IsosColor ),
2044                   colorFromResources( "isos_color", QColor( 200, 200, 200 ) ) );
2045
2046   // - outlines color (take default value from preferences)
2047   propMap.insert( GEOM::propertyName( GEOM::OutlineColor ),
2048                   colorFromResources( "edges_in_shading_color", QColor( 180, 180, 180 ) ) );
2049
2050   // - deflection coefficient (take default value from preferences)
2051   propMap.insert( GEOM::propertyName( GEOM::Deflection ),
2052                   resMgr->doubleValue( "Geometry", "deflection_coeff", 0.001 ) );
2053
2054   // - material (take default value from preferences)
2055   Material_Model material;
2056   material.fromResources( resMgr->stringValue( "Geometry", "material", "Plastic" ) );
2057   propMap.insert( GEOM::propertyName( GEOM::Material ), material.toProperties() );
2058
2059   // - edge width (take default value from preferences)
2060   propMap.insert( GEOM::propertyName( GEOM::LineWidth ),
2061                   resMgr->integerValue( "Geometry", "edge_width", 1 ) );
2062
2063   // - isos width (take default value from preferences)
2064   propMap.insert( GEOM::propertyName( GEOM::IsosWidth ),
2065                   resMgr->integerValue( "Geometry", "isolines_width", 1 ) );
2066
2067   // - point marker (take default value from preferences)
2068   propMap.insert( GEOM::propertyName( GEOM::PointMarker ),
2069                   QString( "%1%2%3" ).
2070                   arg( resMgr->integerValue( "Geometry", "type_of_marker", 1 ) + 1 ).
2071                   arg( GEOM::subSectionSeparator() ).
2072                   arg( resMgr->integerValue( "Geometry", "marker_scale", 1 ) ) );
2073
2074   // - top-level flag (false by default)
2075   propMap.insert( GEOM::propertyName( GEOM::TopLevel ), false );
2076
2077   return propMap;
2078 }
2079
2080 SALOMEDS::Color GEOM_Displayer::getColor(GEOM::GEOM_Object_var theGeomObject, bool& hasColor) {
2081   SALOMEDS::Color aSColor;
2082   hasColor = false;
2083
2084   SUIT_Session* session = SUIT_Session::session();
2085   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
2086
2087   if ( app && !theGeomObject->_is_nil()) {
2088     SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
2089
2090     if ( study ) {
2091       _PTR(Study) aStudy = study->studyDS();
2092       aSColor = theGeomObject->GetColor();
2093       hasColor = aSColor.R >= 0 && aSColor.G >= 0 && aSColor.B >= 0;
2094       if ( !hasColor ) {
2095 #ifdef GENERAL_AUTOCOLOR // auto-color for all sub-shapes
2096         bool general_autocolor = true;
2097 #else                    // auto-color for groups only
2098         bool general_autocolor = false;
2099 #endif                   // GENERAL_AUTOCOLOR
2100         if ( general_autocolor || theGeomObject->GetType() == GEOM_GROUP ) {
2101           GEOM::GEOM_Object_var aMainObject = theGeomObject->GetMainShape();
2102           if ( !CORBA::is_nil( aMainObject ) && aMainObject->GetAutoColor() ) {
2103 #ifdef SIMPLE_AUTOCOLOR  // simplified algorithm for auto-colors
2104             aSColor = getPredefinedUniqueColor();
2105             hasColor = true;
2106 #else                    // old algorithm  for auto-colors
2107             QList<SALOMEDS::Color> aReservedColors;
2108             CORBA::String_var IOR = app->orb()->object_to_string( aMainObject );
2109             _PTR(SObject) aMainSObject( aStudy->FindObjectIOR( IOR.in() ) );
2110             if ( aMainSObject ) {
2111               _PTR(ChildIterator) it( aStudy->NewChildIterator( aMainSObject ) );
2112               for ( ; it->More(); it->Next() ) {
2113                 _PTR(SObject) aChildSObject( it->Value() );
2114                 GEOM::GEOM_Object_var aChildObject =
2115                   GEOM::GEOM_Object::_narrow(GeometryGUI::ClientSObjectToObject(aChildSObject));
2116                 if ( CORBA::is_nil( aChildObject ) )
2117                   continue;
2118
2119                 SALOMEDS::Color aReservedColor = aChildObject->GetColor();
2120                 if ( aReservedColor.R >= 0 && aReservedColor.G >= 0 && aReservedColor.B >= 0 )
2121                   aReservedColors.append( aReservedColor );
2122               }
2123             }
2124             aSColor = getUniqueColor( aReservedColors );
2125             hasColor = true;
2126 #endif                   // SIMPLE_AUTOCOLOR
2127           }
2128         }
2129       }
2130     }
2131   }
2132   return aSColor;
2133 }
2134
2135
2136 void GEOM_Displayer::EraseWithChildren(const Handle(SALOME_InteractiveObject)& theIO,
2137                                        const bool eraseOnlyChildren) {
2138   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
2139   if ( !app )
2140     return;
2141
2142   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
2143   if ( !appStudy )
2144     return;
2145
2146   LightApp_DataObject* parent = appStudy->findObjectByEntry(theIO->getEntry());
2147
2148   if( !parent)
2149     return;
2150
2151   // Erase from all views
2152   QList<SALOME_View*> views;
2153   SALOME_View* view;
2154   ViewManagerList vmans = app->viewManagers();
2155   SUIT_ViewManager* vman;
2156   foreach ( vman, vmans ) {
2157     SUIT_ViewModel* vmod = vman->getViewModel();
2158     view = dynamic_cast<SALOME_View*> ( vmod );
2159     if ( view )
2160       views.append( view );
2161   }
2162
2163   if( views.count() == 0 )
2164     return;
2165
2166   //Erase childrens w/o update views
2167   DataObjectList listObj = parent->children( true );
2168   SUIT_DataObject* obj;
2169   foreach( obj, listObj ) {
2170     LightApp_DataObject* l_obj = dynamic_cast<LightApp_DataObject*>(obj);
2171     if(l_obj)
2172       foreach ( view, views ) {
2173       Handle(SALOME_InteractiveObject) anIO =
2174         new SALOME_InteractiveObject(qPrintable(l_obj->entry()), "GEOM", "");
2175       Erase(anIO, false, false, view);
2176     }
2177   }
2178
2179   //Erase parent with view update or repaint views
2180   foreach ( view, views ) {
2181     if(!eraseOnlyChildren)
2182       Erase(theIO, false, true, view);
2183     else
2184       view->Repaint();
2185   }
2186 }
2187
2188 void GEOM_Displayer::readFieldStepInfo( GEOM::GEOM_FieldStep_var theGeomFieldStep )
2189 {
2190   if( theGeomFieldStep->_is_nil() )
2191     return;
2192
2193   GEOM::GEOM_Field_var aGeomField = theGeomFieldStep->GetField();
2194   if( aGeomField->_is_nil() )
2195     return;
2196
2197   GEOM::GEOM_Object_var aGeomFieldShape = aGeomField->GetShape();
2198   if( aGeomFieldShape->_is_nil() )
2199     return;
2200
2201   TCollection_AsciiString aFieldStepName( theGeomFieldStep->GetName() );
2202   TCollection_AsciiString aFieldName( aGeomField->GetName() );
2203   TCollection_AsciiString aShapeName( aGeomFieldShape->GetName() );
2204
2205   aFieldStepName = aShapeName + "\n" + aFieldName + "\n" + aFieldStepName;
2206
2207   GEOM::field_data_type aFieldDataType = aGeomField->GetDataType();
2208
2209   int aFieldDimension = aGeomField->GetDimension();
2210
2211   GEOM::string_array_var aFieldComponents = aGeomField->GetComponents();
2212   int aFieldNbComponents = aFieldComponents->length();
2213
2214   QList<QVariant> aFieldStepData;
2215   if( aFieldDataType == GEOM::FDT_Bool )
2216   {
2217     GEOM::GEOM_BoolFieldStep_var aGeomBoolFieldStep = GEOM::GEOM_BoolFieldStep::_narrow( theGeomFieldStep );
2218     if ( !aGeomBoolFieldStep->_is_nil() )
2219     {
2220       GEOM::short_array_var aValues = aGeomBoolFieldStep->GetValues();
2221       for( size_t i = 0, n = aValues->length(); i < n; i++ )
2222         aFieldStepData << (bool)aValues[i];
2223     }
2224   }
2225   else if( aFieldDataType == GEOM::FDT_Int )
2226   {
2227     GEOM::GEOM_IntFieldStep_var aGeomIntFieldStep = GEOM::GEOM_IntFieldStep::_narrow( theGeomFieldStep );
2228     if ( !aGeomIntFieldStep->_is_nil() )
2229     {
2230       GEOM::ListOfLong_var aValues = aGeomIntFieldStep->GetValues();
2231       for( size_t i = 0, n = aValues->length(); i < n; i++ )
2232         aFieldStepData << (long)aValues[i];
2233     }
2234   }
2235   else if( aFieldDataType == GEOM::FDT_Double )
2236   {
2237     GEOM::GEOM_DoubleFieldStep_var aGeomDoubleFieldStep = GEOM::GEOM_DoubleFieldStep::_narrow( theGeomFieldStep );
2238     if ( !aGeomDoubleFieldStep->_is_nil() )
2239     {
2240       GEOM::ListOfDouble_var aValues = aGeomDoubleFieldStep->GetValues();
2241       for( size_t i = 0, n = aValues->length(); i < n; i++ )
2242         aFieldStepData << (double)aValues[i];
2243     }
2244   }
2245   else if( aFieldDataType == GEOM::FDT_String )
2246   {
2247     GEOM::GEOM_StringFieldStep_var aGeomStringFieldStep = GEOM::GEOM_StringFieldStep::_narrow( theGeomFieldStep );
2248     if ( !aGeomStringFieldStep->_is_nil() )
2249     {
2250       GEOM::string_array_var aValues = aGeomStringFieldStep->GetValues();
2251       for( size_t i = 0, n = aValues->length(); i < n; i++ )
2252         aFieldStepData << QString( aValues[i] );
2253     }
2254   }
2255
2256   double aFieldStepRangeMin = 0, aFieldStepRangeMax = 0;
2257   aFieldStepData = groupFieldData( aFieldStepData,
2258                                    aFieldNbComponents,
2259                                    aFieldDataType == GEOM::FDT_String,
2260                                    aFieldStepRangeMin,
2261                                    aFieldStepRangeMax );
2262
2263   setFieldStepInfo( aFieldDataType,
2264                     aFieldDimension,
2265                     aFieldStepData,
2266                     aFieldStepName,
2267                     aFieldStepRangeMin,
2268                     aFieldStepRangeMax );
2269 }
2270
2271 QList<QVariant> GEOM_Displayer::groupFieldData( const QList<QVariant>& theFieldStepData,
2272                                                 const int theFieldNbComponents,
2273                                                 const bool theIsString,
2274                                                 double& theFieldStepRangeMin,
2275                                                 double& theFieldStepRangeMax )
2276 {
2277   QList<QVariant> aResultList;
2278   theFieldStepRangeMin = 0;
2279   theFieldStepRangeMax = 0;
2280
2281   if( theFieldStepData.isEmpty() || theFieldNbComponents < 1 )
2282     return aResultList;
2283
2284   int aNbSubShapes = theFieldStepData.count() / theFieldNbComponents;
2285
2286   QList<QVariant> aGroupedList;
2287
2288   bool anIsBoolean = false;
2289   for( int aSubShape = 0; aSubShape < aNbSubShapes; aSubShape++ )
2290   {
2291     double aNorm = 0;
2292     QStringList aStringList;
2293
2294     int aBaseIndex = aSubShape * theFieldNbComponents;
2295     for( int aComponent = 0; aComponent < theFieldNbComponents; aComponent++ )
2296     {
2297       int anIndex = aComponent + aBaseIndex;
2298
2299       const QVariant& aVariant = theFieldStepData[ anIndex ];
2300       if( theIsString )
2301       {
2302         if( aVariant.type() == QVariant::String )
2303           aStringList << aVariant.toString();
2304       }
2305       else
2306       {
2307         double aValue = 0;
2308         if( aVariant.type() == QVariant::Bool )
2309         {
2310           aValue = aVariant.toBool() ? 1.0 : 0.0;
2311           aNorm += aValue;
2312           anIsBoolean = true;
2313         }
2314         else
2315         {
2316           if( aVariant.type() == QVariant::Int )
2317             aValue = double( aVariant.toInt() );
2318           else if( aVariant.type() == QVariant::Double )
2319             aValue = aVariant.toDouble();
2320           aNorm += aValue * aValue;
2321         }
2322       }
2323     }
2324
2325     if( theIsString )
2326       aGroupedList << aStringList.join( "\n" );
2327     else
2328     {
2329       if( anIsBoolean )
2330         aNorm /= theFieldNbComponents;
2331       else
2332         aNorm = pow( aNorm, 0.5 );
2333
2334       if( aGroupedList.isEmpty() )
2335         theFieldStepRangeMin = theFieldStepRangeMax = aNorm;
2336       else
2337       {
2338         theFieldStepRangeMin = Min( theFieldStepRangeMin, aNorm );
2339         theFieldStepRangeMax = Max( theFieldStepRangeMax, aNorm );
2340       }
2341
2342       aGroupedList << aNorm;
2343     }
2344   }
2345
2346   if( anIsBoolean )
2347   {
2348     theFieldStepRangeMin = 0.0;
2349     theFieldStepRangeMax = 1.0;
2350   }
2351
2352   SUIT_Session* session = SUIT_Session::session();
2353   SUIT_ResourceMgr* resMgr = session->resourceMgr();
2354   Standard_Integer aNbIntervals = resMgr->integerValue( "Geometry", "scalar_bar_nb_intervals", 20 );
2355
2356   QListIterator<QVariant> anIter( aGroupedList );
2357   while( anIter.hasNext() )
2358   {
2359     const QVariant& aVariant = anIter.next();
2360     if( theIsString )
2361       aResultList << aVariant;
2362     else
2363     {
2364       QColor aQColor;
2365       Quantity_Color aColor;
2366       if( FindColor( aVariant.toDouble(), theFieldStepRangeMin, theFieldStepRangeMax, anIsBoolean ? 2 : aNbIntervals, aColor ) )
2367         aQColor = QColor::fromRgbF( aColor.Red(), aColor.Green(), aColor.Blue() );
2368       aResultList << aQColor;
2369     }
2370   }
2371   return aResultList;
2372 }
2373
2374 // Note: the method is copied from Aspect_ColorScale class
2375 Standard_Integer GEOM_Displayer::HueFromValue( const Standard_Integer aValue,
2376                                                const Standard_Integer aMin,
2377                                                const Standard_Integer aMax )
2378 {
2379   Standard_Integer minLimit( 0 ), maxLimit( 230 );
2380
2381   Standard_Integer aHue = maxLimit;
2382   if ( aMin != aMax )
2383     aHue = (Standard_Integer)( maxLimit - ( maxLimit - minLimit ) * ( aValue - aMin ) / ( aMax - aMin ) );
2384
2385   aHue = Min( Max( minLimit, aHue ), maxLimit );
2386
2387   return aHue;
2388 }
2389
2390 // Note: the method is copied from Aspect_ColorScale class
2391 Standard_Boolean GEOM_Displayer::FindColor( const Standard_Real aValue, 
2392                                             const Standard_Real aMin,
2393                                             const Standard_Real aMax,
2394                                             const Standard_Integer ColorsCount,
2395                                             Quantity_Color& aColor )
2396 {
2397   if( aValue<aMin || aValue>aMax || aMax<aMin )
2398     return Standard_False;
2399
2400   else
2401   {
2402     Standard_Real IntervNumber = 0;
2403     if( aValue<aMin )
2404       IntervNumber = 0;
2405     else if( aValue>aMax )
2406       IntervNumber = ColorsCount-1;
2407     else if( Abs( aMax-aMin ) > Precision::Approximation() )
2408       IntervNumber = Floor( Standard_Real( ColorsCount ) * ( aValue - aMin ) / ( aMax - aMin ) ); // 'Ceiling' replaced with 'Floor'
2409
2410     Standard_Integer Interv = Standard_Integer( IntervNumber );
2411
2412     aColor = Quantity_Color( HueFromValue( Interv, 0, ColorsCount - 1 ), 1.0, 1.0, Quantity_TOC_HLS );
2413
2414     return Standard_True;
2415   } 
2416 }
2417
2418 void GEOM_Displayer::UpdateColorScale( const bool theIsRedisplayFieldSteps )
2419 {
2420   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( myApp->activeStudy() );
2421   if( !aStudy )
2422     return;
2423
2424   SOCC_Viewer* aViewModel = dynamic_cast<SOCC_Viewer*>( GetActiveView() );
2425   if( !aViewModel )
2426     return;
2427
2428   Handle(V3d_Viewer) aViewer = aViewModel->getViewer3d();
2429   if( aViewer.IsNull() )
2430     return;
2431
2432   aViewer->InitActiveViews();
2433   if( !aViewer->MoreActiveViews() )
2434     return;
2435
2436   Handle(V3d_View) aView = aViewer->ActiveView();
2437   if( aView.IsNull() )
2438     return;
2439
2440   Standard_Boolean anIsDisplayColorScale = Standard_False;
2441   TCollection_AsciiString aColorScaleTitle;
2442   Standard_Real aColorScaleMin = 0, aColorScaleMax = 0;
2443   Standard_Boolean anIsBoolean = Standard_False;
2444
2445   SALOME_ListIO aSelectedObjects;
2446   myApp->selectionMgr()->selectedObjects( aSelectedObjects );
2447   if( aSelectedObjects.Extent() == 1 )
2448   {
2449     Handle(SALOME_InteractiveObject) anIO = aSelectedObjects.First();
2450     if( !anIO.IsNull() )
2451     {
2452       SOCC_Prs* aPrs = dynamic_cast<SOCC_Prs*>( aViewModel->CreatePrs( anIO->getEntry() ) );
2453       if( aPrs )
2454       {
2455         AIS_ListOfInteractive aList;
2456         aPrs->GetObjects( aList );
2457         AIS_ListIteratorOfListOfInteractive anIter( aList );
2458         for( ; anIter.More(); anIter.Next() )
2459         {
2460           Handle(GEOM_AISShape) aShape = Handle(GEOM_AISShape)::DownCast( anIter.Value() );
2461           if( !aShape.IsNull() )
2462           {
2463             GEOM::field_data_type aFieldDataType;
2464             int aFieldDimension;
2465             QList<QVariant> aFieldStepData;
2466             TCollection_AsciiString aFieldStepName;
2467             double aFieldStepRangeMin, aFieldStepRangeMax;
2468             aShape->getFieldStepInfo( aFieldDataType,
2469                                       aFieldDimension,
2470                                       aFieldStepData,
2471                                       aFieldStepName,
2472                                       aFieldStepRangeMin,
2473                                       aFieldStepRangeMax );
2474             if( !aFieldStepData.isEmpty() && aFieldDataType != GEOM::FDT_String )
2475             {
2476               anIsDisplayColorScale = Standard_True;
2477               aColorScaleTitle = aFieldStepName;
2478               aColorScaleMin = aFieldStepRangeMin;
2479               aColorScaleMax = aFieldStepRangeMax;
2480               anIsBoolean = aFieldDataType == GEOM::FDT_Bool;
2481             }
2482           }
2483         }
2484       }
2485     }
2486   }
2487
2488   if( anIsDisplayColorScale )
2489   {
2490     Handle(Aspect_ColorScale) aColorScale = aView->ColorScale();
2491     if( !aColorScale.IsNull() )
2492     {
2493       SUIT_Session* session = SUIT_Session::session();
2494       SUIT_ResourceMgr* resMgr = session->resourceMgr();
2495
2496       Standard_Real anXPos = resMgr->doubleValue( "Geometry", "scalar_bar_x_position", 0.05 );
2497       Standard_Real anYPos = resMgr->doubleValue( "Geometry", "scalar_bar_y_position", 0.1 );
2498       Standard_Real aWidth = resMgr->doubleValue( "Geometry", "scalar_bar_width", 0.2 );
2499       Standard_Real aHeight = resMgr->doubleValue( "Geometry", "scalar_bar_height", 0.5 );
2500       Standard_Integer aTextHeight = resMgr->integerValue( "Geometry", "scalar_bar_text_height", 14 );
2501       Standard_Integer aNbIntervals = resMgr->integerValue( "Geometry", "scalar_bar_nb_intervals", 20 );
2502  
2503       aColorScale->SetXPosition( anXPos );
2504       aColorScale->SetYPosition( anYPos );
2505       aColorScale->SetWidth( aWidth );
2506       aColorScale->SetHeight( aHeight );
2507
2508       aColorScale->SetTextHeight( aTextHeight );
2509       aColorScale->SetNumberOfIntervals( anIsBoolean ? 2 : aNbIntervals );
2510
2511       aColorScale->SetTitle( aColorScaleTitle );
2512       aColorScale->SetRange( aColorScaleMin, aColorScaleMax );
2513     }
2514     if( !aView->ColorScaleIsDisplayed() )
2515       aView->ColorScaleDisplay();
2516   }
2517   else
2518   {
2519     if( aView->ColorScaleIsDisplayed() )
2520       aView->ColorScaleErase();
2521   }
2522
2523   if( theIsRedisplayFieldSteps )
2524   {
2525     _PTR(Study) aStudyDS = aStudy->studyDS();
2526     QList<SUIT_ViewManager*> vmList;
2527     myApp->viewManagers( vmList );
2528     for( QList<SUIT_ViewManager*>::Iterator vmIt = vmList.begin(); vmIt != vmList.end(); vmIt++ )
2529     {
2530       if( SUIT_ViewManager* aViewManager = *vmIt )
2531       {
2532         const ObjMap anObjects = aStudy->getObjectMap( aViewManager->getGlobalId() );
2533         for( ObjMap::ConstIterator objIt = anObjects.begin(); objIt != anObjects.end(); objIt++ )
2534         {
2535           _PTR(SObject) aSObj( aStudyDS->FindObjectID( objIt.key().toLatin1().constData() ) );
2536           if( aSObj )
2537           {
2538             CORBA::Object_var anObject = GeometryGUI::ClientSObjectToObject( aSObj );
2539             if( !CORBA::is_nil( anObject ) )
2540             {
2541               GEOM::GEOM_FieldStep_var aFieldStep = GEOM::GEOM_FieldStep::_narrow( anObject );
2542               if( !aFieldStep->_is_nil() )
2543               {
2544                 CORBA::String_var aStepEntry = aFieldStep->GetStudyEntry();
2545                 Handle(SALOME_InteractiveObject) aStepIO =
2546                   new SALOME_InteractiveObject( aStepEntry.in(), "GEOM", "TEMP_IO" );
2547                 Redisplay( aStepIO, false, false );
2548               }
2549             }
2550           }
2551         }
2552       }
2553     }
2554   }
2555
2556   UpdateViewer();
2557 }