Salome HOME
Fix errors in new unit tests
[modules/shaper.git] / src / GeomAPI / Test / TestBuilders.py
1 ## Copyright (C) 2018-20xx  CEA/DEN, EDF R&D
2 ##
3 ## This library is free software; you can redistribute it and/or
4 ## modify it under the terms of the GNU Lesser General Public
5 ## License as published by the Free Software Foundation; either
6 ## version 2.1 of the License, or (at your option) any later version.
7 ##
8 ## This library is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 ## Lesser General Public License for more details.
12 ##
13 ## You should have received a copy of the GNU Lesser General Public
14 ## License along with this library; if not, write to the Free Software
15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 ##
17 ## See http:##www.salome-platform.org/ or
18 ## email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 ##
20
21 import math
22
23 from GeomAlgoAPI import GeomAlgoAPI_Circ2dBuilder as circBuilder
24 from GeomAlgoAPI import GeomAlgoAPI_CurveBuilder as curveBuilder
25 from GeomAlgoAPI import GeomAlgoAPI_EdgeBuilder as edgeBuilder
26 from GeomAlgoAPI import GeomAlgoAPI_FaceBuilder as faceBuilder
27 from GeomAlgoAPI import GeomAlgoAPI_ShapeTools as tools
28 from GeomAPI import GeomAPI_Ax3 as ax3
29 from GeomAPI import GeomAPI_Dir as dir
30 from GeomAPI import GeomAPI_Pnt as pnt
31 from GeomAPI import GeomAPI_Pnt2d as pnt2d
32 from GeomAPI import GeomAPI_Circ2d
33 from GeomAPI import GeomAPI_Shape
34
35 TOLERANCE = 1.e-7
36
37 center = pnt(10, 0, 0)
38 normal = dir(0, 0, 1)
39 xAxis = dir(1, 0, 0)
40 majorRadius = 10
41 minorRadius = 5
42
43 # Test 1. Elliptical edge
44 ellipseEdge = edgeBuilder.ellipse(center, normal, xAxis, majorRadius, minorRadius)
45 ellipse = ellipseEdge.ellipse()
46 assert(ellipse is not None)
47 assert(ellipse.center().distance(center) < TOLERANCE)
48 assert(ellipse.firstFocus().distance(pnt(18.66025404, 0., 0.)) < TOLERANCE)
49 assert(ellipse.secondFocus().distance(pnt(1.339745962, 0., 0.)) < TOLERANCE)
50 assert(ellipse.majorRadius() == majorRadius)
51 assert(ellipse.minorRadius() == minorRadius)
52
53 # Test 2. Square face
54 size = 100.
55 squareFace = faceBuilder.squareFace(center, normal, size)
56 assert(squareFace.middlePoint().distance(center) < TOLERANCE)
57
58 # Test 3. Check are of the face
59 assert(tools.area(squareFace) == size**2)
60
61 # Test 4. Check properties of wrong shapes
62 assert(tools.volume(None) == 0)
63 assert(tools.volume(GeomAPI_Shape()) == 0)
64 assert(tools.area(None) == 0)
65 assert(tools.area(GeomAPI_Shape()) == 0)
66 assert(tools.centreOfMass(None) is None)
67 assert(tools.centreOfMass(GeomAPI_Shape()) is None)
68
69 # Test 5. Curve with duplicated points
70 points = [pnt(10, 0, 0),
71           pnt(20, 10, 0),
72           pnt(20, 10, 0),
73           pnt(10, 10, 0),
74           pnt(10, 0, 0)]
75 curve = curveBuilder.edge(points, True, True, None, None)
76 assert(curve is not None)
77
78 # Test 6. Circle by center and radius
79 center = pnt2d(5, 0)
80 radius = 5
81 plane = ax3(pnt(0, 0, 0), dir(0, 0, 1), dir(1, 0, 0))
82 circleBuilder = circBuilder(plane)
83 circleBuilder.setCenter(center)
84 circleBuilder.setRadius(radius)
85 circle = circBuilder.circle(circleBuilder)
86 assert(circle is not None)
87 assert(circle.center().distance(center) < TOLERANCE)
88 assert(circle.radius() == radius)
89
90 # Test 7. Circle by 3 points
91 p1 = pnt2d(center.x() + radius, center.y())
92 p2 = pnt2d(center.x() - radius, center.y())
93 p3 = pnt2d(center.x(), center.y() + radius)
94 circle = circBuilder.circle(p1, p2, p3)
95 assert(circle is not None)
96 assert(circle.center().distance(center) < TOLERANCE)
97 assert(circle.radius() == radius)