From afba75464872a7bb15dfa5c477a605f7fc2195cc Mon Sep 17 00:00:00 2001 From: azv Date: Thu, 6 Apr 2017 16:42:30 +0300 Subject: [PATCH] Unit test for creating arc by tangent edge --- src/SketchPlugin/CMakeLists.txt | 2 +- .../Test/TestCreateArcByCenterStartEnd.py | 1 - .../Test/TestCreateArcByTangentEdge.py | 202 ++++++++++++++++++ 3 files changed, 203 insertions(+), 2 deletions(-) create mode 100644 src/SketchPlugin/Test/TestCreateArcByTangentEdge.py diff --git a/src/SketchPlugin/CMakeLists.txt b/src/SketchPlugin/CMakeLists.txt index 9316f3423..452f5c139 100644 --- a/src/SketchPlugin/CMakeLists.txt +++ b/src/SketchPlugin/CMakeLists.txt @@ -128,7 +128,7 @@ INSTALL(FILES ${TEXT_RESOURCES} DESTINATION ${SHAPER_INSTALL_XML_RESOURCES}) ADD_UNIT_TESTS(TestSketchPointLine.py TestCreateArcByCenterStartEnd.py # TestCreateArcByThreePoints.py - # TestCreateArcByTangentEdge.py + TestCreateArcByTangentEdge.py # TestCreateArcChangeType.py TestCreateCircleByCenterAndPassed.py TestCreateCircleByThreePoints.py diff --git a/src/SketchPlugin/Test/TestCreateArcByCenterStartEnd.py b/src/SketchPlugin/Test/TestCreateArcByCenterStartEnd.py index 4f05ecf69..f8fbb67dd 100644 --- a/src/SketchPlugin/Test/TestCreateArcByCenterStartEnd.py +++ b/src/SketchPlugin/Test/TestCreateArcByCenterStartEnd.py @@ -62,7 +62,6 @@ def verifyPointOnCircle(thePoint, theCircular): return assert math.fabs(model.distancePointPoint(aCenterPoint, thePoint) - aRadius) < TOLERANCE - def distancePointLine(thePoint, theLine): aLineStart = geomDataAPI_Point2D(theLine.attribute("StartPoint")).pnt().xy() aLineEnd = geomDataAPI_Point2D(theLine.attribute("EndPoint")).pnt().xy() diff --git a/src/SketchPlugin/Test/TestCreateArcByTangentEdge.py b/src/SketchPlugin/Test/TestCreateArcByTangentEdge.py new file mode 100644 index 000000000..a59e9299f --- /dev/null +++ b/src/SketchPlugin/Test/TestCreateArcByTangentEdge.py @@ -0,0 +1,202 @@ +""" + TestCreateArc.py + + static const std::string MY_SKETCH_ARC_ID("SketchArc"); + static const std::string MY_CENTER_ID = "center_point"; + static const std::string MY_START_ID = "start_point"; + static const std::string MY_END_ID = "end_point"; + data()->addAttribute(SketchPlugin_Arc::CENTER_ID(), GeomDataAPI_Point2D::typeId()); + data()->addAttribute(SketchPlugin_Arc::START_ID(), GeomDataAPI_Point2D::typeId()); + data()->addAttribute(SketchPlugin_Arc::END_ID(), GeomDataAPI_Point2D::typeId()); +""" + +#========================================================================= +# Initialization of the test +#========================================================================= +from GeomDataAPI import * +from ModelAPI import * +from SketchAPI import SketchAPI_Sketch +import math +from salome.shaper import model + +__updated__ = "2017-04-06" + +TOLERANCE = 1.e-7 + +#========================================================================= +# Auxiliary functions +#========================================================================= + +def verifyLastArc(theSketch, theCenter, theStart, theEnd): + """ + subroutine to verify position of last arc in the sketch + """ + aLastArc = model.lastSubFeature(theSketch, "SketchArc") + aCenterPnt = geomDataAPI_Point2D(aLastArc.attribute("center_point")) + aStartPnt = geomDataAPI_Point2D(aLastArc.attribute("start_point")) + aEndPnt = geomDataAPI_Point2D(aLastArc.attribute("end_point")) + if len(theCenter): + verifyPointCoordinates(aCenterPnt, theCenter[0], theCenter[1]) + if len(theStart): + verifyPointCoordinates(aStartPnt, theStart[0], theStart[1]) + if len(theEnd): + verifyPointCoordinates(aEndPnt, theEnd[0], theEnd[1]) + model.assertSketchArc(aLastArc) + +def verifyPointCoordinates(thePoint, theX, theY): + assert thePoint.x() == theX and thePoint.y() == theY, "Wrong '{0}' point ({1}, {2}), expected ({3}, {4})".format(thePoint.id(), thePoint.x(), thePoint.y(), theX, theY) + +def verifyTangent(theFeature1, theFeature2): + anArcs = [] + aLines = [] + aFeatures = [theFeature1, theFeature2] + for feat in aFeatures: + if feat.getKind() == "SketchLine": + aLines.append(feat) + elif feat.getKind() == "SketchArc": + anArcs.append(feat) + if len(anArcs) == 2: + verifyArcArcTangent(anArcs[0], anArcs[1]) + elif len(anArcs) == 1 and len(aLines) == 1: + verifyArcLineTangent(anArcs[0], aLines[0]) + +def verifyArcArcTangent(theArc1, theArc2): + aCenter1 = geomDataAPI_Point2D(theArc1.attribute("center_point")) + aStart1 = geomDataAPI_Point2D(theArc1.attribute("start_point")) + aRadius1 = model.distancePointPoint(aStart1, aCenter1) + + aCenter2 = geomDataAPI_Point2D(theArc2.attribute("center_point")) + aStart2 = geomDataAPI_Point2D(theArc2.attribute("start_point")) + aRadius2 = model.distancePointPoint(aStart2, aCenter2) + + aDistCC = model.distancePointPoint(aCenter1, aCenter2) + aRSum = aRadius1 + aRadius2 + aRDiff = math.fabs(aRadius1 - aRadius2) + assert math.fabs(aRSum - aDistCC) < TOLERANCE or math.fabs(aRDiff - aDistCC) < TOLERANCE, "Arcs do not tangent" + +def verifyArcLineTangent(theArc, theLine): + aCenter = geomDataAPI_Point2D(theArc.attribute("center_point")) + aStart = geomDataAPI_Point2D(theArc.attribute("start_point")) + aRadius = model.distancePointPoint(aStart, aCenter) + + aDistCL = distancePointLine(aCenter, theLine) + assert math.fabs(aDistCL - aRadius) < TOLERANCE, "Arc and line do not tangent" + +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 verifyPointOnLine(thePoint, theLine): + aDistance = distancePointLine(thePoint, theLine) + assert aDistance < TOLERANCE, "Point is not on Line, distance: {0}".format(aDistance) + + + +aSession = ModelAPI_Session.get() +aDocument = aSession.moduleDocument() +#========================================================================= +# Creation of a sketch +#========================================================================= +aSession.startOperation() +aSketchCommonFeature = aDocument.addFeature("Sketch") +aSketchFeature = featureToCompositeFeature(aSketchCommonFeature) +origin = geomDataAPI_Point(aSketchFeature.attribute("Origin")) +origin.setValue(0, 0, 0) +dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX")) +dirx.setValue(1, 0, 0) +norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm")) +norm.setValue(0, 0, 1) +aSession.finishOperation() +aSketch = SketchAPI_Sketch(aSketchFeature) + +# auxiliary line +aLineStartPnt = [0., 0.] +aLineEndPnt = [50., 0.] +aSession.startOperation() +aSketchLine = aSketchFeature.addFeature("SketchLine") +aLineStart = geomDataAPI_Point2D(aSketchLine.attribute("StartPoint")) +aLineEnd = geomDataAPI_Point2D(aSketchLine.attribute("EndPoint")) +aLineStart.setValue(aLineStartPnt[0], aLineStartPnt[1]) +aLineEnd.setValue(aLineEndPnt[0], aLineEndPnt[1]) +aSession.finishOperation() + +#========================================================================= +# Test 1. Create an arc, tangent to the line +#========================================================================= +anArcEndPnt = [80., 20.] +aSession.startOperation() +anArc = aSketchFeature.addFeature("SketchMacroArc") +assert (anArc.getKind() == "SketchMacroArc") +anArcTgPnt = anArc.refattr("tangent_point") +assert (not anArcTgPnt.isInitialized()) +anArcEnd = geomDataAPI_Point2D(anArc.attribute("end_point_3")) +assert (not anArcEnd.isInitialized()) +anArcType = anArc.string("arc_type") +assert (not anArcType.isInitialized()) +# initialize attributes +anArcType.setValue("by_tangent_edge") +anArcTgPnt.setAttr(aLineEnd) +anArcEnd.setValue(anArcEndPnt[0], anArcEndPnt[1]) +aSession.finishOperation() +verifyLastArc(aSketchFeature, [], aLineEndPnt, anArcEndPnt) +aLastArc = model.lastSubFeature(aSketchFeature, "SketchArc") +verifyTangent(aLastArc, aSketchLine) +model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 1) +model.testNbSubFeatures(aSketch, "SketchConstraintTangent", 1) + +#========================================================================= +# Test 2. Create an arc, tangent to the previous arc +#========================================================================= +aPrevArc = aLastArc +aPrevArcEnd = geomDataAPI_Point2D(aPrevArc.attribute("end_point")) +anArcEndPnt = [50., 100.] +aSession.startOperation() +anArc = aSketchFeature.addFeature("SketchMacroArc") +anArcTgPnt = anArc.refattr("tangent_point") +anArcEnd = geomDataAPI_Point2D(anArc.attribute("end_point_3")) +anArcType = anArc.string("arc_type") +# initialize attributes +anArcType.setValue("by_tangent_edge") +anArcTgPnt.setAttr(aPrevArcEnd) +anArcEnd.setValue(anArcEndPnt[0], anArcEndPnt[1]) +aSession.finishOperation() +verifyLastArc(aSketchFeature, [], [aPrevArcEnd.x(), aPrevArcEnd.y()], anArcEndPnt) +aLastArc = model.lastSubFeature(aSketchFeature, "SketchArc") +verifyTangent(aLastArc, aPrevArc) +model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 2) +model.testNbSubFeatures(aSketch, "SketchConstraintTangent", 2) + +#========================================================================= +# Test 3. Create an arc, tangent to the previous arc with end point on the line +#========================================================================= +aPrevArc = aLastArc +aPrevArcEnd = geomDataAPI_Point2D(aPrevArc.attribute("end_point")) +aSession.startOperation() +anArc = aSketchFeature.addFeature("SketchMacroArc") +anArcTgPnt = anArc.refattr("tangent_point") +anArcEnd = geomDataAPI_Point2D(anArc.attribute("end_point_3")) +anArcEndRef = anArc.refattr("end_point_ref") +anArcType = anArc.string("arc_type") +# initialize attributes +anArcType.setValue("by_tangent_edge") +anArcTgPnt.setAttr(aPrevArcEnd) +anArcEndRef.setObject(aSketchLine.lastResult()) +anArcEnd.setValue(aLineStartPnt[0], aLineStartPnt[1]) +aSession.finishOperation() +verifyLastArc(aSketchFeature, [], [aPrevArcEnd.x(), aPrevArcEnd.y()], []) +aLastArc = model.lastSubFeature(aSketchFeature, "SketchArc") +verifyTangent(aLastArc, aPrevArc) +aLastArcEnd = geomDataAPI_Point2D(aLastArc.attribute("end_point")) +verifyPointOnLine(aLastArcEnd, aSketchLine) +model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 4) +model.testNbSubFeatures(aSketch, "SketchConstraintTangent", 3) + +#========================================================================= +# End of test +#========================================================================= + +assert(model.checkPythonDump()) -- 2.30.2