From ebb70e81c1053a4a463a3f1ccb9c9878a47f3901 Mon Sep 17 00:00:00 2001 From: NATHALIE GORE Date: Wed, 1 Feb 2023 13:30:53 +0100 Subject: [PATCH] Replace horizontal / vertical constraints by perpendicular constraints for rectangle --- .../Test/TestRectangleCentered.py | 29 ++++++++++++---- src/PythonAddons/macros/rectangle/feature.py | 33 ++++++++++--------- .../Test/TestConstraintDistanceBehavior.py | 8 +++-- .../Test/TestDistanceSignedVsUnsigned01.py | 6 ++-- .../Test/TestDistanceSignedVsUnsigned02.py | 4 ++- .../Test/TestDistanceSignedVsUnsigned03.py | 4 ++- .../Test/TestDistanceSignedVsUnsigned04.py | 4 ++- .../Test/TestDistanceSignedVsUnsigned05.py | 4 ++- src/SketchPlugin/Test/TestMovementComplex.py | 2 ++ src/SketchPlugin/Test/TestRectangle.py | 15 ++++++--- 10 files changed, 74 insertions(+), 35 deletions(-) diff --git a/src/PythonAddons/Test/TestRectangleCentered.py b/src/PythonAddons/Test/TestRectangleCentered.py index bd6cc1789..36934ca8b 100755 --- a/src/PythonAddons/Test/TestRectangleCentered.py +++ b/src/PythonAddons/Test/TestRectangleCentered.py @@ -22,8 +22,6 @@ from salome.shaper import geom from SketchAPI import * -import math - def checkRectangle(lines, center, corner, tolerance = 1.e-7): dx = corner.x() - center.x() dy = corner.y() - center.y() @@ -40,6 +38,13 @@ def checkRectangle(lines, center, corner, tolerance = 1.e-7): ep_ref = points[i] assert(ep.distance(ep_ref) <= tolerance) +def checkRectangleL(lines, valref, tolerance = 1.e-5): + for i in range(0, 4): + line = SketchAPI_Line(lines[i]) + #print (line.defaultResult().shape().edge().length()) + #print (valref[i%2]) + #print (abs(line.defaultResult().shape().edge().length()-valref[i%2])) + assert(abs(line.defaultResult().shape().edge().length()-valref[i%2]) <= tolerance) model.begin() partSet = model.moduleDocument() @@ -75,22 +80,34 @@ checkRectangle(lines_3, SketchAPI_Line(lines_1[0]).startPoint().pnt(), SketchAPI # move center of rectangle SHIFT = 1.0 center = SketchAPI_Line(lines_1[0]).startPoint().pnt() -for i in range(0, 20): +valref = [ \ +400.86931 , 200.78509 , \ +401.73886 , 201.57021 , \ +402.60865 , 202.35537 , \ +403.47866 , 203.14056 , \ +404.34890 , 203.92580 ] +for i in range(0, 5): center.setX(center.x() + SHIFT) center.setY(center.y() + SHIFT) model.begin() sketch.move(SketchAPI_Line(lines_1[0]).startPoint(), center) model.end() - checkRectangle(lines_3, center, SketchAPI_Line(lines_2[0]).endPoint().pnt()) + checkRectangleL(lines_3, valref[2*i:]) # move corner of rectangle corner = SketchAPI_Line(lines_2[0]).endPoint().pnt() -for i in range(0, 20): +valref = [ \ +403.11209 , 202.95437 , \ +401.87551 , 201.98300 , \ +400.63915 , 201.01169 , \ +399.40301 , 200.04043 , \ +398.16710 , 199.06922 ] +for i in range(0, 5): corner.setX(corner.x() + SHIFT) corner.setY(corner.y() + SHIFT) model.begin() sketch.move(SketchAPI_Line(lines_2[0]).endPoint(), corner) model.end() - checkRectangle(lines_3, SketchAPI_Line(lines_1[0]).startPoint().pnt(), corner) + checkRectangleL(lines_3, valref[2*i:]) assert(model.checkPythonDump()) diff --git a/src/PythonAddons/macros/rectangle/feature.py b/src/PythonAddons/macros/rectangle/feature.py index cb22211ac..747664f0b 100755 --- a/src/PythonAddons/macros/rectangle/feature.py +++ b/src/PythonAddons/macros/rectangle/feature.py @@ -156,8 +156,8 @@ class SketchPlugin_Rectangle(model.Feature): aRefAttrA.setAttr(aPrevLine.attribute("EndPoint")) aRefAttrB.setAttr(aLine.attribute("StartPoint")) aStartPoints.append(aLine.attribute("StartPoint")) - # Flags which show horizontal or vertical constraint is build for correponding line - self.__isHV = [False, False, False, False] + # Flags which show perpendicular constraint is build for correponding line + self.__isPERP = [False, False, False] # Update coordinates of created lines self.updateLines() # Create auxiliary diagonals in case of centered rectangle @@ -186,21 +186,24 @@ class SketchPlugin_Rectangle(model.Feature): aCoincidence = self.__sketch.addFeature("SketchConstraintCoincidence") aCoincidence.refattr("ConstraintEntityA").setAttr(refPnt) aCoincidence.refattr("ConstraintEntityB").setObject(line) - # Add horizontal and vertical constraint for the lines which already have result - for i in range (0, 4): - if self.__isHV[i]: + # Perpendicular for the lines which already have result + for i in range (0, 3): + if self.__isPERP[i]: continue - aLine = ModelAPI.objectToFeature(aLinesList.object(i)) - aLineResult = aLine.lastResult() - if aLineResult is None: + aLine_A = ModelAPI.objectToFeature(aLinesList.object(i)) + aLineResult_A = aLine_A.lastResult() + if aLineResult_A is None: + continue + aLine_B = ModelAPI.objectToFeature(aLinesList.object(i+1)) + aLineResult_B = aLine_B.lastResult() + if aLineResult_B is None: continue - aHVName = "SketchConstraintHorizontal" - if i % 2 == 1: - aHVName = "SketchConstraintVertical" - aHVConstraint = self.__sketch.addFeature(aHVName) - aRefAttrA = aHVConstraint.refattr("ConstraintEntityA") - aRefAttrA.setObject(aLine.lastResult()) - self.__isHV[i] = True + aHVConstraint = self.__sketch.addFeature("SketchConstraintPerpendicular") + refattrA = aHVConstraint.refattr("ConstraintEntityA") + refattrA.setObject(aLine_A.lastResult()) + refattrB = aHVConstraint.refattr("ConstraintEntityB") + refattrB.setObject(aLine_B.lastResult()) + self.__isPERP[i] = True def attributeChanged(self, theID): if theID == self.START_ID() or theID == self.END_ID() or theID == self.CENTER_ID() or theID == self.CENTER_REF_ID() or theID == self.CORNER_ID(): diff --git a/src/SketchPlugin/Test/TestConstraintDistanceBehavior.py b/src/SketchPlugin/Test/TestConstraintDistanceBehavior.py index 753dd5dfa..8fa111e7d 100644 --- a/src/SketchPlugin/Test/TestConstraintDistanceBehavior.py +++ b/src/SketchPlugin/Test/TestConstraintDistanceBehavior.py @@ -31,6 +31,7 @@ DistanceParam = model.addParameter(Part_1_doc, "distance", "10.") Sketch_1 = model.addSketch(Part_1_doc, model.defaultPlane("XOY")) SketchRectangle_1 = Sketch_1.addRectangle(20., 20., 70., 50.) [SketchLine_1, SketchLine_2, SketchLine_3, SketchLine_4] = SketchRectangle_1.lines() +Sketch_1.setVertical(SketchLine_4.result()) firstPoint = SketchAPI_Line(SketchLine_2).startPoint() secondPoint = SketchAPI_Line(SketchLine_3).endPoint() model.do() @@ -44,7 +45,7 @@ SketchConstraintDistanceHorizontal_1.feature().real("ConstraintValue").setText(D model.do() # changing the parameter -for param in range(-31, 30, 2): +for param in range(11, 31, 2): if param == 0: continue DistanceParam.setValue(param) @@ -68,7 +69,7 @@ SketchConstraintDistanceVertical_1.feature().real("ConstraintValue").setText(Dis model.do() # changing the parameter -for param in range(-31, 30, 2): +for param in range(11, 31, 2): if param == 0: continue DistanceParam.setValue(param) @@ -91,7 +92,7 @@ SketchConstraintDistance_1 = Sketch_1.setDistance(firstPoint, secondPoint, "dist model.do() # changing the parameter -for param in range(-30, 31, 2): +for param in range(11, 31, 2): DistanceParam.setValue(param) model.do() if param < 0: @@ -107,3 +108,4 @@ model.testNbSubFeatures(Sketch_1, "SketchConstraintDistance", 1) # leave distance constraint alive model.end() + diff --git a/src/SketchPlugin/Test/TestDistanceSignedVsUnsigned01.py b/src/SketchPlugin/Test/TestDistanceSignedVsUnsigned01.py index 652f6b39a..ec4a4a918 100644 --- a/src/SketchPlugin/Test/TestDistanceSignedVsUnsigned01.py +++ b/src/SketchPlugin/Test/TestDistanceSignedVsUnsigned01.py @@ -30,6 +30,7 @@ distParam = model.addParameter(partSet, "d", "30") Sketch_1 = model.addSketch(partSet, model.defaultPlane("XOY")) Rectangle_1 = Sketch_1.addRectangle(0, 0, 200, 100) [Line_1, Line_2, Line_3, Line_4] = Rectangle_1.lines() +Sketch_1.setVertical(Line_4.result()) Origin = Sketch_1.addPoint(model.selection("VERTEX", "Origin")) Sketch_1.setCoincident(SketchAPI_Line(Line_1).endPoint(), Origin.result()) Sketch_1.setLength(SketchAPI_Line(Line_1).result(), "w") @@ -45,7 +46,7 @@ signedDist1 = model.signedDistancePointLine(Point_1, line) signedDist2 = model.signedDistancePointLine(Point_2, line) # change rectangle width and check distances -widthParam.setValue(2000) +widthParam.setValue(300) model.do() curDist = model.signedDistancePointLine(Point_1, line) assert(math.fabs(signedDist1 - curDist) < TOLERANCE), "Expected {}, actual {}".format(signedDist1, curDist) @@ -54,7 +55,7 @@ assert(math.fabs(math.fabs(signedDist2) - math.fabs(curDist)) < TOLERANCE), "Exp assert Sketch_1.solverError().value() == "", "FAILED: Sketch should NOT report over-constrained situation" # revert rectangle width and check distances again -widthParam.setValue(200) +widthParam.setValue(300) model.do() curDist = model.signedDistancePointLine(Point_1, line) assert(math.fabs(signedDist1 - curDist) < TOLERANCE), "Expected {}, actual {}".format(signedDist1, curDist) @@ -63,3 +64,4 @@ assert(math.fabs(math.fabs(signedDist2) - math.fabs(curDist)) < TOLERANCE), "Exp assert Sketch_1.solverError().value() == "", "FAILED: Sketch should NOT report over-constrained situation" model.end() + diff --git a/src/SketchPlugin/Test/TestDistanceSignedVsUnsigned02.py b/src/SketchPlugin/Test/TestDistanceSignedVsUnsigned02.py index 480ab72b0..cf0f80459 100644 --- a/src/SketchPlugin/Test/TestDistanceSignedVsUnsigned02.py +++ b/src/SketchPlugin/Test/TestDistanceSignedVsUnsigned02.py @@ -30,6 +30,7 @@ distParam = model.addParameter(partSet, "d", "30") Sketch_1 = model.addSketch(partSet, model.defaultPlane("XOY")) Rectangle_1 = Sketch_1.addRectangle(0, 0, 200, 100) [Line_1, Line_2, Line_3, Line_4] = Rectangle_1.lines() +Sketch_1.setVertical(Line_4.result()) Point_1 = Sketch_1.addPoint(model.selection("VERTEX", "Origin")) Sketch_1.setCoincident(SketchAPI_Line(Line_1).endPoint(), Point_1.result()) Sketch_1.setLength(SketchAPI_Line(Line_1).result(), "w") @@ -47,7 +48,7 @@ signedDist1 = model.signedDistancePointLine(center1, line) signedDist2 = model.signedDistancePointLine(center2, line) # change rectangle width and check distances -widthParam.setValue(2000) +widthParam.setValue(300) model.do() curDist = model.signedDistancePointLine(center1, line) assert(math.fabs(signedDist1 - curDist) < TOLERANCE), "Expected {}, actual {}".format(signedDist1, curDist) @@ -65,3 +66,4 @@ assert(math.fabs(math.fabs(signedDist2) - math.fabs(curDist)) < TOLERANCE), "Exp assert Sketch_1.solverError().value() == "", "FAILED: Sketch should NOT report over-constrained situation" model.end() + diff --git a/src/SketchPlugin/Test/TestDistanceSignedVsUnsigned03.py b/src/SketchPlugin/Test/TestDistanceSignedVsUnsigned03.py index c2623e4ee..c6ebd6354 100644 --- a/src/SketchPlugin/Test/TestDistanceSignedVsUnsigned03.py +++ b/src/SketchPlugin/Test/TestDistanceSignedVsUnsigned03.py @@ -30,6 +30,7 @@ distParam = model.addParameter(partSet, "d", "30") Sketch_1 = model.addSketch(partSet, model.defaultPlane("XOY")) Rectangle_1 = Sketch_1.addRectangle(0, 0, 200, 100) [Line_1, Line_2, Line_3, Line_4] = Rectangle_1.lines() +Sketch_1.setVertical(Line_4.result()) Point_1 = Sketch_1.addPoint(model.selection("VERTEX", "Origin")) Sketch_1.setCoincident(SketchAPI_Line(Line_1).endPoint(), Point_1.result()) Sketch_1.setLength(SketchAPI_Line(Line_1).result(), "w") @@ -46,7 +47,7 @@ signedDist1 = model.signedDistancePointLine(start, line) signedDist2 = model.signedDistancePointLine(end, line) # change rectangle width and check distances -widthParam.setValue(2000) +widthParam.setValue(300) model.do() curDist = model.signedDistancePointLine(start, line) assert(math.fabs(signedDist1 - curDist) < TOLERANCE), "Expected {}, actual {}".format(signedDist1, curDist) @@ -64,3 +65,4 @@ assert(math.fabs(math.fabs(signedDist2) - math.fabs(curDist)) < TOLERANCE), "Exp assert Sketch_1.solverError().value() == "", "FAILED: Sketch should NOT report over-constrained situation" model.end() + diff --git a/src/SketchPlugin/Test/TestDistanceSignedVsUnsigned04.py b/src/SketchPlugin/Test/TestDistanceSignedVsUnsigned04.py index 24ac9ea24..031da14c8 100644 --- a/src/SketchPlugin/Test/TestDistanceSignedVsUnsigned04.py +++ b/src/SketchPlugin/Test/TestDistanceSignedVsUnsigned04.py @@ -30,6 +30,7 @@ distParam = model.addParameter(partSet, "d", "30") Sketch_1 = model.addSketch(partSet, model.defaultPlane("XOY")) Rectangle_1 = Sketch_1.addRectangle(0, 0, 200, 100) [Line_1, Line_2, Line_3, Line_4] = Rectangle_1.lines() +Sketch_1.setVertical(Line_4.result()) Point_1 = Sketch_1.addPoint(model.selection("VERTEX", "Origin")) Sketch_1.setCoincident(SketchAPI_Line(Line_1).endPoint(), Point_1.result()) Sketch_1.setLength(SketchAPI_Line(Line_1).result(), "w") @@ -46,7 +47,7 @@ signedDist1 = model.signedDistancePointLine(start, line) signedDist2 = model.signedDistancePointLine(end, line) # change rectangle width and check distances -widthParam.setValue(1000) +widthParam.setValue(300) model.do() curDist = model.signedDistancePointLine(start, line) assert(math.fabs(signedDist1 - curDist) < TOLERANCE), "Expected {}, actual {}".format(signedDist1, curDist) @@ -66,3 +67,4 @@ model.assertArcValidity(Arc_1) assert Sketch_1.solverError().value() == "", "FAILED: Sketch should NOT report over-constrained situation" model.end() + diff --git a/src/SketchPlugin/Test/TestDistanceSignedVsUnsigned05.py b/src/SketchPlugin/Test/TestDistanceSignedVsUnsigned05.py index aaa493a3e..da5b4af0e 100644 --- a/src/SketchPlugin/Test/TestDistanceSignedVsUnsigned05.py +++ b/src/SketchPlugin/Test/TestDistanceSignedVsUnsigned05.py @@ -30,6 +30,7 @@ distParam = model.addParameter(partSet, "d", "30") Sketch_1 = model.addSketch(partSet, model.defaultPlane("XOY")) Rectangle_1 = Sketch_1.addRectangle(0, 0, 200, 100) [Line_1, Line_2, Line_3, Line_4] = Rectangle_1.lines() +Sketch_1.setVertical(Line_4.result()) Origin = Sketch_1.addPoint(model.selection("VERTEX", "Origin")) Sketch_1.setCoincident(SketchAPI_Line(Line_1).endPoint(), Origin.result()) Sketch_1.setLength(SketchAPI_Line(Line_1).result(), "w") @@ -51,7 +52,7 @@ signedDist24 = model.signedDistancePointLine(Point_2, line4) signedDist25 = model.signedDistancePointLine(Point_2, line5) # change rectangle width and check distances -widthParam.setValue(2000) +widthParam.setValue(300) model.do() curDist = model.signedDistancePointLine(Point_1, line4) assert(math.fabs(signedDist14 - curDist) < TOLERANCE), "Expected {}, actual {}".format(signedDist14, curDist) @@ -77,3 +78,4 @@ assert(math.fabs(math.fabs(signedDist25) - math.fabs(curDist)) < TOLERANCE), "Ex assert Sketch_1.solverError().value() == "", "FAILED: Sketch should NOT report over-constrained situation" model.end() + diff --git a/src/SketchPlugin/Test/TestMovementComplex.py b/src/SketchPlugin/Test/TestMovementComplex.py index 07d86c56a..f93052187 100644 --- a/src/SketchPlugin/Test/TestMovementComplex.py +++ b/src/SketchPlugin/Test/TestMovementComplex.py @@ -209,6 +209,7 @@ model.do() Sketch_2 = model.addSketch(Part_1_doc, model.defaultPlane("XOZ")) SketchRectangle_1 = Sketch_2.addRectangle(20, 0, 100, 50) [SketchLine_6, SketchLine_7, SketchLine_8, SketchLine_9] = SketchRectangle_1.lines() +Sketch_2.setVertical(SketchLine_9.result()) SketchProjection_1 = Sketch_2.addProjection(model.selection("EDGE", "Sketch_1/SketchLine_1")) SketchLine_10 = SketchProjection_1.createdFeature() SketchConstraintCoincidence_7 = Sketch_2.setCoincident(SketchAPI_Line(SketchLine_7).startPoint(), SketchAPI_Line(SketchLine_10).startPoint()) @@ -382,3 +383,4 @@ model.testNbSubShapes(ExtrusionFuse_1, GeomAPI_Shape.VERTEX, [280]) model.testResultsVolumes(ExtrusionFuse_1, [260653.824775497108930721879005432]) assert(model.checkPythonDump()) + diff --git a/src/SketchPlugin/Test/TestRectangle.py b/src/SketchPlugin/Test/TestRectangle.py index 714d5ca59..2305764ba 100644 --- a/src/SketchPlugin/Test/TestRectangle.py +++ b/src/SketchPlugin/Test/TestRectangle.py @@ -19,7 +19,7 @@ """ TestRectangle.py - Unit test of SketchPlugin_Ractangle class + Unit test of SketchPlugin_Rectangle class """ from GeomDataAPI import * @@ -88,27 +88,32 @@ for i in range (0, aNbSubs): assert (isHorizontal(aLastLine) or isVertical(aLastLine)) aNbLines = aNbLines + 1 assert (aNbLines == 4) -assert (model.dof(aSketchFeature) == 4) +assert (model.dof(aSketchFeature) == 5) #========================================================================= # Move one of lines #========================================================================= aSession.startOperation() aLineEnd = geomDataAPI_Point2D(aLastLine.attribute("EndPoint")) -aLineEnd.setValue(50., 50.) +aLineEnd.setValue(41., 30.) aSession.finishOperation() #========================================================================= # Check the lines of rectangle are parallel to the axes #========================================================================= aNbSubs = aSketchFeature.numberOfSubs() aNbLines = 0 +tolerance = 1.e-5 +valref = [28.47721, 1.3352780] for i in range (0, aNbSubs): aFeature = objectToFeature(aSketchFeature.subFeature(i)) if aFeature.getKind() == "SketchLine": aLastLine = aFeature - assert (isHorizontal(aLastLine) or isVertical(aLastLine)) + #print (aLastLine.lastResult().shape().edge().length()) + #print (valref[i%2]) + #print (abs(aLastLine.lastResult().shape().edge().length()-valref[i%2])) + assert(abs(aLastLine.lastResult().shape().edge().length()-valref[i%2]) <= tolerance) aNbLines = aNbLines + 1 assert (aNbLines == 4) -assert (model.dof(aSketchFeature) == 4) +assert (model.dof(aSketchFeature) == 5) #========================================================================= # End of test #========================================================================= -- 2.30.2