Salome HOME
29cfb568025fcc4482f30f47de0d864e47ccf611
[modules/shaper.git] / src / SketchPlugin / Test / TestConstraintDistanceHorizontal.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     TestConstraintDistanceHorizontal.py
23     Unit test of SketchPlugin_ConstraintDistanceHorizontal class
24
25     SketchPlugin_ConstraintDistanceHorizontal
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 horizontalDistance(point1, point2):
46     """
47     subroutine to calculate signed distance between two points
48     """
49     return point2.x() - point1.x()
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 horizontal distance between movable points
91 #=========================================================================
92 DISTANCE1 = 25.
93 aSession.startOperation()
94 aHDist1 = aSketchFeature.addFeature("SketchConstraintDistanceHorizontal")
95 aDistance = aHDist1.real("ConstraintValue")
96 refattrA = aHDist1.refattr("ConstraintEntityA")
97 refattrB = aHDist1.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(aHDist1.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(horizontalDistance(aPoint1Coords, aPoint2Coords) - DISTANCE1) < 1.e-5, "Distance values are different: {0} != {1}".format(horizontalDistance(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     if DISTANCE1 == 0:
129         assert(aHDist1.error() != "")
130     else:
131         assert math.fabs(horizontalDistance(aPoint1Coords, aPoint2Coords) - DISTANCE1) < 1.e-5, "Distance values are different: {0} != {1}".format(horizontalDistance(aPoint1Coords, aPoint2Coords), DISTANCE1)
132     d += dStep
133 assert (model.dof(aSketchFeature) == 3)
134
135 #=========================================================================
136 # Create a constraint to keep horizontal distance between fixed and movable points
137 #=========================================================================
138 DISTANCE2 = 50.
139 aSession.startOperation()
140 aHDist2 = aSketchFeature.addFeature("SketchConstraintDistanceHorizontal")
141 aDistance = aHDist2.real("ConstraintValue")
142 refattrA = aHDist2.refattr("ConstraintEntityA")
143 refattrB = aHDist2.refattr("ConstraintEntityB")
144 assert (not aDistance.isInitialized())
145 assert (not refattrA.isInitialized())
146 assert (not refattrB.isInitialized())
147 refattrA.setObject(anExtPoint.lastResult())
148 refattrB.setAttr(aPoint1Coords)
149 aDistance.setValue(DISTANCE2)
150 aSession.finishOperation()
151 assert math.fabs(horizontalDistance(anExtCoords, aPoint1Coords) - DISTANCE2) < 1.e-5, "Distance values are different: {0} != {1}".format(horizontalDistance(anExtCoords, aPoint1Coords), DISTANCE2)
152 assert math.fabs(aPoint1Coords.x() - DISTANCE2) < 1.e-5, "Wrong point coordinates ({}, {}), expected x = {}".format(aPoint1Coords.x(), aPoint1Coords.y(), DISTANCE2)
153 assert (model.dof(aSketchFeature) == 2)
154 #=========================================================================
155 # Change a distance value (check previous constraint is applied too)
156 #=========================================================================
157 d = DISTANCE2
158 dStep = -5.
159 while d >= -50.:
160     aSession.startOperation()
161     DISTANCE2 = d
162     aDistance.setValue(DISTANCE2)
163     aSession.finishOperation()
164     if DISTANCE2 == 0:
165         assert(aHDist2.error() != "")
166     else:
167         assert math.fabs(horizontalDistance(anExtCoords, aPoint1Coords) - DISTANCE2) < 1.e-5, "Distance values are different: {0} != {1}".format(horizontalDistance(anExtCoords, aPoint1Coords), DISTANCE2)
168         assert math.fabs(aPoint1Coords.x() - DISTANCE2) < 1.e-5, "Wrong point coordinates ({}, {}), expected x = {}".format(aPoint1Coords.x(), aPoint1Coords.y(), DISTANCE2)
169         assert math.fabs(horizontalDistance(aPoint1Coords, aPoint2Coords) - DISTANCE1) < 1.e-5, "Distance values are different: {0} != {1}".format(horizontalDistance(aPoint1Coords, aPoint2Coords), DISTANCE1)
170     d += dStep
171 assert (model.dof(aSketchFeature) == 2)
172
173 #=========================================================================
174 # Remove first distance
175 #=========================================================================
176 aStoredCoords = [aPoint2Coords.x(), aPoint2Coords.y()]
177 aSession.startOperation()
178 aDocument.removeFeature(aHDist1)
179 aSession.finishOperation()
180 assert (model.dof(aSketchFeature) == 3)
181 aSession.startOperation()
182 DISTANCE2 = 20.
183 aDistance.setValue(DISTANCE2)
184 aSession.finishOperation()
185 assert math.fabs(horizontalDistance(anExtCoords, aPoint1Coords) - DISTANCE2) < 1.e-5, "Distance values are different: {0} != {1}".format(horizontalDistance(anExtCoords, aPoint1Coords), DISTANCE2)
186 assert math.fabs(aPoint1Coords.x() - DISTANCE2) < 1.e-5, "Wrong point coordinates ({}, {}), expected x = {}".format(aPoint1Coords.x(), aPoint1Coords.y(), DISTANCE2)
187 assert aPoint2Coords.x() == aStoredCoords[0] and aPoint2Coords.y() == aStoredCoords[1]
188
189 #=========================================================================
190 # Create line and set horizontal distance between line boundaries
191 #=========================================================================
192 aSession.startOperation()
193 aLine = aSketchFeature.addFeature("SketchLine")
194 aStartPoint = geomDataAPI_Point2D(aLine.attribute("StartPoint"))
195 aEndPoint = geomDataAPI_Point2D(aLine.attribute("EndPoint"))
196 aStartPoint.setValue(50., 0.)
197 aEndPoint.setValue(100., 20.)
198 aSession.finishOperation()
199 assert (model.dof(aSketchFeature) == 7)
200
201 DISTANCE3 = 50.
202 aSession.startOperation()
203 aHDist3 = aSketchFeature.addFeature("SketchConstraintDistanceHorizontal")
204 aDistance = aHDist3.real("ConstraintValue")
205 refattrA = aHDist3.refattr("ConstraintEntityA")
206 refattrB = aHDist3.refattr("ConstraintEntityB")
207 assert (not aDistance.isInitialized())
208 assert (not refattrA.isInitialized())
209 assert (not refattrB.isInitialized())
210 refattrA.setAttr(aStartPoint)
211 refattrB.setAttr(aEndPoint)
212 aDistance.setValue(DISTANCE3)
213 aSession.finishOperation()
214 assert math.fabs(horizontalDistance(aStartPoint, aEndPoint) - DISTANCE3) < 1.e-5, "Distance values are different: {0} != {1}".format(horizontalDistance(aStartPoint, aEndPoint), DISTANCE3)
215 assert (model.dof(aSketchFeature) == 6)
216 #=========================================================================
217 # Change a distance value
218 #=========================================================================
219 d = DISTANCE3
220 dStep = -5.
221 while d >= -50.:
222     aSession.startOperation()
223     DISTANCE3 = d
224     aDistance.setValue(DISTANCE3)
225     aSession.finishOperation()
226     if DISTANCE3 == 0:
227         assert(aHDist3.error() != "")
228     else:
229         assert math.fabs(horizontalDistance(aStartPoint, aEndPoint) - DISTANCE3) < 1.e-5, "Distance values are different: {0} != {1}".format(horizontalDistance(aStartPoint, aEndPoint), DISTANCE3)
230     d += dStep
231 assert (model.dof(aSketchFeature) == 6)
232
233 #=========================================================================
234 # End of test
235 #=========================================================================
236
237 assert(model.checkPythonDump())