Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[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 def distancePointLine(point, line):
55     """
56     subroutine to calculate distance between point and line
57     result of calculated distance is has 10**-5 precision
58     """
59     aStartPoint = geomDataAPI_Point2D(line.attribute("StartPoint"))
60     aEndPoint = geomDataAPI_Point2D(line.attribute("EndPoint"))
61     # orthogonal direction
62     aDirX = -(aEndPoint.y() - aStartPoint.y())
63     aDirY = (aEndPoint.x() - aStartPoint.x())
64     aLen = math.sqrt(aDirX**2 + aDirY**2)
65     aDirX = aDirX / aLen
66     aDirY = aDirY / aLen
67     aVecX = point.x() - aStartPoint.x()
68     aVecY = point.y() - aStartPoint.y()
69     return round(math.fabs(aVecX * aDirX + aVecY * aDirY), 5)
70
71 aSession = ModelAPI_Session.get()
72 aDocument = aSession.moduleDocument()
73 #=========================================================================
74 # Creation of a sketch
75 #=========================================================================
76 aSession.startOperation()
77 aSketchCommonFeature = aDocument.addFeature("Sketch")
78 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
79 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
80 origin.setValue(0, 0, 0)
81 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
82 dirx.setValue(1, 0, 0)
83 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
84 norm.setValue(0, 0, 1)
85 aSession.finishOperation()
86 #=========================================================================
87 # Create a point and a line
88 #=========================================================================
89 aSession.startOperation()
90 aSketchPoint = aSketchFeature.addFeature("SketchPoint")
91 aSketchPointCoords = geomDataAPI_Point2D(
92     aSketchPoint.attribute("PointCoordinates"))
93 aSketchPointCoords.setValue(50., 50.)
94 aSketchLine = aSketchFeature.addFeature("SketchLine")
95 aLineAStartPoint = geomDataAPI_Point2D(aSketchLine.attribute("StartPoint"))
96 aLineAEndPoint = geomDataAPI_Point2D(aSketchLine.attribute("EndPoint"))
97 aLineAStartPoint.setValue(0., 25.)
98 aLineAEndPoint.setValue(100., 25.)
99 aSession.finishOperation()
100 assert (model.dof(aSketchFeature) == 6)
101 #=========================================================================
102 # Make a constraint to keep the distance
103 #=========================================================================
104 PT_PT_DIST = 25.
105 aDist = model.distancePointPoint(aSketchPointCoords, aLineAStartPoint);
106 assert (aDist != PT_PT_DIST)
107 aSession.startOperation()
108 aConstraint = aSketchFeature.addFeature("SketchConstraintDistance")
109 aDistance = aConstraint.real("ConstraintValue")
110 refattrA = aConstraint.refattr("ConstraintEntityA")
111 refattrB = aConstraint.refattr("ConstraintEntityB")
112 assert (not aDistance.isInitialized())
113 assert (not refattrA.isInitialized())
114 assert (not refattrB.isInitialized())
115 aLineResult = aSketchLine.firstResult()
116 assert (aLineResult is not None)
117 # the following line is necessary to check automatic calculation of a distance
118 aConstraint.execute()
119 refattrA.setAttr(aSketchPointCoords)
120 refattrB.setAttr(aLineAStartPoint)
121 aSession.finishOperation()
122 assert (model.dof(aSketchFeature) == 5)
123 # set flyout point then abort operation, after that check the Distance is correct
124 aSession.startOperation()
125 aFlyoutPoint = geomDataAPI_Point2D(aConstraint.attribute("ConstraintFlyoutValuePnt"))
126 aFlyoutPoint.setValue(50.0, 100.0)
127 aSession.abortOperation()
128 assert (refattrA.isInitialized())
129 assert (refattrB.isInitialized())
130 assert (aDistance.isInitialized())
131 assert math.fabs(aDistance.value() - aDist) < 1.e-4, "Distance values are different: {0} != {1}".format(aDistance.value(), aDist)
132 assert (model.dof(aSketchFeature) == 5)
133 #=========================================================================
134 # Change distance value
135 #=========================================================================
136 aSession.startOperation()
137 aDistance.setValue(PT_PT_DIST)
138 aSession.finishOperation()
139 assert (math.fabs(model.distancePointPoint(aSketchPointCoords, aLineAStartPoint) - PT_PT_DIST) < 1.e-10)
140 assert (model.dof(aSketchFeature) == 5)
141 #=========================================================================
142 # Move line, check that distance is constant
143 #=========================================================================
144 aSession.startOperation()
145 aLineAStartPoint.setValue(0., 40.)
146 aLineAEndPoint.setValue(100., 40.)
147 aSession.finishOperation()
148 assert (math.fabs(model.distancePointPoint(aSketchPointCoords, aLineAStartPoint) - PT_PT_DIST) < 1.e-10)
149 assert (model.dof(aSketchFeature) == 5)
150 #=========================================================================
151 # Remove constraint, check the points are unconstrained now
152 #=========================================================================
153 aSession.startOperation()
154 aDocument.removeFeature(aConstraint)
155 aSession.finishOperation()
156 aSession.startOperation()
157 aSketchPointCoords.setValue(0., 0.)
158 aSession.finishOperation()
159 assert (math.fabs(model.distancePointPoint(aSketchPointCoords, aLineAStartPoint) - PT_PT_DIST) > 1.e-10)
160 assert (model.dof(aSketchFeature) == 6)
161
162 #=========================================================================
163 # Add distance between point and line
164 #=========================================================================
165 PT_LINE_DIST = 50.
166 aDist = distancePointLine(aSketchPointCoords, aSketchLine)
167 aSession.startOperation()
168 aConstraint = aSketchFeature.addFeature("SketchConstraintDistance")
169 aDistance = aConstraint.real("ConstraintValue")
170 refattrA = aConstraint.refattr("ConstraintEntityA")
171 refattrB = aConstraint.refattr("ConstraintEntityB")
172 assert (not aDistance.isInitialized())
173 assert (not refattrA.isInitialized())
174 assert (not refattrB.isInitialized())
175 aLineResult = aSketchLine.firstResult()
176 assert (aLineResult is not None)
177 refattrA.setObject(aLineResult)
178 refattrB.setAttr(aSketchPointCoords)
179 aSession.finishOperation()
180 assert (model.dof(aSketchFeature) == 5)
181 # set flyout point then abort operation, after that check the Distance is correct
182 aSession.startOperation()
183 aFlyoutPoint = geomDataAPI_Point2D(aConstraint.attribute("ConstraintFlyoutValuePnt"))
184 aFlyoutPoint.setValue(50.0, 100.0)
185 aSession.abortOperation()
186 assert (refattrA.isInitialized())
187 assert (refattrB.isInitialized())
188 assert (aDistance.isInitialized())
189 assert math.fabs(aDistance.value() - aDist) < 1.e-4, "Distance values are different: {0} != {1}".format(aDistance.value(), aDist)
190 assert (model.dof(aSketchFeature) == 5)
191 #=========================================================================
192 # Change distance value
193 #=========================================================================
194 aSession.startOperation()
195 aDistance.setValue(PT_LINE_DIST)
196 aSession.finishOperation()
197 assert (math.fabs(distancePointLine(aSketchPointCoords, aSketchLine) - PT_LINE_DIST) < 1.e-10)
198 assert (model.dof(aSketchFeature) == 5)
199 #=========================================================================
200 # Set distance between line boundaries
201 #=========================================================================
202 aSession.startOperation()
203 refattrA.setAttr(aLineAStartPoint)
204 refattrB.setAttr(aLineAEndPoint)
205 aSession.finishOperation()
206 assert (math.fabs(model.distancePointPoint(aLineAStartPoint, aLineAEndPoint) - PT_LINE_DIST) < 1.e-10)
207 assert (model.dof(aSketchFeature) == 5)
208 #=========================================================================
209 # End of test
210 #=========================================================================
211
212 assert(model.checkPythonDump())