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