Salome HOME
[SALOME platform 0013410]: SubMesh not taken into account with Netgen 1D-2D et 1D...
[plugins/netgenplugin.git] / src / NETGENPlugin / NETGENPlugin_SimpleHypothesis_2D.cxx
1 //  NETGENPlugin : C++ implementation
2 //
3 //  Copyright (C) 2006  OPEN CASCADE, CEA/DEN, EDF R&D
4 // 
5 //  This library is free software; you can redistribute it and/or 
6 //  modify it under the terms of the GNU Lesser General Public 
7 //  License as published by the Free Software Foundation; either 
8 //  version 2.1 of the License. 
9 // 
10 //  This library is distributed in the hope that it will be useful, 
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
13 //  Lesser General Public License for more details. 
14 // 
15 //  You should have received a copy of the GNU Lesser General Public 
16 //  License along with this library; if not, write to the Free Software 
17 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
18 // 
19 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 //
21 //
22 // File      : NETGENPlugin_SimpleHypothesis_2D.cxx
23 // Author    : Edward AGAPOV
24 // Project   : SALOME
25 //=============================================================================
26
27 #include "NETGENPlugin_SimpleHypothesis_2D.hxx"
28 #include "NETGENPlugin_Hypothesis.hxx"
29
30 #include <SMESH_Mesh.hxx>
31 #include <SMESH_subMesh.hxx>
32 #include <SMESH_ControlsDef.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 {
53   _name = "NETGEN_SimpleParameters_2D";
54   _param_algo_dim = 2;
55 }
56
57 //=============================================================================
58 /*!
59  *  
60  */
61 //=============================================================================
62 void NETGENPlugin_SimpleHypothesis_2D::SetNumberOfSegments(int nb) throw (SALOME_Exception)
63 {
64   if ( nb < 1 )
65     throw SALOME_Exception("Number of segments must be positive");
66   if (nb != _nbSegments)
67   {
68     _nbSegments = nb;
69     if ( _nbSegments ) _segmentLength = 0.;
70     NotifySubMeshesHypothesisModification();
71   }
72 }
73
74 //=============================================================================
75 /*!
76  *  
77  */
78 //=============================================================================
79 void NETGENPlugin_SimpleHypothesis_2D::SetLocalLength(double segmentLength)
80   throw (SALOME_Exception)
81 {
82   if ( segmentLength < DBL_MIN )
83     throw SALOME_Exception("segment length must be more than zero");
84   if (segmentLength != _segmentLength)
85   {
86     _segmentLength = segmentLength;
87     if ( _segmentLength > DBL_MIN ) _nbSegments = 0;
88     NotifySubMeshesHypothesisModification();
89   }
90 }
91
92 //=============================================================================
93 /*!
94  *  
95  */
96 //=============================================================================
97 void NETGENPlugin_SimpleHypothesis_2D::LengthFromEdges()
98 {
99   if (_area > DBL_MIN )
100   {
101     _area = 0;
102     NotifySubMeshesHypothesisModification();
103   }
104 }
105
106 //=============================================================================
107 /*!
108  *  
109  */
110 //=============================================================================
111 void NETGENPlugin_SimpleHypothesis_2D::SetMaxElementArea(double area)
112 {
113   if ( area < DBL_MIN )
114     area = 0.;
115   if (_area != area)
116   {
117     _area = area;
118     NotifySubMeshesHypothesisModification();
119   }
120 }
121
122 //=============================================================================
123 /*!
124  *  
125  */
126 //=============================================================================
127 ostream & NETGENPlugin_SimpleHypothesis_2D::SaveTo(ostream & save)
128 {
129   save << _nbSegments << " " << _segmentLength << " " << _area;
130
131   return save;
132 }
133
134 //=============================================================================
135 /*!
136  *  
137  */
138 //=============================================================================
139 istream & NETGENPlugin_SimpleHypothesis_2D::LoadFrom(istream & load)
140 {
141   bool isOK = true;
142   double val;
143
144   isOK = (load >> val);
145   if (isOK)
146     _nbSegments = (int) val;
147   else
148     load.clear(ios::badbit | load.rdstate());
149
150   isOK = (load >> val);
151   if (isOK)
152     _segmentLength = val;
153   else
154     load.clear(ios::badbit | load.rdstate());
155
156   isOK = (load >> val);
157   if (isOK)
158     _area = val;
159   else
160     load.clear(ios::badbit | load.rdstate());
161
162   return load;
163 }
164
165 //================================================================================
166 /*!
167  * \brief Does nothing
168  * \param theMesh - the built mesh
169  * \param theShape - the geometry of interest
170  * \retval bool - always false
171  */
172 //================================================================================
173 bool NETGENPlugin_SimpleHypothesis_2D::SetParametersByMesh(const SMESH_Mesh*   theMesh,
174                                                            const TopoDS_Shape& theShape)
175 {
176   // Find out nb of segments.
177   int nbSeg = 0, nbEdges = 0;
178   TopExp_Explorer exp( theShape, TopAbs_EDGE );
179   for ( ; exp.More(); exp.Next() ) {
180     SMESH_subMesh* sm = theMesh->GetSubMeshContaining( exp.Current() );
181     if ( sm && !sm->IsEmpty() ) {
182       nbSeg += sm->GetSubMeshDS()->NbElements();
183       nbEdges++;
184     }
185   }
186   if ( nbEdges )
187     _nbSegments = nbSeg / nbEdges;
188
189   // Find out max face area
190   _area = 0;
191   SMESH::Controls::Area areaControl;
192   SMESH::Controls::TSequenceOfXYZ nodesCoords;
193   const int nbFacesToCheck = 100;
194   for ( exp.Init( theShape, TopAbs_FACE ); exp.More(); exp.Next() ) {
195     SMESH_subMesh* sm = theMesh->GetSubMeshContaining( exp.Current() );
196     if ( sm && !sm->IsEmpty() ) {
197       SMDS_ElemIteratorPtr fIt = sm->GetSubMeshDS()->GetElements();
198       int nbCheckedFaces = 0;
199       while ( fIt->more() && nbCheckedFaces++ < nbFacesToCheck ) {
200         const SMDS_MeshElement* elem = fIt->next();
201         areaControl.GetPoints( elem, nodesCoords );
202         _area = max( _area, areaControl.GetValue( nodesCoords ));
203       }
204     }
205   }
206   return nbEdges;
207 }