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