Salome HOME
Fix errors in SketchPlugin/plugin-Sketch.xml
[modules/shaper.git] / src / SketchPlugin / Test / TestConstraintDistanceVertical.py
1 ## Copyright (C) 2017  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
18 ## email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 ##
20
21 """
22     TestConstraintDistanceVertical.py
23     Unit test of SketchPlugin_ConstraintDistanceVertical class
24
25     SketchPlugin_ConstraintDistanceVertical
26         static const std::string MY_CONSTRAINT_DISTANCE_ID("SketchConstraintDistance");
27         data()->addAttribute(SketchPlugin_Constraint::VALUE(),    ModelAPI_AttributeDouble::typeId());
28         data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
29         data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
30         data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
31 """
32
33 from GeomDataAPI import *
34 from ModelAPI import *
35 import math
36 from salome.shaper import model
37
38 #=========================================================================
39 # Initialization of the test
40 #=========================================================================
41
42 __updated__ = "2017-05-10"
43
44
45 def verticalDistance(point1, point2):
46     """
47     subroutine to calculate signed distance between two points
48     """
49     return point2.y() - point1.y()
50
51 aSession = ModelAPI_Session.get()
52 aDocument = aSession.moduleDocument()
53 #=========================================================================
54 # Creation of a sketch
55 #=========================================================================
56 aSession.startOperation()
57 aSketchCommonFeature = aDocument.addFeature("Sketch")
58 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
59 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
60 origin.setValue(0, 0, 0)
61 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
62 dirx.setValue(1, 0, 0)
63 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
64 norm.setValue(0, 0, 1)
65 aSession.finishOperation()
66 #=========================================================================
67 # Create two movable and one fixed point
68 #=========================================================================
69 aSession.startOperation()
70 aPoint1 = aSketchFeature.addFeature("SketchPoint")
71 aPoint1Coords = geomDataAPI_Point2D(aPoint1.attribute("PointCoordinates"))
72 aPoint1Coords.setValue(50., 50.)
73 aSession.finishOperation()
74 aSession.startOperation()
75 aPoint2 = aSketchFeature.addFeature("SketchPoint")
76 aPoint2Coords = geomDataAPI_Point2D(aPoint2.attribute("PointCoordinates"))
77 aPoint2Coords.setValue(70., 70.)
78 aSession.finishOperation()
79 aSession.startOperation()
80 anOriginResult = modelAPI_Result(aDocument.objectByName("Construction", "Origin"))
81 anOriginShape = anOriginResult.shape()
82 anExtPoint = aSketchFeature.addFeature("SketchPoint")
83 anExtCoords = geomDataAPI_Point2D(anExtPoint.attribute("PointCoordinates"))
84 anExtCoords.setValue(0., 0.)
85 anExtPoint.selection("External").setValue(anOriginResult, anOriginShape)
86 aSession.finishOperation()
87 assert (model.dof(aSketchFeature) == 4)
88
89 #=========================================================================
90 # Create a constraint to keep vertical distance between movable points
91 #=========================================================================
92 DISTANCE1 = 25.
93 aSession.startOperation()
94 aVDist1 = aSketchFeature.addFeature("SketchConstraintDistanceVertical")
95 aDistance = aVDist1.real("ConstraintValue")
96 refattrA = aVDist1.refattr("ConstraintEntityA")
97 refattrB = aVDist1.refattr("ConstraintEntityB")
98 assert (not aDistance.isInitialized())
99 assert (not refattrA.isInitialized())
100 assert (not refattrB.isInitialized())
101 refattrA.setObject(aPoint1.lastResult())
102 refattrB.setObject(aPoint2.lastResult())
103 aSession.finishOperation()
104 assert (model.dof(aSketchFeature) == 3)
105 # set flyout point then abort operation, after that check the Distance is correct
106 aSession.startOperation()
107 aFlyoutPoint = geomDataAPI_Point2D(aVDist1.attribute("ConstraintFlyoutValuePnt"))
108 aFlyoutPoint.setValue(50.0, 100.0)
109 aSession.abortOperation()
110 assert (refattrA.isInitialized())
111 assert (refattrB.isInitialized())
112 assert (aDistance.isInitialized())
113 aSession.startOperation()
114 aDistance.setValue(DISTANCE1)
115 aSession.finishOperation()
116 assert math.fabs(verticalDistance(aPoint1Coords, aPoint2Coords) - DISTANCE1) < 1.e-5, "Distance values are different: {0} != {1}".format(verticalDistance(aPoint1Coords, aPoint2Coords), DISTANCE1)
117 assert (model.dof(aSketchFeature) == 3)
118 #=========================================================================
119 # Change a distance value
120 #=========================================================================
121 d = DISTANCE1 + 20.
122 dStep = -5.
123 while d >= -30.:
124     aSession.startOperation()
125     DISTANCE1 = d
126     aDistance.setValue(DISTANCE1)
127     aSession.finishOperation()
128     assert math.fabs(verticalDistance(aPoint1Coords, aPoint2Coords) - DISTANCE1) < 1.e-5, "Distance values are different: {0} != {1}".format(verticalDistance(aPoint1Coords, aPoint2Coords), DISTANCE1)
129     d += dStep
130 assert (model.dof(aSketchFeature) == 3)
131
132 #=========================================================================
133 # Create a constraint to keep vertical distance between fixed and movable points
134 #=========================================================================
135 DISTANCE2 = 50.
136 aSession.startOperation()
137 aVDist2 = aSketchFeature.addFeature("SketchConstraintDistanceVertical")
138 aDistance = aVDist2.real("ConstraintValue")
139 refattrA = aVDist2.refattr("ConstraintEntityA")
140 refattrB = aVDist2.refattr("ConstraintEntityB")
141 assert (not aDistance.isInitialized())
142 assert (not refattrA.isInitialized())
143 assert (not refattrB.isInitialized())
144 refattrA.setObject(anExtPoint.lastResult())
145 refattrB.setAttr(aPoint1Coords)
146 aDistance.setValue(DISTANCE2)
147 aSession.finishOperation()
148 assert math.fabs(verticalDistance(anExtCoords, aPoint1Coords) - DISTANCE2) < 1.e-5, "Distance values are different: {0} != {1}".format(verticalDistance(anExtCoords, aPoint1Coords), DISTANCE2)
149 assert math.fabs(aPoint1Coords.y() - DISTANCE2) < 1.e-5, "Wrong point coordinates ({}, {}), expected y = {}".format(aPoint1Coords.x(), aPoint1Coords.y(), DISTANCE2)
150 assert (model.dof(aSketchFeature) == 2)
151 #=========================================================================
152 # Change a distance value (check previous constraint is applied too)
153 #=========================================================================
154 d = DISTANCE2
155 dStep = -5.
156 while d >= -50.:
157     aSession.startOperation()
158     DISTANCE2 = d
159     aDistance.setValue(DISTANCE2)
160     aSession.finishOperation()
161     assert math.fabs(verticalDistance(anExtCoords, aPoint1Coords) - DISTANCE2) < 1.e-5, "Distance values are different: {0} != {1}".format(verticalDistance(anExtCoords, aPoint1Coords), DISTANCE2)
162     assert math.fabs(aPoint1Coords.y() - DISTANCE2) < 1.e-5, "Wrong point coordinates ({}, {}), expected y = {}".format(aPoint1Coords.x(), aPoint1Coords.y(), DISTANCE2)
163     assert math.fabs(verticalDistance(aPoint1Coords, aPoint2Coords) - DISTANCE1) < 1.e-5, "Distance values are different: {0} != {1}".format(verticalDistance(aPoint1Coords, aPoint2Coords), DISTANCE1)
164     d += dStep
165 assert (model.dof(aSketchFeature) == 2)
166
167 #=========================================================================
168 # Remove first distance
169 #=========================================================================
170 aStoredCoords = [aPoint2Coords.x(), aPoint2Coords.y()]
171 aSession.startOperation()
172 aDocument.removeFeature(aVDist1)
173 aSession.finishOperation()
174 assert (model.dof(aSketchFeature) == 3)
175 aSession.startOperation()
176 DISTANCE2 = 20.
177 aDistance.setValue(DISTANCE2)
178 aSession.finishOperation()
179 assert math.fabs(verticalDistance(anExtCoords, aPoint1Coords) - DISTANCE2) < 1.e-5, "Distance values are different: {0} != {1}".format(verticalDistance(anExtCoords, aPoint1Coords), DISTANCE2)
180 assert math.fabs(aPoint1Coords.y() - DISTANCE2) < 1.e-5, "Wrong point coordinates ({}, {}), expected y = {}".format(aPoint1Coords.x(), aPoint1Coords.y(), DISTANCE2)
181 assert aPoint2Coords.x() == aStoredCoords[0] and aPoint2Coords.y() == aStoredCoords[1]
182
183 #=========================================================================
184 # Create line and set vertical distance between line boundaries
185 #=========================================================================
186 aSession.startOperation()
187 aLine = aSketchFeature.addFeature("SketchLine")
188 aStartPoint = geomDataAPI_Point2D(aLine.attribute("StartPoint"))
189 aEndPoint = geomDataAPI_Point2D(aLine.attribute("EndPoint"))
190 aStartPoint.setValue(50., 0.)
191 aEndPoint.setValue(100., 20.)
192 aSession.finishOperation()
193 assert (model.dof(aSketchFeature) == 7)
194
195 DISTANCE3 = 50.
196 aSession.startOperation()
197 aVDist3 = aSketchFeature.addFeature("SketchConstraintDistanceVertical")
198 aDistance = aVDist3.real("ConstraintValue")
199 refattrA = aVDist3.refattr("ConstraintEntityA")
200 refattrB = aVDist3.refattr("ConstraintEntityB")
201 assert (not aDistance.isInitialized())
202 assert (not refattrA.isInitialized())
203 assert (not refattrB.isInitialized())
204 refattrA.setAttr(aStartPoint)
205 refattrB.setAttr(aEndPoint)
206 aDistance.setValue(DISTANCE3)
207 aSession.finishOperation()
208 assert math.fabs(verticalDistance(aStartPoint, aEndPoint) - DISTANCE3) < 1.e-5, "Distance values are different: {0} != {1}".format(verticalDistance(aStartPoint, aEndPoint), DISTANCE3)
209 assert (model.dof(aSketchFeature) == 6)
210 #=========================================================================
211 # Change a distance value
212 #=========================================================================
213 d = DISTANCE3
214 dStep = -5.
215 while d >= -50.:
216     aSession.startOperation()
217     DISTANCE3 = d
218     aDistance.setValue(DISTANCE3)
219     aSession.finishOperation()
220     assert math.fabs(verticalDistance(aStartPoint, aEndPoint) - DISTANCE3) < 1.e-5, "Distance values are different: {0} != {1}".format(verticalDistance(aStartPoint, aEndPoint), DISTANCE3)
221     d += dStep
222 assert (model.dof(aSketchFeature) == 6)
223
224 #=========================================================================
225 # End of test
226 #=========================================================================
227
228 assert(model.checkPythonDump())