1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 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.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // SMESH SMESH_I : idl implementation based on 'SMESH' unit's calsses
23 // File : SMESH_Pattern_i.cxx
24 // Created : Fri Aug 20 16:15:49 2004
25 // Author : Edward AGAPOV (eap)
28 #include "SMESH_Pattern_i.hxx"
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"
38 #include <TopExp_Explorer.hxx>
40 #include <TopoDS_Face.hxx>
42 #include <Standard_Failure.hxx>
43 #include <Standard_ErrorHandler.hxx>
48 using SMESH::TPythonDump;
50 //=======================================================================
51 //function : dumpErrorCode
53 //=======================================================================
55 static void addErrorCode(const char* thePyCommand)
57 TPythonDump() << "if (isDone != 1):";
58 TPythonDump() << "\tprint '" << thePyCommand << " :', pattern.GetErrorCode()";
61 //=============================================================================
63 * SMESH_Gen_i::GetPattern
65 * Create pattern mapper
67 //=============================================================================
69 SMESH::SMESH_Pattern_ptr SMESH_Gen_i::GetPattern()
71 // Update Python script
72 TPythonDump() << "pattern = " << this << ".GetPattern()";
74 SMESH_Pattern_i* i = new SMESH_Pattern_i( this );
75 SMESH::SMESH_Pattern_var anObj = i->_this();
79 //=======================================================================
80 //function : SMESH_Pattern_i
82 //=======================================================================
84 SMESH_Pattern_i::SMESH_Pattern_i( SMESH_Gen_i* theGen_i ):
89 //=======================================================================
92 //=======================================================================
94 ::SMESH_Mesh* SMESH_Pattern_i::getMesh( SMESH::SMESH_Mesh_ptr & theMesh )
96 SMESH_Mesh_i* anImplPtr =
97 dynamic_cast<SMESH_Mesh_i*>( SMESH_Gen_i::GetServant( theMesh ).in() );
99 return & anImplPtr->GetImpl();
104 //=======================================================================
105 //function : LoadFromFile
107 //=======================================================================
109 CORBA::Boolean SMESH_Pattern_i::LoadFromFile(const char* theFileContents)
111 // remove some gabage from the end
112 TCollection_AsciiString patternDescription = (char*) theFileContents;
113 int pos = patternDescription.Length();
114 while (! isdigit( patternDescription.Value( pos )))
116 if ( pos != patternDescription.Length() ) {
117 patternDescription.Trunc( pos );
120 // Update Python script
121 TPythonDump() << "isDone = pattern.LoadFromFile("
122 << TPythonDump::LongStringStart("Pattern")
123 << patternDescription
124 << TPythonDump::LongStringEnd()
126 addErrorCode( "LoadFromFile" );
128 return myPattern.Load( theFileContents );
131 //=======================================================================
132 //function : LoadFromFace
134 //=======================================================================
136 CORBA::Boolean SMESH_Pattern_i::LoadFromFace(SMESH::SMESH_Mesh_ptr theMesh,
137 GEOM::GEOM_Object_ptr theFace,
138 CORBA::Boolean theProject)
140 if ( theMesh->_is_nil() || theFace->_is_nil() )
143 ::SMESH_Mesh* aMesh = getMesh( theMesh );
147 TopoDS_Shape aFace = myGen->GeomObjectToShape( theFace );
148 if ( aFace.IsNull() || aFace.ShapeType() != TopAbs_FACE )
151 // Update Python script
152 TPythonDump() << "isDone = pattern.LoadFromFace( " << theMesh << ".GetMesh(), "
153 << theFace << ", " << theProject << " )";
154 addErrorCode( "LoadFromFace" );
156 return myPattern.Load( aMesh, TopoDS::Face( aFace ), theProject );
159 //=======================================================================
160 //function : LoadFrom3DBlock
162 //=======================================================================
164 CORBA::Boolean SMESH_Pattern_i::LoadFrom3DBlock(SMESH::SMESH_Mesh_ptr theMesh,
165 GEOM::GEOM_Object_ptr theBlock)
167 if ( theMesh->_is_nil() || theBlock->_is_nil() )
170 ::SMESH_Mesh* aMesh = getMesh( theMesh );
174 TopoDS_Shape aShape = myGen->GeomObjectToShape( theBlock );
175 if ( aShape.IsNull())
178 TopExp_Explorer exp ( aShape, TopAbs_SHELL );
182 // Update Python script
183 TPythonDump() << "isDone = pattern.LoadFrom3DBlock( " << theMesh << ".GetMesh(), " << theBlock << " )";
184 addErrorCode( "LoadFrom3DBlock" );
186 return myPattern.Load( aMesh, TopoDS::Shell( exp.Current() ));
189 //=======================================================================
190 //function : ApplyToFace
192 //=======================================================================
194 SMESH::point_array* SMESH_Pattern_i::ApplyToFace(GEOM::GEOM_Object_ptr theFace,
195 GEOM::GEOM_Object_ptr theVertexOnKeyPoint1,
196 CORBA::Boolean theReverse)
198 SMESH::point_array_var points = new SMESH::point_array;
199 list<const gp_XYZ *> xyzList;
201 TopoDS_Shape F = myGen->GeomObjectToShape( theFace );
202 TopoDS_Shape V = myGen->GeomObjectToShape( theVertexOnKeyPoint1 );
204 if (!F.IsNull() && F.ShapeType() == TopAbs_FACE &&
205 !V.IsNull() && V.ShapeType() == TopAbs_VERTEX
207 myPattern.Apply( TopoDS::Face( F ), TopoDS::Vertex( V ), theReverse ) &&
208 myPattern.GetMappedPoints( xyzList ))
210 points->length( xyzList.size() );
211 list<const gp_XYZ *>::iterator xyzIt = xyzList.begin();
212 for ( int i = 0; xyzIt != xyzList.end(); xyzIt++ ) {
213 SMESH::PointStruct & p = points[ i++ ];
214 (*xyzIt)->Coord( p.x, p.y, p.z );
218 // Update Python script
219 TPythonDump() << "pattern.ApplyToFace( " << theFace << ", "
220 << theVertexOnKeyPoint1 << ", " << theReverse << " )";
222 return points._retn();
225 //=======================================================================
226 //function : ApplyTo3DBlock
228 //=======================================================================
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)
234 SMESH::point_array_var points = new SMESH::point_array;
235 list<const gp_XYZ *> xyzList;
237 TopExp_Explorer exp( myGen->GeomObjectToShape( theBlock ), TopAbs_SHELL );
238 TopoDS_Shape V000 = myGen->GeomObjectToShape( theVertex000 );
239 TopoDS_Shape V001 = myGen->GeomObjectToShape( theVertex001 );
242 !V000.IsNull() && V000.ShapeType() == TopAbs_VERTEX &&
243 !V001.IsNull() && V001.ShapeType() == TopAbs_VERTEX
245 myPattern.Apply(TopoDS::Shell( exp.Current() ),
246 TopoDS::Vertex( V000 ),
247 TopoDS::Vertex( V001 )) &&
248 myPattern.GetMappedPoints( xyzList ))
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 );
258 // Update Python script
259 TPythonDump() << "pattern.ApplyTo3DBlock( " << theBlock << ", "
260 << theVertex000 << ", " << theVertex001 << " )";
262 return points._retn();
265 //=======================================================================
266 //function : ApplyToMeshFaces
268 //=======================================================================
271 SMESH_Pattern_i::ApplyToMeshFaces(SMESH::SMESH_Mesh_ptr theMesh,
272 const SMESH::long_array& theFacesIDs,
273 CORBA::Long theNodeIndexOnKeyPoint1,
274 CORBA::Boolean theReverse)
276 SMESH::point_array_var points = new SMESH::point_array;
278 ::SMESH_Mesh* aMesh = getMesh( theMesh );
280 return points._retn();
282 list<const gp_XYZ *> xyzList;
283 set<const SMDS_MeshFace*> fset;
284 for (int i = 0; i < theFacesIDs.length(); i++)
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 ));
293 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
296 ok = myPattern.Apply( aMesh, fset, theNodeIndexOnKeyPoint1, theReverse );
298 catch (Standard_Failure& exc) {
299 MESSAGE("OCCT Exception in SMESH_Pattern: " << exc.GetMessageString());
301 catch ( std::exception& exc ) {
302 MESSAGE("STD Exception in SMESH_Pattern: << exc.what()");
305 MESSAGE("Unknown Exception in SMESH_Pattern");
308 if ( ok && myPattern.GetMappedPoints( xyzList ))
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 );
318 // Update Python script
319 TPythonDump() << "pattern.ApplyToMeshFaces( " << theMesh << ".GetMesh(), "
320 << theFacesIDs << ", "
321 << theNodeIndexOnKeyPoint1 << ", " << theReverse << " )";
323 return points._retn();
326 //=======================================================================
327 //function : ApplyToHexahedrons
329 //=======================================================================
332 SMESH_Pattern_i::ApplyToHexahedrons(SMESH::SMESH_Mesh_ptr theMesh,
333 const SMESH::long_array& theVolumesIDs,
334 CORBA::Long theNode000Index,
335 CORBA::Long theNode001Index)
337 SMESH::point_array_var points = new SMESH::point_array;
339 ::SMESH_Mesh* aMesh = getMesh( theMesh );
341 return points._retn();
343 list<const gp_XYZ *> xyzList;
344 set<const SMDS_MeshVolume*> vset;
345 for (int i = 0; i < theVolumesIDs.length(); i++)
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 ));
352 if (myPattern.Apply( vset, theNode000Index, theNode001Index ) &&
353 myPattern.GetMappedPoints( xyzList ))
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 );
363 // Update Python script
364 TPythonDump() << "pattern.ApplyToHexahedrons( " << theMesh << ".GetMesh(), "
365 << theVolumesIDs << ", "
366 << theNode000Index << ", " << theNode001Index << " )";
368 return points._retn();
371 //=======================================================================
372 //function : MakeMesh
374 //=======================================================================
376 CORBA::Boolean SMESH_Pattern_i::MakeMesh (SMESH::SMESH_Mesh_ptr theMesh,
377 const CORBA::Boolean CreatePolygons,
378 const CORBA::Boolean CreatePolyedrs)
380 ::SMESH_Mesh* aMesh = getMesh( theMesh );
384 // Update Python script
385 TPythonDump() << "isDone = pattern.MakeMesh( " << theMesh << ".GetMesh(), "
386 << CreatePolygons << ", " << CreatePolyedrs << " )";
387 addErrorCode( "MakeMesh" );
389 return myPattern.MakeMesh( aMesh, CreatePolygons, CreatePolyedrs );
392 //=======================================================================
393 //function : GetString
395 //=======================================================================
397 char* SMESH_Pattern_i::GetString()
400 myPattern.Save( os );
402 return CORBA::string_dup( os.str().c_str() );
405 //=======================================================================
408 //=======================================================================
410 CORBA::Boolean SMESH_Pattern_i::Is2D()
412 return myPattern.Is2D();
415 //=======================================================================
416 //function : GetPoints
418 //=======================================================================
420 SMESH::point_array* SMESH_Pattern_i::GetPoints()
422 SMESH::point_array_var points = new SMESH::point_array;
423 list<const gp_XYZ *> xyzList;
425 if (myPattern.GetPoints( xyzList ))
427 points->length( xyzList.size() );
428 list<const gp_XYZ *>::iterator xyzIt = xyzList.begin();
429 for ( int i = 0; xyzIt != xyzList.end(); xyzIt++ ) {
430 SMESH::PointStruct & p = points[ i++ ];
431 (*xyzIt)->Coord( p.x, p.y, p.z );
435 return points._retn();
438 //=======================================================================
439 //function : GetKeyPoints
441 //=======================================================================
443 SMESH::long_array* SMESH_Pattern_i::GetKeyPoints()
445 SMESH::long_array_var ids = new SMESH::long_array;
446 if ( myPattern.IsLoaded() ) {
447 const list< int > & idList = myPattern.GetKeyPointIDs();
448 ids->length( idList.size() );
449 list< int >::const_iterator iIt = idList.begin();
450 for ( int i = 0; iIt != idList.end(); iIt++, i++ )
456 //=======================================================================
457 //function : GetElementPoints
459 //=======================================================================
461 SMESH::array_of_long_array* SMESH_Pattern_i::GetElementPoints(CORBA::Boolean applied)
463 SMESH::array_of_long_array_var arrayOfArray = new SMESH::array_of_long_array;
465 const list< list< int > >& listOfIdList = myPattern.GetElementPointIDs(applied);
466 arrayOfArray->length( listOfIdList.size() );
467 list< list< int > >::const_iterator llIt = listOfIdList.begin();
468 for ( int i = 0 ; llIt != listOfIdList.end(); llIt++, i++ )
470 const list< int > & idList = (*llIt);
471 SMESH::long_array& ids = arrayOfArray[ i ];
472 ids.length( idList.size() );
473 list< int >::const_iterator iIt = idList.begin();
474 for ( int j = 0; iIt != idList.end(); iIt++, j++ )
477 return arrayOfArray._retn();
480 //=======================================================================
481 //function : GetErrorCode
483 //=======================================================================
485 #define RETCASE(enm) case ::SMESH_Pattern::enm: return SMESH::SMESH_Pattern::enm;
487 SMESH::SMESH_Pattern::ErrorCode SMESH_Pattern_i::GetErrorCode()
489 switch ( myPattern.GetErrorCode() ) {
491 RETCASE( ERR_READ_NB_POINTS );
492 RETCASE( ERR_READ_POINT_COORDS );
493 RETCASE( ERR_READ_TOO_FEW_POINTS );
494 RETCASE( ERR_READ_3D_COORD );
495 RETCASE( ERR_READ_NO_KEYPOINT );
496 RETCASE( ERR_READ_BAD_INDEX );
497 RETCASE( ERR_READ_ELEM_POINTS );
498 RETCASE( ERR_READ_NO_ELEMS );
499 RETCASE( ERR_READ_BAD_KEY_POINT );
500 RETCASE( ERR_SAVE_NOT_LOADED );
501 RETCASE( ERR_LOAD_EMPTY_SUBMESH );
502 RETCASE( ERR_LOADF_NARROW_FACE );
503 RETCASE( ERR_LOADF_CLOSED_FACE );
504 RETCASE( ERR_LOADF_CANT_PROJECT );
505 RETCASE( ERR_LOADV_BAD_SHAPE );
506 RETCASE( ERR_LOADV_COMPUTE_PARAMS );
507 RETCASE( ERR_APPL_NOT_LOADED );
508 RETCASE( ERR_APPL_BAD_DIMENTION );
509 RETCASE( ERR_APPL_BAD_NB_VERTICES );
510 RETCASE( ERR_APPLF_BAD_TOPOLOGY );
511 RETCASE( ERR_APPLF_BAD_VERTEX );
512 RETCASE( ERR_APPLF_INTERNAL_EEROR );
513 RETCASE( ERR_APPLV_BAD_SHAPE );
514 RETCASE( ERR_MAKEM_NOT_COMPUTED );
517 return SMESH::SMESH_Pattern::ERR_OK;