From 07683d9c10e21258e1379f356ca7c40cbc56f404 Mon Sep 17 00:00:00 2001 From: Renaud NEDELEC Date: Mon, 2 Nov 2015 16:00:27 +0100 Subject: [PATCH] [PythonAPI / sketcher] Added methods for sketch features modification --- src/PythonAPI/Test/TestSketcherAddArc.py | 15 ++++++- src/PythonAPI/Test/TestSketcherAddCircle.py | 14 +++++- src/PythonAPI/Test/TestSketcherAddLine.py | 22 +++++++--- src/PythonAPI/Test/TestSketcherAddPoint.py | 7 +++ src/PythonAPI/model/sketcher/arc.py | 43 +++++++++++++++---- src/PythonAPI/model/sketcher/circle.py | 14 ++++-- src/PythonAPI/model/sketcher/line.py | 47 +++++++++++++-------- src/PythonAPI/model/sketcher/point.py | 13 +++--- src/PythonAPI/model/sketcher/sketch.py | 1 + 9 files changed, 131 insertions(+), 45 deletions(-) diff --git a/src/PythonAPI/Test/TestSketcherAddArc.py b/src/PythonAPI/Test/TestSketcherAddArc.py index 22c43b826..9b433f0fd 100644 --- a/src/PythonAPI/Test/TestSketcherAddArc.py +++ b/src/PythonAPI/Test/TestSketcherAddArc.py @@ -16,7 +16,7 @@ class SketcherAddArc(SketcherTestCase): def test_arc_by_points(self): center = geom.Pnt2d(0, 1) start = geom.Pnt2d(0, 0) - end = geom.Pnt2d(0, 1) + end = geom.Pnt2d(1, 1) arc = self.sketch.addArc(center, start, end) model.do() self.assertEqual(arc.startPointData().x(), 0) @@ -27,6 +27,19 @@ class SketcherAddArc(SketcherTestCase): self.sketch.addArc(0, 1, 1, 1) with self.assertRaises(WrongNumberOfArguments): self.sketch.addArc(0, 1) + + def test_modify_arc(self): + arc = self.sketch.addArc(0, 1, 0, 0, 1, 1) + arc.setCenter(0, 0) + arc.setStartPoint(-1.0, 0) + arc.setEndPoint(0, 1.0) + model.do() + self.assertEqual(arc.centerData().x(), 0) + self.assertEqual(arc.centerData().y(), 0) + self.assertEqual(arc.startPointData().x(), -1) + self.assertEqual(arc.startPointData().y(), 0) + self.assertEqual(arc.endPointData().x(), 0) + self.assertEqual(arc.endPointData().y(), 1) if __name__ == "__main__": diff --git a/src/PythonAPI/Test/TestSketcherAddCircle.py b/src/PythonAPI/Test/TestSketcherAddCircle.py index 48b15b0d3..932bb6881 100644 --- a/src/PythonAPI/Test/TestSketcherAddCircle.py +++ b/src/PythonAPI/Test/TestSketcherAddCircle.py @@ -3,13 +3,23 @@ import model from TestSketcher import SketcherTestCase class SketcherAddCircle(SketcherTestCase): - def runTest(self): + def test_add_cricle(self): circle = self.sketch.addCircle(0, 10, 20) model.do() self.assertEqual(circle.centerData().x(), 0.0) self.assertEqual(circle.centerData().y(), 10.0) self.assertEqual(circle.radiusData().value(), 20.0) + def test_modify_circle(self): + circle = self.sketch.addCircle(0, 10, 20) + model.do() + circle.setCenter(10, 10) + circle.setRadius(30) + model.do() + self.assertEqual(circle.centerData().x(), 10.0) + self.assertEqual(circle.centerData().y(), 10.0) + self.assertEqual(circle.radiusData().value(), 30.0) + if __name__ == "__main__": - unittest.main() \ No newline at end of file + unittest.main(verbosity=2) \ No newline at end of file diff --git a/src/PythonAPI/Test/TestSketcherAddLine.py b/src/PythonAPI/Test/TestSketcherAddLine.py index 2c9254633..fa4ed94c6 100644 --- a/src/PythonAPI/Test/TestSketcherAddLine.py +++ b/src/PythonAPI/Test/TestSketcherAddLine.py @@ -3,12 +3,22 @@ import model from TestSketcher import SketcherTestCase class SketcherAddLine(SketcherTestCase): - def runTest(self): + def test_add_line(self): line = self.sketch.addLine(0, 0, 0, 1) model.do() - self.assertEqual(line.startPointData().x(), line.endPointData().x()) - self.assertNotEqual(line.startPointData().y(), line.endPointData().y()) - - + self.assertEqual(line.getStartPoint().x(), line.getEndPoint().x()) + self.assertNotEqual(line.getStartPoint().y(), line.getEndPoint().y()) + + def test_modify_line(self): + line = self.sketch.addLine(0, 0, 0, 1) + model.do() + line.setStartPoint(0, 1) + line.setEndPoint(1, 1) + self.assertEqual(line.getStartPoint().x(), 0) + self.assertEqual(line.getStartPoint().y(), 1) + self.assertEqual(line.getEndPoint().x(), 1) + self.assertEqual(line.getEndPoint().y(), 1) + + if __name__ == "__main__": - unittest.main() \ No newline at end of file + unittest.main(verbosity=2) \ No newline at end of file diff --git a/src/PythonAPI/Test/TestSketcherAddPoint.py b/src/PythonAPI/Test/TestSketcherAddPoint.py index e7abc535f..198887b0c 100644 --- a/src/PythonAPI/Test/TestSketcherAddPoint.py +++ b/src/PythonAPI/Test/TestSketcherAddPoint.py @@ -9,6 +9,13 @@ class SketcherAddPoint(SketcherTestCase): self.assertEqual(point.pointData().x(), 0.0) self.assertEqual(point.pointData().y(), 1.0) + def test_modify_point(self): + point = self.sketch.addPoint(0, 1) + point.setValue(1, 2) + model.do() + self.assertEqual(point.pointData().x(), 1.0) + self.assertEqual(point.pointData().y(), 2.0) + if __name__ == "__main__": unittest.main(verbosity=2) \ No newline at end of file diff --git a/src/PythonAPI/model/sketcher/arc.py b/src/PythonAPI/model/sketcher/arc.py index d685befe4..9bac67762 100644 --- a/src/PythonAPI/model/sketcher/arc.py +++ b/src/PythonAPI/model/sketcher/arc.py @@ -27,6 +27,33 @@ class Arc(Interface): raise WrongNumberOfArguments( "Arc takes 3 or 6 arguments (%s given)" % len(args) ) + self._execute() + + ######## + # + # Set methods + # + ######## + + def setCenter(self, x, y): + """Set arc center.""" + self._center.setValue(x, y) + + def setStartPoint(self, x, y): + """Set start point.""" + self._start_point.setValue(x, y) + + def setEndPoint(self, x, y): + """Set end point value.""" + self._end_point.setValue(x, y) + + + ######## + # + # Getters + # + ######## + def centerData(self): """Return the center point data.""" @@ -44,24 +71,24 @@ class Arc(Interface): """Return the arc circular line attribute.""" return self._feature.lastResult() + ######## # # Private methods # ######## + def __createByCoordinates(self, center_x, center_y, start_x, start_y, end_x, end_y): """Create an arc by point coordinates.""" - self._center.setValue(center_x, center_y) - self._start_point.setValue(start_x, start_y) - self._end_point.setValue(end_x, end_y) - self._feature.execute() + self.setCenter(center_x, center_y) + self.setStartPoint(start_x, start_y) + self.setEndPoint(end_x, end_y) def __createByPoints(self, center, start, end): """Create an arc with point objects.""" - self._center.setValue(center.x(), center.y()) - self._start_point.setValue(start.x(), start.y()) - self._end_point.setValue(end.x(), end.y()) - self._feature.execute() + self.setCenter(center.x(), center.y()) + self.setStartPoint(start.x(), start.y()) + self.setEndPoint(end.x(), end.y()) diff --git a/src/PythonAPI/model/sketcher/circle.py b/src/PythonAPI/model/sketcher/circle.py index 1fed091e6..b6ce45e8a 100644 --- a/src/PythonAPI/model/sketcher/circle.py +++ b/src/PythonAPI/model/sketcher/circle.py @@ -5,7 +5,7 @@ from model.roots import Interface class Circle(Interface): """Interface for circle feature data manipulation.""" - def __init__(self, feature, x, y, r): + def __init__(self, feature, x, y, radius): Interface.__init__(self, feature) assert(self._feature.getKind() == "SketchCircle") @@ -13,10 +13,18 @@ class Circle(Interface): self._feature.data().attribute("CircleCenter") ) self._radius = self._feature.data().real("CircleRadius") - self._center.setValue(x, y) - self._radius.setValue(r) + self.setCenter(x, y) + self.setRadius(radius) self.execute() + def setCenter(self, x, y): + """Set the center of the circle.""" + self._center.setValue(x, y) + + def setRadius(self, radius): + """Set the radius of the circle.""" + self._radius.setValue(radius) + def centerData(self): """Return center data.""" return self._center diff --git a/src/PythonAPI/model/sketcher/line.py b/src/PythonAPI/model/sketcher/line.py index 2c2113244..eb8d735e0 100644 --- a/src/PythonAPI/model/sketcher/line.py +++ b/src/PythonAPI/model/sketcher/line.py @@ -16,14 +16,12 @@ class Line(Interface): self._feature.data().attribute("EndPoint") ) - assert(self._start_point) - assert(self._end_point) - - if not args: - return - - assert(len(args) in (1, 2, 4)) - + # Check that input arguments are not None + if args is None: + raise WrongNumberOfArguments( + "No argument given, at least one argument needed" + ) + # Set attribute values and execute if len(args) == 4: self.__createByCoordinates(*args) @@ -31,24 +29,39 @@ class Line(Interface): self.__createByPoints(*args) elif len(args) == 1: self.__createByName(*args) - + else: + raise WrongNumberOfArguments( + "Arc takes 1, 2 or 4 arguments (%s given)" % len(args) + ) self._execute() - pass def __createByCoordinates(self, x1, y1, x2, y2): - self._start_point.setValue(x1, y1) - self._end_point.setValue(x2, y2) - pass + self.setStartPoint(x1, y1) + self.setEndPoint(x2, y2) def __createByPoints(self, p1, p2): - self._start_point.setValue(p1.x(), p1.y()) - self._end_point.setValue(p2.x(), p2.y()) - pass + self.setStartPoint(p1.x(), p1.y()) + self.setEndPoint(p2.x(), p2.y()) def __createByName(self, name): self._feature.data().selection("External").selectSubShape("EDGE", name) - pass + + ####### + # + # Set methods + # + ####### + + def setStartPoint(self, x, y): + """Set the start point of the line.""" + self._start_point.setValue(x, y) + + def setEndPoint(self, x, y): + """Set the end point of the line.""" + self._end_point.setValue(x, y) + # TODO : methods below will be removed. + # Kept until all tests have been updated def startPointData(self): return self._start_point diff --git a/src/PythonAPI/model/sketcher/point.py b/src/PythonAPI/model/sketcher/point.py index 69b9f88fc..3a65acea5 100644 --- a/src/PythonAPI/model/sketcher/point.py +++ b/src/PythonAPI/model/sketcher/point.py @@ -14,15 +14,12 @@ class Point(Interface): self._point_data = geomDataAPI_Point2D( self._feature.data().attribute("PointCoordinates") ) + self.setValue(x, y) + self._execute() + + def setValue(self, x, y): + """Set point coordinates.""" self._point_data.setValue(x, y) - - # Control input and execute - if self.areInputValid(): - self.execute() - else: - raise FeatureInputInvalid( - "cannot create the Sketch Point, the input is invalid" - ) def pointData (self): """Return the point data.""" diff --git a/src/PythonAPI/model/sketcher/sketch.py b/src/PythonAPI/model/sketcher/sketch.py index a47759071..04f603031 100644 --- a/src/PythonAPI/model/sketcher/sketch.py +++ b/src/PythonAPI/model/sketcher/sketch.py @@ -1,5 +1,6 @@ """Sketch Feature Interface Author: Daniel Brunier-Coulin with contribution by Mikhail Ponikarov + finalized by Renaud Nedelec and Sergey Pokhodenko Copyright (C) 2014-20xx CEA/DEN, EDF R&D """ -- 2.39.2