1 // Copyright (C) 2007-2022 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 SMESH_PreviewActorsCollection::SMESH_PreviewActorsCollection() :
49 mySelector( 0 ), myRenderer( 0 ), myCurrentChunk( 0 ), myChunkSize( 0 ), myIsShown( true )
51 MESSAGE("SMESH_PreviewActorsCollection - "<<this);
55 SMESH_PreviewActorsCollection::~SMESH_PreviewActorsCollection()
57 MESSAGE("~SMESH_PreviewActorsCollection - "<<this);
61 bool SMESH_PreviewActorsCollection::Init( const TopoDS_Shape& theShape,
62 const TopoDS_Shape& theMainShape,
63 TopAbs_ShapeEnum theType,
64 const QString& theEntry )
66 SUIT_ResourceMgr* mgr = SUIT_Session::session()->resourceMgr();
70 myMainShape = theShape;
71 myMapOfActors.clear();
72 myMapOfShapes.Clear();
75 myChunkSize = qMax(1, mgr->integerValue( "SMESH", "preview_actor_chunk_size", 100 ) );
77 if ( theShape.IsNull() )
80 // Handle( SALOME_InteractiveObject ) anIO = new SALOME_InteractiveObject();
81 // anIO->setEntry( theEntry.toUtf8().constData() );
83 // get indexes of selected elements
84 TopExp::MapShapes( theMainShape, myMapOfShapes );
85 TopExp_Explorer exp( theShape, theType );
87 for ( ; exp.More(); exp.Next() )
88 indices << myMapOfShapes.FindIndex( exp.Current() );
89 myIndices = indices.toList();
98 GEOM_Actor* SMESH_PreviewActorsCollection::createActor(const TopoDS_Shape& shape)
100 GEOM_Actor* actor = GEOM_Actor::New();
101 actor->SetShape( shape, 0, 0 );
105 GEOM_Actor* SMESH_PreviewActorsCollection::GetActorByIndex(int index)
107 return myMapOfActors.value( index );
110 bool SMESH_PreviewActorsCollection::IsValidIndex( int index )
112 return 0 < index && index <= myMapOfShapes.Extent();
115 int SMESH_PreviewActorsCollection::GetIndexByShape( const TopoDS_Shape& theShape )
117 return myMapOfShapes.FindIndex( theShape );
120 TopoDS_Shape SMESH_PreviewActorsCollection::GetShapeByIndex( int index )
122 return IsValidIndex( index ) ? myMapOfShapes.FindKey( index ) : TopoDS_Shape();
125 int SMESH_PreviewActorsCollection::NbShapesOfType( TopAbs_ShapeEnum type )
127 if ( type == TopAbs_SHAPE ) return myMapOfShapes.Extent();
130 for ( int i = 1; i <= myMapOfShapes.Extent(); ++i )
131 nb += ( myMapOfShapes(i).ShapeType() == type );
136 void SMESH_PreviewActorsCollection::SetIndices( const QList<int>& indices)
138 if ( myIndices != indices )
145 void SMESH_PreviewActorsCollection::AddToRender(vtkRenderer* theRenderer)
147 myRenderer = theRenderer;
149 QMap<int, GEOM_Actor*>::iterator iter = myMapOfActors.begin();
150 for ( ; iter != myMapOfActors.end(); ++iter ) {
151 iter.value()->SetVisibility( myIsShown );
152 iter.value()->AddToRender( theRenderer );
156 void SMESH_PreviewActorsCollection::RemoveFromRender(vtkRenderer* theRenderer)
158 QMap<int, GEOM_Actor*>::iterator iter = myMapOfActors.begin();
159 for ( ; iter != myMapOfActors.end(); ++iter )
160 iter.value()->RemoveFromRender( theRenderer );
163 void SMESH_PreviewActorsCollection::SetSelector(SVTK_Selector* theSelector)
165 mySelector = theSelector;
168 void SMESH_PreviewActorsCollection::HighlightAll( bool theHighlight )
170 QMap<int, GEOM_Actor*>::iterator iter = myMapOfActors.begin();
171 for ( ; iter != myMapOfActors.end(); ++iter )
172 iter.value()->Highlight( theHighlight );
175 void SMESH_PreviewActorsCollection::HighlightID( int index )
177 GEOM_Actor* anActor = GetActorByIndex( index );
178 if ( anActor && !anActor->isHighlighted() )
179 anActor->Highlight( true );
182 void SMESH_PreviewActorsCollection::SetShown( bool shown )
185 QMap<int, GEOM_Actor*>::iterator iter = myMapOfActors.begin();
186 for ( ; iter != myMapOfActors.end(); ++iter )
187 iter.value()->SetVisibility( shown );
190 int SMESH_PreviewActorsCollection::count() const
192 return myIndices.count();
195 int SMESH_PreviewActorsCollection::chunkSize() const
200 int SMESH_PreviewActorsCollection::currentChunk() const
202 return myCurrentChunk;
205 bool SMESH_PreviewActorsCollection::hasPrevious() const
207 return chunkSize() > 0 && currentChunk() > 0;
210 bool SMESH_PreviewActorsCollection::hasNext() const
212 return chunkSize() > 0 && (currentChunk()+1)*chunkSize() < count();
215 void SMESH_PreviewActorsCollection::previous()
217 if ( !hasPrevious() ) return;
222 void SMESH_PreviewActorsCollection::next()
224 if ( !hasNext() ) return;
229 void SMESH_PreviewActorsCollection::showCurrentChunk()
232 int imin = currentChunk() * chunkSize();
233 int imax = std::min( (currentChunk()+1) * chunkSize(), count() );
234 for ( int i = imin; i < imax; i++ ) {
235 int index = myIndices[i];
236 if ( !index || myMapOfActors.contains( index ) ) continue;
237 TopoDS_Shape s = myMapOfShapes.FindKey( index );
238 if ( s.IsNull() ) continue;
239 // create actor if the index is present
240 if ( GEOM_Actor* anActor = createActor( s.Oriented(TopAbs_FORWARD))) {
241 // Create new entry for actor
242 QString entry = QString( "%1_%2" ).arg( myEntry ).arg( index );
243 // Create interactive object
244 Handle( SALOME_InteractiveObject ) anIO = new SALOME_InteractiveObject();
245 anIO->setEntry( entry.toUtf8().constData() );
247 anActor->SetVectorMode( myType==TopAbs_EDGE );
248 anActor->setIO( anIO );
249 anActor->SetSelector( mySelector );
250 anActor->SetPickable( true );
251 anActor->SetResolveCoincidentTopology( true );
253 // Add Actor to the Actors Map
254 myMapOfActors.insert(index, anActor);
258 mySelector->ClearIObjects();
260 AddToRender( myRenderer );
263 void SMESH_PreviewActorsCollection::clearActors()
266 RemoveFromRender(myRenderer);
268 QMap<int, GEOM_Actor*>::iterator iter = myMapOfActors.begin();
269 for ( ; iter != myMapOfActors.end(); ++iter )
270 if ( GEOM_Actor* anActor = iter.value() )
272 myMapOfActors.clear();