Salome HOME
sources v1.2
[modules/smesh.git] / src / SMESHDS / SMESHDS_Mesh.cxx
1 //  SMESH SMESHDS : management of mesh data and SMESH document
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
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. 
10 // 
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. 
15 // 
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 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SMESH_Mesh.cxx
25 //  Author : Yves FRICAUD, OCC
26 //  Module : SMESH
27 //  $Header: 
28
29 using namespace std;
30 #include "SMESHDS_Mesh.ixx"
31 #include "SMESHDS_Hypothesis.hxx"
32 #include "SMESHDS_DataMapOfShapeListOfPtrHypothesis.hxx"
33 #include "SMESHDS_ListIteratorOfListOfPtrHypothesis.hxx"
34 #include "SMDS_VertexPosition.hxx"
35 #include "SMDS_EdgePosition.hxx"
36 #include "SMDS_FacePosition.hxx"
37 #include <TopExp_Explorer.hxx>
38 #include <TopExp.hxx>
39
40 #include <Standard_NullObject.hxx>
41 #include "utilities.h"
42
43 //=======================================================================
44 //function : Create
45 //purpose  : 
46 //=======================================================================
47 SMESHDS_Mesh::SMESHDS_Mesh(const Standard_Integer MeshID) : myMeshID( MeshID)
48 {
49   myScript = new SMESHDS_Script();
50 }
51
52 //=======================================================================
53 //function : ShapeToMesh
54 //purpose  : 
55 //=======================================================================
56 void SMESHDS_Mesh::ShapeToMesh(const TopoDS_Shape& S) 
57 {
58   myShape = S;
59   TopExp::MapShapes(myShape,myIndexToShape);
60 }
61   
62   
63 //=======================================================================
64 //function : AddHypothesis
65 //purpose  : 
66 //=======================================================================
67
68 Standard_Boolean SMESHDS_Mesh::AddHypothesis(const TopoDS_Shape& SS,
69                                              const SMESHDS_PtrHypothesis& H) 
70 {
71   if (!myShapeToHypothesis.IsBound(SS)){
72     SMESHDS_ListOfPtrHypothesis empty;
73     myShapeToHypothesis.Bind(SS,empty);
74   }
75   else {
76     //Check if the Hypothesis is still present
77     SMESHDS_ListOfPtrHypothesis& Hypos = myShapeToHypothesis.ChangeFind (SS);
78
79     for (SMESHDS_ListIteratorOfListOfPtrHypothesis it(Hypos); it.More(); it.Next()) {
80       if (H == it.Value()) {
81         return Standard_False;
82       }
83     }
84   }
85   myShapeToHypothesis(SS).Append(H);
86   return Standard_True;
87 }
88
89 //=======================================================================
90 //function : RemoveHypothesis
91 //purpose  : 
92 //=======================================================================
93
94 Standard_Boolean SMESHDS_Mesh::RemoveHypothesis(const TopoDS_Shape& S,
95                                                 const SMESHDS_PtrHypothesis& H) 
96 {
97   if (myShapeToHypothesis.IsBound(S)){
98     SMESHDS_ListOfPtrHypothesis& Hypos = myShapeToHypothesis.ChangeFind (S);
99     for (SMESHDS_ListIteratorOfListOfPtrHypothesis it(Hypos); it.More(); it.Next()) {
100       if (H == it.Value()) {
101         Hypos.Remove(it);
102         return Standard_True;
103       }
104     }
105   }
106   return Standard_False;
107 }
108   
109   
110 //=======================================================================
111 //function : AddNode
112 //purpose  : 
113 //=======================================================================
114 Standard_Integer SMESHDS_Mesh::AddNode(const Standard_Real x,
115                                        const Standard_Real y,
116                                        const Standard_Real z) 
117 {
118   Standard_Integer NodeID = SMDS_Mesh::AddNode(x,y,z);
119   myScript->AddNode(NodeID,x,y,z);
120   return NodeID;
121 }
122
123 //=======================================================================
124 //function : MoveNode
125 //purpose  : 
126 //=======================================================================
127 void SMESHDS_Mesh::MoveNode(const Standard_Integer ID,
128                                         const Standard_Real x,
129                                         const Standard_Real y,
130                                         const Standard_Real z) 
131 {
132
133   Handle(SMDS_MeshNode) Node = Handle(SMDS_MeshNode)::DownCast(FindNode(ID));
134   gp_Pnt P(x,y,z);
135   Node->SetPnt(P);
136   myScript->MoveNode(ID,x,y,z);
137 }
138
139
140
141 //=======================================================================
142 //function : AddEdge
143 //purpose  : 
144 //=======================================================================
145 Standard_Integer SMESHDS_Mesh::AddEdge(const Standard_Integer idnode1,
146                                        const Standard_Integer idnode2) 
147 {
148   Standard_Integer ID = SMDS_Mesh::AddEdge(idnode1,idnode2);
149   myScript->AddEdge (ID,idnode1,idnode2);
150   return ID;
151 }
152
153
154 //=======================================================================
155 //function :AddFace
156 //purpose  : 
157 //=======================================================================
158 Standard_Integer SMESHDS_Mesh::AddFace(const Standard_Integer idnode1,
159                                        const Standard_Integer idnode2,
160                                        const Standard_Integer idnode3) 
161 {
162   Standard_Integer ID = SMDS_Mesh::AddFace(idnode1,idnode2,idnode3);
163   myScript->AddFace (ID,idnode1,idnode2,idnode3);
164   return ID;
165 }
166
167 //=======================================================================
168 //function :AddFace
169 //purpose  : 
170 //=======================================================================
171 Standard_Integer SMESHDS_Mesh::AddFace(const Standard_Integer idnode1,
172                                        const Standard_Integer idnode2,
173                                        const Standard_Integer idnode3,
174                                        const Standard_Integer idnode4) 
175 {
176   Standard_Integer ID = SMDS_Mesh::AddFace(idnode1,idnode2,idnode3,idnode4);
177   myScript->AddFace (ID,idnode1,idnode2,idnode3,idnode4);
178   return ID;
179 }
180
181
182 //=======================================================================
183 //function :AddVolume
184 //purpose  : 
185 //=======================================================================
186 Standard_Integer SMESHDS_Mesh::AddVolume(const Standard_Integer idnode1,
187                                          const Standard_Integer idnode2,
188                                          const Standard_Integer idnode3,
189                                          const Standard_Integer idnode4) 
190 {
191   Standard_Integer ID = SMDS_Mesh::AddVolume(idnode1,idnode2,idnode3,idnode4);
192   myScript->AddVolume (ID,idnode1,idnode2,idnode3,idnode4);
193   return ID;
194 }
195
196
197 //=======================================================================
198 //function :AddVolume
199 //purpose  : 
200 //=======================================================================
201 Standard_Integer SMESHDS_Mesh::AddVolume(const Standard_Integer idnode1,
202                                          const Standard_Integer idnode2,
203                                          const Standard_Integer idnode3,
204                                          const Standard_Integer idnode4,
205                                          const Standard_Integer idnode5) 
206 {
207   Standard_Integer ID = SMDS_Mesh::AddVolume(idnode1,idnode2,idnode3,idnode4,idnode5);
208   myScript->AddVolume (ID,idnode1,idnode2,idnode3,idnode4,idnode5);
209   return ID;
210 }
211
212
213 //=======================================================================
214 //function :AddVolume
215 //purpose  : 
216 //=======================================================================
217 Standard_Integer SMESHDS_Mesh::AddVolume(const Standard_Integer idnode1,
218                                          const Standard_Integer idnode2,
219                                          const Standard_Integer idnode3,
220                                          const Standard_Integer idnode4,
221                                          const Standard_Integer idnode5,
222                                          const Standard_Integer idnode6) 
223 {
224   Standard_Integer ID = SMDS_Mesh::AddVolume(idnode1,idnode2,idnode3,idnode4,idnode5,idnode6);
225   myScript->AddVolume (ID,idnode1,idnode2,idnode3,idnode4,idnode5,idnode6);
226   return ID;
227 }
228
229 //=======================================================================
230 //function :AddVolume
231 //purpose  : 
232 //=======================================================================
233 Standard_Integer SMESHDS_Mesh::AddVolume(const Standard_Integer idnode1,
234                                          const Standard_Integer idnode2,
235                                          const Standard_Integer idnode3,
236                                          const Standard_Integer idnode4,
237                                          const Standard_Integer idnode5,
238                                          const Standard_Integer idnode6,
239                                          const Standard_Integer idnode7,
240                                          const Standard_Integer idnode8) 
241 {
242   Standard_Integer ID = SMDS_Mesh::AddVolume(idnode1,idnode2,idnode3,idnode4,idnode5,idnode6,idnode7,idnode8);
243   myScript->AddVolume (ID,idnode1,idnode2,idnode3,idnode4,idnode5,idnode6,idnode7,idnode8);
244   return ID;
245 }
246
247
248 //=======================================================================
249 //function : RemoveNode
250 //purpose  : 
251 //=======================================================================
252 void SMESHDS_Mesh::RemoveNode(const Standard_Integer ID) 
253 {
254   SMDS_Mesh::RemoveNode (ID);
255   myScript->RemoveNode  (ID);
256 }
257
258
259
260 //=======================================================================
261 //function : RemoveElement
262 //purpose  : 
263 //========================================================================
264 void SMESHDS_Mesh::RemoveElement(const Standard_Integer ID) 
265 {
266   SMDS_Mesh::RemoveElement (ID);
267   myScript->RemoveElement  (ID);
268 }
269
270 //=======================================================================
271 //function : SetNodeOnVolume
272 //purpose  : 
273 //=======================================================================
274 void SMESHDS_Mesh::SetNodeInVolume (const Handle(SMDS_MeshNode)& aNode,
275                                     const TopoDS_Shell& S) 
276 {
277   if (myShape.IsNull()) 
278     Standard_NullObject::Raise("SMESHDS_Mesh::SetNodeOnVolume");
279   
280   Standard_Integer Index = myIndexToShape.FindIndex(S);
281   
282   //Set Position on Node
283   //Handle (SMDS_FacePosition) aPos = new SMDS_FacePosition (myFaceToId(S),0.,0.);;
284   //aNode->SetPosition(aPos);
285   
286   //Update or build submesh
287   if (!myShapeIndexToSubMesh.IsBound(Index)) {
288     Handle(SMESHDS_SubMesh) SM = new  SMESHDS_SubMesh (this);
289     myShapeIndexToSubMesh.Bind(Index,SM);
290   }
291   myShapeIndexToSubMesh(Index)->AddNode (aNode);
292 }
293
294 //=======================================================================
295 //function : SetNodeOnFace
296 //purpose  : 
297 //=======================================================================
298 void SMESHDS_Mesh::SetNodeOnFace (const Handle(SMDS_MeshNode)& aNode,
299                                   const TopoDS_Face& S) 
300 {
301   if (myShape.IsNull()) 
302     Standard_NullObject::Raise("SMESHDS_Mesh::SetNodeOnFace");
303   
304   Standard_Integer Index = myIndexToShape.FindIndex(S);
305   
306   //Set Position on Node
307   Handle (SMDS_FacePosition) aPos = new SMDS_FacePosition (Index,0.,0.);;
308   aNode->SetPosition(aPos);
309   
310   //Update or build submesh
311   if (!myShapeIndexToSubMesh.IsBound(Index)) {
312     Handle(SMESHDS_SubMesh) SM = new  SMESHDS_SubMesh (this);
313     myShapeIndexToSubMesh.Bind(Index,SM);
314   }
315   myShapeIndexToSubMesh(Index)->AddNode (aNode);
316 }
317
318 //=======================================================================
319 //function : SetNodeOnEdge
320 //purpose  : 
321 //=======================================================================
322 void SMESHDS_Mesh::SetNodeOnEdge (const Handle(SMDS_MeshNode)& aNode,
323                                   const TopoDS_Edge& S) 
324 {
325   if (myShape.IsNull()) 
326     Standard_NullObject::Raise("SMESHDS_Mesh::SetNodeOnEdge");
327   
328   Standard_Integer Index = myIndexToShape.FindIndex(S);
329   
330   //Set Position on Node
331   Handle (SMDS_EdgePosition) aPos = new SMDS_EdgePosition (Index,0.);;
332   aNode->SetPosition(aPos);
333   
334   //Update or build submesh
335   if (!myShapeIndexToSubMesh.IsBound(Index)) {
336     Handle(SMESHDS_SubMesh) SM = new  SMESHDS_SubMesh (this);
337     myShapeIndexToSubMesh.Bind(Index,SM);
338   }
339   myShapeIndexToSubMesh(Index)->AddNode (aNode);
340   
341 }
342
343 //=======================================================================
344 //function : SetNodeOnVertex
345 //purpose  : 
346 //=======================================================================
347 void SMESHDS_Mesh::SetNodeOnVertex (const Handle(SMDS_MeshNode)& aNode,
348                                     const TopoDS_Vertex& S) 
349
350   if (myShape.IsNull()) 
351     Standard_NullObject::Raise("SMESHDS_Mesh::SetNodeOnVertex");
352   
353   Standard_Integer Index = myIndexToShape.FindIndex(S);
354   
355   //Set Position on Node
356   Handle (SMDS_VertexPosition) aPos = new SMDS_VertexPosition (Index);;
357   aNode->SetPosition(aPos);
358   
359   //Update or build submesh
360   if (!myShapeIndexToSubMesh.IsBound(Index)) {
361     Handle(SMESHDS_SubMesh) SM = new  SMESHDS_SubMesh (this);
362     myShapeIndexToSubMesh.Bind(Index,SM);
363   }
364   myShapeIndexToSubMesh(Index)->AddNode (aNode);
365 }
366
367
368 //=======================================================================
369 //function : UnSetNodeOnShape
370 //purpose  : 
371 //=======================================================================
372 void SMESHDS_Mesh::UnSetNodeOnShape(const Handle(SMDS_MeshNode)& aNode) 
373
374   MESSAGE("not implemented");
375 }
376
377
378 //=======================================================================
379 //function : SetMeshElementOnShape
380 //purpose  : 
381 //=======================================================================
382 void SMESHDS_Mesh::SetMeshElementOnShape (const Handle(SMDS_MeshElement)& anElement,
383                                            const TopoDS_Shape& S) 
384 {
385   if (myShape.IsNull()) 
386     Standard_NullObject::Raise("SMESHDS_Mesh::SetMeshElementOnShape");
387   
388   Standard_Integer Index = myIndexToShape.FindIndex(S);
389   
390
391   if (!myShapeIndexToSubMesh.IsBound(Index)) {
392     Handle(SMESHDS_SubMesh) SM = new  SMESHDS_SubMesh (this);
393     myShapeIndexToSubMesh.Bind(Index,SM);
394   }
395   myShapeIndexToSubMesh(Index)->AddElement (anElement);
396 }
397
398 //=======================================================================
399 //function : UnSetMeshElementOnShape
400 //purpose  : 
401 //=======================================================================
402 void SMESHDS_Mesh::UnSetMeshElementOnShape (const Handle(SMDS_MeshElement)& anElement,
403                                              const TopoDS_Shape& S)
404   
405 {
406   if (myShape.IsNull()) 
407     Standard_NullObject::Raise("SMESHDS_Mesh::UnSetMeshElementOnShape");
408   
409   Standard_Integer Index = myIndexToShape.FindIndex(S);
410
411   if (myShapeIndexToSubMesh.IsBound(Index)) 
412     myShapeIndexToSubMesh(Index)->RemoveElement (anElement);
413 }
414
415 //=======================================================================
416 //function : ShapeToMesh
417 //purpose  : 
418 //=======================================================================
419 TopoDS_Shape SMESHDS_Mesh::ShapeToMesh() 
420 {
421   return myShape;
422 }
423
424 //=======================================================================
425 //function : MeshElements
426 //purpose  : 
427 //=======================================================================
428 Handle_SMESHDS_SubMesh SMESHDS_Mesh::MeshElements(const TopoDS_Shape& S) 
429 {
430   if (myShape.IsNull()) 
431     Standard_NullObject::Raise("SMESHDS_Mesh::MeshElements");
432   
433   Standard_Integer Index = myIndexToShape.FindIndex(S);
434
435   if (myShapeIndexToSubMesh.IsBound(Index)) 
436     return myShapeIndexToSubMesh(Index);
437   Handle(SMESHDS_SubMesh) SM;
438   return SM;
439 }
440
441 //=======================================================================
442 //function : GetHypothesis
443 //purpose  : 
444 //=======================================================================
445 const SMESHDS_ListOfPtrHypothesis&  SMESHDS_Mesh::GetHypothesis(const TopoDS_Shape& S) 
446 {
447   if (myShapeToHypothesis.IsBound(S))
448     return myShapeToHypothesis(S);
449  
450   static SMESHDS_ListOfPtrHypothesis empty;
451   return empty;
452 }
453
454 //=======================================================================
455 //function : GetScript
456 //purpose  : 
457 //=======================================================================
458 const Handle (SMESHDS_Script)&  SMESHDS_Mesh::GetScript() 
459 {
460   return myScript;
461 }
462
463 //=======================================================================
464 //function : ClearScript
465 //purpose  : 
466 //=======================================================================
467 void SMESHDS_Mesh::ClearScript() 
468 {
469   myScript->Clear();
470 }
471
472 //=======================================================================
473 //function : HasMeshElements
474 //purpose  : 
475 //=======================================================================
476 Standard_Boolean SMESHDS_Mesh::HasMeshElements(const TopoDS_Shape& S) 
477 {
478   if (myShape.IsNull()) 
479     Standard_NullObject::Raise("SMESHDS_Mesh::MeshElements");
480   
481   Standard_Integer Index = myIndexToShape.FindIndex(S);
482
483   return myShapeIndexToSubMesh.IsBound(Index);
484 }
485
486 //=======================================================================
487 //function : HasHypothesis
488 //purpose  : 
489 //=======================================================================
490 Standard_Boolean SMESHDS_Mesh::HasHypothesis(const TopoDS_Shape& S) 
491 {
492   return myShapeToHypothesis.IsBound(S);
493 }
494
495 //=======================================================================
496 //function : NewSubMesh 
497 //purpose  : 
498 //=======================================================================
499 void SMESHDS_Mesh::NewSubMesh(const Standard_Integer Index) 
500 {
501   if (!myShapeIndexToSubMesh.IsBound(Index)) {
502     Handle(SMESHDS_SubMesh) SM = new  SMESHDS_SubMesh (this);
503     myShapeIndexToSubMesh.Bind(Index,SM);
504   }
505 }
506
507 //=======================================================================
508 //function : IndexToShape
509 //purpose  : 
510 //=======================================================================
511 TopoDS_Shape SMESHDS_Mesh::IndexToShape(const Standard_Integer ShapeIndex)
512 {
513   return  myIndexToShape.FindKey(ShapeIndex);
514
515
516 //=======================================================================
517 //function : ShapeToIndex
518 //purpose  : 
519 //=======================================================================
520 Standard_Integer SMESHDS_Mesh::ShapeToIndex(const TopoDS_Shape& S) 
521 {
522   if (myShape.IsNull()) 
523     Standard_NullObject::Raise("SMESHDS_Mesh::SetNodeOnVolume");
524   
525   return  myIndexToShape.FindIndex(S);
526 }
527
528 //=======================================================================
529 //function : SetNodeOnVolume
530 //purpose  : 
531 //=======================================================================
532 void SMESHDS_Mesh::SetNodeInVolume (const Handle(SMDS_MeshNode)& aNode,
533                                     const Standard_Integer Index) 
534 {
535   
536   //Set Position on Node
537   //Handle (SMDS_FacePosition) aPos = new SMDS_FacePosition (myFaceToId(S),0.,0.);;
538   //aNode->SetPosition(aPos);
539   
540   //Update or build submesh
541   if (!myShapeIndexToSubMesh.IsBound(Index)) {
542     Handle(SMESHDS_SubMesh) SM = new  SMESHDS_SubMesh (this);
543     myShapeIndexToSubMesh.Bind(Index,SM);
544   }
545   myShapeIndexToSubMesh(Index)->AddNode (aNode);
546 }
547
548 //=======================================================================
549 //function : SetNodeOnFace
550 //purpose  : 
551 //=======================================================================
552 void SMESHDS_Mesh::SetNodeOnFace (const Handle(SMDS_MeshNode)& aNode,
553                                   const Standard_Integer Index) 
554 {
555   
556   //Set Position on Node
557   Handle (SMDS_FacePosition) aPos = new SMDS_FacePosition (Index,0.,0.);;
558   aNode->SetPosition(aPos);
559   
560   //Update or build submesh
561   if (!myShapeIndexToSubMesh.IsBound(Index)) {
562     Handle(SMESHDS_SubMesh) SM = new  SMESHDS_SubMesh (this);
563     myShapeIndexToSubMesh.Bind(Index,SM);
564   }
565   myShapeIndexToSubMesh(Index)->AddNode (aNode);
566 }
567
568 //=======================================================================
569 //function : SetNodeOnEdge
570 //purpose  : 
571 //=======================================================================
572 void SMESHDS_Mesh::SetNodeOnEdge (const Handle(SMDS_MeshNode)& aNode,
573                                   const Standard_Integer Index) 
574 {
575   
576   //Set Position on Node
577   Handle (SMDS_EdgePosition) aPos = new SMDS_EdgePosition (Index,0.);;
578   aNode->SetPosition(aPos);
579   
580   //Update or build submesh
581   if (!myShapeIndexToSubMesh.IsBound(Index)) {
582     Handle(SMESHDS_SubMesh) SM = new  SMESHDS_SubMesh (this);
583     myShapeIndexToSubMesh.Bind(Index,SM);
584   }
585   myShapeIndexToSubMesh(Index)->AddNode (aNode);
586   
587 }
588
589 //=======================================================================
590 //function : SetNodeOnVertex
591 //purpose  : 
592 //=======================================================================
593 void SMESHDS_Mesh::SetNodeOnVertex (const Handle(SMDS_MeshNode)& aNode,
594                                     const Standard_Integer Index) 
595
596   //Set Position on Node
597   Handle (SMDS_VertexPosition) aPos = new SMDS_VertexPosition (Index);;
598   aNode->SetPosition(aPos);
599   
600   //Update or build submesh
601   if (!myShapeIndexToSubMesh.IsBound(Index)) {
602     Handle(SMESHDS_SubMesh) SM = new  SMESHDS_SubMesh (this);
603     myShapeIndexToSubMesh.Bind(Index,SM);
604   }
605   myShapeIndexToSubMesh(Index)->AddNode (aNode);
606 }
607
608 //=======================================================================
609 //function : SetMeshElementOnShape
610 //purpose  : 
611 //=======================================================================
612 void SMESHDS_Mesh::SetMeshElementOnShape (const Handle(SMDS_MeshElement)& anElement,
613                                           const Standard_Integer Index) 
614 {
615   if (!myShapeIndexToSubMesh.IsBound(Index)) {
616     Handle(SMESHDS_SubMesh) SM = new  SMESHDS_SubMesh (this);
617     myShapeIndexToSubMesh.Bind(Index,SM);
618   }
619   myShapeIndexToSubMesh(Index)->AddElement (anElement);
620 }