Salome HOME
b4365147f67b06b0567650fafd1d9b46466b2b4f
[modules/shaper.git] / src / SketchPlugin / Test / TestConstraintDistanceVertical.py
1 # Copyright (C) 2017-2019  CEA/DEN, EDF R&D
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 + 20.
121 dStep = -5.
122 while d >= -30.:
123     aSession.startOperation()
124     DISTANCE1 = d
125     aDistance.setValue(DISTANCE1)
126     aSession.finishOperation()
127     if DISTANCE1 == 0:
128         assert(aVDist1.error() != "")
129     else:
130         assert math.fabs(verticalDistance(aPoint1Coords, aPoint2Coords) - DISTANCE1) < 1.e-5, "Distance values are different: {0} != {1}".format(verticalDistance(aPoint1Coords, aPoint2Coords), DISTANCE1)
131     d += dStep
132 assert (model.dof(aSketchFeature) == 3)
133
134 #=========================================================================
135 # Create a constraint to keep vertical distance between fixed and movable points
136 #=========================================================================
137 DISTANCE2 = 50.
138 aSession.startOperation()
139 aVDist2 = aSketchFeature.addFeature("SketchConstraintDistanceVertical")
140 aDistance = aVDist2.real("ConstraintValue")
141 refattrA = aVDist2.refattr("ConstraintEntityA")
142 refattrB = aVDist2.refattr("ConstraintEntityB")
143 assert (not aDistance.isInitialized())
144 assert (not refattrA.isInitialized())
145 assert (not refattrB.isInitialized())
146 refattrA.setObject(anExtPoint.lastResult())
147 refattrB.setAttr(aPoint1Coords)
148 aDistance.setValue(DISTANCE2)
149 aSession.finishOperation()
150 assert math.fabs(verticalDistance(anExtCoords, aPoint1Coords) - DISTANCE2) < 1.e-5, "Distance values are different: {0} != {1}".format(verticalDistance(anExtCoords, aPoint1Coords), DISTANCE2)
151 assert math.fabs(aPoint1Coords.y() - DISTANCE2) < 1.e-5, "Wrong point coordinates ({}, {}), expected y = {}".format(aPoint1Coords.x(), aPoint1Coords.y(), DISTANCE2)
152 assert (model.dof(aSketchFeature) == 2)
153 #=========================================================================
154 # Change a distance value (check previous constraint is applied too)
155 #=========================================================================
156 d = DISTANCE2
157 dStep = -5.
158 while d >= -50.:
159     aSession.startOperation()
160     DISTANCE2 = d
161     aDistance.setValue(DISTANCE2)
162     aSession.finishOperation()
163     if DISTANCE2 == 0:
164         assert(aVDist2.error() != "")
165     else:
166         assert math.fabs(verticalDistance(anExtCoords, aPoint1Coords) - DISTANCE2) < 1.e-5, "Distance values are different: {0} != {1}".format(verticalDistance(anExtCoords, aPoint1Coords), DISTANCE2)
167         assert math.fabs(aPoint1Coords.y() - DISTANCE2) < 1.e-5, "Wrong point coordinates ({}, {}), expected y = {}".format(aPoint1Coords.x(), aPoint1Coords.y(), DISTANCE2)
168         assert math.fabs(verticalDistance(aPoint1Coords, aPoint2Coords) - DISTANCE1) < 1.e-5, "Distance values are different: {0} != {1}".format(verticalDistance(aPoint1Coords, aPoint2Coords), DISTANCE1)
169     d += dStep
170 assert (model.dof(aSketchFeature) == 2)
171
172 #=========================================================================
173 # Remove first distance
174 #=========================================================================
175 aStoredCoords = [aPoint2Coords.x(), aPoint2Coords.y()]
176 aSession.startOperation()
177 aDocument.removeFeature(aVDist1)
178 aSession.finishOperation()
179 assert (model.dof(aSketchFeature) == 3)
180 aSession.startOperation()
181 DISTANCE2 = 20.
182 aDistance.setValue(DISTANCE2)
183 aSession.finishOperation()
184 assert math.fabs(verticalDistance(anExtCoords, aPoint1Coords) - DISTANCE2) < 1.e-5, "Distance values are different: {0} != {1}".format(verticalDistance(anExtCoords, aPoint1Coords), DISTANCE2)
185 assert math.fabs(aPoint1Coords.y() - DISTANCE2) < 1.e-5, "Wrong point coordinates ({}, {}), expected y = {}".format(aPoint1Coords.x(), aPoint1Coords.y(), DISTANCE2)
186 assert aPoint2Coords.x() == aStoredCoords[0] and aPoint2Coords.y() == aStoredCoords[1]
187
188 #=========================================================================
189 # Create line and set vertical distance between line boundaries
190 #=========================================================================
191 aSession.startOperation()
192 aLine = aSketchFeature.addFeature("SketchLine")
193 aStartPoint = geomDataAPI_Point2D(aLine.attribute("StartPoint"))
194 aEndPoint = geomDataAPI_Point2D(aLine.attribute("EndPoint"))
195 aStartPoint.setValue(50., 0.)
196 aEndPoint.setValue(100., 20.)
197 aSession.finishOperation()
198 assert (model.dof(aSketchFeature) == 7)
199
200 DISTANCE3 = 50.
201 aSession.startOperation()
202 aVDist3 = aSketchFeature.addFeature("SketchConstraintDistanceVertical")
203 aDistance = aVDist3.real("ConstraintValue")
204 refattrA = aVDist3.refattr("ConstraintEntityA")
205 refattrB = aVDist3.refattr("ConstraintEntityB")
206 assert (not aDistance.isInitialized())
207 assert (not refattrA.isInitialized())
208 assert (not refattrB.isInitialized())
209 refattrA.setAttr(aStartPoint)
210 refattrB.setAttr(aEndPoint)
211 aDistance.setValue(DISTANCE3)
212 aSession.finishOperation()
213 assert math.fabs(verticalDistance(aStartPoint, aEndPoint) - DISTANCE3) < 1.e-5, "Distance values are different: {0} != {1}".format(verticalDistance(aStartPoint, aEndPoint), DISTANCE3)
214 assert (model.dof(aSketchFeature) == 6)
215 #=========================================================================
216 # Change a distance value
217 #=========================================================================
218 d = DISTANCE3
219 dStep = -5.
220 while d >= -50.:
221     aSession.startOperation()
222     DISTANCE3 = d
223     aDistance.setValue(DISTANCE3)
224     aSession.finishOperation()
225     if DISTANCE3 == 0:
226         assert(aVDist3.error() != "")
227     else:
228         assert math.fabs(verticalDistance(aStartPoint, aEndPoint) - DISTANCE3) < 1.e-5, "Distance values are different: {0} != {1}".format(verticalDistance(aStartPoint, aEndPoint), DISTANCE3)
229     d += dStep
230 assert (model.dof(aSketchFeature) == 6)
231
232 #=========================================================================
233 # End of test
234 #=========================================================================
235
236 assert(model.checkPythonDump())