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