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