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