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