Salome HOME
Copyright update 2022
[modules/shaper.git] / src / BuildPlugin / Test / TestInterpolation.py
1 # Copyright (C) 2014-2022  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
22 import math
23
24 # Create document
25 model.begin()
26 partSet = model.moduleDocument()
27 Part_1 = model.addPart(partSet)
28 Part_1_doc = Part_1.document()
29
30 # Create sketch
31 #
32 #  2         4
33 #
34 #       3
35 #
36 #  1         5
37 Sketch_1 = model.addSketch(Part_1_doc, model.defaultPlane("XOY"))
38 SketchPoint_1 = Sketch_1.addPoint(0, 0)
39 SketchPoint_2 = Sketch_1.addPoint(0, 50)
40 SketchPoint_3 = Sketch_1.addPoint(25, 25)
41 SketchPoint_4 = Sketch_1.addPoint(50, 50)
42 SketchPoint_5 = Sketch_1.addPoint(50, 0)
43
44 SketchLine_1 = Sketch_1.addLine(0, 0, -45, -45)
45 SketchLine_2 = Sketch_1.addLine(0, 0, 45, 45)
46 model.do()
47
48 # Get sketch points
49 base_name = "Sketch_1/SketchPoint_"
50 p_1, p_2, p_3, p_4, p_5 = [model.selection("VERTEX", base_name + str(i + 1)) for i in range(0, 5)]
51
52 # Get sketch edges
53 Tangent_1 = model.selection("EDGE", "Sketch_1/SketchLine_1")
54 Tangent_2 = model.selection("EDGE", "Sketch_1/SketchLine_2")
55
56 # =============================================================================
57 # Test 1. Create curve 1-2-3-4-5, closed off, reorder off, without tangents
58 # =============================================================================
59 Interpolation_1 = model.addInterpolation(Part_1_doc, [p_1, p_2, p_3, p_4, p_5], False, False)
60 model.do()
61
62 model.checkResult(Interpolation_1, model, 1, [0], [0], [0], [1], [2])
63
64 # =============================================================================
65 # Test 2. Create curve 1-2-3-4-5-1, closed on, reorder off, without tangents
66 # =============================================================================
67 Interpolation_2 = model.addInterpolation(Part_1_doc, [p_1, p_2, p_3, p_4, p_5], True, False)
68 model.do()
69
70 model.checkResult(Interpolation_2, model, 1, [0], [0], [0], [1], [2])
71
72 # =============================================================================
73 # Test 3. Create curve 1-2-3-4, closed off, reorder on, without tangents
74 # =============================================================================
75 Interpolation_3 = model.addInterpolation(Part_1_doc, [p_1, p_2, p_3, p_4], False, True)
76 model.do()
77
78 model.checkResult(Interpolation_3, model, 1, [0], [0], [0], [1], [2])
79
80 # =============================================================================
81 # Test 4. Create curve 1-2-3-5, closed on, reorder on, without tangents
82 # =============================================================================
83 Interpolation_4 = model.addInterpolation(Part_1_doc, [p_1, p_2, p_3, p_5], True, True)
84 model.do()
85
86 model.checkResult(Interpolation_4, model, 1, [0], [0], [0], [1], [2])
87
88 # =============================================================================
89 # Test 5. Create curve 1-2-3-4-5, closed off, reorder off, with tangents
90 # =============================================================================
91
92 Interpolation_5 = model.addInterpolation(Part_1_doc, [p_1, p_2, p_3, p_4, p_5],
93                                          Tangent_1, Tangent_2, False, False)
94 model.do()
95
96 model.checkResult(Interpolation_5, model, 1, [0], [0], [0], [1], [2])
97
98 # =============================================================================
99 # Test 6. Try to create closed curve 1-2-1, closed off, reorder off, without tangents
100 # =============================================================================
101 Interpolation_6 = model.addInterpolation(Part_1_doc, [p_1, p_2, p_1], False, False)
102 model.do()
103
104 model.testNbResults(Interpolation_6, 0)
105
106 # =============================================================================
107 # Test 7. Try to create curve on a single point 3
108 # =============================================================================
109 Interpolation_7 = model.addInterpolation(Part_1_doc, [p_3], False, False)
110 model.do()
111
112 model.testNbResults(Interpolation_7, 0)
113
114 # =============================================================================
115 # Test 8. Create curve on box vertices, closed off, reorder off, without tangents
116 # =============================================================================
117 Part_2 = model.addPart(partSet)
118 Part_2_doc = Part_2.document()
119 Box_1 = model.addBox(Part_2_doc, 10, 10, 10)
120
121 point_names = ("[Box_1_1/Back][Box_1_1/Left][Box_1_1/Bottom]",
122                "[Box_1_1/Back][Box_1_1/Left][Box_1_1/Top]",
123                "[Box_1_1/Front][Box_1_1/Left][Box_1_1/Top]",
124                "[Box_1_1/Front][Box_1_1/Left][Box_1_1/Bottom]",
125                "[Box_1_1/Front][Box_1_1/Right][Box_1_1/Bottom]",
126                "[Box_1_1/Front][Box_1_1/Right][Box_1_1/Top]",
127                "[Box_1_1/Back][Box_1_1/Right][Box_1_1/Top]",
128                "[Box_1_1/Back][Box_1_1/Right][Box_1_1/Bottom]")
129 points = [model.selection("VERTEX", name) for name in point_names]
130
131 Interpolation_8 = model.addInterpolation(Part_2_doc, points, False, False)
132 model.do()
133
134 model.checkResult(Interpolation_8, model, 1, [0], [0], [0], [1], [2])
135
136 # =============================================================================
137 # Test 9. Create curve on box vertices, closed off, reorder off, with tangents
138 # =============================================================================
139 Part_3 = model.addPart(partSet)
140 Part_3_doc = Part_3.document()
141 Box_1 = model.addBox(Part_3_doc, 20, 30, 40)
142
143 points = [model.selection("VERTEX", name) for name in point_names]
144 Tangent_1 = model.selection("EDGE", "[Box_1_1/Back][Box_1_1/Top]")
145 Tangent_2 = model.selection("EDGE", "[Box_1_1/Left][Box_1_1/Top]")
146
147 Interpolation_9 = model.addInterpolation(Part_3_doc, points,
148                                          Tangent_1, Tangent_2, False, False)
149 model.do()
150
151 model.checkResult(Interpolation_9, model, 1, [0], [0], [0], [1], [2])
152
153 # =============================================================================
154 # Test 10. Create curve using equal vertices
155 # =============================================================================
156 Part_4 = model.addPart(partSet)
157 Part_4_doc = Part_4.document()
158
159 Point_1 = model.addPoint(Part_4_doc, 0, 0, 0)
160 Point_2 = model.addPoint(Part_4_doc, 0, 0, 0)
161 P_1 = model.selection("VERTEX", "Point_1")
162 P_2 = model.selection("VERTEX", "Point_2")
163
164 Interpolation_10 = model.addInterpolation(Part_4_doc, [P_1, P_2],
165                                           False, False)
166 model.do()
167 model.testNbResults(Interpolation_10, 0)
168
169 # =============================================================================
170 # Test 11. Check subshapes naming
171 # =============================================================================
172 model.testHaveNamingSubshapes(Interpolation_5, model, Part_1_doc)
173 model.testHaveNamingSubshapes(Interpolation_9, model, Part_3_doc)
174 model.end()
175
176 # =============================================================================
177 # Test 12. Create curve using an analytical expression
178 # =============================================================================
179 Part_5 = model.addPart(partSet)
180 Part_5_doc = Part_5.document()
181
182 Interpolation_11 = model.addInterpolation(Part_5_doc, "sin(t)","cos(t)","t", 0, 100, 100)
183 model.do()
184 model.testNbResults(Interpolation_11, 1)
185 myDelta = 1e-6
186 Props = model.getGeometryCalculation(Part_5_doc,model.selection("EDGE", "Interpolation_1_1"))
187 model.do()
188 aRefLength = 141.32010978124
189 aResLength = Props[0]
190 assert (math.fabs(aResLength - aRefLength) < myDelta), "The length is wrong: expected = {0}, real = {1}".format(aRefLength, aResLength)
191 model.end()
192 # =============================================================================
193 # Test 13. Check Python dump
194 # =============================================================================
195 assert(model.checkPythonDump(model.ModelHighAPI.CHECK_NAMING))