Salome HOME
Update mail address
[modules/geom.git] / src / GEOMGUI / GEOM_Displayer.cxx
1 //  GEOM GEOMGUI : GUI for Geometry component
2 //
3 //  Copyright (C) 2003  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 //
24 //  File   : GEOM_Displayer.cxx
25 //  Author : Vadim SANDLER
26 //  Module : GEOM
27 //  $Header$
28
29 #include "GEOM_Displayer.h"
30
31 #include "GeometryGUI.h"
32
33 #include "GEOM_TypeFilter.h"
34 #include "GEOM_EdgeFilter.h"
35 #include "GEOM_FaceFilter.h"
36 #include "GEOM_PreviewFilter.h"
37 #include "GEOM_LogicalFilter.h"
38 #include "GEOM_OCCFilter.h"
39
40 #include "GEOM_Actor.h"
41 #include "GEOM_AssemblyBuilder.h"
42 #include "GEOM_AISShape.hxx"
43 #include "GEOM_AISTrihedron.hxx"
44 #include "GEOM_VTKTrihedron.hxx"
45
46 #include <SUIT_Desktop.h>
47 #include <SUIT_ViewWindow.h>
48 #include <SUIT_Session.h>
49 #include <SUIT_Tools.h>
50 #include <SUIT_ViewManager.h>
51
52 #include <SalomeApp_Study.h>
53 #include <SalomeApp_Application.h>
54 #include <LightApp_SelectionMgr.h>
55 #include <SalomeApp_TypeFilter.h>
56 #include <SalomeApp_Tools.h>
57
58 #include <SALOME_ListIteratorOfListIO.hxx>
59
60 #include <SOCC_Prs.h>
61 #include <SOCC_ViewModel.h>
62
63 #include <SVTK_Prs.h>
64 #include <SVTK_ViewModel.h>
65
66 #include <SALOMEDSClient.hxx>
67 #include <SALOMEDSClient_SObject.hxx>
68
69 // OCCT Includes
70 #include <AIS_Drawer.hxx>
71 #include <AIS_ListIteratorOfListOfInteractive.hxx>
72 #include <Prs3d_PointAspect.hxx>
73 #include <Graphic3d_AspectMarker3d.hxx>
74 #include <StdSelect_TypeOfEdge.hxx>
75 #include <StdSelect_TypeOfFace.hxx>
76 #include <TopoDS_Face.hxx>
77 #include <BRep_Tool.hxx>
78 #include <Geom_Plane.hxx>
79 #include <Geom_Axis2Placement.hxx>
80 #include <gp_Pln.hxx>
81 #include <TColStd_MapOfInteger.hxx>
82 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
83
84 // VTK Includes
85 #include <vtkActorCollection.h>
86 #include <vtkProperty.h>
87
88 // STL Includes
89 #include <cstring>
90
91 // CORBA Headers
92 #include CORBA_CLIENT_HEADER(SALOMEDS_Attributes)
93
94 #include "GEOMImpl_Types.hxx"
95
96 using namespace std;
97
98 //================================================================
99 // Function : getActiveStudy
100 // Purpose  : Get active study, returns 0 if no open study frame
101 //================================================================
102 static inline SalomeApp_Study* getActiveStudy()
103 {
104   SUIT_Session* session = SUIT_Session::session();
105   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
106   if ( app )
107     return ( SalomeApp_Study* )app->activeStudy();
108   return 0;
109 }
110
111 //================================================================
112 // Function : getTopAbsMode
113 // Purpose  : Get TopAbs_ShapeEnum value corresponding to the
114 //            one from GEOMImpl_Types.h
115 //================================================================
116 static inline int getTopAbsMode( const int implType )
117 {
118   switch ( implType )
119   {
120     case GEOM_COMPOUND  : return TopAbs_COMPOUND;
121     case GEOM_SOLID     : return TopAbs_SOLID;
122     case GEOM_SHELL     : return TopAbs_SHELL;
123     case GEOM_FACE      : return TopAbs_FACE;
124     case GEOM_WIRE      : return TopAbs_WIRE;
125     case GEOM_EDGE      : return TopAbs_EDGE;
126     case GEOM_POINT     : return TopAbs_VERTEX;
127     default             : return -1;
128   }
129 }
130
131 //================================================================
132 // Function : getFilter
133 // Purpose  : Get filter corresponding to the type of object
134 //            from GEOMImpl_Types.h
135 //================================================================
136 SUIT_SelectionFilter* GEOM_Displayer::getFilter( const int theMode )
137 {
138   SUIT_SelectionFilter* aFilter;
139
140   int aTopAbsMode = getTopAbsMode( theMode );
141   if ( aTopAbsMode != -1 )
142     aFilter = new GEOM_TypeFilter( getStudy(), aTopAbsMode, true ); //@ aFilter = new GEOM_TypeFilter( ( TopAbs_ShapeEnum )aTopAbsMode );
143   else
144     switch ( theMode )
145       {
146       case GEOM_LINE      : aFilter = new GEOM_EdgeFilter( getStudy(), StdSelect_Line ); break;
147       case GEOM_CIRCLE    : aFilter = new GEOM_EdgeFilter( getStudy(), StdSelect_Circle ); break;
148
149       case GEOM_PLANE     : aFilter = new GEOM_FaceFilter( getStudy(), StdSelect_Plane ); break;
150       case GEOM_CYLINDER  : aFilter = new GEOM_FaceFilter( getStudy(), StdSelect_Cylinder ); break;
151       case GEOM_SPHERE    : aFilter = new GEOM_FaceFilter( getStudy(), StdSelect_Sphere ); break;
152       case GEOM_TORUS     : aFilter = new GEOM_FaceFilter( getStudy(), StdSelect_Torus ); break;
153       case GEOM_REVOLUTION: aFilter = new GEOM_FaceFilter( getStudy(), StdSelect_Revol ); break;
154       case GEOM_CONE      : aFilter = new GEOM_FaceFilter( getStudy(), StdSelect_Cone ); break;
155
156       case GEOM_PREVIEW   : aFilter = new GEOM_PreviewFilter( getStudy() ); break;
157
158       case GEOM_ALLSHAPES : aFilter = new GEOM_SelectionFilter(getStudy(), true ); break;
159       case GEOM_ALLGEOM   : aFilter = new SalomeApp_TypeFilter( getStudy(), "GEOM" ); break;
160
161       default             : aFilter = new GEOM_TypeFilter( getStudy(), theMode ); break;
162       }
163
164   return aFilter;
165 }
166
167 //================================================================
168 // Function : getEntry
169 // Purpose  :
170 //================================================================
171 static string getEntry( GEOM::GEOM_Object_ptr object )
172 {
173   SUIT_Session* session = SUIT_Session::session();
174   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
175   if ( app )
176   {
177     string IOR = app->orb()->object_to_string( object );
178     if ( IOR != "" )
179     {
180       SalomeApp_Study* study = ( SalomeApp_Study* )app->activeStudy();
181       _PTR(SObject) SO ( study->studyDS()->FindObjectIOR( IOR ) );
182       if ( SO )
183         return SO->GetID();
184     }
185   }
186   return "";
187 }
188
189 //================================================================
190 // Function : getName
191 // Purpose  :
192 //================================================================
193 static string getName( GEOM::GEOM_Object_ptr object )
194 {
195   SUIT_Session* session = SUIT_Session::session();
196   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
197   if ( app )
198   {
199     string IOR = app->orb()->object_to_string( object );
200     if ( IOR != "" )
201     {
202       SalomeApp_Study* study = ( SalomeApp_Study* )app->activeStudy();
203       _PTR(SObject) aSObj ( study->studyDS()->FindObjectIOR( IOR ) );
204
205       _PTR(GenericAttribute) anAttr;
206
207       if ( aSObj && aSObj->FindAttribute( anAttr, "AttributeName") )
208       {
209         _PTR(AttributeName) aNameAttr( anAttr );
210         return aNameAttr->Value();
211       }
212     }
213   }
214
215   return "";
216 }
217
218 //=================================================================
219 /*!
220  *  GEOM_Displayer::GEOM_Displayer
221  *  Constructor
222  */
223 //=================================================================
224 GEOM_Displayer::GEOM_Displayer( SalomeApp_Study* st )
225 {
226   if( st )
227     myApp = dynamic_cast<SalomeApp_Application*>( st->application() );
228   else
229     myApp = 0;
230
231   /* Shading Color */
232   SUIT_Session* session = SUIT_Session::session();
233   SUIT_ResourceMgr* resMgr = session->resourceMgr();
234
235   QColor col = resMgr->colorValue( "Geometry", "shading_color", QColor( 255, 0, 0 ) );
236   myShadingColor = SalomeApp_Tools::color( col );
237
238   myDisplayMode = resMgr->integerValue("Geometry", "display_mode", 0);
239
240   myColor = -1;
241   // This color is used for shape displaying. If it is equal -1 then
242   // default color is used.
243
244   myWidth = -1;
245   myType = -1;
246
247   myToActivate = true;
248   // This parameter is used for activisation/deactivisation of objects to be displayed
249
250   myViewFrame = 0;
251 }
252
253 //=================================================================
254 /*!
255  *  GEOM_Displayer::~GEOM_Displayer
256  *  Destructor
257  */
258 //=================================================================
259 GEOM_Displayer::~GEOM_Displayer()
260 {
261 }
262
263 //=================================================================
264 /*!
265  *  GEOM_Displayer::Display
266  *  Display interactive object in the current viewer
267  */
268 //=================================================================
269 void GEOM_Displayer::Display( const Handle(SALOME_InteractiveObject)& theIO,
270                              const bool updateViewer,
271                              SALOME_View* theViewFrame )
272 {
273   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
274   if ( vf )
275   {
276     SALOME_Prs* prs = buildPresentation( theIO->getEntry(), vf );
277
278     if ( prs )
279     {
280       vf->BeforeDisplay( this );
281       vf->Display( prs );
282       vf->AfterDisplay( this );
283
284       if ( updateViewer )
285         vf->Repaint();
286
287       delete prs;  // delete presentation because displayer is its owner
288     }
289   }
290 }
291
292 //=================================================================
293 /*!
294  *  GEOM_Displayer::Display
295  *  This overloaded Display() method can be useful for operations
296  *  not using dialog boxes.
297  */
298 //=================================================================
299 void GEOM_Displayer::Display( GEOM::GEOM_Object_ptr theObj, const bool updateViewer )
300 {
301   if ( theObj->_is_nil() )
302     return;
303
304   string entry = getEntry( theObj );
305   if ( entry != "" ) {
306     Display(new SALOME_InteractiveObject(entry.c_str(), "GEOM", getName(theObj).c_str()),
307             updateViewer);
308   }
309 }
310
311 //=================================================================
312 /*!
313  *  GEOM_Displayer::Erase
314  *  Erase interactive object in the current viewer
315  */
316 //=================================================================
317 void GEOM_Displayer::Erase( const Handle(SALOME_InteractiveObject)& theIO,
318                             const bool forced,
319                             const bool updateViewer,
320                             SALOME_View* theViewFrame )
321 {
322   if ( theIO.IsNull() )
323     return;
324
325   SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
326
327   if ( vf ) {
328     SALOME_Prs* prs = vf->CreatePrs( theIO->getEntry() );
329     if ( prs ) {
330       vf->Erase( prs, forced );
331       if ( updateViewer )
332         vf->Repaint();
333       delete prs;  // delete presentation because displayer is its owner
334     }
335   }
336 }
337
338 //=================================================================
339 /*!
340  *  GEOM_Displayer::Erase
341  *  Erase geometry object in the current viewer
342  */
343 //=================================================================
344 void GEOM_Displayer::Erase( GEOM::GEOM_Object_ptr theObj,
345                             const bool forced,
346                             const bool updateViewer )
347 {
348   string entry = getEntry( theObj );
349   if ( entry != "" )
350   {
351     Erase(new SALOME_InteractiveObject(entry.c_str(), "GEOM", getName(theObj).c_str()),
352           forced, updateViewer);
353   }
354 }
355
356 //=================================================================
357 /*!
358  *  GEOM_Displayer::Redisplay
359  *  Redisplay (erase and then display again) interactive object
360  *  in the current viewer
361  */
362 //=================================================================
363 void GEOM_Displayer::Redisplay( const Handle(SALOME_InteractiveObject)& theIO,
364                                 const bool updateViewer )
365 {
366   // Remove the object permanently (<forced> == true)
367   SUIT_Session* ses = SUIT_Session::session();
368   SUIT_Application* app = ses->activeApplication();
369   if ( app )
370   {
371     SUIT_Desktop* desk = app->desktop();
372     QPtrList<SUIT_ViewWindow> wnds = desk->windows();
373     SUIT_ViewWindow* wnd;
374     for ( wnd = wnds.first(); wnd; wnd = wnds.next() )
375     {
376       SUIT_ViewManager* vman = wnd->getViewManager();
377       if ( vman )
378       {
379         SUIT_ViewModel* vmodel = vman->getViewModel();
380         if ( vmodel )
381         {
382           SALOME_View* view = dynamic_cast<SALOME_View*>(vmodel);
383           if ( view )
384           {
385             if ( view->isVisible( theIO ) || view == GetActiveView() )
386             {
387               Erase( theIO, true, false, view );
388               Display( theIO, updateViewer, view );
389             }
390           }
391         }
392       }
393     }
394   }
395 }
396
397 //=================================================================
398 /*!
399  *  GEOM_Displayer::Display
400  *  Calls Display() method for each object in the given list
401  */
402 //=================================================================
403 void GEOM_Displayer::Display( const SALOME_ListIO& theIOList, const bool updateViewer )
404 {
405   SALOME_ListIteratorOfListIO Iter( theIOList );
406   for ( ; Iter.More(); Iter.Next() ) {
407     Display( Iter.Value(), false );
408   }
409   if ( updateViewer )
410     UpdateViewer();
411 }
412
413 //=================================================================
414 /*!
415  *  GEOM_Displayer::Erase
416  *  Calls Erase() method for each object in the given list
417  */
418 //=================================================================
419 void GEOM_Displayer::Erase( const SALOME_ListIO& theIOList,
420                             const bool forced,
421                             const bool updateViewer )
422 {
423   SALOME_ListIteratorOfListIO Iter( theIOList );
424   for ( ; Iter.More(); Iter.Next() )
425     Erase( Iter.Value(), forced, false );
426
427   if ( updateViewer )
428     UpdateViewer();
429 }
430
431 //=================================================================
432 /*!
433  *  GEOM_Displayer::Redisplay
434  *  Calls Redisplay() method for each object in the given list
435  */
436 //=================================================================
437 void GEOM_Displayer::Redisplay( const SALOME_ListIO& theIOList, const bool updateViewer )
438 {
439   SALOME_ListIteratorOfListIO Iter( theIOList );
440   for ( ; Iter.More(); Iter.Next() )
441     Redisplay( Iter.Value(), false );
442
443   if ( updateViewer )
444     UpdateViewer();
445 }
446
447 //=================================================================
448 /*!
449  *  GEOM_Displayer::Update
450  *  Update OCC presentaion
451  *  [ Reimplemented from SALOME_Displayer ]
452  */
453 //=================================================================
454 void GEOM_Displayer::Update( SALOME_OCCPrs* prs )
455 {
456   SOCC_Prs* occPrs = dynamic_cast<SOCC_Prs*>( prs );
457   if ( !occPrs )
458     return;
459
460   if ( myType == GEOM_MARKER && !myShape.IsNull() && myShape.ShapeType() == TopAbs_FACE )
461   {
462     TopoDS_Face aFace = TopoDS::Face( myShape );
463     Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast( BRep_Tool::Surface( aFace ) );
464     if ( !aPlane.IsNull() )
465     {
466       gp_Ax3 aPos = aPlane->Pln().Position();
467       Handle(Geom_Axis2Placement) aPlc = new Geom_Axis2Placement( aPos.Ax2() );
468
469       Handle(GEOM_AISTrihedron) aTrh;
470
471       if ( occPrs->IsNull() )
472       {
473         aTrh = new GEOM_AISTrihedron( aPlc );
474
475         if ( HasColor() )
476           aTrh->SetColor( (Quantity_NameOfColor)GetColor() );
477
478         if ( HasWidth() )
479           aTrh->SetWidth( GetWidth() );
480
481         if ( !myIO.IsNull() )
482         {
483           aTrh->setIO( myIO );
484           aTrh->SetOwner( myIO );
485         }
486
487         occPrs->AddObject( aTrh );
488       }
489       else
490       {
491         AIS_ListOfInteractive aList;
492         occPrs->GetObjects( aList );
493         AIS_ListIteratorOfListOfInteractive anIter( aList );
494         for ( ; anIter.More(); anIter.Next() )
495         {
496           aTrh = Handle(GEOM_AISTrihedron)::DownCast( anIter.Value() );
497           if ( !aTrh.IsNull() )
498           {
499             aTrh->SetComponent( aPlc );
500             aTrh->SetToUpdate();
501           }
502         }
503       }
504
505       occPrs->SetToActivate( ToActivate() );
506     }
507   }
508   else
509   {
510     // if presentation is empty we try to create new one
511     if ( occPrs->IsNull() )
512     {
513       if ( !myShape.IsNull() )
514       {
515         Handle(GEOM_AISShape) AISShape = new GEOM_AISShape( myShape, "" );
516         // Temporary staff: vertex must be infinite for correct visualization
517         AISShape->SetInfiniteState( myShape.Infinite() || myShape.ShapeType() == TopAbs_VERTEX );
518
519         // Setup shape properties here ..., e.g. display mode, color, transparency, etc
520         AISShape->SetDisplayMode( myDisplayMode );
521         AISShape->SetShadingColor( myShadingColor );
522
523         if ( HasColor() )
524         {
525           AISShape->SetColor( (Quantity_NameOfColor)GetColor() );
526           if ( myShape.ShapeType() == TopAbs_VERTEX )
527           {
528             Handle(Prs3d_PointAspect) anAspect = AISShape->Attributes()->PointAspect();
529             anAspect->SetColor( (Quantity_NameOfColor)GetColor() );
530             AISShape->Attributes()->SetPointAspect( anAspect );
531           }
532         }
533
534         if ( HasWidth() )
535           AISShape->SetWidth( GetWidth() );
536
537         if ( !myIO.IsNull() )
538         {
539           AISShape->setIO( myIO );
540           AISShape->SetOwner( myIO );
541         }
542         else if ( !myName.empty() )
543         {
544           // Workaround to allow selection of temporary objects
545           static int tempId = 0;
546           char buf[50];
547           sprintf( buf, "TEMP_%d", tempId++ );
548           Handle( SALOME_InteractiveObject ) anObj =
549             new SALOME_InteractiveObject( buf, "GEOM", myName.c_str() );
550           AISShape->setIO( anObj );
551           AISShape->SetOwner( anObj );
552         }
553         // AISShape->SetName(???); ??? necessary to set name ???
554         occPrs->AddObject( AISShape );
555
556         // In accordance with ToActivate() value object will be activated/deactivated
557         // when it will be displayed
558         occPrs->SetToActivate( ToActivate() );
559       }
560     }
561     // if presentation is found -> set again shape for it
562     else
563     {
564       if ( !myShape.IsNull() )
565       {
566         AIS_ListOfInteractive IOList;
567         occPrs->GetObjects( IOList );
568         AIS_ListIteratorOfListOfInteractive Iter( IOList );
569         for ( ; Iter.More(); Iter.Next() )
570         {
571           Handle(GEOM_AISShape) AISShape = Handle(GEOM_AISShape)::DownCast( Iter.Value() );
572           if ( AISShape.IsNull() )
573             continue;
574           if ( AISShape->Shape() != myShape )
575           {
576             AISShape->Set( myShape );
577             AISShape->UpdateSelection();
578             AISShape->SetToUpdate();
579           }
580           if ( !myIO.IsNull() )
581           {
582             AISShape->setIO( myIO );
583             AISShape->SetOwner( myIO );
584           }
585         }
586       }
587     }
588   }
589 }
590
591 //=================================================================
592 /*!
593  *  GEOM_Displayer::Update
594  *  Update VTK presentaion
595  *  [ Reimplemented from SALOME_Displayer ]
596  */
597 //=================================================================
598 void GEOM_Displayer::Update( SALOME_VTKPrs* prs )
599 {
600   SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
601   if ( !vtkPrs || myShape.IsNull() )
602     return;
603
604   vtkActorCollection* theActors = 0;
605
606   if ( myType == GEOM_MARKER && !myShape.IsNull() && myShape.ShapeType() == TopAbs_FACE )
607   {
608     myToActivate = false;
609     GEOM_VTKTrihedron* aTrh = GEOM_VTKTrihedron::New();
610
611     if ( HasColor() )
612     {
613       Quantity_Color aColor( (Quantity_NameOfColor)GetColor() );
614       aTrh->SetColor( aColor.Red(), aColor.Green(), aColor.Blue() );
615     }
616
617     Handle(Geom_Plane) aPlane =
618       Handle(Geom_Plane)::DownCast( BRep_Tool::Surface( TopoDS::Face( myShape ) ) );
619     if ( aPlane.IsNull() )
620       return;
621
622     gp_Ax2 anAx2 = aPlane->Pln().Position().Ax2();
623     aTrh->SetPlacement( new Geom_Axis2Placement( anAx2 ) );
624
625 //    if ( SVTK_Viewer* vf = dynamic_cast<SVTK_Viewer*>( GetActiveView() ) )
626 //      aTrh->SetSize( 0.5 * vf->GetTrihedronSize() );
627
628     vtkPrs->AddObject( aTrh );
629
630     theActors = vtkActorCollection::New();
631     theActors->AddItem( aTrh );
632   }
633   else
634     theActors = GEOM_AssemblyBuilder::BuildActors( myShape, 0, 0, Standard_True );
635
636   theActors->InitTraversal();
637
638   vtkActor* anActor = (vtkActor*)theActors->GetNextActor();
639
640   vtkProperty* aProp = 0;
641
642   if ( HasColor() || HasWidth() )
643   {
644     aProp = vtkProperty::New();
645     aProp->SetRepresentationToWireframe();
646   }
647
648   if ( HasColor() )
649   {
650     Quantity_Color aColor( (Quantity_NameOfColor)GetColor() );
651     aProp->SetColor( aColor.Red(), aColor.Green(), aColor.Blue() );
652   }
653
654   if ( HasWidth() )
655   {
656     aProp->SetLineWidth( GetWidth() );
657   }
658
659   while ( anActor != NULL )
660   {
661     SALOME_Actor* GActor = SALOME_Actor::SafeDownCast( anActor );
662
663     GActor->setIO( myIO );
664
665     if ( aProp )
666     {
667       GActor->SetProperty( aProp );
668       GActor->SetPreviewProperty( aProp );
669
670       GEOM_Actor* aGeomGActor = GEOM_Actor::SafeDownCast( anActor );
671       if ( aGeomGActor != 0 )
672       {
673         aGeomGActor->SetShadingProperty( aProp );
674         aGeomGActor->SetWireframeProperty( aProp );
675       }
676     }
677
678     if ( myToActivate )
679       GActor->PickableOn();
680     else
681       GActor->PickableOff();
682
683     vtkPrs->AddObject( GActor );
684
685     anActor = (vtkActor*)theActors->GetNextActor();
686   }
687
688   if ( aProp )
689     aProp->Delete();
690
691   theActors->Delete();
692 }
693
694 //=================================================================
695 /*!
696  *  GEOM_Displayer::BuildPrs
697  *  Build presentation accordint to the current viewer type
698  */
699 //=================================================================
700 SALOME_Prs* GEOM_Displayer::BuildPrs( GEOM::GEOM_Object_ptr theObj )
701 {
702   if ( theObj->_is_nil() )
703     return 0;
704
705   myViewFrame = GetActiveView();
706   if ( myViewFrame == 0 )
707     return 0;
708
709   SALOME_Prs* aPrs = myViewFrame->CreatePrs();
710   if ( aPrs == 0 )
711     return 0;
712
713   internalReset();
714   setShape( GEOM_Client().GetShape( GeometryGUI::GetGeomGen(), theObj ) );
715   myType = theObj->GetType();
716
717   // Update presentation
718   UpdatePrs( aPrs );
719
720   return aPrs;
721 }
722
723 //=================================================================
724 /*!
725  *  GEOM_Displayer::BuildPrs
726  *  Build presentation accordint to the current viewer type
727  */
728 //=================================================================
729 SALOME_Prs* GEOM_Displayer::BuildPrs( const TopoDS_Shape& theShape )
730 {
731   myViewFrame = GetActiveView();
732   if ( theShape.IsNull() || myViewFrame == 0 )
733     return 0;
734
735   SALOME_Prs* aPrs = myViewFrame->CreatePrs();
736   if ( aPrs == 0 )
737     return 0;
738
739   internalReset();
740   setShape( theShape );
741   myType = -1;
742
743   UpdatePrs( aPrs );
744
745   return aPrs;
746 }
747
748 //=================================================================
749 /*!
750  *  GEOM_Displayer::buildPresentation
751  *  Builds/finds object's presentation for the current viewer
752  *  Calls corresponding Update() method by means of double dispatch
753  *  [ internal ]
754  */
755 //=================================================================
756 SALOME_Prs* GEOM_Displayer::buildPresentation( const QString& entry,
757                                                SALOME_View* theViewFrame )
758 {
759   SALOME_Prs* prs = 0;
760   internalReset();
761
762   myViewFrame = theViewFrame ? theViewFrame : GetActiveView();
763
764   if ( myViewFrame )
765   {
766     prs = LightApp_Displayer::buildPresentation( entry, theViewFrame );
767     if ( prs )
768     {
769       Handle( SALOME_InteractiveObject ) theIO = new SALOME_InteractiveObject();
770       theIO->setEntry( entry.latin1() );
771       if ( !theIO.IsNull() )
772       {
773         // set interactive object
774         setIO( theIO );
775         //  Find SOBject (because shape should be published previously)
776         SUIT_Session* session = SUIT_Session::session();
777         SUIT_Application* app = session->activeApplication();
778         if ( app )
779         {
780           SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
781           if ( study )
782           {
783             _PTR(SObject) SO ( study->studyDS()->FindObjectID( theIO->getEntry() ) );
784             if ( SO )
785             {
786               // get CORBA reference to data object
787               CORBA::Object_var object = GeometryGUI::ClientSObjectToObject(SO);
788               if ( !CORBA::is_nil( object ) )
789               {
790                 // downcast to GEOM object
791                 GEOM::GEOM_Object_var GeomObject = GEOM::GEOM_Object::_narrow( object );
792                 if ( !GeomObject->_is_nil() )
793                 {
794                   // finally set shape
795                   setShape( GEOM_Client().GetShape( GeometryGUI::GetGeomGen(), GeomObject ) );
796                   myType = GeomObject->GetType();
797                 }
798               }
799             }
800           }
801         }
802       }
803       UpdatePrs( prs );  // Update presentation by using of the double dispatch
804     }
805   }
806   return prs;
807 }
808
809 //=================================================================
810 /*!
811  *  GEOM_Displayer::internalReset
812  *  Resets internal data
813  *  [internal]
814  */
815 //=================================================================
816 void GEOM_Displayer::internalReset()
817 {
818   myIO.Nullify();
819   myShape.Nullify();
820 }
821
822 //=================================================================
823 /*!
824  *  GEOM_Displayer::LocalSelection
825  *  Activate selection of CAD shapes with activisation of selection
826  *  of their sub-shapes (with opened local context for OCC viewer)
827  */
828 //=================================================================
829 void GEOM_Displayer::LocalSelection( const Handle(SALOME_InteractiveObject)& theIO, const int theMode )
830 {
831   SUIT_Session* session = SUIT_Session::session();
832   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
833   if ( !app )
834     return;
835
836   LightApp_SelectionMgr* sm = app->selectionMgr();
837
838   // remove all filters from selection
839   sm->clearFilters();
840
841   SALOME_View* vf = GetActiveView();
842   if ( vf ) {
843     if (!theIO.IsNull() && !vf->isVisible(theIO))
844       Display(theIO);
845     SALOME_Prs* prs = vf->CreatePrs( theIO.IsNull() ? 0 : theIO->getEntry() );
846     vf->LocalSelection( prs, theMode );
847     delete prs;  // delete presentation because displayer is its owner
848   }
849 }
850
851 //=================================================================
852 /*!
853  *  GEOM_Displayer::globalSelection
854  *  Activate selection of CAD shapes without activisation of selection
855  *  of their sub-shapes (without opened local context for OCC viewer)
856  */
857 //=================================================================
858 void GEOM_Displayer::GlobalSelection( const int theMode, const bool update )
859 {
860   TColStd_MapOfInteger aModes;
861   aModes.Add( theMode );
862   GlobalSelection( aModes, update );
863 }
864
865 //=================================================================
866 /*!
867  *  GEOM_Displayer::globalSelection
868  *  Activate selection of CAD shapes without activisation of selection
869  *  of their sub-shapes (without opened local context for OCC viewer)
870  */
871 //=================================================================
872 void GEOM_Displayer::GlobalSelection( const TColStd_MapOfInteger& theModes,
873                                       const bool update )
874 {
875   SUIT_Session* session = SUIT_Session::session();
876   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
877   if ( !app )
878     return;
879
880   SALOME_View* vf = GetActiveView();
881   if ( vf == 0 )
882     return;
883
884   // Close local context
885   vf->GlobalSelection( update );
886
887   // Set selection filters in accordance with current mode
888   LightApp_SelectionMgr* sm = app->selectionMgr();
889   if ( !sm )
890     return;
891
892   // Remove from selection temporary objects if necessary
893   if ( !theModes.Contains( GEOM_PREVIEW ) )
894     clearTemporary( sm );
895
896   //@ aSel->ClearIndex();
897
898   sm->clearFilters();
899
900   // Remove filters from AIS_InteractiveContext
901   Handle(AIS_InteractiveContext) ic;
902   SOCC_Viewer* viewer = dynamic_cast<SOCC_Viewer*>( vf );
903   if ( viewer )
904     {
905       ic = viewer->getAISContext();
906       if ( !ic.IsNull() )
907         ic->RemoveFilters();
908     }
909
910   if ( theModes.Contains( GEOM_ALLOBJECTS ) )
911     return;
912
913   SUIT_SelectionFilter* aFilter;
914   if ( theModes.Extent() == 1 )
915     {
916       int aMode = TColStd_MapIteratorOfMapOfInteger( theModes ).Key();
917       aFilter = getFilter( aMode );
918     }
919   else if ( theModes.Extent() > 1 )
920     {
921       TColStd_MapOfInteger aTopAbsModes;
922       TColStd_MapIteratorOfMapOfInteger anIter( theModes );
923       QPtrList<SUIT_SelectionFilter> aListOfFilters;
924       for ( ; anIter.More(); anIter.Next() )
925         {
926           SUIT_SelectionFilter* aFilter = getFilter( anIter.Key() );
927           if ( aFilter )
928             aListOfFilters.append( aFilter );
929         }
930
931       aFilter = new GEOM_LogicalFilter( aListOfFilters, GEOM_LogicalFilter::LO_OR );
932     }
933   else
934     return;
935
936   if ( aFilter )
937     {
938       sm->installFilter( aFilter );
939       if ( !ic.IsNull() )
940         {
941           Handle(GEOM_OCCFilter) anOCCFilter = new GEOM_OCCFilter( sm );
942           ic->AddFilter( anOCCFilter );
943         }
944     }
945 }
946
947 //=================================================================
948 /*!
949  *  GEOM_Displayer::LocalSelection
950  *  Activate selection of CAD shapes with activisation of selection
951  *  of their sub-shapes (with opened local context for OCC viewer)
952  */
953 //=================================================================
954 void GEOM_Displayer::LocalSelection( const SALOME_ListIO& theIOList, const int theMode )
955 {
956   SALOME_ListIteratorOfListIO Iter( theIOList );
957   for ( ; Iter.More(); Iter.Next() )
958     LocalSelection( Iter.Value(), theMode );
959 }
960
961 //=================================================================
962 /*!
963  *  GEOM_Displayer::BeforeDisplay
964  *  Called before displaying of pars. Close local context
965  *  [ Reimplemented from SALOME_Displayer ]
966  */
967 //=================================================================
968 void GEOM_Displayer::BeforeDisplay( SALOME_View* v, const SALOME_OCCViewType& )
969 {
970   SOCC_Viewer* vf = dynamic_cast<SOCC_Viewer*>( v );
971   if ( vf )
972   {
973     Handle(AIS_InteractiveContext) ic = vf->getAISContext();
974     if ( !ic.IsNull() )
975     {
976       if ( ic->HasOpenedContext() )
977       ic->CloseAllContexts();
978     }
979   }
980 }
981
982 void GEOM_Displayer::AfterDisplay( SALOME_View*, const SALOME_OCCViewType& )
983 {
984 }
985
986
987 //=================================================================
988 /*!
989  *  GEOM_Displayer::SetColor
990  *  Set color for shape displaying. If it is equal -1 then default color is used.
991  *  Available values are from Quantity_NameOfColor enumeration
992  */
993 //=================================================================
994 void GEOM_Displayer::SetColor( const int color )
995 {
996   myColor = color;
997 }
998
999 int GEOM_Displayer::GetColor() const
1000 {
1001   return myColor;
1002 }
1003
1004 bool GEOM_Displayer::HasColor() const
1005 {
1006   return myColor != -1;
1007 }
1008
1009 void GEOM_Displayer::UnsetColor()
1010 {
1011   myColor = -1;
1012 }
1013
1014 //=================================================================
1015 /*!
1016  *  GEOM_Displayer::SetWidth
1017  *  Set width of shape displaying. If it is equal -1 then default width is used.
1018  */
1019 //=================================================================
1020 void GEOM_Displayer::SetWidth( const double width )
1021 {
1022   myWidth = width;
1023 }
1024
1025 double GEOM_Displayer::GetWidth() const
1026 {
1027   return myWidth;
1028 }
1029
1030 bool GEOM_Displayer::HasWidth() const
1031 {
1032   return myWidth != -1;
1033 }
1034
1035 void GEOM_Displayer::UnsetWidth()
1036 {
1037   myWidth = -1;
1038 }
1039
1040 //=================================================================
1041 /*!
1042  *  GEOM_Displayer::SetToActivate
1043  *  This method is used for activisation/deactivisation of objects to be displayed
1044  */
1045 //=================================================================
1046 void GEOM_Displayer::SetToActivate( const bool toActivate )
1047 {
1048   myToActivate = toActivate;
1049 }
1050 bool GEOM_Displayer::ToActivate() const
1051 {
1052   return myToActivate;
1053 }
1054
1055 //=================================================================
1056 /*!
1057  *  GEOM_Displayer::clearTemporary
1058  *  Removes from selection temporary objects
1059  */
1060 //=================================================================
1061 void GEOM_Displayer::clearTemporary( LightApp_SelectionMgr* theSelMgr )
1062 {
1063   SALOME_ListIO selected, toSelect;
1064   theSelMgr->selectedObjects( selected );
1065
1066   for (  SALOME_ListIteratorOfListIO it( selected ) ; it.More(); it.Next() ) {
1067     Handle(SALOME_InteractiveObject) io = it.Value();
1068     if ( !io.IsNull() && io->hasEntry() && strncmp( io->getEntry(), "TEMP_", 5 ) != 0 )
1069       toSelect.Append( it.Value() );
1070   }
1071
1072   theSelMgr->setSelectedObjects( toSelect, true );
1073 }
1074
1075 void GEOM_Displayer::SetName( const char* theName )
1076 {
1077   myName = theName;
1078 }
1079
1080 void GEOM_Displayer::UnsetName()
1081 {
1082   myName = "";
1083 }
1084
1085 SalomeApp_Study* GEOM_Displayer::getStudy() const
1086 {
1087   return dynamic_cast<SalomeApp_Study*>( myApp->activeStudy() );
1088 }
1089
1090 void GEOM_Displayer::setIO( const Handle(SALOME_InteractiveObject)& theIO )
1091 {
1092   myIO = theIO;
1093 }
1094
1095 void GEOM_Displayer::setShape( const TopoDS_Shape& theShape )
1096 {
1097   myShape = theShape;
1098 }
1099
1100 bool GEOM_Displayer::canBeDisplayed( const QString& /*entry*/, const QString& viewer_type ) const
1101 {
1102   return viewer_type==SOCC_Viewer::Type() || viewer_type==SVTK_Viewer::Type();
1103 }