Salome HOME
Updated copyright comment
[modules/shaper.git] / src / SketchPlugin / Test / TestConstraintDistanceVertical.py
1 # Copyright (C) 2017-2024  CEA, EDF
2 #
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.
7 #
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.
12 #
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
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19
20 """
21     TestConstraintDistanceVertical.py
22     Unit test of SketchPlugin_ConstraintDistanceVertical class
23
24     SketchPlugin_ConstraintDistanceVertical
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());
30 """
31
32 from GeomDataAPI import *
33 from ModelAPI import *
34 import math
35 from salome.shaper import model
36
37 #=========================================================================
38 # Initialization of the test
39 #=========================================================================
40
41 __updated__ = "2017-05-10"
42
43
44 def verticalDistance(point1, point2):
45     """
46     subroutine to calculate signed distance between two points
47     """
48     return point2.y() - point1.y()
49
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)
87
88 #=========================================================================
89 # Create a constraint to keep vertical distance between movable points
90 #=========================================================================
91 DISTANCE1 = 25.
92 aSession.startOperation()
93 aVDist1 = aSketchFeature.addFeature("SketchConstraintDistanceVertical")
94 aDistance = aVDist1.real("ConstraintValue")
95 refattrA = aVDist1.refattr("ConstraintEntityA")
96 refattrB = aVDist1.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(aVDist1.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(verticalDistance(aPoint1Coords, aPoint2Coords) - DISTANCE1) < 1.e-5, "Distance values are different: {0} != {1}".format(verticalDistance(aPoint1Coords, aPoint2Coords), DISTANCE1)
116 assert (model.dof(aSketchFeature) == 3)
117 #=========================================================================
118 # Change a distance value
119 #=========================================================================
120 d = DISTANCE1 + 21.
121 dStep = -5.
122 while d >= -30.:
123     aSession.startOperation()
124     DISTANCE1 = d
125     aDistance.setValue(DISTANCE1)
126     aSession.finishOperation()
127     assert math.fabs(verticalDistance(aPoint1Coords, aPoint2Coords) - DISTANCE1) < 1.e-5, "Distance values are different: {0} != {1}".format(verticalDistance(aPoint1Coords, aPoint2Coords), DISTANCE1)
128     d += dStep
129 assert (model.dof(aSketchFeature) == 3)
130
131 #=========================================================================
132 # Create a constraint to keep vertical distance between fixed and movable points
133 #=========================================================================
134 DISTANCE2 = 50.
135 aSession.startOperation()
136 aVDist2 = aSketchFeature.addFeature("SketchConstraintDistanceVertical")
137 aDistance = aVDist2.real("ConstraintValue")
138 refattrA = aVDist2.refattr("ConstraintEntityA")
139 refattrB = aVDist2.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(verticalDistance(anExtCoords, aPoint1Coords) - DISTANCE2) < 1.e-5, "Distance values are different: {0} != {1}".format(verticalDistance(anExtCoords, aPoint1Coords), DISTANCE2)
148 assert math.fabs(aPoint1Coords.y() - DISTANCE2) < 1.e-5, "Wrong point coordinates ({}, {}), expected y = {}".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 #=========================================================================
153 d = DISTANCE2
154 dStep = -7.
155 while d >= -50.:
156     aSession.startOperation()
157     DISTANCE2 = d
158     aDistance.setValue(DISTANCE2)
159     aSession.finishOperation()
160     assert math.fabs(verticalDistance(anExtCoords, aPoint1Coords) - DISTANCE2) < 1.e-5, "Distance values are different: {0} != {1}".format(verticalDistance(anExtCoords, aPoint1Coords), DISTANCE2)
161     assert math.fabs(aPoint1Coords.y() - DISTANCE2) < 1.e-5, "Wrong point coordinates ({}, {}), expected y = {}".format(aPoint1Coords.x(), aPoint1Coords.y(), DISTANCE2)
162     assert math.fabs(verticalDistance(aPoint1Coords, aPoint2Coords) - DISTANCE1) < 1.e-5, "Distance values are different: {0} != {1}".format(verticalDistance(aPoint1Coords, aPoint2Coords), DISTANCE1)
163     d += dStep
164 assert (model.dof(aSketchFeature) == 2)
165
166 #=========================================================================
167 # Remove first distance
168 #=========================================================================
169 aStoredCoords = [aPoint2Coords.x(), aPoint2Coords.y()]
170 aSession.startOperation()
171 aDocument.removeFeature(aVDist1)
172 aSession.finishOperation()
173 assert (model.dof(aSketchFeature) == 3)
174 aSession.startOperation()
175 DISTANCE2 = 20.
176 aDistance.setValue(DISTANCE2)
177 aSession.finishOperation()
178 assert math.fabs(verticalDistance(anExtCoords, aPoint1Coords) - DISTANCE2) < 1.e-5, "Distance values are different: {0} != {1}".format(verticalDistance(anExtCoords, aPoint1Coords), DISTANCE2)
179 assert math.fabs(aPoint1Coords.y() - DISTANCE2) < 1.e-5, "Wrong point coordinates ({}, {}), expected y = {}".format(aPoint1Coords.x(), aPoint1Coords.y(), DISTANCE2)
180 assert aPoint2Coords.x() == aStoredCoords[0] and aPoint2Coords.y() == aStoredCoords[1]
181
182 #=========================================================================
183 # Create line and set vertical 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)
193
194 DISTANCE3 = 50.
195 aSession.startOperation()
196 aVDist3 = aSketchFeature.addFeature("SketchConstraintDistanceVertical")
197 aDistance = aVDist3.real("ConstraintValue")
198 refattrA = aVDist3.refattr("ConstraintEntityA")
199 refattrB = aVDist3.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(verticalDistance(aStartPoint, aEndPoint) - DISTANCE3) < 1.e-5, "Distance values are different: {0} != {1}".format(verticalDistance(aStartPoint, aEndPoint), DISTANCE3)
208 assert (model.dof(aSketchFeature) == 6)
209 #=========================================================================
210 # Change a distance value
211 #=========================================================================
212 d = DISTANCE3
213 dStep = -7.
214 while d >= -50.:
215     aSession.startOperation()
216     DISTANCE3 = d
217     aDistance.setValue(DISTANCE3)
218     aSession.finishOperation()
219     assert math.fabs(verticalDistance(aStartPoint, aEndPoint) - DISTANCE3) < 1.e-5, "Distance values are different: {0} != {1}".format(verticalDistance(aStartPoint, aEndPoint), DISTANCE3)
220     d += dStep
221 assert (model.dof(aSketchFeature) == 6)
222
223 #=========================================================================
224 # End of test
225 #=========================================================================
226
227 assert(model.checkPythonDump())