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