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