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