1 # Copyright (C) 2014-2019 CEA/DEN, EDF R&D
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.
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.
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
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
26 from salome.shaper import geom
27 from salome.shaper import model
29 class SketchTestCase(unittest.TestCase):
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"))
42 assert(model.checkPythonDump())
43 self.session.closeAll()
45 def checkPoint(self, thePoint, theReference):
46 self.assertAlmostEqual(thePoint.x(), theReference.x(), 6)
47 self.assertAlmostEqual(thePoint.y(), theReference.y(), 6)
49 def test_circle_by_point(self):
50 """ Test 1. Create point by GeomAPI_Pnt and circle coincident by the center
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())
57 self.checkPoint(aPoint.coordinates(), point)
58 self.checkPoint(aCircle.center(), point)
60 def test_circle_by_external(self):
61 """ Test 2. Create point and circle by external features
63 aPoint = self.sketch.addPoint("PartSet/Origin")
64 aCircle = self.sketch.addCircle("[Cylinder_1_1/Face_1][Cylinder_1_1/Face_3]")
66 point = geom.Pnt2d(0., 0.)
67 self.checkPoint(aPoint.coordinates(), point)
68 self.checkPoint(aCircle.center(), point)
70 def test_circle_center_passed(self):
71 """ Test 3. Create circle by center and passed point
73 aCenter = geom.Pnt2d(10., 10.)
74 aPassed = geom.Pnt2d(20., 30.)
76 self.sketch.addCircle(aCenter.x(), aCenter.y(), aPassed.x(), aPassed.y())
78 aCircle1 = SketchAPI.SketchAPI_Circle(model.lastSubFeature(self.sketch, "SketchCircle"))
80 self.sketch.addCircle(aCenter, aPassed)
82 aCircle2 = SketchAPI.SketchAPI_Circle(model.lastSubFeature(self.sketch, "SketchCircle"))
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)
90 def test_circle_by_three_points(self):
91 """ Test 4. Create circle by three passed points
93 aCenter = geom.Pnt2d(10., 10.)
96 self.sketch.addCircle(aCenter.x() - aRadius, aCenter.y(), aCenter.x() + aRadius, aCenter.y(), aCenter.x(), aCenter.y() + aRadius)
98 aCircle = SketchAPI.SketchAPI_Circle(model.lastSubFeature(self.sketch, "SketchCircle"))
100 self.checkPoint(aCircle.center(), aCenter)
101 self.assertAlmostEqual(aCircle.radius().value(), aRadius, 6)
103 def test_arc_by_three_points(self):
104 """ Test 5. Create arc by three passed points
106 aCenter = geom.Pnt2d(10., 10.)
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)
113 self.sketch.addArc(aPoint1.x(), aPoint1.y(), aPoint2.x(), aPoint2.y(), aPoint3.x(), aPoint3.y())
115 anArc1 = SketchAPI.SketchAPI_Arc(model.lastSubFeature(self.sketch, "SketchArc"))
117 self.checkPoint(anArc1.center(), aCenter)
118 self.assertAlmostEqual(anArc1.radius().value(), aRadius, 6)
120 self.sketch.addArc(aPoint1, aPoint2, aPoint3)
122 anArc2 = SketchAPI.SketchAPI_Arc(model.lastSubFeature(self.sketch, "SketchArc"))
124 self.checkPoint(anArc2.center(), aCenter)
125 self.assertAlmostEqual(anArc2.radius().value(), aRadius, 6)
127 def test_arc_by_tangent_point(self):
128 """ Test 6. Create arc tangent to a line
130 aTgPnt = geom.Pnt2d(10., 0.)
133 aLine = self.sketch.addLine(0., 0., aTgPnt.x(), aTgPnt.y())
136 aPassed = geom.Pnt2d(aTgPnt.x(), aTgPnt.y() + 2. * aRadius)
137 self.sketch.addArc(aLine.endPoint(), aPassed, False)
139 anArc = SketchAPI.SketchAPI_Arc(model.lastSubFeature(self.sketch, "SketchArc"))
141 aCenter = geom.Pnt2d(aTgPnt.x(), aTgPnt.y() + aRadius)
142 self.checkPoint(anArc.center(), aCenter)
143 self.assertAlmostEqual(anArc.radius().value(), aRadius, 6)
145 def test_arc_by_external(self):
146 """ Test 7. Create arc by external feature
148 anArc = self.sketch.addArc(model.selection("EDGE", "[Cylinder_2_1/Face_1][Cylinder_2_1/Face_3]"))
150 point = geom.Pnt2d(0., 0.)
151 self.checkPoint(anArc.center(), point)
152 self.assertAlmostEqual(anArc.radius().value(), 10, 6)
154 def test_arc_by_name(self):
155 """ Test 8. Create arc by external feature
157 anArc = self.sketch.addArc("[Cylinder_2_1/Face_1][Cylinder_2_1/Face_3]")
159 point = geom.Pnt2d(0., 0.)
160 self.checkPoint(anArc.center(), point)
161 self.assertAlmostEqual(anArc.radius().value(), 10, 6)
163 def test_point_by_intersection(self):
164 """ Test 9. Create point as intersection with external edge given by a name
166 self.sketch.addIntersectionPoint("[Cylinder_2_1/Face_1][Cylinder_2_1/Face_4]")
168 aPoint = SketchAPI.SketchAPI_Point(model.lastSubFeature(self.sketch, "SketchPoint"))
170 point = geom.Pnt2d(10., 0.)
171 self.checkPoint(aPoint.coordinates(), point)
173 def test_arc_by_projection(self):
174 """ Test 10. Create arc by projection of external feature
176 self.sketch.addProjection("[Cylinder_2_1/Face_1][Cylinder_2_1/Face_3]")
178 anArc = SketchAPI.SketchAPI_Arc(model.lastSubFeature(self.sketch, "SketchArc"))
180 point = geom.Pnt2d(0., 0.)
181 self.checkPoint(anArc.center(), point)
182 self.assertAlmostEqual(anArc.radius().value(), 10, 6)
185 if __name__ == "__main__":
186 test_program = unittest.main(exit=False)
187 assert test_program.result.wasSuccessful(), "Test failed"