1 // Copyright (C) 2007-2011 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.
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 TopAbs_ShapeEnum theType,
69 const QString& theEntry )
71 SUIT_ResourceMgr* mgr = SUIT_Session::session()->resourceMgr();
75 myMainShape = theShape;
76 myMapOfActors.clear();
77 myMapOfShapes.Clear();
80 myChunkSize = mgr->integerValue( "SMESH", "preview_actor_chunk_size", 100 );
82 if ( theShape.IsNull() )
85 Handle( SALOME_InteractiveObject ) anIO = new SALOME_InteractiveObject();
86 anIO->setEntry( theEntry.toLatin1().constData() );
88 // get indexes of seleted elements
89 TopExp::MapShapes( theShape, myMapOfShapes );
90 TopExp_Explorer exp( theShape, theType );
92 for ( ; exp.More(); exp.Next() )
93 indices << myMapOfShapes.FindIndex( exp.Current() );
94 myIndices = indices.toList();
103 GEOM_Actor* SMESH_PreviewActorsCollection::createActor(const TopoDS_Shape& shape)
105 GEOM_Actor* actor = GEOM_Actor::New();
106 actor->SetShape( shape, 0, 0 );
110 GEOM_Actor* SMESH_PreviewActorsCollection::GetActorByIndex(int index)
112 return myMapOfActors.value( index );
115 int SMESH_PreviewActorsCollection::GetIndexByShape( const TopoDS_Shape& theShape )
117 return myMapOfShapes.FindIndex( theShape );
120 void SMESH_PreviewActorsCollection::AddToRender(vtkRenderer* theRenderer)
122 myRenderer = theRenderer;
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 );
131 void SMESH_PreviewActorsCollection::RemoveFromRender(vtkRenderer* theRenderer)
133 QMap<int, GEOM_Actor*>::iterator iter = myMapOfActors.begin();
134 for ( ; iter != myMapOfActors.end(); ++iter )
135 iter.value()->RemoveFromRender( theRenderer );
138 void SMESH_PreviewActorsCollection::SetSelector(SVTK_Selector* theSelector)
140 mySelector = theSelector;
143 void SMESH_PreviewActorsCollection::HighlightAll( bool theHighlight )
145 QMap<int, GEOM_Actor*>::iterator iter = myMapOfActors.begin();
146 for ( ; iter != myMapOfActors.end(); ++iter )
147 iter.value()->Highlight( theHighlight );
150 void SMESH_PreviewActorsCollection::HighlightID( int index )
152 GEOM_Actor* anActor = GetActorByIndex( index );
153 if ( anActor && !anActor->isHighlighted() )
154 anActor->Highlight( true );
157 void SMESH_PreviewActorsCollection::SetShown( bool shown )
160 QMap<int, GEOM_Actor*>::iterator iter = myMapOfActors.begin();
161 for ( ; iter != myMapOfActors.end(); ++iter )
162 iter.value()->SetVisibility( shown );
165 int SMESH_PreviewActorsCollection::count() const
167 return myIndices.count();
170 int SMESH_PreviewActorsCollection::chunkSize() const
175 int SMESH_PreviewActorsCollection::currentChunk() const
177 return myCurrentChunk;
180 bool SMESH_PreviewActorsCollection::hasPrevious() const
182 return chunkSize() > 0 && currentChunk() > 0;
185 bool SMESH_PreviewActorsCollection::hasNext() const
187 return chunkSize() > 0 && (currentChunk()+1)*chunkSize() < count();
190 void SMESH_PreviewActorsCollection::previous()
192 if ( !hasPrevious() ) return;
197 void SMESH_PreviewActorsCollection::next()
199 if ( !hasNext() ) return;
204 void SMESH_PreviewActorsCollection::showCurrentChunk()
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() );
222 anActor->SetVectorMode( myType==TopAbs_EDGE );
223 anActor->setIO( anIO );
224 anActor->SetSelector( mySelector );
225 anActor->SetPickable( true );
226 anActor->SetResolveCoincidentTopology( true );
228 // Add Actor to the Actors Map
229 myMapOfActors.insert(index, anActor);
232 mySelector->ClearIObjects();
234 AddToRender( myRenderer );
237 void SMESH_PreviewActorsCollection::clearActors()
240 RemoveFromRender(myRenderer);
242 QMap<int, GEOM_Actor*>::iterator iter = myMapOfActors.begin();
243 for ( ; iter != myMapOfActors.end(); ++iter )
244 if ( GEOM_Actor* anActor = iter.value() )
246 myMapOfActors.clear();