]> SALOME platform Git repositories - modules/shaper.git/blobdiff - src/SketchPlugin/Test/TestCreateArcByCenterStartEnd.py
Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / SketchPlugin / Test / TestCreateArcByCenterStartEnd.py
index 5cbe2fafb90255cf4ac446b32f669312d3037e9f..097e636b2a1177df781d1eb4dbdbb7abd2672b4f 100644 (file)
@@ -1,3 +1,23 @@
+## Copyright (C) 2014-2017  CEA/DEN, EDF R&D
+##
+## 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<mailto:webmaster.salome@opencascade.com>
+##
+
 """
     TestCreateArc.py
 
@@ -15,6 +35,7 @@
 #=========================================================================
 from GeomDataAPI import *
 from ModelAPI import *
+from SketchAPI import SketchAPI_Sketch
 import math
 from salome.shaper import model
 
@@ -45,6 +66,30 @@ def verifyLastArc(theSketch, theCenter, theStart, theEnd):
 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 verifyPointOnLine(thePoint, theLine):
+    aDistance = distancePointLine(thePoint, theLine)
+    assert aDistance < TOLERANCE, "Point is not on Line, distance: {0}".format(aDistance)
+
+def verifyPointOnCircle(thePoint, theCircular):
+    if theCircular.getKind() == "SketchArc":
+        aCenterPoint = geomDataAPI_Point2D(theCircular.attribute("center_point"))
+        aStartPoint = geomDataAPI_Point2D(theCircular.attribute("start_point"))
+        aRadius = model.distancePointPoint(aCenterPoint, aStartPoint)
+    elif theCircular.getKind() == "SketchCircle":
+        aCenterPoint = geomDataAPI_Point2D(theCircular.attribute("circle_center"))
+        aRadius = theCircular.real("circle_radius").value()
+    else:
+        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()
+    aLineDir = aLineEnd.decreased(aLineStart)
+    aLineLen = aLineEnd.distance(aLineStart)
+    aPntDir = thePoint.pnt().xy().decreased(aLineStart)
+    return math.fabs(aPntDir.cross(aLineDir) / aLineLen)
+
 
 aSession = ModelAPI_Session.get()
 aDocument = aSession.moduleDocument()
@@ -61,6 +106,7 @@ dirx.setValue(1, 0, 0)
 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
 norm.setValue(0, 0, 1)
 aSession.finishOperation()
+aSketch = SketchAPI_Sketch(aSketchFeature)
 
 #=========================================================================
 # Test 1. Create an arc by center, start and end points
@@ -118,6 +164,137 @@ aSession.finishOperation()
 assert (math.fabs(anArcAngle.value() - aPrevAngle) < TOLERANCE)
 verifyLastArc(aSketchFeature, [], [], [])
 
+#=========================================================================
+# Test 2. Create an arc as a macro-feature by center, start and end points
+#=========================================================================
+aSession.startOperation()
+anArc = aSketchFeature.addFeature("SketchMacroArc")
+assert (anArc.getKind() == "SketchMacroArc")
+anArcCenter = geomDataAPI_Point2D(anArc.attribute("center_point"))
+assert (not anArcCenter.isInitialized())
+anArcStart = geomDataAPI_Point2D(anArc.attribute("start_point_1"))
+assert (not anArcStart.isInitialized())
+anArcEnd = geomDataAPI_Point2D(anArc.attribute("end_point_1"))
+assert (not anArcEnd.isInitialized())
+anArcType = anArc.string("arc_type")
+assert (not anArcType.isInitialized())
+anArcType.setValue("by_center_and_points")
+anArcCenter.setValue(aCenter[0], aCenter[1])
+anArcStart.setValue(aStart[0], aStart[1])
+anArcEnd.setValue(aEnd[0], aEnd[1])
+aSession.finishOperation()
+assert (aSketchFeature.numberOfSubs() == 2)
+verifyLastArc(aSketchFeature, [], [], [])
+
+#=========================================================================
+# Test 3. Create an arc by center and two points coincident to other points
+#=========================================================================
+# get previous arc
+aPrevArc = model.lastSubFeature(aSketchFeature, "SketchArc")
+aPrevArcStart = geomDataAPI_Point2D(aPrevArc.attribute("start_point"))
+aPrevArcEnd = geomDataAPI_Point2D(aPrevArc.attribute("end_point"))
+# create additional point
+aPointCoordinates = [0., 0.]
+aSession.startOperation()
+aPoint = aSketchFeature.addFeature("SketchPoint")
+aPointCoord = geomDataAPI_Point2D(aPoint.attribute("PointCoordinates"))
+aPointCoord.setValue(aPointCoordinates[0], aPointCoordinates[1])
+aPoint.selection("External").selectSubShape("VERTEX", "Origin")
+aSession.finishOperation()
+# create additional line
+aLineStart = [20., -5.]
+aLineEnd = [20., 20]
+aSession.startOperation()
+aLine = aSketchFeature.addFeature("SketchLine")
+aLineStartPoint = geomDataAPI_Point2D(aLine.attribute("StartPoint"))
+aLineEndPoint = geomDataAPI_Point2D(aLine.attribute("EndPoint"))
+aLineStartPoint.setValue(aLineStart[0], aLineStart[1])
+aLineEndPoint.setValue(aLineEnd[0], aLineEnd[1])
+aSession.finishOperation()
+# create new arc
+aSession.startOperation()
+anArc = aSketchFeature.addFeature("SketchMacroArc")
+aCenter = geomDataAPI_Point2D(anArc.attribute("center_point"))
+aCenterRef = anArc.refattr("center_point_ref")
+assert (not aCenterRef.isInitialized())
+aStart = geomDataAPI_Point2D(anArc.attribute("start_point_1"))
+aStartRef = anArc.refattr("start_point_ref")
+assert (not aStartRef.isInitialized())
+aEnd = geomDataAPI_Point2D(anArc.attribute("end_point_1"))
+aEndRef = anArc.refattr("end_point_ref")
+assert (not aEndRef.isInitialized())
+anArcType = anArc.string("arc_type")
+assert (not anArcType.isInitialized())
+# initialize attributes
+anArcType.setValue("by_center_and_points")
+aCenterRef.setAttr(aPrevArcStart)
+aCenter.setValue(aPrevArcStart.pnt())
+aStartRef.setObject(aPoint.lastResult())
+aStart.setValue(aPointCoord.pnt())
+aEndRef.setAttr(aPrevArcEnd)
+aEnd.setValue(aPrevArcEnd.pnt())
+aSession.finishOperation()
+# check the MacroArc is not valid (selection of end point is not possible)
+aLastFeature = aSketchFeature.subFeature(aSketchFeature.numberOfSubs() - 1)
+assert aLastFeature.getKind() == "SketchMacroArc", "ERROR: SketchMacroArc has NOT expected to be valid"
+# change end point reference to a line
+aSession.startOperation()
+aEndRef.setObject(aLine.lastResult())
+aEnd.setValue(aLineStart[0], aLineStart[1])
+aSession.finishOperation()
+assert (aSketchFeature.numberOfSubs() == 8), "Number of subs {}".format(aSketchFeature.numberOfSubs())
+verifyPointCoordinates(aPointCoord, aPointCoordinates[0], aPointCoordinates[1])
+verifyLastArc(aSketchFeature, [aPrevArcStart.x(), aPrevArcStart.y()], aPointCoordinates, [])
+model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 3)
+
+#=========================================================================
+# Test 4. Create an arc by center and points coincident to other features
+#=========================================================================
+# get previous arc
+aPrevArc = model.lastSubFeature(aSketchFeature, "SketchArc")
+aPrevArcCenter = geomDataAPI_Point2D(aPrevArc.attribute("center_point"))
+aPrevArcStart = geomDataAPI_Point2D(aPrevArc.attribute("start_point"))
+aPrevArcEnd = geomDataAPI_Point2D(aPrevArc.attribute("end_point"))
+aPrevArcCenterXY = [aPrevArcCenter.x(), aPrevArcCenter.y()]
+aPrevArcStartXY = [aPrevArcStart.x(), aPrevArcStart.y()]
+aPrevArcEndXY = [aPrevArcEnd.x(), aPrevArcEnd.y()]
+# create new arc
+aSession.startOperation()
+anArc = aSketchFeature.addFeature("SketchMacroArc")
+aCenter = geomDataAPI_Point2D(anArc.attribute("center_point"))
+aCenterRef = anArc.refattr("center_point_ref")
+aStart = geomDataAPI_Point2D(anArc.attribute("start_point_1"))
+aStartRef = anArc.refattr("start_point_ref")
+aEnd = geomDataAPI_Point2D(anArc.attribute("end_point_1"))
+aEndRef = anArc.refattr("end_point_ref")
+anArcType = anArc.string("arc_type")
+# initialize attributes
+anArcType.setValue("by_center_and_points")
+aCenterRef.setObject(aLine.lastResult())
+aCenter.setValue(aLineStartPoint.pnt())
+aStartRef.setObject(aPrevArc.lastResult())
+aStart.setValue(aPrevArcStart.pnt())
+aEndRef.setObject(aLine.lastResult())
+aEnd.setValue(aLineEndPoint.pnt())
+aSession.finishOperation()
+assert (aSketchFeature.numberOfSubs() == 12), "Number of subs {}".format(aSketchFeature.numberOfSubs())
+# check connected features do not change their positions
+verifyPointCoordinates(aPrevArcCenter, aPrevArcCenterXY[0], aPrevArcCenterXY[1])
+verifyPointCoordinates(aPrevArcStart, aPrevArcStartXY[0], aPrevArcStartXY[1])
+verifyPointCoordinates(aPrevArcEnd, aPrevArcEndXY[0], aPrevArcEndXY[1])
+verifyPointCoordinates(aLineStartPoint, aLineStart[0], aLineStart[1])
+verifyPointCoordinates(aLineEndPoint, aLineEnd[0], aLineEnd[1])
+# verify newly created arc
+anArc = model.lastSubFeature(aSketchFeature, "SketchArc")
+aCenter = geomDataAPI_Point2D(anArc.attribute("center_point"))
+aStart = geomDataAPI_Point2D(anArc.attribute("start_point"))
+aEnd = geomDataAPI_Point2D(anArc.attribute("end_point"))
+verifyPointOnLine(aCenter, aLine)
+verifyPointOnLine(aEnd, aLine)
+verifyPointOnCircle(aStart, aPrevArc)
+model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 6)
+model.testNbSubFeatures(aSketch, "SketchConstraintTangent", 0)
+
 #=========================================================================
 # End of test
 #=========================================================================