Salome HOME
Task 2.12. New entities: ellipses and arcs of ellipses (issue #3003)
[modules/shaper.git] / src / SketchPlugin / Test / TestSplitEllipse.py
1 # Copyright (C) 2019  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 from salome.shaper import geom
22 import math
23
24 from ModelAPI import *
25 from SketchAPI import *
26
27 CENTER = geom.Pnt2d(10, 10)
28 MAJOR_RADIUS = 50
29 MINOR_RADIUS = 30
30
31 DOF = 10
32 NB_LINES = 3
33 NB_ELLIPSES = 1
34 NB_ELLIPTIC_ARCS = 0
35 NB_COINCIDENCES = 6
36
37 TOLERANCE = 1.e-6
38
39 def checkFeaturesQuantity(theSketch):
40     model.testNbSubFeatures(theSketch, "SketchLine", NB_LINES)
41     model.testNbSubFeatures(theSketch, "SketchEllipse", NB_ELLIPSES)
42     model.testNbSubFeatures(theSketch, "SketchEllipticArc", NB_ELLIPTIC_ARCS)
43     model.testNbSubFeatures(theSketch, "SketchConstraintCoincidence", NB_COINCIDENCES)
44     assert(model.dof(theSketch) == DOF)
45
46 def checkEllipticArcs(theSketch):
47     for aSub in theSketch.features().list():
48         aFeature = ModelAPI_Feature.feature(aSub)
49         if aFeature is not None and aFeature.getKind() == "SketchEllipticArc":
50             assertEllipticArc(SketchAPI_EllipticArc(aFeature))
51
52 def assertEllipticArc(theArc):
53     assertPoints(theArc.center(), CENTER)
54     assertPoints(theArc.majorAxisPositive(), geom.Pnt2d(CENTER.x() + MAJOR_RADIUS, CENTER.y()))
55     assertPoints(theArc.minorAxisPositive(), geom.Pnt2d(CENTER.x(), CENTER.y() + MINOR_RADIUS))
56
57 def assertPoints(thePoint1, thePoint2):
58   assert(math.fabs(thePoint1.x() - thePoint2.x()) < TOLERANCE), "{} != {}".format(thePoint1.x(), thePoint2.x())
59   assert(math.fabs(thePoint1.y() - thePoint2.y()) < TOLERANCE), "{} != {}".format(thePoint1.y(), thePoint2.y())
60
61
62 model.begin()
63 partSet = model.moduleDocument()
64 Part_1 = model.addPart(partSet)
65 Part_1_doc = Part_1.document()
66 Sketch_1 = model.addSketch(Part_1_doc, model.defaultPlane("XOY"))
67 SketchEllipse_1 = Sketch_1.addEllipse(CENTER.x(), CENTER.y(), CENTER.x() + math.sqrt(MAJOR_RADIUS**2 - MINOR_RADIUS**2), CENTER.y(), MINOR_RADIUS)
68 SketchLine_1 = Sketch_1.addLine(-16.74176451428603, -15.34869012470842, -16.85909682653373, 35.30399198463829)
69 SketchConstraintCoincidence_1 = Sketch_1.setCoincident(SketchLine_1.startPoint(), SketchEllipse_1.result())
70 SketchConstraintCoincidence_2 = Sketch_1.setCoincident(SketchLine_1.endPoint(), SketchEllipse_1.result())
71 SketchLine_2 = Sketch_1.addLine(-16.85909682653373, 35.30399198463829, 20.9032928583277, -19.27802168426675)
72 SketchConstraintCoincidence_3 = Sketch_1.setCoincident(SketchLine_1.endPoint(), SketchLine_2.startPoint())
73 SketchConstraintCoincidence_4 = Sketch_1.setCoincident(SketchLine_2.endPoint(), SketchEllipse_1.result())
74 SketchLine_3 = Sketch_1.addLine(34.69765676551338, 36.08465583643841, 35.0422024535432, -15.96612629290852)
75 SketchConstraintCoincidence_5 = Sketch_1.setCoincident(SketchLine_3.startPoint(), SketchEllipse_1.result())
76 SketchConstraintCoincidence_6 = Sketch_1.setCoincident(SketchLine_3.endPoint(), SketchEllipse_1.result())
77 model.do()
78
79 checkFeaturesQuantity(Sketch_1)
80 checkEllipticArcs(Sketch_1)
81
82 # split the ellipse
83 SketchSplit = Sketch_1.addSplit(SketchEllipse_1, geom.Pnt2d(CENTER.x() + MAJOR_RADIUS, CENTER.y()))
84 model.do()
85 NB_ELLIPSES -= 1
86 NB_ELLIPTIC_ARCS += 2
87 NB_COINCIDENCES += 4
88 DOF += 3
89
90 checkFeaturesQuantity(Sketch_1)
91 checkEllipticArcs(Sketch_1)
92
93 # split the middle arc of ellipse
94 EllipticArc = SketchAPI_EllipticArc(model.lastSubFeature(Sketch_1, "SketchEllipticArc"))
95 ANGLE = -math.pi/2 - math.pi/10
96 SketchSplit = Sketch_1.addSplit(EllipticArc, geom.Pnt2d(CENTER.x() + MAJOR_RADIUS * math.cos(ANGLE), CENTER.y() + MINOR_RADIUS * math.sin(ANGLE)))
97 model.do()
98 NB_ELLIPTIC_ARCS += 2
99 NB_COINCIDENCES += 4
100 DOF += 8
101
102 checkFeaturesQuantity(Sketch_1)
103 checkEllipticArcs(Sketch_1)
104
105 # try to split the boundary arc of ellipse,
106 # it shoult fail, because there is no coincident points
107 EllipticArc = SketchAPI_EllipticArc(model.lastSubFeature(Sketch_1, "SketchEllipticArc"))
108 SketchSplit = Sketch_1.addSplit(EllipticArc, geom.Pnt2d(CENTER.x() - MAJOR_RADIUS, CENTER.y()))
109 model.end()
110 aValidators = ModelAPI_Session.get().validators()
111 assert(not aValidators.validate(SketchSplit.feature()))
112
113 # remove previous split and add coincidence
114 model.undo()
115 model.begin()
116 Part_1_doc.removeFeature(SketchSplit.feature())
117 model.do()
118 Sketch_1.setCoincident(SketchLine_2.startPoint(), EllipticArc.result())
119 model.do()
120 NB_COINCIDENCES += 1
121 DOF -= 1
122
123 # split the boundary arc of ellipse
124 SketchSplit = Sketch_1.addSplit(EllipticArc, geom.Pnt2d(CENTER.x() - MAJOR_RADIUS, CENTER.y()))
125 model.do()
126 NB_ELLIPTIC_ARCS += 1
127 NB_COINCIDENCES += 2
128 DOF += 4
129
130 checkFeaturesQuantity(Sketch_1)
131 checkEllipticArcs(Sketch_1)
132
133 model.end()
134
135 assert(model.checkPythonDump())