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