Salome HOME
eliminate compilation warning
[modules/smesh.git] / src / StdMeshers / StdMeshers_MaxElementVolume.cxx
1 //  Copyright (C) 2007-2008  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 //  SMESH SMESH : implementaion of SMESH idl descriptions
23 //  File   : StdMeshers_MaxElementVolume.cxx
24 //           Moved here from SMESH_MaxElementVolume.cxx
25 //  Author : Paul RASCLE, EDF
26 //  Module : SMESH
27 //
28 #include "StdMeshers_MaxElementVolume.hxx"
29
30 #include "SMDS_MeshElement.hxx"
31 #include "SMESHDS_SubMesh.hxx"
32 #include "SMESH_ControlsDef.hxx"
33 #include "SMESH_Mesh.hxx"
34
35 #include "utilities.h"
36
37 #include <TopExp.hxx>
38 #include <TopExp_Explorer.hxx>
39 #include <TopTools_IndexedMapOfShape.hxx>
40
41 using namespace std;
42
43 //=============================================================================
44 /*!
45  *  
46  */
47 //=============================================================================
48
49 StdMeshers_MaxElementVolume::StdMeshers_MaxElementVolume(int hypId, int studyId, SMESH_Gen* gen)
50   : SMESH_Hypothesis(hypId, studyId, gen)
51 {
52   _maxVolume = 1.;
53   _name = "MaxElementVolume";
54   _param_algo_dim = 3;
55 }
56
57 //=============================================================================
58 /*!
59  *  
60  */
61 //=============================================================================
62
63 StdMeshers_MaxElementVolume::~StdMeshers_MaxElementVolume()
64 {
65   MESSAGE("StdMeshers_MaxElementVolume::~StdMeshers_MaxElementVolume");
66 }
67
68 //=============================================================================
69 /*!
70  *  
71  */
72 //=============================================================================
73
74 void StdMeshers_MaxElementVolume::SetMaxVolume(double maxVolume)
75   throw (SALOME_Exception)
76 {
77   double oldVolume = _maxVolume;
78   if (maxVolume <= 0) 
79     throw SALOME_Exception(LOCALIZED("maxVolume must be positive"));
80   _maxVolume = maxVolume;
81   if (_maxVolume != oldVolume)
82     NotifySubMeshesHypothesisModification();
83 }
84
85 //=============================================================================
86 /*!
87  *  
88  */
89 //=============================================================================
90
91 double StdMeshers_MaxElementVolume::GetMaxVolume() const
92 {
93   return _maxVolume;
94 }
95
96 //=============================================================================
97 /*!
98  *  
99  */
100 //=============================================================================
101
102 ostream & StdMeshers_MaxElementVolume::SaveTo(ostream & save)
103 {
104   save << this->_maxVolume;
105   return save;
106 }
107
108 //=============================================================================
109 /*!
110  *  
111  */
112 //=============================================================================
113
114 istream & StdMeshers_MaxElementVolume::LoadFrom(istream & load)
115 {
116   bool isOK = true;
117   double a;
118   isOK = (load >> a);
119   if (isOK)
120     this->_maxVolume = a;
121   else 
122     load.clear(ios::badbit | load.rdstate());
123   return load;
124 }
125
126 //=============================================================================
127 /*!
128  *  
129  */
130 //=============================================================================
131
132 ostream & operator << (ostream & save, StdMeshers_MaxElementVolume & hyp)
133 {
134   return hyp.SaveTo( save );
135 }
136
137 //=============================================================================
138 /*!
139  *  
140  */
141 //=============================================================================
142
143 istream & operator >> (istream & load, StdMeshers_MaxElementVolume & hyp)
144 {
145   return hyp.LoadFrom( load );
146 }
147
148
149 //================================================================================
150 /*!
151  * \brief Initialize maximal area by the mesh built on the geometry
152  * \param theMesh - the built mesh
153  * \param theShape - the geometry of interest
154  * \retval bool - true if parameter values have been successfully defined
155  */
156 //================================================================================
157
158 bool StdMeshers_MaxElementVolume::SetParametersByMesh(const SMESH_Mesh*   theMesh,
159                                                       const TopoDS_Shape& theShape)
160 {
161   if ( !theMesh || theShape.IsNull() )
162     return false;
163
164   _maxVolume = 0;
165
166   SMESH::Controls::Volume volumeControl;
167
168   TopTools_IndexedMapOfShape volMap;
169   TopExp::MapShapes( theShape, TopAbs_SOLID, volMap );
170   if ( volMap.IsEmpty() )
171     TopExp::MapShapes( theShape, TopAbs_SHELL, volMap );
172   if ( volMap.IsEmpty() )
173     return false;
174
175   SMESHDS_Mesh* aMeshDS = const_cast< SMESH_Mesh* >( theMesh )->GetMeshDS();
176
177   for ( int iV = 1; iV <= volMap.Extent(); ++iV )
178   {
179     const TopoDS_Shape& S = volMap( iV );
180     SMESHDS_SubMesh * subMesh = aMeshDS->MeshElements( S );
181     if ( !subMesh && S.ShapeType() == TopAbs_SOLID ) {
182       TopExp_Explorer shellExp( S, TopAbs_SHELL );
183       if ( shellExp.More() )
184         subMesh = aMeshDS->MeshElements( shellExp.Current() );
185     }
186     if ( !subMesh) 
187       return false;
188     SMDS_ElemIteratorPtr vIt = subMesh->GetElements();
189     while ( vIt->more() )
190     {
191       const SMDS_MeshElement* elem = vIt->next();
192       if ( elem->GetType() == SMDSAbs_Volume ) {
193         _maxVolume = max( _maxVolume, volumeControl.GetValue( elem->GetID() ));
194       }
195     }
196   }
197   return _maxVolume > 0;
198 }
199 //================================================================================
200 /*!
201  * \brief Initialize my parameter values by default parameters.
202  *  \retval bool - true if parameter values have been successfully defined
203  */
204 //================================================================================
205
206 bool StdMeshers_MaxElementVolume::SetParametersByDefaults(const TDefaults&  dflts,
207                                                           const SMESH_Mesh* /*theMesh*/)
208 {
209   return ( _maxVolume = dflts._elemLength*dflts._elemLength*dflts._elemLength );
210 }
211