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