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