1 # Copyright (C) 2017-2019 CEA/DEN, EDF R&D
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # Lesser General Public License for more details.
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 TestConstraintDistanceHorizontal.py
22 Unit test of SketchPlugin_ConstraintDistanceHorizontal class
24 SketchPlugin_ConstraintDistanceHorizontal
25 static const std::string MY_CONSTRAINT_DISTANCE_ID("SketchConstraintDistance");
26 data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
27 data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
28 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
29 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
32 from GeomDataAPI import *
33 from ModelAPI import *
35 from salome.shaper import model
37 #=========================================================================
38 # Initialization of the test
39 #=========================================================================
41 __updated__ = "2017-05-10"
44 def horizontalDistance(point1, point2):
46 subroutine to calculate signed distance between two points
48 return point2.x() - point1.x()
50 aSession = ModelAPI_Session.get()
51 aDocument = aSession.moduleDocument()
52 #=========================================================================
53 # Creation of a sketch
54 #=========================================================================
55 aSession.startOperation()
56 aSketchCommonFeature = aDocument.addFeature("Sketch")
57 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
58 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
59 origin.setValue(0, 0, 0)
60 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
61 dirx.setValue(1, 0, 0)
62 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
63 norm.setValue(0, 0, 1)
64 aSession.finishOperation()
65 #=========================================================================
66 # Create two movable and one fixed point
67 #=========================================================================
68 aSession.startOperation()
69 aPoint1 = aSketchFeature.addFeature("SketchPoint")
70 aPoint1Coords = geomDataAPI_Point2D(aPoint1.attribute("PointCoordinates"))
71 aPoint1Coords.setValue(50., 50.)
72 aSession.finishOperation()
73 aSession.startOperation()
74 aPoint2 = aSketchFeature.addFeature("SketchPoint")
75 aPoint2Coords = geomDataAPI_Point2D(aPoint2.attribute("PointCoordinates"))
76 aPoint2Coords.setValue(70., 70.)
77 aSession.finishOperation()
78 aSession.startOperation()
79 anOriginResult = modelAPI_Result(aDocument.objectByName("Construction", "Origin"))
80 anOriginShape = anOriginResult.shape()
81 anExtPoint = aSketchFeature.addFeature("SketchPoint")
82 anExtCoords = geomDataAPI_Point2D(anExtPoint.attribute("PointCoordinates"))
83 anExtCoords.setValue(0., 0.)
84 anExtPoint.selection("External").setValue(anOriginResult, anOriginShape)
85 aSession.finishOperation()
86 assert (model.dof(aSketchFeature) == 4)
88 #=========================================================================
89 # Create a constraint to keep horizontal distance between movable points
90 #=========================================================================
92 aSession.startOperation()
93 aHDist1 = aSketchFeature.addFeature("SketchConstraintDistanceHorizontal")
94 aDistance = aHDist1.real("ConstraintValue")
95 refattrA = aHDist1.refattr("ConstraintEntityA")
96 refattrB = aHDist1.refattr("ConstraintEntityB")
97 assert (not aDistance.isInitialized())
98 assert (not refattrA.isInitialized())
99 assert (not refattrB.isInitialized())
100 refattrA.setObject(aPoint1.lastResult())
101 refattrB.setObject(aPoint2.lastResult())
102 aSession.finishOperation()
103 assert (model.dof(aSketchFeature) == 3)
104 # set flyout point then abort operation, after that check the Distance is correct
105 aSession.startOperation()
106 aFlyoutPoint = geomDataAPI_Point2D(aHDist1.attribute("ConstraintFlyoutValuePnt"))
107 aFlyoutPoint.setValue(50.0, 100.0)
108 aSession.abortOperation()
109 assert (refattrA.isInitialized())
110 assert (refattrB.isInitialized())
111 assert (aDistance.isInitialized())
112 aSession.startOperation()
113 aDistance.setValue(DISTANCE1)
114 aSession.finishOperation()
115 assert math.fabs(horizontalDistance(aPoint1Coords, aPoint2Coords) - DISTANCE1) < 1.e-5, "Distance values are different: {0} != {1}".format(horizontalDistance(aPoint1Coords, aPoint2Coords), DISTANCE1)
116 assert (model.dof(aSketchFeature) == 3)
117 #=========================================================================
118 # Change a distance value
119 #=========================================================================
123 aSession.startOperation()
125 aDistance.setValue(DISTANCE1)
126 aSession.finishOperation()
127 assert math.fabs(horizontalDistance(aPoint1Coords, aPoint2Coords) - DISTANCE1) < 1.e-5, "Distance values are different: {0} != {1}".format(horizontalDistance(aPoint1Coords, aPoint2Coords), DISTANCE1)
129 assert (model.dof(aSketchFeature) == 3)
131 #=========================================================================
132 # Create a constraint to keep horizontal distance between fixed and movable points
133 #=========================================================================
135 aSession.startOperation()
136 aHDist2 = aSketchFeature.addFeature("SketchConstraintDistanceHorizontal")
137 aDistance = aHDist2.real("ConstraintValue")
138 refattrA = aHDist2.refattr("ConstraintEntityA")
139 refattrB = aHDist2.refattr("ConstraintEntityB")
140 assert (not aDistance.isInitialized())
141 assert (not refattrA.isInitialized())
142 assert (not refattrB.isInitialized())
143 refattrA.setObject(anExtPoint.lastResult())
144 refattrB.setAttr(aPoint1Coords)
145 aDistance.setValue(DISTANCE2)
146 aSession.finishOperation()
147 assert math.fabs(horizontalDistance(anExtCoords, aPoint1Coords) - DISTANCE2) < 1.e-5, "Distance values are different: {0} != {1}".format(horizontalDistance(anExtCoords, aPoint1Coords), DISTANCE2)
148 assert math.fabs(aPoint1Coords.x() - DISTANCE2) < 1.e-5, "Wrong point coordinates ({}, {}), expected x = {}".format(aPoint1Coords.x(), aPoint1Coords.y(), DISTANCE2)
149 assert (model.dof(aSketchFeature) == 2)
150 #=========================================================================
151 # Change a distance value (check previous constraint is applied too)
152 #=========================================================================
156 aSession.startOperation()
158 aDistance.setValue(DISTANCE2)
159 aSession.finishOperation()
160 assert math.fabs(horizontalDistance(anExtCoords, aPoint1Coords) - DISTANCE2) < 1.e-5, "Distance values are different: {0} != {1}".format(horizontalDistance(anExtCoords, aPoint1Coords), DISTANCE2)
161 assert math.fabs(aPoint1Coords.x() - DISTANCE2) < 1.e-5, "Wrong point coordinates ({}, {}), expected x = {}".format(aPoint1Coords.x(), aPoint1Coords.y(), DISTANCE2)
162 assert math.fabs(horizontalDistance(aPoint1Coords, aPoint2Coords) - DISTANCE1) < 1.e-5, "Distance values are different: {0} != {1}".format(horizontalDistance(aPoint1Coords, aPoint2Coords), DISTANCE1)
164 assert (model.dof(aSketchFeature) == 2)
166 #=========================================================================
167 # Remove first distance
168 #=========================================================================
169 aStoredCoords = [aPoint2Coords.x(), aPoint2Coords.y()]
170 aSession.startOperation()
171 aDocument.removeFeature(aHDist1)
172 aSession.finishOperation()
173 assert (model.dof(aSketchFeature) == 3)
174 aSession.startOperation()
176 aDistance.setValue(DISTANCE2)
177 aSession.finishOperation()
178 assert math.fabs(horizontalDistance(anExtCoords, aPoint1Coords) - DISTANCE2) < 1.e-5, "Distance values are different: {0} != {1}".format(horizontalDistance(anExtCoords, aPoint1Coords), DISTANCE2)
179 assert math.fabs(aPoint1Coords.x() - DISTANCE2) < 1.e-5, "Wrong point coordinates ({}, {}), expected x = {}".format(aPoint1Coords.x(), aPoint1Coords.y(), DISTANCE2)
180 assert aPoint2Coords.x() == aStoredCoords[0] and aPoint2Coords.y() == aStoredCoords[1]
182 #=========================================================================
183 # Create line and set horizontal distance between line boundaries
184 #=========================================================================
185 aSession.startOperation()
186 aLine = aSketchFeature.addFeature("SketchLine")
187 aStartPoint = geomDataAPI_Point2D(aLine.attribute("StartPoint"))
188 aEndPoint = geomDataAPI_Point2D(aLine.attribute("EndPoint"))
189 aStartPoint.setValue(50., 0.)
190 aEndPoint.setValue(100., 20.)
191 aSession.finishOperation()
192 assert (model.dof(aSketchFeature) == 7)
195 aSession.startOperation()
196 aHDist3 = aSketchFeature.addFeature("SketchConstraintDistanceHorizontal")
197 aDistance = aHDist3.real("ConstraintValue")
198 refattrA = aHDist3.refattr("ConstraintEntityA")
199 refattrB = aHDist3.refattr("ConstraintEntityB")
200 assert (not aDistance.isInitialized())
201 assert (not refattrA.isInitialized())
202 assert (not refattrB.isInitialized())
203 refattrA.setAttr(aStartPoint)
204 refattrB.setAttr(aEndPoint)
205 aDistance.setValue(DISTANCE3)
206 aSession.finishOperation()
207 assert math.fabs(horizontalDistance(aStartPoint, aEndPoint) - DISTANCE3) < 1.e-5, "Distance values are different: {0} != {1}".format(horizontalDistance(aStartPoint, aEndPoint), DISTANCE3)
208 assert (model.dof(aSketchFeature) == 6)
209 #=========================================================================
210 # Change a distance value
211 #=========================================================================
215 aSession.startOperation()
217 aDistance.setValue(DISTANCE3)
218 aSession.finishOperation()
219 assert math.fabs(horizontalDistance(aStartPoint, aEndPoint) - DISTANCE3) < 1.e-5, "Distance values are different: {0} != {1}".format(horizontalDistance(aStartPoint, aEndPoint), DISTANCE3)
221 assert (model.dof(aSketchFeature) == 6)
223 #=========================================================================
225 #=========================================================================
227 assert(model.checkPythonDump())