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(const SMDS_MeshNode *n, double x, double y, double z)
116 SMDS_MeshNode * node=const_cast<SMDS_MeshNode*>(n);
118 myScript->MoveNode(n->GetID(), x, y, z);
121 //=======================================================================
124 //=======================================================================
125 SMDS_MeshEdge* SMESHDS_Mesh::AddEdge(const SMDS_MeshNode * n1,
126 const SMDS_MeshNode * n2)
128 SMDS_MeshEdge* e = SMDS_Mesh::AddEdge(n1,n2);
129 if(e!=NULL) myScript->AddEdge(e->GetID(), n1->GetID(), n2->GetID());
133 //=======================================================================
136 //=======================================================================
137 SMDS_MeshFace* SMESHDS_Mesh::AddFace( const SMDS_MeshNode * n1,
138 const SMDS_MeshNode * n2,
139 const SMDS_MeshNode * n3)
141 SMDS_MeshFace *f = SMDS_Mesh::AddFace(n1, n2, n3);
142 if(f!=NULL) myScript->AddFace(f->GetID(), n1->GetID(), n2->GetID(),
147 //=======================================================================
150 //=======================================================================
151 SMDS_MeshFace* SMESHDS_Mesh::AddFace(const SMDS_MeshNode * n1,
152 const SMDS_MeshNode * n2,
153 const SMDS_MeshNode * n3,
154 const SMDS_MeshNode * n4)
156 SMDS_MeshFace *f = SMDS_Mesh::AddFace(n1, n2, n3, n4);
158 myScript->AddFace(f->GetID(), n1->GetID(), n2->GetID(), n3->GetID(),
163 //=======================================================================
164 //function :AddVolume
166 //=======================================================================
167 SMDS_MeshVolume* SMESHDS_Mesh::AddVolume(
168 const SMDS_MeshNode * n1,
169 const SMDS_MeshNode * n2,
170 const SMDS_MeshNode * n3,
171 const SMDS_MeshNode * n4)
173 SMDS_MeshVolume *f = SMDS_Mesh::AddVolume(n1, n2, n3, n4);
175 myScript->AddVolume(f->GetID(), n1->GetID(), n2->GetID(), n3->GetID(),
180 //=======================================================================
181 //function :AddVolume
183 //=======================================================================
184 SMDS_MeshVolume* SMESHDS_Mesh::AddVolume(
185 const SMDS_MeshNode * n1,
186 const SMDS_MeshNode * n2,
187 const SMDS_MeshNode * n3,
188 const SMDS_MeshNode * n4,
189 const SMDS_MeshNode * n5)
191 SMDS_MeshVolume *v = SMDS_Mesh::AddVolume(n1, n2, n3, n4, n5);
193 myScript->AddVolume(v->GetID(), n1->GetID(), n2->GetID(), n3->GetID(),
194 n4->GetID(), n5->GetID());
198 //=======================================================================
199 //function :AddVolume
201 //=======================================================================
202 SMDS_MeshVolume* SMESHDS_Mesh::AddVolume(
203 const SMDS_MeshNode * n1,
204 const SMDS_MeshNode * n2,
205 const SMDS_MeshNode * n3,
206 const SMDS_MeshNode * n4,
207 const SMDS_MeshNode * n5,
208 const SMDS_MeshNode * n6)
210 SMDS_MeshVolume *v= SMDS_Mesh::AddVolume(n1, n2, n3, n4, n5, n6);
212 myScript->AddVolume(v->GetID(), n1->GetID(), n2->GetID(), n3->GetID(),
213 n4->GetID(), n5->GetID(), n6->GetID());
217 //=======================================================================
218 //function :AddVolume
220 //=======================================================================
221 SMDS_MeshVolume* SMESHDS_Mesh::AddVolume(
222 const SMDS_MeshNode * n1,
223 const SMDS_MeshNode * n2,
224 const SMDS_MeshNode * n3,
225 const SMDS_MeshNode * n4,
226 const SMDS_MeshNode * n5,
227 const SMDS_MeshNode * n6,
228 const SMDS_MeshNode * n7,
229 const SMDS_MeshNode * n8)
232 SMDS_Mesh::AddVolume(n1, n2, n3, n4, n5, n6, n7, n8);
234 myScript->AddVolume(v->GetID(), n1->GetID(), n2->GetID(), n3->GetID(),
235 n4->GetID(), n5->GetID(), n6->GetID(), n7->GetID(), n8->GetID());
239 //=======================================================================
240 //function : RemoveNode
242 //=======================================================================
243 void SMESHDS_Mesh::RemoveNode(const SMDS_MeshNode * n)
245 SMDS_Mesh::RemoveNode(n);
246 myScript->RemoveNode(n->GetID());
249 //=======================================================================
250 //function : RemoveElement
252 //========================================================================
253 void SMESHDS_Mesh::RemoveElement(const SMDS_MeshElement * elt)
255 SMDS_Mesh::RemoveElement(elt);
256 myScript->RemoveElement(elt->GetID());
259 //=======================================================================
260 //function : SetNodeOnVolume
262 //=======================================================================
263 void SMESHDS_Mesh::SetNodeInVolume(SMDS_MeshNode * aNode,
264 const TopoDS_Shell & S)
266 if (myShape.IsNull()) MESSAGE("myShape is NULL");
268 int Index = myIndexToShape.FindIndex(S);
270 //Set Position on Node
271 //Handle (SMDS_FacePosition) aPos = new SMDS_FacePosition (myFaceToId(S),0.,0.);;
272 //aNode->SetPosition(aPos);
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();
279 myShapeIndexToSubMesh[Index]->AddNode(aNode);
282 //=======================================================================
283 //function : SetNodeOnFace
285 //=======================================================================
286 void SMESHDS_Mesh::SetNodeOnFace(SMDS_MeshNode * aNode,
287 const TopoDS_Face & 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_FacePosition(Index, 0., 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();
301 myShapeIndexToSubMesh[Index]->AddNode(aNode);
304 //=======================================================================
305 //function : SetNodeOnEdge
307 //=======================================================================
308 void SMESHDS_Mesh::SetNodeOnEdge(SMDS_MeshNode * aNode,
309 const TopoDS_Edge & 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_EdgePosition(Index, 0.));
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();
323 myShapeIndexToSubMesh[Index]->AddNode(aNode);
326 //=======================================================================
327 //function : SetNodeOnVertex
329 //=======================================================================
330 void SMESHDS_Mesh::SetNodeOnVertex(SMDS_MeshNode * aNode,
331 const TopoDS_Vertex & S)
333 if (myShape.IsNull()) MESSAGE("myShape is NULL");
335 int Index = myIndexToShape.FindIndex(S);
337 //Set Position on Node
338 aNode->SetPosition(new SMDS_VertexPosition(Index));
340 //Update or build submesh
341 map<int,SMESHDS_SubMesh*>::iterator it=myShapeIndexToSubMesh.find(Index);
342 if (it==myShapeIndexToSubMesh.end())
343 myShapeIndexToSubMesh[Index]= new SMESHDS_SubMesh();
345 myShapeIndexToSubMesh[Index]->AddNode(aNode);
348 //=======================================================================
349 //function : UnSetNodeOnShape
351 //=======================================================================
352 void SMESHDS_Mesh::UnSetNodeOnShape(const SMDS_MeshNode* aNode)
354 MESSAGE("not implemented");
357 //=======================================================================
358 //function : SetMeshElementOnShape
360 //=======================================================================
361 void SMESHDS_Mesh::SetMeshElementOnShape(const SMDS_MeshElement * anElement,
362 const TopoDS_Shape & S)
364 if (myShape.IsNull()) MESSAGE("myShape is NULL");
366 int Index = myIndexToShape.FindIndex(S);
368 if (myShapeIndexToSubMesh.find(Index)==myShapeIndexToSubMesh.end())
369 myShapeIndexToSubMesh[Index]=new SMESHDS_SubMesh();
371 myShapeIndexToSubMesh[Index]->AddElement(anElement);
374 //=======================================================================
375 //function : UnSetMeshElementOnShape
377 //=======================================================================
379 UnSetMeshElementOnShape(const SMDS_MeshElement * anElement,
380 const TopoDS_Shape & S)
382 if (myShape.IsNull()) MESSAGE("myShape is NULL");
384 int Index = myIndexToShape.FindIndex(S);
386 if (myShapeIndexToSubMesh.find(Index)!=myShapeIndexToSubMesh.end())
387 myShapeIndexToSubMesh[Index]->RemoveElement(anElement);
390 //=======================================================================
391 //function : ShapeToMesh
393 //=======================================================================
394 TopoDS_Shape SMESHDS_Mesh::ShapeToMesh() const
399 ///////////////////////////////////////////////////////////////////////////////
400 /// Return the sub mesh linked to the a given TopoDS_Shape or NULL if the given
401 /// TopoDS_Shape is unknown
402 ///////////////////////////////////////////////////////////////////////////////
403 SMESHDS_SubMesh * SMESHDS_Mesh::MeshElements(const TopoDS_Shape & S)
405 if (myShape.IsNull()) MESSAGE("myShape is NULL");
407 int Index = myIndexToShape.FindIndex(S);
408 if (myShapeIndexToSubMesh.find(Index)!=myShapeIndexToSubMesh.end())
409 return myShapeIndexToSubMesh[Index];
414 //=======================================================================
415 //function : GetHypothesis
417 //=======================================================================
419 const list<const SMESHDS_Hypothesis*>& SMESHDS_Mesh::GetHypothesis(
420 const TopoDS_Shape & S) const
422 if (myShapeToHypothesis.find(S)!=myShapeToHypothesis.end())
423 return myShapeToHypothesis.find(S)->second;
425 static list<const SMESHDS_Hypothesis*> empty;
429 //=======================================================================
430 //function : GetScript
432 //=======================================================================
433 SMESHDS_Script* SMESHDS_Mesh::GetScript()
438 //=======================================================================
439 //function : ClearScript
441 //=======================================================================
442 void SMESHDS_Mesh::ClearScript()
447 //=======================================================================
448 //function : HasMeshElements
450 //=======================================================================
451 bool SMESHDS_Mesh::HasMeshElements(const TopoDS_Shape & S)
453 if (myShape.IsNull()) MESSAGE("myShape is NULL");
454 int Index = myIndexToShape.FindIndex(S);
455 return myShapeIndexToSubMesh.find(Index)!=myShapeIndexToSubMesh.end();
458 //=======================================================================
459 //function : HasHypothesis
461 //=======================================================================
462 bool SMESHDS_Mesh::HasHypothesis(const TopoDS_Shape & S)
464 return myShapeToHypothesis.find(S)!=myShapeToHypothesis.end();
467 //=======================================================================
468 //function : NewSubMesh
470 //=======================================================================
471 void SMESHDS_Mesh::NewSubMesh(int Index)
473 if (myShapeIndexToSubMesh.find(Index)==myShapeIndexToSubMesh.end())
475 SMESHDS_SubMesh* SM = new SMESHDS_SubMesh();
476 myShapeIndexToSubMesh[Index]=SM;
480 //=======================================================================
481 //function : IndexToShape
483 //=======================================================================
484 TopoDS_Shape SMESHDS_Mesh::IndexToShape(int ShapeIndex)
486 return myIndexToShape.FindKey(ShapeIndex);
489 //=======================================================================
490 //function : ShapeToIndex
492 //=======================================================================
493 int SMESHDS_Mesh::ShapeToIndex(const TopoDS_Shape & S)
495 if (myShape.IsNull()) MESSAGE("myShape is NULL");
496 return myIndexToShape.FindIndex(S);
499 //=======================================================================
500 //function : SetNodeOnVolume
502 //=======================================================================
503 void SMESHDS_Mesh::SetNodeInVolume(const SMDS_MeshNode* aNode, int Index)
505 //Set Position on Node
506 //Handle (SMDS_FacePosition) aPos = new SMDS_FacePosition (myFaceToId(S),0.,0.);;
507 //aNode->SetPosition(aPos);
509 //Update or build submesh
510 if (myShapeIndexToSubMesh.find(Index)==myShapeIndexToSubMesh.end())
511 myShapeIndexToSubMesh[Index]=new SMESHDS_SubMesh();
513 myShapeIndexToSubMesh[Index]->AddNode(aNode);
516 //=======================================================================
517 //function : SetNodeOnFace
519 //=======================================================================
520 void SMESHDS_Mesh::SetNodeOnFace(SMDS_MeshNode* aNode, int Index)
522 //Set Position on Node
523 aNode->SetPosition(new SMDS_FacePosition(Index, 0., 0.));
525 //Update or build submesh
526 if (myShapeIndexToSubMesh.find(Index)==myShapeIndexToSubMesh.end())
527 myShapeIndexToSubMesh[Index]=new SMESHDS_SubMesh();
529 myShapeIndexToSubMesh[Index]->AddNode(aNode);
532 //=======================================================================
533 //function : SetNodeOnEdge
535 //=======================================================================
536 void SMESHDS_Mesh::SetNodeOnEdge(SMDS_MeshNode* aNode, int Index)
538 //Set Position on Node
539 aNode->SetPosition(new SMDS_EdgePosition(Index, 0.));
541 //Update or build submesh
542 if (myShapeIndexToSubMesh.find(Index)==myShapeIndexToSubMesh.end())
543 myShapeIndexToSubMesh[Index]=new SMESHDS_SubMesh();
545 myShapeIndexToSubMesh[Index]->AddNode(aNode);
548 //=======================================================================
549 //function : SetNodeOnVertex
551 //=======================================================================
552 void SMESHDS_Mesh::SetNodeOnVertex(SMDS_MeshNode* aNode, int Index)
554 //Set Position on Node
555 aNode->SetPosition(new SMDS_VertexPosition(Index));
557 //Update or build submesh
558 if (myShapeIndexToSubMesh.find(Index)==myShapeIndexToSubMesh.end())
559 myShapeIndexToSubMesh[Index]=new SMESHDS_SubMesh();
561 myShapeIndexToSubMesh[Index]->AddNode(aNode);
564 //=======================================================================
565 //function : SetMeshElementOnShape
567 //=======================================================================
568 void SMESHDS_Mesh::SetMeshElementOnShape(const SMDS_MeshElement* anElement,
571 if (myShapeIndexToSubMesh.find(Index)==myShapeIndexToSubMesh.end())
572 myShapeIndexToSubMesh[Index]=new SMESHDS_SubMesh();
574 myShapeIndexToSubMesh[Index]->AddElement(anElement);
577 SMESHDS_Mesh::~SMESHDS_Mesh()