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