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