Salome HOME
Copyright update 2022
[modules/shaper.git] / src / GeomAPI / Test / TestBuilders.py
1 # Copyright (C) 2018-2022  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 email : webmaster.salome@opencascade.com
18 #
19
20 import math
21
22 from GeomAlgoAPI import GeomAlgoAPI_Circ2dBuilder as circBuilder
23 from GeomAlgoAPI import GeomAlgoAPI_CurveBuilder as curveBuilder
24 from GeomAlgoAPI import GeomAlgoAPI_EdgeBuilder as edgeBuilder
25 from GeomAlgoAPI import GeomAlgoAPI_FaceBuilder as faceBuilder
26 from GeomAlgoAPI import GeomAlgoAPI_ShapeTools as tools
27 from GeomAPI import GeomAPI_Ax3 as ax3
28 from GeomAPI import GeomAPI_Dir as dir
29 from GeomAPI import GeomAPI_Pnt as pnt
30 from GeomAPI import GeomAPI_Pnt2d as pnt2d
31 from GeomAPI import GeomAPI_Circ2d
32 from GeomAPI import GeomAPI_Shape
33
34 TOLERANCE = 1.e-7
35
36 center = pnt(10, 0, 0)
37 normal = dir(0, 0, 1)
38 xAxis = dir(1, 0, 0)
39 majorRadius = 10
40 minorRadius = 5
41
42 # Test 1. Elliptical edge
43 ellipseEdge = edgeBuilder.ellipse(center, normal, xAxis, majorRadius, minorRadius)
44 ellipse = ellipseEdge.ellipse()
45 assert(ellipse is not None)
46 assert(ellipse.center().distance(center) < TOLERANCE)
47 assert(ellipse.firstFocus().distance(pnt(18.66025404, 0., 0.)) < TOLERANCE)
48 assert(ellipse.secondFocus().distance(pnt(1.339745962, 0., 0.)) < TOLERANCE)
49 assert(ellipse.majorRadius() == majorRadius)
50 assert(ellipse.minorRadius() == minorRadius)
51
52 # Test 2. Square face
53 size = 100.
54 squareFace = faceBuilder.squareFace(center, normal, size)
55 assert(squareFace.middlePoint().distance(center) < TOLERANCE)
56
57 # Test 3. Check are of the face
58 assert(tools.area(squareFace) == size**2)
59
60 # Test 4. Check properties of wrong shapes
61 assert(tools.volume(None) == 0)
62 assert(tools.volume(GeomAPI_Shape()) == 0)
63 assert(tools.area(None) == 0)
64 assert(tools.area(GeomAPI_Shape()) == 0)
65 assert(tools.centreOfMass(None) is None)
66 assert(tools.centreOfMass(GeomAPI_Shape()) is None)
67
68 # Test 5. Curve with duplicated points
69 points = [pnt(10, 0, 0),
70           pnt(20, 10, 0),
71           pnt(20, 10, 0),
72           pnt(10, 10, 0),
73           pnt(10, 0, 0)]
74 curve = curveBuilder.edge(points, True, True, None, None)
75 assert(curve is not None)
76
77 # Test 6. Circle by center and radius
78 center = pnt2d(5, 0)
79 radius = 5
80 plane = ax3(pnt(0, 0, 0), dir(0, 0, 1), dir(1, 0, 0))
81 circleBuilder = circBuilder(plane)
82 circleBuilder.setCenter(center)
83 circleBuilder.setRadius(radius)
84 circle = circBuilder.circle(circleBuilder)
85 assert(circle is not None)
86 assert(circle.center().distance(center) < TOLERANCE)
87 assert(circle.radius() == radius)
88
89 # Test 7. Circle by 3 points
90 p1 = pnt2d(center.x() + radius, center.y())
91 p2 = pnt2d(center.x() - radius, center.y())
92 p3 = pnt2d(center.x(), center.y() + radius)
93 circle = circBuilder.circle(p1, p2, p3)
94 assert(circle is not None)
95 assert(circle.center().distance(center) < TOLERANCE)
96 assert(circle.radius() == radius)