1 // SMESH SMESH_I : idl implementation based on 'SMESH' unit's calsses
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : SMESH_Pattern_i.cxx
25 // Created : Fri Aug 20 16:15:49 2004
26 // Author : Edward AGAPOV (eap)
29 #include "SMESH_Pattern_i.hxx"
31 #include "GEOM_Client.hxx"
32 #include "SMESH_Gen_i.hxx"
33 #include "SMESH_Mesh.hxx"
34 #include "SMESH_Mesh_i.hxx"
35 #include "SMDS_MeshFace.hxx"
36 #include "SMDS_MeshVolume.hxx"
38 #include <TopExp_Explorer.hxx>
40 #include <TopoDS_Face.hxx>
45 //=======================================================================
46 //function : dumpErrorCode
48 //=======================================================================
50 static void addErrorCode(const char* thePyCommand)
52 SMESH_Gen_i::AddToCurrentPyScript("if (isDone != 1):");
53 TCollection_AsciiString str ("\tprint \"");
54 str += (char*) thePyCommand;
55 str += ":\", pattern.GetErrorCode()";
56 SMESH_Gen_i::AddToCurrentPyScript( str );
59 //=============================================================================
61 * SMESH_Gen_i::GetPattern
63 * Create pattern mapper
65 //=============================================================================
67 SMESH::SMESH_Pattern_ptr SMESH_Gen_i::GetPattern()
69 // Update Python script
70 SMESH_Gen_i::AddToCurrentPyScript( "pattern = smesh.GetPattern()" );
72 SMESH_Pattern_i* i = new SMESH_Pattern_i( this );
73 SMESH::SMESH_Pattern_var anObj = i->_this();
77 //=======================================================================
78 //function : SMESH_Pattern_i
80 //=======================================================================
82 SMESH_Pattern_i::SMESH_Pattern_i( SMESH_Gen_i* theGen_i ):
87 //=======================================================================
90 //=======================================================================
92 ::SMESH_Mesh* SMESH_Pattern_i::getMesh( SMESH::SMESH_Mesh_ptr & theMesh )
94 SMESH_Mesh_i* anImplPtr =
95 dynamic_cast<SMESH_Mesh_i*>( SMESH_Gen_i::GetServant( theMesh ).in() );
97 return & anImplPtr->GetImpl();
102 //=======================================================================
103 //function : LoadFromFile
105 //=======================================================================
107 CORBA::Boolean SMESH_Pattern_i::LoadFromFile(const char* theFileContents)
109 // Update Python script
110 TCollection_AsciiString str( "isDone = pattern.LoadFromFile(" );
111 str += TCollection_AsciiString( (char*) theFileContents ) + ")";
112 SMESH_Gen_i::AddToCurrentPyScript( str );
113 addErrorCode( "LoadFromFile" );
115 return myPattern.Load( theFileContents );
118 //=======================================================================
119 //function : LoadFromFace
121 //=======================================================================
123 CORBA::Boolean SMESH_Pattern_i::LoadFromFace(SMESH::SMESH_Mesh_ptr theMesh,
124 GEOM::GEOM_Object_ptr theFace,
125 CORBA::Boolean theProject)
127 if ( theMesh->_is_nil() || theFace->_is_nil() )
130 ::SMESH_Mesh* aMesh = getMesh( theMesh );
134 TopoDS_Face aFace = TopoDS::Face( myGen->GeomObjectToShape( theFace ));
135 if ( aFace.IsNull() )
138 // Update Python script
139 TCollection_AsciiString str( "isDone = pattern.LoadFromFace( " );
140 SMESH_Gen_i::AddObject( str, theMesh ) += ", ";
141 SMESH_Gen_i::AddObject( str, theFace ) += ", ";
142 str += TCollection_AsciiString( theProject ) + " )";
143 SMESH_Gen_i::AddToCurrentPyScript( str );
144 addErrorCode( "LoadFromFace" );
146 return myPattern.Load( aMesh, aFace, theProject );
149 //=======================================================================
150 //function : LoadFrom3DBlock
152 //=======================================================================
154 CORBA::Boolean SMESH_Pattern_i::LoadFrom3DBlock(SMESH::SMESH_Mesh_ptr theMesh,
155 GEOM::GEOM_Object_ptr theBlock)
157 if ( theMesh->_is_nil() || theBlock->_is_nil() )
160 ::SMESH_Mesh* aMesh = getMesh( theMesh );
164 TopoDS_Shape aShape = myGen->GeomObjectToShape( theBlock );
165 if ( aShape.IsNull())
168 TopExp_Explorer exp ( aShape, TopAbs_SHELL );
172 // Update Python script
173 TCollection_AsciiString str( "isDone = pattern.LoadFrom3DBlock( " );
174 SMESH_Gen_i::AddObject( str, theMesh ) += ", ";
175 SMESH_Gen_i::AddObject( str, theBlock ) += " )";
176 SMESH_Gen_i::AddToCurrentPyScript( str );
177 addErrorCode( "LoadFrom3DBlock" );
179 return myPattern.Load( aMesh, TopoDS::Shell( exp.Current() ));
182 //=======================================================================
183 //function : ApplyToFace
185 //=======================================================================
187 SMESH::point_array* SMESH_Pattern_i::ApplyToFace(GEOM::GEOM_Object_ptr theFace,
188 GEOM::GEOM_Object_ptr theVertexOnKeyPoint1,
189 CORBA::Boolean theReverse)
191 SMESH::point_array_var points = new SMESH::point_array;
192 list<const gp_XYZ *> xyzList;
194 TopoDS_Shape F = myGen->GeomObjectToShape( theFace );
195 TopoDS_Shape V = myGen->GeomObjectToShape( theVertexOnKeyPoint1 );
197 if (!F.IsNull() && F.ShapeType() == TopAbs_FACE &&
198 !V.IsNull() && V.ShapeType() == TopAbs_VERTEX
200 myPattern.Apply( TopoDS::Face( F ), TopoDS::Vertex( V ), theReverse ) &&
201 myPattern.GetMappedPoints( xyzList ))
203 points->length( xyzList.size() );
204 list<const gp_XYZ *>::iterator xyzIt = xyzList.begin();
205 for ( int i = 0; xyzIt != xyzList.end(); xyzIt++ ) {
206 SMESH::PointStruct & p = points[ i++ ];
207 (*xyzIt)->Coord( p.x, p.y, p.z );
211 // Update Python script
212 TCollection_AsciiString str( "pattern.ApplyToFace( " );
213 SMESH_Gen_i::AddObject( str, theFace ) += ", ";
214 SMESH_Gen_i::AddObject( str, theVertexOnKeyPoint1 ) += ", ";
215 str += TCollection_AsciiString( theReverse ) + " )";
216 SMESH_Gen_i::AddToCurrentPyScript( str );
218 return points._retn();
221 //=======================================================================
222 //function : ApplyTo3DBlock
224 //=======================================================================
226 SMESH::point_array* SMESH_Pattern_i::ApplyTo3DBlock(GEOM::GEOM_Object_ptr theBlock,
227 GEOM::GEOM_Object_ptr theVertex000,
228 GEOM::GEOM_Object_ptr theVertex001)
230 SMESH::point_array_var points = new SMESH::point_array;
231 list<const gp_XYZ *> xyzList;
233 TopExp_Explorer exp( myGen->GeomObjectToShape( theBlock ), TopAbs_SHELL );
234 TopoDS_Shape V000 = myGen->GeomObjectToShape( theVertex000 );
235 TopoDS_Shape V001 = myGen->GeomObjectToShape( theVertex001 );
238 !V000.IsNull() && V000.ShapeType() == TopAbs_VERTEX &&
239 !V001.IsNull() && V001.ShapeType() == TopAbs_VERTEX
241 myPattern.Apply(TopoDS::Shell( exp.Current() ),
242 TopoDS::Vertex( V000 ),
243 TopoDS::Vertex( V001 )) &&
244 myPattern.GetMappedPoints( xyzList ))
246 points->length( xyzList.size() );
247 list<const gp_XYZ *>::iterator xyzIt = xyzList.begin();
248 for ( int i = 0; xyzIt != xyzList.end(); xyzIt++ ) {
249 SMESH::PointStruct & p = points[ i++ ];
250 (*xyzIt)->Coord( p.x, p.y, p.z );
254 // Update Python script
255 TCollection_AsciiString str( "pattern.ApplyTo3DBlock( " );
256 SMESH_Gen_i::AddObject( str, theBlock ) += ", ";
257 SMESH_Gen_i::AddObject( str, theVertex000 ) += ", ";
258 SMESH_Gen_i::AddObject( str, theVertex001 ) += " )";
259 SMESH_Gen_i::AddToCurrentPyScript( str );
261 return points._retn();
264 //=======================================================================
265 //function : ApplyToMeshFaces
267 //=======================================================================
270 SMESH_Pattern_i::ApplyToMeshFaces(SMESH::SMESH_Mesh_ptr theMesh,
271 const SMESH::long_array& theFacesIDs,
272 CORBA::Long theNodeIndexOnKeyPoint1,
273 CORBA::Boolean theReverse)
275 SMESH::point_array_var points = new SMESH::point_array;
277 ::SMESH_Mesh* aMesh = getMesh( theMesh );
279 return points._retn();
281 list<const gp_XYZ *> xyzList;
282 set<const SMDS_MeshFace*> fset;
283 for (int i = 0; i < theFacesIDs.length(); i++)
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 ));
290 if (myPattern.Apply( fset, theNodeIndexOnKeyPoint1, theReverse ) &&
291 myPattern.GetMappedPoints( xyzList ))
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 );
301 // Update Python script
302 TCollection_AsciiString str( "pattern.ApplyToMeshFaces( " );
303 SMESH_Gen_i::AddObject( str, theMesh ) += ", ";
304 SMESH_Gen_i::AddArray( str, theFacesIDs ) += ", ";
305 str += TCollection_AsciiString( (int)theNodeIndexOnKeyPoint1 ) + ", ";
306 str += TCollection_AsciiString( theReverse ) + " )";
307 SMESH_Gen_i::AddToCurrentPyScript( str );
309 return points._retn();
312 //=======================================================================
313 //function : ApplyToHexahedrons
315 //=======================================================================
318 SMESH_Pattern_i::ApplyToHexahedrons(SMESH::SMESH_Mesh_ptr theMesh,
319 const SMESH::long_array& theVolumesIDs,
320 CORBA::Long theNode000Index,
321 CORBA::Long theNode001Index)
323 SMESH::point_array_var points = new SMESH::point_array;
325 ::SMESH_Mesh* aMesh = getMesh( theMesh );
327 return points._retn();
329 list<const gp_XYZ *> xyzList;
330 set<const SMDS_MeshVolume*> vset;
331 for (int i = 0; i < theVolumesIDs.length(); i++)
333 CORBA::Long index = theVolumesIDs[i];
334 const SMDS_MeshElement * elem = aMesh->GetMeshDS()->FindElement(index);
335 if ( elem && elem->GetType() == SMDSAbs_Volume && elem->NbNodes() == 8 )
336 vset.insert( static_cast<const SMDS_MeshVolume *>( elem ));
338 if (myPattern.Apply( vset, theNode000Index, theNode001Index ) &&
339 myPattern.GetMappedPoints( xyzList ))
341 points->length( xyzList.size() );
342 list<const gp_XYZ *>::iterator xyzIt = xyzList.begin();
343 for ( int i = 0; xyzIt != xyzList.end(); xyzIt++ ) {
344 SMESH::PointStruct & p = points[ i++ ];
345 (*xyzIt)->Coord( p.x, p.y, p.z );
349 // Update Python script
350 TCollection_AsciiString str( "pattern.ApplyToHexahedrons( " );
351 SMESH_Gen_i::AddObject( str, theMesh ) += ", ";
352 SMESH_Gen_i::AddArray( str, theVolumesIDs ) += ", ";
353 str += TCollection_AsciiString( (int)theNode000Index ) + ", ";
354 str += TCollection_AsciiString( (int)theNode001Index ) + " )";
355 SMESH_Gen_i::AddToCurrentPyScript( str );
357 return points._retn();
360 //=======================================================================
361 //function : MakeMesh
363 //=======================================================================
365 CORBA::Boolean SMESH_Pattern_i::MakeMesh (SMESH::SMESH_Mesh_ptr theMesh,
366 const CORBA::Boolean CreatePolygons,
367 const CORBA::Boolean CreatePolyedrs)
369 ::SMESH_Mesh* aMesh = getMesh( theMesh );
373 // Update Python script
374 TCollection_AsciiString str( "isDone = pattern.MakeMesh( " );
375 SMESH_Gen_i::AddObject( str, theMesh ) += ", ";
376 str += TCollection_AsciiString( CreatePolygons ) + ", ";
377 str += TCollection_AsciiString( CreatePolyedrs ) + " )";
378 SMESH_Gen_i::AddToCurrentPyScript( str );
379 addErrorCode( "MakeMesh" );
381 return myPattern.MakeMesh( aMesh, CreatePolygons, CreatePolyedrs );
384 //=======================================================================
385 //function : GetString
387 //=======================================================================
389 char* SMESH_Pattern_i::GetString()
392 myPattern.Save( os );
394 return CORBA::string_dup( os.str().c_str() );
397 //=======================================================================
400 //=======================================================================
402 CORBA::Boolean SMESH_Pattern_i::Is2D()
404 return myPattern.Is2D();
407 //=======================================================================
408 //function : GetPoints
410 //=======================================================================
412 SMESH::point_array* SMESH_Pattern_i::GetPoints()
414 SMESH::point_array_var points = new SMESH::point_array;
415 list<const gp_XYZ *> xyzList;
417 if (myPattern.GetPoints( xyzList ))
419 points->length( xyzList.size() );
420 list<const gp_XYZ *>::iterator xyzIt = xyzList.begin();
421 for ( int i = 0; xyzIt != xyzList.end(); xyzIt++ ) {
422 SMESH::PointStruct & p = points[ i++ ];
423 (*xyzIt)->Coord( p.x, p.y, p.z );
427 return points._retn();
430 //=======================================================================
431 //function : GetKeyPoints
433 //=======================================================================
435 SMESH::long_array* SMESH_Pattern_i::GetKeyPoints()
437 SMESH::long_array_var ids = new SMESH::long_array;
438 if ( myPattern.IsLoaded() ) {
439 const list< int > & idList = myPattern.GetKeyPointIDs();
440 ids->length( idList.size() );
441 list< int >::const_iterator iIt = idList.begin();
442 for ( int i = 0; iIt != idList.end(); iIt++, i++ )
448 //=======================================================================
449 //function : GetElementPoints
451 //=======================================================================
453 SMESH::array_of_long_array* SMESH_Pattern_i::GetElementPoints(CORBA::Boolean applied)
455 SMESH::array_of_long_array_var arrayOfArray = new SMESH::array_of_long_array;
457 const list< list< int > >& listOfIdList = myPattern.GetElementPointIDs(applied);
458 arrayOfArray->length( listOfIdList.size() );
459 list< list< int > >::const_iterator llIt = listOfIdList.begin();
460 for ( int i = 0 ; llIt != listOfIdList.end(); llIt++, i++ )
462 const list< int > & idList = (*llIt);
463 SMESH::long_array& ids = arrayOfArray[ i ];
464 ids.length( idList.size() );
465 list< int >::const_iterator iIt = idList.begin();
466 for ( int j = 0; iIt != idList.end(); iIt++, j++ )
469 return arrayOfArray._retn();
472 //=======================================================================
473 //function : GetErrorCode
475 //=======================================================================
477 #define RETCASE(enm) case ::SMESH_Pattern::enm: return SMESH::SMESH_Pattern::enm;
479 SMESH::SMESH_Pattern::ErrorCode SMESH_Pattern_i::GetErrorCode()
481 switch ( myPattern.GetErrorCode() ) {
483 RETCASE( ERR_READ_NB_POINTS );
484 RETCASE( ERR_READ_POINT_COORDS );
485 RETCASE( ERR_READ_TOO_FEW_POINTS );
486 RETCASE( ERR_READ_3D_COORD );
487 RETCASE( ERR_READ_NO_KEYPOINT );
488 RETCASE( ERR_READ_BAD_INDEX );
489 RETCASE( ERR_READ_ELEM_POINTS );
490 RETCASE( ERR_READ_NO_ELEMS );
491 RETCASE( ERR_READ_BAD_KEY_POINT );
492 RETCASE( ERR_SAVE_NOT_LOADED );
493 RETCASE( ERR_LOAD_EMPTY_SUBMESH );
494 RETCASE( ERR_LOADF_NARROW_FACE );
495 RETCASE( ERR_LOADF_CLOSED_FACE );
496 RETCASE( ERR_LOADV_BAD_SHAPE );
497 RETCASE( ERR_LOADV_COMPUTE_PARAMS );
498 RETCASE( ERR_APPL_NOT_LOADED );
499 RETCASE( ERR_APPL_BAD_DIMENTION );
500 RETCASE( ERR_APPL_BAD_NB_VERTICES );
501 RETCASE( ERR_APPLF_BAD_TOPOLOGY );
502 RETCASE( ERR_APPLF_BAD_VERTEX );
503 RETCASE( ERR_APPLF_INTERNAL_EEROR );
504 RETCASE( ERR_APPLV_BAD_SHAPE );
505 RETCASE( ERR_MAKEM_NOT_COMPUTED );
508 return SMESH::SMESH_Pattern::ERR_OK;