Salome HOME
updated copyright message
[modules/shaper.git] / src / SketchPlugin / Test / TestCreateCircleByCenterAndPassed.py
index e6695c8526d4c257db8bef3a76fdd003fc531afb..14c3c554ecad26e6e733d541f8e12fa23a989d59 100644 (file)
@@ -1,3 +1,22 @@
+# Copyright (C) 2014-2023  CEA, EDF
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+#
+# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+#
+
 """
     TestCreateCircleByCenterAndPassed.py
 
@@ -22,53 +41,32 @@ __updated__ = "2017-03-22"
 #=========================================================================
 TOLERANCE = 1.e-7
 
-def getLastCircle(theSketch):
-    """
-    obtains last feature from the sketch and generates error if the feature is not a circle
-    """
-    expectedKind = "SketchCircle"
-    for anIndex in range(theSketch.numberOfSubs() - 1, -1, -1):
-        aSub = theSketch.subFeature(anIndex)
-        if (aSub.getKind() == expectedKind):
-            return aSub
-
 def verifyLastCircle(theSketch, theX, theY, theR):
     """
     subroutine to verify position of last circle in the sketch
     """
-    aLastCircle = getLastCircle(theSketch)
-    aCenter = geomDataAPI_Point2D(aLastCircle.attribute("circle_center"))
-    verifyPointCoordinates(aCenter, theX, theY)
-    aRadius = aLastCircle.real("circle_radius")
-    assert aRadius.value() == theR, "Wrong radius {0}, expected {1}".format(aRadius.value(), theR)
-
-def verifyPointCoordinates(thePoint, theX, theY):
-    assert thePoint.x() == theX and thePoint.y() == theY, "Wrong '{0}' point ({1}, {2}), expected ({3}, {4})".format(thePoint.attributeType(), thePoint.x(), thePoint.y(), theX, theY)
+    aLastCircle = model.lastSubFeature(theSketch, "SketchCircle")
+    model.assertCircle(aLastCircle, [theX, theY], theR)
 
 def verifyPointOnLine(thePoint, theLine):
-    aDistance = distancePointLine(thePoint, theLine)
+    aDistance = model.distancePointLine(thePoint, theLine)
     assert aDistance < TOLERANCE, "Point is not on Line, distance: {0}".format(aDistance)
 
 def verifyTangentCircles(theCircle1, theCircle2):
     aCenter1 = geomDataAPI_Point2D(theCircle1.attribute("circle_center"))
     aCenter2 = geomDataAPI_Point2D(theCircle2.attribute("circle_center"))
-    aDistCC = distancePointPoint(aCenter1, aCenter2)
+    aDistCC = model.distancePointPoint(aCenter1, aCenter2)
     aRadius1 = theCircle1.real("circle_radius").value()
     aRadius2 = theCircle2.real("circle_radius").value()
     aRSum = aRadius1 + aRadius2
     aRDiff = math.fabs(aRadius1 - aRadius2)
     assert math.fabs(aRSum - aDistCC) < TOLERANCE or math.fabs(aRDiff - aDistCC) < TOLERANCE, "Circles do not tangent"
 
-def distancePointPoint(thePoint1, thePoint2):
-    return thePoint1.pnt().distance(thePoint2.pnt())
-
-def distancePointLine(thePoint, theLine):
-    aLineStart = geomDataAPI_Point2D(theLine.attribute("StartPoint")).pnt().xy()
-    aLineEnd = geomDataAPI_Point2D(theLine.attribute("EndPoint")).pnt().xy()
-    aLineDir = aLineEnd.decreased(aLineStart)
-    aLineLen = aLineEnd.distance(aLineStart)
-    aPntDir = thePoint.pnt().xy().decreased(aLineStart)
-    return math.fabs(aPntDir.cross(aLineDir) / aLineLen)
+def verifyTangentCircleLine(theCircle, theLine):
+    aCenter = geomDataAPI_Point2D(theCircle.attribute("circle_center"))
+    aRadius = theCircle.real("circle_radius").value()
+    aDistCL = model.distancePointLine(aCenter, theLine)
+    assert math.fabs(aDistCL - aRadius) < TOLERANCE, "Circle and line are not tangent"
 
 
 #=========================================================================
@@ -137,7 +135,7 @@ assert (not aCircleType.isInitialized())
 aCircleType.setValue("circle_type_by_center_and_passed_points")
 aCircleCenter.setValue(-25., -25)
 aCirclePassed.setValue(0., -25.)
-aRadius = distancePointPoint(aCircleCenter, aCirclePassed)
+aRadius = model.distancePointPoint(aCircleCenter, aCirclePassed)
 aSession.finishOperation()
 assert (aSketchFeature.numberOfSubs() == 2)
 verifyLastCircle(aSketchFeature, -25., -25., aRadius)
@@ -146,14 +144,14 @@ verifyLastCircle(aSketchFeature, -25., -25., aRadius)
 # Test 3. Create a circle as a macro-feature by center and passed point coincident to other points
 #=========================================================================
 # get previous circle
-aPrevCircle = getLastCircle(aSketchFeature)
+aPrevCircle = model.lastSubFeature(aSketchFeature, "SketchCircle")
 aPrevCenter = geomDataAPI_Point2D(aPrevCircle.attribute("circle_center"))
 # create additional point
-aPointCoodinates = [0., 0.]
+aPointCoordinates = [0., 0.]
 aSession.startOperation()
 aPoint = aSketchFeature.addFeature("SketchPoint")
-aPointCoord = geomDataAPI_Point2D(aPoint.attribute("PointCoordindates"))
-aPointCoord.setValue(aPointCoodinates[0], aPointCoodinates[1])
+aPointCoord = geomDataAPI_Point2D(aPoint.attribute("PointCoordinates"))
+aPointCoord.setValue(aPointCoordinates[0], aPointCoordinates[1])
 aSession.finishOperation()
 # create new circle
 aSession.startOperation()
@@ -172,10 +170,10 @@ aCenterRef.setObject(aPoint.lastResult())
 aCenter.setValue(aPointCoord.pnt())
 aPassedRef.setAttr(aPrevCenter)
 aPassed.setValue(aPrevCenter.pnt())
-aRadius = distancePointPoint(aPrevCenter, aPointCoord)
+aRadius = model.distancePointPoint(aPrevCenter, aPointCoord)
 aSession.finishOperation()
 assert (aSketchFeature.numberOfSubs() == 6)
-verifyPointCoordinates(aPointCoord, aPointCoodinates[0], aPointCoodinates[1])
+model.assertPoint(aPointCoord, aPointCoordinates)
 verifyLastCircle(aSketchFeature, aPointCoord.x(), aPointCoord.y(), aRadius)
 model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 2)
 
@@ -183,7 +181,7 @@ model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 2)
 # Test 4. Create a circle as a macro-feature by center on a line and passed point on another circle
 #=========================================================================
 # get previous circle
-aPrevCircle = getLastCircle(aSketchFeature)
+aPrevCircle = model.lastSubFeature(aSketchFeature, "SketchCircle")
 aPrevCenter = geomDataAPI_Point2D(aPrevCircle.attribute("circle_center"))
 aPrevCenterXY = [aPrevCenter.x(), aPrevCenter.y()]
 aPrevRadius = aPrevCircle.real("circle_radius").value()
@@ -212,23 +210,51 @@ anExpectedCenter = [(aLineStart[0] + aLineEnd[0]) * 0.5, (aLineStart[1] + aLineE
 aCenter.setValue(anExpectedCenter[0], anExpectedCenter[1])
 aPassedRef.setObject(aPrevCircle.lastResult())
 aPassed.setValue(aPrevCenter.x() + aPrevRadius, aPrevCenter.y())
-aRadius = distancePointPoint(aCenter, aPassed)
+aRadius = model.distancePointPoint(aCenter, aPassed)
 aSession.finishOperation()
 assert (aSketchFeature.numberOfSubs() == 10)
 # check connected features do not change their positions
-verifyPointCoordinates(aPrevCenter, aPrevCenterXY[0], aPrevCenterXY[1])
+model.assertPoint(aPrevCenter, aPrevCenterXY)
 assert(aPrevCircle.real("circle_radius").value() == aPrevRadius)
-verifyPointCoordinates(aStartPnt, aLineStart[0], aLineStart[1])
-verifyPointCoordinates(aEndPnt, aLineEnd[0], aLineEnd[1])
+model.assertLine(aLine, aLineStart, aLineEnd)
 # verify newly created circle
-aCircle = getLastCircle(aSketchFeature)
+aCircle = model.lastSubFeature(aSketchFeature, "SketchCircle")
 aCenter = geomDataAPI_Point2D(aCircle.attribute("circle_center"))
-verifyPointCoordinates(aCenter, anExpectedCenter[0], anExpectedCenter[1])
+model.assertPoint(aCenter, anExpectedCenter)
 verifyPointOnLine(aCenter, aLine)
 verifyTangentCircles(aCircle, aPrevCircle)
 model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 3)
 model.testNbSubFeatures(aSketch, "SketchConstraintTangent", 1)
 
+#=========================================================================
+# Test 5. Create a circle as a macro-feature by center and passed point on line
+#=========================================================================
+# create new circle
+aSession.startOperation()
+aCircle = aSketchFeature.addFeature("SketchMacroCircle")
+aCenter = geomDataAPI_Point2D(aCircle.attribute("center_point"))
+aCenterRef = aCircle.refattr("center_point_ref")
+aPassed = geomDataAPI_Point2D(aCircle.attribute("passed_point"))
+aPassedRef = aCircle.refattr("passed_point_ref")
+aCircleType = aCircle.string("circle_type")
+# initialize attributes
+aCircleType.setValue("circle_type_by_center_and_passed_points")
+anExpectedCenter = [(aLineStart[0] + aLineEnd[0]) * 0.5 + 10., (aLineStart[1] + aLineEnd[1]) * 0.5]
+aCenter.setValue(anExpectedCenter[0], anExpectedCenter[1])
+aPassedRef.setObject(aLine.lastResult())
+aPassed.setValue(aLineStart[0], aLineStart[1])
+aSession.finishOperation()
+assert (aSketchFeature.numberOfSubs() == 12)
+# check connected features do not change their positions
+model.assertLine(aLine, aLineStart, aLineEnd)
+# verify newly created circle
+aCircle = model.lastSubFeature(aSketchFeature, "SketchCircle")
+aCenter = geomDataAPI_Point2D(aCircle.attribute("circle_center"))
+model.assertPoint(aCenter, anExpectedCenter)
+verifyTangentCircleLine(aCircle, aLine)
+model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 3)
+model.testNbSubFeatures(aSketch, "SketchConstraintTangent", 2)
+
 #=========================================================================
 # Test 5. Create a circle as a macro-feature by center and passed point placed on the same line
 #         Check the circle is not created
@@ -248,9 +274,11 @@ aPassedRef.setObject(aLine.lastResult())
 aPassed.setValue(aLineStart[0], aLineStart[1])
 aSession.finishOperation()
 aLastFeature = aSketchFeature.subFeature(aSketchFeature.numberOfSubs() - 1)
-assert aLastFeature == aCircle, "ERROR: SketchMacroCircle has NOT expected to be valid"
+assert aLastFeature.getKind() == "SketchMacroCircle", "ERROR: SketchMacroCircle has NOT expected to be valid"
+aSession.startOperation()
 aDocument.removeFeature(aCircle)
-assert (aSketchFeature.numberOfSubs() == 10)
+aSession.finishOperation()
+assert (aSketchFeature.numberOfSubs() == 12)
 
 #=========================================================================
 # End of test