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