Salome HOME
Implementation of the "0021239: EDF 1829 OCC: Bring to front selected objects" issue.
[modules/geom.git] / src / GEOMGUI / GEOM_Displayer.cxx
1 // Copyright (C) 2007-2011  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_Constants.h>
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_AISShape.hxx>
41 #include <GEOM_AISVector.hxx>
42 #include <GEOM_AISTrihedron.hxx>
43 #include <GEOM_VTKTrihedron.hxx>
44
45 #include <Material_Model.h>
46
47 #include <SUIT_Desktop.h>
48 #include <SUIT_ViewWindow.h>
49 #include <SUIT_Session.h>
50 #include <SUIT_ViewManager.h>
51 #include <SUIT_ResourceMgr.h>
52
53 #include <Basics_OCCTVersion.hxx>
54
55 #include <SalomeApp_Study.h>
56 #include <SalomeApp_Application.h>
57 #include <LightApp_SelectionMgr.h>
58 #include <LightApp_DataObject.h>
59 #include <SalomeApp_TypeFilter.h>
60 #include <SalomeApp_Tools.h>
61
62 #include <SALOME_ListIteratorOfListIO.hxx>
63 #include <SALOME_ListIO.hxx>
64 #include <SALOME_Prs.h>
65
66 #include <SOCC_Prs.h>
67 #include <SOCC_ViewModel.h>
68
69 #include <SVTK_Prs.h>
70 #include <SVTK_ViewModel.h>
71
72 // OCCT Includes
73 #include <AIS_Drawer.hxx>
74 #include <AIS_ListIteratorOfListOfInteractive.hxx>
75 #include <Prs3d_IsoAspect.hxx>
76 #include <Prs3d_PointAspect.hxx>
77 #include <StdSelect_TypeOfEdge.hxx>
78 #include <StdSelect_TypeOfFace.hxx>
79 #include <StdSelect_DisplayMode.hxx>
80 #include <TopoDS_Face.hxx>
81 #include <BRep_Tool.hxx>
82 #include <Geom_Plane.hxx>
83 #include <Geom_Axis2Placement.hxx>
84 #include <Graphic3d_AspectFillArea3d.hxx>
85 #include <gp_Pln.hxx>
86 #include <TColStd_MapOfInteger.hxx>
87 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
88 #include <TopoDS_Iterator.hxx>
89 #include <Graphic3d_AspectMarker3d.hxx>
90 #include <TopTools_MapOfShape.hxx>
91 #include <TopTools_ListOfShape.hxx>
92 #include <TopTools_ListIteratorOfListOfShape.hxx>
93 #include <TopoDS.hxx>
94
95 #include <Prs3d_ShadingAspect.hxx>
96
97 #include <BRepMesh_IncrementalMesh.hxx>
98
99 // VTK Includes
100 #include <vtkActorCollection.h>
101 #include <vtkProperty.h>
102
103 // CORBA Headers
104 #include CORBA_CLIENT_HEADER(SALOMEDS_Attributes)
105
106 #include <GEOMImpl_Types.hxx>
107
108 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
109 #include <TColStd_HArray1OfByte.hxx>
110 #else
111 #include <Graphic3d_HArray1OfBytes.hxx>
112 #endif
113
114 //================================================================
115 // Function : getActiveStudy
116 // Purpose  : Get active study, returns 0 if no open study frame
117 //================================================================
118 static inline SalomeApp_Study* getActiveStudy()
119 {
120   SUIT_Session* session = SUIT_Session::session();
121   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
122   if ( app )
123     return ( SalomeApp_Study* )app->activeStudy();
124   return 0;
125 }
126
127 static inline int getViewManagerId( SALOME_View* theViewFrame) {
128   SUIT_ViewModel* aModel = dynamic_cast<SUIT_ViewModel*>(theViewFrame);
129   SUIT_ViewManager* aViewMgr = 0;
130   if (aModel != 0)
131     aViewMgr = aModel->getViewManager();
132   return ((aViewMgr == 0) ? -1 :aViewMgr->getGlobalId());
133 }
134
135 //================================================================
136 // Function : getTopAbsMode
137 // Purpose  : Get TopAbs_ShapeEnum value corresponding to the
138 //            one from GEOMImpl_Types.h
139 //================================================================
140 static inline int getTopAbsMode( const int implType )
141 {
142   switch ( implType )
143   {
144     case GEOM_COMPOUND  : return TopAbs_COMPOUND;
145     case GEOM_SOLID     : return TopAbs_SOLID;
146     case GEOM_SHELL     : return TopAbs_SHELL;
147     case GEOM_FACE      : return TopAbs_FACE;
148     case GEOM_WIRE      : return TopAbs_WIRE;
149     case GEOM_EDGE      : return TopAbs_EDGE;
150     case GEOM_POINT     : return TopAbs_VERTEX;
151     default             : return -1;
152   }
153 }
154
155 int GEOM_Displayer::getMinMaxShapeType( const TopoDS_Shape& shape, bool ismin )
156 {
157   if ( shape.IsNull() )
158     return TopAbs_SHAPE;
159
160   int ret = shape.ShapeType();
161
162   if ( shape.ShapeType() == TopAbs_COMPOUND || shape.ShapeType() == TopAbs_COMPSOLID ) {
163     TopoDS_Iterator it(shape, Standard_True, Standard_False);
164     for (; it.More(); it.Next()) {
165       TopoDS_Shape sub_shape = it.Value();
166       if ( sub_shape.IsNull() ) continue;
167       int stype = getMinMaxShapeType( sub_shape, ismin );
168       if ( stype == TopAbs_SHAPE ) continue;
169       if ( ismin && stype > ret )
170         ret = stype;
171       else if ( !ismin && ( ret < TopAbs_SOLID || stype < ret ) )
172         ret = stype;
173     }
174   }
175
176   return ret;
177 }
178
179 bool GEOM_Displayer::isCompoundOfVertices( const TopoDS_Shape& theShape )
180 {
181   return theShape.ShapeType() == TopAbs_COMPOUND && getMinMaxShapeType( theShape, false ) == TopAbs_VERTEX;
182 }
183
184 //================================================================
185 // Function : getFilter
186 // Purpose  : Get filter corresponding to the type of object
187 //            from GEOMImpl_Types.h
188 //================================================================
189 SUIT_SelectionFilter* GEOM_Displayer::getFilter( const int theMode )
190 {
191   SUIT_SelectionFilter* aFilter;
192
193   int aTopAbsMode = getTopAbsMode( theMode );
194   if ( aTopAbsMode != -1 )
195     aFilter = new GEOM_TypeFilter( getStudy(), aTopAbsMode, true ); //@ aFilter = new GEOM_TypeFilter( ( TopAbs_ShapeEnum )aTopAbsMode );
196   else
197     switch ( theMode )
198       {
199       case GEOM_LINE      : aFilter = new GEOM_EdgeFilter( getStudy(), StdSelect_Line ); break;
200       case GEOM_CIRCLE    : aFilter = new GEOM_EdgeFilter( getStudy(), StdSelect_Circle ); break;
201
202       case GEOM_PLANE     : aFilter = new GEOM_FaceFilter( getStudy(), StdSelect_Plane ); break;
203       case GEOM_CYLINDER  : aFilter = new GEOM_FaceFilter( getStudy(), StdSelect_Cylinder ); break;
204       case GEOM_SPHERE    : aFilter = new GEOM_FaceFilter( getStudy(), StdSelect_Sphere ); break;
205       case GEOM_TORUS     : aFilter = new GEOM_FaceFilter( getStudy(), StdSelect_Torus ); break;
206       case GEOM_REVOLUTION: aFilter = new GEOM_FaceFilter( getStudy(), StdSelect_Revol ); break;
207       case GEOM_CONE      : aFilter = new GEOM_FaceFilter( getStudy(), StdSelect_Cone ); break;
208
209       case GEOM_PREVIEW   : aFilter = new GEOM_PreviewFilter( getStudy() ); break;
210
211       case GEOM_ALLSHAPES : aFilter = new GEOM_SelectionFilter(getStudy(), true ); break;
212       case GEOM_ALLGEOM   : aFilter = new SalomeApp_TypeFilter( getStudy(), "GEOM" ); break;
213
214       default             : aFilter = new GEOM_TypeFilter( getStudy(), theMode ); break;
215       }
216
217   return aFilter;
218 }
219
220 //================================================================
221 // Function : getComplexFilter
222 // Purpose  : Get compound filter corresponding to the type of
223 //            object from GEOMImpl_Types.h
224 //================================================================
225 SUIT_SelectionFilter* GEOM_Displayer::getComplexFilter( const QList<int>* aSubShapes)
226 {
227   GEOM_CompoundFilter* aFilter;
228
229   if(aSubShapes != NULL ) {
230     aFilter = new GEOM_CompoundFilter(getStudy());
231     QList<int> aTopAbsTypes;
232     QList<int>::const_iterator it;
233     for(it = aSubShapes->constBegin(); it != aSubShapes->constEnd(); ++it ) {
234       int topAbsMode = getTopAbsMode(*it);
235       if(topAbsMode != -1 )
236         aTopAbsTypes.append(topAbsMode);
237     }
238     aFilter->addSubTypes(aTopAbsTypes);
239   }
240
241   return aFilter;
242 }
243
244 //================================================================
245 // Function : getEntry
246 // Purpose  :
247 //================================================================
248 static std::string getEntry( 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) SO ( study->studyDS()->FindObjectIOR( std::string(IOR) ) );
259       if ( SO )
260         return SO->GetID();
261     }
262   }
263   return "";
264 }
265
266 //================================================================
267 // Function : getName
268 // Purpose  :
269 //================================================================
270 static std::string getName( GEOM::GEOM_Object_ptr object )
271 {
272   SUIT_Session* session = SUIT_Session::session();
273   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
274   if ( app )
275   {
276     CORBA::String_var IOR = app->orb()->object_to_string( object );
277     if ( strcmp(IOR.in(), "") != 0 )
278     {
279       SalomeApp_Study* study = ( SalomeApp_Study* )app->activeStudy();
280       _PTR(SObject) aSObj ( study->studyDS()->FindObjectIOR( std::string(IOR) ) );
281
282       _PTR(GenericAttribute) anAttr;
283
284       if ( aSObj && aSObj->FindAttribute( anAttr, "AttributeName") )
285       {
286         _PTR(AttributeName) aNameAttr( anAttr );
287         return aNameAttr->Value();
288       }
289     }
290   }
291
292   return "";
293 }
294
295 //=================================================================
296 /*!
297  *  GEOM_Displayer::GEOM_Displayer
298  *  Constructor
299  */
300 //=================================================================
301 GEOM_Displayer::GEOM_Displayer( SalomeApp_Study* st )
302 {
303   if( st )
304     myApp = dynamic_cast<SalomeApp_Application*>( st->application() );
305   else
306     myApp = 0;
307
308   /* Shading Color */
309   SUIT_Session* session = SUIT_Session::session();
310   SUIT_ResourceMgr* resMgr = session->resourceMgr();
311
312   QColor col = resMgr->colorValue( "Geometry", "shading_color", QColor( 255, 0, 0 ) );
313   myShadingColor = SalomeApp_Tools::color( col );
314
315   myDisplayMode = resMgr->integerValue("Geometry", "display_mode", 0);
316
317   int aType = resMgr->integerValue("Geometry", "type_of_marker", (int)Aspect_TOM_PLUS);
318   myWidth = resMgr->integerValue("Geometry", "edge_width", -1);
319   myIsosWidth = resMgr->integerValue("Geometry", "isolines_width", -1);
320
321   myTypeOfMarker = (Aspect_TypeOfMarker)(std::min((int)Aspect_TOM_RING3, std::max((int)Aspect_TOM_POINT, aType)));
322   myScaleOfMarker = (resMgr->integerValue("Geometry", "marker_scale", 1)-(int)GEOM::MS_10)*0.5 + 1.0;
323   myScaleOfMarker = std::min(7.0, std::max(1., myScaleOfMarker));
324
325   myColor = -1;
326   // This color is used for shape displaying. If it is equal -1 then
327   // default color is used.
328   myTexture = "";
329
330   myWidth = -1;
331   myType = -1;
332
333   myToActivate = true;
334   // This parameter is used for activisation/deactivisation of objects to be displayed
335   
336   #if OCC_VERSION_LARGE > 0x06050100 // Functionnality available only in OCCT 6.5.2
337   // Activate parallel vizualisation only for testing purpose  
338   // and if the corresponding env variable is set to 1 
339   char* parallel_visu = getenv("PARALLEL_VISU");
340   if (parallel_visu && atoi(parallel_visu))
341   {
342     MESSAGE("Parallel visualisation on");
343     BRepMesh_IncrementalMesh::SetParallelDefault(Standard_True);
344   }
345   #endif
346
347   myViewFrame = 0;
348 }
349
350 //=================================================================
351 /*!
352  *  GEOM_Displayer::~GEOM_Displayer
353  *  Destructor
354  */
355 //=================================================================
356 GEOM_Displayer::~GEOM_Displayer()
357 {
358 }
359
360 //=================================================================
361 /*!
362  *  GEOM_Displayer::Display
363  *  Display interactive object in the current viewer
364  */
365 //=================================================================
366 void GEOM_Displayer::Display( const Handle(SALOME_InteractiveObject)& theIO,
367                              const bool updateViewer,
368                              SALOME_View* theViewFrame )
369 {
370   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
371   if ( vf )
372   {
373     SALOME_Prs* prs = buildPresentation( theIO->getEntry(), vf );
374
375     if ( prs )
376     {
377       vf->BeforeDisplay( this, prs );
378       vf->Display( prs );
379       vf->AfterDisplay( this, prs );
380
381       if ( updateViewer )
382         vf->Repaint();
383
384       int aMgrId = getViewManagerId(vf);
385       SalomeApp_Study* aStudy = getStudy();
386       aStudy->setObjectProperty(aMgrId, theIO->getEntry(), VISIBILITY_PROP, 1 );
387
388       setVisibilityState(theIO->getEntry(), Qtx::ShownState);
389
390       delete prs;  // delete presentation because displayer is its owner
391     }
392   }
393 }
394
395 //=================================================================
396 /*!
397  *  GEOM_Displayer::Display
398  *  This overloaded Display() method can be useful for operations
399  *  not using dialog boxes.
400  */
401 //=================================================================
402 void GEOM_Displayer::Display( GEOM::GEOM_Object_ptr theObj, const bool updateViewer )
403 {
404   if ( theObj->_is_nil() )
405     return;
406
407   std::string entry = getEntry( theObj );
408   if ( entry != "" ) {
409     Display(new SALOME_InteractiveObject(entry.c_str(), "GEOM", getName(theObj).c_str()),
410             updateViewer);
411   }
412 }
413
414 //=================================================================
415 /*!
416  *  GEOM_Displayer::Erase
417  *  Erase interactive object in the current viewer
418  */
419 //=================================================================
420 void GEOM_Displayer::Erase( const Handle(SALOME_InteractiveObject)& theIO,
421                             const bool forced,
422                             const bool updateViewer,
423                             SALOME_View* theViewFrame )
424 {
425   if ( theIO.IsNull() )
426     return;
427
428   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
429
430   if ( vf ) {
431     SALOME_Prs* prs = vf->CreatePrs( theIO->getEntry() );
432     if ( prs ) {
433       vf->BeforeErase( this, prs );
434       vf->Erase( prs, forced );
435       vf->AfterErase( this, prs );
436       if ( updateViewer )
437         vf->Repaint();
438       delete prs;  // delete presentation because displayer is its owner
439
440       int aMgrId = getViewManagerId(vf);
441       SalomeApp_Study* aStudy = getStudy();
442       aStudy->setObjectProperty(aMgrId, theIO->getEntry(), VISIBILITY_PROP, 0 );
443
444       setVisibilityState(theIO->getEntry(), Qtx::HiddenState);
445     }
446   }
447 }
448
449 //=================================================================
450 /*!
451  *  GEOM_Displayer::Erase
452  *  Erase geometry object in the current viewer
453  */
454 //=================================================================
455 void GEOM_Displayer::Erase( GEOM::GEOM_Object_ptr theObj,
456                             const bool forced,
457                             const bool updateViewer )
458 {
459   std::string entry = getEntry( theObj );
460   if ( entry != "" )
461   {
462     Erase(new SALOME_InteractiveObject(entry.c_str(), "GEOM", getName(theObj).c_str()),
463           forced, updateViewer);
464   }
465 }
466
467 //=================================================================
468 /*!
469  *  GEOM_Displayer::Redisplay
470  *  Redisplay (erase and then display again) interactive object
471  *  in the current viewer
472  */
473 //=================================================================
474 void GEOM_Displayer::Redisplay( const Handle(SALOME_InteractiveObject)& theIO,
475                                 const bool updateViewer )
476 {
477   // Remove the object permanently (<forced> == true)
478   SUIT_Session* ses = SUIT_Session::session();
479   SUIT_Application* app = ses->activeApplication();
480   if ( app )
481   {
482     SUIT_Desktop* desk = app->desktop();
483     QList<SUIT_ViewWindow*> wnds = desk->windows();
484     SUIT_ViewWindow* wnd;
485     QListIterator<SUIT_ViewWindow*> it( wnds );
486     while ( it.hasNext() && (wnd = it.next()) )
487     {
488       SUIT_ViewManager* vman = wnd->getViewManager();
489       if ( vman )
490       {
491         SUIT_ViewModel* vmodel = vman->getViewModel();
492         if ( vmodel )
493         {
494           SALOME_View* view = dynamic_cast<SALOME_View*>(vmodel);
495           if ( view )
496           {
497             if ( view->isVisible( theIO ) || view == GetActiveView() )
498             {
499               Erase( theIO, true, false, view );
500               Display( theIO, updateViewer, view );
501             }
502           }
503         }
504       }
505     }
506   }
507 }
508
509 //=================================================================
510 /*!
511  *  GEOM_Displayer::Display
512  *  Calls Display() method for each object in the given list
513  */
514 //=================================================================
515 void GEOM_Displayer::Display( const SALOME_ListIO& theIOList, const bool updateViewer )
516 {
517   SALOME_ListIteratorOfListIO Iter( theIOList );
518   for ( ; Iter.More(); Iter.Next() ) {
519     Display( Iter.Value(), false );
520   }
521   if ( updateViewer )
522     UpdateViewer();
523 }
524
525 //=================================================================
526 /*!
527  *  GEOM_Displayer::Erase
528  *  Calls Erase() method for each object in the given list
529  */
530 //=================================================================
531 void GEOM_Displayer::Erase( const SALOME_ListIO& theIOList,
532                             const bool forced,
533                             const bool updateViewer )
534 {
535   SALOME_ListIteratorOfListIO Iter( theIOList );
536   for ( ; Iter.More(); Iter.Next() )
537     Erase( Iter.Value(), forced, false );
538
539   if ( updateViewer )
540     UpdateViewer();
541 }
542
543 //=================================================================
544 /*!
545  *  GEOM_Displayer::Redisplay
546  *  Calls Redisplay() method for each object in the given list
547  */
548 //=================================================================
549 void GEOM_Displayer::Redisplay( const SALOME_ListIO& theIOList, const bool updateViewer )
550 {
551   SALOME_ListIteratorOfListIO Iter( theIOList );
552   for ( ; Iter.More(); Iter.Next() )
553     Redisplay( Iter.Value(), false );
554
555   if ( updateViewer )
556     UpdateViewer();
557 }
558
559 //=================================================================
560 /*!
561  *  GEOM_Displayer::Update
562  *  Update OCC presentaion
563  *  [ Reimplemented from SALOME_Displayer ]
564  */
565 //=================================================================
566 void GEOM_Displayer::Update( SALOME_OCCPrs* prs )
567 {
568   SOCC_Prs* occPrs = dynamic_cast<SOCC_Prs*>( prs );
569   if ( !occPrs )
570     return;
571
572   if ( myType == GEOM_MARKER && !myShape.IsNull() && myShape.ShapeType() == TopAbs_FACE )
573   {
574     TopoDS_Face aFace = TopoDS::Face( myShape );
575     Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast( BRep_Tool::Surface( aFace ) );
576     if ( !aPlane.IsNull() )
577     {
578       gp_Ax3 aPos = aPlane->Pln().Position();
579       Handle(Geom_Axis2Placement) aPlc = new Geom_Axis2Placement( aPos.Ax2() );
580
581       Handle(GEOM_AISTrihedron) aTrh;
582
583       if ( occPrs->IsNull() )
584       {
585         aTrh = new GEOM_AISTrihedron( aPlc );
586
587         if ( HasColor() )
588           aTrh->SetColor( (Quantity_NameOfColor)GetColor() );
589
590         if ( HasWidth() )
591           aTrh->SetWidth( GetWidth() );
592
593         if ( !myIO.IsNull() )
594         {
595           aTrh->setIO( myIO );
596           aTrh->SetOwner( myIO );
597         }
598
599         occPrs->AddObject( aTrh );
600       }
601       else
602       {
603         AIS_ListOfInteractive aList;
604         occPrs->GetObjects( aList );
605         AIS_ListIteratorOfListOfInteractive anIter( aList );
606         for ( ; anIter.More(); anIter.Next() )
607         {
608           aTrh = Handle(GEOM_AISTrihedron)::DownCast( anIter.Value() );
609           if ( !aTrh.IsNull() )
610           {
611             aTrh->SetComponent( aPlc );
612             aTrh->SetToUpdate();
613           }
614         }
615       }
616
617       occPrs->SetToActivate( ToActivate() );
618     }
619   }
620   else
621   {
622     // if presentation is empty we try to create new one
623     if ( occPrs->IsNull() )
624     {
625       SalomeApp_Study* aStudy = getStudy();
626       if(!aStudy)
627         return;
628       if ( !myShape.IsNull() ) {
629
630         bool onlyVertex = (myShape.ShapeType() == TopAbs_VERTEX || isCompoundOfVertices( myShape ));
631
632         QString anEntry;
633         int aMgrId = -1;
634         if(!myIO.IsNull()) {
635           aMgrId = getViewManagerId(myViewFrame);
636           anEntry = myIO->getEntry();
637         }
638         bool useStudy = !anEntry.isEmpty() && aMgrId != -1;
639         bool useObjColor = false;
640         bool useObjMarker = false;
641
642         PropMap aPropMap;
643         PropMap aDefPropMap;
644
645         if(useStudy){
646           aPropMap = aStudy->getObjectPropMap(aMgrId,anEntry);
647           aDefPropMap = getDefaultPropertyMap(SOCC_Viewer::Type());
648           MergePropertyMaps(aPropMap, aDefPropMap);
649         }
650
651         //Handle(GEOM_AISShape) AISShape = new GEOM_AISShape( myShape, "" );
652         Handle(GEOM_AISShape) AISShape;
653         if (myType == GEOM_VECTOR)
654           AISShape = new GEOM_AISVector (myShape, "");
655         else {
656           if (myShape.ShapeType() != TopAbs_VERTEX && // fix pb with not displayed points
657               !TopoDS_Iterator(myShape).More())
658             return;// NPAL15983 (Bug when displaying empty groups)
659           AISShape = new GEOM_AISShape (myShape, "");
660         }
661         // Temporary staff: vertex must be infinite for correct visualization
662         AISShape->SetInfiniteState( myShape.Infinite() ); // || myShape.ShapeType() == TopAbs_VERTEX // VSR: 05/04/2010: Fix 20668 (Fit All for points & lines)
663
664         // Setup shape properties here ..., e.g. display mode, color, transparency, etc
665         if(useStudy) {
666           AISShape->SetDisplayMode( aPropMap.value(DISPLAY_MODE_PROP).toInt() );
667           AISShape->SetDisplayVectors(aPropMap.value(VECTOR_MODE_PROP).toInt());
668
669           if(aPropMap.contains(TOP_LEVEL_PROP)) {
670             AISShape->setTopLevel( aPropMap.value(TOP_LEVEL_PROP).value<Standard_Boolean>() );
671           } 
672           
673           if(aPropMap.contains(COLOR_PROP)) {
674             Quantity_Color  quant_col = SalomeApp_Tools::color( aPropMap.value(COLOR_PROP).value<QColor>());
675             AISShape->SetShadingColor( quant_col );
676           } else
677             useObjColor = true;
678         }else {
679           MESSAGE("myDisplayMode = "<< myDisplayMode)
680             AISShape->SetDisplayMode( myDisplayMode );
681           AISShape->SetShadingColor( myShadingColor );
682         }
683
684         // Set color and number for iso lines
685         SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
686         QColor col = aResMgr->colorValue( "Geometry", "isos_color",
687                                           QColor(int(0.5*255), int(0.5*255), int(0.5*255)) );
688         Quantity_Color aColor = SalomeApp_Tools::color( col );
689
690         //get the ISOS number, set transparency if need
691         int anUIsoNumber, aVIsoNumber;
692         if(useStudy) {
693           QString anIsos = aPropMap.value(ISOS_PROP).toString();
694           QStringList uv =  anIsos.split(DIGIT_SEPARATOR);
695           anUIsoNumber = uv[0].toInt();
696           aVIsoNumber = uv[1].toInt();
697           //AISShape->SetTransparency(aPropMap.value(TRANSPARENCY_PROP).toDouble());
698         } else {
699           anUIsoNumber = aResMgr->integerValue("OCCViewer", "iso_number_u", 1);
700           aVIsoNumber  = aResMgr->integerValue("OCCViewer", "iso_number_v", 1);
701         }
702
703         Handle(Prs3d_IsoAspect) anAspect = AISShape->Attributes()->UIsoAspect();
704         anAspect->SetNumber( anUIsoNumber );
705         anAspect->SetColor( aColor );
706         if(HasIsosWidth())
707           anAspect->SetWidth( GetIsosWidth() );
708         AISShape->Attributes()->SetUIsoAspect( anAspect );
709
710
711         anAspect = AISShape->Attributes()->VIsoAspect();
712         anAspect->SetNumber( aVIsoNumber );
713         if(HasIsosWidth())
714           anAspect->SetWidth( GetIsosWidth() );
715         anAspect->SetColor( aColor );
716         AISShape->Attributes()->SetVIsoAspect( anAspect );
717
718         if ( HasColor() )
719         {
720           AISShape->SetColor( (Quantity_NameOfColor)GetColor() );
721           if ( onlyVertex )
722           {
723             if(aPropMap.contains(MARKER_TYPE_PROP)) {
724               QStringList aList = aPropMap.value(MARKER_TYPE_PROP).toString().split(DIGIT_SEPARATOR);
725               if(aList.size() == 2) { //Standard marker string contains "TypeOfMarker:ScaleOfMarker"
726                 Handle(Prs3d_PointAspect) anAspect = AISShape->Attributes()->PointAspect();
727                 int  aTypeOfMarker = aList[0].toInt();
728                 double  aScaleOfMarker = aList[1].toDouble();
729                 anAspect->SetScale( aScaleOfMarker );
730                 anAspect->SetTypeOfMarker((Aspect_TypeOfMarker) (aTypeOfMarker-1));
731                 anAspect->SetColor((Quantity_NameOfColor)GetColor());
732                 AISShape->Attributes()->SetPointAspect(anAspect);
733               } else { //Custom marker string contains "IdOfTexsture"
734                 int textureId = aList[0].toInt();
735                 Standard_Integer aWidth, aHeight;
736 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
737                 Handle(TColStd_HArray1OfByte) aTexture =
738 #else
739                 Handle(Graphic3d_HArray1OfBytes) aTexture =
740 #endif
741                   GeometryGUI::getTexture(aStudy, textureId, aWidth, aHeight);
742                 if (!aTexture.IsNull()) {
743                   static int TextureId = 0;
744                   Handle(Prs3d_PointAspect) aTextureAspect =
745                     new Prs3d_PointAspect ((Quantity_NameOfColor)GetColor(),
746                                            ++TextureId,
747                                            aWidth, aHeight,
748                                            aTexture);
749                   AISShape->Attributes()->SetPointAspect(aTextureAspect);
750                 } else {
751                   useObjMarker = true;
752                 }
753               }
754             } else {
755               useObjMarker = true;
756             }
757           }
758         }
759         
760         else
761         {
762           if ( onlyVertex )
763           {
764             col = aResMgr->colorValue( "Geometry", "point_color", QColor( 255, 255, 0 ) );
765             aColor = SalomeApp_Tools::color( col );
766
767             if(aPropMap.contains(MARKER_TYPE_PROP)) {
768               QStringList aList = aPropMap.value(MARKER_TYPE_PROP).toString().split(DIGIT_SEPARATOR);
769               if(aList.size() == 2) { //Standard marker string contains "TypeOfMarker:ScaleOfMarker"
770                 int  aTypeOfMarker = aList[0].toInt();
771                 double  aScaleOfMarker = aList[1].toDouble();
772                 Handle(Prs3d_PointAspect) anAspect = AISShape->Attributes()->PointAspect();
773                 anAspect->SetScale( aScaleOfMarker );
774                 anAspect->SetTypeOfMarker((Aspect_TypeOfMarker) (aTypeOfMarker-1) );
775                 anAspect->SetColor( aColor );
776                 AISShape->Attributes()->SetPointAspect( anAspect );
777               } else { //Custom marker string contains "IdOfTexsture"
778                 int textureId = aList[0].toInt();
779                 Standard_Integer aWidth, aHeight;
780 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
781                 Handle(TColStd_HArray1OfByte) aTexture =
782 #else
783                 Handle(Graphic3d_HArray1OfBytes) aTexture =
784 #endif
785                   GeometryGUI::getTexture(aStudy, textureId, aWidth, aHeight);
786                 if (!aTexture.IsNull()) {
787                   static int TextureId = 0;
788                   Handle(Prs3d_PointAspect) aTextureAspect =
789                     new Prs3d_PointAspect (aColor, ++TextureId, aWidth, aHeight, aTexture);
790                   AISShape->Attributes()->SetPointAspect( aTextureAspect );
791                 } else {
792                   useObjMarker = true;
793                 }
794               }
795             } else {
796               useObjMarker = true;
797             }
798           }
799           else {
800             // Set line aspect
801             col = aResMgr->colorValue( "Geometry", "wireframe_color", QColor( 255, 255, 0 ) );
802             aColor = SalomeApp_Tools::color( col );
803
804             Handle(Prs3d_LineAspect) anAspect = AISShape->Attributes()->LineAspect();
805             anAspect->SetColor( aColor );
806             AISShape->Attributes()->SetLineAspect( anAspect );
807
808             // Set unfree boundaries aspect
809             anAspect = AISShape->Attributes()->UnFreeBoundaryAspect();
810             anAspect->SetColor( aColor );
811             AISShape->Attributes()->SetUnFreeBoundaryAspect( anAspect );
812             AISShape->storeBoundaryColors();
813
814             // Set free boundaries aspect
815             col = aResMgr->colorValue( "Geometry", "free_bound_color", QColor( 0, 255, 0 ) );
816             aColor = SalomeApp_Tools::color( col );
817
818             anAspect = AISShape->Attributes()->FreeBoundaryAspect();
819             anAspect->SetColor( aColor );
820             AISShape->Attributes()->SetFreeBoundaryAspect( anAspect );
821
822             // Set wire aspect
823             col = aResMgr->colorValue( "Geometry", "line_color", QColor( 255, 0, 0 ) );
824             aColor = SalomeApp_Tools::color( col );
825
826             anAspect = AISShape->Attributes()->WireAspect();
827             anAspect->SetColor( aColor );
828             AISShape->Attributes()->SetWireAspect( anAspect );
829
830             // Set color for edges in shading
831             col = aResMgr->colorValue( "Geometry", "edges_in_shading_color", QColor( 255, 255, 0 ) );
832             aColor = SalomeApp_Tools::color( col );
833             AISShape->SetEdgesInShadingColor( aColor );
834             
835             // bug [SALOME platform 0019868]
836             // Set deviation angle. Default one is 12 degrees (Prs3d_Drawer.cxx:18)
837             //AISShape->SetOwnDeviationAngle( 10*PI/180 );
838
839             // IMP 0020626
840             double aDC = 0;
841             if(useStudy) {
842               aDC = aPropMap.value(DEFLECTION_COEFF_PROP).toDouble();
843               SetWidth(aPropMap.value(EDGE_WIDTH_PROP).toInt());
844               SetIsosWidth(aPropMap.value(ISOS_WIDTH_PROP).toInt());
845             }
846             else {
847               aDC = aResMgr->doubleValue("Geometry", "deflection_coeff", 0.001);
848             }
849
850             aDC = std::max( aDC, DEFLECTION_MIN ); // to avoid to small values of the coefficient
851             AISShape->SetOwnDeviationCoefficient(aDC);
852           }
853         }
854                 
855         if ( HasTexture() )
856         {
857           AISShape->SetTextureFileName(TCollection_AsciiString(myTexture.c_str()));
858           AISShape->SetTextureMapOn();
859           AISShape->DisableTextureModulate();
860           AISShape->SetDisplayMode(3);
861         }
862           
863         if ( HasWidth() )
864           AISShape->SetWidth( GetWidth() );
865
866         if ( !myIO.IsNull() )
867         {
868           AISShape->setIO( myIO );
869           AISShape->SetOwner( myIO );
870         }
871         else if ( !myName.empty() )
872         {
873           // Workaround to allow selection of temporary objects
874           static int tempId = 0;
875           char buf[50];
876           sprintf( buf, "TEMP_%d", tempId++ );
877           Handle( SALOME_InteractiveObject ) anObj =
878             new SALOME_InteractiveObject( buf, "GEOM", myName.c_str() );
879           AISShape->setIO( anObj );
880           AISShape->SetOwner( anObj );
881         }
882
883         Handle( SALOME_InteractiveObject ) anIO = AISShape->getIO();
884         if ( !anIO.IsNull() ) {
885           _PTR(SObject) SO ( aStudy->studyDS()->FindObjectID( anIO->getEntry() ) );
886           if ( SO ) {
887             // get CORBA reference to data object
888             CORBA::Object_var object = GeometryGUI::ClientSObjectToObject(SO);
889             if ( !CORBA::is_nil( object ) ) {
890               // downcast to GEOM object
891               GEOM::GEOM_Object_var aGeomObject = GEOM::GEOM_Object::_narrow( object );
892               bool hasColor = false;
893               SALOMEDS::Color aSColor = getColor(aGeomObject,hasColor);
894               if( hasColor && useObjColor) {
895                 Quantity_Color aQuanColor( aSColor.R, aSColor.G, aSColor.B, Quantity_TOC_RGB );
896                 AISShape->SetColor( aQuanColor );
897                 AISShape->SetShadingColor( aQuanColor );
898                 if ( onlyVertex ) {
899                   Handle(Prs3d_PointAspect) anAspect = AISShape->Attributes()->PointAspect();
900                   anAspect->SetColor( aQuanColor );
901                   anAspect->SetScale( myScaleOfMarker );
902                   anAspect->SetTypeOfMarker( myTypeOfMarker );
903                   AISShape->Attributes()->SetPointAspect( anAspect );
904                 }
905               } else if( !hasColor ) {
906                 //In case if color wasn't defined in the property map of the object
907                 //and GEOM_Object color also wasn't defined get default color from Resource Mgr.
908                 QColor col = aResMgr->colorValue( "Geometry", "shading_color", QColor( 255, 0, 0 ) );
909                 Quantity_Color aQuanColor = SalomeApp_Tools::color( col );
910                 AISShape->SetShadingColor( aQuanColor );
911                 aStudy->setObjectProperty( aMgrId, anIO->getEntry(), COLOR_PROP, col );
912               }
913
914               // ... marker type
915               if(useObjMarker) {
916                 GEOM::marker_type aType = aGeomObject->GetMarkerType();
917                 GEOM::marker_size aSize = aGeomObject->GetMarkerSize();
918                 if ( aType > GEOM::MT_NONE && aType < GEOM::MT_USER && aSize > GEOM::MS_NONE && aSize <= GEOM::MS_70 ) {
919                   Aspect_TypeOfMarker aMType = (Aspect_TypeOfMarker)( (int)aType-1 );
920                   double aMSize = ((int)aSize+1)*0.5;
921                   Handle(Prs3d_PointAspect) anAspect = AISShape->Attributes()->PointAspect();
922                   anAspect->SetScale( aMSize );
923                   anAspect->SetTypeOfMarker( aMType );
924                   Quantity_Color aQuanColor = SalomeApp_Tools::color( aResMgr->colorValue( "Geometry", "point_color", QColor( 255, 255, 0 ) ) );
925                   if ( hasColor )
926                     aQuanColor = Quantity_Color( aSColor.R, aSColor.G, aSColor.B, Quantity_TOC_RGB );
927                   anAspect->SetColor( aQuanColor );
928                   AISShape->Attributes()->SetPointAspect( anAspect );
929                 }
930                 else if ( aType == GEOM::MT_USER ) {
931                   int aTextureId = aGeomObject->GetMarkerTexture();
932                   Quantity_Color aQuanColor = SalomeApp_Tools::color( aResMgr->colorValue( "Geometry", "point_color", QColor( 255, 255, 0 ) ) );
933                   if ( hasColor ) aQuanColor = Quantity_Color( aSColor.R, aSColor.G, aSColor.B, Quantity_TOC_RGB );
934                   Standard_Integer aWidth, aHeight;
935 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
936                   Handle(TColStd_HArray1OfByte) aTexture =
937 #else
938                   Handle(Graphic3d_HArray1OfBytes) aTexture =
939 #endif
940                     GeometryGUI::getTexture(getStudy(), aTextureId, aWidth, aHeight);
941                   if (!aTexture.IsNull()) {
942                     static int TextureId = 0;
943                     Handle(Prs3d_PointAspect) aTextureAspect =
944                       new Prs3d_PointAspect(aQuanColor, ++TextureId, aWidth, aHeight, aTexture );
945                     AISShape->Attributes()->SetPointAspect( aTextureAspect );
946                   }
947                 } else { //Use marker from the preferences
948                   Handle(Prs3d_PointAspect) anAspect = AISShape->Attributes()->PointAspect();
949                   anAspect->SetScale( myScaleOfMarker );
950                   anAspect->SetTypeOfMarker( myTypeOfMarker );
951                   Quantity_Color aQuanColor = SalomeApp_Tools::color( aResMgr->colorValue( "Geometry", "point_color", QColor( 255, 255, 0 ) ) );
952                   if ( hasColor )
953                     aQuanColor = Quantity_Color( aSColor.R, aSColor.G, aSColor.B, Quantity_TOC_RGB );
954                   anAspect->SetColor( aQuanColor );
955                   AISShape->Attributes()->SetPointAspect( anAspect );
956                 }
957               }
958             }
959           }
960
961           // get material properties, set material
962           Material_Model* aModelF = 0;
963           Material_Model* aModelB = 0;
964           if ( useStudy ) {
965             // Get front material property from study and construct front material model
966             QString aMaterialF = aPropMap.value(FRONT_MATERIAL_PROP).toString();
967             QStringList aProps =  aMaterialF.split(DIGIT_SEPARATOR);
968             aModelF = Material_Model::getMaterialModel( aProps );
969
970             // Get back material property from study and construct back material model
971             QString aMaterialB = aPropMap.value(BACK_MATERIAL_PROP).toString();
972             if ( !aMaterialB.isEmpty() ) {
973               QStringList aPropsB =  aMaterialB.split(DIGIT_SEPARATOR);
974               aModelB = Material_Model::getMaterialModel( aPropsB );
975             }
976             else
977               aModelB = aModelF;
978
979           } else {
980             // Get front material property from study and construct front material model
981             aModelF = new Material_Model();
982             aModelF->fromResources( aResMgr, "Geometry", true );
983
984             // Get back material property from study and construct back material model
985             aModelB = new Material_Model();
986             aModelB->fromResources( aResMgr, "Geometry", false );         
987           }
988
989           // Set front material property
990           QString aMaterialPropF = aModelF->getMaterialProperty();
991           aStudy->setObjectProperty( aMgrId, anIO->getEntry(), FRONT_MATERIAL_PROP, aMaterialPropF );
992
993           // Set back material property
994           QString aMaterialPropB = aModelB->getMaterialProperty();
995           aStudy->setObjectProperty( aMgrId, anIO->getEntry(), BACK_MATERIAL_PROP, aMaterialPropB );
996
997           // Get front material properties from the model
998           Graphic3d_MaterialAspect aMatF = aModelF->getMaterialOCCAspect();
999
1000           // Get back material properties from the model
1001           Graphic3d_MaterialAspect aMatB = aModelB->getMaterialOCCAspect();
1002
1003           // Set front material for the selected shape
1004           AISShape->SetCurrentFacingModel(Aspect_TOFM_FRONT_SIDE);
1005           AISShape->SetMaterial(aMatF); 
1006
1007           // Set back material for the selected shape
1008           AISShape->SetCurrentFacingModel(Aspect_TOFM_BACK_SIDE);
1009           AISShape->SetMaterial(aMatB);
1010
1011           // Return to the default facing mode
1012           AISShape->SetCurrentFacingModel(Aspect_TOFM_BOTH_SIDE);
1013
1014           // Release memory
1015           if ( aModelF )
1016             delete aModelF;
1017           if ( aModelB )
1018             delete aModelB;
1019
1020           if(HasWidth())
1021             aStudy->setObjectProperty( aMgrId, anIO->getEntry(), EDGE_WIDTH_PROP, GetWidth() );
1022
1023           if(HasIsosWidth())
1024             aStudy->setObjectProperty( aMgrId, anIO->getEntry(), ISOS_WIDTH_PROP, GetIsosWidth() );
1025
1026
1027         }
1028
1029         // AISShape->SetName(???); ??? necessary to set name ???
1030         occPrs->AddObject( AISShape );
1031
1032         // In accordance with ToActivate() value object will be activated/deactivated
1033         // when it will be displayed
1034         occPrs->SetToActivate( ToActivate() );
1035       }
1036     }
1037     // if presentation is found -> set again shape for it
1038     else
1039     {
1040       if ( !myShape.IsNull() )
1041       {
1042         AIS_ListOfInteractive IOList;
1043         occPrs->GetObjects( IOList );
1044         AIS_ListIteratorOfListOfInteractive Iter( IOList );
1045         for ( ; Iter.More(); Iter.Next() )
1046         {
1047           Handle(GEOM_AISShape) AISShape = Handle(GEOM_AISShape)::DownCast( Iter.Value() );
1048           if ( AISShape.IsNull() )
1049             continue;
1050           if ( AISShape->Shape() != myShape )
1051           {
1052             AISShape->Set( myShape );
1053             AISShape->UpdateSelection();
1054             AISShape->SetToUpdate();
1055           }
1056           if ( !myIO.IsNull() )
1057           {
1058             AISShape->setIO( myIO );
1059             AISShape->SetOwner( myIO );
1060           }
1061         }
1062       }
1063     }
1064   }
1065 }
1066
1067 //=================================================================
1068 /*!
1069  *  GEOM_Displayer::Update
1070  *  Update VTK presentaion
1071  *  [ Reimplemented from SALOME_Displayer ]
1072  */
1073 //=================================================================
1074 void GEOM_Displayer::Update( SALOME_VTKPrs* prs )
1075 {
1076   SalomeApp_Study* aStudy = getStudy();
1077   int aMgrId = -1;
1078   SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
1079
1080   if ( !vtkPrs || myShape.IsNull() || !aStudy)
1081     return;
1082
1083   bool useStudy = false;
1084   PropMap aPropMap;
1085
1086   vtkActorCollection* theActors = 0;
1087
1088   QString anEntry;
1089   if(!myIO.IsNull()) {
1090     anEntry = myIO->getEntry();
1091   }
1092
1093   if ( myType == GEOM_MARKER && myShape.ShapeType() == TopAbs_FACE ) {
1094     //myToActivate = false; // ouv: commented to make the trihedron pickable (see IPAL18657)
1095     GEOM_VTKTrihedron* aTrh = GEOM_VTKTrihedron::New();
1096
1097     if ( HasColor() ) {
1098       Quantity_Color aColor( (Quantity_NameOfColor)GetColor() );
1099       aTrh->SetColor( aColor.Red(), aColor.Green(), aColor.Blue() );
1100     }
1101
1102     Handle(Geom_Plane) aPlane =
1103       Handle(Geom_Plane)::DownCast( BRep_Tool::Surface( TopoDS::Face( myShape ) ) );
1104     if ( aPlane.IsNull() )
1105       return;
1106
1107     gp_Ax2 anAx2 = aPlane->Pln().Position().Ax2();
1108     aTrh->SetPlacement( new Geom_Axis2Placement( anAx2 ) );
1109
1110     //    if ( SVTK_Viewer* vf = dynamic_cast<SVTK_Viewer*>( GetActiveView() ) )
1111     //      aTrh->SetSize( 0.5 * vf->GetTrihedronSize() );
1112
1113     vtkPrs->AddObject( aTrh );
1114
1115     theActors = vtkActorCollection::New();
1116     theActors->AddItem( aTrh );
1117   }
1118   else {
1119     PropMap aDefPropMap = getDefaultPropertyMap(SVTK_Viewer::Type());
1120
1121     if(!myIO.IsNull()) {
1122       aMgrId = getViewManagerId(myViewFrame);
1123     }
1124     useStudy = !anEntry.isEmpty() && aMgrId != -1;
1125
1126
1127     theActors = vtkActorCollection::New();
1128     GEOM_Actor* aGeomActor = GEOM_Actor::New();
1129     aGeomActor->SetShape(myShape,aDefPropMap.value(DEFLECTION_COEFF_PROP).toDouble(),myType == GEOM_VECTOR);
1130     theActors->AddItem(aGeomActor);
1131     aGeomActor->Delete();
1132
1133     if(useStudy) {
1134       aPropMap = aStudy->getObjectPropMap(aMgrId,anEntry);
1135       MergePropertyMaps(aPropMap, aDefPropMap);
1136     }
1137   }
1138
1139   theActors->InitTraversal();
1140
1141   vtkActor* anActor = (vtkActor*)theActors->GetNextActor();
1142
1143   vtkProperty* aProp = 0;
1144
1145   if ( HasColor() || HasWidth() )
1146   {
1147     aProp = vtkProperty::New();
1148     aProp->SetRepresentationToWireframe();
1149   }
1150
1151   if ( HasColor() )
1152   {
1153     Quantity_Color aColor( (Quantity_NameOfColor)GetColor() );
1154     aProp->SetColor( aColor.Red(), aColor.Green(), aColor.Blue() );
1155   }
1156
1157   while ( anActor != NULL )
1158   {
1159     SALOME_Actor* GActor = SALOME_Actor::SafeDownCast( anActor );
1160
1161     GActor->setIO( myIO );
1162
1163     if ( aProp )
1164     {
1165       GActor->SetProperty( aProp );
1166       GActor->SetPreviewProperty( aProp );
1167     }
1168
1169     GEOM_Actor* aGeomGActor = GEOM_Actor::SafeDownCast( anActor );
1170     if ( aGeomGActor != 0 )
1171       {
1172         if ( aProp ) {
1173           aGeomGActor->SetShadingProperty( aProp );
1174           aGeomGActor->SetWireframeProperty( aProp );
1175         }
1176
1177         // Set color for edges in shading
1178         SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
1179         if(aResMgr) {
1180           QColor c = aResMgr->colorValue( "Geometry", "edges_in_shading_color", QColor( 255, 255, 0 ) );
1181           aGeomGActor->SetEdgesInShadingColor( c.red()/255., c.green()/255., c.blue()/255. );
1182         }
1183
1184         int aIsos[2]= { 1, 1 };
1185         if(useStudy) {
1186           QString anIsos = aPropMap.value(ISOS_PROP).toString();
1187           QStringList uv =  anIsos.split(DIGIT_SEPARATOR);
1188           aIsos[0] = uv[0].toInt(); aIsos[1] = uv[1].toInt();
1189           aGeomGActor->SetNbIsos(aIsos);
1190           aGeomGActor->SetOpacity(1.0 - aPropMap.value(TRANSPARENCY_PROP).toDouble());
1191           SetWidth(aPropMap.value(EDGE_WIDTH_PROP).toInt());
1192           SetIsosWidth(aPropMap.value(ISOS_WIDTH_PROP).toInt());
1193           aGeomGActor->SetVectorMode(aPropMap.value(VECTOR_MODE_PROP).toInt());
1194           int aDispModeId = aPropMap.value(DISPLAY_MODE_PROP).toInt();
1195           // Specially processing of 'Shading with edges' mode from preferences,
1196           // because there is the following enum in VTK viewer:
1197           // Points - 0, Wireframe - 1, Surface - 2, Insideframe - 3, SurfaceWithEdges - 4
1198           // (see VTKViewer::Representation enum) and the following enum in GEOM_Actor:
1199           // eWireframe - 0, eShading - 1, eShadingWithEdges - 3
1200           if ( aDispModeId == 2 )
1201             // this is 'Shading with edges' mode => do the correct mapping to EDisplayMode
1202             // enum in GEOM_Actor (and further to VTKViewer::Representation enum)
1203             aDispModeId++;
1204           aGeomGActor->setDisplayMode(aDispModeId);
1205           aGeomGActor->SetDeflection(aPropMap.value(DEFLECTION_COEFF_PROP).toDouble());
1206
1207           // Get front material property of the object stored in the study
1208           QString aMaterialF = aPropMap.value(FRONT_MATERIAL_PROP).toString();
1209           QStringList aPropsF =  aMaterialF.split(DIGIT_SEPARATOR);
1210           // Create front material model
1211           Material_Model* aModelF = Material_Model::getMaterialModel( aPropsF );          
1212           // Set front material properties for the object
1213           QString aMaterialPropF = aModelF->getMaterialProperty();
1214           aStudy->setObjectProperty( aMgrId, anEntry, FRONT_MATERIAL_PROP, aMaterialPropF );      
1215           // Get material properties from the front model
1216           vtkProperty* aMatPropF = aModelF->getMaterialVTKProperty();
1217           
1218           // Get back material property of the object stored in the study
1219           QString aMaterialB = aPropMap.value(BACK_MATERIAL_PROP).toString();
1220           if ( !aMaterialB.isEmpty() ) {
1221             QStringList aPropsB =  aMaterialB.split(DIGIT_SEPARATOR);       
1222             // Create back material model
1223             Material_Model* aModelB = Material_Model::getMaterialModel( aPropsB );
1224             // Set back material properties for the object
1225             QString aMaterialPropB = aModelB->getMaterialProperty();
1226             aStudy->setObjectProperty( aMgrId, anEntry, BACK_MATERIAL_PROP, aMaterialPropB );
1227             // Get material properties from the back model
1228             vtkProperty* aMatPropB = aModelB->getMaterialVTKProperty();
1229
1230             // Set front and back materials for the selected shape
1231             std::vector<vtkProperty*> aProps;
1232             aProps.push_back(aMatPropF);
1233             aProps.push_back(aMatPropB);
1234             aGeomGActor->SetMaterial(aProps);
1235
1236             // Release memory
1237             delete aModelB;
1238
1239             if(HasWidth())
1240               aStudy->setObjectProperty( aMgrId, anEntry, EDGE_WIDTH_PROP, GetWidth() );
1241
1242             if(HasIsosWidth())
1243               aStudy->setObjectProperty( aMgrId, anEntry, ISOS_WIDTH_PROP, GetIsosWidth() );
1244           
1245           }
1246           else {
1247             // Set the same front and back materials for the selected shape
1248             std::vector<vtkProperty*> aProps;
1249             aProps.push_back(aMatPropF);
1250             aGeomGActor->SetMaterial(aProps);
1251           }
1252
1253           // Release memory
1254           delete aModelF;
1255
1256           vtkFloatingPointType aColor[3] = {1.,0.,0.};
1257           if(aPropMap.contains(COLOR_PROP)) {
1258             QColor c = aPropMap.value(COLOR_PROP).value<QColor>();
1259             aColor[0] = c.red()/255.; aColor[1] = c.green()/255.; aColor[2] = c.blue()/255.;
1260           } else { //Get Color from geom object
1261             Handle( SALOME_InteractiveObject ) anIO = aGeomGActor->getIO();
1262             if ( !anIO.IsNull() ) {
1263               _PTR(SObject) SO ( aStudy->studyDS()->FindObjectID( anIO->getEntry() ) );
1264               if ( SO ) {
1265                 // get CORBA reference to data object
1266                 CORBA::Object_var object = GeometryGUI::ClientSObjectToObject(SO);
1267                 if ( !CORBA::is_nil( object ) ) {
1268                   // downcast to GEOM object
1269                   GEOM::GEOM_Object_var aGeomObject = GEOM::GEOM_Object::_narrow( object );
1270                   bool hasColor = false;
1271                   SALOMEDS::Color aSColor = getColor(aGeomObject,hasColor);
1272                   if(hasColor) {
1273                     aColor[0] = aSColor.R; aColor[1] = aSColor.G; aColor[2] = aSColor.B;
1274                   } else {
1275                     SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
1276                     if(aResMgr) {
1277                       QColor c = aResMgr->colorValue( "Geometry", "shading_color", QColor( 255, 0, 0 ) );
1278                       aColor[0] = c.red()/255.; aColor[1] = c.green()/255.; aColor[2] = c.blue()/255.;
1279                       aStudy->setObjectProperty( aMgrId, anIO->getEntry(), COLOR_PROP, c );
1280                     }
1281                   }
1282                 }
1283               }
1284             }
1285           }
1286           aGeomGActor->SetColor(aColor[0],aColor[1],aColor[2]);
1287         }
1288         else {
1289           SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
1290           if ( aResMgr ) {
1291             // Create front material model
1292             Material_Model aModelF;
1293             // Get front material name from resources
1294             aModelF.fromResources( aResMgr, "Geometry", true );
1295             // Set front material properties for the object
1296             QString aMaterialPropF = aModelF.getMaterialProperty();
1297             aStudy->setObjectProperty( aMgrId, anEntry, FRONT_MATERIAL_PROP, aMaterialPropF );      
1298             // Get material properties from the front model
1299             vtkProperty* aMatPropF = aModelF.getMaterialVTKProperty();
1300
1301             // Create back material model
1302             Material_Model aModelB;
1303             // Get back material name from resources
1304             aModelB.fromResources( aResMgr, "Geometry", false );
1305             // Set back material properties for the object
1306             QString aMaterialPropB = aModelB.getMaterialProperty();
1307             aStudy->setObjectProperty( aMgrId, anEntry, BACK_MATERIAL_PROP, aMaterialPropB );
1308             // Get material properties from the back model
1309             vtkProperty* aMatPropB = aModelB.getMaterialVTKProperty();
1310
1311             // Set material for the selected shape
1312             std::vector<vtkProperty*> aProps;
1313             aProps.push_back(aMatPropF);
1314             aProps.push_back(aMatPropB);
1315             aGeomGActor->SetMaterial(aProps);
1316           }
1317         }
1318       }
1319
1320     if ( HasWidth() )
1321       {
1322         aGeomGActor->SetWidth( GetWidth() );
1323       }
1324     
1325     
1326     if ( HasIsosWidth() )
1327       {
1328         aGeomGActor->SetIsosWidth( GetIsosWidth() );
1329       }
1330     
1331     if ( myToActivate )
1332       GActor->PickableOn();
1333     else
1334       GActor->PickableOff();
1335
1336     vtkPrs->AddObject( GActor );
1337
1338     anActor = (vtkActor*)theActors->GetNextActor();
1339   }
1340
1341   if ( aProp )
1342     aProp->Delete();
1343
1344   theActors->Delete();
1345 }
1346
1347 //=================================================================
1348 /*!
1349  *  GEOM_Displayer::BuildPrs
1350  *  Build presentation accordint to the current viewer type
1351  */
1352 //=================================================================
1353 SALOME_Prs* GEOM_Displayer::BuildPrs( GEOM::GEOM_Object_ptr theObj )
1354 {
1355   if ( theObj->_is_nil() )
1356     return 0;
1357
1358   myViewFrame = GetActiveView();
1359   if ( myViewFrame == 0 )
1360     return 0;
1361
1362   SALOME_Prs* aPrs = myViewFrame->CreatePrs();
1363   if ( aPrs == 0 )
1364     return 0;
1365
1366   internalReset();
1367   setShape( GEOM_Client::get_client().GetShape( GeometryGUI::GetGeomGen(), theObj ) );
1368   myType = theObj->GetType();
1369
1370   // Update presentation
1371   UpdatePrs( aPrs );
1372
1373   return aPrs;
1374 }
1375
1376 //=================================================================
1377 /*!
1378  *  GEOM_Displayer::BuildPrs
1379  *  Build presentation accordint to the current viewer type
1380  */
1381 //=================================================================
1382 SALOME_Prs* GEOM_Displayer::BuildPrs( const TopoDS_Shape& theShape )
1383 {
1384   myViewFrame = GetActiveView();
1385   if ( theShape.IsNull() || myViewFrame == 0 )
1386     return 0;
1387
1388   SALOME_Prs* aPrs = myViewFrame->CreatePrs();
1389   if ( aPrs == 0 )
1390     return 0;
1391
1392   internalReset();
1393   setShape( theShape );
1394   myType = -1;
1395
1396   UpdatePrs( aPrs );
1397
1398   return aPrs;
1399 }
1400
1401 //=================================================================
1402 /*!
1403  *  GEOM_Displayer::buildPresentation
1404  *  Builds/finds object's presentation for the current viewer
1405  *  Calls corresponding Update() method by means of double dispatch
1406  *  [ internal ]
1407  */
1408 //=================================================================
1409 SALOME_Prs* GEOM_Displayer::buildPresentation( const QString& entry,
1410                                                SALOME_View* theViewFrame )
1411 {
1412   SALOME_Prs* prs = 0;
1413   internalReset();
1414
1415   myViewFrame = theViewFrame ? theViewFrame : GetActiveView();
1416
1417   if ( myViewFrame )
1418   {
1419     prs = LightApp_Displayer::buildPresentation( entry, theViewFrame );
1420     if ( prs )
1421     {
1422       Handle( SALOME_InteractiveObject ) theIO = new SALOME_InteractiveObject();
1423       theIO->setEntry( entry.toLatin1().constData() );
1424       if ( !theIO.IsNull() )
1425       {
1426         // set interactive object
1427         setIO( theIO );
1428         //  Find SOBject (because shape should be published previously)
1429         SUIT_Session* session = SUIT_Session::session();
1430         SUIT_Application* app = session->activeApplication();
1431         if ( app )
1432         {
1433           SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
1434           if ( study )
1435           {
1436             _PTR(SObject) SO ( study->studyDS()->FindObjectID( theIO->getEntry() ) );
1437             if ( SO )
1438             {
1439               // get CORBA reference to data object
1440               CORBA::Object_var object = GeometryGUI::ClientSObjectToObject(SO);
1441               if ( !CORBA::is_nil( object ) )
1442               {
1443                 // downcast to GEOM object
1444                 GEOM::GEOM_Object_var GeomObject = GEOM::GEOM_Object::_narrow( object );
1445                 if ( !GeomObject->_is_nil() )
1446                 {
1447                   // finally set shape
1448                   setShape( GEOM_Client::get_client().GetShape( GeometryGUI::GetGeomGen(), GeomObject ) );
1449                   myType = GeomObject->GetType();
1450                 }
1451               }
1452             }
1453           }
1454         }
1455       }
1456       UpdatePrs( prs );  // Update presentation by using of the double dispatch
1457     }
1458   }
1459   return prs;
1460 }
1461
1462 //=================================================================
1463 /*!
1464  *  GEOM_Displayer::buildSubshapePresentation
1465  *  Builds/finds object's presentation for the current viewer
1466  *  Calls corresponding Update() method by means of double dispatch
1467  *  For not published objects (for Mantis issue 0020435)
1468  */
1469 //=================================================================
1470 SALOME_Prs* GEOM_Displayer::buildSubshapePresentation(const TopoDS_Shape& aShape,
1471                                                       const QString& entry,
1472                                                       SALOME_View* theViewFrame)
1473 {
1474   SALOME_Prs* prs = 0;
1475   internalReset();
1476
1477   myViewFrame = theViewFrame ? theViewFrame : GetActiveView();
1478
1479   if (myViewFrame)
1480   {
1481     prs = LightApp_Displayer::buildPresentation(entry, theViewFrame);
1482     if (prs)
1483     {
1484       Handle(SALOME_InteractiveObject) theIO = new SALOME_InteractiveObject();
1485       theIO->setEntry(entry.toLatin1().constData());
1486       if (!theIO.IsNull())
1487       {
1488         // set interactive object
1489         setIO(theIO);
1490         // finally set shape
1491         setShape(aShape);
1492         myType = GEOM_SUBSHAPE;
1493       }
1494       UpdatePrs(prs);  // Update presentation by using of the double dispatch
1495     }
1496   }
1497   return prs;
1498 }
1499
1500 //=================================================================
1501 /*!
1502  *  GEOM_Displayer::internalReset
1503  *  Resets internal data
1504  *  [internal]
1505  */
1506 //=================================================================
1507 void GEOM_Displayer::internalReset()
1508 {
1509   myIO.Nullify();
1510   myShape.Nullify();
1511 }
1512
1513 //=================================================================
1514 /*!
1515  *  GEOM_Displayer::LocalSelection
1516  *  Activate selection of CAD shapes with activisation of selection
1517  *  of their sub-shapes (with opened local context for OCC viewer)
1518  */
1519 //=================================================================
1520 void GEOM_Displayer::LocalSelection( const Handle(SALOME_InteractiveObject)& theIO, const int theMode )
1521 {
1522   SUIT_Session* session = SUIT_Session::session();
1523   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
1524   if ( !app )
1525     return;
1526
1527   LightApp_SelectionMgr* sm = app->selectionMgr();
1528
1529   // remove all filters from selection
1530   sm->clearFilters();
1531
1532   SALOME_View* vf = GetActiveView();
1533   if ( vf ) {
1534     if (!theIO.IsNull() && !vf->isVisible(theIO))
1535       Display(theIO);
1536     SALOME_Prs* prs = vf->CreatePrs( theIO.IsNull() ? 0 : theIO->getEntry() );
1537     vf->LocalSelection( prs, theMode );
1538     delete prs;  // delete presentation because displayer is its owner
1539   }
1540 }
1541
1542 //=================================================================
1543 /*!
1544  *  GEOM_Displayer::globalSelection
1545  *  Activate selection of CAD shapes without activisation of selection
1546  *  of their sub-shapes (without opened local context for OCC viewer)
1547  */
1548 //=================================================================
1549 void GEOM_Displayer::GlobalSelection( const int theMode, const bool update )
1550 {
1551   TColStd_MapOfInteger aModes;
1552   aModes.Add( theMode );
1553   GlobalSelection( aModes, update );
1554 }
1555
1556 //=================================================================
1557 /*!
1558  *  GEOM_Displayer::globalSelection
1559  *  Activate selection of CAD shapes without activisation of selection
1560  *  of their sub-shapes (without opened local context for OCC viewer)
1561  */
1562 //=================================================================
1563 void GEOM_Displayer::GlobalSelection( const TColStd_MapOfInteger& theModes,
1564                                       const bool update, const QList<int>* theSubShapes )
1565 {
1566   SUIT_Session* session = SUIT_Session::session();
1567   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
1568   if ( !app )
1569     return;
1570
1571   SALOME_View* vf = GetActiveView();
1572   if ( vf == 0 )
1573     return;
1574
1575   // Close local context
1576   vf->GlobalSelection( update );
1577
1578   // Set selection filters in accordance with current mode
1579   LightApp_SelectionMgr* sm = app->selectionMgr();
1580   if ( !sm )
1581     return;
1582
1583   // Remove from selection temporary objects if necessary
1584   if ( !theModes.Contains( GEOM_PREVIEW ) )
1585     clearTemporary( sm );
1586
1587   //@ aSel->ClearIndex();
1588
1589   sm->clearFilters();
1590
1591   // Remove filters from AIS_InteractiveContext
1592   Handle(AIS_InteractiveContext) ic;
1593   SOCC_Viewer* viewer = dynamic_cast<SOCC_Viewer*>( vf );
1594   if ( viewer )
1595     {
1596       ic = viewer->getAISContext();
1597       if ( !ic.IsNull() )
1598         ic->RemoveFilters();
1599     }
1600
1601   if ( theModes.Contains( GEOM_ALLOBJECTS ) )
1602     return;
1603
1604   SUIT_SelectionFilter* aFilter;
1605   if ( theModes.Extent() == 1 )
1606     {
1607       int aMode = TColStd_MapIteratorOfMapOfInteger( theModes ).Key();
1608
1609       if ( aMode == GEOM_COMPOUNDFILTER )
1610         aFilter = getComplexFilter( theSubShapes );
1611       else
1612         aFilter = getFilter( aMode );
1613     }
1614   else if ( theModes.Extent() > 1 )
1615     {
1616       TColStd_MapOfInteger aTopAbsModes;
1617       TColStd_MapIteratorOfMapOfInteger anIter( theModes );
1618       QList<SUIT_SelectionFilter*> aListOfFilters;
1619       for ( ; anIter.More(); anIter.Next() )
1620         {
1621           SUIT_SelectionFilter* aFilter;
1622           int aMode = anIter.Key();
1623           if ( aMode == GEOM_COMPOUNDFILTER )
1624             aFilter = getComplexFilter( theSubShapes );
1625           else
1626             aFilter = getFilter( aMode );
1627
1628           if ( aFilter )
1629             aListOfFilters.append( aFilter );
1630         }
1631
1632       aFilter = new GEOM_LogicalFilter( aListOfFilters, GEOM_LogicalFilter::LO_OR );
1633     }
1634   else
1635     return;
1636
1637   if ( aFilter )
1638     {
1639       sm->installFilter( aFilter );
1640       if ( !ic.IsNull() )
1641         {
1642           Handle(GEOM_OCCFilter) anOCCFilter = new GEOM_OCCFilter( sm );
1643           ic->AddFilter( anOCCFilter );
1644         }
1645     }
1646 }
1647
1648 //=================================================================
1649 /*!
1650  *  GEOM_Displayer::LocalSelection
1651  *  Activate selection of CAD shapes with activisation of selection
1652  *  of their sub-shapes (with opened local context for OCC viewer)
1653  */
1654 //=================================================================
1655 void GEOM_Displayer::LocalSelection( const SALOME_ListIO& theIOList, const int theMode )
1656 {
1657   SALOME_ListIteratorOfListIO Iter( theIOList );
1658   for ( ; Iter.More(); Iter.Next() )
1659     LocalSelection( Iter.Value(), theMode );
1660 }
1661
1662 //=================================================================
1663 /*!
1664  *  GEOM_Displayer::BeforeDisplay
1665  *  Called before displaying of pars. Close local context
1666  *  [ Reimplemented from SALOME_Displayer ]
1667  */
1668 //=================================================================
1669 void GEOM_Displayer::BeforeDisplay( SALOME_View* v, const SALOME_OCCPrs* )
1670 {
1671   SOCC_Viewer* vf = dynamic_cast<SOCC_Viewer*>( v );
1672   if ( vf )
1673   {
1674     Handle(AIS_InteractiveContext) ic = vf->getAISContext();
1675     if ( !ic.IsNull() )
1676     {
1677       if ( ic->HasOpenedContext() )
1678       ic->CloseAllContexts();
1679     }
1680   }
1681 }
1682
1683 void GEOM_Displayer::AfterDisplay( SALOME_View* v, const SALOME_OCCPrs* p )
1684 {
1685   SalomeApp_Study* aStudy = getStudy();
1686   if (!aStudy) return;
1687   SOCC_Viewer* vf = dynamic_cast<SOCC_Viewer*>( v );
1688   if ( vf && !p->IsNull() ) {
1689     int aMgrId = getViewManagerId( vf );
1690     Handle(AIS_InteractiveContext) ic = vf->getAISContext();
1691     const SOCC_Prs* prs = dynamic_cast<const SOCC_Prs*>( p );
1692     if ( !ic.IsNull() && prs ) {
1693       AIS_ListOfInteractive objects;
1694       prs->GetObjects( objects );
1695       AIS_ListIteratorOfListOfInteractive it( objects );
1696       for ( ; it.More(); it.Next() ) {
1697         Handle(GEOM_AISShape) sh = Handle(GEOM_AISShape)::DownCast( it.Value() );
1698         if ( sh.IsNull() ) continue;
1699         Handle(SALOME_InteractiveObject) IO = sh->getIO();
1700         if ( IO.IsNull() ) continue;
1701         PropMap aPropMap = aStudy->getObjectPropMap( aMgrId, IO->getEntry() );
1702         if ( aPropMap.contains( TRANSPARENCY_PROP ) ) {
1703           double transparency = aPropMap.value(TRANSPARENCY_PROP).toDouble();
1704           ic->SetTransparency( sh, transparency, true );
1705         }
1706       }
1707     }
1708   }
1709 }
1710
1711 //=================================================================
1712 /*!
1713  *  GEOM_Displayer::SetColor
1714  *  Set color for shape displaying. If it is equal -1 then default color is used.
1715  *  Available values are from Quantity_NameOfColor enumeration
1716  */
1717 //=================================================================
1718 void GEOM_Displayer::SetColor( const int color )
1719 {
1720   if ( color == -1 )
1721     UnsetColor();
1722   else
1723   {
1724     myColor = color;
1725     myShadingColor = Quantity_Color( (Quantity_NameOfColor)color );
1726   }
1727 }
1728
1729 int GEOM_Displayer::GetColor() const
1730 {
1731   return myColor;
1732 }
1733
1734 bool GEOM_Displayer::HasColor() const
1735 {
1736   return myColor != -1;
1737 }
1738
1739 void GEOM_Displayer::UnsetColor()
1740 {
1741   myColor = -1;
1742
1743   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
1744   QColor col = resMgr->colorValue( "Geometry", "shading_color", QColor( 255, 0, 0 ) );
1745   myShadingColor = SalomeApp_Tools::color( col );
1746 }
1747
1748 //=================================================================
1749 /*!
1750  *  GEOM_Displayer::SetTexture
1751  *  Set color for shape displaying. If it is equal -1 then default color is used.
1752  *  Available values are from Quantity_NameOfColor enumeration
1753  */
1754 //=================================================================
1755 void GEOM_Displayer::SetTexture( const std::string& texureFileName )
1756 {
1757   if(texureFileName!="")
1758   {
1759     myTexture = texureFileName;
1760   }
1761 }
1762
1763 bool GEOM_Displayer::HasTexture() const
1764 {
1765   return myTexture != "";
1766 }
1767
1768 std::string GEOM_Displayer::GetTexture() const
1769 {
1770   return myTexture;
1771 }
1772
1773 //=================================================================
1774 /*!
1775  *  GEOM_Displayer::SetWidth
1776  *  Set width of shape displaying. If it is equal -1 then default width is used.
1777  */
1778 //=================================================================
1779 void GEOM_Displayer::SetWidth( const double width )
1780 {
1781   myWidth = width;
1782 }
1783
1784 double GEOM_Displayer::GetWidth() const
1785 {
1786   return myWidth;
1787 }
1788
1789 bool GEOM_Displayer::HasWidth() const
1790 {
1791   return myWidth != -1;
1792 }
1793
1794 void GEOM_Displayer::UnsetWidth()
1795 {
1796   myWidth = -1;
1797 }
1798
1799
1800 int GEOM_Displayer::GetIsosWidth() const
1801 {
1802   return myIsosWidth;
1803 }
1804
1805 void GEOM_Displayer::SetIsosWidth(const int width) 
1806 {
1807   myIsosWidth = width;
1808 }
1809
1810 bool GEOM_Displayer::HasIsosWidth() const
1811 {
1812   return myIsosWidth != -1;
1813 }
1814
1815
1816 //=================================================================
1817 /*!
1818  *  GEOM_Displayer::SetToActivate
1819  *  This method is used for activisation/deactivisation of objects to be displayed
1820  */
1821 //=================================================================
1822 void GEOM_Displayer::SetToActivate( const bool toActivate )
1823 {
1824   myToActivate = toActivate;
1825 }
1826 bool GEOM_Displayer::ToActivate() const
1827 {
1828   return myToActivate;
1829 }
1830
1831 //=================================================================
1832 /*!
1833  *  GEOM_Displayer::clearTemporary
1834  *  Removes from selection temporary objects
1835  */
1836 //=================================================================
1837 void GEOM_Displayer::clearTemporary( LightApp_SelectionMgr* theSelMgr )
1838 {
1839   SALOME_ListIO selected, toSelect;
1840   theSelMgr->selectedObjects( selected );
1841
1842   for (  SALOME_ListIteratorOfListIO it( selected ) ; it.More(); it.Next() ) {
1843     Handle(SALOME_InteractiveObject) io = it.Value();
1844     if ( !io.IsNull() && io->hasEntry() && strncmp( io->getEntry(), "TEMP_", 5 ) != 0 )
1845       toSelect.Append( it.Value() );
1846   }
1847
1848   theSelMgr->setSelectedObjects( toSelect, true );
1849 }
1850
1851 void GEOM_Displayer::SetName( const char* theName )
1852 {
1853   myName = theName;
1854 }
1855
1856 void GEOM_Displayer::UnsetName()
1857 {
1858   myName = "";
1859 }
1860
1861 SalomeApp_Study* GEOM_Displayer::getStudy() const
1862 {
1863   return dynamic_cast<SalomeApp_Study*>( myApp->activeStudy() );
1864 }
1865
1866 void GEOM_Displayer::setIO( const Handle(SALOME_InteractiveObject)& theIO )
1867 {
1868   myIO = theIO;
1869 }
1870
1871 void GEOM_Displayer::setShape( const TopoDS_Shape& theShape )
1872 {
1873   myShape = theShape;
1874 }
1875
1876 bool GEOM_Displayer::canBeDisplayed( const QString& entry, const QString& viewer_type ) const
1877 {
1878   return viewer_type == SOCC_Viewer::Type() || viewer_type == SVTK_Viewer::Type();
1879 }
1880
1881 int GEOM_Displayer::SetDisplayMode( const int theMode )
1882 {
1883   int aPrevMode = myDisplayMode;
1884   if ( theMode != -1 )
1885     myDisplayMode = theMode;
1886   else
1887   {
1888     SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
1889     myDisplayMode = resMgr->integerValue( "Geometry", "display_mode", 0 );
1890   }
1891   return aPrevMode;
1892 }
1893
1894 int GEOM_Displayer::GetDisplayMode() const
1895 {
1896   return myDisplayMode;
1897 }
1898
1899 int GEOM_Displayer::UnsetDisplayMode()
1900 {
1901   int aPrevMode = myDisplayMode;
1902   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
1903   myDisplayMode = resMgr->integerValue( "Geometry", "display_mode", 0 );
1904   return aPrevMode;
1905 }
1906
1907 SALOMEDS::Color GEOM_Displayer::getUniqueColor( const QList<SALOMEDS::Color>& theReservedColors )
1908 {
1909   int aHue = -1;
1910   int aTolerance = 64;
1911   int anIterations = 0;
1912   int aPeriod = 5;
1913
1914   while( 1 )
1915   {
1916     anIterations++;
1917     if( anIterations % aPeriod == 0 )
1918     {
1919       aTolerance /= 2;
1920       if( aTolerance < 1 )
1921         break;
1922     }
1923     //std::cout << "Iteration N" << anIterations << " (tolerance=" << aTolerance << ")"<< std::endl;
1924
1925     aHue = (int)( 360.0 * rand() / RAND_MAX );
1926     //std::cout << "Hue = " << aHue << std::endl;
1927
1928     //std::cout << "Auto colors : ";
1929     bool ok = true;
1930     QList<SALOMEDS::Color>::const_iterator it = theReservedColors.constBegin();
1931     QList<SALOMEDS::Color>::const_iterator itEnd = theReservedColors.constEnd();
1932     for( ; it != itEnd; ++it )
1933     {
1934       SALOMEDS::Color anAutoColor = *it;
1935       QColor aQColor( (int)( anAutoColor.R * 255.0 ), (int)( anAutoColor.G * 255.0 ), (int)( anAutoColor.B * 255.0 ) );
1936
1937       int h, s, v;
1938       aQColor.getHsv( &h, &s, &v );
1939       //std::cout << h << " ";
1940       if( abs( h - aHue ) < aTolerance )
1941       {
1942         ok = false;
1943         //std::cout << "break (diff = " << abs( h - aHue ) << ")";
1944         break;
1945       }
1946     }
1947     //std::cout << std::endl;
1948
1949     if( ok )
1950       break;
1951   }
1952
1953   //std::cout << "Hue of the returned color = " << aHue << std::endl;
1954   QColor aColor;
1955   aColor.setHsv( aHue, 255, 255 );
1956
1957   SALOMEDS::Color aSColor;
1958   aSColor.R = (double)aColor.red() / 255.0;
1959   aSColor.G = (double)aColor.green() / 255.0;
1960   aSColor.B = (double)aColor.blue() / 255.0;
1961
1962   return aSColor;
1963 }
1964
1965
1966
1967 PropMap GEOM_Displayer::getDefaultPropertyMap(const QString& viewer_type) {
1968   PropMap aDefaultMap;
1969   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
1970   //1. Visibility
1971   aDefaultMap.insert(VISIBILITY_PROP , 1);
1972
1973   //2. Nb Isos
1974   int anUIsoNumber;
1975   int aVIsoNumber;
1976   if(viewer_type == SOCC_Viewer::Type()) {
1977     anUIsoNumber = aResMgr->integerValue("OCCViewer", "iso_number_u", 1);
1978     aVIsoNumber = aResMgr->integerValue("OCCViewer", "iso_number_v", 1);
1979   } else if( viewer_type==SVTK_Viewer::Type()) {
1980     anUIsoNumber = aResMgr->integerValue("VTKViewer", "iso_number_u", 1);
1981     aVIsoNumber = aResMgr->integerValue("VTKViewer", "iso_number_u", 1);
1982   }
1983   QString anIsos("%1%2%3");
1984   anIsos = anIsos.arg(anUIsoNumber);anIsos = anIsos.arg(DIGIT_SEPARATOR);anIsos = anIsos.arg(aVIsoNumber);
1985   aDefaultMap.insert(ISOS_PROP , anIsos);
1986
1987   //3. Transparency
1988   aDefaultMap.insert( TRANSPARENCY_PROP , 0.0 );
1989
1990   //4. Display Mode
1991   aDefaultMap.insert( DISPLAY_MODE_PROP , aResMgr->integerValue("Geometry", "display_mode", 0));
1992
1993   //5. Vector Mode
1994   aDefaultMap.insert( VECTOR_MODE_PROP , 0);
1995
1996   //6. Color
1997   QColor col = aResMgr->colorValue( "Geometry", "shading_color", QColor( 255, 0, 0 ) );
1998   aDefaultMap.insert( COLOR_PROP , col);
1999
2000   //7. Deflection Coeff
2001   double aDC;
2002
2003   if(viewer_type == SOCC_Viewer::Type()) {
2004     aDC = aResMgr->doubleValue("Geometry", "deflection_coeff", 0.001);
2005   } else if( viewer_type==SVTK_Viewer::Type()) {
2006     aDC = 0.001;
2007   }
2008
2009   aDefaultMap.insert( DEFLECTION_COEFF_PROP , aDC);
2010
2011   //8. Material
2012   //   Front material
2013   Material_Model aModelF;
2014   aModelF.fromResources( aResMgr, "Geometry", true );
2015   QString aMaterialF = aModelF.getMaterialProperty();
2016   aDefaultMap.insert( FRONT_MATERIAL_PROP , aMaterialF );  
2017
2018   //9.  Back material
2019   Material_Model aModelB;
2020   aModelB.fromResources( aResMgr, "Geometry", false );
2021   QString aMaterialB = aModelB.getMaterialProperty();
2022   aDefaultMap.insert( BACK_MATERIAL_PROP , aMaterialB );
2023
2024   //10. Width of the edges
2025   aDefaultMap.insert( EDGE_WIDTH_PROP , aResMgr->integerValue("Geometry", "edge_width", 1));
2026
2027
2028   //11. Width of iso-lines
2029   aDefaultMap.insert( ISOS_WIDTH_PROP , aResMgr->integerValue("Geometry", "isolines_width", 1));
2030
2031   if(viewer_type == SOCC_Viewer::Type()) {
2032
2033     aDefaultMap.insert(TOP_LEVEL_PROP, Standard_False);
2034   }
2035
2036   return aDefaultMap;
2037 }
2038
2039 bool GEOM_Displayer::MergePropertyMaps(PropMap& theOrigin, PropMap& theDefault) {
2040   int nbInserted = 0;
2041   if(!theOrigin.contains(VISIBILITY_PROP)) {
2042     theOrigin.insert(VISIBILITY_PROP, 0);
2043     nbInserted++;
2044   }
2045   if(!theOrigin.contains(TRANSPARENCY_PROP)) {
2046     theOrigin.insert(TRANSPARENCY_PROP, theDefault.value(TRANSPARENCY_PROP));
2047     nbInserted++;
2048   }
2049   if(!theOrigin.contains(DISPLAY_MODE_PROP)) {
2050     theOrigin.insert(DISPLAY_MODE_PROP, theDefault.value(DISPLAY_MODE_PROP));
2051     nbInserted++;
2052   }
2053   if(!theOrigin.contains(ISOS_PROP)) {
2054     theOrigin.insert(ISOS_PROP, theDefault.value(ISOS_PROP));
2055     nbInserted++;
2056   }
2057   if(!theOrigin.contains(VECTOR_MODE_PROP)) {
2058     theOrigin.insert(VECTOR_MODE_PROP, theDefault.value(VECTOR_MODE_PROP));
2059     nbInserted++;
2060   }
2061   if(!theOrigin.contains(DEFLECTION_COEFF_PROP)) {
2062     theOrigin.insert(DEFLECTION_COEFF_PROP, theDefault.value(DEFLECTION_COEFF_PROP));
2063     nbInserted++;
2064   }
2065   if(!theOrigin.contains(FRONT_MATERIAL_PROP)) {
2066     theOrigin.insert(FRONT_MATERIAL_PROP, theDefault.value(FRONT_MATERIAL_PROP));
2067     nbInserted++;
2068   }
2069   if(!theOrigin.contains(BACK_MATERIAL_PROP)) {
2070     theOrigin.insert(BACK_MATERIAL_PROP, theDefault.value(BACK_MATERIAL_PROP));
2071     nbInserted++;
2072   }
2073
2074   if(!theOrigin.contains(EDGE_WIDTH_PROP)) {
2075     theOrigin.insert(EDGE_WIDTH_PROP, theDefault.value(EDGE_WIDTH_PROP));
2076     nbInserted++;
2077   }
2078
2079   if(!theOrigin.contains(ISOS_WIDTH_PROP)) {
2080     theOrigin.insert(ISOS_WIDTH_PROP, theDefault.value(ISOS_WIDTH_PROP));
2081     nbInserted++;
2082   }
2083
2084   return (nbInserted > 0);
2085 }
2086
2087
2088 SALOMEDS::Color GEOM_Displayer::getColor(GEOM::GEOM_Object_var theGeomObject, bool& hasColor) {
2089   SALOMEDS::Color aSColor;
2090   hasColor = false;
2091
2092   SUIT_Session* session = SUIT_Session::session();
2093   SUIT_Application* app = session->activeApplication();
2094
2095   if ( app && !theGeomObject->_is_nil()) {
2096     SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
2097
2098     if ( study ) {
2099       aSColor = theGeomObject->GetColor();
2100       hasColor = aSColor.R >= 0 && aSColor.G >= 0 && aSColor.B >= 0;
2101       if( !hasColor && theGeomObject->GetType() == GEOM_GROUP ) { // auto color for group
2102         GEOM::GEOM_Gen_var theGeomGen = GeometryGUI::GetGeomGen();
2103         GEOM::GEOM_IGroupOperations_var anOperations = theGeomGen->GetIGroupOperations( study->id() );
2104         GEOM::GEOM_Object_var aMainObject = anOperations->GetMainShape( theGeomObject );
2105         if ( !aMainObject->_is_nil() && aMainObject->GetAutoColor() )
2106           {
2107             QList<SALOMEDS::Color> aReservedColors;
2108
2109             SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( app );
2110             CORBA::String_var IOR = app->orb()->object_to_string( aMainObject );
2111             if ( strcmp(IOR.in(), "") != 0 )
2112               {
2113                 _PTR(Study) aStudy = study->studyDS();
2114                 _PTR(SObject) aMainSObject( aStudy->FindObjectIOR( std::string(IOR) ) );
2115                 _PTR(ChildIterator) it( aStudy->NewChildIterator( aMainSObject ) );
2116                 for( ; it->More(); it->Next() )
2117                   {
2118                     _PTR(SObject) aChildSObject( it->Value() );
2119                     GEOM::GEOM_Object_var aChildObject =
2120                       GEOM::GEOM_Object::_narrow(GeometryGUI::ClientSObjectToObject(aChildSObject));
2121                     if( CORBA::is_nil( aChildObject ) )
2122                       continue;
2123
2124                     if( aChildObject->GetType() != GEOM_GROUP )
2125                       continue;
2126
2127                     SALOMEDS::Color aReservedColor = aChildObject->GetColor();
2128                     aReservedColors.append( aReservedColor );
2129                   }
2130               }
2131
2132             aSColor = getUniqueColor( aReservedColors );
2133             hasColor = true;
2134           }
2135       }
2136     }
2137   }
2138   return aSColor;
2139 }
2140
2141
2142 void GEOM_Displayer::EraseWithChildren(const Handle(SALOME_InteractiveObject)& theIO,
2143                                        const bool eraseOnlyChildren) {
2144   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
2145   if ( !app )
2146     return;
2147
2148   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
2149   if ( !appStudy )
2150     return;
2151
2152   LightApp_DataObject* parent = appStudy->findObjectByEntry(theIO->getEntry());
2153
2154   if( !parent)
2155     return;
2156
2157   // Erase from all views
2158   QList<SALOME_View*> views;
2159   SALOME_View* view;
2160   ViewManagerList vmans = app->viewManagers();
2161   SUIT_ViewManager* vman;
2162   foreach ( vman, vmans ) {
2163     SUIT_ViewModel* vmod = vman->getViewModel();
2164     view = dynamic_cast<SALOME_View*> ( vmod );
2165     if ( view )
2166       views.append( view );
2167   }
2168
2169   if( views.count() == 0 )
2170     return;
2171
2172   //Erase childrens w/o update views
2173   DataObjectList listObj = parent->children( true );
2174   SUIT_DataObject* obj;
2175   foreach( obj, listObj ) {
2176     LightApp_DataObject* l_obj = dynamic_cast<LightApp_DataObject*>(obj);
2177     if(l_obj)
2178       foreach ( view, views ) {
2179       Handle(SALOME_InteractiveObject) anIO =
2180         new SALOME_InteractiveObject(qPrintable(l_obj->entry()), "GEOM", "");
2181       Erase(anIO, false, false, view);
2182     }
2183   }
2184
2185   //Erase parent with view update or repaint views
2186   foreach ( view, views ) {
2187     if(!eraseOnlyChildren)
2188       Erase(theIO, false, true, view);
2189     else
2190       view->Repaint();
2191   }
2192 }