Salome HOME
Update of CheckDone
[modules/smesh.git] / src / OBJECT / SMESH_ActorUtils.cxx
1 // Copyright (C) 2007-2024  CEA, EDF, 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, or (at your option) any later version.
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 #include <Qtx.h>
39
40
41 #include "utilities.h"
42
43 #include <vtkUnstructuredGrid.h>
44 #include <vtkCellType.h>
45 #include <vtkXMLUnstructuredGridWriter.h>
46 #include <vtkUnstructuredGridWriter.h>
47 #include <vtkUnsignedCharArray.h>
48
49
50 namespace SMESH
51 {
52
53   double
54   GetFloat( const QString& theValue, 
55             double theDefault )
56   {
57     int pos = theValue.indexOf( ":" );
58     double val = theDefault;
59     if( pos>=0 ) 
60     {
61       QString name = theValue.right( theValue.length()-pos-1 ),
62               sect = theValue.left( pos );
63       if( !name.isEmpty() && !sect.isEmpty() )
64         val = GetFloat( name, sect, theDefault );
65     }
66     return val;
67   }
68
69   double
70   GetFloat( const QString& theValue, 
71             const QString& theSection, 
72             double theDefault )
73   {
74     double val = theDefault;
75     SUIT_ResourceMgr* mgr = SUIT_Session::session()->resourceMgr();
76     if( mgr )
77       val = (double) mgr->doubleValue( theSection, theValue, theDefault );
78
79     return val;
80   }
81
82   void
83   WriteUnstructuredGrid(vtkUnstructuredGrid* theGrid, 
84                         const char* theFileName)
85   {
86     vtkXMLUnstructuredGridWriter* aWriter = vtkXMLUnstructuredGridWriter::New();
87     aWriter->SetFileName(theFileName);
88     aWriter->SetInputData(theGrid);
89     aWriter->SetDataModeToAscii();
90     if(theGrid->GetNumberOfCells()){
91       aWriter->Write();
92     }
93     aWriter->Delete();
94   }
95
96   QColor
97   GetColor( const QString& theSect, 
98             const QString& theName, 
99             const QColor& def )
100   {
101     QColor c = def;
102     SUIT_ResourceMgr* mgr = SUIT_Session::session()->resourceMgr();
103     if ( mgr )
104       c = mgr->colorValue( theSect, theName, def );
105     return c;
106   }
107
108   void
109   GetColor( const QString& theSect, 
110             const QString& theName, 
111             int& r, 
112             int& g, 
113             int& b, 
114             const QColor& def )
115   {
116     QColor c = def;
117     SUIT_ResourceMgr* mgr = SUIT_Session::session()->resourceMgr();
118     if ( mgr )
119       c = mgr->colorValue( theSect, theName, def );
120
121     SUIT_Tools::rgbSet( SUIT_Tools::rgbSet( c ), r, g, b );
122   }
123
124   void
125   GetColor( const QString& theSect, 
126             const QString& theName, 
127             double& r, 
128             double& g, 
129             double& b, 
130             const QColor& def )
131   {
132     int ir( 0 ), ig( 0 ), ib( 0 );
133     GetColor( theSect, theName, ir, ig, ib, def );
134     r = ir / 255.;
135     g = ig / 255.;
136     b = ib / 255.;
137   }
138
139
140   void
141   GetColor(  const QString& theSect, 
142              const QString& theName, 
143              QColor& color,
144              int& delta,
145              QString def) 
146   {
147     
148     SUIT_ResourceMgr* mgr = SUIT_Session::session()->resourceMgr();
149     if ( mgr ) {
150       QString str = mgr->stringValue( theSect, theName, def );
151       Qtx::stringToBiColor(str,color,delta);
152     }
153   }
154
155   std::map<SMDSAbs_ElementType,::smIdType> GetEntitiesFromObject(SMESH_VisualObj *theObject) {
156     std::map<SMDSAbs_ElementType,::smIdType> entities;
157     entities.insert(std::pair<SMDSAbs_ElementType,::smIdType>(SMDSAbs_0DElement,
158                 theObject ? theObject->GetNbEntities(SMDSAbs_0DElement) : 0));
159     entities.insert(std::pair<SMDSAbs_ElementType,::smIdType>(SMDSAbs_Ball,
160                 theObject ? theObject->GetNbEntities(SMDSAbs_Ball) : 0));
161     entities.insert(std::pair<SMDSAbs_ElementType,::smIdType>(SMDSAbs_Edge,
162                 theObject ? theObject->GetNbEntities(SMDSAbs_Edge) : 0));
163     entities.insert(std::pair<SMDSAbs_ElementType,::smIdType>(SMDSAbs_Face,
164                 theObject ? theObject->GetNbEntities(SMDSAbs_Face) : 0));
165     entities.insert(std::pair<SMDSAbs_ElementType,::smIdType>(SMDSAbs_Volume,
166                 theObject ? theObject->GetNbEntities(SMDSAbs_Volume) : 0));
167     return entities;
168   }
169   
170
171
172 #ifndef DISABLE_PLOT2DVIEWER
173   //=======================================================================
174   /**
175      Get histogram from the input actor
176      Repaint/Remove the histogram in/from each opened Plot2D Viewer 
177   */
178   //=======================================================================
179   void ProcessIn2DViewers( SMESH_Actor *theActor, Viewer2dActionType aType ) {
180     SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>(SUIT_Session::session()->activeApplication());
181     
182     if(!anApp || !theActor)
183       return;
184     
185     SPlot2d_Histogram* aHistogram = 0;
186     
187     if(theActor->GetPlot2Histogram())
188       if(aType == UpdateIn2dViewer)
189         aHistogram = theActor->UpdatePlot2Histogram();
190       else
191         aHistogram = theActor->GetPlot2Histogram();
192     else 
193       return;
194     
195     ViewManagerList aViewManagerList;
196     anApp->viewManagers(SPlot2d_Viewer::Type(), aViewManagerList);
197     
198     aType = aHistogram->getPointList().empty() ? RemoveFrom2dViewer : aType;
199     
200     SUIT_ViewManager* aViewManager;
201     foreach( aViewManager, aViewManagerList ) {
202       if (Plot2d_ViewManager* aManager = dynamic_cast<Plot2d_ViewManager*>(aViewManager)) {
203         if (SPlot2d_Viewer* aViewer = dynamic_cast<SPlot2d_Viewer*>(aManager->getViewModel())) {
204           if (Plot2d_ViewFrame* aViewFrame = aViewer->getActiveViewFrame()) {
205             if(aType == UpdateIn2dViewer )
206               aViewFrame->displayObject(aHistogram, true);
207             else if (aType == RemoveFrom2dViewer)
208               aViewFrame->eraseObject(aHistogram, true);
209           }
210         }
211       }
212     }
213   }
214 #endif //DISABLE_PLOT2DVIEWER
215   
216 }