]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/Test/TestConstraintTangent.py
Salome HOME
Receive DoF from the solver. Update test cases to check DoF.
[modules/shaper.git] / src / SketchPlugin / Test / TestConstraintTangent.py
1 """
2     TestConstraintTangent.py
3     Unit test of SketchPlugin_ConstraintTangent class
4         
5     SketchPlugin_ConstraintTangent
6         static const std::string MY_CONSTRAINT_TANGENT_ID("SketchConstraintTangent");
7         data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
8         data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
9
10 """
11 from GeomDataAPI import *
12 from GeomAPI import *
13 from ModelAPI import *
14 import math
15 from salome.shaper import model
16
17 #=========================================================================
18 # Initialization of the test
19 #=========================================================================
20
21 __updated__ = "2015-03-17"
22
23 def distancePointLine(point, line):
24     """
25     subroutine to calculate distance between point and line
26     result of calculated distance is has 10**-5 precision
27     """
28     aStartPoint = geomDataAPI_Point2D(line.attribute("StartPoint"))
29     aEndPoint = geomDataAPI_Point2D(line.attribute("EndPoint"))
30     # orthogonal direction
31     aDirX = -(aEndPoint.y() - aStartPoint.y())
32     aDirY = (aEndPoint.x() - aStartPoint.x())
33     aLen = math.sqrt(aDirX**2 + aDirY**2)
34     aDirX = aDirX / aLen
35     aDirY = aDirY / aLen
36     aVecX = point.x() - aStartPoint.x()
37     aVecY = point.y() - aStartPoint.y()
38     return round(math.fabs(aVecX * aDirX + aVecY * aDirY), 5)
39
40
41 aSession = ModelAPI_Session.get()
42 aDocument = aSession.moduleDocument()
43 #=========================================================================
44 # Creation of a sketch
45 #=========================================================================
46 aSession.startOperation()
47 aSketchCommonFeature = aDocument.addFeature("Sketch")
48 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
49 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
50 origin.setValue(0, 0, 0)
51 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
52 dirx.setValue(1, 0, 0)
53 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
54 norm.setValue(0, 0, 1)
55 aSession.finishOperation()
56 #=========================================================================
57 # TEST 1. Arc-line tangency
58 #=========================================================================
59 # Creation of an arc and two lines
60 #=========================================================================
61 # Arc
62 aSession.startOperation()
63 aSketchArc1 = aSketchFeature.addFeature("SketchArc")
64 anArcCentr = geomDataAPI_Point2D(aSketchArc1.attribute("ArcCenter"))
65 anArcCentr.setValue(10., 10.)
66 anArcStartPoint = geomDataAPI_Point2D(aSketchArc1.attribute("ArcStartPoint"))
67 anArcStartPoint.setValue(0., 50.)
68 anArcEndPoint = geomDataAPI_Point2D(aSketchArc1.attribute("ArcEndPoint"))
69 anArcEndPoint.setValue(50., 0.)
70 aSession.finishOperation()
71 # Line 1
72 aSession.startOperation()
73 aSketchLine1 = aSketchFeature.addFeature("SketchLine")
74 aLine1StartPoint = geomDataAPI_Point2D(aSketchLine1.attribute("StartPoint"))
75 aLine1EndPoint = geomDataAPI_Point2D(aSketchLine1.attribute("EndPoint"))
76 aLine1StartPoint.setValue(0., 50.)
77 aLine1EndPoint.setValue(0., 100.)
78 aSession.finishOperation()
79 # Line 2
80 aSession.startOperation()
81 aSketchLine2 = aSketchFeature.addFeature("SketchLine")
82 aLine2StartPoint = geomDataAPI_Point2D(aSketchLine2.attribute("StartPoint"))
83 aLine2EndPoint = geomDataAPI_Point2D(aSketchLine2.attribute("EndPoint"))
84 aLine2StartPoint.setValue(50., 0.)
85 aLine2EndPoint.setValue(100., 0.)
86 aSession.finishOperation()
87 assert (model.dof(aSketchFeature) == 13)
88 #=========================================================================
89 # Link arc points and lines points by the coincidence constraint
90 #=========================================================================
91 aSession.startOperation()
92 aConstraint = aSketchFeature.addFeature("SketchConstraintCoincidence")
93 reflistA = aConstraint.refattr("ConstraintEntityA")
94 reflistB = aConstraint.refattr("ConstraintEntityB")
95 reflistA.setAttr(anArcStartPoint)
96 reflistB.setAttr(aLine1StartPoint)
97 aConstraint.execute()
98 aSession.finishOperation()
99 aSession.startOperation()
100 aConstraint = aSketchFeature.addFeature("SketchConstraintCoincidence")
101 reflistA = aConstraint.refattr("ConstraintEntityA")
102 reflistB = aConstraint.refattr("ConstraintEntityB")
103 reflistA.setAttr(anArcEndPoint)
104 reflistB.setAttr(aLine2StartPoint)
105 aConstraint.execute()
106 aSession.finishOperation()
107 assert (model.dof(aSketchFeature) == 9)
108 #=========================================================================
109 # Add tangency constraint and check correctness
110 #=========================================================================
111 aSession.startOperation()
112 aTangency = aSketchFeature.addFeature("SketchConstraintTangent")
113 aRefObjectA = aTangency.refattr("ConstraintEntityA")
114 aRefObjectB = aTangency.refattr("ConstraintEntityB")
115 anObjectA = modelAPI_ResultConstruction(aSketchArc1.lastResult())
116 anObjectB = modelAPI_ResultConstruction(aSketchLine1.firstResult())
117 assert (anObjectA is not None)
118 assert (anObjectB is not None)
119 aRefObjectA.setObject(anObjectA)
120 aRefObjectB.setObject(anObjectB)
121 aTangency.execute()
122 aSession.finishOperation()
123 anArcVecX = anArcStartPoint.x() - anArcCentr.x()
124 anArcVecY = anArcStartPoint.y() - anArcCentr.y()
125 aLen = math.sqrt(anArcVecX**2 + anArcVecY**2)
126 aLineVecX = aLine1EndPoint.x() - aLine1StartPoint.x()
127 aLineVecY = aLine1EndPoint.y() - aLine1StartPoint.y()
128 aLen = aLen * math.sqrt(aLineVecX**2 + aLineVecY**2)
129 aDot = anArcVecX * aLineVecX + anArcVecY * aLineVecY
130 assert math.fabs(aDot) <= 2.e-6 * aLen, "Observed dot product: {0}".format(aDot)
131 assert (model.dof(aSketchFeature) == 8)
132 #=========================================================================
133 # Add tangency constraint for arc and second line and check correctness
134 #=========================================================================
135 aSession.startOperation()
136 aTangency = aSketchFeature.addFeature("SketchConstraintTangent")
137 aRefObjectA = aTangency.refattr("ConstraintEntityA")
138 aRefObjectB = aTangency.refattr("ConstraintEntityB")
139 anObjectA = modelAPI_ResultConstruction(aSketchArc1.lastResult())
140 anObjectB = modelAPI_ResultConstruction(aSketchLine2.firstResult())
141 assert (anObjectA is not None)
142 assert (anObjectB is not None)
143 aRefObjectA.setObject(anObjectA)
144 aRefObjectB.setObject(anObjectB)
145 aTangency.execute()
146 aSession.finishOperation()
147 anArcVecX = anArcEndPoint.x() - anArcCentr.x()
148 anArcVecY = anArcEndPoint.y() - anArcCentr.y()
149 aLen = math.sqrt(anArcVecX**2 + anArcVecY**2)
150 aLineVecX = aLine2EndPoint.x() - aLine2StartPoint.x()
151 aLineVecY = aLine2EndPoint.y() - aLine2StartPoint.y()
152 aLen = aLen * math.sqrt(aLineVecX**2 + aLineVecY**2)
153 aDot = anArcVecX * aLineVecX + anArcVecY * aLineVecY
154 assert math.fabs(aDot) <= 2.e-6 * aLen, "Observed dot product: {0}".format(aDot)
155 assert (model.dof(aSketchFeature) == 7)
156
157 #=========================================================================
158 # TEST 2. Arc-arc tangency
159 #=========================================================================
160 # Creation of arcs
161 #=========================================================================
162 # Arc 1
163 aSession.startOperation()
164 aSketchArc1 = aSketchFeature.addFeature("SketchArc")
165 anArc1Centr = geomDataAPI_Point2D(aSketchArc1.attribute("ArcCenter"))
166 anArc1Centr.setValue(10., 10.)
167 anArc1StartPoint = geomDataAPI_Point2D(aSketchArc1.attribute("ArcStartPoint"))
168 anArc1StartPoint.setValue(50., 0.)
169 anArc1EndPoint = geomDataAPI_Point2D(aSketchArc1.attribute("ArcEndPoint"))
170 anArc1EndPoint.setValue(0., 50.)
171 aSession.finishOperation()
172 # Arc 2
173 aSession.startOperation()
174 aSketchArc2 = aSketchFeature.addFeature("SketchArc")
175 aSketchArc2.string("ArcType").setValue("ThreePoints")
176 anArc2Centr = geomDataAPI_Point2D(aSketchArc2.attribute("ArcCenter"))
177 anArc2StartPoint = geomDataAPI_Point2D(aSketchArc2.attribute("ArcStartPoint"))
178 anArc2StartPoint.setValue(0., 50.)
179 anArc2EndPoint = geomDataAPI_Point2D(aSketchArc2.attribute("ArcEndPoint"))
180 anArc2EndPoint.setValue(-50., 0.)
181 anArc2PassedPoint = geomDataAPI_Point2D(aSketchArc2.attribute("ArcPassedPoint"))
182 anArc2PassedPoint.setValue(-40., 40.)
183 aSession.finishOperation()
184 assert (model.dof(aSketchFeature) == 17)
185 #=========================================================================
186 # Link points of arcs by the coincidence constraint
187 #=========================================================================
188 aSession.startOperation()
189 aConstraint = aSketchFeature.addFeature("SketchConstraintCoincidence")
190 reflistA = aConstraint.refattr("ConstraintEntityA")
191 reflistB = aConstraint.refattr("ConstraintEntityB")
192 reflistA.setAttr(anArc1EndPoint)
193 reflistB.setAttr(anArc2StartPoint)
194 aConstraint.execute()
195 aSession.finishOperation()
196 assert (model.dof(aSketchFeature) == 15)
197 #=========================================================================
198 # Add tangency constraint and check correctness
199 #=========================================================================
200 aSession.startOperation()
201 aTangency = aSketchFeature.addFeature("SketchConstraintTangent")
202 aRefObjectA = aTangency.refattr("ConstraintEntityA")
203 aRefObjectB = aTangency.refattr("ConstraintEntityB")
204 anObjectA = modelAPI_ResultConstruction(aSketchArc1.lastResult())
205 anObjectB = modelAPI_ResultConstruction(aSketchArc2.lastResult())
206 assert (anObjectA is not None)
207 assert (anObjectB is not None)
208 aRefObjectA.setObject(anObjectA)
209 aRefObjectB.setObject(anObjectB)
210 aTangency.execute()
211 aSession.finishOperation()
212 anArc1VecX = anArc1EndPoint.x() - anArc1Centr.x()
213 anArc1VecY = anArc1EndPoint.y() - anArc1Centr.y()
214 aLen = math.sqrt(anArc1VecX**2 + anArc1VecY**2)
215 anArc2VecX = anArc2StartPoint.x() - anArc2Centr.x()
216 anArc2VecY = anArc2StartPoint.y() - anArc2Centr.y()
217 aLen = aLen * math.sqrt(anArc2VecX**2 + anArc2VecY**2)
218 aCross = anArc1VecX * anArc2VecY - anArc1VecY * anArc2VecX
219 assert math.fabs(aCross) <= 2.e-6 * aLen, "Observed cross product: {0}".format(aCross)
220 assert (model.dof(aSketchFeature) == 14)
221
222 #=========================================================================
223 # TEST 3. Tangency between non-connected objects should be wrong
224 #=========================================================================
225 # Store data
226 aLine2StartPointPrev = (aLine2StartPoint.x(), aLine2StartPoint.y())
227 aLine2EndPointPrev = (aLine2EndPoint.x(), aLine2EndPoint.y())
228 anArc2CenterPrev = (anArc2Centr.x(), anArc2Centr.y())
229 anArc2StartPointPrev = (anArc2StartPoint.x(), anArc2StartPoint.y())
230 anArc2EndPointPrev = (anArc2EndPoint.x(), anArc2EndPoint.y())
231 #=========================================================================
232 # Add tangency between arc2 and line2
233 #=========================================================================
234 aSession.startOperation()
235 aTangency = aSketchFeature.addFeature("SketchConstraintTangent")
236 aRefObjectA = aTangency.refattr("ConstraintEntityA")
237 aRefObjectB = aTangency.refattr("ConstraintEntityB")
238 anObjectA = modelAPI_ResultConstruction(aSketchArc2.lastResult())
239 anObjectB = modelAPI_ResultConstruction(aSketchLine2.firstResult())
240 assert (anObjectA is not None)
241 assert (anObjectB is not None)
242 aRefObjectA.setObject(anObjectA)
243 aRefObjectB.setObject(anObjectB)
244 aTangency.execute()
245 aSession.finishOperation()
246 # Check that nothing is changed
247 aLine2StartPointNew = (aLine2StartPoint.x(), aLine2StartPoint.y())
248 aLine2EndPointNew = (aLine2EndPoint.x(), aLine2EndPoint.y())
249 anArc2CenterNew = (anArc2Centr.x(), anArc2Centr.y())
250 anArc2StartPointNew = (anArc2StartPoint.x(), anArc2StartPoint.y())
251 anArc2EndPointNew = (anArc2EndPoint.x(), anArc2EndPoint.y())
252 assert(aLine2StartPointNew == aLine2StartPointPrev)
253 assert(aLine2EndPointNew == aLine2EndPointPrev)
254 assert(anArc2CenterNew == anArc2CenterPrev)
255 assert(anArc2StartPointNew == anArc2StartPointPrev)
256 assert(anArc2EndPointNew == anArc2EndPointPrev)
257 assert (model.dof(aSketchFeature) == 14)
258 aSession.startOperation()
259 aDocument.removeFeature(aTangency)
260 aSession.finishOperation()
261 assert (model.dof(aSketchFeature) == 14)
262
263 #=========================================================================
264 # TEST 4. Creating of tangency arc by the option of the SketchArc feature
265 #=========================================================================
266 aSession.startOperation()
267 aSketchArc3 = aSketchFeature.addFeature("SketchArc")
268 aSketchArc3.string("ArcType").setValue("Tangent")
269 anArc3Start = aSketchArc3.refattr("ArcTangentPoint")
270 anArc3Start.setAttr(anArc1StartPoint)
271 anArc3EndPoint = geomDataAPI_Point2D(aSketchArc3.attribute("ArcEndPoint"))
272 anArc3EndPoint.setValue(100., 0.)
273 aSession.finishOperation()
274 anArc3Center = geomDataAPI_Point2D(aSketchArc2.attribute("ArcCenter"))
275 anArc3StartPoint = geomDataAPI_Point2D(aSketchArc2.attribute("ArcStartPoint"))
276
277 anArc1VecX = anArc1EndPoint.x() - anArc1Centr.x()
278 anArc1VecY = anArc1EndPoint.y() - anArc1Centr.y()
279 aLen = math.sqrt(anArc1VecX**2 + anArc1VecY**2)
280 anArc3VecX = anArc3StartPoint.x() - anArc3Center.x()
281 anArc3VecY = anArc3StartPoint.y() - anArc3Center.y()
282 aLen = aLen * math.sqrt(anArc3VecX**2 + anArc3VecY**2)
283 aCross = anArc1VecX * anArc3VecY - anArc1VecY * anArc3VecX
284 assert math.fabs(aCross) <= 2.e-6 * aLen, "Observed cross product: {0}".format(aCross)
285 assert (model.dof(aSketchFeature) == 19)
286
287 #=========================================================================
288 # TEST 5. Creating of tangency between line and circle
289 #=========================================================================
290 aSession.startOperation()
291 aLine = aSketchFeature.addFeature("SketchLine")
292 aLineStart = geomDataAPI_Point2D(aLine.attribute("StartPoint"))
293 aLineEnd = geomDataAPI_Point2D(aLine.attribute("EndPoint"))
294 aLineStart.setValue(100., 100.)
295 aLineEnd.setValue(200., 200.)
296 aCircle = aSketchFeature.addFeature("SketchCircle")
297 aCircleCenter = geomDataAPI_Point2D(aCircle.attribute("CircleCenter"))
298 aCircleRadius = aCircle.real("CircleRadius")
299 aCircleCenter.setValue(150., 100.)
300 aCircleRadius.setValue(20.)
301 aSession.finishOperation()
302 assert (model.dof(aSketchFeature) == 26)
303
304 aSession.startOperation()
305 aTangency = aSketchFeature.addFeature("SketchConstraintTangent")
306 aRefObjectA = aTangency.refattr("ConstraintEntityA")
307 aRefObjectB = aTangency.refattr("ConstraintEntityB")
308 anObjectA = modelAPI_ResultConstruction(aLine.lastResult())
309 anObjectB = modelAPI_ResultConstruction(aCircle.lastResult())
310 assert (anObjectA is not None)
311 assert (anObjectB is not None)
312 aRefObjectA.setObject(anObjectA)
313 aRefObjectB.setObject(anObjectB)
314 aTangency.execute()
315 aSession.finishOperation()
316
317 assert(math.fabs(distancePointLine(aCircleCenter, aLine) - round(aCircleRadius.value(), 5)) < 1.e-10)
318 assert (model.dof(aSketchFeature) == 25)
319 #=========================================================================
320 # End of test
321 #=========================================================================
322
323 assert(model.checkPythonDump())