Salome HOME
yfr : Merge with v1.2
[modules/smesh.git] / src / SMESH_I / SMESH_MeshEditor_i.cxx
1 using namespace std;
2 //  File      : SMESH_MeshEditor_i.cxx
3 //  Created   : Wed Jun 19 18:43:26 2002
4 //  Author    : Nicolas REJNERI
5
6 //  Project   : SALOME
7 //  Module    : SMESH
8 //  Copyright : Open CASCADE 2002
9 //  $Header$
10
11 #include "SMESH_MeshEditor_i.hxx"
12
13 #include "SMDS_MeshEdgesIterator.hxx"
14 #include "SMDS_MeshFacesIterator.hxx"
15 #include "SMDS_MeshVolumesIterator.hxx"
16 #include "SMDS_MeshEdge.hxx"
17 #include "SMDS_MeshFace.hxx"
18 #include "SMDS_MeshVolume.hxx"
19
20 #include "utilities.h"
21
22 #include <TColStd_MapOfInteger.hxx>
23 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
24
25
26
27 //=============================================================================
28 /*!
29  *  
30  */
31 //=============================================================================
32
33 SMESH_MeshEditor_i::SMESH_MeshEditor_i(const Handle(SMESHDS_Mesh)& theMesh) {
34   _myMeshDS = theMesh;
35 };
36
37 //=============================================================================
38 /*!
39  *  
40  */
41 //=============================================================================
42
43 CORBA::Boolean SMESH_MeshEditor_i::RemoveElements(const SMESH::long_array& IDsOfElements) {
44   for (int i = 0 ; i< IDsOfElements.length(); i++) {
45     CORBA::Long index = IDsOfElements[i];
46     _myMeshDS->RemoveElement(index);
47     MESSAGE ( "Element "<< index  << " was removed" )
48   }
49  return true;
50 };
51
52 //=============================================================================
53 /*!
54  *  
55  */
56 //=============================================================================
57
58 CORBA::Boolean SMESH_MeshEditor_i::RemoveNodes(const SMESH::long_array& IDsOfNodes) {
59
60   // Here we try to collect all 1D, 2D and 3D elements which contain at least one 
61   // of <IDsOfNodes> in order to remove such elements. 
62   // This seems correct since e.g a triangle without 1 vertex looks nonsense.
63   TColStd_MapOfInteger elemsToRemove;
64
65   for (int i = 0 ; i< IDsOfNodes.length(); i++) {
66
67     CORBA::Long ID = IDsOfNodes[i];
68
69     SMDS_MeshEdgesIterator edgeIt(_myMeshDS);
70     for (; edgeIt.More(); edgeIt.Next()) {
71       Handle(SMDS_MeshEdge) anEdge = Handle(SMDS_MeshEdge)::DownCast(edgeIt.Value());
72       for (Standard_Integer i = 0; i < anEdge->NbNodes(); i++) {
73         if (anEdge->GetConnection(i) == ID) {
74           Standard_Integer elemID = anEdge->GetID();
75           if (!elemsToRemove.Contains(elemID)) elemsToRemove.Add(elemID);
76         }
77       }
78     }
79
80     SMDS_MeshFacesIterator faceIt(_myMeshDS);
81     for (; faceIt.More(); faceIt.Next()) {
82       Handle(SMDS_MeshFace) aFace = Handle(SMDS_MeshFace)::DownCast(faceIt.Value());
83       for (Standard_Integer i = 0; i < aFace->NbNodes(); i++) {
84         if (aFace->GetConnection(i) == ID) {
85           Standard_Integer elemID = aFace->GetID();
86           if (!elemsToRemove.Contains(elemID)) elemsToRemove.Add(elemID);
87         }
88       }
89     }
90   
91     SMDS_MeshVolumesIterator volIt(_myMeshDS);
92     for (; volIt.More(); volIt.Next()) {
93       Handle(SMDS_MeshVolume) aVol = Handle(SMDS_MeshVolume)::DownCast(volIt.Value());
94       for (Standard_Integer i = 0; i < aVol->NbNodes(); i++) {
95         if (aVol->GetConnection(i) == ID) {
96           Standard_Integer elemID = aVol->GetID();
97           if (!elemsToRemove.Contains(elemID)) elemsToRemove.Add(elemID);
98         }
99       }
100     }
101   }
102
103   // Now remove them!
104   TColStd_MapIteratorOfMapOfInteger it(elemsToRemove);
105   for (; it.More(); it.Next()) {
106     Standard_Integer elemID = it.Key();
107     _myMeshDS->RemoveElement(elemID);
108     MESSAGE("RemoveNodes(): element removed: " << elemID)
109   }
110   
111   // It's nodes' turn to die
112   for (int i = 0 ; i< IDsOfNodes.length(); i++) {
113     CORBA::Long index = IDsOfNodes[i];
114     _myMeshDS->RemoveNode(index);
115     MESSAGE ( "Node "<< index  << " was removed" )
116   }
117  return true;
118 };
119
120 //=============================================================================
121 /*!
122  *  
123  */
124 //=============================================================================
125
126 CORBA::Boolean SMESH_MeshEditor_i::AddEdge(const SMESH::long_array& IDsOfNodes) {
127   int NbNodes = IDsOfNodes.length();
128   if ( NbNodes == 2 ) {
129     CORBA::Long index1 = IDsOfNodes[0];
130     CORBA::Long index2 = IDsOfNodes[1];
131     int idTri = _myMeshDS->AddEdge(index1,index2);
132   }
133   return true;
134 }
135
136 //=============================================================================
137 /*!
138  *  
139  */
140 //=============================================================================
141
142 CORBA::Boolean SMESH_MeshEditor_i::AddNode(CORBA::Double x,
143                                            CORBA::Double y,
144                                            CORBA::Double z) {
145   MESSAGE( " AddNode " << x << " , " << y << " , " << z )
146   int idNode = _myMeshDS->AddNode(x,y,z);
147   MESSAGE( " idNode " << idNode )
148   return true;
149 }
150
151 //=============================================================================
152 /*!
153  *  
154  */
155 //=============================================================================
156
157 CORBA::Boolean SMESH_MeshEditor_i::AddFace(const SMESH::long_array& IDsOfNodes) {
158   int NbNodes = IDsOfNodes.length();
159   if ( NbNodes == 3 ) {
160     CORBA::Long index1 = IDsOfNodes[0];
161     CORBA::Long index2 = IDsOfNodes[1];
162     CORBA::Long index3 = IDsOfNodes[2];
163     int idTri = _myMeshDS->AddFace(index1,index2,index3);
164   } else  if ( NbNodes == 4 ) {
165     CORBA::Long index1 = IDsOfNodes[0];
166     CORBA::Long index2 = IDsOfNodes[1];
167     CORBA::Long index3 = IDsOfNodes[2];
168     CORBA::Long index4 = IDsOfNodes[3];
169     int idTri = _myMeshDS->AddFace(index1,index2,index3,index4);
170   }
171  return true;
172 };
173
174 //=============================================================================
175 /*!
176  *  
177  */
178 //=============================================================================
179
180 CORBA::Boolean SMESH_MeshEditor_i::AddVolume(const SMESH::long_array& IDsOfNodes) {
181   int NbNodes = IDsOfNodes.length();
182   if ( NbNodes == 4 ) {
183     CORBA::Long index1 = IDsOfNodes[0];
184     CORBA::Long index2 = IDsOfNodes[1];
185     CORBA::Long index3 = IDsOfNodes[2];
186     CORBA::Long index4 = IDsOfNodes[3];
187     int idTetra = _myMeshDS->AddVolume(index1,index2,index3,index4);
188   } else if ( NbNodes == 5 ) {
189     CORBA::Long index1 = IDsOfNodes[0];
190     CORBA::Long index2 = IDsOfNodes[1];
191     CORBA::Long index3 = IDsOfNodes[2];
192     CORBA::Long index4 = IDsOfNodes[3];
193     CORBA::Long index5 = IDsOfNodes[4];
194     int idPyramid = _myMeshDS->AddVolume(index1,index2,index3,index4,index5);
195   } else if ( NbNodes == 6 ) {
196     CORBA::Long index1 = IDsOfNodes[0];
197     CORBA::Long index2 = IDsOfNodes[1];
198     CORBA::Long index3 = IDsOfNodes[2];
199     CORBA::Long index4 = IDsOfNodes[3];
200     CORBA::Long index5 = IDsOfNodes[4];
201     CORBA::Long index6 = IDsOfNodes[5];
202     int idPrism = _myMeshDS->AddVolume(index1,index2,index3,index4,index5,index6);
203   } else if ( NbNodes == 8 ) {
204     CORBA::Long index1 = IDsOfNodes[0];
205     CORBA::Long index2 = IDsOfNodes[1];
206     CORBA::Long index3 = IDsOfNodes[2];
207     CORBA::Long index4 = IDsOfNodes[3];
208     CORBA::Long index5 = IDsOfNodes[4];
209     CORBA::Long index6 = IDsOfNodes[5];
210     CORBA::Long index7 = IDsOfNodes[6];
211     CORBA::Long index8 = IDsOfNodes[7];
212     int idHexa = _myMeshDS->AddVolume(index1,index2,index3,index4,index5,index6,index7,index8);
213   } 
214   return true;
215 };