Salome HOME
f57fed58fe12b4d9cb27cdfe8f3941a869ecca7a
[modules/shaper.git] / src / PythonAPI / examples / Platine.py
1 # Copyright (C) 2014-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 # Creation of Platine model using the end-user API
21 # Author: Sergey POKHODENKO
22 # -----------------------------
23
24 from salome.shaper import geom
25 from salome.shaper import model
26
27 # START DEBUG PURPOSES
28 # Should be removed
29 import os
30 import ModelAPI
31 # END DEBUG PURPOSES
32
33 # Initialisation
34 model.begin()
35 partset = model.moduleDocument()
36
37 # Create a new Part
38 part = model.addPart(partset).document()
39
40 L = 64
41 E = 16
42 P = 80
43
44 # Create Parameters
45 model.addParameter(part, "L", str(L))
46 model.addParameter(part, "E", str(E))
47 model.addParameter(part, "P", str(P))
48
49 def vertical_body():
50     # Create YOZ sketch
51     sketch = model.addSketch(part, model.defaultPlane("YOZ"))
52
53     points = [(0, 0), (0, L), (L, L), (L, 0)]
54     geom_points = [geom.Pnt2d(*p) for p in points]
55     left, top, right, bottom = model.addPolygon(sketch, *geom_points)
56
57     # Set constraints
58     sketch.setFixed(left.startPoint())
59
60     sketch.setHorizontal(bottom)
61     sketch.setHorizontal(top)
62
63     sketch.setVertical(right)
64     sketch.setVertical(left)
65
66     sketch.setLength(top, "L")
67     sketch.setLength(left, "L")
68
69     sketch.setFilletWithRadius(left.endPoint(), 32)
70
71     model.do()  #!!!
72
73     # Create extrusion
74     body = model.addExtrusion(part, sketch.selectFace(), "E")
75
76     return body
77
78 def bottom_body():
79     # Create XOY sketch
80     sketch = model.addSketch(part, "Extrusion_1_1/Generated_Face&Sketch_1/SketchLine_4")
81
82     # Create base polygon
83     points = [(0, 0), (0, L), (P, L), (P, 16 + 16), (P - 20, 16 + 16), (P - 20, 16), (P, 16), (P, 0)]
84     points = [(p[0], -p[1]) for p in points]  # as we look to back of the face
85     geom_points = [geom.Pnt2d(*p) for p in points]
86     left, top, v2, h2, v1, h1, right, bottom = model.addPolygon(sketch, *geom_points)
87
88     points = [(P - 20, 16 + 16 / 2), (P - 20, 16 + 16), (P - 20, 16)]
89     points = [(p[0], -p[1]) for p in points]  # as we look to back of the face
90     center, start, end = [geom.Pnt2d(*p) for p in points]
91     arc = sketch.addArc(center, start, end, True)
92
93     # Set Auxiliarity
94     v1.setAuxiliary(True)
95
96     # Set constraints
97     sketch.setParallel(left, right)
98     sketch.setParallel(left, v2)
99     sketch.setParallel(bottom, h1)
100     sketch.setParallel(top, h2)
101
102     sketch.setPerpendicular(left, bottom)
103     sketch.setPerpendicular(left, top)
104
105     sketch.setEqual(top, bottom)
106     sketch.setEqual(h1, h2)
107
108     sketch.setCoincident(arc.center(), v1)
109     sketch.setCoincident(arc.startPoint(), h2.endPoint())
110     sketch.setCoincident(arc.endPoint(), h1.startPoint())
111
112     # Binding
113     left_e = sketch.addLine("[Extrusion_1_1/Generated_Face&Sketch_1/SketchLine_4][Extrusion_1_1/To_Face]")
114     sketch.setCoincident(left_e.startPoint(), left.endPoint())
115     sketch.setCoincident(left_e.endPoint(), left.startPoint())
116
117     # Dimensions
118     #sketch.setLength(v1, 16)
119     sketch.setLength(v2, 32)
120     sketch.setLength(h2, 20)
121     sketch.setLength(right, 16)
122     sketch.setLength(top, "P")
123     model.do()
124
125     # Create extrusion
126     body = model.addExtrusion(part, sketch.selectFace(), "-E")
127
128     model.do()
129
130     return body
131
132 def body_3():
133     # Create XOZ sketch
134     sketch = model.addSketch(part, "Boolean_1_1/Modified_Face&Sketch_1/SketchLine_3")
135
136     # Create base polygon
137     H, L, l, r = 28, 40, 8, 12
138
139     points = [(0, 0), (0, H), (l, H), (l + 2 * r, H), (L, H), (L, 0)]
140     points = [(p[0], -p[1]) for p in points]  # as we look to back of the face
141     geom_points = [geom.Pnt2d(*p) for p in points]
142     left, top_left, top_middle, top_right, right, bottom, = model.addPolygon(sketch, *geom_points)
143
144     points = [(l + r, H), (l + 2 * r, H), (l, H)]
145     points = [(p[0], -p[1]) for p in points]  # as we look to back of the face
146     center, start, end = [geom.Pnt2d(*p) for p in points]
147     arc = sketch.addArc(center, start, end, False)
148
149     # Set Auxiliarity
150     top_middle.setAuxiliary(True)
151
152     # Set constraints
153     sketch.setParallel(bottom, top_left)
154     sketch.setParallel(bottom, top_right)
155
156     sketch.setPerpendicular(bottom, left)
157     sketch.setPerpendicular(bottom, right)
158
159     sketch.setEqual(left, right)
160
161     sketch.setLength(bottom, L)
162     sketch.setLength(right, H)
163     sketch.setLength(top_left, l)
164
165     sketch.setCoincident(top_middle, arc.center())
166     sketch.setCoincident(top_middle.endPoint(), arc.startPoint())
167     sketch.setCoincident(top_middle.startPoint(), arc.endPoint())
168
169     sketch.setRadius(arc, r)
170
171     # Binding
172     bottom_e = sketch.addLine("[Boolean_1_1/Modified_Face&Sketch_2/SketchLine_6][Extrusion_2_1/To_Face]")
173     sketch.setCoincident(bottom_e, bottom.startPoint())
174     sketch.setCoincident(bottom_e.startPoint(), bottom.endPoint())
175
176     model.do()  #!!!
177
178     # Create extrusion
179     body = model.addExtrusion(part, sketch.selectFace(), "-(L-22)")
180
181     return body
182
183 def body_4():
184     # Create XOZ 2nd sketch
185     sketch = model.addSketch(part, "Boolean_2_1/Modified_Face&Sketch_2/SketchLine_6")
186
187     # Create base polygon
188     points = [(0, 0), (0, 1), (1, 0)]
189     points = [(p[0], -p[1]) for p in points]  # as we look to back of the face
190     geom_points = [geom.Pnt2d(*p) for p in points]
191     left, diagonal, bottom = model.addPolygon(sketch, *geom_points)
192
193     # Binding
194     bottom_e = sketch.addLine("[Boolean_2_1/Modified_Face&Extrusion_2_1/To_Face][Boolean_2_1/Modified_Face&Sketch_2/SketchLine_6]")
195     sketch.setCoincident(bottom_e.endPoint(), bottom.startPoint())
196     sketch.setCoincident(bottom_e.startPoint(), left.startPoint())
197
198     left_e = sketch.addLine("[Boolean_2_1/Modified_Face&Extrusion_3_1/From_Face][Extrusion_3_1/Generated_Face&Sketch_3/SketchLine_18]")
199     sketch.setCoincident(left_e.startPoint(), left.endPoint())
200
201     model.do()  #!!!
202
203     # Create extrusion
204     body = model.addExtrusion(part, sketch.selectFace(), "-12")
205
206     return body
207
208
209 b1 = vertical_body()
210 b2 = bottom_body()
211
212 boolean = model.addFuse(part, b1.results() + b2.results())
213 boolean.result().setName("Boolean_1_1")
214 model.do()
215
216 b3 = body_3()
217
218 boolean = model.addFuse(part, boolean.results() + b3.results())
219 boolean.result().setName("Boolean_2_1")
220 model.do()
221
222 # START DEBUG PURPOSES
223 # prepare a study without last operation to trap floating problem with degenerated line
224 #aPathToStore = os.path.join(os.getcwd(), "Data")
225 #print aPathToStore
226 #if not os.path.exists(aPathToStore):
227 #    os.mkdir(aPathToStore)
228 #results = ModelAPI.StringList()
229 #ModelAPI.ModelAPI_Session.get().save(aPathToStore, results)
230 # END DEBUG PURPOSES
231 b4 = body_4()
232
233 boolean = model.addFuse(part, boolean.results() + b4.results())
234 model.end()
235
236 assert(model.checkPythonDump())