Salome HOME
Merge branch 'V8_4_BR'
[modules/smesh.git] / src / StdMeshers_I / StdMeshers_ProjectionSource2D_i.cxx
1 // Copyright (C) 2007-2016  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_I : idl implementation based on 'SMESH' unit's classes
24 //  File   : StdMeshers_ProjectionSource2D_i.cxx
25 //  Author : Edward AGAPOV
26 //  Module : SMESH
27 //
28 #include "StdMeshers_ProjectionSource2D_i.hxx"
29
30 #include "SMESH_Gen_i.hxx"
31 #include "SMESH_Gen.hxx"
32 #include "SMESH_PythonDump.hxx"
33
34 #include "Utils_CorbaException.hxx"
35 #include "utilities.h"
36
37 #include "StdMeshers_ObjRefUlils.hxx"
38
39 using namespace std;
40
41 //=============================================================================
42 /*!
43  *  StdMeshers_ProjectionSource2D_i::StdMeshers_ProjectionSource2D_i
44  *
45  *  Constructor
46  */
47 //=============================================================================
48
49 StdMeshers_ProjectionSource2D_i::StdMeshers_ProjectionSource2D_i
50 ( PortableServer::POA_ptr thePOA,
51   int                     theStudyId,
52   ::SMESH_Gen*            theGenImpl ) : SALOME::GenericObj_i( thePOA ), 
53                                          SMESH_Hypothesis_i( thePOA )
54 {
55   myBaseImpl = new ::StdMeshers_ProjectionSource2D( theGenImpl->GetANewId(),
56                                                     theStudyId,
57                                                     theGenImpl );
58 }
59
60 //=============================================================================
61 /*!
62  *  StdMeshers_ProjectionSource2D_i::~StdMeshers_ProjectionSource2D_i
63  *
64  *  Destructor
65  */
66 //=============================================================================
67
68 StdMeshers_ProjectionSource2D_i::~StdMeshers_ProjectionSource2D_i()
69 {
70 }
71
72 //=============================================================================
73   /*!
74    * Sets a source <face> to take a mesh pattern from
75    */
76 //=============================================================================
77
78 void StdMeshers_ProjectionSource2D_i::SetSourceFace(GEOM::GEOM_Object_ptr face)
79   throw ( SALOME::SALOME_Exception )
80 {
81   ASSERT( myBaseImpl );
82   try {
83     this->GetImpl()->SetSourceFace( StdMeshers_ObjRefUlils::GeomObjectToShape( face ));
84     CORBA::String_var entry = face->GetStudyEntry();
85     myShapeEntries[ SRC_FACE ] = entry.in();
86   }
87   catch ( SALOME_Exception& S_ex ) {
88     THROW_SALOME_CORBA_EXCEPTION( S_ex.what(), SALOME::BAD_PARAM );
89   }
90   // Update Python script
91   SMESH::TPythonDump() << _this() << ".SetSourceFace( " << face << " )";
92 }
93
94 //=============================================================================
95 /*!
96  * Sets source <mesh> to take a mesh pattern from
97  */
98 //=============================================================================
99
100 void StdMeshers_ProjectionSource2D_i::SetSourceMesh(SMESH::SMESH_Mesh_ptr theMesh)
101   throw ( SALOME::SALOME_Exception )
102 {
103   ASSERT( myBaseImpl );
104
105   ::SMESH_Mesh* mesh = 0;
106
107   if ( !CORBA::is_nil( theMesh ))
108   {
109     SMESH_Mesh_i* mesh_i = SMESH::DownCast< SMESH_Mesh_i* >( theMesh );
110     if ( !mesh_i )
111       THROW_SALOME_CORBA_EXCEPTION( "bad mesh", SALOME::BAD_PARAM );
112     mesh = &mesh_i->GetImpl();
113   }
114
115   try {
116     this->GetImpl()->SetSourceMesh ( mesh );
117   }
118   catch ( SALOME_Exception& S_ex ) {
119     THROW_SALOME_CORBA_EXCEPTION( S_ex.what(), SALOME::BAD_PARAM );
120   }
121
122   myCorbaMesh = SMESH::SMESH_Mesh::_duplicate( theMesh );
123
124   // Update Python script
125   SMESH::TPythonDump() << _this() << ".SetSourceMesh( " << theMesh << " )";
126 }
127
128 //=============================================================================
129 /*!
130  * Return source mesh
131  */
132 //=============================================================================
133
134 SMESH::SMESH_Mesh_ptr StdMeshers_ProjectionSource2D_i::GetSourceMesh()
135 {
136   SMESH::SMESH_Mesh_var mesh = myCorbaMesh;
137   return mesh._retn();
138 }
139
140 //=============================================================================
141 /*!
142  * Sets vertex association between the source face and the target one.
143  * This parameter is optional.
144  * Two vertices must belong to one edge of a face
145  */
146 //=============================================================================
147
148 void StdMeshers_ProjectionSource2D_i::SetVertexAssociation(GEOM::GEOM_Object_ptr sourceVertex1,
149                                                            GEOM::GEOM_Object_ptr sourceVertex2,
150                                                            GEOM::GEOM_Object_ptr targetVertex1,
151                                                            GEOM::GEOM_Object_ptr targetVertex2)
152   throw ( SALOME::SALOME_Exception )
153 {
154   ASSERT( myBaseImpl );
155   try {
156     TopoDS_Shape v1 = StdMeshers_ObjRefUlils::GeomObjectToShape( sourceVertex1 );
157     TopoDS_Shape v2 = StdMeshers_ObjRefUlils::GeomObjectToShape( sourceVertex2 );
158     TopoDS_Shape v3 = StdMeshers_ObjRefUlils::GeomObjectToShape( targetVertex1 );
159     TopoDS_Shape v4 = StdMeshers_ObjRefUlils::GeomObjectToShape( targetVertex2 );
160     this->GetImpl()->SetVertexAssociation( v1, v2, v3, v4 );
161
162     myShapeEntries[ SRC_VERTEX1 ] = StdMeshers_ObjRefUlils::GeomObjectToEntry( sourceVertex1 );
163     myShapeEntries[ SRC_VERTEX2 ] = StdMeshers_ObjRefUlils::GeomObjectToEntry( sourceVertex2 );
164     myShapeEntries[ TGT_VERTEX1 ] = StdMeshers_ObjRefUlils::GeomObjectToEntry( targetVertex1 );
165     myShapeEntries[ TGT_VERTEX2 ] = StdMeshers_ObjRefUlils::GeomObjectToEntry( targetVertex2 );
166   }
167   catch ( SALOME_Exception& S_ex ) {
168     THROW_SALOME_CORBA_EXCEPTION( S_ex.what(), SALOME::BAD_PARAM );
169   }
170   // Update Python script
171   SMESH::TPythonDump() << _this() << ".SetVertexAssociation( "
172                        << sourceVertex1 << ", "
173                        << sourceVertex2 << ", "
174                        << targetVertex1 << ", "
175                        << targetVertex2 << " )";
176 }
177
178 //=============================================================================
179 /*!
180  * Returns the source face
181  */
182 //=============================================================================
183
184 GEOM::GEOM_Object_ptr StdMeshers_ProjectionSource2D_i::GetSourceFace()
185 {
186   ASSERT( myBaseImpl );
187   return StdMeshers_ObjRefUlils::EntryOrShapeToGeomObject
188     ( myShapeEntries[ SRC_FACE ],
189       this->GetImpl()->GetSourceFace() );
190 }
191
192 //=============================================================================
193 /*!
194  * Returns the vertex associated with the target vertex.
195  * Result may be nil if association not set
196  */
197 //=============================================================================
198
199 GEOM::GEOM_Object_ptr StdMeshers_ProjectionSource2D_i::GetSourceVertex(CORBA::Long i)
200 {
201   ASSERT( myBaseImpl );
202   return StdMeshers_ObjRefUlils::EntryOrShapeToGeomObject
203     ( myShapeEntries[ i == 1 ? SRC_VERTEX1 : SRC_VERTEX2 ],
204       this->GetImpl()->GetSourceVertex((int) i ));
205 }
206
207 //=============================================================================
208 /*!
209  * Returns the <i>-th target vertex associated with the <i>-th source vertex.
210  * Result may be nil if association not set.
211  */
212 //=============================================================================
213
214 GEOM::GEOM_Object_ptr StdMeshers_ProjectionSource2D_i::GetTargetVertex(CORBA::Long i)
215 {
216   ASSERT( myBaseImpl );
217   return StdMeshers_ObjRefUlils::EntryOrShapeToGeomObject
218     ( myShapeEntries[ i == 1 ? TGT_VERTEX1 : TGT_VERTEX2 ],
219       this->GetImpl()->GetTargetVertex( (int)i ));
220 }
221
222 //=============================================================================
223 /*!
224  *  StdMeshers_ProjectionSource2D_i::GetImpl
225  *
226  *  Get implementation
227  */
228 //=============================================================================
229
230 ::StdMeshers_ProjectionSource2D* StdMeshers_ProjectionSource2D_i::GetImpl()
231 {
232   return ( ::StdMeshers_ProjectionSource2D* )myBaseImpl;
233 }
234
235 //================================================================================
236 /*!
237  * \brief Verify whether hypothesis supports given entity type 
238   * \param type - dimension (see SMESH::Dimension enumeration)
239   * \retval CORBA::Boolean - TRUE if dimension is supported, FALSE otherwise
240  * 
241  * Verify whether hypothesis supports given entity type (see SMESH::Dimension enumeration)
242  */
243 //================================================================================  
244 CORBA::Boolean StdMeshers_ProjectionSource2D_i::IsDimSupported( SMESH::Dimension type )
245 {
246   return type == SMESH::DIM_2D;
247 }
248
249 //================================================================================
250 /*!
251  * \brief Write parameters in a string
252   * \retval char* - resulting string
253  */
254 //================================================================================
255
256 char* StdMeshers_ProjectionSource2D_i::SaveTo()
257 {
258   ASSERT( myBaseImpl );
259   std::ostringstream os;
260
261   for ( int i = 0; i < NB_SHAPES; ++i )
262     StdMeshers_ObjRefUlils::SaveToStream( myShapeEntries[ i ], os );
263   StdMeshers_ObjRefUlils::SaveToStream( GetSourceMesh(), os );
264
265   myBaseImpl->SaveTo( os );
266
267   return CORBA::string_dup( os.str().c_str() );
268 }
269
270 //================================================================================
271 /*!
272  * \brief Retrieve parameters from the string
273   * \param theStream - the input string
274  */
275 //================================================================================
276
277 void StdMeshers_ProjectionSource2D_i::LoadFrom( const char* theStream )
278 {
279   ASSERT( myBaseImpl );
280   std::istringstream is( theStream );
281
282   TopoDS_Shape shapes[ NB_SHAPES ];
283   for ( int i = 0; i < NB_SHAPES; ++i )
284     shapes[ i ] = StdMeshers_ObjRefUlils::LoadFromStream( is );
285   SMESH::SMESH_Mesh_var mesh = 
286     StdMeshers_ObjRefUlils::LoadObjectFromStream< SMESH::SMESH_Mesh >( is );
287
288   ::SMESH_Mesh* meshImpl = 0;
289
290   if ( !CORBA::is_nil( mesh ))
291   {
292     SMESH_Mesh_i* mesh_i = SMESH::DownCast< SMESH_Mesh_i* >( mesh );
293     if ( mesh_i )
294       meshImpl = &mesh_i->GetImpl();
295   }
296
297   myCorbaMesh = SMESH::SMESH_Mesh::_duplicate( mesh );
298
299   try {
300     GetImpl()->SetSourceMesh       ( meshImpl );
301     GetImpl()->SetSourceFace       ( shapes[ SRC_FACE ] );
302     GetImpl()->SetVertexAssociation( shapes[ SRC_VERTEX1 ],
303                                      shapes[ SRC_VERTEX2 ],
304                                      shapes[ TGT_VERTEX1 ],
305                                      shapes[ TGT_VERTEX2 ]);
306   }
307   catch( ... ) {
308   }
309   myBaseImpl->LoadFrom( is );
310
311   std::istringstream str( theStream );
312   for ( int i = 0; i < NB_SHAPES; ++i )
313     str >> myShapeEntries[ i ];
314 }
315