Salome HOME
0020082: EDF 869 GEOM : Edges Orientation indicator/reverse
[modules/smesh.git] / src / OBJECT / SMESH_PreviewActorsCollection.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  SMESH OBJECT : interactive object for SMESH visualization
23 //  File   : SMESH_PreviewActorsCollection.cxx
24 //  Author : 
25 //  Module : SMESH
26 //
27
28 #include "SMESH_PreviewActorsCollection.h"
29
30 #include "utilities.h"
31
32 #include "SALOME_InteractiveObject.hxx"
33
34 // OCC includes
35 #include <TopoDS.hxx>
36 #include <TopExp.hxx>
37 #include <TopExp_Explorer.hxx>
38
39 // VTK includes
40 #include <vtkUnstructuredGrid.h>
41 #include <vtkScalarBarActor.h>
42 #include <vtkPlane.h>
43 #include <vtkRenderer.h>
44 #include <vtkProperty.h>
45
46 #include "VTKViewer_Actor.h"
47
48 #include "SVTK_DeviceActor.h"
49 #include "SALOME_Actor.h"
50
51 // QT
52 #include <QString>
53 #include <QColor>
54
55 #ifdef _DEBUG_
56 static int MYDEBUG = 0;
57 #else
58 static int MYDEBUG = 0;
59 #endif
60
61 using namespace std;
62
63
64 //vtkStandardNewMacro(SMESH_PreviewActorsCollection);
65
66
67 SMESH_PreviewActorsCollection
68 ::SMESH_PreviewActorsCollection()
69 {
70   if(MYDEBUG) MESSAGE("SMESH_PreviewActorsCollection - "<<this);
71 }
72
73
74 SMESH_PreviewActorsCollection
75 ::~SMESH_PreviewActorsCollection()
76 {
77   if(MYDEBUG) MESSAGE("~SMESH_PreviewActorsCollection - "<<this);
78   if (myRenderer)
79     RemoveFromRender(myRenderer);
80
81   myMapOfActors.clear();
82 }
83
84 bool SMESH_PreviewActorsCollection::Init( const TopoDS_Shape& theShape, TopAbs_ShapeEnum theType, const QString& theEntry )
85 {
86   myMainShape = theShape;
87   myMapOfActors.clear();
88   myMapOfShapes.Clear();
89
90   if ( theShape.IsNull() )
91     return false;
92
93   Handle( SALOME_InteractiveObject ) anIO = new SALOME_InteractiveObject();
94   anIO->setEntry( theEntry.toLatin1().constData() );
95   
96   // get indexes of seleted elements
97   TopExp::MapShapes(theShape, myMapOfShapes);
98
99   TopExp_Explorer exp( theShape, theType );
100   for ( ; exp.More(); exp.Next() ) {
101     int index = myMapOfShapes.FindIndex( exp.Current() );
102     if ( index ) { 
103       // create actor if the index is present
104       if ( GEOM_Actor* anActor = createActor( exp.Current() )) {
105         // Create new entry for actor
106         QString aString = theEntry;
107         aString += QString("_%1").arg( index ); // add index to actor entry
108
109         // Create interactive object
110         Handle( SALOME_InteractiveObject ) anIO = new SALOME_InteractiveObject();
111         anIO->setEntry( aString.toLatin1().constData() );
112
113         // Init Actor
114         anActor->SetVectorMode( true );
115         anActor->setIO( anIO );
116         anActor->SetSelector( mySelector );
117         anActor->SetPickable( true );
118         anActor->SetResolveCoincidentTopology( true );
119
120         // Add Actor to the Actors Map
121         myMapOfActors.insert(index, anActor);
122       }
123     }
124   }
125   mySelector->ClearIObjects();
126
127   return true;
128 }
129
130 GEOM_Actor* SMESH_PreviewActorsCollection::createActor(const TopoDS_Shape& shape)
131 {
132   GEOM_Actor* actor = GEOM_Actor::New();
133   actor->SetShape(shape,0,0);
134
135   //Color Properties
136   /*    
137         vtkProperty* aProp = vtkProperty::New();
138         vtkProperty* aHLProp = vtkProperty::New();
139         vtkProperty* aPHLProp = vtkProperty::New();
140         
141         aProp->SetColor( 255, 0, 0);
142         actor->SetProperty(aProp);
143
144         aHLProp->SetColor( 255, 255, 255);
145         actor->SetHighlightProperty(aHLProp);
146
147         aPHLProp->SetColor( 155, 155, 155);
148         aPHLProp->SetLineWidth ( 3 );
149         aPHLProp->SetOpacity ( 0.75 );
150         actor->SetPreHighlightProperty(aPHLProp);
151
152         aProp->Delete();
153         aHLProp->Delete();
154         aPHLProp->Delete();
155   */
156
157   return actor;
158 }
159
160 GEOM_Actor* SMESH_PreviewActorsCollection::GetActorByIndex(int index)
161 {
162   return myMapOfActors.value(index);
163 }
164
165 int SMESH_PreviewActorsCollection::GetIndexByShape( const TopoDS_Shape& theShape )
166 {
167   return myMapOfShapes.FindIndex( theShape );
168 }
169
170 void SMESH_PreviewActorsCollection::AddToRender(vtkRenderer* theRenderer)
171 {
172   myRenderer = theRenderer;
173
174   QMap<int, GEOM_Actor*>::iterator iter = myMapOfActors.begin();
175   for ( ; iter != myMapOfActors.end(); ++iter )
176     iter.value()->AddToRender( theRenderer );
177 }
178
179 void SMESH_PreviewActorsCollection::RemoveFromRender(vtkRenderer* theRenderer){
180   QMap<int, GEOM_Actor*>::iterator iter = myMapOfActors.begin();
181   for ( ; iter != myMapOfActors.end(); ++iter )
182     iter.value()->RemoveFromRender( theRenderer );
183 }
184
185 void SMESH_PreviewActorsCollection::SetSelector(SVTK_Selector* theSelector)
186 {
187   mySelector = theSelector;
188 }
189
190 void SMESH_PreviewActorsCollection::HighlightAll( bool theHighlight ){
191   QMap<int, GEOM_Actor*>::iterator iter = myMapOfActors.begin();
192   for ( ; iter != myMapOfActors.end(); ++iter )
193     iter.value()->Highlight( theHighlight );
194 }
195
196 void SMESH_PreviewActorsCollection::HighlightID( int index ){
197   GEOM_Actor* anActor = GetActorByIndex( index );
198   if ( anActor && !anActor->isHighlighted() )
199     anActor->Highlight( true );
200 }
201
202 void SMESH_PreviewActorsCollection::SetShown( bool shown ){
203   QMap<int, GEOM_Actor*>::iterator iter = myMapOfActors.begin();
204   for ( ; iter != myMapOfActors.end(); ++iter )
205     iter.value()->SetVisibility( shown );
206 }