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