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