Salome HOME
f137ab4fa9185a87d277cfcacedc3178de955d44
[modules/shaper.git] / src / SketchPlugin / Test / TestConstraintPerpendicularEllipseLine.py
1 # Copyright (C) 2019-2023  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 email : webmaster.salome@opencascade.com
18 #
19
20 from salome.shaper import model
21 import math
22 from GeomAPI import *
23
24 TOLERANCE = 1.e-7
25
26 def checkPerpendicular(theLine, theEllipse):
27     focus1 = theEllipse.firstFocus()
28     focus2 = theEllipse.secondFocus()
29     pf1 = theLine.project(focus1)
30     pf2 = theLine.project(focus2)
31     dist1 = focus1.distance(pf1)
32     dist2 = focus2.distance(pf2)
33     if math.fabs(dist1 - dist2) < TOLERANCE:
34         # no need further check, the line is equal to one of the ellipses axis
35         return
36     x = (dist2 * pf1.x() - dist1 * pf2.x()) / (dist2 - dist1)
37     y = (dist2 * pf1.y() - dist1 * pf2.y()) / (dist2 - dist1)
38     pnt = GeomAPI_Pnt(x, y, 0)
39     # check point on ellipse
40     majorRad = theEllipse.majorRadius()
41     assert(math.fabs(pnt.distance(focus1) + pnt.distance(focus2) - 2.0 * majorRad) < TOLERANCE * majorRad)
42
43
44 model.begin()
45 partSet = model.moduleDocument()
46 Part_1 = model.addPart(partSet)
47 Part_1_doc = Part_1.document()
48 Sketch_1 = model.addSketch(Part_1_doc, model.defaultPlane("XOY"))
49 SketchLine_1 = Sketch_1.addLine(-40, 10, -20, 30)
50 SketchArc_1 = Sketch_1.addArc(25, -30, 42, -34, 13, -17.31142245955048, False)
51 SketchEllipse_1 = Sketch_1.addEllipse(-50, 0, -30, -5, 15)
52 [SketchPoint_1, SketchPoint_2, SketchPoint_3, SketchPoint_4, SketchPoint_5, SketchPoint_6, SketchPoint_7, SketchLine_2, SketchLine_3] = SketchEllipse_1.construction(center = "aux", firstFocus = "aux", secondFocus = "aux", majorAxisStart = "aux", majorAxisEnd = "aux", minorAxisStart = "aux", minorAxisEnd = "aux", majorAxis = "aux", minorAxis = "aux")
53 SketchEllipticArc_1 = Sketch_1.addEllipticArc(5, 30, -5, 45, -7, 47, 15, 33.68010611, False)
54 [SketchPoint_8, SketchPoint_9, SketchPoint_10, SketchPoint_11, SketchPoint_12, SketchPoint_13, SketchPoint_14, SketchLine_4, SketchLine_5] = SketchEllipticArc_1.construction(center = "aux", firstFocus = "aux", secondFocus = "aux", majorAxisStart = "aux", majorAxisEnd = "aux", minorAxisStart = "aux", minorAxisEnd = "aux", majorAxis = "aux", minorAxis = "aux")
55 model.do()
56
57 # check error on perpendicularity of arc and elliptic arc
58 SketchConstraintPerpendicular_1 = Sketch_1.setPerpendicular(SketchArc_1.results()[1], SketchEllipticArc_1.result())
59 model.do()
60 assert(SketchConstraintPerpendicular_1.feature().error() != "")
61
62 # avoid the failure
63 Part_1_doc.removeFeature(SketchConstraintPerpendicular_1.feature())
64 model.do()
65 assert(Sketch_1.feature().error() == "")
66
67 # set correct constraints
68 SketchConstraintPerpendicular_2 = Sketch_1.setPerpendicular(SketchEllipticArc_1.result(), SketchLine_1.result())
69 SketchConstraintPerpendicular_3 = Sketch_1.setPerpendicular(SketchLine_1.result(), SketchEllipse_1.result())
70 model.do()
71
72 checkPerpendicular(SketchLine_1.defaultResult().shape().edge().line(), SketchEllipse_1.defaultResult().shape().edge().ellipse())
73 checkPerpendicular(SketchLine_1.defaultResult().shape().edge().line(), SketchEllipticArc_1.defaultResult().shape().edge().ellipse())
74 model.end()
75
76 assert(model.checkPythonDump())