Salome HOME
correct previous integration (Porting to Python 2.6)
[modules/smesh.git] / src / StdMeshers / StdMeshers_SegmentLengthAroundVertex.cxx
1 //  Copyright (C) 2007-2008  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.
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 //  SMESH SMESH : implementaion of SMESH idl descriptions
23 //  File   : StdMeshers_SegmentLengthAroundVertex.cxx
24 //  Module : SMESH
25
26 #include "StdMeshers_SegmentLengthAroundVertex.hxx"
27
28 #include "SMESH_Mesh.hxx"
29 #include "SMESH_Algo.hxx"
30 #include "SMDS_MeshNode.hxx"
31 #include "SMESHDS_Mesh.hxx"
32 #include "SMESHDS_SubMesh.hxx"
33 #include "SMESH_MesherHelper.hxx"
34
35 #include "utilities.h"
36
37 #include <BRepAdaptor_Curve.hxx>
38 #include <GCPnts_AbscissaPoint.hxx>
39 #include <TopTools_IndexedMapOfShape.hxx>
40 #include <TopoDS.hxx>
41 #include <TopoDS_Edge.hxx>
42
43 using namespace std;
44
45 //=============================================================================
46 /*!
47  *  
48  */
49 //=============================================================================
50
51 StdMeshers_SegmentLengthAroundVertex::StdMeshers_SegmentLengthAroundVertex
52                                        (int hypId, int studyId, SMESH_Gen * gen)
53   :SMESH_Hypothesis(hypId, studyId, gen)
54 {
55   _length = 1.;
56   _name = "SegmentLengthAroundVertex";
57   _param_algo_dim = 0; // is used by StdMeshers_SegmentAroundVertex_0D
58 }
59
60 //=============================================================================
61 /*!
62  *  
63  */
64 //=============================================================================
65
66 StdMeshers_SegmentLengthAroundVertex::~StdMeshers_SegmentLengthAroundVertex()
67 {
68 }
69
70 //=============================================================================
71 /*!
72  *  
73  */
74 //=============================================================================
75
76 void StdMeshers_SegmentLengthAroundVertex::SetLength(double length) throw(SALOME_Exception)
77 {
78   if (length <= 0)
79     throw SALOME_Exception(LOCALIZED("length must be positive"));
80   if (_length != length) {
81     _length = length;
82     NotifySubMeshesHypothesisModification();
83   }
84 }
85
86 //=============================================================================
87 /*!
88  *  
89  */
90 //=============================================================================
91
92 double StdMeshers_SegmentLengthAroundVertex::GetLength() const
93 {
94   return _length;
95 }
96
97 //=============================================================================
98 /*!
99  *  
100  */
101 //=============================================================================
102
103 ostream & StdMeshers_SegmentLengthAroundVertex::SaveTo(ostream & save)
104 {
105   save << this->_length;
106   return save;
107 }
108
109 //=============================================================================
110 /*!
111  *  
112  */
113 //=============================================================================
114
115 istream & StdMeshers_SegmentLengthAroundVertex::LoadFrom(istream & load)
116 {
117   bool isOK = true;
118   double a;
119   isOK = (load >> a);
120   if (isOK)
121     this->_length = 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_SegmentLengthAroundVertex & hyp)
134 {
135   return hyp.SaveTo( save );
136 }
137
138 //=============================================================================
139 /*!
140  *  
141  */
142 //=============================================================================
143
144 istream & operator >>(istream & load, StdMeshers_SegmentLengthAroundVertex & hyp)
145 {
146   return hyp.LoadFrom( load );
147 }
148
149 //================================================================================
150 /*!
151  * \brief Initialize segment length 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_SegmentLengthAroundVertex::SetParametersByMesh(const SMESH_Mesh*   theMesh,
159                                                                const TopoDS_Shape& theShape)
160 {
161   if ( !theMesh || theShape.IsNull() || theShape.ShapeType() != TopAbs_VERTEX )
162     return false;
163
164   SMESH_MeshEditor editor( const_cast<SMESH_Mesh*>( theMesh ) );
165   SMESH_MesherHelper helper( *editor.GetMesh() );
166
167   // get node built on theShape vertex
168   SMESHDS_Mesh* meshDS = editor.GetMeshDS();
169   SMESHDS_SubMesh* smV = meshDS->MeshElements( theShape );
170   if ( !smV || smV->NbNodes() == 0 )
171     return false;
172   const SMDS_MeshNode* vNode = smV->GetNodes()->next();
173
174   // calculate average length of segments sharing vNode
175
176   _length = 0.;
177   int nbSegs = 0;
178
179   SMDS_ElemIteratorPtr segIt = vNode->GetInverseElementIterator(SMDSAbs_Edge);
180   while ( segIt->more() ) {
181     const SMDS_MeshElement* seg = segIt->next();
182     // get geom edge
183     int shapeID = editor.FindShape( seg );
184     if (!shapeID) continue;
185     const TopoDS_Shape& s = meshDS->IndexToShape( shapeID );
186     if ( s.IsNull() || s.ShapeType() != TopAbs_EDGE ) continue;
187     const TopoDS_Edge& edge = TopoDS::Edge( s );
188     // params of edge ends
189     double u0 = helper.GetNodeU( edge, seg->GetNode(0) );
190     double u1 = helper.GetNodeU( edge, seg->GetNode(1) );
191     // length
192     BRepAdaptor_Curve AdaptCurve( edge );
193     _length += GCPnts_AbscissaPoint::Length( AdaptCurve, u0, u1);
194     nbSegs++;
195   }
196   
197   if ( nbSegs > 1 )
198     _length /= nbSegs;
199
200   return nbSegs;
201 }
202
203 //================================================================================
204 /*!
205  * \brief Initialize my parameter values by default parameters.
206  *  \retval bool - true if parameter values have been successfully defined
207  */
208 //================================================================================
209
210 bool StdMeshers_SegmentLengthAroundVertex::SetParametersByDefaults(const TDefaults&,
211                                                                    const SMESH_Mesh*)
212 {
213   return false;
214 }
215