1 // Copyright (C) 2007-2019 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.toUtf8().constData() );
89 // get indexes of selected 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 int SMESH_PreviewActorsCollection::NbShapesOfType( TopAbs_ShapeEnum type )
133 if ( type == TopAbs_SHAPE ) return myMapOfShapes.Extent();
136 for ( int i = 1; i <= myMapOfShapes.Extent(); ++i )
137 nb += ( myMapOfShapes(i).ShapeType() == type );
142 void SMESH_PreviewActorsCollection::SetIndices( const QList<int>& indices)
144 if ( myIndices != indices )
151 void SMESH_PreviewActorsCollection::AddToRender(vtkRenderer* theRenderer)
153 myRenderer = theRenderer;
155 QMap<int, GEOM_Actor*>::iterator iter = myMapOfActors.begin();
156 for ( ; iter != myMapOfActors.end(); ++iter ) {
157 iter.value()->SetVisibility( myIsShown );
158 iter.value()->AddToRender( theRenderer );
162 void SMESH_PreviewActorsCollection::RemoveFromRender(vtkRenderer* theRenderer)
164 QMap<int, GEOM_Actor*>::iterator iter = myMapOfActors.begin();
165 for ( ; iter != myMapOfActors.end(); ++iter )
166 iter.value()->RemoveFromRender( theRenderer );
169 void SMESH_PreviewActorsCollection::SetSelector(SVTK_Selector* theSelector)
171 mySelector = theSelector;
174 void SMESH_PreviewActorsCollection::HighlightAll( bool theHighlight )
176 QMap<int, GEOM_Actor*>::iterator iter = myMapOfActors.begin();
177 for ( ; iter != myMapOfActors.end(); ++iter )
178 iter.value()->Highlight( theHighlight );
181 void SMESH_PreviewActorsCollection::HighlightID( int index )
183 GEOM_Actor* anActor = GetActorByIndex( index );
184 if ( anActor && !anActor->isHighlighted() )
185 anActor->Highlight( true );
188 void SMESH_PreviewActorsCollection::SetShown( bool shown )
191 QMap<int, GEOM_Actor*>::iterator iter = myMapOfActors.begin();
192 for ( ; iter != myMapOfActors.end(); ++iter )
193 iter.value()->SetVisibility( shown );
196 int SMESH_PreviewActorsCollection::count() const
198 return myIndices.count();
201 int SMESH_PreviewActorsCollection::chunkSize() const
206 int SMESH_PreviewActorsCollection::currentChunk() const
208 return myCurrentChunk;
211 bool SMESH_PreviewActorsCollection::hasPrevious() const
213 return chunkSize() > 0 && currentChunk() > 0;
216 bool SMESH_PreviewActorsCollection::hasNext() const
218 return chunkSize() > 0 && (currentChunk()+1)*chunkSize() < count();
221 void SMESH_PreviewActorsCollection::previous()
223 if ( !hasPrevious() ) return;
228 void SMESH_PreviewActorsCollection::next()
230 if ( !hasNext() ) return;
235 void SMESH_PreviewActorsCollection::showCurrentChunk()
238 int imin = currentChunk() * chunkSize();
239 int imax = std::min( (currentChunk()+1) * chunkSize(), count() );
240 for ( int i = imin; i < imax; i++ ) {
241 int index = myIndices[i];
242 if ( !index || myMapOfActors.contains( index ) ) continue;
243 TopoDS_Shape s = myMapOfShapes.FindKey( index );
244 if ( s.IsNull() ) continue;
245 // create actor if the index is present
246 if ( GEOM_Actor* anActor = createActor( s.Oriented(TopAbs_FORWARD))) {
247 // Create new entry for actor
248 QString entry = QString( "%1_%2" ).arg( myEntry ).arg( index );
249 // Create interactive object
250 Handle( SALOME_InteractiveObject ) anIO = new SALOME_InteractiveObject();
251 anIO->setEntry( entry.toUtf8().constData() );
253 anActor->SetVectorMode( myType==TopAbs_EDGE );
254 anActor->setIO( anIO );
255 anActor->SetSelector( mySelector );
256 anActor->SetPickable( true );
257 anActor->SetResolveCoincidentTopology( true );
259 // Add Actor to the Actors Map
260 myMapOfActors.insert(index, anActor);
264 mySelector->ClearIObjects();
266 AddToRender( myRenderer );
269 void SMESH_PreviewActorsCollection::clearActors()
272 RemoveFromRender(myRenderer);
274 QMap<int, GEOM_Actor*>::iterator iter = myMapOfActors.begin();
275 for ( ; iter != myMapOfActors.end(); ++iter )
276 if ( GEOM_Actor* anActor = iter.value() )
278 myMapOfActors.clear();