Salome HOME
Merge branch 'V8_4_BR'
[modules/smesh.git] / src / StdMeshers / StdMeshers_MaxElementArea.cxx
1 // Copyright (C) 2007-2016  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 //  SMESH SMESH : implementation of SMESH idl descriptions
24 //  File   : StdMeshers_MaxElementArea.cxx
25 //           Moved here from SMESH_MaxElementArea.cxx
26 //  Author : Paul RASCLE, EDF
27 //  Module : SMESH
28 //
29 #include "StdMeshers_MaxElementArea.hxx"
30
31 #include "SMDS_MeshElement.hxx"
32 #include "SMESHDS_Mesh.hxx"
33 #include "SMESHDS_Mesh.hxx"
34 #include "SMESHDS_SubMesh.hxx"
35 #include "SMESH_ControlsDef.hxx"
36 #include "SMESH_Mesh.hxx"
37
38 #include <TopExp.hxx>
39 #include <TopTools_IndexedMapOfShape.hxx>
40
41 #include "utilities.h"
42
43 using namespace std;
44
45 //=============================================================================
46 /*!
47  *  
48  */
49 //=============================================================================
50
51 StdMeshers_MaxElementArea::StdMeshers_MaxElementArea(int hypId, int studyId, SMESH_Gen* gen)
52   : SMESH_Hypothesis(hypId, studyId, gen)
53 {
54   _maxArea =1.;
55   _name = "MaxElementArea";
56   _param_algo_dim = 2; 
57 }
58
59 //=============================================================================
60 /*!
61  *  
62  */
63 //=============================================================================
64
65 StdMeshers_MaxElementArea::~StdMeshers_MaxElementArea()
66 {
67 }
68
69 //=============================================================================
70 /*!
71  *  
72  */
73 //=============================================================================
74
75 void StdMeshers_MaxElementArea::SetMaxArea(double maxArea)
76   throw (SALOME_Exception)
77 {
78   double oldArea = _maxArea;
79   if (maxArea <= 0) 
80     throw SALOME_Exception(LOCALIZED("maxArea must be positive"));
81   _maxArea = maxArea;
82   if (_maxArea != oldArea)
83     NotifySubMeshesHypothesisModification();
84 }
85
86 //=============================================================================
87 /*!
88  *  
89  */
90 //=============================================================================
91
92 double StdMeshers_MaxElementArea::GetMaxArea() const
93 {
94   return _maxArea;
95 }
96
97 //=============================================================================
98 /*!
99  *  
100  */
101 //=============================================================================
102
103 ostream & StdMeshers_MaxElementArea::SaveTo(ostream & save)
104 {
105   save << this->_maxArea;
106   return save;
107 }
108
109 //=============================================================================
110 /*!
111  *  
112  */
113 //=============================================================================
114
115 istream & StdMeshers_MaxElementArea::LoadFrom(istream & load)
116 {
117   bool isOK = true;
118   double a;
119   isOK = static_cast<bool>(load >> a);
120   if (isOK) 
121     this->_maxArea = a;
122   else 
123     load.clear(ios::badbit | load.rdstate());
124   return load;
125 }
126
127 //=============================================================================
128 /*!
129  *  
130  */
131 //=============================================================================
132
133 ostream & operator << (ostream & save, StdMeshers_MaxElementArea & hyp)
134 {
135   return hyp.SaveTo( save );
136 }
137
138 //=============================================================================
139 /*!
140  *  
141  */
142 //=============================================================================
143
144 istream & operator >> (istream & load, StdMeshers_MaxElementArea & hyp)
145 {
146   return hyp.LoadFrom( load );
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_MaxElementArea::SetParametersByMesh(const SMESH_Mesh*   theMesh,
159                                                     const TopoDS_Shape& theShape)
160 {
161   if ( !theMesh || theShape.IsNull() )
162     return false;
163
164   _maxArea = 0;
165
166   SMESH::Controls::Area areaControl;
167   SMESH::Controls::TSequenceOfXYZ nodesCoords;
168
169   SMESHDS_Mesh* aMeshDS = const_cast< SMESH_Mesh* >( theMesh )->GetMeshDS();
170
171   TopTools_IndexedMapOfShape faceMap;
172   TopExp::MapShapes( theShape, TopAbs_FACE, faceMap );
173   for ( int iF = 1; iF <= faceMap.Extent(); ++iF )
174   {
175     SMESHDS_SubMesh * subMesh = aMeshDS->MeshElements( faceMap( iF ));
176     if ( !subMesh )
177       return false;
178     SMDS_ElemIteratorPtr fIt = subMesh->GetElements();
179     while ( fIt->more() )
180     {
181       const SMDS_MeshElement* elem = fIt->next();
182       if ( elem->GetType() == SMDSAbs_Face ) {
183         areaControl.GetPoints( elem, nodesCoords );
184         _maxArea = max( _maxArea, areaControl.GetValue( nodesCoords ));
185       }
186     }
187   }
188   return _maxArea > 0;
189 }
190 //================================================================================
191 /*!
192  * \brief Initialize my parameter values by default parameters.
193  *  \retval bool - true if parameter values have been successfully defined
194  */
195 //================================================================================
196
197 bool StdMeshers_MaxElementArea::SetParametersByDefaults(const TDefaults&  dflts,
198                                                         const SMESH_Mesh* /*theMesh*/)
199 {
200   return ( _maxArea = dflts._elemLength*dflts._elemLength );
201 }
202