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