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