Salome HOME
PAL10494 (SMESH python dump uses idl interface). Python dump converted to smesh...
[modules/smesh.git] / src / SMESH_I / SMESH_Pattern_i.cxx
1 //  SMESH SMESH_I : idl implementation based on 'SMESH' unit's calsses
2 //
3 //  Copyright (C) 2003  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. 
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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 // File      : SMESH_Pattern_i.cxx
25 // Created   : Fri Aug 20 16:15:49 2004
26 // Author    : Edward AGAPOV (eap)
27 //  $Header: 
28
29 #include "SMESH_Pattern_i.hxx"
30
31 #include "GEOM_Client.hxx"
32 #include "SMESH_Gen_i.hxx"
33 #include "SMESH_Mesh.hxx"
34 #include "SMESH_Mesh_i.hxx"
35 #include "SMESH_PythonDump.hxx"
36 #include "SMDS_MeshFace.hxx"
37 #include "SMDS_MeshVolume.hxx"
38
39 #include <TopExp_Explorer.hxx>
40 #include <TopoDS.hxx>
41 #include <TopoDS_Face.hxx>
42
43 #include <sstream>
44 #include <set>
45
46 using SMESH::TPythonDump;
47
48 //=======================================================================
49 //function : dumpErrorCode
50 //purpose  : 
51 //=======================================================================
52
53 static void addErrorCode(const char* thePyCommand)
54 {
55   TPythonDump() << "if (isDone != 1):";
56   TPythonDump() << "\tprint '" << thePyCommand << " :', pattern.GetErrorCode()";
57 }
58
59 //=============================================================================
60 /*!
61  *  SMESH_Gen_i::GetPattern
62  *
63  *  Create pattern mapper
64  */
65 //=============================================================================
66
67 SMESH::SMESH_Pattern_ptr SMESH_Gen_i::GetPattern()
68 {
69   // Update Python script
70   TPythonDump() << "pattern = " << this << ".GetPattern()";
71
72   SMESH_Pattern_i* i = new SMESH_Pattern_i( this );
73   SMESH::SMESH_Pattern_var anObj = i->_this();
74   return anObj._retn();
75 }
76
77 //=======================================================================
78 //function : SMESH_Pattern_i
79 //purpose  : 
80 //=======================================================================
81
82 SMESH_Pattern_i::SMESH_Pattern_i( SMESH_Gen_i* theGen_i ):
83        myGen( theGen_i )
84 {
85 }
86
87 //=======================================================================
88 //function : getMesh
89 //purpose  : 
90 //=======================================================================
91
92 ::SMESH_Mesh* SMESH_Pattern_i::getMesh( SMESH::SMESH_Mesh_ptr & theMesh )
93 {
94   SMESH_Mesh_i* anImplPtr = 
95     dynamic_cast<SMESH_Mesh_i*>( SMESH_Gen_i::GetServant( theMesh ).in() );
96   if ( anImplPtr )
97     return & anImplPtr->GetImpl();
98
99   return 0;
100 }
101
102 //=======================================================================
103 //function : LoadFromFile
104 //purpose  : 
105 //=======================================================================
106
107 CORBA::Boolean SMESH_Pattern_i::LoadFromFile(const char* theFileContents)
108 {
109   // Update Python script
110   TPythonDump() << "isDone = pattern.LoadFromFile(" << theFileContents << ")";
111   addErrorCode( "LoadFromFile" );
112
113   return myPattern.Load( theFileContents );
114 }
115
116 //=======================================================================
117 //function : LoadFromFace
118 //purpose  : 
119 //=======================================================================
120
121 CORBA::Boolean SMESH_Pattern_i::LoadFromFace(SMESH::SMESH_Mesh_ptr theMesh,
122                                              GEOM::GEOM_Object_ptr theFace,
123                                              CORBA::Boolean        theProject)
124 {
125   if ( theMesh->_is_nil() || theFace->_is_nil() )
126     return false;
127
128   ::SMESH_Mesh* aMesh = getMesh( theMesh );
129   if ( !aMesh )
130     return false;
131
132   TopoDS_Shape aFace = myGen->GeomObjectToShape( theFace );
133   if ( aFace.IsNull() || aFace.ShapeType() != TopAbs_FACE )
134     return false;
135
136   // Update Python script
137   TPythonDump() << "isDone = pattern.LoadFromFace( " << theMesh << ", "
138                 << theFace << ", " << theProject << " )";
139   addErrorCode( "LoadFromFace" );
140
141   return myPattern.Load( aMesh, TopoDS::Face( aFace ), theProject );
142 }
143
144 //=======================================================================
145 //function : LoadFrom3DBlock
146 //purpose  : 
147 //=======================================================================
148
149 CORBA::Boolean SMESH_Pattern_i::LoadFrom3DBlock(SMESH::SMESH_Mesh_ptr theMesh,
150                                                 GEOM::GEOM_Object_ptr theBlock)
151 {
152   if ( theMesh->_is_nil() || theBlock->_is_nil() )
153     return false;
154
155   ::SMESH_Mesh* aMesh = getMesh( theMesh );
156   if ( !aMesh )
157     return false;
158
159   TopoDS_Shape aShape = myGen->GeomObjectToShape( theBlock );
160   if ( aShape.IsNull())
161     return false;
162
163   TopExp_Explorer exp ( aShape, TopAbs_SHELL );
164   if ( !exp.More() )
165     return false;
166
167   // Update Python script
168   TPythonDump() << "isDone = pattern.LoadFrom3DBlock( " << theMesh << ", " << theBlock << " )";
169   addErrorCode( "LoadFrom3DBlock" );
170
171   return myPattern.Load( aMesh, TopoDS::Shell( exp.Current() ));
172 }
173
174 //=======================================================================
175 //function : ApplyToFace
176 //purpose  : 
177 //=======================================================================
178
179 SMESH::point_array* SMESH_Pattern_i::ApplyToFace(GEOM::GEOM_Object_ptr theFace,
180                                                  GEOM::GEOM_Object_ptr theVertexOnKeyPoint1,
181                                                  CORBA::Boolean        theReverse)
182 {
183   SMESH::point_array_var points = new SMESH::point_array;
184   list<const gp_XYZ *> xyzList;
185
186   TopoDS_Shape F = myGen->GeomObjectToShape( theFace );
187   TopoDS_Shape V = myGen->GeomObjectToShape( theVertexOnKeyPoint1 );
188
189   if (!F.IsNull() && F.ShapeType() == TopAbs_FACE &&
190       !V.IsNull() && V.ShapeType() == TopAbs_VERTEX
191       &&
192       myPattern.Apply( TopoDS::Face( F ), TopoDS::Vertex( V ), theReverse ) &&
193       myPattern.GetMappedPoints( xyzList ))
194   {
195     points->length( xyzList.size() );
196     list<const gp_XYZ *>::iterator xyzIt = xyzList.begin();
197     for ( int i = 0; xyzIt != xyzList.end(); xyzIt++ ) {
198       SMESH::PointStruct & p = points[ i++ ];
199       (*xyzIt)->Coord( p.x, p.y, p.z );
200     }
201   }
202
203   // Update Python script
204   TPythonDump() << "pattern.ApplyToFace( " << theFace << ", "
205                 << theVertexOnKeyPoint1 << ", " << theReverse << " )";
206
207   return points._retn();
208 }
209
210 //=======================================================================
211 //function : ApplyTo3DBlock
212 //purpose  : 
213 //=======================================================================
214
215 SMESH::point_array* SMESH_Pattern_i::ApplyTo3DBlock(GEOM::GEOM_Object_ptr theBlock,
216                                                     GEOM::GEOM_Object_ptr theVertex000,
217                                                     GEOM::GEOM_Object_ptr theVertex001)
218 {
219   SMESH::point_array_var points = new SMESH::point_array;
220   list<const gp_XYZ *> xyzList;
221
222   TopExp_Explorer exp( myGen->GeomObjectToShape( theBlock ), TopAbs_SHELL );
223   TopoDS_Shape V000 = myGen->GeomObjectToShape( theVertex000 );
224   TopoDS_Shape V001 = myGen->GeomObjectToShape( theVertex001 );
225
226   if (exp.More() &&
227       !V000.IsNull() && V000.ShapeType() == TopAbs_VERTEX &&
228       !V001.IsNull() && V001.ShapeType() == TopAbs_VERTEX 
229       &&
230       myPattern.Apply(TopoDS::Shell( exp.Current() ),
231                       TopoDS::Vertex( V000 ),
232                       TopoDS::Vertex( V001 )) &&
233       myPattern.GetMappedPoints( xyzList ))
234   {
235     points->length( xyzList.size() );
236     list<const gp_XYZ *>::iterator xyzIt = xyzList.begin();
237     for ( int i = 0; xyzIt != xyzList.end(); xyzIt++ ) {
238       SMESH::PointStruct & p = points[ i++ ];
239       (*xyzIt)->Coord( p.x, p.y, p.z );
240     }
241   }
242
243   // Update Python script
244   TPythonDump() << "pattern.ApplyTo3DBlock( " << theBlock << ", "
245                 << theVertex000 << ", " << theVertex001 << " )";
246
247   return points._retn();
248 }
249
250 //=======================================================================
251 //function : ApplyToMeshFaces
252 //purpose  : 
253 //=======================================================================
254
255 SMESH::point_array*
256   SMESH_Pattern_i::ApplyToMeshFaces(SMESH::SMESH_Mesh_ptr    theMesh,
257                                     const SMESH::long_array& theFacesIDs,
258                                     CORBA::Long              theNodeIndexOnKeyPoint1,
259                                     CORBA::Boolean           theReverse)
260 {
261   SMESH::point_array_var points = new SMESH::point_array;
262
263   ::SMESH_Mesh* aMesh = getMesh( theMesh );
264   if ( !aMesh )
265     return points._retn();
266
267   list<const gp_XYZ *> xyzList;
268   set<const SMDS_MeshFace*> fset;
269   for (int i = 0; i < theFacesIDs.length(); i++)
270   {
271     CORBA::Long index = theFacesIDs[i];
272     const SMDS_MeshElement * elem = aMesh->GetMeshDS()->FindElement(index);
273     if ( elem && elem->GetType() == SMDSAbs_Face )
274       fset.insert( static_cast<const SMDS_MeshFace *>( elem ));
275   }
276   if (myPattern.Apply( fset, theNodeIndexOnKeyPoint1, theReverse ) &&
277       myPattern.GetMappedPoints( xyzList ))
278   {
279     points->length( xyzList.size() );
280     list<const gp_XYZ *>::iterator xyzIt = xyzList.begin();
281     for ( int i = 0; xyzIt != xyzList.end(); xyzIt++ ) {
282       SMESH::PointStruct & p = points[ i++ ];
283       (*xyzIt)->Coord( p.x, p.y, p.z );
284     }
285   }
286
287   // Update Python script
288   TPythonDump() << "pattern.ApplyToMeshFaces( " << theMesh << ", "
289                 << theFacesIDs << ", "
290                 << theNodeIndexOnKeyPoint1 << ", " << theReverse << " )";
291
292   return points._retn();
293 }
294
295 //=======================================================================
296 //function : ApplyToHexahedrons
297 //purpose  : 
298 //=======================================================================
299
300 SMESH::point_array*
301   SMESH_Pattern_i::ApplyToHexahedrons(SMESH::SMESH_Mesh_ptr    theMesh,
302                                       const SMESH::long_array& theVolumesIDs,
303                                       CORBA::Long              theNode000Index,
304                                       CORBA::Long              theNode001Index)
305 {
306   SMESH::point_array_var points = new SMESH::point_array;
307
308   ::SMESH_Mesh* aMesh = getMesh( theMesh );
309   if ( !aMesh )
310     return points._retn();
311
312   list<const gp_XYZ *> xyzList;
313   set<const SMDS_MeshVolume*> vset;
314   for (int i = 0; i < theVolumesIDs.length(); i++)
315   {
316     CORBA::Long index = theVolumesIDs[i];
317     const SMDS_MeshElement * elem = aMesh->GetMeshDS()->FindElement(index);
318     if ( elem && elem->GetType() == SMDSAbs_Volume && elem->NbNodes() == 8 )
319       vset.insert( static_cast<const SMDS_MeshVolume *>( elem ));
320   }
321   if (myPattern.Apply( vset, theNode000Index, theNode001Index ) &&
322       myPattern.GetMappedPoints( xyzList ))
323   {
324     points->length( xyzList.size() );
325     list<const gp_XYZ *>::iterator xyzIt = xyzList.begin();
326     for ( int i = 0; xyzIt != xyzList.end(); xyzIt++ ) {
327       SMESH::PointStruct & p = points[ i++ ];
328       (*xyzIt)->Coord( p.x, p.y, p.z );
329     }
330   }
331
332   // Update Python script
333   TPythonDump() << "pattern.ApplyToHexahedrons( " << theMesh << ", "
334                 << theVolumesIDs << ", "
335                 << theNode000Index << ", " << theNode001Index << " )";
336
337   return points._retn();
338 }
339
340 //=======================================================================
341 //function : MakeMesh
342 //purpose  : 
343 //=======================================================================
344
345 CORBA::Boolean SMESH_Pattern_i::MakeMesh (SMESH::SMESH_Mesh_ptr theMesh,
346                                           const CORBA::Boolean  CreatePolygons,
347                                           const CORBA::Boolean  CreatePolyedrs)
348 {
349   ::SMESH_Mesh* aMesh = getMesh( theMesh );
350   if ( !aMesh )
351     return false;
352
353   // Update Python script
354   TPythonDump() << "isDone = pattern.MakeMesh( " << theMesh << ", "
355                 << CreatePolygons << ", " << CreatePolyedrs << " )";
356   addErrorCode( "MakeMesh" );
357
358   return myPattern.MakeMesh( aMesh, CreatePolygons, CreatePolyedrs );
359 }
360
361 //=======================================================================
362 //function : GetString
363 //purpose  : 
364 //=======================================================================
365
366 char* SMESH_Pattern_i::GetString()
367 {
368   ostringstream os;
369   myPattern.Save( os );
370   
371   return CORBA::string_dup( os.str().c_str() );
372 }
373
374 //=======================================================================
375 //function : Is2D
376 //purpose  : 
377 //=======================================================================
378
379 CORBA::Boolean SMESH_Pattern_i::Is2D()
380 {
381   return myPattern.Is2D();
382 }
383
384 //=======================================================================
385 //function : GetPoints
386 //purpose  : 
387 //=======================================================================
388
389 SMESH::point_array* SMESH_Pattern_i::GetPoints()
390 {
391   SMESH::point_array_var points = new SMESH::point_array;
392   list<const gp_XYZ *> xyzList;
393
394   if (myPattern.GetPoints( xyzList ))
395   {
396     points->length( xyzList.size() );
397     list<const gp_XYZ *>::iterator xyzIt = xyzList.begin();
398     for ( int i = 0; xyzIt != xyzList.end(); xyzIt++ ) {
399       SMESH::PointStruct & p = points[ i++ ];
400       (*xyzIt)->Coord( p.x, p.y, p.z );
401     }
402   }
403
404   return points._retn();
405 }
406
407 //=======================================================================
408 //function : GetKeyPoints
409 //purpose  : 
410 //=======================================================================
411
412 SMESH::long_array* SMESH_Pattern_i::GetKeyPoints()
413 {
414   SMESH::long_array_var ids = new SMESH::long_array;
415   if ( myPattern.IsLoaded() ) {
416     const list< int > & idList = myPattern.GetKeyPointIDs();
417     ids->length( idList.size() );
418     list< int >::const_iterator iIt = idList.begin();
419     for ( int i = 0; iIt != idList.end(); iIt++, i++ )
420       ids[ i ] = *iIt;
421   }
422   return ids._retn();
423 }
424
425 //=======================================================================
426 //function : GetElementPoints
427 //purpose  : 
428 //=======================================================================
429
430 SMESH::array_of_long_array* SMESH_Pattern_i::GetElementPoints(CORBA::Boolean applied)
431 {
432   SMESH::array_of_long_array_var arrayOfArray = new SMESH::array_of_long_array;
433
434   const list< list< int > >& listOfIdList = myPattern.GetElementPointIDs(applied);
435   arrayOfArray->length( listOfIdList.size() );
436   list< list< int > >::const_iterator llIt = listOfIdList.begin();
437   for ( int i = 0 ; llIt != listOfIdList.end(); llIt++, i++ )
438   {
439     const list< int > & idList = (*llIt);
440     SMESH::long_array& ids = arrayOfArray[ i ];
441     ids.length( idList.size() );
442     list< int >::const_iterator iIt = idList.begin();
443     for ( int j = 0; iIt != idList.end(); iIt++, j++ )
444       ids[ j ] = *iIt;
445   }
446   return arrayOfArray._retn();
447 }
448
449 //=======================================================================
450 //function : GetErrorCode
451 //purpose  : 
452 //=======================================================================
453
454 #define RETCASE(enm) case ::SMESH_Pattern::enm: return SMESH::SMESH_Pattern::enm;
455
456 SMESH::SMESH_Pattern::ErrorCode SMESH_Pattern_i::GetErrorCode()
457 {
458   switch ( myPattern.GetErrorCode() ) {
459     RETCASE( ERR_OK );
460     RETCASE( ERR_READ_NB_POINTS );
461     RETCASE( ERR_READ_POINT_COORDS );
462     RETCASE( ERR_READ_TOO_FEW_POINTS );
463     RETCASE( ERR_READ_3D_COORD );
464     RETCASE( ERR_READ_NO_KEYPOINT );
465     RETCASE( ERR_READ_BAD_INDEX );
466     RETCASE( ERR_READ_ELEM_POINTS );
467     RETCASE( ERR_READ_NO_ELEMS );
468     RETCASE( ERR_READ_BAD_KEY_POINT );
469     RETCASE( ERR_SAVE_NOT_LOADED );
470     RETCASE( ERR_LOAD_EMPTY_SUBMESH );
471     RETCASE( ERR_LOADF_NARROW_FACE );
472     RETCASE( ERR_LOADF_CLOSED_FACE );
473     RETCASE( ERR_LOADV_BAD_SHAPE );
474     RETCASE( ERR_LOADV_COMPUTE_PARAMS );
475     RETCASE( ERR_APPL_NOT_LOADED );
476     RETCASE( ERR_APPL_BAD_DIMENTION );
477     RETCASE( ERR_APPL_BAD_NB_VERTICES );
478     RETCASE( ERR_APPLF_BAD_TOPOLOGY );
479     RETCASE( ERR_APPLF_BAD_VERTEX );
480     RETCASE( ERR_APPLF_INTERNAL_EEROR );
481     RETCASE( ERR_APPLV_BAD_SHAPE );
482     RETCASE( ERR_MAKEM_NOT_COMPUTED );
483   default:;
484   };
485   return SMESH::SMESH_Pattern::ERR_OK;
486 }
487