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