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):
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"))
41 assert(model.checkPythonDump())
42 self.session.closeAll()
44 def checkPoint(self, thePoint, theReference):
45 self.assertAlmostEqual(thePoint.x(), theReference.x(), 6)
46 self.assertAlmostEqual(thePoint.y(), theReference.y(), 6)
48 def test_circle_by_point(self):
49 """ Test 1. Create point by GeomAPI_Pnt and circle coincident by the center
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())
56 self.checkPoint(aPoint.coordinates(), point)
57 self.checkPoint(aCircle.center(), point)
59 def test_circle_by_external(self):
60 """ Test 2. Create point and circle by external features
62 aPoint = self.sketch.addPoint("PartSet/Origin")
63 aCircle = self.sketch.addCircle("[Cylinder_1_1/Face_1][Cylinder_1_1/Face_3]")
65 point = geom.Pnt2d(0., 0.)
66 self.checkPoint(aPoint.coordinates(), point)
67 self.checkPoint(aCircle.center(), point)
69 def test_circle_center_passed(self):
70 """ Test 3. Create circle by center and passed point
72 aCenter = geom.Pnt2d(10., 10.)
73 aPassed = geom.Pnt2d(20., 30.)
75 self.sketch.addCircle(aCenter.x(), aCenter.y(), aPassed.x(), aPassed.y())
77 aCircle1 = SketchAPI.SketchAPI_Circle(model.lastSubFeature(self.sketch, "SketchCircle"))
79 self.sketch.addCircle(aCenter, aPassed)
81 aCircle2 = SketchAPI.SketchAPI_Circle(model.lastSubFeature(self.sketch, "SketchCircle"))
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)
89 def test_circle_by_three_points(self):
90 """ Test 4. Create circle by three passed points
92 aCenter = geom.Pnt2d(10., 10.)
95 self.sketch.addCircle(aCenter.x() - aRadius, aCenter.y(), aCenter.x() + aRadius, aCenter.y(), aCenter.x(), aCenter.y() + aRadius)
97 aCircle = SketchAPI.SketchAPI_Circle(model.lastSubFeature(self.sketch, "SketchCircle"))
99 self.checkPoint(aCircle.center(), aCenter)
100 self.assertAlmostEqual(aCircle.radius().value(), aRadius, 6)
102 def test_arc_by_three_points(self):
103 """ Test 5. Create arc by three passed points
105 aCenter = geom.Pnt2d(10., 10.)
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)
112 self.sketch.addArc(aPoint1.x(), aPoint1.y(), aPoint2.x(), aPoint2.y(), aPoint3.x(), aPoint3.y())
114 anArc1 = SketchAPI.SketchAPI_Arc(model.lastSubFeature(self.sketch, "SketchArc"))
116 self.checkPoint(anArc1.center(), aCenter)
117 self.assertAlmostEqual(anArc1.radius().value(), aRadius, 6)
119 self.sketch.addArc(aPoint1, aPoint2, aPoint3)
121 anArc2 = SketchAPI.SketchAPI_Arc(model.lastSubFeature(self.sketch, "SketchArc"))
123 self.checkPoint(anArc2.center(), aCenter)
124 self.assertAlmostEqual(anArc2.radius().value(), aRadius, 6)
126 def test_arc_by_tangent_point(self):
127 """ Test 6. Create arc tangent to a line
129 aTgPnt = geom.Pnt2d(10., 0.)
132 aLine = self.sketch.addLine(0., 0., aTgPnt.x(), aTgPnt.y())
135 aPassed = geom.Pnt2d(aTgPnt.x(), aTgPnt.y() + 2. * aRadius)
136 self.sketch.addArc(aLine.endPoint(), aPassed, False)
138 anArc = SketchAPI.SketchAPI_Arc(model.lastSubFeature(self.sketch, "SketchArc"))
140 aCenter = geom.Pnt2d(aTgPnt.x(), aTgPnt.y() + aRadius)
141 self.checkPoint(anArc.center(), aCenter)
142 self.assertAlmostEqual(anArc.radius().value(), aRadius, 6)
144 def test_arc_by_external(self):
145 """ Test 7. Create arc by external feature
147 anArc = self.sketch.addArc(model.selection("EDGE", "[Cylinder_2_1/Face_1][Cylinder_2_1/Face_3]"))
149 point = geom.Pnt2d(0., 0.)
150 self.checkPoint(anArc.center(), point)
151 self.assertAlmostEqual(anArc.radius().value(), 10, 6)
153 def test_arc_by_name(self):
154 """ Test 8. Create arc by external feature
156 anArc = self.sketch.addArc("[Cylinder_2_1/Face_1][Cylinder_2_1/Face_3]")
158 point = geom.Pnt2d(0., 0.)
159 self.checkPoint(anArc.center(), point)
160 self.assertAlmostEqual(anArc.radius().value(), 10, 6)
162 def test_point_by_intersection(self):
163 """ Test 9. Create point as intersection with external edge given by a name
165 self.sketch.addIntersectionPoint("[Cylinder_2_1/Face_1][Cylinder_2_1/Face_4]")
167 aPoint = SketchAPI.SketchAPI_Point(model.lastSubFeature(self.sketch, "SketchPoint"))
169 point = geom.Pnt2d(10., 0.)
170 self.checkPoint(aPoint.coordinates(), point)
172 def test_arc_by_projection(self):
173 """ Test 10. Create arc by projection of external feature
175 self.sketch.addProjection("[Cylinder_2_1/Face_1][Cylinder_2_1/Face_3]")
177 anArc = SketchAPI.SketchAPI_Arc(model.lastSubFeature(self.sketch, "SketchArc"))
179 point = geom.Pnt2d(0., 0.)
180 self.checkPoint(anArc.center(), point)
181 self.assertAlmostEqual(anArc.radius().value(), 10, 6)
184 if __name__ == "__main__":
185 test_program = unittest.main(exit=False)
186 assert test_program.result.wasSuccessful(), "Test failed"