1 // SMESH SMESHDS : management of mesh data and SMESH document
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 : Yves FRICAUD, OCC
29 #include "SMESHDS_Mesh.hxx"
30 #include "SMDS_VertexPosition.hxx"
31 #include "SMDS_EdgePosition.hxx"
32 #include "SMDS_FacePosition.hxx"
33 #include <TopExp_Explorer.hxx>
36 #include "utilities.h"
37 //=======================================================================
40 //=======================================================================
41 SMESHDS_Mesh::SMESHDS_Mesh(int MeshID):myMeshID(MeshID)
43 myScript = new SMESHDS_Script();
46 //=======================================================================
47 //function : ShapeToMesh
49 //=======================================================================
50 void SMESHDS_Mesh::ShapeToMesh(const TopoDS_Shape & S)
53 TopExp::MapShapes(myShape, myIndexToShape);
56 //=======================================================================
57 //function : AddHypothesis
59 //=======================================================================
61 bool SMESHDS_Mesh::AddHypothesis(const TopoDS_Shape & SS,
62 const SMESHDS_Hypothesis * H)
64 list<const SMESHDS_Hypothesis *>& alist=myShapeToHypothesis[SS];
66 //Check if the Hypothesis is still present
67 list<const SMESHDS_Hypothesis*>::iterator ith=alist.begin();
69 for (; ith!=alist.end(); ith++)
70 if (H == *ith) return false;
76 //=======================================================================
77 //function : RemoveHypothesis
79 //=======================================================================
81 bool SMESHDS_Mesh::RemoveHypothesis(const TopoDS_Shape & S,
82 const SMESHDS_Hypothesis * H)
84 ShapeToHypothesis::iterator its=myShapeToHypothesis.find(S);
85 if(its!=myShapeToHypothesis.end())
87 list<const SMESHDS_Hypothesis*>::iterator ith=(*its).second.begin();
89 for (; ith!=(*its).second.end(); ith++)
92 (*its).second.erase(ith);
99 //=======================================================================
102 //=======================================================================
103 SMDS_MeshNode* SMESHDS_Mesh::AddNode(double x, double y, double z)
105 SMDS_MeshNode* node = SMDS_Mesh::AddNode(x, y, z);
106 if(node!=NULL) myScript->AddNode(node->GetID(), x, y, z);
110 //=======================================================================
111 //function : MoveNode
113 //=======================================================================
114 void SMESHDS_Mesh::MoveNode(int ID, double x, double y, double z)
116 SMDS_MeshNode * node=const_cast<SMDS_MeshNode*>(FindNode(ID));
118 myScript->MoveNode(ID, x, y, z);
121 //=======================================================================
124 //=======================================================================
125 SMDS_MeshEdge* SMESHDS_Mesh::AddEdge(int idnode1, int idnode2)
127 SMDS_MeshEdge* e = SMDS_Mesh::AddEdge(idnode1, idnode2);
128 if(e!=NULL) myScript->AddEdge(e->GetID(), idnode1, idnode2);
132 //=======================================================================
135 //=======================================================================
136 SMDS_MeshFace* SMESHDS_Mesh::AddFace(int idnode1, int idnode2, int idnode3)
138 SMDS_MeshFace *f = SMDS_Mesh::AddFace(idnode1, idnode2, idnode3);
139 if(f!=NULL) myScript->AddFace(f->GetID(), idnode1, idnode2, idnode3);
143 //=======================================================================
146 //=======================================================================
147 SMDS_MeshFace* SMESHDS_Mesh::AddFace(int idnode1, int idnode2, int idnode3,
150 SMDS_MeshFace *f = SMDS_Mesh::AddFace(idnode1, idnode2, idnode3, idnode4);
152 myScript->AddFace(f->GetID(), idnode1, idnode2, idnode3, idnode4);
156 //=======================================================================
157 //function :AddVolume
159 //=======================================================================
160 SMDS_MeshVolume* SMESHDS_Mesh::AddVolume(int idnode1, int idnode2, int idnode3,
163 SMDS_MeshVolume *f = SMDS_Mesh::AddVolume(idnode1, idnode2, idnode3,
166 myScript->AddVolume(f->GetID(), idnode1, idnode2, idnode3, idnode4);
170 //=======================================================================
171 //function :AddVolume
173 //=======================================================================
174 SMDS_MeshVolume* SMESHDS_Mesh::AddVolume(int idnode1, int idnode2, int idnode3,
175 int idnode4, int idnode5)
177 SMDS_MeshVolume *v = SMDS_Mesh::AddVolume(idnode1, idnode2, idnode3,
180 myScript->AddVolume(v->GetID(), idnode1, idnode2, idnode3, idnode4,
185 //=======================================================================
186 //function :AddVolume
188 //=======================================================================
189 SMDS_MeshVolume* SMESHDS_Mesh::AddVolume(int idnode1, int idnode2, int idnode3,
190 int idnode4, int idnode5, int idnode6)
193 SMDS_Mesh::AddVolume(idnode1, idnode2, idnode3, idnode4, idnode5,
196 myScript->AddVolume(v->GetID(), idnode1, idnode2, idnode3, idnode4,
201 //=======================================================================
202 //function :AddVolume
204 //=======================================================================
205 SMDS_MeshVolume* SMESHDS_Mesh::AddVolume(int idnode1, int idnode2, int idnode3,
206 int idnode4, int idnode5, int idnode6, int idnode7, int idnode8)
209 SMDS_Mesh::AddVolume(idnode1, idnode2, idnode3, idnode4, idnode5,
210 idnode6, idnode7, idnode8);
212 myScript->AddVolume(v->GetID(), idnode1, idnode2, idnode3, idnode4,
213 idnode5, idnode6, idnode7, idnode8);
217 //=======================================================================
218 //function : RemoveNode
220 //=======================================================================
221 void SMESHDS_Mesh::RemoveNode(int ID)
223 SMDS_Mesh::RemoveNode(ID);
224 myScript->RemoveNode(ID);
227 //=======================================================================
228 //function : RemoveElement
230 //========================================================================
231 void SMESHDS_Mesh::RemoveElement(int ID)
233 SMDS_Mesh::RemoveElement(ID);
234 myScript->RemoveElement(ID);
237 //=======================================================================
238 //function : SetNodeOnVolume
240 //=======================================================================
241 void SMESHDS_Mesh::SetNodeInVolume(SMDS_MeshNode * aNode,
242 const TopoDS_Shell & S)
244 if (myShape.IsNull()) MESSAGE("myShape is NULL");
246 int Index = myIndexToShape.FindIndex(S);
248 //Set Position on Node
249 //Handle (SMDS_FacePosition) aPos = new SMDS_FacePosition (myFaceToId(S),0.,0.);;
250 //aNode->SetPosition(aPos);
252 //Update or build submesh
253 map<int,SMESHDS_SubMesh*>::iterator it=myShapeIndexToSubMesh.find(Index);
254 if (it==myShapeIndexToSubMesh.end())
255 myShapeIndexToSubMesh[Index]= new SMESHDS_SubMesh(this);
257 myShapeIndexToSubMesh[Index]->AddNode(aNode);
260 //=======================================================================
261 //function : SetNodeOnFace
263 //=======================================================================
264 void SMESHDS_Mesh::SetNodeOnFace(SMDS_MeshNode * aNode,
265 const TopoDS_Face & S)
267 if (myShape.IsNull()) MESSAGE("myShape is NULL");
269 int Index = myIndexToShape.FindIndex(S);
271 //Set Position on Node
272 aNode->SetPosition(new SMDS_FacePosition(Index, 0., 0.));
274 //Update or build submesh
275 map<int,SMESHDS_SubMesh*>::iterator it=myShapeIndexToSubMesh.find(Index);
276 if (it==myShapeIndexToSubMesh.end())
277 myShapeIndexToSubMesh[Index]= new SMESHDS_SubMesh(this);
279 myShapeIndexToSubMesh[Index]->AddNode(aNode);
282 //=======================================================================
283 //function : SetNodeOnEdge
285 //=======================================================================
286 void SMESHDS_Mesh::SetNodeOnEdge(SMDS_MeshNode * aNode,
287 const TopoDS_Edge & S)
289 if (myShape.IsNull()) MESSAGE("myShape is NULL");
291 int Index = myIndexToShape.FindIndex(S);
293 //Set Position on Node
294 aNode->SetPosition(new SMDS_EdgePosition(Index, 0.));
296 //Update or build submesh
297 map<int,SMESHDS_SubMesh*>::iterator it=myShapeIndexToSubMesh.find(Index);
298 if (it==myShapeIndexToSubMesh.end())
299 myShapeIndexToSubMesh[Index]= new SMESHDS_SubMesh(this);
301 myShapeIndexToSubMesh[Index]->AddNode(aNode);
304 //=======================================================================
305 //function : SetNodeOnVertex
307 //=======================================================================
308 void SMESHDS_Mesh::SetNodeOnVertex(SMDS_MeshNode * aNode,
309 const TopoDS_Vertex & S)
311 if (myShape.IsNull()) MESSAGE("myShape is NULL");
313 int Index = myIndexToShape.FindIndex(S);
315 //Set Position on Node
316 aNode->SetPosition(new SMDS_VertexPosition(Index));
318 //Update or build submesh
319 map<int,SMESHDS_SubMesh*>::iterator it=myShapeIndexToSubMesh.find(Index);
320 if (it==myShapeIndexToSubMesh.end())
321 myShapeIndexToSubMesh[Index]= new SMESHDS_SubMesh(this);
323 myShapeIndexToSubMesh[Index]->AddNode(aNode);
326 //=======================================================================
327 //function : UnSetNodeOnShape
329 //=======================================================================
330 void SMESHDS_Mesh::UnSetNodeOnShape(const SMDS_MeshNode* aNode)
332 MESSAGE("not implemented");
335 //=======================================================================
336 //function : SetMeshElementOnShape
338 //=======================================================================
339 void SMESHDS_Mesh::SetMeshElementOnShape(const SMDS_MeshElement * anElement,
340 const TopoDS_Shape & S)
342 if (myShape.IsNull()) MESSAGE("myShape is NULL");
344 int Index = myIndexToShape.FindIndex(S);
346 if (myShapeIndexToSubMesh.find(Index)==myShapeIndexToSubMesh.end())
347 myShapeIndexToSubMesh[Index]=new SMESHDS_SubMesh(this);
349 myShapeIndexToSubMesh[Index]->AddElement(anElement);
352 //=======================================================================
353 //function : UnSetMeshElementOnShape
355 //=======================================================================
357 UnSetMeshElementOnShape(const SMDS_MeshElement * anElement,
358 const TopoDS_Shape & S)
360 if (myShape.IsNull()) MESSAGE("myShape is NULL");
362 int Index = myIndexToShape.FindIndex(S);
364 if (myShapeIndexToSubMesh.find(Index)!=myShapeIndexToSubMesh.end())
365 myShapeIndexToSubMesh[Index]->RemoveElement(anElement);
368 //=======================================================================
369 //function : ShapeToMesh
371 //=======================================================================
372 TopoDS_Shape SMESHDS_Mesh::ShapeToMesh() const
377 ///////////////////////////////////////////////////////////////////////////////
378 /// Return the sub mesh linked to the a given TopoDS_Shape or NULL if the given
379 /// TopoDS_Shape is unknown
380 ///////////////////////////////////////////////////////////////////////////////
381 SMESHDS_SubMesh * SMESHDS_Mesh::MeshElements(const TopoDS_Shape & S)
383 if (myShape.IsNull()) MESSAGE("myShape is NULL");
385 int Index = myIndexToShape.FindIndex(S);
386 if (myShapeIndexToSubMesh.find(Index)!=myShapeIndexToSubMesh.end())
387 return myShapeIndexToSubMesh[Index];
392 //=======================================================================
393 //function : GetHypothesis
395 //=======================================================================
397 const list<const SMESHDS_Hypothesis*>& SMESHDS_Mesh::GetHypothesis(
398 const TopoDS_Shape & S) const
400 if (myShapeToHypothesis.find(S)!=myShapeToHypothesis.end())
401 return myShapeToHypothesis.find(S)->second;
403 static list<const SMESHDS_Hypothesis*> empty;
407 //=======================================================================
408 //function : GetScript
410 //=======================================================================
411 SMESHDS_Script* SMESHDS_Mesh::GetScript()
416 //=======================================================================
417 //function : ClearScript
419 //=======================================================================
420 void SMESHDS_Mesh::ClearScript()
425 //=======================================================================
426 //function : HasMeshElements
428 //=======================================================================
429 bool SMESHDS_Mesh::HasMeshElements(const TopoDS_Shape & S)
431 if (myShape.IsNull()) MESSAGE("myShape is NULL");
432 int Index = myIndexToShape.FindIndex(S);
433 return myShapeIndexToSubMesh.find(Index)!=myShapeIndexToSubMesh.end();
436 //=======================================================================
437 //function : HasHypothesis
439 //=======================================================================
440 bool SMESHDS_Mesh::HasHypothesis(const TopoDS_Shape & S)
442 return myShapeToHypothesis.find(S)!=myShapeToHypothesis.end();
445 //=======================================================================
446 //function : NewSubMesh
448 //=======================================================================
449 void SMESHDS_Mesh::NewSubMesh(int Index)
451 if (myShapeIndexToSubMesh.find(Index)==myShapeIndexToSubMesh.end())
453 SMESHDS_SubMesh* SM = new SMESHDS_SubMesh(this);
454 myShapeIndexToSubMesh[Index]=SM;
458 //=======================================================================
459 //function : IndexToShape
461 //=======================================================================
462 TopoDS_Shape SMESHDS_Mesh::IndexToShape(int ShapeIndex)
464 return myIndexToShape.FindKey(ShapeIndex);
467 //=======================================================================
468 //function : ShapeToIndex
470 //=======================================================================
471 int SMESHDS_Mesh::ShapeToIndex(const TopoDS_Shape & S)
473 if (myShape.IsNull()) MESSAGE("myShape is NULL");
474 return myIndexToShape.FindIndex(S);
477 //=======================================================================
478 //function : SetNodeOnVolume
480 //=======================================================================
481 void SMESHDS_Mesh::SetNodeInVolume(const SMDS_MeshNode* aNode, int Index)
483 //Set Position on Node
484 //Handle (SMDS_FacePosition) aPos = new SMDS_FacePosition (myFaceToId(S),0.,0.);;
485 //aNode->SetPosition(aPos);
487 //Update or build submesh
488 if (myShapeIndexToSubMesh.find(Index)==myShapeIndexToSubMesh.end())
489 myShapeIndexToSubMesh[Index]=new SMESHDS_SubMesh(this);
491 myShapeIndexToSubMesh[Index]->AddNode(aNode);
494 //=======================================================================
495 //function : SetNodeOnFace
497 //=======================================================================
498 void SMESHDS_Mesh::SetNodeOnFace(SMDS_MeshNode* aNode, int Index)
500 //Set Position on Node
501 aNode->SetPosition(new SMDS_FacePosition(Index, 0., 0.));
503 //Update or build submesh
504 if (myShapeIndexToSubMesh.find(Index)==myShapeIndexToSubMesh.end())
505 myShapeIndexToSubMesh[Index]=new SMESHDS_SubMesh(this);
507 myShapeIndexToSubMesh[Index]->AddNode(aNode);
510 //=======================================================================
511 //function : SetNodeOnEdge
513 //=======================================================================
514 void SMESHDS_Mesh::SetNodeOnEdge(SMDS_MeshNode* aNode, int Index)
516 //Set Position on Node
517 aNode->SetPosition(new SMDS_EdgePosition(Index, 0.));
519 //Update or build submesh
520 if (myShapeIndexToSubMesh.find(Index)==myShapeIndexToSubMesh.end())
521 myShapeIndexToSubMesh[Index]=new SMESHDS_SubMesh(this);
523 myShapeIndexToSubMesh[Index]->AddNode(aNode);
526 //=======================================================================
527 //function : SetNodeOnVertex
529 //=======================================================================
530 void SMESHDS_Mesh::SetNodeOnVertex(SMDS_MeshNode* aNode, int Index)
532 //Set Position on Node
533 aNode->SetPosition(new SMDS_VertexPosition(Index));
535 //Update or build submesh
536 if (myShapeIndexToSubMesh.find(Index)==myShapeIndexToSubMesh.end())
537 myShapeIndexToSubMesh[Index]=new SMESHDS_SubMesh(this);
539 myShapeIndexToSubMesh[Index]->AddNode(aNode);
542 //=======================================================================
543 //function : SetMeshElementOnShape
545 //=======================================================================
546 void SMESHDS_Mesh::SetMeshElementOnShape(const SMDS_MeshElement* anElement,
549 if (myShapeIndexToSubMesh.find(Index)==myShapeIndexToSubMesh.end())
550 myShapeIndexToSubMesh[Index]=new SMESHDS_SubMesh(this);
552 myShapeIndexToSubMesh[Index]->AddElement(anElement);