Salome HOME
Update copyrights
[modules/shaper.git] / src / SketchAPI / Test / TestSketch.py
1 # Copyright (C) 2014-2019  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 import unittest
22
23 import ModelAPI
24 import SketchAPI
25
26 from salome.shaper import geom
27 from salome.shaper import model
28
29 class SketchTestCase(unittest.TestCase):
30
31     def setUp(self):
32         self.session = ModelAPI.ModelAPI_Session.get()
33         aPartSet = self.session.moduleDocument()
34         self.doc = model.addPart(aPartSet).document()
35         model.addCylinder(self.doc, model.selection("VERTEX", "PartSet/Origin"), model.selection("EDGE", "PartSet/OZ"), 5, 10)
36         model.addCylinder(self.doc, model.selection("VERTEX", "PartSet/Origin"), model.selection("EDGE", "PartSet/OZ"), 10, 10, 90)
37         self.sketch = model.addSketch(self.doc, model.defaultPlane("XOY"))
38
39     def tearDown(self):
40         model.end()
41         assert(model.checkPythonDump())
42         self.session.closeAll()
43
44     def checkPoint(self, thePoint, theReference):
45         self.assertAlmostEqual(thePoint.x(), theReference.x(), 6)
46         self.assertAlmostEqual(thePoint.y(), theReference.y(), 6)
47
48     def test_circle_by_point(self):
49         """ Test 1. Create point by GeomAPI_Pnt and circle coincident by the center
50         """
51         point = geom.Pnt2d(10., 10.)
52         aPoint = self.sketch.addPoint(point)
53         aCircle = self.sketch.addCircle(point, 10.)
54         aCoincidence = self.sketch.setCoincident(aPoint.coordinates(), aCircle.center())
55         model.do()
56         self.checkPoint(aPoint.coordinates(), point)
57         self.checkPoint(aCircle.center(), point)
58
59     def test_circle_by_external(self):
60         """ Test 2. Create point and circle by external features
61         """
62         aPoint = self.sketch.addPoint("PartSet/Origin")
63         aCircle = self.sketch.addCircle("[Cylinder_1_1/Face_1][Cylinder_1_1/Face_3]")
64         model.do()
65         point = geom.Pnt2d(0., 0.)
66         self.checkPoint(aPoint.coordinates(), point)
67         self.checkPoint(aCircle.center(), point)
68
69     def test_circle_center_passed(self):
70         """ Test 3. Create circle by center and passed point
71         """
72         aCenter = geom.Pnt2d(10., 10.)
73         aPassed = geom.Pnt2d(20., 30.)
74
75         self.sketch.addCircle(aCenter.x(), aCenter.y(), aPassed.x(), aPassed.y())
76         model.do()
77         aCircle1 = SketchAPI.SketchAPI_Circle(model.lastSubFeature(self.sketch, "SketchCircle"))
78
79         self.sketch.addCircle(aCenter, aPassed)
80         model.do()
81         aCircle2 = SketchAPI.SketchAPI_Circle(model.lastSubFeature(self.sketch, "SketchCircle"))
82
83         aRadius = math.sqrt((aCenter.x()-aPassed.x())**2 + (aCenter.y()-aPassed.y())**2)
84         self.checkPoint(aCircle1.center(), aCenter)
85         self.assertAlmostEqual(aCircle1.radius().value(), aRadius, 6)
86         self.checkPoint(aCircle2.center(), aCenter)
87         self.assertAlmostEqual(aCircle2.radius().value(), aRadius, 6)
88
89     def test_circle_by_three_points(self):
90         """ Test 4. Create circle by three passed points
91         """
92         aCenter = geom.Pnt2d(10., 10.)
93         aRadius = 5.
94
95         self.sketch.addCircle(aCenter.x() - aRadius, aCenter.y(), aCenter.x() + aRadius, aCenter.y(), aCenter.x(), aCenter.y() + aRadius)
96         model.do()
97         aCircle = SketchAPI.SketchAPI_Circle(model.lastSubFeature(self.sketch, "SketchCircle"))
98
99         self.checkPoint(aCircle.center(), aCenter)
100         self.assertAlmostEqual(aCircle.radius().value(), aRadius, 6)
101
102     def test_arc_by_three_points(self):
103         """ Test 5. Create arc by three passed points
104         """
105         aCenter = geom.Pnt2d(10., 10.)
106         aRadius = 5.
107
108         aPoint1 = geom.Pnt2d(aCenter.x() - aRadius, aCenter.y())
109         aPoint2 = geom.Pnt2d(aCenter.x() + aRadius, aCenter.y())
110         aPoint3 = geom.Pnt2d(aCenter.x(), aCenter.y() + aRadius)
111
112         self.sketch.addArc(aPoint1.x(), aPoint1.y(), aPoint2.x(), aPoint2.y(), aPoint3.x(), aPoint3.y())
113         model.do()
114         anArc1 = SketchAPI.SketchAPI_Arc(model.lastSubFeature(self.sketch, "SketchArc"))
115
116         self.checkPoint(anArc1.center(), aCenter)
117         self.assertAlmostEqual(anArc1.radius().value(), aRadius, 6)
118
119         self.sketch.addArc(aPoint1, aPoint2, aPoint3)
120         model.do()
121         anArc2 = SketchAPI.SketchAPI_Arc(model.lastSubFeature(self.sketch, "SketchArc"))
122
123         self.checkPoint(anArc2.center(), aCenter)
124         self.assertAlmostEqual(anArc2.radius().value(), aRadius, 6)
125
126     def test_arc_by_tangent_point(self):
127         """ Test 6. Create arc tangent to a line
128         """
129         aTgPnt = geom.Pnt2d(10., 0.)
130         aRadius = 5.
131
132         aLine = self.sketch.addLine(0., 0., aTgPnt.x(), aTgPnt.y())
133         model.do()
134
135         aPassed = geom.Pnt2d(aTgPnt.x(), aTgPnt.y() + 2. * aRadius)
136         self.sketch.addArc(aLine.endPoint(), aPassed, False)
137         model.do()
138         anArc = SketchAPI.SketchAPI_Arc(model.lastSubFeature(self.sketch, "SketchArc"))
139
140         aCenter = geom.Pnt2d(aTgPnt.x(), aTgPnt.y() + aRadius)
141         self.checkPoint(anArc.center(), aCenter)
142         self.assertAlmostEqual(anArc.radius().value(), aRadius, 6)
143
144     def test_arc_by_external(self):
145         """ Test 7. Create arc by external feature
146         """
147         anArc = self.sketch.addArc(model.selection("EDGE", "[Cylinder_2_1/Face_1][Cylinder_2_1/Face_3]"))
148         model.do()
149         point = geom.Pnt2d(0., 0.)
150         self.checkPoint(anArc.center(), point)
151         self.assertAlmostEqual(anArc.radius().value(), 10, 6)
152
153     def test_arc_by_name(self):
154         """ Test 8. Create arc by external feature
155         """
156         anArc = self.sketch.addArc("[Cylinder_2_1/Face_1][Cylinder_2_1/Face_3]")
157         model.do()
158         point = geom.Pnt2d(0., 0.)
159         self.checkPoint(anArc.center(), point)
160         self.assertAlmostEqual(anArc.radius().value(), 10, 6)
161
162     def test_point_by_intersection(self):
163         """ Test 9. Create point as intersection with external edge given by a name
164         """
165         self.sketch.addIntersectionPoint("[Cylinder_2_1/Face_1][Cylinder_2_1/Face_4]")
166         model.do()
167         aPoint = SketchAPI.SketchAPI_Point(model.lastSubFeature(self.sketch, "SketchPoint"))
168
169         point = geom.Pnt2d(10., 0.)
170         self.checkPoint(aPoint.coordinates(), point)
171
172     def test_arc_by_projection(self):
173         """ Test 10. Create arc by projection of external feature
174         """
175         self.sketch.addProjection("[Cylinder_2_1/Face_1][Cylinder_2_1/Face_3]")
176         model.do()
177         anArc = SketchAPI.SketchAPI_Arc(model.lastSubFeature(self.sketch, "SketchArc"))
178
179         point = geom.Pnt2d(0., 0.)
180         self.checkPoint(anArc.center(), point)
181         self.assertAlmostEqual(anArc.radius().value(), 10, 6)
182
183
184 if __name__ == "__main__":
185     test_program = unittest.main(exit=False)
186     assert test_program.result.wasSuccessful(), "Test failed"