]> SALOME platform Git repositories - modules/shaper.git/commitdiff
Salome HOME
[PythonAPI / sketcher] added addArc method and the corresponding test + fix for point API
authorRenaud NEDELEC <renaud.nedelec@opencascade.com>
Mon, 19 Oct 2015 13:50:17 +0000 (15:50 +0200)
committerRenaud NEDELEC <renaud.nedelec@opencascade.com>
Mon, 19 Oct 2015 13:50:17 +0000 (15:50 +0200)
src/PythonAPI/CMakeLists.txt
src/PythonAPI/Test/TestSketcherAddArc.py [new file with mode: 0644]
src/PythonAPI/Test/TestSketcherAddPoint.py [new file with mode: 0644]
src/PythonAPI/modeler/__init__.py
src/PythonAPI/modeler/errors.py [new file with mode: 0644]
src/PythonAPI/modeler/sketcher/arc.py [new file with mode: 0644]
src/PythonAPI/modeler/sketcher/sketch.py
src/SketchPlugin/SketchPlugin_Point.h

index 534588452f96431e919d0d0c90306f4074f86cae..300e74f4d52ddb1a6665edfea6451531d425c760 100644 (file)
@@ -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 (file)
index 0000000..53c07cf
--- /dev/null
@@ -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 (file)
index 0000000..8ebdb23
--- /dev/null
@@ -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
index 76ca85c590bcbdc92c9c95eab65af9b5aa719c64..a84a1651596a55ec809fb252261b062e18037637 100644 (file)
@@ -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 (file)
index 0000000..642af0a
--- /dev/null
@@ -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 (file)
index 0000000..853ceb6
--- /dev/null
@@ -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()
+        
+        
+        
+        
+        
index 1ec03ca03c7bad6fdbc178d7c303dc3e7b0e812e..5e9c1d5d32e0706fbe947292e700c61129760388 100644 (file)
@@ -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.
index 59995ff4310aeb44edc72c2f950f4d3e09c0c119..1e1a4f65cc842ac70804ab32dbfa3bc6368fdb60 100644 (file)
@@ -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