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