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