Salome HOME
CCAR: GEOM_Client modification to optimize the method find
[modules/geom.git] / src / GEOMGUI / GEOM_Displayer.cxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // GEOM GEOMGUI : GUI for Geometry component
24 // File   : GEOM_Displayer.cxx
25 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
26 //
27 #include "GEOM_Displayer.h"
28
29 #include "GeometryGUI.h"
30
31 #include <GEOM_TypeFilter.h>
32 #include <GEOM_EdgeFilter.h>
33 #include <GEOM_FaceFilter.h>
34 #include <GEOM_CompoundFilter.h>
35 #include <GEOM_PreviewFilter.h>
36 #include <GEOM_LogicalFilter.h>
37 #include <GEOM_OCCFilter.h>
38
39 #include <GEOM_Actor.h>
40 #include <GEOM_AssemblyBuilder.h>
41 #include <GEOM_AISShape.hxx>
42 #include <GEOM_AISVector.hxx>
43 #include <GEOM_AISTrihedron.hxx>
44 #include <GEOM_VTKTrihedron.hxx>
45
46 #include <SUIT_Desktop.h>
47 #include <SUIT_ViewWindow.h>
48 #include <SUIT_Session.h>
49 #include <SUIT_ViewManager.h>
50 #include <SUIT_ResourceMgr.h>
51
52 #include <SalomeApp_Study.h>
53 #include <SalomeApp_Application.h>
54 #include <LightApp_SelectionMgr.h>
55 #include <SalomeApp_TypeFilter.h>
56 #include <SalomeApp_Tools.h>
57
58 #include <SALOME_ListIteratorOfListIO.hxx>
59 #include <SALOME_ListIO.hxx>
60 #include <SALOME_Prs.h>
61
62 #include <SOCC_Prs.h>
63 #include <SOCC_ViewModel.h>
64
65 #include <SVTK_Prs.h>
66 #include <SVTK_ViewModel.h>
67
68 // OCCT Includes
69 #include <AIS_Drawer.hxx>
70 #include <AIS_ListIteratorOfListOfInteractive.hxx>
71 #include <Prs3d_IsoAspect.hxx>
72 #include <Prs3d_PointAspect.hxx>
73 #include <StdSelect_TypeOfEdge.hxx>
74 #include <StdSelect_TypeOfFace.hxx>
75 #include <TopoDS_Face.hxx>
76 #include <BRep_Tool.hxx>
77 #include <Geom_Plane.hxx>
78 #include <Geom_Axis2Placement.hxx>
79 #include <gp_Pln.hxx>
80 #include <TColStd_MapOfInteger.hxx>
81 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
82 #include <TopoDS_Iterator.hxx>
83 #include <Graphic3d_AspectMarker3d.hxx>
84 #include <TopTools_MapOfShape.hxx>
85 #include <TopTools_ListOfShape.hxx>
86 #include <TopTools_ListIteratorOfListOfShape.hxx>
87
88 // VTK Includes
89 #include <vtkActorCollection.h>
90 #include <vtkProperty.h>
91
92 // CORBA Headers
93 #include CORBA_CLIENT_HEADER(SALOMEDS_Attributes)
94
95 #include <GEOMImpl_Types.hxx>
96 #include <Graphic3d_HArray1OfBytes.hxx>
97
98
99 //================================================================
100 // Function : getActiveStudy
101 // Purpose  : Get active study, returns 0 if no open study frame
102 //================================================================
103 static inline SalomeApp_Study* getActiveStudy()
104 {
105   SUIT_Session* session = SUIT_Session::session();
106   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
107   if ( app )
108     return ( SalomeApp_Study* )app->activeStudy();
109   return 0;
110 }
111
112 //================================================================
113 // Function : getTopAbsMode
114 // Purpose  : Get TopAbs_ShapeEnum value corresponding to the
115 //            one from GEOMImpl_Types.h
116 //================================================================
117 static inline int getTopAbsMode( const int implType )
118 {
119   switch ( implType )
120   {
121     case GEOM_COMPOUND  : return TopAbs_COMPOUND;
122     case GEOM_SOLID     : return TopAbs_SOLID;
123     case GEOM_SHELL     : return TopAbs_SHELL;
124     case GEOM_FACE      : return TopAbs_FACE;
125     case GEOM_WIRE      : return TopAbs_WIRE;
126     case GEOM_EDGE      : return TopAbs_EDGE;
127     case GEOM_POINT     : return TopAbs_VERTEX;
128     default             : return -1;
129   }
130 }
131
132 static bool isCompoundOfVertices( const TopoDS_Shape& theShape )
133 {
134   bool ret = false;
135   if ( !theShape.IsNull() ) {
136     int iType, nbTypes [TopAbs_SHAPE];
137     for (iType = 0; iType < TopAbs_SHAPE; ++iType)
138       nbTypes[iType] = 0;
139     nbTypes[theShape.ShapeType()]++;
140
141     TopTools_MapOfShape aMapOfShape;
142     aMapOfShape.Add(theShape);
143     TopTools_ListOfShape aListOfShape;
144     aListOfShape.Append(theShape);
145
146     TopTools_ListIteratorOfListOfShape itL (aListOfShape);
147     for (; itL.More(); itL.Next()) {
148       TopoDS_Iterator it (itL.Value());
149       for (; it.More(); it.Next()) {
150         TopoDS_Shape s = it.Value();
151         if (aMapOfShape.Add(s)) {
152           aListOfShape.Append(s);
153           nbTypes[s.ShapeType()]++;
154         }
155       }
156     }
157     ret = nbTypes[TopAbs_VERTEX] > 0 && nbTypes[TopAbs_COMPOUND] == 1;
158   }
159   return ret;
160 }
161
162 //================================================================
163 // Function : getFilter
164 // Purpose  : Get filter corresponding to the type of object
165 //            from GEOMImpl_Types.h
166 //================================================================
167 SUIT_SelectionFilter* GEOM_Displayer::getFilter( const int theMode )
168 {
169   SUIT_SelectionFilter* aFilter;
170
171   int aTopAbsMode = getTopAbsMode( theMode );
172   if ( aTopAbsMode != -1 )
173     aFilter = new GEOM_TypeFilter( getStudy(), aTopAbsMode, true ); //@ aFilter = new GEOM_TypeFilter( ( TopAbs_ShapeEnum )aTopAbsMode );
174   else
175     switch ( theMode )
176       {
177       case GEOM_LINE      : aFilter = new GEOM_EdgeFilter( getStudy(), StdSelect_Line ); break;
178       case GEOM_CIRCLE    : aFilter = new GEOM_EdgeFilter( getStudy(), StdSelect_Circle ); break;
179
180       case GEOM_PLANE     : aFilter = new GEOM_FaceFilter( getStudy(), StdSelect_Plane ); break;
181       case GEOM_CYLINDER  : aFilter = new GEOM_FaceFilter( getStudy(), StdSelect_Cylinder ); break;
182       case GEOM_SPHERE    : aFilter = new GEOM_FaceFilter( getStudy(), StdSelect_Sphere ); break;
183       case GEOM_TORUS     : aFilter = new GEOM_FaceFilter( getStudy(), StdSelect_Torus ); break;
184       case GEOM_REVOLUTION: aFilter = new GEOM_FaceFilter( getStudy(), StdSelect_Revol ); break;
185       case GEOM_CONE      : aFilter = new GEOM_FaceFilter( getStudy(), StdSelect_Cone ); break;
186
187       case GEOM_PREVIEW   : aFilter = new GEOM_PreviewFilter( getStudy() ); break;
188
189       case GEOM_ALLSHAPES : aFilter = new GEOM_SelectionFilter(getStudy(), true ); break;
190       case GEOM_ALLGEOM   : aFilter = new SalomeApp_TypeFilter( getStudy(), "GEOM" ); break;
191
192       default             : aFilter = new GEOM_TypeFilter( getStudy(), theMode ); break;
193       }
194
195   return aFilter;
196 }
197
198 //================================================================
199 // Function : getComplexFilter
200 // Purpose  : Get compound filter corresponding to the type of 
201 //            object from GEOMImpl_Types.h
202 //================================================================
203 SUIT_SelectionFilter* GEOM_Displayer::getComplexFilter( const QList<int>* aSubShapes)
204 {
205   GEOM_CompoundFilter* aFilter;
206   
207   if(aSubShapes != NULL ) {
208     aFilter = new GEOM_CompoundFilter(getStudy());
209     QList<int> aTopAbsTypes;
210     QList<int>::const_iterator it;
211     for(it = aSubShapes->constBegin(); it != aSubShapes->constEnd(); ++it ) {
212       int topAbsMode = getTopAbsMode(*it);
213       if(topAbsMode != -1 )
214         aTopAbsTypes.append(topAbsMode);
215     }
216     aFilter->addSubTypes(aTopAbsTypes);
217   }
218   
219   return aFilter;
220 }
221
222 //================================================================
223 // Function : getEntry
224 // Purpose  :
225 //================================================================
226 static std::string getEntry( GEOM::GEOM_Object_ptr object )
227 {
228   SUIT_Session* session = SUIT_Session::session();
229   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
230   if ( app )
231   {
232     CORBA::String_var IOR = app->orb()->object_to_string( object );
233     if ( strcmp(IOR.in(), "") != 0 )
234     {
235       SalomeApp_Study* study = ( SalomeApp_Study* )app->activeStudy();
236       _PTR(SObject) SO ( study->studyDS()->FindObjectIOR( std::string(IOR) ) );
237       if ( SO )
238         return SO->GetID();
239     }
240   }
241   return "";
242 }
243
244 //================================================================
245 // Function : getName
246 // Purpose  :
247 //================================================================
248 static std::string getName( GEOM::GEOM_Object_ptr object )
249 {
250   SUIT_Session* session = SUIT_Session::session();
251   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
252   if ( app )
253   {
254     CORBA::String_var IOR = app->orb()->object_to_string( object );
255     if ( strcmp(IOR.in(), "") != 0 )
256     {
257       SalomeApp_Study* study = ( SalomeApp_Study* )app->activeStudy();
258       _PTR(SObject) aSObj ( study->studyDS()->FindObjectIOR( std::string(IOR) ) );
259
260       _PTR(GenericAttribute) anAttr;
261
262       if ( aSObj && aSObj->FindAttribute( anAttr, "AttributeName") )
263       {
264         _PTR(AttributeName) aNameAttr( anAttr );
265         return aNameAttr->Value();
266       }
267     }
268   }
269
270   return "";
271 }
272
273 //=================================================================
274 /*!
275  *  GEOM_Displayer::GEOM_Displayer
276  *  Constructor
277  */
278 //=================================================================
279 GEOM_Displayer::GEOM_Displayer( SalomeApp_Study* st )
280 {
281   if( st )
282     myApp = dynamic_cast<SalomeApp_Application*>( st->application() );
283   else
284     myApp = 0;
285
286   /* Shading Color */
287   SUIT_Session* session = SUIT_Session::session();
288   SUIT_ResourceMgr* resMgr = session->resourceMgr();
289
290   QColor col = resMgr->colorValue( "Geometry", "shading_color", QColor( 255, 0, 0 ) );
291   myShadingColor = SalomeApp_Tools::color( col );
292
293   myDisplayMode = resMgr->integerValue("Geometry", "display_mode", 0);
294   int aType = resMgr->integerValue("Geometry", "type_of_marker", (int)Aspect_TOM_PLUS);
295   myTypeOfMarker = (Aspect_TypeOfMarker)(std::min((int)Aspect_TOM_RING3, std::max((int)Aspect_TOM_POINT, aType)));
296   myScaleOfMarker = (resMgr->integerValue("Geometry", "marker_scale", 1)-(int)GEOM::MS_10)*0.5 + 1.0;
297   myScaleOfMarker = std::min(7.0, std::max(1., myScaleOfMarker));
298
299   myColor = -1;
300   // This color is used for shape displaying. If it is equal -1 then
301   // default color is used.
302
303   myWidth = -1;
304   myType = -1;
305
306   myToActivate = true;
307   // This parameter is used for activisation/deactivisation of objects to be displayed
308
309   myViewFrame = 0;
310 }
311
312 //=================================================================
313 /*!
314  *  GEOM_Displayer::~GEOM_Displayer
315  *  Destructor
316  */
317 //=================================================================
318 GEOM_Displayer::~GEOM_Displayer()
319 {
320 }
321
322 //=================================================================
323 /*!
324  *  GEOM_Displayer::Display
325  *  Display interactive object in the current viewer
326  */
327 //=================================================================
328 void GEOM_Displayer::Display( const Handle(SALOME_InteractiveObject)& theIO,
329                              const bool updateViewer,
330                              SALOME_View* theViewFrame )
331 {
332   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
333   if ( vf )
334   {
335     SALOME_Prs* prs = buildPresentation( theIO->getEntry(), vf );
336
337     if ( prs )
338     {
339       vf->BeforeDisplay( this );
340       vf->Display( prs );
341       vf->AfterDisplay( this );
342
343       if ( updateViewer )
344         vf->Repaint();
345
346       delete prs;  // delete presentation because displayer is its owner
347     }
348   }
349 }
350
351 //=================================================================
352 /*!
353  *  GEOM_Displayer::Display
354  *  This overloaded Display() method can be useful for operations
355  *  not using dialog boxes.
356  */
357 //=================================================================
358 void GEOM_Displayer::Display( GEOM::GEOM_Object_ptr theObj, const bool updateViewer )
359 {
360   if ( theObj->_is_nil() )
361     return;
362
363   std::string entry = getEntry( theObj );
364   if ( entry != "" ) {
365     Display(new SALOME_InteractiveObject(entry.c_str(), "GEOM", getName(theObj).c_str()),
366             updateViewer);
367   }
368 }
369
370 //=================================================================
371 /*!
372  *  GEOM_Displayer::Erase
373  *  Erase interactive object in the current viewer
374  */
375 //=================================================================
376 void GEOM_Displayer::Erase( const Handle(SALOME_InteractiveObject)& theIO,
377                             const bool forced,
378                             const bool updateViewer,
379                             SALOME_View* theViewFrame )
380 {
381   if ( theIO.IsNull() )
382     return;
383
384   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
385
386   if ( vf ) {
387     SALOME_Prs* prs = vf->CreatePrs( theIO->getEntry() );
388     if ( prs ) {
389       vf->Erase( prs, forced );
390       if ( updateViewer )
391         vf->Repaint();
392       delete prs;  // delete presentation because displayer is its owner
393     }
394   }
395 }
396
397 //=================================================================
398 /*!
399  *  GEOM_Displayer::Erase
400  *  Erase geometry object in the current viewer
401  */
402 //=================================================================
403 void GEOM_Displayer::Erase( GEOM::GEOM_Object_ptr theObj,
404                             const bool forced,
405                             const bool updateViewer )
406 {
407   std::string entry = getEntry( theObj );
408   if ( entry != "" )
409   {
410     Erase(new SALOME_InteractiveObject(entry.c_str(), "GEOM", getName(theObj).c_str()),
411           forced, updateViewer);
412   }
413 }
414
415 //=================================================================
416 /*!
417  *  GEOM_Displayer::Redisplay
418  *  Redisplay (erase and then display again) interactive object
419  *  in the current viewer
420  */
421 //=================================================================
422 void GEOM_Displayer::Redisplay( const Handle(SALOME_InteractiveObject)& theIO,
423                                 const bool updateViewer )
424 {
425   // Remove the object permanently (<forced> == true)
426   SUIT_Session* ses = SUIT_Session::session();
427   SUIT_Application* app = ses->activeApplication();
428   if ( app )
429   {
430     SUIT_Desktop* desk = app->desktop();
431     QList<SUIT_ViewWindow*> wnds = desk->windows();
432     SUIT_ViewWindow* wnd;
433     QListIterator<SUIT_ViewWindow*> it( wnds );
434     while ( it.hasNext() && (wnd = it.next()) )
435     {
436       SUIT_ViewManager* vman = wnd->getViewManager();
437       if ( vman )
438       {
439         SUIT_ViewModel* vmodel = vman->getViewModel();
440         if ( vmodel )
441         {
442           SALOME_View* view = dynamic_cast<SALOME_View*>(vmodel);
443           if ( view )
444           {
445             if ( view->isVisible( theIO ) || view == GetActiveView() )
446             {
447               Erase( theIO, true, false, view );
448               Display( theIO, updateViewer, view );
449             }
450           }
451         }
452       }
453     }
454   }
455 }
456
457 //=================================================================
458 /*!
459  *  GEOM_Displayer::Display
460  *  Calls Display() method for each object in the given list
461  */
462 //=================================================================
463 void GEOM_Displayer::Display( const SALOME_ListIO& theIOList, const bool updateViewer )
464 {
465   SALOME_ListIteratorOfListIO Iter( theIOList );
466   for ( ; Iter.More(); Iter.Next() ) {
467     Display( Iter.Value(), false );
468   }
469   if ( updateViewer )
470     UpdateViewer();
471 }
472
473 //=================================================================
474 /*!
475  *  GEOM_Displayer::Erase
476  *  Calls Erase() method for each object in the given list
477  */
478 //=================================================================
479 void GEOM_Displayer::Erase( const SALOME_ListIO& theIOList,
480                             const bool forced,
481                             const bool updateViewer )
482 {
483   SALOME_ListIteratorOfListIO Iter( theIOList );
484   for ( ; Iter.More(); Iter.Next() )
485     Erase( Iter.Value(), forced, false );
486
487   if ( updateViewer )
488     UpdateViewer();
489 }
490
491 //=================================================================
492 /*!
493  *  GEOM_Displayer::Redisplay
494  *  Calls Redisplay() method for each object in the given list
495  */
496 //=================================================================
497 void GEOM_Displayer::Redisplay( const SALOME_ListIO& theIOList, const bool updateViewer )
498 {
499   SALOME_ListIteratorOfListIO Iter( theIOList );
500   for ( ; Iter.More(); Iter.Next() )
501     Redisplay( Iter.Value(), false );
502
503   if ( updateViewer )
504     UpdateViewer();
505 }
506
507 //=================================================================
508 /*!
509  *  GEOM_Displayer::Update
510  *  Update OCC presentaion
511  *  [ Reimplemented from SALOME_Displayer ]
512  */
513 //=================================================================
514 void GEOM_Displayer::Update( SALOME_OCCPrs* prs )
515 {
516   SOCC_Prs* occPrs = dynamic_cast<SOCC_Prs*>( prs );
517   if ( !occPrs )
518     return;
519
520   if ( myType == GEOM_MARKER && !myShape.IsNull() && myShape.ShapeType() == TopAbs_FACE )
521   {
522     TopoDS_Face aFace = TopoDS::Face( myShape );
523     Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast( BRep_Tool::Surface( aFace ) );
524     if ( !aPlane.IsNull() )
525     {
526       gp_Ax3 aPos = aPlane->Pln().Position();
527       Handle(Geom_Axis2Placement) aPlc = new Geom_Axis2Placement( aPos.Ax2() );
528
529       Handle(GEOM_AISTrihedron) aTrh;
530
531       if ( occPrs->IsNull() )
532       {
533         aTrh = new GEOM_AISTrihedron( aPlc );
534
535         if ( HasColor() )
536           aTrh->SetColor( (Quantity_NameOfColor)GetColor() );
537
538         if ( HasWidth() )
539           aTrh->SetWidth( GetWidth() );
540
541         if ( !myIO.IsNull() )
542         {
543           aTrh->setIO( myIO );
544           aTrh->SetOwner( myIO );
545         }
546
547         occPrs->AddObject( aTrh );
548       }
549       else
550       {
551         AIS_ListOfInteractive aList;
552         occPrs->GetObjects( aList );
553         AIS_ListIteratorOfListOfInteractive anIter( aList );
554         for ( ; anIter.More(); anIter.Next() )
555         {
556           aTrh = Handle(GEOM_AISTrihedron)::DownCast( anIter.Value() );
557           if ( !aTrh.IsNull() )
558           {
559             aTrh->SetComponent( aPlc );
560             aTrh->SetToUpdate();
561           }
562         }
563       }
564
565       occPrs->SetToActivate( ToActivate() );
566     }
567   }
568   else
569   {
570     // if presentation is empty we try to create new one
571     if ( occPrs->IsNull() )
572     {
573       if ( !myShape.IsNull() )
574       {
575         //Handle(GEOM_AISShape) AISShape = new GEOM_AISShape( myShape, "" );
576         Handle(GEOM_AISShape) AISShape;
577         if (myType == GEOM_VECTOR)
578           AISShape = new GEOM_AISVector (myShape, "");
579         else {
580           if (myShape.ShapeType() != TopAbs_VERTEX && // fix pb with not displayed points
581               !TopoDS_Iterator(myShape).More())
582             return;// NPAL15983 (Bug when displaying empty groups)
583           AISShape = new GEOM_AISShape (myShape, "");
584         }
585         // Temporary staff: vertex must be infinite for correct visualization
586         AISShape->SetInfiniteState( myShape.Infinite() ); // || myShape.ShapeType() == TopAbs_VERTEX // VSR: 05/04/2010: Fix 20668 (Fit All for points & lines)
587
588         // Setup shape properties here ..., e.g. display mode, color, transparency, etc
589         AISShape->SetDisplayMode( myDisplayMode );
590         AISShape->SetShadingColor( myShadingColor );
591
592         // Set color and number for iso lines
593         SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
594         QColor col = aResMgr->colorValue( "Geometry", "isos_color",
595                                           QColor(int(0.5*255), int(0.5*255), int(0.5*255)) );
596         Quantity_Color aColor = SalomeApp_Tools::color( col );
597         int anUIsoNumber = aResMgr->integerValue("OCCViewer", "iso_number_u", 1);
598         int aVIsoNumber  = aResMgr->integerValue("OCCViewer", "iso_number_v", 1);
599
600         Handle(Prs3d_IsoAspect) anAspect = AISShape->Attributes()->UIsoAspect();
601         anAspect->SetNumber( anUIsoNumber );
602         anAspect->SetColor( aColor );
603         AISShape->Attributes()->SetUIsoAspect( anAspect );
604
605         anAspect = AISShape->Attributes()->VIsoAspect();
606         anAspect->SetNumber( aVIsoNumber );
607         anAspect->SetColor( aColor );
608         AISShape->Attributes()->SetVIsoAspect( anAspect );
609
610         if ( HasColor() )
611         {
612           AISShape->SetColor( (Quantity_NameOfColor)GetColor() );
613           Handle(Prs3d_PointAspect) anAspect = AISShape->Attributes()->PointAspect();
614           anAspect->SetColor( (Quantity_NameOfColor)GetColor() );
615           if ( myShape.ShapeType() == TopAbs_VERTEX || isCompoundOfVertices( myShape ) )
616           {
617             anAspect->SetScale( myScaleOfMarker );
618             anAspect->SetTypeOfMarker( myTypeOfMarker );
619           }
620           AISShape->Attributes()->SetPointAspect( anAspect );
621         }
622         else
623         {
624           if ( myShape.ShapeType() == TopAbs_VERTEX || isCompoundOfVertices( myShape ) )
625           {
626             col = aResMgr->colorValue( "Geometry", "point_color", QColor( 255, 255, 0 ) );
627             aColor = SalomeApp_Tools::color( col );
628             
629             Handle(Prs3d_PointAspect) anAspect = AISShape->Attributes()->PointAspect();
630             anAspect->SetColor( aColor );
631             anAspect->SetScale( myScaleOfMarker );
632             anAspect->SetTypeOfMarker( myTypeOfMarker );
633             AISShape->Attributes()->SetPointAspect( anAspect );
634           }
635           else
636           {
637             // Set line aspect
638             col = aResMgr->colorValue( "Geometry", "wireframe_color", QColor( 255, 255, 0 ) );
639             aColor = SalomeApp_Tools::color( col );
640             
641             Handle(Prs3d_LineAspect) anAspect = AISShape->Attributes()->LineAspect();
642             anAspect->SetColor( aColor );
643             AISShape->Attributes()->SetLineAspect( anAspect );
644             
645             // Set unfree boundaries aspect
646             anAspect = AISShape->Attributes()->UnFreeBoundaryAspect();
647             anAspect->SetColor( aColor );
648             AISShape->Attributes()->SetUnFreeBoundaryAspect( anAspect );
649             
650             // Set free boundaries aspect
651             col = aResMgr->colorValue( "Geometry", "free_bound_color", QColor( 0, 255, 0 ) );
652             aColor = SalomeApp_Tools::color( col );
653             
654             anAspect = AISShape->Attributes()->FreeBoundaryAspect();
655             anAspect->SetColor( aColor );
656             AISShape->Attributes()->SetFreeBoundaryAspect( anAspect );
657             
658             // Set wire aspect
659             col = aResMgr->colorValue( "Geometry", "line_color", QColor( 255, 0, 0 ) );
660             aColor = SalomeApp_Tools::color( col );
661             
662             anAspect = AISShape->Attributes()->WireAspect();
663             anAspect->SetColor( aColor );
664             AISShape->Attributes()->SetWireAspect( anAspect );
665             
666             // bug [SALOME platform 0019868]
667             // Set deviation angle. Default one is 12 degrees (Prs3d_Drawer.cxx:18)
668             //AISShape->SetOwnDeviationAngle( 10*PI/180 );
669
670             // IMP 0020626
671             double aDC = aResMgr->doubleValue("Geometry", "deflection_coeff", 0.001);
672             aDC = std::max( aDC, DEFLECTION_MIN ); // to avoid to small values of the coefficient
673             AISShape->SetOwnDeviationCoefficient(aDC);
674           }
675         }
676
677         if ( HasWidth() )
678           AISShape->SetWidth( GetWidth() );
679
680         if ( !myIO.IsNull() )
681         {
682           AISShape->setIO( myIO );
683           AISShape->SetOwner( myIO );
684         }
685         else if ( !myName.empty() )
686         {
687           // Workaround to allow selection of temporary objects
688           static int tempId = 0;
689           char buf[50];
690           sprintf( buf, "TEMP_%d", tempId++ );
691           Handle( SALOME_InteractiveObject ) anObj =
692             new SALOME_InteractiveObject( buf, "GEOM", myName.c_str() );
693           AISShape->setIO( anObj );
694           AISShape->SetOwner( anObj );
695         }
696
697         // Get color and other properties from GEOM_Object
698         SUIT_Session* session = SUIT_Session::session();
699         SUIT_Application* app = session->activeApplication();
700         if ( app )
701         {
702           SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
703           if ( study )
704           {
705             Handle( SALOME_InteractiveObject ) anIO = AISShape->getIO();
706             if ( !anIO.IsNull() )
707             {
708               _PTR(SObject) SO ( study->studyDS()->FindObjectID( anIO->getEntry() ) );
709               if ( SO )
710               {
711                 // get CORBA reference to data object
712                 CORBA::Object_var object = GeometryGUI::ClientSObjectToObject(SO);
713                 if ( !CORBA::is_nil( object ) )
714                 {
715                   // downcast to GEOM object
716                   GEOM::GEOM_Object_var aGeomObject = GEOM::GEOM_Object::_narrow( object );
717                   if ( !aGeomObject->_is_nil() )
718                   {
719                     SALOMEDS::Color aSColor = aGeomObject->GetColor();
720                     bool hasColor = aSColor.R >= 0 && aSColor.G >= 0 && aSColor.B >= 0;
721                     if( !hasColor && aGeomObject->GetType() == GEOM_GROUP ) // auto color for group
722                     {
723                       GEOM::GEOM_Gen_var aGeomGen = GeometryGUI::GetGeomGen();
724                       GEOM::GEOM_IGroupOperations_var anOperations = aGeomGen->GetIGroupOperations( study->id() );
725                       GEOM::GEOM_Object_var aMainObject = anOperations->GetMainShape( aGeomObject );
726                       if ( !aMainObject->_is_nil() && aMainObject->GetAutoColor() )
727                       {
728                         QList<SALOMEDS::Color> aReservedColors;
729
730                         SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( app );
731                         CORBA::String_var IOR = app->orb()->object_to_string( aMainObject );
732                         if ( strcmp(IOR.in(), "") != 0 )
733                         {
734                           _PTR(Study) aStudy = study->studyDS();
735                           _PTR(SObject) aMainSObject( aStudy->FindObjectIOR( std::string(IOR) ) );
736                           _PTR(ChildIterator) it( aStudy->NewChildIterator( aMainSObject ) );
737                           for( ; it->More(); it->Next() )
738                           {
739                             _PTR(SObject) aChildSObject( it->Value() );
740                             GEOM::GEOM_Object_var aChildObject =
741                               GEOM::GEOM_Object::_narrow(GeometryGUI::ClientSObjectToObject(aChildSObject));
742                             if( CORBA::is_nil( aChildObject ) )
743                               continue;
744
745                             if( aChildObject->GetType() != GEOM_GROUP )
746                               continue;
747
748                             SALOMEDS::Color aReservedColor = aChildObject->GetColor();
749                             aReservedColors.append( aReservedColor );
750                           }
751                         }
752
753                         aSColor = getUniqueColor( aReservedColors );
754                         hasColor = true;
755                       }
756                     }
757
758                     if( hasColor )
759                     {
760                       Quantity_Color aQuanColor( aSColor.R, aSColor.G, aSColor.B, Quantity_TOC_RGB );
761                       AISShape->SetColor( aQuanColor );
762                       AISShape->SetShadingColor( aQuanColor );
763                       if ( myShape.ShapeType() == TopAbs_VERTEX || isCompoundOfVertices( myShape ) ) {
764                         Handle(Prs3d_PointAspect) anAspect = AISShape->Attributes()->PointAspect();
765                         anAspect->SetColor( aQuanColor );
766                         anAspect->SetScale( myScaleOfMarker );
767                         anAspect->SetTypeOfMarker( myTypeOfMarker );
768                         AISShape->Attributes()->SetPointAspect( anAspect );
769                       }
770                     }
771                     // ... marker type
772                     GEOM::marker_type aType = aGeomObject->GetMarkerType();
773                     GEOM::marker_size aSize = aGeomObject->GetMarkerSize();
774                     if ( aType > GEOM::MT_NONE && aType < GEOM::MT_USER && aSize > GEOM::MS_NONE && aSize <= GEOM::MS_70 ) {
775                       Aspect_TypeOfMarker aMType = (Aspect_TypeOfMarker)( (int)aType-1 );
776                       double aMSize = ((int)aSize+1)*0.5;
777                       Handle(Prs3d_PointAspect) anAspect = AISShape->Attributes()->PointAspect();
778                       anAspect->SetScale( aMSize );
779                       anAspect->SetTypeOfMarker( aMType );
780                       Quantity_Color aQuanColor = SalomeApp_Tools::color( aResMgr->colorValue( "Geometry", "point_color", QColor( 255, 255, 0 ) ) );
781                       if ( hasColor )
782                         aQuanColor = Quantity_Color( aSColor.R, aSColor.G, aSColor.B, Quantity_TOC_RGB );
783                       anAspect->SetColor( aQuanColor );
784                       AISShape->Attributes()->SetPointAspect( anAspect );
785                     }
786                     else if ( aType == GEOM::MT_USER ) {
787                       int aTextureId = aGeomObject->GetMarkerTexture();
788                       Quantity_Color aQuanColor = SalomeApp_Tools::color( aResMgr->colorValue( "Geometry", "point_color", QColor( 255, 255, 0 ) ) );
789                       if ( hasColor ) aQuanColor = Quantity_Color( aSColor.R, aSColor.G, aSColor.B, Quantity_TOC_RGB );
790                       Standard_Integer aWidth, aHeight;
791                       Handle(Graphic3d_HArray1OfBytes) aTexture = GeometryGUI::getTexture( getStudy(), aTextureId, aWidth, aHeight );
792                       if ( !aTexture.IsNull() ) {
793                         static int TextureId = 0;
794                         Handle(Prs3d_PointAspect) aTextureAspect = new Prs3d_PointAspect(aQuanColor,
795                                                                                          ++TextureId,
796                                                                                          aWidth, aHeight,
797                                                                                          aTexture );
798                         AISShape->Attributes()->SetPointAspect( aTextureAspect );
799                       }
800                     }
801                   }
802                 }
803               }
804             }
805           }
806         }
807
808         // AISShape->SetName(???); ??? necessary to set name ???
809         occPrs->AddObject( AISShape );
810
811         // In accordance with ToActivate() value object will be activated/deactivated
812         // when it will be displayed
813         occPrs->SetToActivate( ToActivate() );
814       }
815     }
816     // if presentation is found -> set again shape for it
817     else
818     {
819       if ( !myShape.IsNull() )
820       {
821         AIS_ListOfInteractive IOList;
822         occPrs->GetObjects( IOList );
823         AIS_ListIteratorOfListOfInteractive Iter( IOList );
824         for ( ; Iter.More(); Iter.Next() )
825         {
826           Handle(GEOM_AISShape) AISShape = Handle(GEOM_AISShape)::DownCast( Iter.Value() );
827           if ( AISShape.IsNull() )
828             continue;
829           if ( AISShape->Shape() != myShape )
830           {
831             AISShape->Set( myShape );
832             AISShape->UpdateSelection();
833             AISShape->SetToUpdate();
834           }
835           if ( !myIO.IsNull() )
836           {
837             AISShape->setIO( myIO );
838             AISShape->SetOwner( myIO );
839           }
840         }
841       }
842     }
843   }
844 }
845
846 //=================================================================
847 /*!
848  *  GEOM_Displayer::Update
849  *  Update VTK presentaion
850  *  [ Reimplemented from SALOME_Displayer ]
851  */
852 //=================================================================
853 void GEOM_Displayer::Update( SALOME_VTKPrs* prs )
854 {
855   SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
856   if ( !vtkPrs || myShape.IsNull() )
857     return;
858
859   vtkActorCollection* theActors = 0;
860
861   if ( myType == GEOM_MARKER && myShape.ShapeType() == TopAbs_FACE )
862   {
863     //myToActivate = false; // ouv: commented to make the trihedron pickable (see IPAL18657)
864     GEOM_VTKTrihedron* aTrh = GEOM_VTKTrihedron::New();
865
866     if ( HasColor() )
867     {
868       Quantity_Color aColor( (Quantity_NameOfColor)GetColor() );
869       aTrh->SetColor( aColor.Red(), aColor.Green(), aColor.Blue() );
870     }
871
872     Handle(Geom_Plane) aPlane =
873       Handle(Geom_Plane)::DownCast( BRep_Tool::Surface( TopoDS::Face( myShape ) ) );
874     if ( aPlane.IsNull() )
875       return;
876
877     gp_Ax2 anAx2 = aPlane->Pln().Position().Ax2();
878     aTrh->SetPlacement( new Geom_Axis2Placement( anAx2 ) );
879
880 //    if ( SVTK_Viewer* vf = dynamic_cast<SVTK_Viewer*>( GetActiveView() ) )
881 //      aTrh->SetSize( 0.5 * vf->GetTrihedronSize() );
882
883     vtkPrs->AddObject( aTrh );
884
885     theActors = vtkActorCollection::New();
886     theActors->AddItem( aTrh );
887   }
888   else
889   {
890     theActors = GEOM_AssemblyBuilder::BuildActors( myShape, 0, 0, Standard_True, myType == GEOM_VECTOR );
891   }
892
893   theActors->InitTraversal();
894
895   vtkActor* anActor = (vtkActor*)theActors->GetNextActor();
896
897   vtkProperty* aProp = 0;
898
899   if ( HasColor() || HasWidth() )
900   {
901     aProp = vtkProperty::New();
902     aProp->SetRepresentationToWireframe();
903   }
904
905   if ( HasColor() )
906   {
907     Quantity_Color aColor( (Quantity_NameOfColor)GetColor() );
908     aProp->SetColor( aColor.Red(), aColor.Green(), aColor.Blue() );
909   }
910
911   if ( HasWidth() )
912   {
913     aProp->SetLineWidth( GetWidth() );
914   }
915
916   while ( anActor != NULL )
917   {
918     SALOME_Actor* GActor = SALOME_Actor::SafeDownCast( anActor );
919
920     GActor->setIO( myIO );
921
922     if ( aProp )
923     {
924       GActor->SetProperty( aProp );
925       GActor->SetPreviewProperty( aProp );
926
927       GEOM_Actor* aGeomGActor = GEOM_Actor::SafeDownCast( anActor );
928       if ( aGeomGActor != 0 )
929       {
930         aGeomGActor->SetShadingProperty( aProp );
931         aGeomGActor->SetWireframeProperty( aProp );
932       }
933     }
934
935     if ( myToActivate )
936       GActor->PickableOn();
937     else
938       GActor->PickableOff();
939
940     vtkPrs->AddObject( GActor );
941
942     anActor = (vtkActor*)theActors->GetNextActor();
943   }
944
945   if ( aProp )
946     aProp->Delete();
947
948   theActors->Delete();
949 }
950
951 //=================================================================
952 /*!
953  *  GEOM_Displayer::BuildPrs
954  *  Build presentation accordint to the current viewer type
955  */
956 //=================================================================
957 SALOME_Prs* GEOM_Displayer::BuildPrs( GEOM::GEOM_Object_ptr theObj )
958 {
959   if ( theObj->_is_nil() )
960     return 0;
961
962   myViewFrame = GetActiveView();
963   if ( myViewFrame == 0 )
964     return 0;
965
966   SALOME_Prs* aPrs = myViewFrame->CreatePrs();
967   if ( aPrs == 0 )
968     return 0;
969
970   internalReset();
971   setShape( GEOM_Client::get_client().GetShape( GeometryGUI::GetGeomGen(), theObj ) );
972   myType = theObj->GetType();
973
974   // Update presentation
975   UpdatePrs( aPrs );
976
977   return aPrs;
978 }
979
980 //=================================================================
981 /*!
982  *  GEOM_Displayer::BuildPrs
983  *  Build presentation accordint to the current viewer type
984  */
985 //=================================================================
986 SALOME_Prs* GEOM_Displayer::BuildPrs( const TopoDS_Shape& theShape )
987 {
988   myViewFrame = GetActiveView();
989   if ( theShape.IsNull() || myViewFrame == 0 )
990     return 0;
991
992   SALOME_Prs* aPrs = myViewFrame->CreatePrs();
993   if ( aPrs == 0 )
994     return 0;
995
996   internalReset();
997   setShape( theShape );
998   myType = -1;
999
1000   UpdatePrs( aPrs );
1001
1002   return aPrs;
1003 }
1004
1005 //=================================================================
1006 /*!
1007  *  GEOM_Displayer::buildPresentation
1008  *  Builds/finds object's presentation for the current viewer
1009  *  Calls corresponding Update() method by means of double dispatch
1010  *  [ internal ]
1011  */
1012 //=================================================================
1013 SALOME_Prs* GEOM_Displayer::buildPresentation( const QString& entry,
1014                                                SALOME_View* theViewFrame )
1015 {
1016   SALOME_Prs* prs = 0;
1017   internalReset();
1018
1019   myViewFrame = theViewFrame ? theViewFrame : GetActiveView();
1020
1021   if ( myViewFrame )
1022   {
1023     prs = LightApp_Displayer::buildPresentation( entry, theViewFrame );
1024     if ( prs )
1025     {
1026       Handle( SALOME_InteractiveObject ) theIO = new SALOME_InteractiveObject();
1027       theIO->setEntry( entry.toLatin1().constData() );
1028       if ( !theIO.IsNull() )
1029       {
1030         // set interactive object
1031         setIO( theIO );
1032         //  Find SOBject (because shape should be published previously)
1033         SUIT_Session* session = SUIT_Session::session();
1034         SUIT_Application* app = session->activeApplication();
1035         if ( app )
1036         {
1037           SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
1038           if ( study )
1039           {
1040             _PTR(SObject) SO ( study->studyDS()->FindObjectID( theIO->getEntry() ) );
1041             if ( SO )
1042             {
1043               // get CORBA reference to data object
1044               CORBA::Object_var object = GeometryGUI::ClientSObjectToObject(SO);
1045               if ( !CORBA::is_nil( object ) )
1046               {
1047                 // downcast to GEOM object
1048                 GEOM::GEOM_Object_var GeomObject = GEOM::GEOM_Object::_narrow( object );
1049                 if ( !GeomObject->_is_nil() )
1050                 {
1051                   // finally set shape
1052                   setShape( GEOM_Client::get_client().GetShape( GeometryGUI::GetGeomGen(), GeomObject ) );
1053                   myType = GeomObject->GetType();
1054                 }
1055               }
1056             }
1057           }
1058         }
1059       }
1060       UpdatePrs( prs );  // Update presentation by using of the double dispatch
1061     }
1062   }
1063   return prs;
1064 }
1065
1066 //=================================================================
1067 /*!
1068  *  GEOM_Displayer::buildSubshapePresentation
1069  *  Builds/finds object's presentation for the current viewer
1070  *  Calls corresponding Update() method by means of double dispatch
1071  *  For not published objects (for Mantis issue 0020435)
1072  */
1073 //=================================================================
1074 SALOME_Prs* GEOM_Displayer::buildSubshapePresentation(const TopoDS_Shape& aShape,
1075                                                       const QString& entry,
1076                                                       SALOME_View* theViewFrame)
1077 {
1078   SALOME_Prs* prs = 0;
1079   internalReset();
1080
1081   myViewFrame = theViewFrame ? theViewFrame : GetActiveView();
1082
1083   if (myViewFrame)
1084   {
1085     prs = LightApp_Displayer::buildPresentation(entry, theViewFrame);
1086     if (prs)
1087     {
1088       Handle(SALOME_InteractiveObject) theIO = new SALOME_InteractiveObject();
1089       theIO->setEntry(entry.toLatin1().constData());
1090       if (!theIO.IsNull())
1091       {
1092         // set interactive object
1093         setIO(theIO);
1094         // finally set shape
1095         setShape(aShape);
1096         myType = GEOM_SUBSHAPE;
1097       }
1098       UpdatePrs(prs);  // Update presentation by using of the double dispatch
1099     }
1100   }
1101   return prs;
1102 }
1103
1104 //=================================================================
1105 /*!
1106  *  GEOM_Displayer::internalReset
1107  *  Resets internal data
1108  *  [internal]
1109  */
1110 //=================================================================
1111 void GEOM_Displayer::internalReset()
1112 {
1113   myIO.Nullify();
1114   myShape.Nullify();
1115 }
1116
1117 //=================================================================
1118 /*!
1119  *  GEOM_Displayer::LocalSelection
1120  *  Activate selection of CAD shapes with activisation of selection
1121  *  of their sub-shapes (with opened local context for OCC viewer)
1122  */
1123 //=================================================================
1124 void GEOM_Displayer::LocalSelection( const Handle(SALOME_InteractiveObject)& theIO, const int theMode )
1125 {
1126   SUIT_Session* session = SUIT_Session::session();
1127   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
1128   if ( !app )
1129     return;
1130
1131   LightApp_SelectionMgr* sm = app->selectionMgr();
1132
1133   // remove all filters from selection
1134   sm->clearFilters();
1135
1136   SALOME_View* vf = GetActiveView();
1137   if ( vf ) {
1138     if (!theIO.IsNull() && !vf->isVisible(theIO))
1139       Display(theIO);
1140     SALOME_Prs* prs = vf->CreatePrs( theIO.IsNull() ? 0 : theIO->getEntry() );
1141     vf->LocalSelection( prs, theMode );
1142     delete prs;  // delete presentation because displayer is its owner
1143   }
1144 }
1145
1146 //=================================================================
1147 /*!
1148  *  GEOM_Displayer::globalSelection
1149  *  Activate selection of CAD shapes without activisation of selection
1150  *  of their sub-shapes (without opened local context for OCC viewer)
1151  */
1152 //=================================================================
1153 void GEOM_Displayer::GlobalSelection( const int theMode, const bool update )
1154 {
1155   TColStd_MapOfInteger aModes;
1156   aModes.Add( theMode );
1157   GlobalSelection( aModes, update );
1158 }
1159
1160 //=================================================================
1161 /*!
1162  *  GEOM_Displayer::globalSelection
1163  *  Activate selection of CAD shapes without activisation of selection
1164  *  of their sub-shapes (without opened local context for OCC viewer)
1165  */
1166 //=================================================================
1167 void GEOM_Displayer::GlobalSelection( const TColStd_MapOfInteger& theModes,
1168                                       const bool update, const QList<int>* theSubShapes )
1169 {
1170   SUIT_Session* session = SUIT_Session::session();
1171   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
1172   if ( !app )
1173     return;
1174
1175   SALOME_View* vf = GetActiveView();
1176   if ( vf == 0 )
1177     return;
1178
1179   // Close local context
1180   vf->GlobalSelection( update );
1181
1182   // Set selection filters in accordance with current mode
1183   LightApp_SelectionMgr* sm = app->selectionMgr();
1184   if ( !sm )
1185     return;
1186
1187   // Remove from selection temporary objects if necessary
1188   if ( !theModes.Contains( GEOM_PREVIEW ) )
1189     clearTemporary( sm );
1190
1191   //@ aSel->ClearIndex();
1192
1193   sm->clearFilters();
1194
1195   // Remove filters from AIS_InteractiveContext
1196   Handle(AIS_InteractiveContext) ic;
1197   SOCC_Viewer* viewer = dynamic_cast<SOCC_Viewer*>( vf );
1198   if ( viewer )
1199     {
1200       ic = viewer->getAISContext();
1201       if ( !ic.IsNull() )
1202         ic->RemoveFilters();
1203     }
1204
1205   if ( theModes.Contains( GEOM_ALLOBJECTS ) )
1206     return;
1207
1208   SUIT_SelectionFilter* aFilter;
1209   if ( theModes.Extent() == 1 )
1210     {
1211       int aMode = TColStd_MapIteratorOfMapOfInteger( theModes ).Key();
1212       
1213       if ( aMode == GEOM_COMPOUNDFILTER )
1214         aFilter = getComplexFilter( theSubShapes );
1215       else    
1216         aFilter = getFilter( aMode );
1217     }
1218   else if ( theModes.Extent() > 1 )
1219     {
1220       TColStd_MapOfInteger aTopAbsModes;
1221       TColStd_MapIteratorOfMapOfInteger anIter( theModes );
1222       QList<SUIT_SelectionFilter*> aListOfFilters;
1223       for ( ; anIter.More(); anIter.Next() )
1224         {
1225           SUIT_SelectionFilter* aFilter;
1226           int aMode = anIter.Key();
1227           if ( aMode == GEOM_COMPOUNDFILTER )
1228             aFilter = getComplexFilter( theSubShapes );
1229           else    
1230             aFilter = getFilter( aMode );
1231
1232           if ( aFilter )
1233             aListOfFilters.append( aFilter );
1234         }
1235
1236       aFilter = new GEOM_LogicalFilter( aListOfFilters, GEOM_LogicalFilter::LO_OR );
1237     }
1238   else
1239     return;
1240
1241   if ( aFilter )
1242     {
1243       sm->installFilter( aFilter );
1244       if ( !ic.IsNull() )
1245         {
1246           Handle(GEOM_OCCFilter) anOCCFilter = new GEOM_OCCFilter( sm );
1247           ic->AddFilter( anOCCFilter );
1248         }
1249     }
1250 }
1251
1252 //=================================================================
1253 /*!
1254  *  GEOM_Displayer::LocalSelection
1255  *  Activate selection of CAD shapes with activisation of selection
1256  *  of their sub-shapes (with opened local context for OCC viewer)
1257  */
1258 //=================================================================
1259 void GEOM_Displayer::LocalSelection( const SALOME_ListIO& theIOList, const int theMode )
1260 {
1261   SALOME_ListIteratorOfListIO Iter( theIOList );
1262   for ( ; Iter.More(); Iter.Next() )
1263     LocalSelection( Iter.Value(), theMode );
1264 }
1265
1266 //=================================================================
1267 /*!
1268  *  GEOM_Displayer::BeforeDisplay
1269  *  Called before displaying of pars. Close local context
1270  *  [ Reimplemented from SALOME_Displayer ]
1271  */
1272 //=================================================================
1273 void GEOM_Displayer::BeforeDisplay( SALOME_View* v, const SALOME_OCCViewType& )
1274 {
1275   SOCC_Viewer* vf = dynamic_cast<SOCC_Viewer*>( v );
1276   if ( vf )
1277   {
1278     Handle(AIS_InteractiveContext) ic = vf->getAISContext();
1279     if ( !ic.IsNull() )
1280     {
1281       if ( ic->HasOpenedContext() )
1282       ic->CloseAllContexts();
1283     }
1284   }
1285 }
1286
1287 void GEOM_Displayer::AfterDisplay( SALOME_View*, const SALOME_OCCViewType& )
1288 {
1289 }
1290
1291
1292 //=================================================================
1293 /*!
1294  *  GEOM_Displayer::SetColor
1295  *  Set color for shape displaying. If it is equal -1 then default color is used.
1296  *  Available values are from Quantity_NameOfColor enumeration
1297  */
1298 //=================================================================
1299 void GEOM_Displayer::SetColor( const int color )
1300 {
1301   if ( color == -1 )
1302     UnsetColor();
1303   else
1304   {
1305     myColor = color;
1306     myShadingColor = Quantity_Color( (Quantity_NameOfColor)color );
1307   }
1308 }
1309
1310 int GEOM_Displayer::GetColor() const
1311 {
1312   return myColor;
1313 }
1314
1315 bool GEOM_Displayer::HasColor() const
1316 {
1317   return myColor != -1;
1318 }
1319
1320 void GEOM_Displayer::UnsetColor()
1321 {
1322   myColor = -1;
1323   
1324   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
1325   QColor col = resMgr->colorValue( "Geometry", "shading_color", QColor( 255, 0, 0 ) );
1326   myShadingColor = SalomeApp_Tools::color( col );
1327 }
1328
1329 //=================================================================
1330 /*!
1331  *  GEOM_Displayer::SetWidth
1332  *  Set width of shape displaying. If it is equal -1 then default width is used.
1333  */
1334 //=================================================================
1335 void GEOM_Displayer::SetWidth( const double width )
1336 {
1337   myWidth = width;
1338 }
1339
1340 double GEOM_Displayer::GetWidth() const
1341 {
1342   return myWidth;
1343 }
1344
1345 bool GEOM_Displayer::HasWidth() const
1346 {
1347   return myWidth != -1;
1348 }
1349
1350 void GEOM_Displayer::UnsetWidth()
1351 {
1352   myWidth = -1;
1353 }
1354
1355 //=================================================================
1356 /*!
1357  *  GEOM_Displayer::SetToActivate
1358  *  This method is used for activisation/deactivisation of objects to be displayed
1359  */
1360 //=================================================================
1361 void GEOM_Displayer::SetToActivate( const bool toActivate )
1362 {
1363   myToActivate = toActivate;
1364 }
1365 bool GEOM_Displayer::ToActivate() const
1366 {
1367   return myToActivate;
1368 }
1369
1370 //=================================================================
1371 /*!
1372  *  GEOM_Displayer::clearTemporary
1373  *  Removes from selection temporary objects
1374  */
1375 //=================================================================
1376 void GEOM_Displayer::clearTemporary( LightApp_SelectionMgr* theSelMgr )
1377 {
1378   SALOME_ListIO selected, toSelect;
1379   theSelMgr->selectedObjects( selected );
1380
1381   for (  SALOME_ListIteratorOfListIO it( selected ) ; it.More(); it.Next() ) {
1382     Handle(SALOME_InteractiveObject) io = it.Value();
1383     if ( !io.IsNull() && io->hasEntry() && strncmp( io->getEntry(), "TEMP_", 5 ) != 0 )
1384       toSelect.Append( it.Value() );
1385   }
1386
1387   theSelMgr->setSelectedObjects( toSelect, true );
1388 }
1389
1390 void GEOM_Displayer::SetName( const char* theName )
1391 {
1392   myName = theName;
1393 }
1394
1395 void GEOM_Displayer::UnsetName()
1396 {
1397   myName = "";
1398 }
1399
1400 SalomeApp_Study* GEOM_Displayer::getStudy() const
1401 {
1402   return dynamic_cast<SalomeApp_Study*>( myApp->activeStudy() );
1403 }
1404
1405 void GEOM_Displayer::setIO( const Handle(SALOME_InteractiveObject)& theIO )
1406 {
1407   myIO = theIO;
1408 }
1409
1410 void GEOM_Displayer::setShape( const TopoDS_Shape& theShape )
1411 {
1412   myShape = theShape;
1413 }
1414
1415 bool GEOM_Displayer::canBeDisplayed( const QString& /*entry*/, const QString& viewer_type ) const
1416 {
1417   return viewer_type==SOCC_Viewer::Type() || viewer_type==SVTK_Viewer::Type();
1418 }
1419
1420 int GEOM_Displayer::SetDisplayMode( const int theMode )
1421 {
1422   int aPrevMode = myDisplayMode;
1423   if ( theMode != -1 )
1424     myDisplayMode = theMode;
1425   else
1426   {
1427     SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
1428     myDisplayMode = resMgr->integerValue( "Geometry", "display_mode", 0 );
1429   }
1430   return aPrevMode;
1431 }
1432
1433 int GEOM_Displayer::GetDisplayMode() const
1434 {
1435   return myDisplayMode;
1436 }
1437
1438 int GEOM_Displayer::UnsetDisplayMode()
1439 {
1440   int aPrevMode = myDisplayMode;
1441   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
1442   myDisplayMode = resMgr->integerValue( "Geometry", "display_mode", 0 );
1443   return aPrevMode;
1444 }
1445
1446 SALOMEDS::Color GEOM_Displayer::getUniqueColor( const QList<SALOMEDS::Color>& theReservedColors )
1447 {
1448   int aHue = -1;
1449   int aTolerance = 64;
1450   int anIterations = 0;
1451   int aPeriod = 5;
1452
1453   while( 1 )
1454   {
1455     anIterations++;
1456     if( anIterations % aPeriod == 0 )
1457     {
1458       aTolerance /= 2;
1459       if( aTolerance < 1 )
1460         break;
1461     }
1462     //std::cout << "Iteration N" << anIterations << " (tolerance=" << aTolerance << ")"<< std::endl;
1463
1464     aHue = (int)( 360.0 * rand() / RAND_MAX );
1465     //std::cout << "Hue = " << aHue << std::endl;
1466
1467     //std::cout << "Auto colors : ";
1468     bool ok = true;
1469     QList<SALOMEDS::Color>::const_iterator it = theReservedColors.constBegin();
1470     QList<SALOMEDS::Color>::const_iterator itEnd = theReservedColors.constEnd();
1471     for( ; it != itEnd; ++it )
1472     {
1473       SALOMEDS::Color anAutoColor = *it;
1474       QColor aQColor( (int)( anAutoColor.R * 255.0 ), (int)( anAutoColor.G * 255.0 ), (int)( anAutoColor.B * 255.0 ) );
1475
1476       int h, s, v;
1477       aQColor.getHsv( &h, &s, &v );
1478       //std::cout << h << " ";
1479       if( abs( h - aHue ) < aTolerance )
1480       {
1481         ok = false;
1482         //std::cout << "break (diff = " << abs( h - aHue ) << ")";
1483         break;
1484       }
1485     }
1486     //std::cout << std::endl;
1487
1488     if( ok )
1489       break;
1490   }
1491
1492   //std::cout << "Hue of the returned color = " << aHue << std::endl;
1493   QColor aColor;
1494   aColor.setHsv( aHue, 255, 255 );
1495
1496   SALOMEDS::Color aSColor;
1497   aSColor.R = (double)aColor.red() / 255.0;
1498   aSColor.G = (double)aColor.green() / 255.0;
1499   aSColor.B = (double)aColor.blue() / 255.0;
1500
1501   return aSColor;
1502 }