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