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