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