Salome HOME
Merge from V6_3_BR 06/06/2011
[modules/smesh.git] / src / OBJECT / SMESH_ActorUtils.cxx
1 // Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "SMESH_ActorUtils.h"
24 #include "SMESH_Actor.h"
25
26 #include "SUIT_Tools.h"
27 #include "SUIT_Session.h"
28 #include "SUIT_ResourceMgr.h"
29 #include <SALOMEconfig.h> // To fix some redefinition
30 #include "SalomeApp_Application.h"
31
32 #ifndef DISABLE_PLOT2DVIEWER
33 #include <SPlot2d_ViewModel.h>
34 #include <SPlot2d_Histogram.h>
35 #include <Plot2d_ViewManager.h>
36 #endif
37
38
39 #include "utilities.h"
40
41 #include <vtkUnstructuredGrid.h>
42 #include <vtkXMLUnstructuredGridWriter.h>
43 #include <vtkUnstructuredGridWriter.h>
44
45 //#ifdef _DEBUG_
46 //static int MYDEBUG = 1;
47 //#else
48 //static int MYDEBUG = 0;
49 //#endif
50
51 namespace SMESH
52 {
53
54   vtkFloatingPointType
55   GetFloat( const QString& theValue, 
56             vtkFloatingPointType theDefault )
57   {
58     int pos = theValue.indexOf( ":" );
59     vtkFloatingPointType val = theDefault;
60     if( pos>=0 ) 
61     {
62       QString name = theValue.right( theValue.length()-pos-1 ),
63               sect = theValue.left( pos );
64       if( !name.isEmpty() && !sect.isEmpty() )
65         val = GetFloat( name, sect, theDefault );
66     }
67     return val;
68   }
69
70   vtkFloatingPointType
71   GetFloat( const QString& theValue, 
72             const QString& theSection, 
73             vtkFloatingPointType theDefault )
74   {
75     vtkFloatingPointType val = theDefault;
76     SUIT_ResourceMgr* mgr = SUIT_Session::session()->resourceMgr();
77     if( mgr )
78       val = (vtkFloatingPointType) mgr->doubleValue( theSection, theValue, theDefault );
79
80     return val;
81   }
82
83   void
84   WriteUnstructuredGrid(vtkUnstructuredGrid* theGrid, 
85                         const char* theFileName)
86   {
87     vtkXMLUnstructuredGridWriter* aWriter = vtkXMLUnstructuredGridWriter::New();
88     aWriter->SetFileName(theFileName);
89     aWriter->SetInput(theGrid);
90     aWriter->SetDataModeToAscii();
91     if(theGrid->GetNumberOfCells()){
92       aWriter->Write();
93     }
94     aWriter->Delete();
95   }
96
97   QColor
98   GetColor( const QString& theSect, 
99             const QString& theName, 
100             const QColor& def )
101   {
102     QColor c = def;
103     SUIT_ResourceMgr* mgr = SUIT_Session::session()->resourceMgr();
104     if ( mgr )
105       c = mgr->colorValue( theSect, theName, def );
106     return c;
107   }
108
109   void
110   GetColor( const QString& theSect, 
111             const QString& theName, 
112             int& r, 
113             int& g, 
114             int& b, 
115             const QColor& def )
116   {
117     QColor c = def;
118     SUIT_ResourceMgr* mgr = SUIT_Session::session()->resourceMgr();
119     if ( mgr )
120       c = mgr->colorValue( theSect, theName, def );
121
122     SUIT_Tools::rgbSet( SUIT_Tools::rgbSet( c ), r, g, b );
123   }
124
125   void
126   GetColor( const QString& theSect, 
127             const QString& theName, 
128             vtkFloatingPointType& r, 
129             vtkFloatingPointType& g, 
130             vtkFloatingPointType& b, 
131             const QColor& def )
132   {
133     int ir( 0 ), ig( 0 ), ib( 0 );
134     GetColor( theSect, theName, ir, ig, ib, def );
135     r = ir / 255.;
136     g = ig / 255.;
137     b = ib / 255.;
138   }
139
140 #ifndef DISABLE_PLOT2DVIEWER
141   //=======================================================================
142   /**
143      Get histogram from the input actor
144      Repaint/Remove the histogram in/from each opened Plot2D Viewer 
145   */
146   //=======================================================================
147   void ProcessIn2DViewers( SMESH_Actor *theActor, Viewer2dActionType aType ) {
148     SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>(SUIT_Session::session()->activeApplication());
149     
150     if(!anApp || !theActor)
151       return;
152     
153     SPlot2d_Histogram* aHistogram = 0;
154     
155     if(theActor->GetPlot2Histogram())
156       if(aType == UpdateIn2dViewer)
157         aHistogram = theActor->UpdatePlot2Histogram();
158       else
159         aHistogram = theActor->GetPlot2Histogram();
160     else 
161       return;
162     
163     ViewManagerList aViewManagerList;
164     anApp->viewManagers(SPlot2d_Viewer::Type(), aViewManagerList);
165     
166     aType = aHistogram->getPointList().empty() ? RemoveFrom2dViewer : aType;
167     
168     SUIT_ViewManager* aViewManager;
169     foreach( aViewManager, aViewManagerList ) {
170       if (Plot2d_ViewManager* aManager = dynamic_cast<Plot2d_ViewManager*>(aViewManager)) {
171         if (SPlot2d_Viewer* aViewer = dynamic_cast<SPlot2d_Viewer*>(aManager->getViewModel())) {
172           if (Plot2d_ViewFrame* aViewFrame = aViewer->getActiveViewFrame()) {
173             if(aType == UpdateIn2dViewer )
174               aViewFrame->displayObject(aHistogram, true);
175             else if (aType == RemoveFrom2dViewer)
176               aViewFrame->eraseObject(aHistogram, true);
177           }
178         }
179       }
180     }
181   }
182 #endif //DISABLE_PLOT2DVIEWER
183   
184 }