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