Salome HOME
Integration of PAL/SALOME V2.1.0c from OCC
[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
36 #include <TopExp_Explorer.hxx>
37 #include <TopoDS.hxx>
38 #include <TopoDS_Face.hxx>
39
40 #include <sstream>
41
42 //=============================================================================
43 /*!
44  *  SMESH_Gen_i::GetPattern
45  *
46  *  Create pattern mapper
47  */
48 //=============================================================================
49
50 SMESH::SMESH_Pattern_ptr SMESH_Gen_i::GetPattern()
51 {
52   SMESH_Pattern_i* i = new SMESH_Pattern_i( this );
53   SMESH::SMESH_Pattern_var anObj = i->_this();
54   return anObj._retn();
55 }
56
57 //=======================================================================
58 //function : SMESH_Pattern_i
59 //purpose  : 
60 //=======================================================================
61
62 SMESH_Pattern_i::SMESH_Pattern_i( SMESH_Gen_i* theGen_i ):
63        myGen( theGen_i )
64 {
65 }
66
67 //=======================================================================
68 //function : getShape
69 //purpose  : 
70 //=======================================================================
71
72 TopoDS_Shape SMESH_Pattern_i::getShape( GEOM::GEOM_Object_ptr & theGeomObject )
73 {
74   return myGen->GetShapeReader()->GetShape( SMESH_Gen_i::GetGeomEngine(), theGeomObject );
75 }
76
77 //=======================================================================
78 //function : getMesh
79 //purpose  : 
80 //=======================================================================
81
82 ::SMESH_Mesh* SMESH_Pattern_i::getMesh( SMESH::SMESH_Mesh_ptr & theMesh )
83 {
84   SMESH_Mesh_i* anImplPtr = 
85     dynamic_cast<SMESH_Mesh_i*>( SMESH_Gen_i::GetServant( theMesh ).in() );
86   if ( anImplPtr )
87     return & anImplPtr->GetImpl();
88
89   return 0;
90 }
91
92 //=======================================================================
93 //function : LoadFromFile
94 //purpose  : 
95 //=======================================================================
96
97 CORBA::Boolean SMESH_Pattern_i::LoadFromFile(const char* theFileContents)
98 {
99   return myPattern.Load( theFileContents );
100 }
101
102 //=======================================================================
103 //function : LoadFromFace
104 //purpose  : 
105 //=======================================================================
106
107 CORBA::Boolean SMESH_Pattern_i::LoadFromFace(SMESH::SMESH_Mesh_ptr theMesh,
108                                              GEOM::GEOM_Object_ptr theFace,
109                                              CORBA::Boolean        theProject)
110 {
111   if ( theMesh->_is_nil() || theFace->_is_nil() )
112     return false;
113
114   ::SMESH_Mesh* aMesh = getMesh( theMesh );
115   if ( !aMesh )
116     return false;
117
118   TopoDS_Face aFace = TopoDS::Face( getShape( theFace ));
119   if ( aFace.IsNull() )
120     return false;
121
122   return myPattern.Load( aMesh, aFace, theProject );
123 }
124
125 //=======================================================================
126 //function : LoadFrom3DBlock
127 //purpose  : 
128 //=======================================================================
129
130 CORBA::Boolean SMESH_Pattern_i::LoadFrom3DBlock(SMESH::SMESH_Mesh_ptr theMesh,
131                                                 GEOM::GEOM_Object_ptr theBlock)
132 {
133   if ( theMesh->_is_nil() || theBlock->_is_nil() )
134     return false;
135
136   ::SMESH_Mesh* aMesh = getMesh( theMesh );
137   if ( !aMesh )
138     return false;
139
140   TopoDS_Shape aShape = getShape( theBlock );
141   if ( aShape.IsNull())
142     return false;
143
144   TopExp_Explorer exp ( aShape, TopAbs_SHELL );
145   if ( !exp.More() )
146     return false;
147
148   return myPattern.Load( aMesh, TopoDS::Shell( exp.Current() ));
149 }
150
151 //=======================================================================
152 //function : ApplyToFace
153 //purpose  : 
154 //=======================================================================
155
156 SMESH::point_array* SMESH_Pattern_i::ApplyToFace(GEOM::GEOM_Object_ptr theFace,
157                                                  GEOM::GEOM_Object_ptr theVertexOnKeyPoint1,
158                                                  CORBA::Boolean        theReverse)
159 {
160   SMESH::point_array_var points = new SMESH::point_array;
161   list<const gp_XYZ *> xyzList;
162
163   TopoDS_Shape F = getShape( theFace );
164   TopoDS_Shape V = getShape( theVertexOnKeyPoint1 );
165
166   if (!F.IsNull() && F.ShapeType() == TopAbs_FACE &&
167       !V.IsNull() && V.ShapeType() == TopAbs_VERTEX
168       &&
169       myPattern.Apply( TopoDS::Face( F ), TopoDS::Vertex( V ), theReverse ) &&
170       myPattern.GetMappedPoints( xyzList ))
171   {
172     points->length( xyzList.size() );
173     list<const gp_XYZ *>::iterator xyzIt = xyzList.begin();
174     for ( int i = 0; xyzIt != xyzList.end(); xyzIt++ ) {
175       SMESH::PointStruct & p = points[ i++ ];
176       (*xyzIt)->Coord( p.x, p.y, p.z );
177     }
178   }
179
180   return points._retn();
181 }
182
183 //=======================================================================
184 //function : ApplyTo3DBlock
185 //purpose  : 
186 //=======================================================================
187
188 SMESH::point_array* SMESH_Pattern_i::ApplyTo3DBlock(GEOM::GEOM_Object_ptr theBlock,
189                                                     GEOM::GEOM_Object_ptr theVertex000,
190                                                     GEOM::GEOM_Object_ptr theVertex001)
191 {
192   SMESH::point_array_var points = new SMESH::point_array;
193   list<const gp_XYZ *> xyzList;
194
195   TopExp_Explorer exp( getShape( theBlock ), TopAbs_SHELL );
196   TopoDS_Shape V000 = getShape( theVertex000 );
197   TopoDS_Shape V001 = getShape( theVertex001 );
198
199   if (exp.More() &&
200       !V000.IsNull() && V000.ShapeType() == TopAbs_VERTEX &&
201       !V001.IsNull() && V001.ShapeType() == TopAbs_VERTEX 
202       &&
203       myPattern.Apply(TopoDS::Shell( exp.Current() ),
204                       TopoDS::Vertex( V000 ),
205                       TopoDS::Vertex( V001 )) &&
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   return points._retn();
217 }
218
219 //=======================================================================
220 //function : MakeMesh
221 //purpose  : 
222 //=======================================================================
223
224 CORBA::Boolean SMESH_Pattern_i::MakeMesh(SMESH::SMESH_Mesh_ptr theMesh)
225 {
226   ::SMESH_Mesh* aMesh = getMesh( theMesh );
227   if ( !aMesh )
228     return false;
229
230   return myPattern.MakeMesh( aMesh );
231 }
232
233 //=======================================================================
234 //function : GetString
235 //purpose  : 
236 //=======================================================================
237
238 char* SMESH_Pattern_i::GetString()
239 {
240   ostringstream os;
241   myPattern.Save( os );
242   
243   return CORBA::string_dup( os.str().c_str() );
244 }
245
246 //=======================================================================
247 //function : Is2D
248 //purpose  : 
249 //=======================================================================
250
251 CORBA::Boolean SMESH_Pattern_i::Is2D()
252 {
253   return myPattern.Is2D();
254 }
255
256 //=======================================================================
257 //function : GetPoints
258 //purpose  : 
259 //=======================================================================
260
261 SMESH::point_array* SMESH_Pattern_i::GetPoints()
262 {
263   SMESH::point_array_var points = new SMESH::point_array;
264   list<const gp_XYZ *> xyzList;
265
266   if (myPattern.GetPoints( xyzList ))
267   {
268     points->length( xyzList.size() );
269     list<const gp_XYZ *>::iterator xyzIt = xyzList.begin();
270     for ( int i = 0; xyzIt != xyzList.end(); xyzIt++ ) {
271       SMESH::PointStruct & p = points[ i++ ];
272       (*xyzIt)->Coord( p.x, p.y, p.z );
273     }
274   }
275
276   return points._retn();
277 }
278
279 //=======================================================================
280 //function : GetKeyPoints
281 //purpose  : 
282 //=======================================================================
283
284 SMESH::long_array* SMESH_Pattern_i::GetKeyPoints()
285 {
286   SMESH::long_array_var ids = new SMESH::long_array;
287   if ( myPattern.IsLoaded() ) {
288     const list< int > & idList = myPattern.GetKeyPointIDs();
289     ids->length( idList.size() );
290     list< int >::const_iterator iIt = idList.begin();
291     for ( int i = 0; iIt != idList.end(); iIt++, i++ )
292       ids[ i ] = *iIt;
293   }
294   return ids._retn();
295 }
296
297 //=======================================================================
298 //function : GetElementPoints
299 //purpose  : 
300 //=======================================================================
301
302 SMESH::array_of_long_array* SMESH_Pattern_i::GetElementPoints()
303 {
304   SMESH::array_of_long_array_var arrayOfArray = new SMESH::array_of_long_array;
305
306   const list< list< int > >& listOfIdList = myPattern.GetElementPointIDs();
307   arrayOfArray->length( listOfIdList.size() );
308   list< list< int > >::const_iterator llIt = listOfIdList.begin();
309   for ( int i = 0 ; llIt != listOfIdList.end(); llIt++, i++ )
310   {
311     const list< int > & idList = (*llIt);
312     SMESH::long_array& ids = arrayOfArray[ i ];
313     ids.length( idList.size() );
314     list< int >::const_iterator iIt = idList.begin();
315     for ( int j = 0; iIt != idList.end(); iIt++, j++ )
316       ids[ j ] = *iIt;
317   }
318   return arrayOfArray._retn();
319 }
320
321 //=======================================================================
322 //function : GetErrorCode
323 //purpose  : 
324 //=======================================================================
325
326 #define RETCASE(enm) case ::SMESH_Pattern::enm: return SMESH::SMESH_Pattern::enm;
327
328 SMESH::SMESH_Pattern::ErrorCode SMESH_Pattern_i::GetErrorCode()
329 {
330   switch ( myPattern.GetErrorCode() ) {
331     RETCASE( ERR_OK );
332     RETCASE( ERR_READ_NB_POINTS );
333     RETCASE( ERR_READ_POINT_COORDS );
334     RETCASE( ERR_READ_TOO_FEW_POINTS );
335     RETCASE( ERR_READ_3D_COORD );
336     RETCASE( ERR_READ_NO_KEYPOINT );
337     RETCASE( ERR_READ_BAD_INDEX );
338     RETCASE( ERR_READ_ELEM_POINTS );
339     RETCASE( ERR_READ_NO_ELEMS );
340     RETCASE( ERR_READ_BAD_KEY_POINT );
341     RETCASE( ERR_SAVE_NOT_LOADED );
342     RETCASE( ERR_LOAD_EMPTY_SUBMESH );
343     RETCASE( ERR_LOADF_NARROW_FACE );
344     RETCASE( ERR_LOADF_CLOSED_FACE );
345     RETCASE( ERR_LOADV_BAD_SHAPE );
346     RETCASE( ERR_LOADV_COMPUTE_PARAMS );
347     RETCASE( ERR_APPL_NOT_LOADED );
348     RETCASE( ERR_APPL_BAD_DIMENTION );
349     RETCASE( ERR_APPL_BAD_NB_VERTICES );
350     RETCASE( ERR_APPLF_BAD_TOPOLOGY );
351     RETCASE( ERR_APPLF_BAD_VERTEX );
352     RETCASE( ERR_APPLF_INTERNAL_EEROR );
353     RETCASE( ERR_APPLV_BAD_SHAPE );
354     RETCASE( ERR_MAKEM_NOT_COMPUTED );
355   default:;
356   };
357   return SMESH::SMESH_Pattern::ERR_OK;
358 }
359