1 // SMESH SMESH : implementaion of SMESH idl descriptions
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_Mesh.cxx
25 // Author : Paul RASCLE, EDF
29 #include "SMESH_Mesh.hxx"
30 #include "SMESH_subMesh.hxx"
31 #include "SMESH_Gen.hxx"
32 #include "SMESH_Hypothesis.hxx"
33 #include "SMESHDS_Script.hxx"
34 #include "SMDS_MeshVolume.hxx"
36 #include "utilities.h"
38 #include "Mesh_Writer.h"
39 #include "DriverMED_W_SMESHDS_Mesh.h"
40 #include "DriverDAT_W_SMESHDS_Mesh.h"
41 #include "DriverUNV_W_SMESHDS_Mesh.h"
43 #include <TCollection_AsciiString.hxx>
45 //=============================================================================
49 //=============================================================================
51 SMESH_Mesh::SMESH_Mesh()
53 MESSAGE("SMESH_Mesh::SMESH_Mesh");
58 //=============================================================================
62 //=============================================================================
64 SMESH_Mesh::SMESH_Mesh(int localId,
65 int studyId, SMESH_Gen * gen, SMESHDS_Document * myDocument)
67 MESSAGE("SMESH_Mesh::SMESH_Mesh(int localId)");
71 _myDocument = myDocument;
72 _idDoc = _myDocument->NewMesh();
73 _myMeshDS = _myDocument->GetMesh(_idDoc);
74 _isShapeToMesh = false;
77 //=============================================================================
81 //=============================================================================
83 SMESH_Mesh::~SMESH_Mesh()
85 MESSAGE("SMESH_Mesh::~SMESH_Mesh");
88 //=============================================================================
92 //=============================================================================
94 void SMESH_Mesh::ShapeToMesh(const TopoDS_Shape & aShape)
95 throw(SALOME_Exception)
97 MESSAGE("SMESH_Mesh::ShapeToMesh");
100 SALOME_Exception(LOCALIZED
101 ("a shape to mesh as already been defined"));
102 _isShapeToMesh = true;
103 _myMeshDS->ShapeToMesh(aShape);
106 TopExp::MapShapes(aShape, _subShapes);
109 //=============================================================================
113 //=============================================================================
115 bool SMESH_Mesh::AddHypothesis(const TopoDS_Shape & aSubShape,
116 int anHypId) throw(SALOME_Exception)
118 MESSAGE("SMESH_Mesh::AddHypothesis");
120 StudyContextStruct *sc = _gen->GetStudyContext(_studyId);
121 if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
123 MESSAGE("Hypothesis ID does not give an hypothesis");
126 throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
129 SMESH_subMesh *subMesh = GetSubMesh(aSubShape);
130 SMESH_Hypothesis *anHyp = sc->mapHypothesis[anHypId];
135 if (anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
136 event = SMESH_subMesh::ADD_HYP;
138 event = SMESH_subMesh::ADD_ALGO;
139 int ret = subMesh->AlgoStateEngine(event, anHyp);
141 // subShapes (only when shape is mainShape)
142 TopoDS_Shape mainShape = _myMeshDS->ShapeToMesh();
143 if (aSubShape.IsSame(mainShape))
145 if (anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
146 event = SMESH_subMesh::ADD_FATHER_HYP;
148 event = SMESH_subMesh::ADD_FATHER_ALGO;
149 subMesh->SubMeshesAlgoStateEngine(event, anHyp);
152 subMesh->DumpAlgoState(true);
157 //=============================================================================
161 //=============================================================================
163 bool SMESH_Mesh::RemoveHypothesis(const TopoDS_Shape & aSubShape,
164 int anHypId)throw(SALOME_Exception)
166 MESSAGE("SMESH_Mesh::RemoveHypothesis");
168 StudyContextStruct *sc = _gen->GetStudyContext(_studyId);
169 if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
170 throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
172 SMESH_subMesh *subMesh = GetSubMesh(aSubShape);
173 SMESH_Hypothesis *anHyp = sc->mapHypothesis[anHypId];
174 int hypType = anHyp->GetType();
180 if (anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
181 event = SMESH_subMesh::REMOVE_HYP;
183 event = SMESH_subMesh::REMOVE_ALGO;
184 int ret = subMesh->AlgoStateEngine(event, anHyp);
186 // subShapes (only when shape is mainShape)
188 TopoDS_Shape mainShape = _myMeshDS->ShapeToMesh();
189 if (aSubShape.IsSame(mainShape))
191 if (anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
192 event = SMESH_subMesh::REMOVE_FATHER_HYP;
194 event = SMESH_subMesh::REMOVE_FATHER_ALGO;
195 subMesh->SubMeshesAlgoStateEngine(event, anHyp);
198 subMesh->DumpAlgoState(true);
203 //=============================================================================
207 //=============================================================================
209 SMESHDS_Mesh * SMESH_Mesh::GetMeshDS()
214 //=============================================================================
218 //=============================================================================
220 const list<const SMESHDS_Hypothesis*>&
221 SMESH_Mesh::GetHypothesisList(const TopoDS_Shape & aSubShape)
222 throw(SALOME_Exception)
224 MESSAGE("SMESH_Mesh::GetHypothesisList");
225 _subShapeHypothesisList.clear();
226 const list<const SMESHDS_Hypothesis*>& listHyp =
227 _myMeshDS->GetHypothesis(aSubShape);
229 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
230 while (it!=listHyp.end())
232 const SMESHDS_Hypothesis *anHyp = *it;
233 _subShapeHypothesisList.push_back(anHyp);
236 return _subShapeHypothesisList;
239 //=============================================================================
243 //=============================================================================
245 const list<SMESHDS_Command*> & SMESH_Mesh::GetLog() throw(SALOME_Exception)
247 MESSAGE("SMESH_Mesh::GetLog");
248 return _myMeshDS->GetScript()->GetCommands();
251 //=============================================================================
255 //=============================================================================
256 void SMESH_Mesh::ClearLog() throw(SALOME_Exception)
258 MESSAGE("SMESH_Mesh::ClearLog");
259 _myMeshDS->GetScript()->Clear();
262 //=============================================================================
266 //=============================================================================
268 int SMESH_Mesh::GetId()
270 MESSAGE("SMESH_Mesh::GetId");
274 //=============================================================================
278 //=============================================================================
280 SMESH_Gen *SMESH_Mesh::GetGen()
285 //=============================================================================
287 * Get or Create the SMESH_subMesh object implementation
289 //=============================================================================
291 SMESH_subMesh *SMESH_Mesh::GetSubMesh(const TopoDS_Shape & aSubShape)
292 throw(SALOME_Exception)
294 //MESSAGE("SMESH_Mesh::GetSubMesh");
295 SMESH_subMesh *aSubMesh;
296 int index = _subShapes.FindIndex(aSubShape);
297 if (_mapSubMesh.find(index) != _mapSubMesh.end())
299 aSubMesh = _mapSubMesh[index];
303 aSubMesh = new SMESH_subMesh(index, this, _myMeshDS, aSubShape);
304 _mapSubMesh[index] = aSubMesh;
309 * if (_subShapes.Contains(aSubShape))
311 * index = _subShapes.FindIndex(aSubShape);
312 * ASSERT(_mapSubMesh.find(index) != _mapSubMesh.end());
313 * aSubMesh = _mapSubMesh[index];
314 * //MESSAGE("found submesh " << index);
318 * index = _subShapes.Add(aSubShape);
319 * aSubMesh = new SMESH_subMesh(index, this, _myMeshDS, aSubShape);
320 * _mapSubMesh[index] = aSubMesh;
321 * //MESSAGE("created submesh " << index);
327 //=============================================================================
329 * Get the SMESH_subMesh object implementation. Dont create it, return null
330 * if it does not exist.
332 //=============================================================================
334 // * Given a subShape, find if there is a subMesh associated to this subShape
335 // * or to a collection of shapes containing this subShape. Collection =
336 // * compsolid, shell, wire.
338 // * WARNING : with arg = compsolid, shell or wire returns always NULL.
339 // * with a face inside a shell, and submesh created for both, if arg is face,
340 // * returns first created submesh of the two.
341 // * subMesh is not created, return may be NULL.
343 SMESH_subMesh *SMESH_Mesh::GetSubMeshContaining(const TopoDS_Shape & aSubShape)
344 throw(SALOME_Exception)
346 //MESSAGE("SMESH_Mesh::GetSubMeshContaining");
347 bool isFound = false;
348 SMESH_subMesh *aSubMesh = NULL;
350 int index = _subShapes.FindIndex(aSubShape);
351 if (_mapSubMesh.find(index) != _mapSubMesh.end())
353 aSubMesh = _mapSubMesh[index];
359 * if (_subShapes.Contains(aSubShape))
361 * index = _subShapes.FindIndex(aSubShape);
362 * ASSERT(_mapSubMesh.find(index) != _mapSubMesh.end());
363 * aSubMesh = _mapSubMesh[index];
365 * //MESSAGE("found submesh " << index);
369 // map<int, SMESH_subMesh*>::iterator itsm;
370 // for (itsm = _mapSubMesh.begin(); itsm != _mapSubMesh.end(); itsm++)
372 // aSubMesh = (*itsm).second;
373 // isFound = aSubMesh->Contains(aSubShape);
374 // if (isFound) break;
382 //=============================================================================
386 //=============================================================================
388 const list < SMESH_subMesh * >&
389 SMESH_Mesh::GetSubMeshUsingHypothesis(SMESHDS_Hypothesis * anHyp)
390 throw(SALOME_Exception)
392 MESSAGE("SMESH_Mesh::GetSubMeshUsingHypothesis");
393 map < int, SMESH_subMesh * >::iterator itsm;
394 _subMeshesUsingHypothesisList.clear();
395 for (itsm = _mapSubMesh.begin(); itsm != _mapSubMesh.end(); itsm++)
397 SMESH_subMesh *aSubMesh = (*itsm).second;
398 bool usesHyp = false;
399 SMESH_Algo *algo = _gen->GetAlgo(*this, aSubMesh->GetSubShape());
402 const list <const SMESHDS_Hypothesis * >&usedHyps
403 = algo->GetUsedHypothesis(*this, aSubMesh->GetSubShape());
404 list <const SMESHDS_Hypothesis * >::const_iterator itl;
405 for (itl = usedHyps.begin(); itl != usedHyps.end(); itl++)
413 _subMeshesUsingHypothesisList.push_back(aSubMesh);
415 return _subMeshesUsingHypothesisList;
418 //=============================================================================
422 //=============================================================================
424 void SMESH_Mesh::ExportMED(const char *file) throw(SALOME_Exception)
426 Mesh_Writer *myWriter = new DriverMED_W_SMESHDS_Mesh;
427 myWriter->SetFile(string(file));
428 myWriter->SetMesh(_myMeshDS);
429 MESSAGE(" _idDoc " << _idDoc) myWriter->SetMeshId(_idDoc);
433 void SMESH_Mesh::ExportDAT(const char *file) throw(SALOME_Exception)
435 Mesh_Writer *myWriter = new DriverDAT_W_SMESHDS_Mesh;
436 myWriter->SetFile(string(file));
437 myWriter->SetMesh(_myMeshDS);
438 myWriter->SetMeshId(_idDoc);
442 void SMESH_Mesh::ExportUNV(const char *file) 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() throw(SALOME_Exception)
458 return _myMeshDS->NbNodes();
461 //=============================================================================
465 //=============================================================================
466 int SMESH_Mesh::NbEdges() throw(SALOME_Exception)
468 return _myMeshDS->NbEdges();
471 //=============================================================================
475 //=============================================================================
476 int SMESH_Mesh::NbFaces() throw(SALOME_Exception)
478 return _myMeshDS->NbFaces();
481 ///////////////////////////////////////////////////////////////////////////////
482 /// Return the number of 3 nodes faces in the mesh. This method run in O(n)
483 ///////////////////////////////////////////////////////////////////////////////
484 int SMESH_Mesh::NbTriangles() throw(SALOME_Exception)
488 SMDS_Iterator<const SMDS_MeshFace*> * itFaces=_myMeshDS->facesIterator();
489 while(itFaces->more()) if(itFaces->next()->NbNodes()==3) Nb++;
494 ///////////////////////////////////////////////////////////////////////////////
495 /// Return the number of 4 nodes faces in the mesh. This method run in O(n)
496 ///////////////////////////////////////////////////////////////////////////////
497 int SMESH_Mesh::NbQuadrangles() throw(SALOME_Exception)
501 SMDS_Iterator<const SMDS_MeshFace*> * itFaces=_myMeshDS->facesIterator();
502 while(itFaces->more()) if(itFaces->next()->NbNodes()==4) Nb++;
507 //=============================================================================
511 //=============================================================================
512 int SMESH_Mesh::NbVolumes() throw(SALOME_Exception)
514 return _myMeshDS->NbVolumes();
517 int SMESH_Mesh::NbTetras() throw(SALOME_Exception)
520 SMDS_Iterator<const SMDS_MeshVolume*> * itVolumes=_myMeshDS->volumesIterator();
521 while(itVolumes->more()) if(itVolumes->next()->NbNodes()==4) Nb++;
526 int SMESH_Mesh::NbHexas() throw(SALOME_Exception)
529 SMDS_Iterator<const SMDS_MeshVolume*> * itVolumes=_myMeshDS->volumesIterator();
530 while(itVolumes->more()) if(itVolumes->next()->NbNodes()==8) Nb++;
535 //=============================================================================
539 //=============================================================================
540 int SMESH_Mesh::NbSubMesh() throw(SALOME_Exception)
542 return _myMeshDS->NbSubMesh();