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