]> SALOME platform Git repositories - plugins/netgenplugin.git/blob - src/NETGENPlugin/NETGENPlugin_SimpleHypothesis_2D.cxx
Salome HOME
Fix compilation after changes in SMESH headers
[plugins/netgenplugin.git] / src / NETGENPlugin / NETGENPlugin_SimpleHypothesis_2D.cxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 //  NETGENPlugin : C++ implementation
21 // File      : NETGENPlugin_SimpleHypothesis_2D.cxx
22 // Author    : Edward AGAPOV
23 // Project   : SALOME
24 //=============================================================================
25 //
26 #include "NETGENPlugin_SimpleHypothesis_2D.hxx"
27 #include "NETGENPlugin_Hypothesis.hxx"
28
29 #include <SMESHDS_SubMesh.hxx>
30 #include <SMESH_ControlsDef.hxx>
31 #include <SMESH_Mesh.hxx>
32 #include <SMESH_subMesh.hxx>
33
34 #include <TopExp_Explorer.hxx>
35
36 #include <utilities.h>
37
38 using namespace std;
39
40 //=============================================================================
41 /*!
42  *  
43  */
44 //=============================================================================
45 NETGENPlugin_SimpleHypothesis_2D::NETGENPlugin_SimpleHypothesis_2D (int         hypId,
46                                                                     int         studyId,
47                                                                     SMESH_Gen * gen)
48   : SMESH_Hypothesis(hypId, studyId, gen),
49     _nbSegments ((int)NETGENPlugin_Hypothesis::GetDefaultNbSegPerEdge()),
50     _segmentLength(0),
51     _area         (0.),
52     _allowQuad    (false)
53 {
54   _name = "NETGEN_SimpleParameters_2D";
55   _param_algo_dim = 2;
56 }
57
58 //=============================================================================
59 /*!
60  *  
61  */
62 //=============================================================================
63 void NETGENPlugin_SimpleHypothesis_2D::SetNumberOfSegments(int nb) throw (SALOME_Exception)
64 {
65   if ( nb < 1 )
66     throw SALOME_Exception("Number of segments must be positive");
67   if (nb != _nbSegments)
68   {
69     _nbSegments = nb;
70     if ( _nbSegments ) _segmentLength = 0.;
71     NotifySubMeshesHypothesisModification();
72   }
73 }
74
75 //=============================================================================
76 /*!
77  *  
78  */
79 //=============================================================================
80 void NETGENPlugin_SimpleHypothesis_2D::SetLocalLength(double segmentLength)
81   throw (SALOME_Exception)
82 {
83   if ( segmentLength < DBL_MIN )
84     throw SALOME_Exception("segment length must be more than zero");
85   if (segmentLength != _segmentLength)
86   {
87     _segmentLength = segmentLength;
88     if ( _segmentLength > DBL_MIN ) _nbSegments = 0;
89     NotifySubMeshesHypothesisModification();
90   }
91 }
92
93 //=============================================================================
94 /*!
95  *  
96  */
97 //=============================================================================
98 void NETGENPlugin_SimpleHypothesis_2D::LengthFromEdges()
99 {
100   if (_area > DBL_MIN )
101   {
102     _area = 0;
103     NotifySubMeshesHypothesisModification();
104   }
105 }
106
107 //=============================================================================
108 /*!
109  *  
110  */
111 //=============================================================================
112 void NETGENPlugin_SimpleHypothesis_2D::SetMaxElementArea(double area)
113 {
114   if ( area < DBL_MIN )
115     area = 0.;
116   if (_area != area)
117   {
118     _area = area;
119     NotifySubMeshesHypothesisModification();
120   }
121 }
122
123 //=======================================================================
124 //function : SetAllowQuadrangles
125 //purpose  : Enables/disables generation of quadrangular faces
126 //=======================================================================
127
128 void NETGENPlugin_SimpleHypothesis_2D::SetAllowQuadrangles(bool toAllow)
129 {
130   if ( _allowQuad != toAllow )
131   {
132     _allowQuad = toAllow;
133     NotifySubMeshesHypothesisModification();
134   }
135 }
136
137 //=======================================================================
138 //function : GetAllowQuadrangles
139 //purpose  : Returns true if generation of quadrangular faces is enabled
140 //=======================================================================
141
142 bool NETGENPlugin_SimpleHypothesis_2D::GetAllowQuadrangles() const
143 {
144   return _allowQuad;
145 }
146
147 //=============================================================================
148 /*!
149  *  
150  */
151 //=============================================================================
152 ostream & NETGENPlugin_SimpleHypothesis_2D::SaveTo(ostream & save)
153 {
154   save << _nbSegments << " " << _segmentLength << " " << _area << " " << _allowQuad;
155
156   return save;
157 }
158
159 //=============================================================================
160 /*!
161  *  
162  */
163 //=============================================================================
164 istream & NETGENPlugin_SimpleHypothesis_2D::LoadFrom(istream & load)
165 {
166   bool isOK = true;
167   double val;
168
169   isOK = static_cast<bool>(load >> val);
170   if (isOK)
171     _nbSegments = (int) val;
172   else
173     load.clear(ios::badbit | load.rdstate());
174
175   isOK = static_cast<bool>(load >> val);
176   if (isOK)
177     _segmentLength = val;
178   else
179     load.clear(ios::badbit | load.rdstate());
180
181   isOK = static_cast<bool>(load >> val);
182   if (isOK)
183     _area = val;
184   else
185     load.clear(ios::badbit | load.rdstate());
186
187   load >> _allowQuad;
188
189   return load;
190 }
191
192 //================================================================================
193 /*!
194  * \brief Does nothing
195  * \param theMesh - the built mesh
196  * \param theShape - the geometry of interest
197  * \retval bool - always false
198  */
199 //================================================================================
200 bool NETGENPlugin_SimpleHypothesis_2D::SetParametersByMesh(const SMESH_Mesh*   theMesh,
201                                                            const TopoDS_Shape& theShape)
202 {
203   // Find out nb of segments.
204   int nbSeg = 0, nbEdges = 0;
205   TopExp_Explorer exp( theShape, TopAbs_EDGE );
206   for ( ; exp.More(); exp.Next() ) {
207     SMESH_subMesh* sm = theMesh->GetSubMeshContaining( exp.Current() );
208     if ( sm && !sm->IsEmpty() ) {
209       nbSeg += sm->GetSubMeshDS()->NbElements();
210       nbEdges++;
211     }
212   }
213   if ( nbEdges )
214     _nbSegments = nbSeg / nbEdges;
215
216   // Find out max face area
217   _area = 0;
218   SMESH::Controls::Area areaControl;
219   SMESH::Controls::TSequenceOfXYZ nodesCoords;
220   const int nbFacesToCheck = 100;
221   for ( exp.Init( theShape, TopAbs_FACE ); exp.More(); exp.Next() ) {
222     SMESH_subMesh* sm = theMesh->GetSubMeshContaining( exp.Current() );
223     if ( sm && !sm->IsEmpty() ) {
224       SMDS_ElemIteratorPtr fIt = sm->GetSubMeshDS()->GetElements();
225       int nbCheckedFaces = 0;
226       while ( fIt->more() && nbCheckedFaces++ < nbFacesToCheck ) {
227         const SMDS_MeshElement* elem = fIt->next();
228         areaControl.GetPoints( elem, nodesCoords );
229         _area = max( _area, areaControl.GetValue( nodesCoords ));
230       }
231     }
232   }
233   return nbEdges;
234 }
235
236 //================================================================================
237 /*!
238  * \brief Initialize my parameter values by default parameters.
239  *  \retval bool - true if parameter values have been successfully defined
240  */
241 //================================================================================
242
243 bool NETGENPlugin_SimpleHypothesis_2D::SetParametersByDefaults(const TDefaults&    dflts,
244                                                                const SMESH_Mesh* /*theMesh*/)
245 {
246   _nbSegments    = dflts._nbSegments;
247   _segmentLength = dflts._elemLength;
248   return _nbSegments && _segmentLength > 0;
249 }
250