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
31 #include "SMESH_Mesh.hxx"
32 #include "SMESH_subMesh.hxx"
33 #include "SMESH_Gen.hxx"
34 #include "SMESH_Hypothesis.hxx"
35 #include "SMESHDS_Script.hxx"
36 //#include "SMESHDS_ListOfAsciiString.hxx"
37 //#include "SMESHDS_ListIteratorOfListOfAsciiString.hxx"
38 #include "SMESHDS_ListOfPtrHypothesis.hxx"
39 #include "SMESHDS_ListIteratorOfListOfPtrHypothesis.hxx"
40 #include "SMDS_MeshElement.hxx"
41 #include "SMDS_MeshFacesIterator.hxx"
42 #include "SMDS_MeshVolumesIterator.hxx"
43 #include "TCollection_AsciiString.hxx"
45 #include "utilities.h"
47 #include "Mesh_Writer.h"
48 #include "DriverMED_W_SMESHDS_Mesh.h"
49 #include "DriverDAT_W_SMESHDS_Mesh.h"
50 #include "DriverUNV_W_SMESHDS_Mesh.h"
52 //=============================================================================
56 //=============================================================================
58 SMESH_Mesh::SMESH_Mesh()
60 MESSAGE("SMESH_Mesh::SMESH_Mesh");
65 //=============================================================================
69 //=============================================================================
71 SMESH_Mesh::SMESH_Mesh(int localId,
74 const Handle(SMESHDS_Document)& myDocument)
76 MESSAGE("SMESH_Mesh::SMESH_Mesh(int localId)");
80 _myDocument = myDocument;
81 _idDoc = _myDocument->NewMesh();
82 _myMeshDS = _myDocument->GetMesh(_idDoc);
83 _isShapeToMesh = false;
86 //=============================================================================
90 //=============================================================================
92 SMESH_Mesh::~SMESH_Mesh()
94 MESSAGE("SMESH_Mesh::~SMESH_Mesh");
97 //=============================================================================
101 //=============================================================================
103 void SMESH_Mesh::ShapeToMesh(const TopoDS_Shape& aShape)
104 throw (SALOME_Exception)
106 MESSAGE("SMESH_Mesh::ShapeToMesh");
108 throw SALOME_Exception(LOCALIZED("a shape to mesh as already been defined"));
109 _isShapeToMesh = true;
110 _myMeshDS->ShapeToMesh(aShape);
113 TopExp::MapShapes(aShape,_subShapes);
116 //=============================================================================
120 //=============================================================================
122 bool SMESH_Mesh::AddHypothesis(const TopoDS_Shape& aSubShape,
124 throw (SALOME_Exception)
126 MESSAGE("SMESH_Mesh::AddHypothesis");
128 StudyContextStruct* sc = _gen->GetStudyContext(_studyId);
129 if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
131 MESSAGE("Hypothesis ID does not give an hypothesis");
134 throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
137 SMESH_subMesh* subMesh = GetSubMesh(aSubShape);
138 SMESH_Hypothesis* anHyp = sc->mapHypothesis[anHypId];
143 if (anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
144 event = SMESH_subMesh::ADD_HYP;
146 event = SMESH_subMesh::ADD_ALGO;
147 int ret = subMesh->AlgoStateEngine(event, anHyp);
149 // subShapes (only when shape is mainShape)
150 TopoDS_Shape mainShape = _myMeshDS->ShapeToMesh();
151 if (aSubShape.IsSame(mainShape))
153 if (anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
154 event = SMESH_subMesh::ADD_FATHER_HYP;
156 event = SMESH_subMesh::ADD_FATHER_ALGO;
157 subMesh->SubMeshesAlgoStateEngine(event, anHyp);
160 subMesh->DumpAlgoState(true);
165 //=============================================================================
169 //=============================================================================
171 bool SMESH_Mesh::RemoveHypothesis(const TopoDS_Shape& aSubShape,
173 throw (SALOME_Exception)
175 MESSAGE("SMESH_Mesh::RemoveHypothesis");
177 StudyContextStruct* sc = _gen->GetStudyContext(_studyId);
178 if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
179 throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
181 SMESH_subMesh* subMesh = GetSubMesh(aSubShape);
182 SMESH_Hypothesis* anHyp = sc->mapHypothesis[anHypId];
183 int hypType = anHyp->GetType();
189 if (anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
190 event = SMESH_subMesh::REMOVE_HYP;
192 event = SMESH_subMesh::REMOVE_ALGO;
193 int ret = subMesh->AlgoStateEngine(event, anHyp);
195 // subShapes (only when shape is mainShape)
197 TopoDS_Shape mainShape = _myMeshDS->ShapeToMesh();
198 if (aSubShape.IsSame(mainShape))
200 if (anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO)
201 event = SMESH_subMesh::REMOVE_FATHER_HYP;
203 event = SMESH_subMesh::REMOVE_FATHER_ALGO;
204 subMesh->SubMeshesAlgoStateEngine(event, anHyp);
207 subMesh->DumpAlgoState(true);
212 //=============================================================================
216 //=============================================================================
218 const Handle(SMESHDS_Mesh)& SMESH_Mesh::GetMeshDS()
224 //=============================================================================
228 //=============================================================================
230 const list<SMESHDS_Hypothesis*>&
231 SMESH_Mesh::GetHypothesisList(const TopoDS_Shape& aSubShape)
232 throw (SALOME_Exception)
234 MESSAGE("SMESH_Mesh::GetHypothesisList");
235 _subShapeHypothesisList.clear();
236 const SMESHDS_ListOfPtrHypothesis& listHyp
237 = _myMeshDS->GetHypothesis(aSubShape);
238 SMESHDS_ListIteratorOfListOfPtrHypothesis it(listHyp);
241 SMESHDS_Hypothesis* anHyp = it.Value();
242 _subShapeHypothesisList.push_back(anHyp);
245 return _subShapeHypothesisList;
248 //=============================================================================
252 //=============================================================================
254 const SMESHDS_ListOfCommand& SMESH_Mesh::GetLog()
255 throw (SALOME_Exception)
257 MESSAGE("SMESH_Mesh::GetLog");
258 Handle (SMESHDS_Script) scriptDS = _myMeshDS->GetScript();
259 const SMESHDS_ListOfCommand& logDS = scriptDS->GetCommands();
260 // SMESHDS_ListIteratorOfListOfCommand its;
261 // const SMESHDS_ListOfAsciiString& logDS = scriptDS->GetCommands();
262 // SMESHDS_ListIteratorOfListOfAsciiString its;
263 // for (its.Initialize(logDS); its.More(); its.Next())
265 // SCRUTE(its.Value().ToCString());
270 //=============================================================================
274 //=============================================================================
275 void SMESH_Mesh::ClearLog()
276 throw (SALOME_Exception)
278 MESSAGE("SMESH_Mesh::ClearLog");
279 Handle (SMESHDS_Script) scriptDS = _myMeshDS->GetScript();
283 //=============================================================================
287 //=============================================================================
289 int SMESH_Mesh::GetId()
291 MESSAGE("SMESH_Mesh::GetId");
295 //=============================================================================
299 //=============================================================================
301 SMESH_Gen* SMESH_Mesh::GetGen()
306 //=============================================================================
308 * Get or Create the SMESH_subMesh object implementation
310 //=============================================================================
312 SMESH_subMesh* SMESH_Mesh::GetSubMesh(const TopoDS_Shape & aSubShape)
313 throw (SALOME_Exception)
315 //MESSAGE("SMESH_Mesh::GetSubMesh");
316 SMESH_subMesh* aSubMesh;
317 int index = _subShapes.FindIndex(aSubShape);
318 if ( _mapSubMesh.find(index) != _mapSubMesh.end() ) {
319 aSubMesh = _mapSubMesh[index];
321 aSubMesh = new SMESH_subMesh(index, this, _myMeshDS, aSubShape);
322 _mapSubMesh[index] = aSubMesh;
327 if (_subShapes.Contains(aSubShape))
329 index = _subShapes.FindIndex(aSubShape);
330 ASSERT(_mapSubMesh.find(index) != _mapSubMesh.end());
331 aSubMesh = _mapSubMesh[index];
332 //MESSAGE("found submesh " << index);
336 index = _subShapes.Add(aSubShape);
337 aSubMesh = new SMESH_subMesh(index, this, _myMeshDS, aSubShape);
338 _mapSubMesh[index] = aSubMesh;
339 //MESSAGE("created submesh " << index);
345 //=============================================================================
347 * Get the SMESH_subMesh object implementation. Dont create it, return null
348 * if it does not exist.
350 //=============================================================================
352 // * Given a subShape, find if there is a subMesh associated to this subShape
353 // * or to a collection of shapes containing this subShape. Collection =
354 // * compsolid, shell, wire.
356 // * WARNING : with arg = compsolid, shell or wire returns always NULL.
357 // * with a face inside a shell, and submesh created for both, if arg is face,
358 // * returns first created submesh of the two.
359 // * subMesh is not created, return may be NULL.
361 SMESH_subMesh* SMESH_Mesh::GetSubMeshContaining(const TopoDS_Shape & aSubShape)
362 throw (SALOME_Exception)
364 //MESSAGE("SMESH_Mesh::GetSubMeshContaining");
365 bool isFound = false;
366 SMESH_subMesh* aSubMesh = NULL;
368 int index = _subShapes.FindIndex(aSubShape);
369 if ( _mapSubMesh.find(index) != _mapSubMesh.end() ) {
370 aSubMesh = _mapSubMesh[index];
376 if (_subShapes.Contains(aSubShape))
378 index = _subShapes.FindIndex(aSubShape);
379 ASSERT(_mapSubMesh.find(index) != _mapSubMesh.end());
380 aSubMesh = _mapSubMesh[index];
382 //MESSAGE("found submesh " << index);
386 // map<int, SMESH_subMesh*>::iterator itsm;
387 // for (itsm = _mapSubMesh.begin(); itsm != _mapSubMesh.end(); itsm++)
389 // aSubMesh = (*itsm).second;
390 // isFound = aSubMesh->Contains(aSubShape);
391 // if (isFound) break;
394 if (! isFound) aSubMesh = NULL;
398 //=============================================================================
402 //=============================================================================
404 const list <SMESH_subMesh*>&
405 SMESH_Mesh::GetSubMeshUsingHypothesis(SMESHDS_Hypothesis* anHyp)
406 throw (SALOME_Exception)
408 MESSAGE("SMESH_Mesh::GetSubMeshUsingHypothesis");
409 map<int, SMESH_subMesh*>::iterator itsm;
410 _subMeshesUsingHypothesisList.clear();
411 for (itsm = _mapSubMesh.begin(); itsm != _mapSubMesh.end(); itsm++)
413 SMESH_subMesh* aSubMesh = (*itsm).second;
414 bool usesHyp = false;
415 SMESH_Algo* algo = _gen->GetAlgo(*this, aSubMesh->GetSubShape());
418 const list<SMESHDS_Hypothesis*>& usedHyps
419 = algo->GetUsedHypothesis(*this, aSubMesh->GetSubShape());
420 list<SMESHDS_Hypothesis*>::const_iterator itl;
421 for(itl=usedHyps.begin(); itl != usedHyps.end(); itl++)
428 if (usesHyp) _subMeshesUsingHypothesisList.push_back(aSubMesh);
430 return _subMeshesUsingHypothesisList;
433 //=============================================================================
437 //=============================================================================
439 void SMESH_Mesh::ExportMED( const char* file )
440 throw (SALOME_Exception)
442 Mesh_Writer* myWriter = new DriverMED_W_SMESHDS_Mesh;
443 myWriter->SetFile( string(file) );
444 myWriter->SetMesh( _myMeshDS );
445 MESSAGE ( " _idDoc " << _idDoc )
446 myWriter->SetMeshId( _idDoc );
450 void SMESH_Mesh::ExportDAT( const char* file )
451 throw (SALOME_Exception)
453 Mesh_Writer* myWriter = new DriverDAT_W_SMESHDS_Mesh;
454 myWriter->SetFile( string(file) );
455 myWriter->SetMesh( _myMeshDS );
456 myWriter->SetMeshId( _idDoc );
460 void SMESH_Mesh::ExportUNV( const char* file )
461 throw (SALOME_Exception)
463 Mesh_Writer* myWriter = new DriverUNV_W_SMESHDS_Mesh;
464 myWriter->SetFile( string(file) );
465 myWriter->SetMesh( _myMeshDS );
466 myWriter->SetMeshId( _idDoc );
470 //=============================================================================
474 //=============================================================================
475 int SMESH_Mesh::NbNodes()
476 throw (SALOME_Exception)
478 return _myMeshDS->NbNodes();
481 //=============================================================================
485 //=============================================================================
486 int SMESH_Mesh::NbEdges()
487 throw (SALOME_Exception)
489 return _myMeshDS->NbEdges();
492 //=============================================================================
496 //=============================================================================
497 int SMESH_Mesh::NbFaces()
498 throw (SALOME_Exception)
500 return _myMeshDS->NbFaces();
502 int SMESH_Mesh::NbTriangles()
503 throw (SALOME_Exception)
505 SMDS_MeshFacesIterator itFaces(_myMeshDS);
507 for (;itFaces.More();itFaces.Next()) {
508 const Handle(SMDS_MeshElement)& elem = itFaces.Value();
510 switch (elem->NbNodes()) {
519 int SMESH_Mesh::NbQuadrangles()
520 throw (SALOME_Exception)
522 SMDS_MeshFacesIterator itFaces(_myMeshDS);
524 for (;itFaces.More();itFaces.Next()) {
525 const Handle(SMDS_MeshElement)& elem = itFaces.Value();
527 switch (elem->NbNodes()) {
537 //=============================================================================
541 //=============================================================================
542 int SMESH_Mesh::NbVolumes()
543 throw (SALOME_Exception)
545 return _myMeshDS->NbVolumes();
547 int SMESH_Mesh::NbTetras()
548 throw (SALOME_Exception)
551 SMDS_MeshVolumesIterator itVolumes(_myMeshDS);
552 for (;itVolumes.More();itVolumes.Next()) {
553 const Handle(SMDS_MeshElement)& elem = itVolumes.Value();
555 switch (elem->NbNodes()) {
564 int SMESH_Mesh::NbHexas()
565 throw (SALOME_Exception)
568 SMDS_MeshVolumesIterator itVolumes(_myMeshDS);
569 for (;itVolumes.More();itVolumes.Next()) {
570 const Handle(SMDS_MeshElement)& elem = itVolumes.Value();
572 switch (elem->NbNodes()) {
582 //=============================================================================
586 //=============================================================================
587 int SMESH_Mesh::NbSubMesh()
588 throw (SALOME_Exception)
590 return _myMeshDS->NbSubMesh();