Salome HOME
Merge remote-tracking branch 'origin/Toolbars_Management'
[modules/shaper.git] / src / SketchPlugin / Test / TestConstraintDistance.py
1 ## Copyright (C) 2014-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     TestConstraintDistance.py
23     Unit test of SketchPlugin_ConstraintDistance class
24
25     SketchPlugin_Constraint
26         static const std::string MY_CONSTRAINT_VALUE("ConstraintValue");
27         static const std::string MY_FLYOUT_VALUE_PNT("ConstraintFlyoutValuePnt");
28         static const std::string MY_ENTITY_A("ConstraintEntityA");
29         static const std::string MY_ENTITY_B("ConstraintEntityB");
30         static const std::string MY_ENTITY_C("ConstraintEntityC");
31         static const std::string MY_ENTITY_D("ConstraintEntityD");
32
33     SketchPlugin_ConstraintDistance
34         static const std::string MY_CONSTRAINT_DISTANCE_ID("SketchConstraintDistance");
35         data()->addAttribute(SketchPlugin_Constraint::VALUE(),    ModelAPI_AttributeDouble::typeId());
36         data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
37         data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
38         data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
39
40
41 """
42 from GeomDataAPI import *
43 from ModelAPI import *
44 import math
45 from salome.shaper import model
46
47 #=========================================================================
48 # Initialization of the test
49 #=========================================================================
50
51 __updated__ = "2014-10-28"
52
53
54 aSession = ModelAPI_Session.get()
55 aDocument = aSession.moduleDocument()
56 #=========================================================================
57 # Creation of a sketch
58 #=========================================================================
59 aSession.startOperation()
60 aSketchCommonFeature = aDocument.addFeature("Sketch")
61 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
62 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
63 origin.setValue(0, 0, 0)
64 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
65 dirx.setValue(1, 0, 0)
66 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
67 norm.setValue(0, 0, 1)
68 aSession.finishOperation()
69 #=========================================================================
70 # Create a point and a line
71 #=========================================================================
72 aSession.startOperation()
73 aSketchPoint = aSketchFeature.addFeature("SketchPoint")
74 aSketchPointCoords = geomDataAPI_Point2D(
75     aSketchPoint.attribute("PointCoordinates"))
76 aSketchPointCoords.setValue(50., 50.)
77 aSketchLine = aSketchFeature.addFeature("SketchLine")
78 aLineAStartPoint = geomDataAPI_Point2D(aSketchLine.attribute("StartPoint"))
79 aLineAEndPoint = geomDataAPI_Point2D(aSketchLine.attribute("EndPoint"))
80 aLineAStartPoint.setValue(0., 25.)
81 aLineAEndPoint.setValue(100., 25.)
82 aSession.finishOperation()
83 assert (model.dof(aSketchFeature) == 6)
84 #=========================================================================
85 # Make a constraint to keep the distance
86 #=========================================================================
87 PT_PT_DIST = 25.
88 aDist = model.distancePointPoint(aSketchPointCoords, aLineAStartPoint);
89 assert (aDist != PT_PT_DIST)
90 aSession.startOperation()
91 aConstraint = aSketchFeature.addFeature("SketchConstraintDistance")
92 aDistance = aConstraint.real("ConstraintValue")
93 refattrA = aConstraint.refattr("ConstraintEntityA")
94 refattrB = aConstraint.refattr("ConstraintEntityB")
95 assert (not aDistance.isInitialized())
96 assert (not refattrA.isInitialized())
97 assert (not refattrB.isInitialized())
98 aLineResult = aSketchLine.firstResult()
99 assert (aLineResult is not None)
100 # the following line is necessary to check automatic calculation of a distance
101 aConstraint.execute()
102 refattrA.setAttr(aSketchPointCoords)
103 refattrB.setAttr(aLineAStartPoint)
104 aSession.finishOperation()
105 assert (model.dof(aSketchFeature) == 5)
106 # set flyout point then abort operation, after that check the Distance is correct
107 aSession.startOperation()
108 aFlyoutPoint = geomDataAPI_Point2D(aConstraint.attribute("ConstraintFlyoutValuePnt"))
109 aFlyoutPoint.setValue(50.0, 100.0)
110 aSession.abortOperation()
111 assert (refattrA.isInitialized())
112 assert (refattrB.isInitialized())
113 assert (aDistance.isInitialized())
114 assert math.fabs(aDistance.value() - aDist) < 1.e-4, "Distance values are different: {0} != {1}".format(aDistance.value(), aDist)
115 assert (model.dof(aSketchFeature) == 5)
116 #=========================================================================
117 # Change distance value
118 #=========================================================================
119 aSession.startOperation()
120 aDistance.setValue(PT_PT_DIST)
121 aSession.finishOperation()
122 assert (math.fabs(model.distancePointPoint(aSketchPointCoords, aLineAStartPoint) - PT_PT_DIST) < 1.e-10)
123 assert (model.dof(aSketchFeature) == 5)
124 #=========================================================================
125 # Move line, check that distance is constant
126 #=========================================================================
127 aSession.startOperation()
128 aLineAStartPoint.setValue(0., 40.)
129 aLineAEndPoint.setValue(100., 40.)
130 aSession.finishOperation()
131 assert (math.fabs(model.distancePointPoint(aSketchPointCoords, aLineAStartPoint) - PT_PT_DIST) < 1.e-10)
132 assert (model.dof(aSketchFeature) == 5)
133 #=========================================================================
134 # Remove constraint, check the points are unconstrained now
135 #=========================================================================
136 aSession.startOperation()
137 aDocument.removeFeature(aConstraint)
138 aSession.finishOperation()
139 aSession.startOperation()
140 aSketchPointCoords.setValue(0., 0.)
141 aSession.finishOperation()
142 assert (math.fabs(model.distancePointPoint(aSketchPointCoords, aLineAStartPoint) - PT_PT_DIST) > 1.e-10)
143 assert (model.dof(aSketchFeature) == 6)
144
145 #=========================================================================
146 # Add distance between point and line
147 #=========================================================================
148 PT_LINE_DIST = 50.
149 aDist = model.distancePointLine(aSketchPointCoords, aSketchLine)
150 aSession.startOperation()
151 aConstraint = aSketchFeature.addFeature("SketchConstraintDistance")
152 aDistance = aConstraint.real("ConstraintValue")
153 refattrA = aConstraint.refattr("ConstraintEntityA")
154 refattrB = aConstraint.refattr("ConstraintEntityB")
155 assert (not aDistance.isInitialized())
156 assert (not refattrA.isInitialized())
157 assert (not refattrB.isInitialized())
158 aLineResult = aSketchLine.firstResult()
159 assert (aLineResult is not None)
160 refattrA.setObject(aLineResult)
161 refattrB.setAttr(aSketchPointCoords)
162 aSession.finishOperation()
163 assert (model.dof(aSketchFeature) == 5)
164 # set flyout point then abort operation, after that check the Distance is correct
165 aSession.startOperation()
166 aFlyoutPoint = geomDataAPI_Point2D(aConstraint.attribute("ConstraintFlyoutValuePnt"))
167 aFlyoutPoint.setValue(50.0, 100.0)
168 aSession.abortOperation()
169 assert (refattrA.isInitialized())
170 assert (refattrB.isInitialized())
171 assert (aDistance.isInitialized())
172 assert math.fabs(aDistance.value() - aDist) < 1.e-4, "Distance values are different: {0} != {1}".format(aDistance.value(), aDist)
173 assert (model.dof(aSketchFeature) == 5)
174
175 #=========================================================================
176 # Swap point and line in the distance
177 #=========================================================================
178 aSession.startOperation()
179 refattrA.setAttr(aSketchPointCoords)
180 refattrB.setObject(aLineResult)
181 aSession.finishOperation()
182 assert (model.dof(aSketchFeature) == 5)
183 # set flyout point then abort operation, after that check the Distance is correct
184 aSession.startOperation()
185 aFlyoutPoint = geomDataAPI_Point2D(aConstraint.attribute("ConstraintFlyoutValuePnt"))
186 aFlyoutPoint.setValue(100.0, 100.0)
187 aSession.abortOperation()
188 assert (refattrA.isInitialized())
189 assert (refattrB.isInitialized())
190 assert (aDistance.isInitialized())
191 assert math.fabs(aDistance.value() - aDist) < 1.e-4, "Distance values are different: {0} != {1}".format(aDistance.value(), aDist)
192 assert (model.dof(aSketchFeature) == 5)
193 #=========================================================================
194 # Change distance value
195 #=========================================================================
196 aSession.startOperation()
197 aDistance.setValue(PT_LINE_DIST)
198 aSession.finishOperation()
199 assert (math.fabs(model.distancePointLine(aSketchPointCoords, aSketchLine) - PT_LINE_DIST) < 1.e-10)
200 assert (model.dof(aSketchFeature) == 5)
201 #=========================================================================
202 # Set distance between line boundaries
203 #=========================================================================
204 aSession.startOperation()
205 refattrA.setAttr(aLineAStartPoint)
206 refattrB.setAttr(aLineAEndPoint)
207 aSession.finishOperation()
208 assert (math.fabs(model.distancePointPoint(aLineAStartPoint, aLineAEndPoint) - PT_LINE_DIST) < 1.e-10)
209 assert (model.dof(aSketchFeature) == 5)
210 #=========================================================================
211 # End of test
212 #=========================================================================
213
214 assert(model.checkPythonDump())