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