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