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