]> SALOME platform Git repositories - modules/shaper.git/commitdiff
Salome HOME
[Code coverage SketchAPI]: Improve coverage
authorazv <azv@opencascade.com>
Mon, 17 Dec 2018 11:35:00 +0000 (14:35 +0300)
committerazv <azv@opencascade.com>
Tue, 18 Dec 2018 04:24:17 +0000 (07:24 +0300)
src/SketchAPI/SketchAPI_Sketch.cpp
src/SketchAPI/Test/TestSketch.py

index 24be396bf39a2a109d11bd5f4d90d51b82b03e9a..9b202f6dd6f3fea76a547b12868fddcc572abc03 100644 (file)
@@ -846,9 +846,7 @@ std::shared_ptr<ModelHighAPI_Interface> SketchAPI_Sketch::setVertical(
 static std::shared_ptr<GeomAPI_Pnt2d> pointCoordinates(const AttributePtr& thePoint)
 {
   AttributePoint2DPtr aPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(thePoint);
-  if (aPnt)
-    return aPnt->pnt();
-  return std::shared_ptr<GeomAPI_Pnt2d>();
+  return aPnt ? aPnt->pnt() : std::shared_ptr<GeomAPI_Pnt2d>();
 }
 
 static std::shared_ptr<GeomAPI_Pnt2d> middlePointOnLine(const FeaturePtr& theFeature)
@@ -919,20 +917,21 @@ static std::shared_ptr<GeomAPI_Pnt2d> middlePointOnArc(const FeaturePtr& theFeat
 
 static std::shared_ptr<GeomAPI_Pnt2d> middlePoint(const ObjectPtr& theObject)
 {
+  std::shared_ptr<GeomAPI_Pnt2d> aMiddlePoint;
   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
   if (aFeature) {
+    // move only features of the following types
     const std::string& aFeatureKind = aFeature->getKind();
     if (aFeatureKind == SketchPlugin_Point::ID())
-      return pointCoordinates(aFeature->attribute(SketchPlugin_Point::COORD_ID()));
+      aMiddlePoint = pointCoordinates(aFeature->attribute(SketchPlugin_Point::COORD_ID()));
     else if (aFeatureKind == SketchPlugin_Line::ID())
-      return middlePointOnLine(aFeature);
+      aMiddlePoint = middlePointOnLine(aFeature);
     else if (aFeatureKind == SketchPlugin_Circle::ID())
-      return pointOnCircle(aFeature);
+      aMiddlePoint = pointOnCircle(aFeature);
     else if (aFeatureKind == SketchPlugin_Arc::ID())
-      return middlePointOnArc(aFeature);
+      aMiddlePoint = middlePointOnArc(aFeature);
   }
-  // do not move other types of features
-  return std::shared_ptr<GeomAPI_Pnt2d>();
+  return aMiddlePoint;
 }
 
 void SketchAPI_Sketch::move(const ModelHighAPI_RefAttr& theMovedEntity,
index fb7c3c0196b4c0d89b1877aa1a459c6aef55358a..8cd95163f0a3b3456fd04d9c278dbeb9459cceb1 100644 (file)
 ## email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
 ##
 
+import math
 import unittest
 
 import ModelAPI
 import SketchAPI
 
+from salome.shaper import geom
+from salome.shaper import model
+
 class SketchTestCase(unittest.TestCase):
 
     def setUp(self):
         self.session = ModelAPI.ModelAPI_Session.get()
-        self.doc = self.session.moduleDocument()
+        aPartSet = self.session.moduleDocument()
+        self.doc = model.addPart(aPartSet).document()
+        model.addCylinder(self.doc, model.selection("VERTEX", "PartSet/Origin"), model.selection("EDGE", "PartSet/OZ"), 5, 10)
+        self.sketch = model.addSketch(self.doc, model.defaultPlane("XOY"))
 
     def tearDown(self):
+        model.end()
+        assert(model.checkPythonDump())
         self.session.closeAll()
 
-# TODO(spo): add tests.
+    def checkPoint(self, thePoint, theReference):
+        self.assertAlmostEqual(thePoint.x(), theReference.x(), 6)
+        self.assertAlmostEqual(thePoint.y(), theReference.y(), 6)
+
+    def test_circle_by_point(self):
+        """ Test 1. Create point by GeomAPI_Pnt and circle coincident by the center
+        """
+        point = geom.Pnt2d(10., 10.)
+        aPoint = self.sketch.addPoint(point)
+        aCircle = self.sketch.addCircle(point, 10.)
+        aCoincidence = self.sketch.setCoincident(aPoint.coordinates(), aCircle.center())
+        model.do()
+        self.checkPoint(aPoint.coordinates(), point)
+        self.checkPoint(aCircle.center(), point)
+
+    def test_circle_by_external(self):
+        """ Test 2. Create point circle by external features
+        """
+        aPoint = self.sketch.addPoint("PartSet/Origin")
+        aCircle = self.sketch.addCircle("[Cylinder_1_1/Face_1][Cylinder_1_1/Face_3]")
+        model.do()
+        point = geom.Pnt2d(0., 0.)
+        self.checkPoint(aPoint.coordinates(), point)
+        self.checkPoint(aCircle.center(), point)
+
+    def test_circle_center_passed(self):
+        """ Test 3. Create circle by center and passed point
+        """
+        aCenter = geom.Pnt2d(10., 10.)
+        aPassed = geom.Pnt2d(20., 30.)
+
+        self.sketch.addCircle(aCenter.x(), aCenter.y(), aPassed.x(), aPassed.y())
+        model.do()
+        aCircle1 = SketchAPI.SketchAPI_Circle(model.lastSubFeature(self.sketch, "SketchCircle"))
+
+        self.sketch.addCircle(aCenter, aPassed)
+        model.do()
+        aCircle2 = SketchAPI.SketchAPI_Circle(model.lastSubFeature(self.sketch, "SketchCircle"))
+
+        aRadius = math.sqrt((aCenter.x()-aPassed.x())**2 + (aCenter.y()-aPassed.y())**2)
+        self.checkPoint(aCircle1.center(), aCenter)
+        self.assertAlmostEqual(aCircle1.radius().value(), aRadius, 6)
+        self.checkPoint(aCircle2.center(), aCenter)
+        self.assertAlmostEqual(aCircle2.radius().value(), aRadius, 6)
+
+    def test_circle_by_three_points(self):
+        """ Test 4. Create circle by three passed points
+        """
+        aCenter = geom.Pnt2d(10., 10.)
+        aRadius = 5.
+
+        self.sketch.addCircle(aCenter.x() - aRadius, aCenter.y(), aCenter.x() + aRadius, aCenter.y(), aCenter.x(), aCenter.y() + aRadius)
+        model.do()
+        aCircle = SketchAPI.SketchAPI_Circle(model.lastSubFeature(self.sketch, "SketchCircle"))
+
+        self.checkPoint(aCircle.center(), aCenter)
+        self.assertAlmostEqual(aCircle.radius().value(), aRadius, 6)
+
+    def test_arc_by_three_points(self):
+        """ Test 5. Create arc by three passed points
+        """
+        aCenter = geom.Pnt2d(10., 10.)
+        aRadius = 5.
+
+        aPoint1 = geom.Pnt2d(aCenter.x() - aRadius, aCenter.y())
+        aPoint2 = geom.Pnt2d(aCenter.x() + aRadius, aCenter.y())
+        aPoint3 = geom.Pnt2d(aCenter.x(), aCenter.y() + aRadius)
+
+        self.sketch.addArc(aPoint1.x(), aPoint1.y(), aPoint2.x(), aPoint2.y(), aPoint3.x(), aPoint3.y())
+        model.do()
+        anArc1 = SketchAPI.SketchAPI_Arc(model.lastSubFeature(self.sketch, "SketchArc"))
+
+        self.checkPoint(anArc1.center(), aCenter)
+        self.assertAlmostEqual(anArc1.radius().value(), aRadius, 6)
+
+        self.sketch.addArc(aPoint1, aPoint2, aPoint3)
+        model.do()
+        anArc2 = SketchAPI.SketchAPI_Arc(model.lastSubFeature(self.sketch, "SketchArc"))
+
+        self.checkPoint(anArc2.center(), aCenter)
+        self.assertAlmostEqual(anArc2.radius().value(), aRadius, 6)
+
+    def test_arc_by_tangent_point(self):
+        """ Test 6. Create arc tangent to a line
+        """
+        aTgPnt = geom.Pnt2d(10., 0.)
+        aRadius = 5.
+
+        aLine = self.sketch.addLine(0., 0., aTgPnt.x(), aTgPnt.y())
+        model.do()
+
+        aPassed = geom.Pnt2d(aTgPnt.x(), aTgPnt.y() + 2. * aRadius)
+        self.sketch.addArc(aLine.endPoint(), aPassed, False)
+        model.do()
+        anArc = SketchAPI.SketchAPI_Arc(model.lastSubFeature(self.sketch, "SketchArc"))
+
+        aCenter = geom.Pnt2d(aTgPnt.x(), aTgPnt.y() + aRadius)
+        self.checkPoint(anArc.center(), aCenter)
+        self.assertAlmostEqual(anArc.radius().value(), aRadius, 6)
+
 
 if __name__ == "__main__":
     test_program = unittest.main(exit=False)