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