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