Salome HOME
Copyright update 2022
[modules/smesh.git] / src / StdMeshers / StdMeshers_ProjectionSource2D.cxx
1 // Copyright (C) 2007-2022  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 : idl implementation based on 'SMESH' unit's classes
24 //  File   : StdMeshers_ProjectionSource2D.cxx
25 //  Author : Edward AGAPOV
26 //  Module : SMESH
27 //
28 #include "StdMeshers_ProjectionSource2D.hxx"
29
30 #include "SMESH_Mesh.hxx"
31 #include "SMESH_MesherHelper.hxx"
32 #include "StdMeshers_ProjectionUtils.hxx"
33
34 #include "utilities.h"
35
36 #include <TopoDS.hxx>
37
38 using namespace std;
39
40 //=============================================================================
41 /*!
42  *  StdMeshers_ProjectionSource2D::StdMeshers_ProjectionSource2D
43  *
44  *  Constructor
45  */
46 //=============================================================================
47
48 StdMeshers_ProjectionSource2D::StdMeshers_ProjectionSource2D(int hypId,
49                                                              SMESH_Gen * gen)
50   : SMESH_Hypothesis(hypId, gen)
51 {
52   _name = "ProjectionSource2D"; // used by Projection_2D
53   _param_algo_dim = 2; // 2D
54   _sourceMesh = 0;
55 }
56
57 //=============================================================================
58 /*!
59  *  StdMeshers_ProjectionSource2D::~StdMeshers_ProjectionSource2D
60  *
61  *  Destructor
62  */
63 //=============================================================================
64
65 StdMeshers_ProjectionSource2D::~StdMeshers_ProjectionSource2D()
66 {
67 }
68
69 //=============================================================================
70 /*!
71  * Sets a source <face> to take a mesh pattern from
72  */
73 //=============================================================================
74
75 void StdMeshers_ProjectionSource2D::SetSourceFace(const TopoDS_Shape& Face)
76 {
77   if ( Face.IsNull() )
78     throw SALOME_Exception(LOCALIZED("Null Face is not allowed"));
79
80   if ( Face.ShapeType() != TopAbs_FACE && Face.ShapeType() != TopAbs_COMPOUND )
81     throw SALOME_Exception(LOCALIZED("Wrong shape type"));
82
83   if ( !_sourceFace.IsSame( Face ) )
84   {
85     _sourceFace = Face;
86
87     NotifySubMeshesHypothesisModification();
88   }
89 }
90
91 //=============================================================================
92 /*!
93  * Sets vertex association between the source face and the target one.
94  * This parameter is optional.
95  * Two vertices must belong to one edge of a face
96  */
97 //=============================================================================
98
99 void StdMeshers_ProjectionSource2D::SetVertexAssociation(const TopoDS_Shape& sourceVertex1,
100                                                          const TopoDS_Shape& sourceVertex2,
101                                                          const TopoDS_Shape& targetVertex1,
102                                                          const TopoDS_Shape& targetVertex2)
103 {
104   if ( sourceVertex1.IsNull() != targetVertex1.IsNull() ||
105        sourceVertex2.IsNull() != targetVertex2.IsNull() )
106     throw SALOME_Exception(LOCALIZED("Vertices must be provided in couples"));
107
108   if ( sourceVertex1.IsNull() != sourceVertex2.IsNull() )
109   {
110     // possibly there is only 1 vertex in the face
111     if ( !_sourceFace.IsNull() &&
112          SMESH_MesherHelper::Count( _sourceFace, TopAbs_VERTEX, /*ignoreSame=*/true) != 1 )
113       throw SALOME_Exception(LOCALIZED("Two or none pairs of vertices must be provided"));
114   }
115
116   if ( !sourceVertex1.IsNull() )
117     if ( sourceVertex1.ShapeType() != TopAbs_VERTEX ||
118          targetVertex1.ShapeType() != TopAbs_VERTEX )
119       throw SALOME_Exception(LOCALIZED("Wrong shape type"));
120
121   if ( !sourceVertex2.IsNull() )
122     if ( sourceVertex2.ShapeType() != TopAbs_VERTEX ||
123          targetVertex2.ShapeType() != TopAbs_VERTEX )
124       throw SALOME_Exception(LOCALIZED("Wrong shape type"));
125
126
127   if ( !_sourceVertex1.IsSame( sourceVertex1 ) ||
128        !_sourceVertex2.IsSame( sourceVertex2 ) ||
129        !_targetVertex1.IsSame( targetVertex1 ) ||
130        !_targetVertex2.IsSame( targetVertex2 ) )
131   {
132     _sourceVertex1 = TopoDS::Vertex( sourceVertex1 );
133     _sourceVertex2 = TopoDS::Vertex( sourceVertex2 );
134     _targetVertex1 = TopoDS::Vertex( targetVertex1 );
135     _targetVertex2 = TopoDS::Vertex( targetVertex2 );
136
137     NotifySubMeshesHypothesisModification();
138   }
139 }
140
141 //=============================================================================
142 /*!
143  * Sets source <mesh> to take a mesh pattern from
144  */
145 //=============================================================================
146
147 void StdMeshers_ProjectionSource2D::SetSourceMesh(SMESH_Mesh* mesh)
148 {
149   if ( _sourceMesh != mesh ) {
150     _sourceMesh = mesh;
151     NotifySubMeshesHypothesisModification();
152   }
153 }
154
155 //=============================================================================
156 /*!
157  * Returns the source face
158  */
159 //=============================================================================
160
161 TopoDS_Shape StdMeshers_ProjectionSource2D::GetSourceFace() const
162 {
163   return _sourceFace;
164 }
165
166 //=============================================================================
167 /*!
168  * Returns the vertex associated with the target vertex.
169  * Result may be nil if association not set
170  */
171 //=============================================================================
172
173 TopoDS_Vertex StdMeshers_ProjectionSource2D::GetSourceVertex(int i) const
174 {
175   if ( i == 1 )
176     return _sourceVertex1;
177   else if ( i == 2 )
178     return _sourceVertex2;
179   else
180     throw SALOME_Exception(LOCALIZED("Wrong vertex index"));
181 }
182
183 //=============================================================================
184 /*!
185  * Returns the <i>-th target vertex associated with the <i>-th source vertex.
186  * Result may be nil if association not set.
187  */
188 //=============================================================================
189
190 TopoDS_Vertex StdMeshers_ProjectionSource2D::GetTargetVertex(int i) const
191 {
192   if ( i == 1 )
193     return _targetVertex1;
194   else if ( i == 2 )
195     return _targetVertex2;
196   else
197     throw SALOME_Exception(LOCALIZED("Wrong vertex index"));
198 }
199
200 //=============================================================================
201 /*!
202  *  
203  */
204 //=============================================================================
205
206 ostream & StdMeshers_ProjectionSource2D::SaveTo(ostream & save)
207 {
208   // we store it in order to be able to detect that hypo is really modified
209   save << " " << _sourceFace.TShape().operator->()  ;
210   save << " " << _sourceVertex1.TShape().operator->();
211   save << " " << _targetVertex1.TShape().operator->();
212   save << " " << _sourceVertex2.TShape().operator->();
213   save << " " << _targetVertex2.TShape().operator->();
214   save << " " << ( _sourceMesh ? _sourceMesh->GetId() : -1 );
215   return save;
216 }
217
218 //=============================================================================
219 /*!
220  *  
221  */
222 //=============================================================================
223
224 istream & StdMeshers_ProjectionSource2D::LoadFrom(istream & load)
225 {
226   // impossible to restore w/o any context
227   // It is done by servant
228   return load;
229 }
230
231 //=============================================================================
232 /*!
233  *  
234  */
235 //=============================================================================
236
237 ostream & operator <<(ostream & save, StdMeshers_ProjectionSource2D & hyp)
238 {
239   return hyp.SaveTo( save );
240 }
241
242 //=============================================================================
243 /*!
244  *  
245  */
246 //=============================================================================
247
248 istream & operator >>(istream & load, StdMeshers_ProjectionSource2D & hyp)
249 {
250   return hyp.LoadFrom( load );
251 }
252
253 //================================================================================
254 /*!
255  * \brief Initialize start and end length by the mesh built on the geometry
256  * \param theMesh - the built mesh
257  * \param theShape - the geometry of interest
258  * \retval bool - true if parameter values have been successfully defined
259  */
260 //================================================================================
261
262 bool StdMeshers_ProjectionSource2D::SetParametersByMesh(const SMESH_Mesh*   ,
263                                                         const TopoDS_Shape& )
264 {
265   return false;
266 }
267
268 //================================================================================
269 /*!
270  * \brief Return all parameters
271  */
272 //================================================================================
273
274 void StdMeshers_ProjectionSource2D::GetStoreParams(TopoDS_Shape& s1,
275                                                    TopoDS_Shape& s2,
276                                                    TopoDS_Shape& s3,
277                                                    TopoDS_Shape& s4,
278                                                    TopoDS_Shape& s5) const
279 {
280   s1 = _sourceFace;
281   s2 = _sourceVertex1;
282   s3 = _sourceVertex2;
283   s4 = _targetVertex1;
284   s5 = _targetVertex2;
285 }
286
287 //================================================================================
288 /*!
289  * \brief Set all parameters without notifying on modification
290  */
291 //================================================================================
292
293 void StdMeshers_ProjectionSource2D::RestoreParams(const TopoDS_Shape& s1,
294                                                   const TopoDS_Shape& s2,
295                                                   const TopoDS_Shape& s3,
296                                                   const TopoDS_Shape& s4,
297                                                   const TopoDS_Shape& s5,
298                                                   SMESH_Mesh*         mesh)
299 {
300   _sourceFace    = s1;
301   _sourceVertex1 = TopoDS::Vertex( s2 );
302   _sourceVertex2 = TopoDS::Vertex( s3 );
303   _targetVertex1 = TopoDS::Vertex( s4 );
304   _targetVertex2 = TopoDS::Vertex( s5 );
305   _sourceMesh   = mesh;
306 }
307
308 //================================================================================
309 /*!
310  * \brief Initialize my parameter values by default parameters.
311  *  \retval bool - true if parameter values have been successfully defined
312  */
313 //================================================================================
314
315 bool StdMeshers_ProjectionSource2D::SetParametersByDefaults(const TDefaults&  /*dflts*/,
316                                                             const SMESH_Mesh* /*theMesh*/)
317 {
318   return false;
319 }
320