Salome HOME
Adjust test cases according to redesigned Arc and Circle features
[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 def checkArcLineTangency(theArc, theLine):
41     """
42     subroutine to check that the line is tangent to arc/circle
43     """
44     if (theArc.getKind() == "SketchCircle"):
45         aCenter = geomDataAPI_Point2D(theArc.attribute("circle_center"))
46         aRadius = theArc.real("circle_radius").value()
47     else:
48         aCenter = geomDataAPI_Point2D(theArc.attribute("center_point"))
49         aStartPnt = geomDataAPI_Point2D(theArc.attribute("start_point"))
50         aRadius = model.distancePointPoint(aStartPnt, aCenter)
51     aDist = distancePointLine(aCenter, theLine)
52     assert math.fabs(aDist - aRadius) < 2.e-5, "aDist = {0}, aRadius = {1}".format(aDist, aRadius)
53
54 def checkArcArcTangency(theArc1, theArc2):
55     """
56     subroutine to check that arcs/circles arc tangent
57     """
58     anArcs = [theArc1, theArc2]
59     aCenters = []
60     aRadii = []
61     for anArc in anArcs:
62         if (anArc.getKind() == "SketchCircle"):
63             aCenter = geomDataAPI_Point2D(anArc.attribute("circle_center"))
64             aRadius = anArc.real("circle_radius").value()
65         else:
66             aCenter = geomDataAPI_Point2D(anArc.attribute("center_point"))
67             aStartPnt = geomDataAPI_Point2D(anArc.attribute("start_point"))
68             aRadius = model.distancePointPoint(aStartPnt, aCenter)
69         aCenters.append(aCenter)
70         aRadii.append(aRadius)
71     aDist = model.distancePointPoint(aCenters[0], aCenters[1])
72     aRSum = aRadii[0] + aRadii[1]
73     aRDiff = math.fabs(aRadii[0] - aRadii[1])
74     assert math.fabs(aDist - aRSum) < 2.e-5 or math.fabs(aDist - aRDiff) < 2.e-5, "aDist = {0}, aRSum = {1}, aRDiff = {2}".format(aDist, aRSum, aRDiff)
75
76
77 aSession = ModelAPI_Session.get()
78 aDocument = aSession.moduleDocument()
79 #=========================================================================
80 # Creation of a sketch
81 #=========================================================================
82 aSession.startOperation()
83 aSketchCommonFeature = aDocument.addFeature("Sketch")
84 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
85 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
86 origin.setValue(0, 0, 0)
87 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
88 dirx.setValue(1, 0, 0)
89 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
90 norm.setValue(0, 0, 1)
91 aSession.finishOperation()
92 #=========================================================================
93 # TEST 1. Arc-line tangency
94 #=========================================================================
95 # Creation of an arc and two lines
96 #=========================================================================
97 # Arc
98 aSession.startOperation()
99 aSketchArc1 = aSketchFeature.addFeature("SketchArc")
100 anArcCentr = geomDataAPI_Point2D(aSketchArc1.attribute("center_point"))
101 anArcCentr.setValue(10., 10.)
102 anArcStartPoint = geomDataAPI_Point2D(aSketchArc1.attribute("start_point"))
103 anArcStartPoint.setValue(0., 50.)
104 anArcEndPoint = geomDataAPI_Point2D(aSketchArc1.attribute("end_point"))
105 anArcEndPoint.setValue(50., 0.)
106 aSession.finishOperation()
107 # Line 1
108 aSession.startOperation()
109 aSketchLine1 = aSketchFeature.addFeature("SketchLine")
110 aLine1StartPoint = geomDataAPI_Point2D(aSketchLine1.attribute("StartPoint"))
111 aLine1EndPoint = geomDataAPI_Point2D(aSketchLine1.attribute("EndPoint"))
112 aLine1StartPoint.setValue(0., 50.)
113 aLine1EndPoint.setValue(0., 100.)
114 aSession.finishOperation()
115 # Line 2
116 aSession.startOperation()
117 aSketchLine2 = aSketchFeature.addFeature("SketchLine")
118 aLine2StartPoint = geomDataAPI_Point2D(aSketchLine2.attribute("StartPoint"))
119 aLine2EndPoint = geomDataAPI_Point2D(aSketchLine2.attribute("EndPoint"))
120 aLine2StartPoint.setValue(50., 0.)
121 aLine2EndPoint.setValue(100., 0.)
122 aSession.finishOperation()
123 assert (model.dof(aSketchFeature) == 13)
124 #=========================================================================
125 # Link arc points and lines points by the coincidence constraint
126 #=========================================================================
127 aSession.startOperation()
128 aConstraint = aSketchFeature.addFeature("SketchConstraintCoincidence")
129 reflistA = aConstraint.refattr("ConstraintEntityA")
130 reflistB = aConstraint.refattr("ConstraintEntityB")
131 reflistA.setAttr(anArcStartPoint)
132 reflistB.setAttr(aLine1StartPoint)
133 aConstraint.execute()
134 aSession.finishOperation()
135 aSession.startOperation()
136 aConstraint = aSketchFeature.addFeature("SketchConstraintCoincidence")
137 reflistA = aConstraint.refattr("ConstraintEntityA")
138 reflistB = aConstraint.refattr("ConstraintEntityB")
139 reflistA.setAttr(anArcEndPoint)
140 reflistB.setAttr(aLine2StartPoint)
141 aConstraint.execute()
142 aSession.finishOperation()
143 assert (model.dof(aSketchFeature) == 9)
144 #=========================================================================
145 # Add tangency constraint and check correctness
146 #=========================================================================
147 aSession.startOperation()
148 aTangency = aSketchFeature.addFeature("SketchConstraintTangent")
149 aRefObjectA = aTangency.refattr("ConstraintEntityA")
150 aRefObjectB = aTangency.refattr("ConstraintEntityB")
151 anObjectA = modelAPI_ResultConstruction(aSketchArc1.lastResult())
152 anObjectB = modelAPI_ResultConstruction(aSketchLine1.lastResult())
153 assert (anObjectA is not None)
154 assert (anObjectB is not None)
155 aRefObjectA.setObject(anObjectA)
156 aRefObjectB.setObject(anObjectB)
157 aTangency.execute()
158 aSession.finishOperation()
159 checkArcLineTangency(aSketchArc1, aSketchLine1)
160 assert (model.dof(aSketchFeature) == 8)
161 #=========================================================================
162 # Add tangency constraint for arc and second line and check correctness
163 #=========================================================================
164 aSession.startOperation()
165 aTangency = aSketchFeature.addFeature("SketchConstraintTangent")
166 aRefObjectA = aTangency.refattr("ConstraintEntityA")
167 aRefObjectB = aTangency.refattr("ConstraintEntityB")
168 anObjectA = modelAPI_ResultConstruction(aSketchArc1.lastResult())
169 anObjectB = modelAPI_ResultConstruction(aSketchLine2.lastResult())
170 assert (anObjectA is not None)
171 assert (anObjectB is not None)
172 aRefObjectA.setObject(anObjectA)
173 aRefObjectB.setObject(anObjectB)
174 aTangency.execute()
175 aSession.finishOperation()
176 checkArcLineTangency(aSketchArc1, aSketchLine2)
177 assert (model.dof(aSketchFeature) == 7)
178
179 #=========================================================================
180 # TEST 2. Arc-arc tangency
181 #=========================================================================
182 # Creation of arcs
183 #=========================================================================
184 # Arc 1
185 aSession.startOperation()
186 aSketchArc1 = aSketchFeature.addFeature("SketchArc")
187 anArc1Centr = geomDataAPI_Point2D(aSketchArc1.attribute("center_point"))
188 anArc1Centr.setValue(10., 10.)
189 anArc1StartPoint = geomDataAPI_Point2D(aSketchArc1.attribute("start_point"))
190 anArc1StartPoint.setValue(50., 0.)
191 anArc1EndPoint = geomDataAPI_Point2D(aSketchArc1.attribute("end_point"))
192 anArc1EndPoint.setValue(0., 50.)
193 aSession.finishOperation()
194 # Arc 2
195 aSession.startOperation()
196 aSketchArc2 = aSketchFeature.addFeature("SketchMacroArc")
197 aSketchArc2.string("arc_type").setValue("by_three_points")
198 anArc2StartPoint = geomDataAPI_Point2D(aSketchArc2.attribute("start_point_2"))
199 anArc2StartPoint.setValue(0., 50.)
200 anArc2EndPoint = geomDataAPI_Point2D(aSketchArc2.attribute("end_point_2"))
201 anArc2EndPoint.setValue(-50., 0.)
202 anArc2PassedPoint = geomDataAPI_Point2D(aSketchArc2.attribute("passed_point"))
203 anArc2PassedPoint.setValue(-40., 40.)
204 aSession.finishOperation()
205 assert (model.dof(aSketchFeature) == 17)
206 aSketchArc2 = model.lastSubFeature(aSketchFeature, "SketchArc")
207 anArc2Centr = geomDataAPI_Point2D(aSketchArc2.attribute("center_point"))
208 anArc2StartPoint = geomDataAPI_Point2D(aSketchArc2.attribute("start_point"))
209 #=========================================================================
210 # Link points of arcs by the coincidence constraint
211 #=========================================================================
212 aSession.startOperation()
213 aConstraint = aSketchFeature.addFeature("SketchConstraintCoincidence")
214 reflistA = aConstraint.refattr("ConstraintEntityA")
215 reflistB = aConstraint.refattr("ConstraintEntityB")
216 reflistA.setAttr(anArc1EndPoint)
217 reflistB.setAttr(anArc2StartPoint)
218 aConstraint.execute()
219 aSession.finishOperation()
220 assert (model.dof(aSketchFeature) == 15)
221 #=========================================================================
222 # Add tangency constraint and check correctness
223 #=========================================================================
224 aSession.startOperation()
225 aTangency = aSketchFeature.addFeature("SketchConstraintTangent")
226 aRefObjectA = aTangency.refattr("ConstraintEntityA")
227 aRefObjectB = aTangency.refattr("ConstraintEntityB")
228 anObjectA = modelAPI_ResultConstruction(aSketchArc1.lastResult())
229 anObjectB = modelAPI_ResultConstruction(aSketchArc2.lastResult())
230 assert (anObjectA is not None)
231 assert (anObjectB is not None)
232 aRefObjectA.setObject(anObjectA)
233 aRefObjectB.setObject(anObjectB)
234 aTangency.execute()
235 aSession.finishOperation()
236 checkArcArcTangency(aSketchArc1, aSketchArc2)
237 assert (model.dof(aSketchFeature) == 14)
238
239 #=========================================================================
240 # TEST 3. Tangency between non-connected objects should work
241 #=========================================================================
242 # 3.1 tangency between arc2 and line2
243 aSession.startOperation()
244 aTangency = aSketchFeature.addFeature("SketchConstraintTangent")
245 aRefObjectA = aTangency.refattr("ConstraintEntityA")
246 aRefObjectB = aTangency.refattr("ConstraintEntityB")
247 anObjectA = modelAPI_ResultConstruction(aSketchArc2.lastResult())
248 anObjectB = modelAPI_ResultConstruction(aSketchLine2.lastResult())
249 assert (anObjectA is not None)
250 assert (anObjectB is not None)
251 aRefObjectA.setObject(anObjectA)
252 aRefObjectB.setObject(anObjectB)
253 aTangency.execute()
254 aSession.finishOperation()
255 checkArcLineTangency(aSketchArc2, aSketchLine2)
256 assert (model.dof(aSketchFeature) == 13)
257
258 aSession.startOperation()
259 aDocument.removeFeature(aTangency)
260 aSession.finishOperation()
261 assert (model.dof(aSketchFeature) == 14)
262
263 # 3.2  tangency between non-connected arcs
264 aSession.startOperation()
265 aSketchArc3 = aSketchFeature.addFeature("SketchArc")
266 anArc3Centr = geomDataAPI_Point2D(aSketchArc3.attribute("center_point"))
267 anArc3Centr.setValue(100., -10.)
268 anArc3StartPoint = geomDataAPI_Point2D(aSketchArc3.attribute("start_point"))
269 anArc3StartPoint.setValue(70., -10.)
270 anArc3EndPoint = geomDataAPI_Point2D(aSketchArc3.attribute("end_point"))
271 anArc3EndPoint.setValue(100., 20.)
272 aSession.finishOperation()
273 assert (model.dof(aSketchFeature) == 19)
274
275 aSession.startOperation()
276 aTangency = aSketchFeature.addFeature("SketchConstraintTangent")
277 aTangency.refattr("ConstraintEntityA").setObject(modelAPI_ResultConstruction(aSketchArc2.lastResult()))
278 aTangency.refattr("ConstraintEntityB").setObject(modelAPI_ResultConstruction(aSketchArc3.lastResult()))
279 aSession.finishOperation()
280 checkArcArcTangency(aSketchArc2, aSketchArc3)
281 assert (model.dof(aSketchFeature) == 18)
282
283 aSession.startOperation()
284 aDocument.removeFeature(aSketchArc3)
285 aDocument.removeFeature(aTangency)
286 aSession.finishOperation()
287 assert (model.dof(aSketchFeature) == 14)
288
289 # 3.3  tangency between arc and circle
290 aSession.startOperation()
291 aCircle1 = aSketchFeature.addFeature("SketchCircle")
292 aCircleCenter = geomDataAPI_Point2D(aCircle1.attribute("circle_center"))
293 aCircleRadius = aCircle1.real("circle_radius")
294 aCircleCenter.setValue(150., 100.)
295 aCircleRadius.setValue(50.)
296 aSession.finishOperation()
297 assert (model.dof(aSketchFeature) == 17)
298
299 aSession.startOperation()
300 aTangency1 = aSketchFeature.addFeature("SketchConstraintTangent")
301 aTangency1.refattr("ConstraintEntityA").setObject(modelAPI_ResultConstruction(aSketchArc2.lastResult()))
302 aTangency1.refattr("ConstraintEntityB").setObject(modelAPI_ResultConstruction(aCircle1.lastResult()))
303 aSession.finishOperation()
304 checkArcArcTangency(aSketchArc2, aCircle1)
305 assert (model.dof(aSketchFeature) == 16)
306
307 # 3.4  tangency between two circles
308 aSession.startOperation()
309 aCircle2 = aSketchFeature.addFeature("SketchCircle")
310 aCircleCenter = geomDataAPI_Point2D(aCircle2.attribute("circle_center"))
311 aCircleRadius = aCircle2.real("circle_radius")
312 aCircleCenter.setValue(120., 70.)
313 aCircleRadius.setValue(20.)
314 aSession.finishOperation()
315 assert (model.dof(aSketchFeature) == 19)
316
317 aSession.startOperation()
318 aTangency2 = aSketchFeature.addFeature("SketchConstraintTangent")
319 aTangency2.refattr("ConstraintEntityA").setObject(modelAPI_ResultConstruction(aCircle1.lastResult()))
320 aTangency2.refattr("ConstraintEntityB").setObject(modelAPI_ResultConstruction(aCircle2.lastResult()))
321 aSession.finishOperation()
322 checkArcArcTangency(aCircle1, aCircle2)
323 assert (model.dof(aSketchFeature) == 18)
324
325 #=========================================================================
326 # TEST 4. Creating of tangency arc by the option of the SketchArc feature
327 #=========================================================================
328 aSession.startOperation()
329 aSketchArc3 = aSketchFeature.addFeature("SketchMacroArc")
330 aSketchArc3.string("arc_type").setValue("by_tangent_edge")
331 anArc3Start = aSketchArc3.refattr("tangent_point")
332 anArc3Start.setAttr(anArc1StartPoint)
333 anArc3EndPoint = geomDataAPI_Point2D(aSketchArc3.attribute("end_point_3"))
334 anArc3EndPoint.setValue(anArc1StartPoint.x()-5, anArc1StartPoint.y()-30)
335 aSession.finishOperation()
336 aSketchArc3 = model.lastSubFeature(aSketchFeature, "SketchArc")
337 checkArcArcTangency(aSketchArc1, aSketchArc3)
338 # freeze radius of tangent arc
339 aSession.startOperation()
340 aConstraintRadius = aSketchFeature.addFeature("SketchConstraintRadius")
341 aConstraintRadius.refattr("ConstraintEntityA").setObject(modelAPI_ResultConstruction(aSketchArc3.lastResult()))
342 aConstraintRadius.real("ConstraintValue").setValue(30.)
343 aSession.finishOperation()
344 checkArcArcTangency(aSketchArc1, aSketchArc3)
345 # do not check DoF here because it is unstable for tangent arcs,
346 # remove tangent arc to avoid instability while dumping
347 aSession.startOperation()
348 ModelAPI.removeFeaturesAndReferences(FeatureSet([aSketchArc3]))
349 aSession.finishOperation()
350 assert (model.dof(aSketchFeature) == 18)
351
352 #=========================================================================
353 # TEST 5. Creating of tangency between line and circle
354 #=========================================================================
355 aSession.startOperation()
356 aLine = aSketchFeature.addFeature("SketchLine")
357 aLineStart = geomDataAPI_Point2D(aLine.attribute("StartPoint"))
358 aLineEnd = geomDataAPI_Point2D(aLine.attribute("EndPoint"))
359 aLineStart.setValue(100., 100.)
360 aLineEnd.setValue(200., 200.)
361 aCircle = aSketchFeature.addFeature("SketchCircle")
362 aCircleCenter = geomDataAPI_Point2D(aCircle.attribute("circle_center"))
363 aCircleRadius = aCircle.real("circle_radius")
364 aCircleCenter.setValue(150., 100.)
365 aCircleRadius.setValue(20.)
366 aSession.finishOperation()
367 assert (model.dof(aSketchFeature) == 25)
368
369 aSession.startOperation()
370 aTangency = aSketchFeature.addFeature("SketchConstraintTangent")
371 aRefObjectA = aTangency.refattr("ConstraintEntityA")
372 aRefObjectB = aTangency.refattr("ConstraintEntityB")
373 anObjectA = modelAPI_ResultConstruction(aLine.lastResult())
374 anObjectB = modelAPI_ResultConstruction(aCircle.lastResult())
375 assert (anObjectA is not None)
376 assert (anObjectB is not None)
377 aRefObjectA.setObject(anObjectA)
378 aRefObjectB.setObject(anObjectB)
379 aTangency.execute()
380 aSession.finishOperation()
381
382 checkArcLineTangency(aCircle, aLine)
383 assert (model.dof(aSketchFeature) == 24)
384 #=========================================================================
385 # End of test
386 #=========================================================================
387
388 assert(model.checkPythonDump())