Back to index

salome-smesh  6.5.0
SMESH_PreviewActorsCollection.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 #include "SMESH_PreviewActorsCollection.h"
00021 
00022 #include <utilities.h>
00023 
00024 // OCC includes
00025 #include <TopoDS.hxx>
00026 #include <TopExp.hxx>
00027 #include <TopExp_Explorer.hxx>
00028 
00029 // VTK includes
00030 #include <vtkUnstructuredGrid.h>
00031 #include <vtkPlane.h>
00032 #include <vtkRenderer.h>
00033 #include <vtkProperty.h>
00034 
00035 #include <QSet>
00036 
00037 // GEOM includes
00038 #include <GEOM_Actor.h>
00039 
00040 // GUI includes
00041 #include <VTKViewer_Actor.h>
00042 #include <SVTK_DeviceActor.h>
00043 #include <SALOME_InteractiveObject.hxx>
00044 #include <SUIT_ResourceMgr.h>
00045 #include <SUIT_Session.h>
00046 
00047 #ifdef _DEBUG_
00048 static int MYDEBUG = 0;
00049 #else
00050 static int MYDEBUG = 0;
00051 #endif
00052 
00053 
00054 SMESH_PreviewActorsCollection::SMESH_PreviewActorsCollection() :
00055   mySelector( 0 ), myRenderer( 0 ), myCurrentChunk( 0 ), myChunkSize( 0 ), myIsShown( true )
00056 {
00057   if(MYDEBUG) MESSAGE("SMESH_PreviewActorsCollection - "<<this);
00058 }
00059 
00060 
00061 SMESH_PreviewActorsCollection::~SMESH_PreviewActorsCollection()
00062 {
00063   if(MYDEBUG) MESSAGE("~SMESH_PreviewActorsCollection - "<<this);
00064   clearActors();
00065 }
00066 
00067 bool SMESH_PreviewActorsCollection::Init( const TopoDS_Shape& theShape,
00068                                      TopAbs_ShapeEnum theType,
00069                                      const QString& theEntry )
00070 {
00071   SUIT_ResourceMgr* mgr = SUIT_Session::session()->resourceMgr();
00072 
00073   myType =  theType;
00074   myEntry = theEntry;
00075   myMainShape = theShape;
00076   myMapOfActors.clear();
00077   myMapOfShapes.Clear();
00078   myIndices.clear();
00079   myCurrentChunk = 0;
00080   myChunkSize = mgr->integerValue( "SMESH", "preview_actor_chunk_size", 100 );
00081 
00082   if ( theShape.IsNull() )
00083     return false;
00084 
00085   Handle( SALOME_InteractiveObject ) anIO = new SALOME_InteractiveObject();
00086   anIO->setEntry( theEntry.toLatin1().constData() );
00087   
00088   // get indexes of seleted elements
00089   TopExp::MapShapes( theShape, myMapOfShapes );
00090   TopExp_Explorer exp( theShape, theType );
00091   QSet<int> indices;
00092   for ( ; exp.More(); exp.Next() )
00093     indices << myMapOfShapes.FindIndex( exp.Current() );
00094   myIndices = indices.toList();
00095   qSort(myIndices);
00096 
00097   // show current chunk
00098   showCurrentChunk();
00099 
00100   return count() > 0;
00101 }
00102 
00103 GEOM_Actor* SMESH_PreviewActorsCollection::createActor(const TopoDS_Shape& shape)
00104 {
00105   GEOM_Actor* actor = GEOM_Actor::New();
00106   actor->SetShape( shape, 0, 0 );
00107   return actor;
00108 }
00109 
00110 GEOM_Actor* SMESH_PreviewActorsCollection::GetActorByIndex(int index)
00111 {
00112   return myMapOfActors.value( index );
00113 }
00114 
00115 int SMESH_PreviewActorsCollection::GetIndexByShape( const TopoDS_Shape& theShape )
00116 {
00117   return myMapOfShapes.FindIndex( theShape );
00118 }
00119 
00120 void SMESH_PreviewActorsCollection::AddToRender(vtkRenderer* theRenderer)
00121 {
00122   myRenderer = theRenderer;
00123 
00124   QMap<int, GEOM_Actor*>::iterator iter = myMapOfActors.begin();
00125   for ( ; iter != myMapOfActors.end(); ++iter ) {
00126     iter.value()->SetVisibility( myIsShown );
00127     iter.value()->AddToRender( theRenderer );
00128   }
00129 }
00130 
00131 void SMESH_PreviewActorsCollection::RemoveFromRender(vtkRenderer* theRenderer)
00132 {
00133   QMap<int, GEOM_Actor*>::iterator iter = myMapOfActors.begin();
00134   for ( ; iter != myMapOfActors.end(); ++iter )
00135     iter.value()->RemoveFromRender( theRenderer );
00136 }
00137 
00138 void SMESH_PreviewActorsCollection::SetSelector(SVTK_Selector* theSelector)
00139 {
00140   mySelector = theSelector;
00141 }
00142 
00143 void SMESH_PreviewActorsCollection::HighlightAll( bool theHighlight )
00144 {
00145   QMap<int, GEOM_Actor*>::iterator iter = myMapOfActors.begin();
00146   for ( ; iter != myMapOfActors.end(); ++iter )
00147     iter.value()->Highlight( theHighlight );
00148 }
00149 
00150 void SMESH_PreviewActorsCollection::HighlightID( int index )
00151 {
00152   GEOM_Actor* anActor = GetActorByIndex( index );
00153   if ( anActor && !anActor->isHighlighted() )
00154     anActor->Highlight( true );
00155 }
00156 
00157 void SMESH_PreviewActorsCollection::SetShown( bool shown )
00158 {
00159   myIsShown = shown;
00160   QMap<int, GEOM_Actor*>::iterator iter = myMapOfActors.begin();
00161   for ( ; iter != myMapOfActors.end(); ++iter )
00162     iter.value()->SetVisibility( shown );
00163 }
00164 
00165 int SMESH_PreviewActorsCollection::count() const
00166 {
00167   return myIndices.count();
00168 }
00169 
00170 int SMESH_PreviewActorsCollection::chunkSize() const
00171 {
00172   return myChunkSize;
00173 }
00174 
00175 int SMESH_PreviewActorsCollection::currentChunk() const
00176 {
00177   return myCurrentChunk;
00178 }
00179 
00180 bool SMESH_PreviewActorsCollection::hasPrevious() const
00181 {
00182   return chunkSize() > 0 && currentChunk() > 0;
00183 }
00184 
00185 bool SMESH_PreviewActorsCollection::hasNext() const
00186 {
00187   return chunkSize() > 0 && (currentChunk()+1)*chunkSize() < count();
00188 }
00189 
00190 void SMESH_PreviewActorsCollection::previous()
00191 {
00192   if ( !hasPrevious() ) return;
00193   myCurrentChunk--;
00194   showCurrentChunk();
00195 }
00196 
00197 void SMESH_PreviewActorsCollection::next()
00198 {
00199   if ( !hasNext() ) return;
00200   myCurrentChunk++;
00201   showCurrentChunk();
00202 }
00203 
00204 void SMESH_PreviewActorsCollection::showCurrentChunk()
00205 {
00206   clearActors();
00207   int imin = currentChunk() * chunkSize();
00208   int imax = std::min( (currentChunk()+1) * chunkSize(), count() );
00209   for ( int i = imin; i < imax; i++ ) {
00210     int index = myIndices[i];
00211     if ( !index || myMapOfActors.contains( index ) ) continue;
00212     TopoDS_Shape s = myMapOfShapes.FindKey( index );
00213     if ( s.IsNull() ) continue;
00214     // create actor if the index is present
00215     if ( GEOM_Actor* anActor = createActor( s.Oriented(TopAbs_FORWARD))) {
00216       // Create new entry for actor
00217       QString entry = QString( "%1_%2" ).arg( myEntry ).arg( index );
00218       // Create interactive object
00219       Handle( SALOME_InteractiveObject ) anIO = new SALOME_InteractiveObject();
00220       anIO->setEntry( entry.toLatin1().constData() );
00221       // Init Actor
00222       anActor->SetVectorMode( myType==TopAbs_EDGE );
00223       anActor->setIO( anIO );
00224       anActor->SetSelector( mySelector );
00225       anActor->SetPickable( true );
00226       anActor->SetResolveCoincidentTopology( true );
00227 
00228       // Add Actor to the Actors Map
00229       myMapOfActors.insert(index, anActor);
00230     }
00231   }
00232   mySelector->ClearIObjects();
00233   if ( myRenderer )
00234     AddToRender( myRenderer );
00235 }
00236 
00237 void SMESH_PreviewActorsCollection::clearActors()
00238 {
00239   if (myRenderer)
00240     RemoveFromRender(myRenderer);
00241 
00242   QMap<int, GEOM_Actor*>::iterator iter = myMapOfActors.begin();
00243   for ( ; iter != myMapOfActors.end(); ++iter )
00244     if ( GEOM_Actor* anActor = iter.value() )
00245       anActor->Delete();
00246   myMapOfActors.clear();
00247 }