From 68e65b42c86cee2d94e26ab00072c9f89df24e6f Mon Sep 17 00:00:00 2001 From: Renaud NEDELEC Date: Thu, 22 Oct 2015 16:18:06 +0200 Subject: [PATCH] [PythonAPI] derived sketcher interfaces from Interface base class + small modifications in tests --- src/PythonAPI/Test/TestSketcherAddArc.py | 3 +-- src/PythonAPI/Test/TestSketcherAddPoint.py | 4 ++-- src/PythonAPI/model/__init__.py | 3 ++- src/PythonAPI/model/errors.py | 4 ++++ src/PythonAPI/model/roots.py | 8 ++++---- src/PythonAPI/model/sketcher/arc.py | 11 +++++++---- src/PythonAPI/model/sketcher/circle.py | 12 +++++++----- src/PythonAPI/model/sketcher/line.py | 16 ++++++++++++---- src/PythonAPI/model/sketcher/point.py | 22 +++++++++++++++++----- src/PythonAPI/model/sketcher/sketch.py | 7 +++++-- 10 files changed, 61 insertions(+), 29 deletions(-) diff --git a/src/PythonAPI/Test/TestSketcherAddArc.py b/src/PythonAPI/Test/TestSketcherAddArc.py index 1b64c4465..22c43b826 100644 --- a/src/PythonAPI/Test/TestSketcherAddArc.py +++ b/src/PythonAPI/Test/TestSketcherAddArc.py @@ -30,5 +30,4 @@ class SketcherAddArc(SketcherTestCase): if __name__ == "__main__": - suite = unittest.TestLoader().loadTestsFromTestCase(SketcherAddArc) - unittest.TextTestRunner(verbosity=2).run(suite) \ 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 783f8c17b..e7abc535f 100644 --- a/src/PythonAPI/Test/TestSketcherAddPoint.py +++ b/src/PythonAPI/Test/TestSketcherAddPoint.py @@ -3,7 +3,7 @@ import model from TestSketcher import SketcherTestCase class SketcherAddPoint(SketcherTestCase): - def runTest(self): + def test_add_point(self): point = self.sketch.addPoint(0, 1) model.do() self.assertEqual(point.pointData().x(), 0.0) @@ -11,4 +11,4 @@ class SketcherAddPoint(SketcherTestCase): 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/model/__init__.py b/src/PythonAPI/model/__init__.py index 28faea654..0e28cac49 100644 --- a/src/PythonAPI/model/__init__.py +++ b/src/PythonAPI/model/__init__.py @@ -17,4 +17,5 @@ from boolean import Intersection as addIntersection # Custom exceptions -from errors import WrongNumberOfArguments \ No newline at end of file +from errors import WrongNumberOfArguments +from errors import FeatureInputInvalid diff --git a/src/PythonAPI/model/errors.py b/src/PythonAPI/model/errors.py index b848f6058..e10ec92ee 100644 --- a/src/PythonAPI/model/errors.py +++ b/src/PythonAPI/model/errors.py @@ -8,6 +8,10 @@ class WrongNumberOfArguments(ModelError): """Exception raised when a wrong number of arguments is given.""" pass +class FeatureInputInvalid(ModelError): + """Exception raised if a feature input is invalid.""" + pass + #Attributes: #expr -- input expression in which the error occurred #msg -- explanation of the error diff --git a/src/PythonAPI/model/roots.py b/src/PythonAPI/model/roots.py index b721821b4..0cf8ae274 100644 --- a/src/PythonAPI/model/roots.py +++ b/src/PythonAPI/model/roots.py @@ -29,7 +29,7 @@ class Feature(ModelAPI.ModelAPI_Feature): class Interface(): - """Base class of hight level Python interfaces to features.""" + """Base class of high level Python interfaces to features.""" def __init__(self, feature): self._feature = feature @@ -53,12 +53,12 @@ class Interface(): return self._feature.__getattribute__(name) - def setRealInput (self, inputid, value): + def setRealInput(self, inputid, value): self._feature.data().real(inputid).setValue(value) - def areInputValid (self): + def areInputValid(self): validators = ModelAPI.ModelAPI_Session.get().validators() return validators.validate(self._feature) - def execute (self): + def execute(self): self._feature.execute() diff --git a/src/PythonAPI/model/sketcher/arc.py b/src/PythonAPI/model/sketcher/arc.py index b025c4636..e4d12e3ba 100644 --- a/src/PythonAPI/model/sketcher/arc.py +++ b/src/PythonAPI/model/sketcher/arc.py @@ -2,11 +2,14 @@ from GeomDataAPI import geomDataAPI_Point2D from model.errors import WrongNumberOfArguments +from model.roots import Interface -class Arc(): - """Interface for editing a sketch arc feature.""" - def __init__(self, arc_feature, *args): - self._feature = arc_feature +class Arc(Interface): + """Interface to a sketch arc feature.""" + def __init__(self, feature, *args): + Interface.__init__(self, feature) + assert(self._feature.getKind() == "SketchArc") + self._center = geomDataAPI_Point2D( self._feature.data().attribute("ArcCenter") ) diff --git a/src/PythonAPI/model/sketcher/circle.py b/src/PythonAPI/model/sketcher/circle.py index a6a0134cb..1fed091e6 100644 --- a/src/PythonAPI/model/sketcher/circle.py +++ b/src/PythonAPI/model/sketcher/circle.py @@ -1,19 +1,21 @@ """Sketch circle feature interface.""" from GeomDataAPI import geomDataAPI_Point2D +from model.roots import Interface -class Circle(): +class Circle(Interface): """Interface for circle feature data manipulation.""" - - def __init__(self, circle_feature, x, y, r): - self._feature = circle_feature + def __init__(self, feature, x, y, r): + Interface.__init__(self, feature) + assert(self._feature.getKind() == "SketchCircle") + self._center = geomDataAPI_Point2D( self._feature.data().attribute("CircleCenter") ) self._radius = self._feature.data().real("CircleRadius") self._center.setValue(x, y) self._radius.setValue(r) - self._feature.execute() + self.execute() def centerData(self): """Return center data.""" diff --git a/src/PythonAPI/model/sketcher/line.py b/src/PythonAPI/model/sketcher/line.py index 22c322f2c..e4b0652f8 100644 --- a/src/PythonAPI/model/sketcher/line.py +++ b/src/PythonAPI/model/sketcher/line.py @@ -1,15 +1,21 @@ from GeomDataAPI import geomDataAPI_Point2D +from model.roots import Interface +from model.errors import WrongNumberOfArguments -class Line(): +class Line(Interface): """Interface for editing of a sketch line feature.""" - def __init__(self, line_feature, *args): - self._feature = line_feature + def __init__(self, feature, *args): + Interface.__init__(self, feature) + assert(self._feature.getKind() == "SketchLine") + + # Initialize attributes self._start_point = geomDataAPI_Point2D( self._feature.data().attribute("StartPoint") ) self._end_point = geomDataAPI_Point2D( self._feature.data().attribute("EndPoint") ) + # Set attribute values and execute if len(args) == 4: self.__createByCoordinates(*args) elif len(args) == 2: @@ -17,7 +23,9 @@ class Line(): elif len(args) == 1: self.__createByName(*args) else: - raise Exception("cannot create the Line") + raise WrongNumberOfArguments( + "Arc takes 1, 2 or 4 arguments (%s given)" % len(args) + ) def __createByCoordinates(self, x1, y1, x2, y2): self._start_point.setValue(x1, y1) diff --git a/src/PythonAPI/model/sketcher/point.py b/src/PythonAPI/model/sketcher/point.py index 5ba47e784..69b9f88fc 100644 --- a/src/PythonAPI/model/sketcher/point.py +++ b/src/PythonAPI/model/sketcher/point.py @@ -1,16 +1,28 @@ """Sketch point feature interface.""" from GeomDataAPI import geomDataAPI_Point2D +from model.roots import Interface +from model.errors import FeatureInputInvalid -class Point(): +class Point(Interface): """Interface on point feature for data manipulation.""" - def __init__(self, point_feature, x, y): - self._point_feature = point_feature + def __init__(self, feature, x, y): + Interface.__init__(self, feature) + assert(self._feature.getKind() == "SketchPoint") + + # Initialize attributes of the feature self._point_data = geomDataAPI_Point2D( - self._point_feature.data().attribute("PointCoordinates") + self._feature.data().attribute("PointCoordinates") ) self._point_data.setValue(x, y) - self._point_feature.execute() + + # 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 56b3d9c34..f32185c70 100644 --- a/src/PythonAPI/model/sketcher/sketch.py +++ b/src/PythonAPI/model/sketcher/sketch.py @@ -10,6 +10,7 @@ from model.sketcher.point import Point from model.sketcher.line import Line from model.sketcher.circle import Circle from model.sketcher.arc import Arc +from model.roots import Interface def addSketch(doc, plane): """Add a Sketch feature to the Part or PartSet and return an interface @@ -22,7 +23,7 @@ def addSketch(doc, plane): feature = featureToCompositeFeature(doc.addFeature("Sketch")) return Sketch(feature, plane) -class Sketch(): +class Sketch(Interface): """Interface on a Sketch feature.""" def __init__(self, feature, plane): """Initialize a 2D Sketch on the given plane. @@ -31,7 +32,9 @@ class Sketch(): - a 3D axis system (geom.Ax3), - an existing face identified by its topological name. """ - self._feature = feature + Interface.__init__(self, feature) + assert(self._feature.getKind() == "Sketch") + # Entities used for building the result shape self._selection = None # self.resultype ="Face" # Type of Sketch result -- 2.39.2