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