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.salome-platform.org/ or email : webmaster.salome@opencascade.com
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 "SMESH_PythonDump.hxx"
36 #include "SMDS_MeshFace.hxx"
37 #include "SMDS_MeshVolume.hxx"
39 #include <TopExp_Explorer.hxx>
41 #include <TopoDS_Face.hxx>
46 using SMESH::TPythonDump;
48 //=======================================================================
49 //function : dumpErrorCode
51 //=======================================================================
53 static void addErrorCode(const char* thePyCommand)
55 TPythonDump() << "if (isDone != 1):";
56 TPythonDump() << "\tprint '" << thePyCommand << " :', pattern.GetErrorCode()";
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 TPythonDump() << "pattern = " << this << ".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 // remove some gabage from the end
110 TCollection_AsciiString patternDescription = (char*) theFileContents;
111 int pos = patternDescription.Length();
112 char test = patternDescription.Value( pos );
113 while (! isdigit( (unsigned char)patternDescription.Value( pos )))
115 if ( pos != patternDescription.Length() ) {
116 patternDescription.Trunc( pos );
119 // Update Python script
120 TPythonDump() << "isDone = pattern.LoadFromFile("
121 << TPythonDump::LongStringStart("Pattern")
122 << patternDescription
123 << TPythonDump::LongStringEnd()
125 addErrorCode( "LoadFromFile" );
127 return myPattern.Load( theFileContents );
130 //=======================================================================
131 //function : LoadFromFace
133 //=======================================================================
135 CORBA::Boolean SMESH_Pattern_i::LoadFromFace(SMESH::SMESH_Mesh_ptr theMesh,
136 GEOM::GEOM_Object_ptr theFace,
137 CORBA::Boolean theProject)
139 if ( theMesh->_is_nil() || theFace->_is_nil() )
142 ::SMESH_Mesh* aMesh = getMesh( theMesh );
146 TopoDS_Shape aFace = myGen->GeomObjectToShape( theFace );
147 if ( aFace.IsNull() || aFace.ShapeType() != TopAbs_FACE )
150 // Update Python script
151 TPythonDump() << "isDone = pattern.LoadFromFace( " << theMesh << ", "
152 << theFace << ", " << theProject << " )";
153 addErrorCode( "LoadFromFace" );
155 return myPattern.Load( aMesh, TopoDS::Face( aFace ), theProject );
158 //=======================================================================
159 //function : LoadFrom3DBlock
161 //=======================================================================
163 CORBA::Boolean SMESH_Pattern_i::LoadFrom3DBlock(SMESH::SMESH_Mesh_ptr theMesh,
164 GEOM::GEOM_Object_ptr theBlock)
166 if ( theMesh->_is_nil() || theBlock->_is_nil() )
169 ::SMESH_Mesh* aMesh = getMesh( theMesh );
173 TopoDS_Shape aShape = myGen->GeomObjectToShape( theBlock );
174 if ( aShape.IsNull())
177 TopExp_Explorer exp ( aShape, TopAbs_SHELL );
181 // Update Python script
182 TPythonDump() << "isDone = pattern.LoadFrom3DBlock( " << theMesh << ", " << theBlock << " )";
183 addErrorCode( "LoadFrom3DBlock" );
185 return myPattern.Load( aMesh, TopoDS::Shell( exp.Current() ));
188 //=======================================================================
189 //function : ApplyToFace
191 //=======================================================================
193 SMESH::point_array* SMESH_Pattern_i::ApplyToFace(GEOM::GEOM_Object_ptr theFace,
194 GEOM::GEOM_Object_ptr theVertexOnKeyPoint1,
195 CORBA::Boolean theReverse)
197 SMESH::point_array_var points = new SMESH::point_array;
198 list<const gp_XYZ *> xyzList;
200 TopoDS_Shape F = myGen->GeomObjectToShape( theFace );
201 TopoDS_Shape V = myGen->GeomObjectToShape( theVertexOnKeyPoint1 );
203 if (!F.IsNull() && F.ShapeType() == TopAbs_FACE &&
204 !V.IsNull() && V.ShapeType() == TopAbs_VERTEX
206 myPattern.Apply( TopoDS::Face( F ), TopoDS::Vertex( V ), theReverse ) &&
207 myPattern.GetMappedPoints( xyzList ))
209 points->length( xyzList.size() );
210 list<const gp_XYZ *>::iterator xyzIt = xyzList.begin();
211 for ( int i = 0; xyzIt != xyzList.end(); xyzIt++ ) {
212 SMESH::PointStruct & p = points[ i++ ];
213 (*xyzIt)->Coord( p.x, p.y, p.z );
217 // Update Python script
218 TPythonDump() << "pattern.ApplyToFace( " << theFace << ", "
219 << theVertexOnKeyPoint1 << ", " << theReverse << " )";
221 return points._retn();
224 //=======================================================================
225 //function : ApplyTo3DBlock
227 //=======================================================================
229 SMESH::point_array* SMESH_Pattern_i::ApplyTo3DBlock(GEOM::GEOM_Object_ptr theBlock,
230 GEOM::GEOM_Object_ptr theVertex000,
231 GEOM::GEOM_Object_ptr theVertex001)
233 SMESH::point_array_var points = new SMESH::point_array;
234 list<const gp_XYZ *> xyzList;
236 TopExp_Explorer exp( myGen->GeomObjectToShape( theBlock ), TopAbs_SHELL );
237 TopoDS_Shape V000 = myGen->GeomObjectToShape( theVertex000 );
238 TopoDS_Shape V001 = myGen->GeomObjectToShape( theVertex001 );
241 !V000.IsNull() && V000.ShapeType() == TopAbs_VERTEX &&
242 !V001.IsNull() && V001.ShapeType() == TopAbs_VERTEX
244 myPattern.Apply(TopoDS::Shell( exp.Current() ),
245 TopoDS::Vertex( V000 ),
246 TopoDS::Vertex( V001 )) &&
247 myPattern.GetMappedPoints( xyzList ))
249 points->length( xyzList.size() );
250 list<const gp_XYZ *>::iterator xyzIt = xyzList.begin();
251 for ( int i = 0; xyzIt != xyzList.end(); xyzIt++ ) {
252 SMESH::PointStruct & p = points[ i++ ];
253 (*xyzIt)->Coord( p.x, p.y, p.z );
257 // Update Python script
258 TPythonDump() << "pattern.ApplyTo3DBlock( " << theBlock << ", "
259 << theVertex000 << ", " << theVertex001 << " )";
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 TPythonDump() << "pattern.ApplyToMeshFaces( " << theMesh << ", "
303 << theFacesIDs << ", "
304 << theNodeIndexOnKeyPoint1 << ", " << theReverse << " )";
306 return points._retn();
309 //=======================================================================
310 //function : ApplyToHexahedrons
312 //=======================================================================
315 SMESH_Pattern_i::ApplyToHexahedrons(SMESH::SMESH_Mesh_ptr theMesh,
316 const SMESH::long_array& theVolumesIDs,
317 CORBA::Long theNode000Index,
318 CORBA::Long theNode001Index)
320 SMESH::point_array_var points = new SMESH::point_array;
322 ::SMESH_Mesh* aMesh = getMesh( theMesh );
324 return points._retn();
326 list<const gp_XYZ *> xyzList;
327 set<const SMDS_MeshVolume*> vset;
328 for (int i = 0; i < theVolumesIDs.length(); i++)
330 CORBA::Long index = theVolumesIDs[i];
331 const SMDS_MeshElement * elem = aMesh->GetMeshDS()->FindElement(index);
332 if ( elem && elem->GetType() == SMDSAbs_Volume && elem->NbNodes() == 8 )
333 vset.insert( static_cast<const SMDS_MeshVolume *>( elem ));
335 if (myPattern.Apply( vset, theNode000Index, theNode001Index ) &&
336 myPattern.GetMappedPoints( xyzList ))
338 points->length( xyzList.size() );
339 list<const gp_XYZ *>::iterator xyzIt = xyzList.begin();
340 for ( int i = 0; xyzIt != xyzList.end(); xyzIt++ ) {
341 SMESH::PointStruct & p = points[ i++ ];
342 (*xyzIt)->Coord( p.x, p.y, p.z );
346 // Update Python script
347 TPythonDump() << "pattern.ApplyToHexahedrons( " << theMesh << ", "
348 << theVolumesIDs << ", "
349 << theNode000Index << ", " << theNode001Index << " )";
351 return points._retn();
354 //=======================================================================
355 //function : MakeMesh
357 //=======================================================================
359 CORBA::Boolean SMESH_Pattern_i::MakeMesh (SMESH::SMESH_Mesh_ptr theMesh,
360 const CORBA::Boolean CreatePolygons,
361 const CORBA::Boolean CreatePolyedrs)
363 ::SMESH_Mesh* aMesh = getMesh( theMesh );
367 // Update Python script
368 TPythonDump() << "isDone = pattern.MakeMesh( " << theMesh << ", "
369 << CreatePolygons << ", " << CreatePolyedrs << " )";
370 addErrorCode( "MakeMesh" );
372 return myPattern.MakeMesh( aMesh, CreatePolygons, CreatePolyedrs );
375 //=======================================================================
376 //function : GetString
378 //=======================================================================
380 char* SMESH_Pattern_i::GetString()
383 myPattern.Save( os );
385 return CORBA::string_dup( os.str().c_str() );
388 //=======================================================================
391 //=======================================================================
393 CORBA::Boolean SMESH_Pattern_i::Is2D()
395 return myPattern.Is2D();
398 //=======================================================================
399 //function : GetPoints
401 //=======================================================================
403 SMESH::point_array* SMESH_Pattern_i::GetPoints()
405 SMESH::point_array_var points = new SMESH::point_array;
406 list<const gp_XYZ *> xyzList;
408 if (myPattern.GetPoints( xyzList ))
410 points->length( xyzList.size() );
411 list<const gp_XYZ *>::iterator xyzIt = xyzList.begin();
412 for ( int i = 0; xyzIt != xyzList.end(); xyzIt++ ) {
413 SMESH::PointStruct & p = points[ i++ ];
414 (*xyzIt)->Coord( p.x, p.y, p.z );
418 return points._retn();
421 //=======================================================================
422 //function : GetKeyPoints
424 //=======================================================================
426 SMESH::long_array* SMESH_Pattern_i::GetKeyPoints()
428 SMESH::long_array_var ids = new SMESH::long_array;
429 if ( myPattern.IsLoaded() ) {
430 const list< int > & idList = myPattern.GetKeyPointIDs();
431 ids->length( idList.size() );
432 list< int >::const_iterator iIt = idList.begin();
433 for ( int i = 0; iIt != idList.end(); iIt++, i++ )
439 //=======================================================================
440 //function : GetElementPoints
442 //=======================================================================
444 SMESH::array_of_long_array* SMESH_Pattern_i::GetElementPoints(CORBA::Boolean applied)
446 SMESH::array_of_long_array_var arrayOfArray = new SMESH::array_of_long_array;
448 const list< list< int > >& listOfIdList = myPattern.GetElementPointIDs(applied);
449 arrayOfArray->length( listOfIdList.size() );
450 list< list< int > >::const_iterator llIt = listOfIdList.begin();
451 for ( int i = 0 ; llIt != listOfIdList.end(); llIt++, i++ )
453 const list< int > & idList = (*llIt);
454 SMESH::long_array& ids = arrayOfArray[ i ];
455 ids.length( idList.size() );
456 list< int >::const_iterator iIt = idList.begin();
457 for ( int j = 0; iIt != idList.end(); iIt++, j++ )
460 return arrayOfArray._retn();
463 //=======================================================================
464 //function : GetErrorCode
466 //=======================================================================
468 #define RETCASE(enm) case ::SMESH_Pattern::enm: return SMESH::SMESH_Pattern::enm;
470 SMESH::SMESH_Pattern::ErrorCode SMESH_Pattern_i::GetErrorCode()
472 switch ( myPattern.GetErrorCode() ) {
474 RETCASE( ERR_READ_NB_POINTS );
475 RETCASE( ERR_READ_POINT_COORDS );
476 RETCASE( ERR_READ_TOO_FEW_POINTS );
477 RETCASE( ERR_READ_3D_COORD );
478 RETCASE( ERR_READ_NO_KEYPOINT );
479 RETCASE( ERR_READ_BAD_INDEX );
480 RETCASE( ERR_READ_ELEM_POINTS );
481 RETCASE( ERR_READ_NO_ELEMS );
482 RETCASE( ERR_READ_BAD_KEY_POINT );
483 RETCASE( ERR_SAVE_NOT_LOADED );
484 RETCASE( ERR_LOAD_EMPTY_SUBMESH );
485 RETCASE( ERR_LOADF_NARROW_FACE );
486 RETCASE( ERR_LOADF_CLOSED_FACE );
487 RETCASE( ERR_LOADF_CANT_PROJECT );
488 RETCASE( ERR_LOADV_BAD_SHAPE );
489 RETCASE( ERR_LOADV_COMPUTE_PARAMS );
490 RETCASE( ERR_APPL_NOT_LOADED );
491 RETCASE( ERR_APPL_BAD_DIMENTION );
492 RETCASE( ERR_APPL_BAD_NB_VERTICES );
493 RETCASE( ERR_APPLF_BAD_TOPOLOGY );
494 RETCASE( ERR_APPLF_BAD_VERTEX );
495 RETCASE( ERR_APPLF_INTERNAL_EEROR );
496 RETCASE( ERR_APPLV_BAD_SHAPE );
497 RETCASE( ERR_MAKEM_NOT_COMPUTED );
500 return SMESH::SMESH_Pattern::ERR_OK;