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