From 736c6207654215b6ec5f5efb2a39dc1c4917372d Mon Sep 17 00:00:00 2001 From: Renaud NEDELEC Date: Tue, 20 Oct 2015 12:43:32 +0200 Subject: [PATCH] [PythonAPI] added two tests and added transaction commit step in previous tests --- src/PythonAPI/CMakeLists.txt | 2 + src/PythonAPI/Test/TestSketcherAddArc.py | 2 + src/PythonAPI/Test/TestSketcherAddCircle.py | 1 + src/PythonAPI/Test/TestSketcherAddLine.py | 1 + src/PythonAPI/Test/TestSketcherAddPoint.py | 1 + .../Test/TestSketcherSetCoincident.py | 1 + src/PythonAPI/Test/TestSketcherSetParallel.py | 1 + .../Test/TestSketcherSetPerpendicular.py | 19 ++++ src/PythonAPI/Test/TestSketcherSetRadius.py | 13 +++ src/PythonAPI/model/sketcher/sketch.py | 90 ++++++++++--------- 10 files changed, 88 insertions(+), 43 deletions(-) create mode 100644 src/PythonAPI/Test/TestSketcherSetPerpendicular.py create mode 100644 src/PythonAPI/Test/TestSketcherSetRadius.py diff --git a/src/PythonAPI/CMakeLists.txt b/src/PythonAPI/CMakeLists.txt index 45cedd051..936e1aeca 100644 --- a/src/PythonAPI/CMakeLists.txt +++ b/src/PythonAPI/CMakeLists.txt @@ -16,4 +16,6 @@ ADD_UNIT_TESTS( TestSketcherAddCircle.py TestSketcherSetCoincident.py TestSketcherSetParallel.py + TestSketcherSetPerpendicular.py + TestSketcherSetRadius.py ) diff --git a/src/PythonAPI/Test/TestSketcherAddArc.py b/src/PythonAPI/Test/TestSketcherAddArc.py index 87378aef0..1b64c4465 100644 --- a/src/PythonAPI/Test/TestSketcherAddArc.py +++ b/src/PythonAPI/Test/TestSketcherAddArc.py @@ -9,6 +9,7 @@ from TestSketcher import SketcherTestCase class SketcherAddArc(SketcherTestCase): def test_arc_by_coords(self): arc = self.sketch.addArc(0, 1, 0, 0, 1, 1) + model.do() self.assertEqual(arc.startPointData().x(), 0) self.assertEqual(arc.startPointData().y(), 0) @@ -17,6 +18,7 @@ class SketcherAddArc(SketcherTestCase): start = geom.Pnt2d(0, 0) end = geom.Pnt2d(0, 1) arc = self.sketch.addArc(center, start, end) + model.do() self.assertEqual(arc.startPointData().x(), 0) self.assertEqual(arc.startPointData().y(), 0) diff --git a/src/PythonAPI/Test/TestSketcherAddCircle.py b/src/PythonAPI/Test/TestSketcherAddCircle.py index 3c45e320f..48b15b0d3 100644 --- a/src/PythonAPI/Test/TestSketcherAddCircle.py +++ b/src/PythonAPI/Test/TestSketcherAddCircle.py @@ -5,6 +5,7 @@ from TestSketcher import SketcherTestCase class SketcherAddCircle(SketcherTestCase): def runTest(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) diff --git a/src/PythonAPI/Test/TestSketcherAddLine.py b/src/PythonAPI/Test/TestSketcherAddLine.py index ce5b31349..2c9254633 100644 --- a/src/PythonAPI/Test/TestSketcherAddLine.py +++ b/src/PythonAPI/Test/TestSketcherAddLine.py @@ -5,6 +5,7 @@ from TestSketcher import SketcherTestCase class SketcherAddLine(SketcherTestCase): def runTest(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()) diff --git a/src/PythonAPI/Test/TestSketcherAddPoint.py b/src/PythonAPI/Test/TestSketcherAddPoint.py index e521c9a08..783f8c17b 100644 --- a/src/PythonAPI/Test/TestSketcherAddPoint.py +++ b/src/PythonAPI/Test/TestSketcherAddPoint.py @@ -5,6 +5,7 @@ from TestSketcher import SketcherTestCase class SketcherAddPoint(SketcherTestCase): def runTest(self): point = self.sketch.addPoint(0, 1) + model.do() self.assertEqual(point.pointData().x(), 0.0) self.assertEqual(point.pointData().y(), 1.0) diff --git a/src/PythonAPI/Test/TestSketcherSetCoincident.py b/src/PythonAPI/Test/TestSketcherSetCoincident.py index f6a13e12a..a5c07f7e6 100644 --- a/src/PythonAPI/Test/TestSketcherSetCoincident.py +++ b/src/PythonAPI/Test/TestSketcherSetCoincident.py @@ -7,6 +7,7 @@ class SketcherSetCoincident(SketcherTestCase): l1 = self.sketch.addLine(0, 0, 0, 1) l2 = self.sketch.addLine(0, 1, 1, 1) self.sketch.setCoincident(l1.endPointData(), l2.startPointData()) + model.do() if __name__ == "__main__": unittest.main() \ No newline at end of file diff --git a/src/PythonAPI/Test/TestSketcherSetParallel.py b/src/PythonAPI/Test/TestSketcherSetParallel.py index 0f7fa1844..75c37ecc3 100644 --- a/src/PythonAPI/Test/TestSketcherSetParallel.py +++ b/src/PythonAPI/Test/TestSketcherSetParallel.py @@ -7,6 +7,7 @@ class SketcherSetParallel(SketcherTestCase): l1 = self.sketch.addLine(0, 0, 0, 1) l2 = self.sketch.addLine(0, 1, 1, 1) self.sketch.setParallel(l1.result(), l2.result()) + model.do() if __name__ == "__main__": unittest.main() \ No newline at end of file diff --git a/src/PythonAPI/Test/TestSketcherSetPerpendicular.py b/src/PythonAPI/Test/TestSketcherSetPerpendicular.py new file mode 100644 index 000000000..61dcfd372 --- /dev/null +++ b/src/PythonAPI/Test/TestSketcherSetPerpendicular.py @@ -0,0 +1,19 @@ +import unittest +import model +from TestSketcher import SketcherTestCase + +class SketcherSetPerpendicular(SketcherTestCase): + def runTest(self): + l1 = self.sketch.addLine(0, 0, 0, 1) + l2 = self.sketch.addLine(0, 0, 1, 1) + self.sketch.setPerpendicular(l1.result(), l2.result()) + model.do() + + dot_product = (l1.endPointData().x() - l1.startPointData().x()) * \ + (l2.endPointData().x() - l2.startPointData().x()) + \ + (l1.endPointData().y() - l1.startPointData().y()) * \ + (l2.endPointData().y() - l2.startPointData().y()) + self.assertEqual(dot_product, 0.0) + +if __name__ == "__main__": + unittest.main() \ No newline at end of file diff --git a/src/PythonAPI/Test/TestSketcherSetRadius.py b/src/PythonAPI/Test/TestSketcherSetRadius.py new file mode 100644 index 000000000..d0fe52bf2 --- /dev/null +++ b/src/PythonAPI/Test/TestSketcherSetRadius.py @@ -0,0 +1,13 @@ +import unittest +import model +from TestSketcher import SketcherTestCase + +class SketcherSetRadius(SketcherTestCase): + def runTest(self): + circle = self.sketch.addCircle(0, 10, 20) + self.sketch.setRadius(circle.result(), 30) + model.do() + self.assertEqual(circle.radiusData().value(), 30.0) + +if __name__ == "__main__": + unittest.main() \ No newline at end of file diff --git a/src/PythonAPI/model/sketcher/sketch.py b/src/PythonAPI/model/sketcher/sketch.py index 30ddbfedc..81df548aa 100644 --- a/src/PythonAPI/model/sketcher/sketch.py +++ b/src/PythonAPI/model/sketcher/sketch.py @@ -25,13 +25,15 @@ def addSketch(doc, plane): class Sketch(): """Interface on a Sketch feature.""" def __init__(self, feature, plane): - """Initialize a 2D Sketch on the given plane + """Initialize a 2D Sketch on the given plane. + The plane can be defined either by: - a 3D axis system (geom.Ax3), - an existing face identified by its topological name. """ self._feature = feature - self._selection = None # Entities used for building the result shape + # Entities used for building the result shape + self._selection = None # self.resultype ="Face" # Type of Sketch result if isinstance(plane, str): self.__sketchOnFace(plane) @@ -56,7 +58,7 @@ class Sketch(): self._feature.data().selection("External").selectSubShape("FACE", plane) -# Creation of Geometries + # Creation of Geometries def addPoint (self, *args): """Add a point to this Sketch.""" @@ -78,45 +80,6 @@ class Sketch(): arc_feature = self._feature.addFeature("SketchArc") return Arc(arc_feature, *args) - def addPolyline (self, *coords): - """Adds a poly-line to this Sketch. - - The end of consecutive segments are defined as coincident. - """ - c0 = coords[0] - c1 = coords[1] - polyline = [] - line_1 = self.addLine(c0, c1) - polyline.append(line_1) - # Adding and connecting next lines - for c2 in coords[2:]: - line_2 = self.addLine(c1, c2) - self.setCoincident(line_1.endPointData(), line_2.startPointData()) - polyline.append(line_2) - c1 = c2 - line_1 = line_2 - return polyline - - def addPolygon (self, *coords): - """Add a polygon to this Sketch. - - The end of consecutive segments are defined as coincident. - """ - pg = self.addPolyline(*coords) - # Closing the poly-line supposed being defined by at least 3 points - c0 = coords[0] - cn = coords[len(coords) - 1] - ln = self.addLine(cn, c0) - self.setCoincident( - pg[len(coords) - 2].endPointData(), ln.startPointData() - ) - self.setCoincident( - ln.endPointData(), pg[0].startPointData() - ) - pg.append(ln) - return pg - - # Creation of Geometrical and Dimensional Constraints def setCoincident (self, p1, p2): @@ -180,9 +143,50 @@ class Sketch(): def setValue (self, constraint, value): """Modify the value of the given dimensional constraint.""" constraint.data().real("ConstraintValue").setValue(value) + + + # Macro functions combining geometry creation and constraints + + def addPolyline (self, *coords): + """Add a poly-line to this Sketch. + + The end of consecutive segments are defined as coincident. + """ + c0 = coords[0] + c1 = coords[1] + polyline = [] + line_1 = self.addLine(c0, c1) + polyline.append(line_1) + # Adding and connecting next lines + for c2 in coords[2:]: + line_2 = self.addLine(c1, c2) + self.setCoincident(line_1.endPointData(), line_2.startPointData()) + polyline.append(line_2) + c1 = c2 + line_1 = line_2 + return polyline + + def addPolygon (self, *coords): + """Add a polygon to this Sketch. + + The end of consecutive segments are defined as coincident. + """ + pg = self.addPolyline(*coords) + # Closing the poly-line supposed being defined by at least 3 points + c0 = coords[0] + cn = coords[len(coords) - 1] + ln = self.addLine(cn, c0) + self.setCoincident( + pg[len(coords) - 2].endPointData(), ln.startPointData() + ) + self.setCoincident( + ln.endPointData(), pg[0].startPointData() + ) + pg.append(ln) + return pg -# Getters + # Getters def selectFace (self, *args): """Select the geometrical entities of this Sketch on which -- 2.39.2