2 //=============================================================================
3 // File : SMESH_Mesh.cxx
4 // Created : sam mai 18 08:08:43 CEST 2002
5 // Author : Paul RASCLE, EDF
7 // Copyright : EDF 2002
9 //=============================================================================
12 #include "SMESH_Mesh.hxx"
13 #include "SMESH_subMesh.hxx"
14 #include "SMESH_Gen.hxx"
15 #include "SMESH_Hypothesis.hxx"
16 #include "SMESHDS_Script.hxx"
17 //#include "SMESHDS_ListOfAsciiString.hxx"
18 //#include "SMESHDS_ListIteratorOfListOfAsciiString.hxx"
19 #include "SMESHDS_ListOfPtrHypothesis.hxx"
20 #include "SMESHDS_ListIteratorOfListOfPtrHypothesis.hxx"
21 #include "SMDS_MeshElement.hxx"
22 #include "SMDS_MeshFacesIterator.hxx"
23 #include "SMDS_MeshVolumesIterator.hxx"
24 #include "TCollection_AsciiString.hxx"
26 #include "utilities.h"
28 #include "Mesh_Writer.h"
29 #include "DriverMED_W_SMESHDS_Mesh.h"
30 #include "DriverDAT_W_SMESHDS_Mesh.h"
31 #include "DriverUNV_W_SMESHDS_Mesh.h"
33 //=============================================================================
37 //=============================================================================
39 SMESH_Mesh::SMESH_Mesh()
41 MESSAGE("SMESH_Mesh::SMESH_Mesh");
46 //=============================================================================
50 //=============================================================================
52 SMESH_Mesh::SMESH_Mesh(int localId,
55 const Handle(SMESHDS_Document)& myDocument)
57 MESSAGE("SMESH_Mesh::SMESH_Mesh(int localId)");
61 _myDocument = myDocument;
62 _idDoc = _myDocument->NewMesh();
63 _myMeshDS = _myDocument->GetMesh(_idDoc);
64 _isShapeToMesh = false;
67 //=============================================================================
71 //=============================================================================
73 SMESH_Mesh::~SMESH_Mesh()
75 MESSAGE("SMESH_Mesh::~SMESH_Mesh");
78 //=============================================================================
82 //=============================================================================
84 void SMESH_Mesh::ShapeToMesh(const TopoDS_Shape& aShape)
85 throw (SALOME_Exception)
87 MESSAGE("SMESH_Mesh::ShapeToMesh");
89 throw SALOME_Exception(LOCALIZED("a shape to mesh as already been defined"));
90 _isShapeToMesh = true;
91 _myMeshDS->ShapeToMesh(aShape);
94 TopExp::MapShapes(aShape,_subShapes);
97 //=============================================================================
101 //=============================================================================
103 bool SMESH_Mesh::AddHypothesis(const TopoDS_Shape& aSubShape,
105 throw (SALOME_Exception)
107 MESSAGE("SMESH_Mesh::AddHypothesis");
109 StudyContextStruct* sc = _gen->GetStudyContext(_studyId);
110 if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
112 MESSAGE("Hypothesis ID does not give an hypothesis");
115 throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
118 SMESH_subMesh* subMesh = GetSubMesh(aSubShape);
119 SMESH_Hypothesis* anHyp = sc->mapHypothesis[anHypId];
124 if (anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
125 event = SMESH_subMesh::ADD_HYP;
127 event = SMESH_subMesh::ADD_ALGO;
128 int ret = subMesh->AlgoStateEngine(event, anHyp);
130 // subShapes (only when shape is mainShape)
131 TopoDS_Shape mainShape = _myMeshDS->ShapeToMesh();
132 if (aSubShape.IsSame(mainShape))
134 if (anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
135 event = SMESH_subMesh::ADD_FATHER_HYP;
137 event = SMESH_subMesh::ADD_FATHER_ALGO;
138 subMesh->SubMeshesAlgoStateEngine(event, anHyp);
141 subMesh->DumpAlgoState(true);
146 //=============================================================================
150 //=============================================================================
152 bool SMESH_Mesh::RemoveHypothesis(const TopoDS_Shape& aSubShape,
154 throw (SALOME_Exception)
156 MESSAGE("SMESH_Mesh::RemoveHypothesis");
158 StudyContextStruct* sc = _gen->GetStudyContext(_studyId);
159 if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
160 throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
162 SMESH_subMesh* subMesh = GetSubMesh(aSubShape);
163 SMESH_Hypothesis* anHyp = sc->mapHypothesis[anHypId];
164 int hypType = anHyp->GetType();
170 if (anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
171 event = SMESH_subMesh::REMOVE_HYP;
173 event = SMESH_subMesh::REMOVE_ALGO;
174 int ret = subMesh->AlgoStateEngine(event, anHyp);
176 // subShapes (only when shape is mainShape)
178 TopoDS_Shape mainShape = _myMeshDS->ShapeToMesh();
179 if (aSubShape.IsSame(mainShape))
181 if (anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
182 event = SMESH_subMesh::REMOVE_FATHER_HYP;
184 event = SMESH_subMesh::REMOVE_FATHER_ALGO;
185 subMesh->SubMeshesAlgoStateEngine(event, anHyp);
188 subMesh->DumpAlgoState(true);
193 //=============================================================================
197 //=============================================================================
199 const Handle(SMESHDS_Mesh)& SMESH_Mesh::GetMeshDS()
205 //=============================================================================
209 //=============================================================================
211 const list<SMESHDS_Hypothesis*>&
212 SMESH_Mesh::GetHypothesisList(const TopoDS_Shape& aSubShape)
213 throw (SALOME_Exception)
215 MESSAGE("SMESH_Mesh::GetHypothesisList");
216 _subShapeHypothesisList.clear();
217 const SMESHDS_ListOfPtrHypothesis& listHyp
218 = _myMeshDS->GetHypothesis(aSubShape);
219 SMESHDS_ListIteratorOfListOfPtrHypothesis it(listHyp);
222 SMESHDS_Hypothesis* anHyp = it.Value();
223 _subShapeHypothesisList.push_back(anHyp);
226 return _subShapeHypothesisList;
229 //=============================================================================
233 //=============================================================================
235 const SMESHDS_ListOfCommand& SMESH_Mesh::GetLog()
236 throw (SALOME_Exception)
238 MESSAGE("SMESH_Mesh::GetLog");
239 Handle (SMESHDS_Script) scriptDS = _myMeshDS->GetScript();
240 const SMESHDS_ListOfCommand& logDS = scriptDS->GetCommands();
241 // SMESHDS_ListIteratorOfListOfCommand its;
242 // const SMESHDS_ListOfAsciiString& logDS = scriptDS->GetCommands();
243 // SMESHDS_ListIteratorOfListOfAsciiString its;
244 // for (its.Initialize(logDS); its.More(); its.Next())
246 // SCRUTE(its.Value().ToCString());
251 //=============================================================================
255 //=============================================================================
256 void SMESH_Mesh::ClearLog()
257 throw (SALOME_Exception)
259 MESSAGE("SMESH_Mesh::ClearLog");
260 Handle (SMESHDS_Script) scriptDS = _myMeshDS->GetScript();
264 //=============================================================================
268 //=============================================================================
270 int SMESH_Mesh::GetId()
272 MESSAGE("SMESH_Mesh::GetId");
276 //=============================================================================
280 //=============================================================================
282 SMESH_Gen* SMESH_Mesh::GetGen()
287 //=============================================================================
289 * Get or Create the SMESH_subMesh object implementation
291 //=============================================================================
293 SMESH_subMesh* SMESH_Mesh::GetSubMesh(const TopoDS_Shape & aSubShape)
294 throw (SALOME_Exception)
296 //MESSAGE("SMESH_Mesh::GetSubMesh");
297 SMESH_subMesh* aSubMesh;
298 int index = _subShapes.FindIndex(aSubShape);
299 if ( _mapSubMesh.find(index) != _mapSubMesh.end() ) {
300 aSubMesh = _mapSubMesh[index];
302 aSubMesh = new SMESH_subMesh(index, this, _myMeshDS, aSubShape);
303 _mapSubMesh[index] = aSubMesh;
308 if (_subShapes.Contains(aSubShape))
310 index = _subShapes.FindIndex(aSubShape);
311 ASSERT(_mapSubMesh.find(index) != _mapSubMesh.end());
312 aSubMesh = _mapSubMesh[index];
313 //MESSAGE("found submesh " << index);
317 index = _subShapes.Add(aSubShape);
318 aSubMesh = new SMESH_subMesh(index, this, _myMeshDS, aSubShape);
319 _mapSubMesh[index] = aSubMesh;
320 //MESSAGE("created submesh " << index);
326 //=============================================================================
328 * Get the SMESH_subMesh object implementation. Dont create it, return null
329 * if it does not exist.
331 //=============================================================================
333 // * Given a subShape, find if there is a subMesh associated to this subShape
334 // * or to a collection of shapes containing this subShape. Collection =
335 // * compsolid, shell, wire.
337 // * WARNING : with arg = compsolid, shell or wire returns always NULL.
338 // * with a face inside a shell, and submesh created for both, if arg is face,
339 // * returns first created submesh of the two.
340 // * subMesh is not created, return may be NULL.
342 SMESH_subMesh* SMESH_Mesh::GetSubMeshContaining(const TopoDS_Shape & aSubShape)
343 throw (SALOME_Exception)
345 //MESSAGE("SMESH_Mesh::GetSubMeshContaining");
346 bool isFound = false;
347 SMESH_subMesh* aSubMesh = NULL;
349 int index = _subShapes.FindIndex(aSubShape);
350 if ( _mapSubMesh.find(index) != _mapSubMesh.end() ) {
351 aSubMesh = _mapSubMesh[index];
357 if (_subShapes.Contains(aSubShape))
359 index = _subShapes.FindIndex(aSubShape);
360 ASSERT(_mapSubMesh.find(index) != _mapSubMesh.end());
361 aSubMesh = _mapSubMesh[index];
363 //MESSAGE("found submesh " << index);
367 // map<int, SMESH_subMesh*>::iterator itsm;
368 // for (itsm = _mapSubMesh.begin(); itsm != _mapSubMesh.end(); itsm++)
370 // aSubMesh = (*itsm).second;
371 // isFound = aSubMesh->Contains(aSubShape);
372 // if (isFound) break;
375 if (! isFound) aSubMesh = NULL;
379 //=============================================================================
383 //=============================================================================
385 const list <SMESH_subMesh*>&
386 SMESH_Mesh::GetSubMeshUsingHypothesis(SMESHDS_Hypothesis* anHyp)
387 throw (SALOME_Exception)
389 MESSAGE("SMESH_Mesh::GetSubMeshUsingHypothesis");
390 map<int, SMESH_subMesh*>::iterator itsm;
391 _subMeshesUsingHypothesisList.clear();
392 for (itsm = _mapSubMesh.begin(); itsm != _mapSubMesh.end(); itsm++)
394 SMESH_subMesh* aSubMesh = (*itsm).second;
395 bool usesHyp = false;
396 SMESH_Algo* algo = _gen->GetAlgo(*this, aSubMesh->GetSubShape());
399 const list<SMESHDS_Hypothesis*>& usedHyps
400 = algo->GetUsedHypothesis(*this, aSubMesh->GetSubShape());
401 list<SMESHDS_Hypothesis*>::const_iterator itl;
402 for(itl=usedHyps.begin(); itl != usedHyps.end(); itl++)
409 if (usesHyp) _subMeshesUsingHypothesisList.push_back(aSubMesh);
411 return _subMeshesUsingHypothesisList;
414 //=============================================================================
418 //=============================================================================
420 void SMESH_Mesh::ExportMED( const char* file )
421 throw (SALOME_Exception)
423 Mesh_Writer* myWriter = new DriverMED_W_SMESHDS_Mesh;
424 myWriter->SetFile( string(file) );
425 myWriter->SetMesh( _myMeshDS );
426 MESSAGE ( " _idDoc " << _idDoc )
427 myWriter->SetMeshId( _idDoc );
431 void SMESH_Mesh::ExportDAT( const char* file )
432 throw (SALOME_Exception)
434 Mesh_Writer* myWriter = new DriverDAT_W_SMESHDS_Mesh;
435 myWriter->SetFile( string(file) );
436 myWriter->SetMesh( _myMeshDS );
437 myWriter->SetMeshId( _idDoc );
441 void SMESH_Mesh::ExportUNV( const char* file )
442 throw (SALOME_Exception)
444 Mesh_Writer* myWriter = new DriverUNV_W_SMESHDS_Mesh;
445 myWriter->SetFile( string(file) );
446 myWriter->SetMesh( _myMeshDS );
447 myWriter->SetMeshId( _idDoc );
451 //=============================================================================
455 //=============================================================================
456 int SMESH_Mesh::NbNodes()
457 throw (SALOME_Exception)
459 return _myMeshDS->NbNodes();
462 //=============================================================================
466 //=============================================================================
467 int SMESH_Mesh::NbEdges()
468 throw (SALOME_Exception)
470 return _myMeshDS->NbEdges();
473 //=============================================================================
477 //=============================================================================
478 int SMESH_Mesh::NbFaces()
479 throw (SALOME_Exception)
481 return _myMeshDS->NbFaces();
483 int SMESH_Mesh::NbTriangles()
484 throw (SALOME_Exception)
486 SMDS_MeshFacesIterator itFaces(_myMeshDS);
488 for (;itFaces.More();itFaces.Next()) {
489 const Handle(SMDS_MeshElement)& elem = itFaces.Value();
491 switch (elem->NbNodes()) {
500 int SMESH_Mesh::NbQuadrangles()
501 throw (SALOME_Exception)
503 SMDS_MeshFacesIterator itFaces(_myMeshDS);
505 for (;itFaces.More();itFaces.Next()) {
506 const Handle(SMDS_MeshElement)& elem = itFaces.Value();
508 switch (elem->NbNodes()) {
518 //=============================================================================
522 //=============================================================================
523 int SMESH_Mesh::NbVolumes()
524 throw (SALOME_Exception)
526 return _myMeshDS->NbVolumes();
528 int SMESH_Mesh::NbTetras()
529 throw (SALOME_Exception)
532 SMDS_MeshVolumesIterator itVolumes(_myMeshDS);
533 for (;itVolumes.More();itVolumes.Next()) {
534 const Handle(SMDS_MeshElement)& elem = itVolumes.Value();
536 switch (elem->NbNodes()) {
545 int SMESH_Mesh::NbHexas()
546 throw (SALOME_Exception)
549 SMDS_MeshVolumesIterator itVolumes(_myMeshDS);
550 for (;itVolumes.More();itVolumes.Next()) {
551 const Handle(SMDS_MeshElement)& elem = itVolumes.Value();
553 switch (elem->NbNodes()) {
563 //=============================================================================
567 //=============================================================================
568 int SMESH_Mesh::NbSubMesh()
569 throw (SALOME_Exception)
571 return _myMeshDS->NbSubMesh();