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