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