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
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)
if __name__ == "__main__":
- unittest.main()
\ No newline at end of file
+ unittest.main(verbosity=2)
\ No newline at end of file
# Custom exceptions
-from errors import WrongNumberOfArguments
\ No newline at end of file
+from errors import WrongNumberOfArguments
+from errors import FeatureInputInvalid
"""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
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
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()
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")
)
"""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."""
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:
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)
"""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."""
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
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.
- 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