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