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