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