Back to index

salome-smesh  6.5.0
SMESH_ActorUtils.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 #include "SMESH_ActorUtils.h"
00024 #include "SMESH_Actor.h"
00025 
00026 #include "SUIT_Tools.h"
00027 #include "SUIT_Session.h"
00028 #include "SUIT_ResourceMgr.h"
00029 #include <SALOMEconfig.h> // To fix some redefinition
00030 #include "SalomeApp_Application.h"
00031 
00032 #ifndef DISABLE_PLOT2DVIEWER
00033 #include <SPlot2d_ViewModel.h>
00034 #include <SPlot2d_Histogram.h>
00035 #include <Plot2d_ViewManager.h>
00036 #endif
00037 
00038 #include <Qtx.h>
00039 
00040 
00041 #include "utilities.h"
00042 
00043 #include <vtkUnstructuredGrid.h>
00044 #include <vtkCellType.h>
00045 #include <vtkXMLUnstructuredGridWriter.h>
00046 #include <vtkUnstructuredGridWriter.h>
00047 #include <vtkUnsignedCharArray.h>
00048 
00049 //#ifdef _DEBUG_
00050 //static int MYDEBUG = 1;
00051 //#else
00052 //static int MYDEBUG = 0;
00053 //#endif
00054 
00055 namespace SMESH
00056 {
00057 
00058   vtkFloatingPointType
00059   GetFloat( const QString& theValue, 
00060             vtkFloatingPointType theDefault )
00061   {
00062     int pos = theValue.indexOf( ":" );
00063     vtkFloatingPointType val = theDefault;
00064     if( pos>=0 ) 
00065     {
00066       QString name = theValue.right( theValue.length()-pos-1 ),
00067               sect = theValue.left( pos );
00068       if( !name.isEmpty() && !sect.isEmpty() )
00069         val = GetFloat( name, sect, theDefault );
00070     }
00071     return val;
00072   }
00073 
00074   vtkFloatingPointType
00075   GetFloat( const QString& theValue, 
00076             const QString& theSection, 
00077             vtkFloatingPointType theDefault )
00078   {
00079     vtkFloatingPointType val = theDefault;
00080     SUIT_ResourceMgr* mgr = SUIT_Session::session()->resourceMgr();
00081     if( mgr )
00082       val = (vtkFloatingPointType) mgr->doubleValue( theSection, theValue, theDefault );
00083 
00084     return val;
00085   }
00086 
00087   void
00088   WriteUnstructuredGrid(vtkUnstructuredGrid* theGrid, 
00089                         const char* theFileName)
00090   {
00091     vtkXMLUnstructuredGridWriter* aWriter = vtkXMLUnstructuredGridWriter::New();
00092     aWriter->SetFileName(theFileName);
00093     aWriter->SetInput(theGrid);
00094     aWriter->SetDataModeToAscii();
00095     if(theGrid->GetNumberOfCells()){
00096       aWriter->Write();
00097     }
00098     aWriter->Delete();
00099   }
00100 
00101   QColor
00102   GetColor( const QString& theSect, 
00103             const QString& theName, 
00104             const QColor& def )
00105   {
00106     QColor c = def;
00107     SUIT_ResourceMgr* mgr = SUIT_Session::session()->resourceMgr();
00108     if ( mgr )
00109       c = mgr->colorValue( theSect, theName, def );
00110     return c;
00111   }
00112 
00113   void
00114   GetColor( const QString& theSect, 
00115             const QString& theName, 
00116             int& r, 
00117             int& g, 
00118             int& b, 
00119             const QColor& def )
00120   {
00121     QColor c = def;
00122     SUIT_ResourceMgr* mgr = SUIT_Session::session()->resourceMgr();
00123     if ( mgr )
00124       c = mgr->colorValue( theSect, theName, def );
00125 
00126     SUIT_Tools::rgbSet( SUIT_Tools::rgbSet( c ), r, g, b );
00127   }
00128 
00129   void
00130   GetColor( const QString& theSect, 
00131             const QString& theName, 
00132             vtkFloatingPointType& r, 
00133             vtkFloatingPointType& g, 
00134             vtkFloatingPointType& b, 
00135             const QColor& def )
00136   {
00137     int ir( 0 ), ig( 0 ), ib( 0 );
00138     GetColor( theSect, theName, ir, ig, ib, def );
00139     r = ir / 255.;
00140     g = ig / 255.;
00141     b = ib / 255.;
00142   }
00143 
00144 
00145   void
00146   GetColor(  const QString& theSect, 
00147             const QString& theName, 
00148             QColor& color,
00149             int& delta,
00150             QString def) 
00151   {
00152     
00153     SUIT_ResourceMgr* mgr = SUIT_Session::session()->resourceMgr();
00154     if ( mgr ) {
00155       QString str = mgr->stringValue( theSect, theName, def );
00156       Qtx::stringToBiColor(str,color,delta);
00157     }
00158   }
00159 
00160   std::map<SMDSAbs_ElementType,int> GetEntitiesFromObject(SMESH_VisualObj *theObject) {
00161     std::map<SMDSAbs_ElementType,int> entities;
00162        entities.insert(std::pair<SMDSAbs_ElementType,int>(SMDSAbs_0DElement,
00163               theObject ? theObject->GetNbEntities(SMDSAbs_0DElement) : 0));
00164     entities.insert(std::pair<SMDSAbs_ElementType,int>(SMDSAbs_Edge,
00165               theObject ? theObject->GetNbEntities(SMDSAbs_Edge) : 0));
00166     entities.insert(std::pair<SMDSAbs_ElementType,int>(SMDSAbs_Face,
00167               theObject ? theObject->GetNbEntities(SMDSAbs_Face) : 0));
00168     entities.insert(std::pair<SMDSAbs_ElementType,int>(SMDSAbs_Volume,
00169               theObject ? theObject->GetNbEntities(SMDSAbs_Volume) : 0));
00170     return entities;
00171   }
00172   
00173 
00174 
00175 #ifndef DISABLE_PLOT2DVIEWER
00176   //=======================================================================
00181   //=======================================================================
00182   void ProcessIn2DViewers( SMESH_Actor *theActor, Viewer2dActionType aType ) {
00183     SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>(SUIT_Session::session()->activeApplication());
00184     
00185     if(!anApp || !theActor)
00186       return;
00187     
00188     SPlot2d_Histogram* aHistogram = 0;
00189     
00190     if(theActor->GetPlot2Histogram())
00191       if(aType == UpdateIn2dViewer)
00192         aHistogram = theActor->UpdatePlot2Histogram();
00193       else
00194         aHistogram = theActor->GetPlot2Histogram();
00195     else 
00196       return;
00197     
00198     ViewManagerList aViewManagerList;
00199     anApp->viewManagers(SPlot2d_Viewer::Type(), aViewManagerList);
00200     
00201     aType = aHistogram->getPointList().empty() ? RemoveFrom2dViewer : aType;
00202     
00203     SUIT_ViewManager* aViewManager;
00204     foreach( aViewManager, aViewManagerList ) {
00205       if (Plot2d_ViewManager* aManager = dynamic_cast<Plot2d_ViewManager*>(aViewManager)) {
00206         if (SPlot2d_Viewer* aViewer = dynamic_cast<SPlot2d_Viewer*>(aManager->getViewModel())) {
00207           if (Plot2d_ViewFrame* aViewFrame = aViewer->getActiveViewFrame()) {
00208             if(aType == UpdateIn2dViewer )
00209               aViewFrame->displayObject(aHistogram, true);
00210             else if (aType == RemoveFrom2dViewer)
00211               aViewFrame->eraseObject(aHistogram, true);
00212           }
00213         }
00214       }
00215     }
00216   }
00217 #endif //DISABLE_PLOT2DVIEWER
00218   
00219 }