Salome HOME
PR: merged from V5_1_4rc1
[modules/smesh.git] / src / OBJECT / SMESH_PreviewActorsCollection.cxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  This library is free software; you can redistribute it and/or
4 //  modify it under the terms of the GNU Lesser General Public
5 //  License as published by the Free Software Foundation; either
6 //  version 2.1 of the License.
7 //
8 //  This library is distributed in the hope that it will be useful,
9 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 //  Lesser General Public License for more details.
12 //
13 //  You should have received a copy of the GNU Lesser General Public
14 //  License along with this library; if not, write to the Free Software
15 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 //  SMESH OBJECT : interactive object for SMESH visualization
21 //  File   : SMESH_PreviewActorsCollection.cxx
22 //  Author : 
23 //  Module : SMESH
24 //
25 #include "SMESH_PreviewActorsCollection.h"
26
27 #include "utilities.h"
28
29 #include "SALOME_InteractiveObject.hxx"
30
31 // OCC includes
32 #include <TopoDS.hxx>
33 #include <TopExp.hxx>
34 #include <TopExp_Explorer.hxx>
35
36 // VTK includes
37 #include <vtkUnstructuredGrid.h>
38 #include <vtkScalarBarActor.h>
39 #include <vtkPlane.h>
40 #include <vtkRenderer.h>
41 #include <vtkProperty.h>
42
43 #include "VTKViewer_Actor.h"
44
45 #include "SVTK_DeviceActor.h"
46 #include "SALOME_Actor.h"
47
48 // QT
49 #include <QString>
50 #include <QColor>
51
52 #ifdef _DEBUG_
53 static int MYDEBUG = 0;
54 #else
55 static int MYDEBUG = 0;
56 #endif
57
58 using namespace std;
59
60
61 //vtkStandardNewMacro(SMESH_PreviewActorsCollection);
62
63
64 SMESH_PreviewActorsCollection
65 ::SMESH_PreviewActorsCollection()
66 {
67   if(MYDEBUG) MESSAGE("SMESH_PreviewActorsCollection - "<<this);
68 }
69
70
71 SMESH_PreviewActorsCollection
72 ::~SMESH_PreviewActorsCollection()
73 {
74   if(MYDEBUG) MESSAGE("~SMESH_PreviewActorsCollection - "<<this);
75   if (myRenderer)
76     RemoveFromRender(myRenderer);
77
78   QMap<int, GEOM_Actor*>::iterator iter = myMapOfActors.begin();
79   for ( ; iter != myMapOfActors.end(); ++iter )
80     if ( GEOM_Actor* anActor = iter.value() )
81       anActor->Delete();
82   myMapOfActors.clear();
83 }
84
85 bool SMESH_PreviewActorsCollection::Init( const TopoDS_Shape& theShape, TopAbs_ShapeEnum theType, const QString& theEntry )
86 {
87   myMainShape = theShape;
88   myMapOfActors.clear();
89   myMapOfShapes.Clear();
90
91   if ( theShape.IsNull() )
92     return false;
93
94   Handle( SALOME_InteractiveObject ) anIO = new SALOME_InteractiveObject();
95   anIO->setEntry( theEntry.toLatin1().constData() );
96   
97   // get indexes of seleted elements
98   TopExp::MapShapes(theShape, myMapOfShapes);
99
100   TopExp_Explorer exp( theShape, theType );
101   for ( ; exp.More(); exp.Next() ) {
102     int index = myMapOfShapes.FindIndex( exp.Current() );
103     if ( index && !myMapOfActors.contains( index ) ) { 
104       // create actor if the index is present
105       if ( GEOM_Actor* anActor = createActor( exp.Current() )) {
106         // Create new entry for actor
107         QString aString = theEntry;
108         aString += QString("_%1").arg( index ); // add index to actor entry
109
110         // Create interactive object
111         Handle( SALOME_InteractiveObject ) anIO = new SALOME_InteractiveObject();
112         anIO->setEntry( aString.toLatin1().constData() );
113
114         // Init Actor
115         anActor->SetVectorMode( true );
116         anActor->setIO( anIO );
117         anActor->SetSelector( mySelector );
118         anActor->SetPickable( true );
119         anActor->SetResolveCoincidentTopology( true );
120
121         // Add Actor to the Actors Map
122         myMapOfActors.insert(index, anActor);
123       }
124     }
125   }
126   mySelector->ClearIObjects();
127
128   return true;
129 }
130
131 GEOM_Actor* SMESH_PreviewActorsCollection::createActor(const TopoDS_Shape& shape)
132 {
133   GEOM_Actor* actor = GEOM_Actor::New();
134   actor->SetShape(shape,0,0);
135
136   //Color Properties
137   /*    
138         vtkProperty* aProp = vtkProperty::New();
139         vtkProperty* aHLProp = vtkProperty::New();
140         vtkProperty* aPHLProp = vtkProperty::New();
141         
142         aProp->SetColor( 255, 0, 0);
143         actor->SetProperty(aProp);
144
145         aHLProp->SetColor( 255, 255, 255);
146         actor->SetHighlightProperty(aHLProp);
147
148         aPHLProp->SetColor( 155, 155, 155);
149         aPHLProp->SetLineWidth ( 3 );
150         aPHLProp->SetOpacity ( 0.75 );
151         actor->SetPreHighlightProperty(aPHLProp);
152
153         aProp->Delete();
154         aHLProp->Delete();
155         aPHLProp->Delete();
156   */
157
158   return actor;
159 }
160
161 GEOM_Actor* SMESH_PreviewActorsCollection::GetActorByIndex(int index)
162 {
163   return myMapOfActors.value(index);
164 }
165
166 int SMESH_PreviewActorsCollection::GetIndexByShape( const TopoDS_Shape& theShape )
167 {
168   return myMapOfShapes.FindIndex( theShape );
169 }
170
171 void SMESH_PreviewActorsCollection::AddToRender(vtkRenderer* theRenderer)
172 {
173   myRenderer = theRenderer;
174
175   QMap<int, GEOM_Actor*>::iterator iter = myMapOfActors.begin();
176   for ( ; iter != myMapOfActors.end(); ++iter )
177     iter.value()->AddToRender( theRenderer );
178 }
179
180 void SMESH_PreviewActorsCollection::RemoveFromRender(vtkRenderer* theRenderer){
181   QMap<int, GEOM_Actor*>::iterator iter = myMapOfActors.begin();
182   for ( ; iter != myMapOfActors.end(); ++iter )
183     iter.value()->RemoveFromRender( theRenderer );
184 }
185
186 void SMESH_PreviewActorsCollection::SetSelector(SVTK_Selector* theSelector)
187 {
188   mySelector = theSelector;
189 }
190
191 void SMESH_PreviewActorsCollection::HighlightAll( bool theHighlight ){
192   QMap<int, GEOM_Actor*>::iterator iter = myMapOfActors.begin();
193   for ( ; iter != myMapOfActors.end(); ++iter )
194     iter.value()->Highlight( theHighlight );
195 }
196
197 void SMESH_PreviewActorsCollection::HighlightID( int index ){
198   GEOM_Actor* anActor = GetActorByIndex( index );
199   if ( anActor && !anActor->isHighlighted() )
200     anActor->Highlight( true );
201 }
202
203 void SMESH_PreviewActorsCollection::SetShown( bool shown ){
204   QMap<int, GEOM_Actor*>::iterator iter = myMapOfActors.begin();
205   for ( ; iter != myMapOfActors.end(); ++iter )
206     iter.value()->SetVisibility( shown );
207 }