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