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