From: Renaud NEDELEC Date: Thu, 15 Oct 2015 17:18:49 +0000 (+0200) Subject: [PythonAPI] refactoring sketcher API further X-Git-Tag: V_2.1.0~206^2~93 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=13fbe711081d03ab7721eaecef3c7e09c91d127f;p=modules%2Fshaper.git [PythonAPI] refactoring sketcher API further --- diff --git a/src/PythonAPI/Test/TestSketcherAddLine.py b/src/PythonAPI/Test/TestSketcherAddLine.py index 959827a6a..41322e80f 100644 --- a/src/PythonAPI/Test/TestSketcherAddLine.py +++ b/src/PythonAPI/Test/TestSketcherAddLine.py @@ -4,7 +4,10 @@ from TestSketcher import SketcherTestCase class SketcherAddLineTestCase(SketcherTestCase): def runTest(self): - self.sketch.addLine(0, 0, 0, 1) + line = self.sketch.addLine(0, 0, 0, 1) + self.assertEqual(line.startPointData().x(), line.endPointData().x()) + self.assertNotEqual(line.startPointData().y(), line.endPointData().y()) + if __name__ == "__main__": unittest.main() \ No newline at end of file diff --git a/src/PythonAPI/modeler/sketcher/line.py b/src/PythonAPI/modeler/sketcher/line.py index 731a238fd..543051b29 100644 --- a/src/PythonAPI/modeler/sketcher/line.py +++ b/src/PythonAPI/modeler/sketcher/line.py @@ -1,40 +1,50 @@ from GeomDataAPI import geomDataAPI_Point2D - -class Line(): - - def __init__(self, sketch, *args): - self.my = sketch.addFeature("SketchLine") - if len(args) == 4: - self.__createByCoordinates(*args) - elif len(args) == 2: - self.__createByPoints(*args) - elif len(args) == 1: - self.__createByName(sketch, *args) - else: - raise Exception("cannot create the Line") - - def __createByCoordinates(self, x1, y1, x2, y2): - geomDataAPI_Point2D( self.my.data().attribute("StartPoint") ).setValue(x1, y1) - geomDataAPI_Point2D( self.my.data().attribute("EndPoint") ).setValue(x2, y2) - self.my.execute() - - def __createByPoints(self, p1, p2): - geomDataAPI_Point2D( self.my.data().attribute("StartPoint") ).setValue(p1.x(), p1.y()) - geomDataAPI_Point2D( self.my.data().attribute("EndPoint") ).setValue(p2.x(), p2.y()) - self.my.execute() - - def __createByName(self, sketch, name): - self.my.data().selection("External").selectSubShape("EDGE", name) - self.my.execute() - rigid = sketch.addFeature("SketchConstraintRigid") - rigid.refattr("ConstraintEntityA").setObject( self.my.firstResult() ) - - def startPointData (self): - return geomDataAPI_Point2D( self.my.data().attribute("StartPoint") ) - - def endPointData (self): - return geomDataAPI_Point2D( self.my.data().attribute("EndPoint") ) - - def result (self): - return self.my.firstResult() \ No newline at end of file +def addLine(sketch, *args): + sketch_feature = sketch.addFeature("SketchLine") + return LineInterface(sketch_feature, *args) + + +class LineInterface(): + """Interface for editing of a sketch line feature.""" + def __init__(self, line_feature, *args): + self._feature = line_feature + self._start_point = geomDataAPI_Point2D( + self._feature.data().attribute("StartPoint") + ) + self._end_point = geomDataAPI_Point2D( + self._feature.data().attribute("EndPoint") + ) + if len(args) == 4: + self.__createByCoordinates(*args) + elif len(args) == 2: + self.__createByPoints(*args) + elif len(args) == 1: + self.__createByName(sketch, *args) + else: + raise Exception("cannot create the Line") + + def __createByCoordinates(self, x1, y1, x2, y2): + self._start_point.setValue(x1, y1) + self._end_point.setValue(x2, y2) + self._feature.execute() + + def __createByPoints(self, p1, p2): + self._start_point.setValue(p1.x(), p1.y()) + self._end_point.setValue(p2.x(), p2.y()) + self._feature.execute() + + def __createByName(self, sketch, name): + self._feature.data().selection("External").selectSubShape("EDGE", name) + self._feature.execute() + rigid = sketch.addFeature("SketchConstraintRigid") + rigid.refattr("ConstraintEntityA").setObject( self._feature.firstResult() ) + + def startPointData (self): + return self._start_point + + def endPointData (self): + return self._end_point + + def result (self): + return self._feature.firstResult() \ No newline at end of file diff --git a/src/PythonAPI/modeler/sketcher/point.py b/src/PythonAPI/modeler/sketcher/point.py index acd88be81..2a99aa7a1 100644 --- a/src/PythonAPI/modeler/sketcher/point.py +++ b/src/PythonAPI/modeler/sketcher/point.py @@ -1,16 +1,31 @@ -# Class definitions of Sketch features +"""Sketch point feature interface.""" from GeomDataAPI import geomDataAPI_Point2D -class Point(): +def addPoint(sketch, x, y): + """Add a sketch point feature to the sketch. + + :return: Interface object on this feature + :rtype: sketcher.Point + """ + sketch_point_feature = sketch.addFeature("SketchPoint") + return PointInterface(sketch_feature, x, y) - def __init__(self, sketch, x, y): - self.my = sketch.addFeature("SketchPoint") - geomDataAPI_Point2D( self.my.data().attribute("PointCoordindates") ).setValue(x, y) - self.my.execute() - def pointData (self): - return geomDataAPI_Point2D( self.my.data().attribute("PointCoordindates") ) +class PointInterface(): + """Interface on point feature for data manipulation.""" + def __init__(self, point_feature, x, y): + self._point_feature = point_feature + self._point_data = geomDataAPI_Point2D( + self._point_feature.data().attribute("PointCoordinates") + ) + self._point_data.setValue(x, y) + self._point_feature.execute() - def result (self): - return self.my.firstResult() + def pointData (self): + """Return the point data.""" + return self._point_data + + def result (self): + """Return the feature result.""" + return self._point_feature.firstResult() diff --git a/src/PythonAPI/modeler/sketcher/sketch.py b/src/PythonAPI/modeler/sketcher/sketch.py index b497ff119..20ddfd842 100644 --- a/src/PythonAPI/modeler/sketcher/sketch.py +++ b/src/PythonAPI/modeler/sketcher/sketch.py @@ -6,15 +6,16 @@ Copyright (C) 2014-20xx CEA/DEN, EDF R&D from ModelAPI import * from GeomDataAPI import * from GeomAlgoAPI import * -from modeler.sketcher.point import Point -from modeler.sketcher.line import Line +import modeler.sketcher.point as sk_point +import modeler.sketcher.line as sk_line from modeler.sketcher.circle import Circle def addSketch(doc, plane): - """Add a Sketch feature to the Part or PartSet. + """Add a Sketch feature to the Part or PartSet and return an interface + on it. - A Sketch object is instanciated with the built feature - it provides an interface for manipulating the feature data. + A Sketch object is instanciated with a feature as input parameter + it provides an interface for manipulation of the feature data. :return: interface on the feature :rtype: Sketch object""" feature = featureToCompositeFeature( doc.addFeature("Sketch") ) @@ -29,8 +30,8 @@ class Sketch(): - an existing face identified by its topological name. """ self._feature = feature - self._selection = None # Entities used for building the result shape - # self.resultype ="Face" # Type of Sketch result + self._selection = None # Entities used for building the result shape + # self.resultype ="Face" # Type of Sketch result if isinstance(plane, str): self.__sketchOnFace(plane) else: @@ -58,14 +59,17 @@ class Sketch(): def addPoint (self, *args): """Adds a point to this Sketch.""" - return Point(self._feature, *args) + point_interface = sk_point.addPoint(self._feature, *args) + return point_interface def addLine (self, *args): """Adds a line to this Sketch.""" - return Line(self._feature, *args) + line_interface = sk_line.addLine(self._feature, *args) + return line_interface def addPolyline (self, *coords): """Adds a poly-line to this Sketch. + The end of consecutive segments are defined as coincident. """ c0 = coords[0] @@ -76,7 +80,7 @@ class Sketch(): # Adding and connecting next lines for c2 in coords[2:]: line_2 = self.addLine(c1, c2) - self.setCoincident( line_1.endPointData(), line_2.startPointData() ) + self.setCoincident(line_1.endPointData(), line_2.startPointData()) polyline.append(line_2) c1 = c2 line_1 = line_2 @@ -169,12 +173,16 @@ class Sketch(): # Getters def selectFace (self, *args): - """Selects the geometrical entities of this Sketch on which the result Face must be built. - When no entity is given, the face is based on all existing geometry of this Sketch. + """Select the geometrical entities of this Sketch on which + the result Face must be built. + + When no entity is given, the face is based on all existing + geometry of this Sketch. """ #self.resultype ="Face" if len(args) == 0: - self._selection = modelAPI_ResultConstruction( self._feature.firstResult() ).shape() + self._selection = modelAPI_ResultConstruction( + self._feature.firstResult()).shape() elif len(args) == 1: self._selection = args[0].shape() else: