Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/smesh.git] / src / OBJECT / SMESH_PreviewActorsCollection.cxx
1 // Copyright (C) 2007-2012  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 #include "SMESH_PreviewActorsCollection.h"
21
22 #include <utilities.h>
23
24 // OCC includes
25 #include <TopoDS.hxx>
26 #include <TopExp.hxx>
27 #include <TopExp_Explorer.hxx>
28
29 // VTK includes
30 #include <vtkUnstructuredGrid.h>
31 #include <vtkPlane.h>
32 #include <vtkRenderer.h>
33 #include <vtkProperty.h>
34
35 #include <QSet>
36
37 // GEOM includes
38 #include <GEOM_Actor.h>
39
40 // GUI includes
41 #include <VTKViewer_Actor.h>
42 #include <SVTK_DeviceActor.h>
43 #include <SALOME_InteractiveObject.hxx>
44 #include <SUIT_ResourceMgr.h>
45 #include <SUIT_Session.h>
46
47 #ifdef _DEBUG_
48 static int MYDEBUG = 0;
49 #else
50 static int MYDEBUG = 0;
51 #endif
52
53
54 SMESH_PreviewActorsCollection::SMESH_PreviewActorsCollection() :
55   mySelector( 0 ), myRenderer( 0 ), myCurrentChunk( 0 ), myChunkSize( 0 ), myIsShown( true )
56 {
57   if(MYDEBUG) MESSAGE("SMESH_PreviewActorsCollection - "<<this);
58 }
59
60
61 SMESH_PreviewActorsCollection::~SMESH_PreviewActorsCollection()
62 {
63   if(MYDEBUG) MESSAGE("~SMESH_PreviewActorsCollection - "<<this);
64   clearActors();
65 }
66
67 bool SMESH_PreviewActorsCollection::Init( const TopoDS_Shape& theShape,
68                                           TopAbs_ShapeEnum theType,
69                                           const QString& theEntry )
70 {
71   SUIT_ResourceMgr* mgr = SUIT_Session::session()->resourceMgr();
72
73   myType =  theType;
74   myEntry = theEntry;
75   myMainShape = theShape;
76   myMapOfActors.clear();
77   myMapOfShapes.Clear();
78   myIndices.clear();
79   myCurrentChunk = 0;
80   myChunkSize = mgr->integerValue( "SMESH", "preview_actor_chunk_size", 100 );
81
82   if ( theShape.IsNull() )
83     return false;
84
85   Handle( SALOME_InteractiveObject ) anIO = new SALOME_InteractiveObject();
86   anIO->setEntry( theEntry.toLatin1().constData() );
87   
88   // get indexes of seleted elements
89   TopExp::MapShapes( theShape, myMapOfShapes );
90   TopExp_Explorer exp( theShape, theType );
91   QSet<int> indices;
92   for ( ; exp.More(); exp.Next() )
93     indices << myMapOfShapes.FindIndex( exp.Current() );
94   myIndices = indices.toList();
95   qSort(myIndices);
96
97   // show current chunk
98   showCurrentChunk();
99
100   return count() > 0;
101 }
102
103 GEOM_Actor* SMESH_PreviewActorsCollection::createActor(const TopoDS_Shape& shape)
104 {
105   GEOM_Actor* actor = GEOM_Actor::New();
106   actor->SetShape( shape, 0, 0 );
107   return actor;
108 }
109
110 GEOM_Actor* SMESH_PreviewActorsCollection::GetActorByIndex(int index)
111 {
112   return myMapOfActors.value( index );
113 }
114
115 int SMESH_PreviewActorsCollection::GetIndexByShape( const TopoDS_Shape& theShape )
116 {
117   return myMapOfShapes.FindIndex( theShape );
118 }
119
120 void SMESH_PreviewActorsCollection::AddToRender(vtkRenderer* theRenderer)
121 {
122   myRenderer = theRenderer;
123
124   QMap<int, GEOM_Actor*>::iterator iter = myMapOfActors.begin();
125   for ( ; iter != myMapOfActors.end(); ++iter ) {
126     iter.value()->SetVisibility( myIsShown );
127     iter.value()->AddToRender( theRenderer );
128   }
129 }
130
131 void SMESH_PreviewActorsCollection::RemoveFromRender(vtkRenderer* theRenderer)
132 {
133   QMap<int, GEOM_Actor*>::iterator iter = myMapOfActors.begin();
134   for ( ; iter != myMapOfActors.end(); ++iter )
135     iter.value()->RemoveFromRender( theRenderer );
136 }
137
138 void SMESH_PreviewActorsCollection::SetSelector(SVTK_Selector* theSelector)
139 {
140   mySelector = theSelector;
141 }
142
143 void SMESH_PreviewActorsCollection::HighlightAll( bool theHighlight )
144 {
145   QMap<int, GEOM_Actor*>::iterator iter = myMapOfActors.begin();
146   for ( ; iter != myMapOfActors.end(); ++iter )
147     iter.value()->Highlight( theHighlight );
148 }
149
150 void SMESH_PreviewActorsCollection::HighlightID( int index )
151 {
152   GEOM_Actor* anActor = GetActorByIndex( index );
153   if ( anActor && !anActor->isHighlighted() )
154     anActor->Highlight( true );
155 }
156
157 void SMESH_PreviewActorsCollection::SetShown( bool shown )
158 {
159   myIsShown = shown;
160   QMap<int, GEOM_Actor*>::iterator iter = myMapOfActors.begin();
161   for ( ; iter != myMapOfActors.end(); ++iter )
162     iter.value()->SetVisibility( shown );
163 }
164
165 int SMESH_PreviewActorsCollection::count() const
166 {
167   return myIndices.count();
168 }
169
170 int SMESH_PreviewActorsCollection::chunkSize() const
171 {
172   return myChunkSize;
173 }
174
175 int SMESH_PreviewActorsCollection::currentChunk() const
176 {
177   return myCurrentChunk;
178 }
179
180 bool SMESH_PreviewActorsCollection::hasPrevious() const
181 {
182   return chunkSize() > 0 && currentChunk() > 0;
183 }
184
185 bool SMESH_PreviewActorsCollection::hasNext() const
186 {
187   return chunkSize() > 0 && (currentChunk()+1)*chunkSize() < count();
188 }
189
190 void SMESH_PreviewActorsCollection::previous()
191 {
192   if ( !hasPrevious() ) return;
193   myCurrentChunk--;
194   showCurrentChunk();
195 }
196
197 void SMESH_PreviewActorsCollection::next()
198 {
199   if ( !hasNext() ) return;
200   myCurrentChunk++;
201   showCurrentChunk();
202 }
203
204 void SMESH_PreviewActorsCollection::showCurrentChunk()
205 {
206   clearActors();
207   int imin = currentChunk() * chunkSize();
208   int imax = std::min( (currentChunk()+1) * chunkSize(), count() );
209   for ( int i = imin; i < imax; i++ ) {
210     int index = myIndices[i];
211     if ( !index || myMapOfActors.contains( index ) ) continue;
212     TopoDS_Shape s = myMapOfShapes.FindKey( index );
213     if ( s.IsNull() ) continue;
214     // create actor if the index is present
215     if ( GEOM_Actor* anActor = createActor( s.Oriented(TopAbs_FORWARD))) {
216       // Create new entry for actor
217       QString entry = QString( "%1_%2" ).arg( myEntry ).arg( index );
218       // Create interactive object
219       Handle( SALOME_InteractiveObject ) anIO = new SALOME_InteractiveObject();
220       anIO->setEntry( entry.toLatin1().constData() );
221       // Init Actor
222       anActor->SetVectorMode( myType==TopAbs_EDGE );
223       anActor->setIO( anIO );
224       anActor->SetSelector( mySelector );
225       anActor->SetPickable( true );
226       anActor->SetResolveCoincidentTopology( true );
227
228       // Add Actor to the Actors Map
229       myMapOfActors.insert(index, anActor);
230     }
231   }
232   mySelector->ClearIObjects();
233   if ( myRenderer )
234     AddToRender( myRenderer );
235 }
236
237 void SMESH_PreviewActorsCollection::clearActors()
238 {
239   if (myRenderer)
240     RemoveFromRender(myRenderer);
241
242   QMap<int, GEOM_Actor*>::iterator iter = myMapOfActors.begin();
243   for ( ; iter != myMapOfActors.end(); ++iter )
244     if ( GEOM_Actor* anActor = iter.value() )
245       anActor->Delete();
246   myMapOfActors.clear();
247 }