Salome HOME
Merge with PAL/SALOME 2.1.0d
[modules/geom.git] / src / GEOM_SWIG / GEOM_TestHealing.py
1 #  GEOM GEOM_SWIG : binding of C++ implementaion with Python
2 #
3 #  Copyright (C) 2003  CEA
4 #
5 #  This library is free software; you can redistribute it and/or
6 #  modify it under the terms of the GNU Lesser General Public
7 #  License as published by the Free Software Foundation; either
8 #  version 2.1 of the License.
9 #
10 #  This library is distributed in the hope that it will be useful,
11 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 #  Lesser General Public License for more details.
14 #
15 #  You should have received a copy of the GNU Lesser General Public
16 #  License along with this library; if not, write to the Free Software
17 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 #
19 #  See http://www.salome-platform.org or email : webmaster.salome@opencascade.org
20 #
21 #
22 #  File   : GEOM_TestHealing.py
23 #  Author : Julia DOROVSKIKH
24 #  Module : GEOM
25 #  $Header$
26
27 def TestProcessShape (geompy):
28
29   ##Load shape from BREP file
30   #import os
31   #datadir = os.getenv("GEOM_TEST")
32   #if datadir != 0:
33   #  datadir = datadir + "/Resources/"
34   #else:
35   #  "Please, define GEOM_TEST variable !"
36   #
37   #print "Import ", datadir + "aze2.brep"
38   #Shape = batchmode_geompy.Import(datadir + "aze2.brep", "BREP")
39
40   p1 = geompy.MakeVertex(0,0,0)
41   p2 = geompy.MakeVertex(200,0,0)
42   p3 = geompy.MakeVertex(100,150,0)
43
44   edge = geompy.MakeEdge(p1,p2)
45   arc  = geompy.MakeArc(p1,p3,p2)
46   wire = geompy.MakeWire([edge,arc])
47   face = geompy.MakeFace(wire, 1)
48
49   theShape = geompy.MakePrismVecH(face, edge, 130)
50
51   #Check shape
52   print "Before ProcessShape:"
53   isValid = geompy.CheckShape(theShape)
54   if isValid == 0:
55     print "The shape is not valid"
56   else:
57     print "The shape seems to be valid"
58
59   #Process Shape
60   Operators = ["FixShape"]
61   Parameters = ["FixShape.Tolerance3d"]
62   Values = ["1e-7"]
63
64   PS = geompy.ProcessShape(theShape, Operators, Parameters, Values)
65
66   #Check shape
67   print "After ProcessShape:"
68   isValid = geompy.CheckShape(PS)
69   if isValid == 0:
70     print "The shape is not valid"
71     raise RuntimeError, "It seems, that the ProcessShape() has failed"
72   else:
73     print "The shape seems to be valid"
74
75   #Add In Study
76   Id_Shape = geompy.addToStudy(theShape, "Invalid Shape")
77   Id_PS    = geompy.addToStudy(PS, "Processed Shape")
78
79 def TestSuppressFaces (geompy):
80
81   #Create base geometry 3D
82   Box = geompy.MakeBoxDXDYDZ(200., 200., 200.)
83
84   #IDList for SuppHole
85   faces = []
86   faces = geompy.SubShapeAllSorted(Box, geompy.ShapeType["FACE"])
87
88   f_glob_id = geompy.GetSubShapeID(Box, faces[5])
89
90   #Transform objects
91   SuppFace = geompy.SuppressFaces(Box, [f_glob_id])
92
93   #Add In Study
94   Id_SuppFace = geompy.addToStudy(SuppFace, "SuppFace")
95
96 def TestSuppressInternalWires (geompy):
97
98   #Create Face with hole
99   p11 = geompy.MakeVertex( 0,  0, 0)
100   p12 = geompy.MakeVertex(30,  0, 0)
101   p13 = geompy.MakeVertex(30, 30, 0)
102   p14 = geompy.MakeVertex( 0, 30, 0)
103
104   p21 = geompy.MakeVertex(10, 10, 0)
105   p22 = geompy.MakeVertex(20, 10, 0)
106   p23 = geompy.MakeVertex(20, 20, 0)
107   p24 = geompy.MakeVertex(10, 20, 0)
108
109   e11 = geompy.MakeEdge(p11, p12)
110   e12 = geompy.MakeEdge(p12, p13)
111   e13 = geompy.MakeEdge(p13, p14)
112   e14 = geompy.MakeEdge(p14, p11)
113
114   e21 = geompy.MakeEdge(p21, p22)
115   e22 = geompy.MakeEdge(p22, p23)
116   e23 = geompy.MakeEdge(p23, p24)
117   e24 = geompy.MakeEdge(p24, p21)
118
119   w1 = geompy.MakeWire([e11, e12, e13, e14])
120   w2 = geompy.MakeWire([e21, e22, e23, e24])
121
122   id_w1 = geompy.addToStudy(w1, "Outside Wire")
123   id_w2 = geompy.addToStudy(w2, "Inside Wire")
124
125   f12 = geompy.MakeFaceWires([w1, w2], 0)
126   id_f12 = geompy.addToStudy(f12, "Face WO + WI")
127
128   #Get Free Boundary
129   Res1 = geompy.GetFreeBoundary(f12)
130   isSuccess1   = Res1[0]
131   ClosedWires1 = Res1[1]
132   OpenWires1   = Res1[2]
133   nbw1 = 0
134
135   for wire in ClosedWires1:
136     nbw1 = nbw1 + 1
137
138   if nbw1 != 2:
139     raise RuntimeError, "GetFreeBoundary(f12) must return 2 closed wires, but returned ", nbw1
140
141   #SuppressInternalWires
142   face = geompy.SuppressInternalWires(f12, [])
143
144   #Get Free Boundary
145   Res2 = geompy.GetFreeBoundary(face)
146   isSuccess2   = Res2[0]
147   ClosedWires2 = Res2[1]
148   OpenWires2   = Res2[2]
149   nbw2 = 0
150
151   for wire in ClosedWires2:
152     nbw2 = nbw2 + 1
153
154   if nbw2 != 1:
155     print "GetFreeBoundary(face) must return 1 closed wires, but returned ", nbw2
156     raise RuntimeError, "SuppressInternalWires() works not correctly"
157
158   #Add In Study
159   Id_face = geompy.addToStudy(face, "Face without internal wires")
160
161 def TestCloseContour (geompy):
162
163   ##Load shape from BREP file
164   #import os
165   #datadir = os.getenv("GEOM_TEST")
166   #if datadir != 0:
167   #  datadir = datadir + "/Resources/"
168   #else:
169   #  "Please, define GEOM_TEST variable !"
170   #
171   #print "Import ", datadir + "open_cont.brep"
172   #Shape = geompy.Import(datadir + "open_cont.brep", "BREP")
173
174   p0   = geompy.MakeVertex(0.  , 0.  , 0.  )
175   py   = geompy.MakeVertex(0.  , 100., 0.  )
176   pz   = geompy.MakeVertex(0.  , 0.  , 100.)
177   p200 = geompy.MakeVertex(200., 200., 200.)
178
179   Shape = geompy.MakePolyline([p0, pz, py, p200])
180
181   #Check shape
182   print "Before closing contour:"
183   isValid = geompy.CheckShape(Shape)
184   if isValid == 0:
185     print "The shape is not valid"
186   else:
187     print "The shape seems to be valid"
188
189   #Close Contour
190   IsCommonVertex = 0 # false
191
192   shape_wires = geompy.SubShapeAll(Shape, geompy.ShapeType["WIRE"])
193   Wires = []
194   wi = 0
195
196   for wire in shape_wires:
197     Wires.append(geompy.GetSubShapeID(Shape, shape_wires[wi]))
198     wi = wi + 1
199
200   CC = geompy.CloseContour(Shape, Wires, IsCommonVertex)
201
202   #Check shape
203   print "After closing contour:"
204   isValid = geompy.CheckShape(CC)
205   if isValid == 0:
206     print "The shape is not valid"
207     raise RuntimeError, "It seems, that the contour was not closed"
208   else:
209     print "The shape seems to be valid"
210
211   #Add In Study
212   Id_Shape = geompy.addToStudy(Shape, "Shape with open wire")
213   Id_CC    = geompy.addToStudy(CC, "Shape with closed wire")
214
215 def TestSuppressHoles (geompy):
216
217   #Create base Variables
218   radius = 50.
219   height = 300.
220
221   #Create base points
222   p1 = geompy.MakeVertex(100., 100., 50.)
223
224   #Create base directions
225   vz = geompy.MakeVectorDXDYDZ(0., 0., 100.)
226
227   #Create base geometry 3D
228   Box      = geompy.MakeBoxDXDYDZ(200., 200., 200.)
229   Cylinder = geompy.MakeCylinder(p1, vz, radius, height)
230
231   #Boolean (Cut)
232   Cut = geompy.MakeBoolean(Box, Cylinder, 2)
233   idCut = geompy.addToStudy(Cut, "CUT")
234
235   #IDList for SuppressFaces
236   faces = []
237   faces = geompy.SubShapeAllSorted(Cut, geompy.ShapeType["FACE"])
238   ind = 0
239   for face in faces:
240       f_name = "FACE %d"%(ind)
241       f_id = geompy.addToStudyInFather(Cut, face, f_name)
242
243       f_glob_id = geompy.GetSubShapeID(Cut, face)
244       print "face ", ind, " global index = ", f_glob_id
245       ind = ind + 1
246
247   f_glob_id_0 = geompy.GetSubShapeID(Cut, faces[0])
248   cut_without_f_0 = geompy.SuppressFaces(Cut, [f_glob_id_0])
249
250   faces1 = []
251   faces1 = geompy.SubShapeAllSorted(cut_without_f_0, geompy.ShapeType["FACE"])
252   ind = 0
253   for face in faces1:
254       f_name = "FACE %d"%(ind)
255       f_id = geompy.addToStudyInFather(cut_without_f_0, face, f_name)
256
257       f_glob_id = geompy.GetSubShapeID(cut_without_f_0, face)
258       print "face ", ind, " global index = ", f_glob_id
259       ind = ind + 1
260
261   f_glob_id_5 = geompy.GetSubShapeID(cut_without_f_0, faces1[5])
262   cut_without_f_0_5 = geompy.SuppressFaces(cut_without_f_0, [f_glob_id_5])
263   cut_without_f_0_5_id = geompy.addToStudy(cut_without_f_0_5, "Cut without faces 0 and 5")
264
265   #IDList for SuppHole
266   wires = []
267   wires = geompy.SubShapeAllSorted(cut_without_f_0_5, geompy.ShapeType["WIRE"])
268   ind = 0
269   for wire in wires:
270       w_name = "WIRE %d"%(ind)
271       w_id = geompy.addToStudyInFather(cut_without_f_0_5, wire, w_name)
272
273       w_glob_id = geompy.GetSubShapeID(cut_without_f_0_5, wire)
274       print "wire ", ind, " global index = ", w_glob_id
275       ind = ind + 1
276
277   w_3 = geompy.GetSubShapeID(cut_without_f_0_5, wires[3])
278
279   SuppHole3 = geompy.SuppressHoles(cut_without_f_0_5, [w_3])
280   SuppHole3_id = geompy.addToStudy(SuppHole3, "Supp Hole 3")
281
282 def TestMakeSewing (geompy, math):
283
284   #Create base points
285   px = geompy.MakeVertex(100., 0., 0.)
286   py = geompy.MakeVertex(0., 100., 0.)
287   pz = geompy.MakeVertex(0., 0., 100.)
288
289   #Create base geometry 2D & 3D
290   Vector = geompy.MakeVector(px, py)
291   Arc    = geompy.MakeArc(py, pz, px)
292
293   #Create base objects
294   angle     = 45. * math.pi / 180
295   WantPlanarFace = 1 #True
296
297   Wire = geompy.MakeWire([Vector, Arc])
298   Face = geompy.MakeFace(Wire, WantPlanarFace)
299   S    = geompy.MakeRotation(Face, Vector, angle)
300
301   #Make Sewing
302   precision = 0.00001
303   Sewing = geompy.MakeSewing([Face, S], precision)
304
305   #Add In Study
306   id_Sewing = geompy.addToStudy(Sewing, "Sewing")
307
308 def TestDivideEdge (geompy):
309
310   #Create Box
311   Box = geompy.MakeBoxDXDYDZ(200., 200., 200.)
312
313   #Divide Edge
314   box_edges = geompy.SubShapeAllSorted(Box, geompy.ShapeType["EDGE"])
315   edge_ind = geompy.GetSubShapeID(Box, box_edges[1])
316
317   Divide = geompy.DivideEdge(Box, edge_ind, 0.5, 1) # Obj, ind, param, is_curve_param
318
319   #Add In Study
320   Id_Box    = geompy.addToStudy(Box, "Box")
321   Id_Divide = geompy.addToStudy(Divide, "Box with Divided Edge")
322
323 def TestHealingOperations (geompy, math):
324
325   TestMakeSewing(geompy, math)
326   TestDivideEdge(geompy)
327   TestSuppressHoles(geompy)
328   TestSuppressInternalWires(geompy)
329   TestCloseContour(geompy)
330   TestSuppressFaces(geompy)
331   TestProcessShape(geompy)