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"
35 #include "utilities.h"
36 #include "SMESHDriver.h"
38 #include <TCollection_AsciiString.hxx>
40 //=============================================================================
44 //=============================================================================
46 SMESH_Mesh::SMESH_Mesh()
48 MESSAGE("SMESH_Mesh::SMESH_Mesh");
53 //=============================================================================
57 //=============================================================================
59 SMESH_Mesh::SMESH_Mesh(int localId,
60 int studyId, SMESH_Gen * gen, SMESHDS_Document * myDocument)
62 MESSAGE("SMESH_Mesh::SMESH_Mesh(int localId)");
66 _myDocument = myDocument;
67 _idDoc = _myDocument->NewMesh();
68 _myMeshDS = _myDocument->GetMesh(_idDoc);
69 _isShapeToMesh = false;
72 //=============================================================================
76 //=============================================================================
78 SMESH_Mesh::~SMESH_Mesh()
80 MESSAGE("SMESH_Mesh::~SMESH_Mesh");
83 //=============================================================================
87 //=============================================================================
89 void SMESH_Mesh::ShapeToMesh(const TopoDS_Shape & aShape)
90 throw(SALOME_Exception)
92 MESSAGE("SMESH_Mesh::ShapeToMesh");
95 SALOME_Exception(LOCALIZED
96 ("a shape to mesh as already been defined"));
97 _isShapeToMesh = true;
98 _myMeshDS->ShapeToMesh(aShape);
101 TopExp::MapShapes(aShape, _subShapes);
104 //=============================================================================
108 //=============================================================================
110 bool SMESH_Mesh::AddHypothesis(const TopoDS_Shape & aSubShape,
111 int anHypId) throw(SALOME_Exception)
113 MESSAGE("SMESH_Mesh::AddHypothesis");
115 StudyContextStruct *sc = _gen->GetStudyContext(_studyId);
116 if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
118 MESSAGE("Hypothesis ID does not give an hypothesis");
121 throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
124 SMESH_subMesh *subMesh = GetSubMesh(aSubShape);
125 SMESH_Hypothesis *anHyp = sc->mapHypothesis[anHypId];
130 if (anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
131 event = SMESH_subMesh::ADD_HYP;
133 event = SMESH_subMesh::ADD_ALGO;
134 int ret = subMesh->AlgoStateEngine(event, anHyp);
136 // subShapes (only when shape is mainShape)
137 TopoDS_Shape mainShape = _myMeshDS->ShapeToMesh();
138 if (aSubShape.IsSame(mainShape))
140 if (anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
141 event = SMESH_subMesh::ADD_FATHER_HYP;
143 event = SMESH_subMesh::ADD_FATHER_ALGO;
144 subMesh->SubMeshesAlgoStateEngine(event, anHyp);
147 subMesh->DumpAlgoState(true);
152 //=============================================================================
156 //=============================================================================
158 bool SMESH_Mesh::RemoveHypothesis(const TopoDS_Shape & aSubShape,
159 int anHypId)throw(SALOME_Exception)
161 MESSAGE("SMESH_Mesh::RemoveHypothesis");
163 StudyContextStruct *sc = _gen->GetStudyContext(_studyId);
164 if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
165 throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
167 SMESH_subMesh *subMesh = GetSubMesh(aSubShape);
168 SMESH_Hypothesis *anHyp = sc->mapHypothesis[anHypId];
169 int hypType = anHyp->GetType();
175 if (anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
176 event = SMESH_subMesh::REMOVE_HYP;
178 event = SMESH_subMesh::REMOVE_ALGO;
179 int ret = subMesh->AlgoStateEngine(event, anHyp);
181 // subShapes (only when shape is mainShape)
183 TopoDS_Shape mainShape = _myMeshDS->ShapeToMesh();
184 if (aSubShape.IsSame(mainShape))
186 if (anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
187 event = SMESH_subMesh::REMOVE_FATHER_HYP;
189 event = SMESH_subMesh::REMOVE_FATHER_ALGO;
190 subMesh->SubMeshesAlgoStateEngine(event, anHyp);
193 subMesh->DumpAlgoState(true);
198 //=============================================================================
202 //=============================================================================
204 SMESHDS_Mesh * SMESH_Mesh::GetMeshDS()
209 //=============================================================================
213 //=============================================================================
215 const list<const SMESHDS_Hypothesis*>&
216 SMESH_Mesh::GetHypothesisList(const TopoDS_Shape & aSubShape)
217 throw(SALOME_Exception)
219 MESSAGE("SMESH_Mesh::GetHypothesisList");
220 _subShapeHypothesisList.clear();
221 const list<const SMESHDS_Hypothesis*>& listHyp =
222 _myMeshDS->GetHypothesis(aSubShape);
224 list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
225 while (it!=listHyp.end())
227 const SMESHDS_Hypothesis *anHyp = *it;
228 _subShapeHypothesisList.push_back(anHyp);
231 return _subShapeHypothesisList;
234 //=============================================================================
238 //=============================================================================
240 const list<SMESHDS_Command*> & SMESH_Mesh::GetLog() throw(SALOME_Exception)
242 MESSAGE("SMESH_Mesh::GetLog");
243 return _myMeshDS->GetScript()->GetCommands();
246 //=============================================================================
250 //=============================================================================
251 void SMESH_Mesh::ClearLog() throw(SALOME_Exception)
253 MESSAGE("SMESH_Mesh::ClearLog");
254 _myMeshDS->GetScript()->Clear();
257 //=============================================================================
261 //=============================================================================
263 int SMESH_Mesh::GetId()
265 MESSAGE("SMESH_Mesh::GetId");
269 //=============================================================================
273 //=============================================================================
275 SMESH_Gen *SMESH_Mesh::GetGen()
280 //=============================================================================
282 * Get or Create the SMESH_subMesh object implementation
284 //=============================================================================
286 SMESH_subMesh *SMESH_Mesh::GetSubMesh(const TopoDS_Shape & aSubShape)
287 throw(SALOME_Exception)
289 //MESSAGE("SMESH_Mesh::GetSubMesh");
290 SMESH_subMesh *aSubMesh;
291 int index = _subShapes.FindIndex(aSubShape);
292 if (_mapSubMesh.find(index) != _mapSubMesh.end())
294 aSubMesh = _mapSubMesh[index];
298 aSubMesh = new SMESH_subMesh(index, this, _myMeshDS, aSubShape);
299 _mapSubMesh[index] = aSubMesh;
304 * if (_subShapes.Contains(aSubShape))
306 * index = _subShapes.FindIndex(aSubShape);
307 * ASSERT(_mapSubMesh.find(index) != _mapSubMesh.end());
308 * aSubMesh = _mapSubMesh[index];
309 * //MESSAGE("found submesh " << index);
313 * index = _subShapes.Add(aSubShape);
314 * aSubMesh = new SMESH_subMesh(index, this, _myMeshDS, aSubShape);
315 * _mapSubMesh[index] = aSubMesh;
316 * //MESSAGE("created submesh " << index);
322 //=============================================================================
324 * Get the SMESH_subMesh object implementation. Dont create it, return null
325 * if it does not exist.
327 //=============================================================================
329 // * Given a subShape, find if there is a subMesh associated to this subShape
330 // * or to a collection of shapes containing this subShape. Collection =
331 // * compsolid, shell, wire.
333 // * WARNING : with arg = compsolid, shell or wire returns always NULL.
334 // * with a face inside a shell, and submesh created for both, if arg is face,
335 // * returns first created submesh of the two.
336 // * subMesh is not created, return may be NULL.
338 SMESH_subMesh *SMESH_Mesh::GetSubMeshContaining(const TopoDS_Shape & aSubShape)
339 throw(SALOME_Exception)
341 //MESSAGE("SMESH_Mesh::GetSubMeshContaining");
342 bool isFound = false;
343 SMESH_subMesh *aSubMesh = NULL;
345 int index = _subShapes.FindIndex(aSubShape);
346 if (_mapSubMesh.find(index) != _mapSubMesh.end())
348 aSubMesh = _mapSubMesh[index];
354 * if (_subShapes.Contains(aSubShape))
356 * index = _subShapes.FindIndex(aSubShape);
357 * ASSERT(_mapSubMesh.find(index) != _mapSubMesh.end());
358 * aSubMesh = _mapSubMesh[index];
360 * //MESSAGE("found submesh " << index);
364 // map<int, SMESH_subMesh*>::iterator itsm;
365 // for (itsm = _mapSubMesh.begin(); itsm != _mapSubMesh.end(); itsm++)
367 // aSubMesh = (*itsm).second;
368 // isFound = aSubMesh->Contains(aSubShape);
369 // if (isFound) break;
377 //=============================================================================
381 //=============================================================================
383 const list < SMESH_subMesh * >&
384 SMESH_Mesh::GetSubMeshUsingHypothesis(SMESHDS_Hypothesis * anHyp)
385 throw(SALOME_Exception)
387 MESSAGE("SMESH_Mesh::GetSubMeshUsingHypothesis");
388 map < int, SMESH_subMesh * >::iterator itsm;
389 _subMeshesUsingHypothesisList.clear();
390 for (itsm = _mapSubMesh.begin(); itsm != _mapSubMesh.end(); itsm++)
392 SMESH_subMesh *aSubMesh = (*itsm).second;
393 bool usesHyp = false;
394 SMESH_Algo *algo = _gen->GetAlgo(*this, aSubMesh->GetSubShape());
397 const list <const SMESHDS_Hypothesis * >&usedHyps
398 = algo->GetUsedHypothesis(*this, aSubMesh->GetSubShape());
399 list <const SMESHDS_Hypothesis * >::const_iterator itl;
400 for (itl = usedHyps.begin(); itl != usedHyps.end(); itl++)
408 _subMeshesUsingHypothesisList.push_back(aSubMesh);
410 return _subMeshesUsingHypothesisList;
414 * Export mesh to a file
415 * @param fileName file name where to export the file
416 * @param fileType Currently it could be either "DAT", "UNV" or "MED".
418 void SMESH_Mesh::Export(const char *fileName, const char *fileType)
419 throw(SALOME_Exception)
421 MESSAGE("SMESH_Mesh::Export("<<fileName<<","<<fileType<<")");
422 Mesh_Writer * writer = SMESHDriver::GetMeshWriter(string(fileType));
425 writer->SetMesh(GetMeshDS());
426 writer->SetFile(string(fileName));
431 //=============================================================================
435 //=============================================================================
436 int SMESH_Mesh::NbNodes() throw(SALOME_Exception)
438 return _myMeshDS->NbNodes();
441 //=============================================================================
445 //=============================================================================
446 int SMESH_Mesh::NbEdges() throw(SALOME_Exception)
448 return _myMeshDS->NbEdges();
451 //=============================================================================
455 //=============================================================================
456 int SMESH_Mesh::NbFaces() throw(SALOME_Exception)
458 return _myMeshDS->NbFaces();
461 ///////////////////////////////////////////////////////////////////////////////
462 /// Return the number of 3 nodes faces in the mesh. This method run in O(n)
463 ///////////////////////////////////////////////////////////////////////////////
464 int SMESH_Mesh::NbTriangles() throw(SALOME_Exception)
468 SMDS_Iterator<const SMDS_MeshFace*> * itFaces=_myMeshDS->facesIterator();
469 while(itFaces->more()) if(itFaces->next()->NbNodes()==3) Nb++;
474 ///////////////////////////////////////////////////////////////////////////////
475 /// Return the number of 4 nodes faces in the mesh. This method run in O(n)
476 ///////////////////////////////////////////////////////////////////////////////
477 int SMESH_Mesh::NbQuadrangles() throw(SALOME_Exception)
481 SMDS_Iterator<const SMDS_MeshFace*> * itFaces=_myMeshDS->facesIterator();
482 while(itFaces->more()) if(itFaces->next()->NbNodes()==4) Nb++;
487 //=============================================================================
491 //=============================================================================
492 int SMESH_Mesh::NbVolumes() throw(SALOME_Exception)
494 return _myMeshDS->NbVolumes();
497 int SMESH_Mesh::NbTetras() throw(SALOME_Exception)
500 SMDS_Iterator<const SMDS_MeshVolume*> * itVolumes=_myMeshDS->volumesIterator();
501 while(itVolumes->more()) if(itVolumes->next()->NbNodes()==4) Nb++;
506 int SMESH_Mesh::NbHexas() throw(SALOME_Exception)
509 SMDS_Iterator<const SMDS_MeshVolume*> * itVolumes=_myMeshDS->volumesIterator();
510 while(itVolumes->more()) if(itVolumes->next()->NbNodes()==8) Nb++;
515 //=============================================================================
519 //=============================================================================
520 int SMESH_Mesh::NbSubMesh() throw(SALOME_Exception)
522 return _myMeshDS->NbSubMesh();