1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
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, or (at your option) any later version.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "SMESH_PreviewActorsCollection.h"
22 #include <utilities.h>
27 #include <TopExp_Explorer.hxx>
30 #include <vtkUnstructuredGrid.h>
32 #include <vtkRenderer.h>
33 #include <vtkProperty.h>
38 #include <GEOM_Actor.h>
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>
48 static int MYDEBUG = 0;
50 static int MYDEBUG = 0;
54 SMESH_PreviewActorsCollection::SMESH_PreviewActorsCollection() :
55 mySelector( 0 ), myRenderer( 0 ), myCurrentChunk( 0 ), myChunkSize( 0 ), myIsShown( true )
57 if(MYDEBUG) MESSAGE("SMESH_PreviewActorsCollection - "<<this);
61 SMESH_PreviewActorsCollection::~SMESH_PreviewActorsCollection()
63 if(MYDEBUG) MESSAGE("~SMESH_PreviewActorsCollection - "<<this);
67 bool SMESH_PreviewActorsCollection::Init( const TopoDS_Shape& theShape,
68 const TopoDS_Shape& theMainShape,
69 TopAbs_ShapeEnum theType,
70 const QString& theEntry )
72 SUIT_ResourceMgr* mgr = SUIT_Session::session()->resourceMgr();
76 myMainShape = theShape;
77 myMapOfActors.clear();
78 myMapOfShapes.Clear();
81 myChunkSize = qMax(1, mgr->integerValue( "SMESH", "preview_actor_chunk_size", 100 ) );
83 if ( theShape.IsNull() )
86 // Handle( SALOME_InteractiveObject ) anIO = new SALOME_InteractiveObject();
87 // anIO->setEntry( theEntry.toLatin1().constData() );
89 // get indexes of seleted elements
90 TopExp::MapShapes( theMainShape, myMapOfShapes );
91 TopExp_Explorer exp( theShape, theType );
93 for ( ; exp.More(); exp.Next() )
94 indices << myMapOfShapes.FindIndex( exp.Current() );
95 myIndices = indices.toList();
104 GEOM_Actor* SMESH_PreviewActorsCollection::createActor(const TopoDS_Shape& shape)
106 GEOM_Actor* actor = GEOM_Actor::New();
107 actor->SetShape( shape, 0, 0 );
111 GEOM_Actor* SMESH_PreviewActorsCollection::GetActorByIndex(int index)
113 return myMapOfActors.value( index );
116 bool SMESH_PreviewActorsCollection::IsValidIndex( int index )
118 return 0 < index && index <= myMapOfShapes.Extent();
121 int SMESH_PreviewActorsCollection::GetIndexByShape( const TopoDS_Shape& theShape )
123 return myMapOfShapes.FindIndex( theShape );
126 TopoDS_Shape SMESH_PreviewActorsCollection::GetShapeByIndex( int index )
128 return IsValidIndex( index ) ? myMapOfShapes.FindKey( index ) : TopoDS_Shape();
131 void SMESH_PreviewActorsCollection::SetIndices( const QList<int>& indices)
133 if ( myIndices != indices )
140 void SMESH_PreviewActorsCollection::AddToRender(vtkRenderer* theRenderer)
142 myRenderer = theRenderer;
144 QMap<int, GEOM_Actor*>::iterator iter = myMapOfActors.begin();
145 for ( ; iter != myMapOfActors.end(); ++iter ) {
146 iter.value()->SetVisibility( myIsShown );
147 iter.value()->AddToRender( theRenderer );
151 void SMESH_PreviewActorsCollection::RemoveFromRender(vtkRenderer* theRenderer)
153 QMap<int, GEOM_Actor*>::iterator iter = myMapOfActors.begin();
154 for ( ; iter != myMapOfActors.end(); ++iter )
155 iter.value()->RemoveFromRender( theRenderer );
158 void SMESH_PreviewActorsCollection::SetSelector(SVTK_Selector* theSelector)
160 mySelector = theSelector;
163 void SMESH_PreviewActorsCollection::HighlightAll( bool theHighlight )
165 QMap<int, GEOM_Actor*>::iterator iter = myMapOfActors.begin();
166 for ( ; iter != myMapOfActors.end(); ++iter )
167 iter.value()->Highlight( theHighlight );
170 void SMESH_PreviewActorsCollection::HighlightID( int index )
172 GEOM_Actor* anActor = GetActorByIndex( index );
173 if ( anActor && !anActor->isHighlighted() )
174 anActor->Highlight( true );
177 void SMESH_PreviewActorsCollection::SetShown( bool shown )
180 QMap<int, GEOM_Actor*>::iterator iter = myMapOfActors.begin();
181 for ( ; iter != myMapOfActors.end(); ++iter )
182 iter.value()->SetVisibility( shown );
185 int SMESH_PreviewActorsCollection::count() const
187 return myIndices.count();
190 int SMESH_PreviewActorsCollection::chunkSize() const
195 int SMESH_PreviewActorsCollection::currentChunk() const
197 return myCurrentChunk;
200 bool SMESH_PreviewActorsCollection::hasPrevious() const
202 return chunkSize() > 0 && currentChunk() > 0;
205 bool SMESH_PreviewActorsCollection::hasNext() const
207 return chunkSize() > 0 && (currentChunk()+1)*chunkSize() < count();
210 void SMESH_PreviewActorsCollection::previous()
212 if ( !hasPrevious() ) return;
217 void SMESH_PreviewActorsCollection::next()
219 if ( !hasNext() ) return;
224 void SMESH_PreviewActorsCollection::showCurrentChunk()
227 int imin = currentChunk() * chunkSize();
228 int imax = std::min( (currentChunk()+1) * chunkSize(), count() );
229 for ( int i = imin; i < imax; i++ ) {
230 int index = myIndices[i];
231 if ( !index || myMapOfActors.contains( index ) ) continue;
232 TopoDS_Shape s = myMapOfShapes.FindKey( index );
233 if ( s.IsNull() ) continue;
234 // create actor if the index is present
235 if ( GEOM_Actor* anActor = createActor( s.Oriented(TopAbs_FORWARD))) {
236 // Create new entry for actor
237 QString entry = QString( "%1_%2" ).arg( myEntry ).arg( index );
238 // Create interactive object
239 Handle( SALOME_InteractiveObject ) anIO = new SALOME_InteractiveObject();
240 anIO->setEntry( entry.toLatin1().constData() );
242 anActor->SetVectorMode( myType==TopAbs_EDGE );
243 anActor->setIO( anIO );
244 anActor->SetSelector( mySelector );
245 anActor->SetPickable( true );
246 anActor->SetResolveCoincidentTopology( true );
248 // Add Actor to the Actors Map
249 myMapOfActors.insert(index, anActor);
253 mySelector->ClearIObjects();
255 AddToRender( myRenderer );
258 void SMESH_PreviewActorsCollection::clearActors()
261 RemoveFromRender(myRenderer);
263 QMap<int, GEOM_Actor*>::iterator iter = myMapOfActors.begin();
264 for ( ; iter != myMapOfActors.end(); ++iter )
265 if ( GEOM_Actor* anActor = iter.value() )
267 myMapOfActors.clear();