From: Renaud NEDELEC Date: Mon, 19 Oct 2015 13:50:17 +0000 (+0200) Subject: [PythonAPI / sketcher] added addArc method and the corresponding test + fix for point API X-Git-Tag: V_2.1.0~206^2~90 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=bfc294ef2ff4c055570ee056850999e37dfc5fb7;p=modules%2Fshaper.git [PythonAPI / sketcher] added addArc method and the corresponding test + fix for point API --- diff --git a/src/PythonAPI/CMakeLists.txt b/src/PythonAPI/CMakeLists.txt index 534588452..300e74f4d 100644 --- a/src/PythonAPI/CMakeLists.txt +++ b/src/PythonAPI/CMakeLists.txt @@ -10,7 +10,9 @@ INCLUDE(UnitTest) ADD_UNIT_TESTS( TestModeler.py + TestSketcherAddPoint.py TestSketcherAddLine.py + TestSketcherAddArc.py TestSketcherAddCircle.py TestSketcherSetCoincident.py TestSketcherSetParallel.py diff --git a/src/PythonAPI/Test/TestSketcherAddArc.py b/src/PythonAPI/Test/TestSketcherAddArc.py new file mode 100644 index 000000000..53c07cfff --- /dev/null +++ b/src/PythonAPI/Test/TestSketcherAddArc.py @@ -0,0 +1,32 @@ +import unittest + +import modeler +import geom +from modeler import WrongNumberOfArguments + +from TestSketcher import SketcherTestCase + +class SketcherAddArc(SketcherTestCase): + def test_arc_by_coords(self): + arc = self.sketch.addArc(0, 1, 0, 0, 1, 1) + self.assertEqual(arc.startPointData().x(), 0) + self.assertEqual(arc.startPointData().y(), 0) + + def test_arc_by_points(self): + center = geom.Pnt2d(0, 1) + start = geom.Pnt2d(0, 0) + end = geom.Pnt2d(0, 1) + arc = self.sketch.addArc(center, start, end) + self.assertEqual(arc.startPointData().x(), 0) + self.assertEqual(arc.startPointData().y(), 0) + + def test_number_of_args(self): + with self.assertRaises(WrongNumberOfArguments): + self.sketch.addArc(0, 1, 1, 1) + with self.assertRaises(WrongNumberOfArguments): + self.sketch.addArc(0, 1) + + +if __name__ == "__main__": + suite = unittest.TestLoader().loadTestsFromTestCase(SketcherAddArc) + unittest.TextTestRunner(verbosity=2).run(suite) \ No newline at end of file diff --git a/src/PythonAPI/Test/TestSketcherAddPoint.py b/src/PythonAPI/Test/TestSketcherAddPoint.py new file mode 100644 index 000000000..8ebdb2314 --- /dev/null +++ b/src/PythonAPI/Test/TestSketcherAddPoint.py @@ -0,0 +1,13 @@ +import unittest +import modeler +from TestSketcher import SketcherTestCase + +class SketcherAddPoint(SketcherTestCase): + def runTest(self): + point = self.sketch.addPoint(0, 1) + self.assertEqual(point.pointData().x(), 0.0) + self.assertEqual(point.pointData().y(), 1.0) + + +if __name__ == "__main__": + unittest.main() \ No newline at end of file diff --git a/src/PythonAPI/modeler/__init__.py b/src/PythonAPI/modeler/__init__.py index 76ca85c59..a84a16515 100644 --- a/src/PythonAPI/modeler/__init__.py +++ b/src/PythonAPI/modeler/__init__.py @@ -14,3 +14,7 @@ from extrusion import Extrusion as addExtrusion from boolean import Addition as addAddition from boolean import Subtraction as addSubtraction from boolean import Intersection as addIntersection + +# Custom exceptions + +from errors import WrongNumberOfArguments \ No newline at end of file diff --git a/src/PythonAPI/modeler/errors.py b/src/PythonAPI/modeler/errors.py new file mode 100644 index 000000000..642af0a51 --- /dev/null +++ b/src/PythonAPI/modeler/errors.py @@ -0,0 +1,18 @@ +# Package exceptions + +class ModelerError(Exception): + """Base class for exceptions in this package.""" + pass + +class WrongNumberOfArguments(ModelerError): + """Exception raised when a wrong number of arguments is given.""" + pass + + #Attributes: + #expr -- input expression in which the error occurred + #msg -- explanation of the error + #""" + + #def __init__(self, expr, msg): + #self.expr = expr + #self.msg = msg diff --git a/src/PythonAPI/modeler/sketcher/arc.py b/src/PythonAPI/modeler/sketcher/arc.py new file mode 100644 index 000000000..853ceb6c9 --- /dev/null +++ b/src/PythonAPI/modeler/sketcher/arc.py @@ -0,0 +1,65 @@ +"""Sketch circle feature interface.""" + +from GeomDataAPI import geomDataAPI_Point2D +from modeler.errors import WrongNumberOfArguments + +class Arc(): + def __init__(self, arc_feature, *args): + self._feature = arc_feature + self._center = geomDataAPI_Point2D( + self._feature.data().attribute("ArcCenter") + ) + self._start_point = geomDataAPI_Point2D( + self._feature.data().attribute("ArcStartPoint") + ) + self._end_point = geomDataAPI_Point2D( + self._feature.data().attribute("ArcEndPoint") + ) + if len(args) == 6: + self.__createByCoordinates(*args) + elif len(args) == 3: + self.__createByPoints(*args) + else: + raise WrongNumberOfArguments( + "Arc takes 3 or 6 arguments (%s given)" % len(args) + ) + + def centerData (self): + return self._center + + def startPointData (self): + return self._start_point + + def endPointData (self): + return self._end_point + + def result (self): + 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() + + 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() + + + + + diff --git a/src/PythonAPI/modeler/sketcher/sketch.py b/src/PythonAPI/modeler/sketcher/sketch.py index 1ec03ca03..5e9c1d5d3 100644 --- a/src/PythonAPI/modeler/sketcher/sketch.py +++ b/src/PythonAPI/modeler/sketcher/sketch.py @@ -9,6 +9,7 @@ from GeomAlgoAPI import * from modeler.sketcher.point import Point from modeler.sketcher.line import Line from modeler.sketcher.circle import Circle +from modeler.sketcher.arc import Arc def addSketch(doc, plane): """Add a Sketch feature to the Part or PartSet and return an interface @@ -71,6 +72,11 @@ class Sketch(): """Add a circle to this Sketch.""" circle_feature = self._feature.addFeature("SketchCircle") return Circle(circle_feature, *args) + + def addArc (self, *args): + """Add an arc to this Sketch.""" + arc_feature = self._feature.addFeature("SketchArc") + return Arc(arc_feature, *args) def addPolyline (self, *coords): """Adds a poly-line to this Sketch. diff --git a/src/SketchPlugin/SketchPlugin_Point.h b/src/SketchPlugin/SketchPlugin_Point.h index 59995ff43..1e1a4f65c 100644 --- a/src/SketchPlugin/SketchPlugin_Point.h +++ b/src/SketchPlugin/SketchPlugin_Point.h @@ -28,7 +28,7 @@ class SketchPlugin_Point : public SketchPlugin_SketchEntity /// Coordinates of the point inline static const std::string& COORD_ID() { - static const std::string MY_COORD_ID("PointCoordindates"); + static const std::string MY_COORD_ID("PointCoordinates"); return MY_COORD_ID; } /// Returns the kind of a feature