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