Salome HOME
Make the "Platine" test more stable
[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     return body
58
59 def bottom_body():
60     # Create XOY sketch
61     sketch = model.addSketch(part, "Extrusion_1_1/Generated_Face_3")
62
63     # Create base polygon
64     points = [(0, 0), (0, L), (P, L), (P, 16 + 16), (P - 20, 16 + 16), (P - 20, 16), (P, 16), (P, 0)]
65     points = [(p[0], -p[1]) for p in points]  # as we look to back of the face
66     geom_points = [geom.Pnt2d(*p) for p in points]
67     left, top, v2, h2, v1, h1, right, bottom = model.addPolygon(sketch, *geom_points)
68
69     points = [(P - 20, 16 + 16 / 2), (P - 20, 16), (P - 20, 16 + 16)]
70     points = [(p[0], -p[1]) for p in points]  # as we look to back of the face
71     center, start, end = [geom.Pnt2d(*p) for p in points]
72     arc = sketch.addArc(center, start, end, True)
73
74     # Set Auxiliarity
75     v1.setAuxiliary(True)
76
77     # Set constraints
78     sketch.setParallel(left, right)
79     sketch.setParallel(left, v2)
80     sketch.setParallel(bottom, h1)
81     sketch.setParallel(top, h2)
82
83     sketch.setPerpendicular(left, bottom)
84     sketch.setPerpendicular(left, top)
85
86     sketch.setEqual(top, bottom)
87     sketch.setEqual(h1, h2)
88
89     sketch.setCoincident(arc.center(), v1)
90     sketch.setCoincident(arc.startPoint(), h2.endPoint())
91     sketch.setCoincident(arc.endPoint(), h1.startPoint())
92
93     # Binding
94     left_e = sketch.addLine("Extrusion_1_1/Generated_Face_3&Extrusion_1_1/To_Face_1_1")
95     sketch.setCoincident(left_e.startPoint(), left.endPoint())
96     sketch.setCoincident(left_e.endPoint(), left.startPoint())
97
98     # Dimensions
99     #sketch.setLength(v1, 16)
100     sketch.setLength(v2, 32)
101     sketch.setLength(h2, 20)
102     sketch.setLength(right, 16)
103     sketch.setLength(top, "P")
104     model.do()
105
106     # Create extrusion
107     body = model.addExtrusion(part, sketch.selectFace(), "-E")
108
109     model.do()
110
111     return body
112
113 def body_3():
114     # Create XOZ sketch
115     sketch = model.addSketch(part, "Boolean_1_1/Modified_4")
116
117     # Create base polygon
118     H, L, l, r = 28, 40, 8, 12
119
120     points = [(0, 0), (0, H), (l, H), (l + 2 * r, H), (L, H), (L, 0)]
121     points = [(p[0], -p[1]) for p in points]  # as we look to back of the face
122     geom_points = [geom.Pnt2d(*p) for p in points]
123     left, top_left, top_middle, top_right, right, bottom, = model.addPolygon(sketch, *geom_points)
124
125     points = [(l + r, H), (l + 2 * r, H), (l, H)]
126     points = [(p[0], -p[1]) for p in points]  # as we look to back of the face
127     center, start, end = [geom.Pnt2d(*p) for p in points]
128     arc = sketch.addArc(center, start, end, False)
129
130     # Set Auxiliarity
131     top_middle.setAuxiliary(True)
132
133     # Set constraints
134     sketch.setParallel(bottom, top_left)
135     sketch.setParallel(bottom, top_right)
136
137     sketch.setPerpendicular(bottom, left)
138     sketch.setPerpendicular(bottom, right)
139
140     sketch.setEqual(left, right)
141
142     sketch.setLength(bottom, L)
143     sketch.setLength(right, H)
144     sketch.setLength(top_left, l)
145
146     sketch.setCoincident(top_middle, arc.center())
147     sketch.setCoincident(top_middle.endPoint(), arc.startPoint())
148     sketch.setCoincident(top_middle.startPoint(), arc.endPoint())
149
150     sketch.setRadius(arc, r)
151
152     # Binding
153     bottom_e = sketch.addLine("Boolean_1_1/Modified_5&Boolean_1_1/Modified_8")
154     sketch.setCoincident(bottom_e, bottom.startPoint())
155     sketch.setCoincident(bottom_e.startPoint(), bottom.endPoint())
156
157     model.do()  #!!!
158
159     # Create extrusion
160     body = model.addExtrusion(part, sketch.selectFace(), "-(L-22)")
161
162     return body
163
164 def body_4():
165     # Create XOZ 2nd sketch
166     sketch = model.addSketch(part, "Boolean_2_1/Modified_4")
167
168     # Create base polygon
169     points = [(0, 0), (0, 1), (1, 0)]
170     points = [(p[0], -p[1]) for p in points]  # as we look to back of the face
171     geom_points = [geom.Pnt2d(*p) for p in points]
172     left, diagonal, bottom = model.addPolygon(sketch, *geom_points)
173
174     # Binding
175     bottom_e = sketch.addLine("Boolean_2_1/Modified_3&Boolean_2_1/Modified_4")
176     sketch.setCoincident(bottom_e.endPoint(), bottom.startPoint())
177     sketch.setCoincident(bottom_e.startPoint(), left.startPoint())
178
179     left_e = sketch.addLine("Boolean_2_1/Modified_6&Boolean_2_1/Modified_8")
180     sketch.setCoincident(left_e.startPoint(), left.endPoint())
181
182     model.do()  #!!!
183
184     # Create extrusion
185     body = model.addExtrusion(part, sketch.selectFace(), "-12")
186
187     return body
188
189
190 b1 = vertical_body()
191 b2 = bottom_body()
192
193 boolean = model.addFuse(part, b1.result() + b2.result())
194 model.do()
195
196 b3 = body_3()
197
198 boolean = model.addFuse(part, boolean.result() + b3.result())
199 model.do()
200
201 # START DEBUG PURPOSES
202 # prepare a study without last operation to trap floating problem with degenerated line
203 #aPathToStore = os.path.join(os.getcwd(), "Data")
204 #print aPathToStore
205 #if not os.path.exists(aPathToStore):
206 #    os.mkdir(aPathToStore)
207 #results = ModelAPI.StringList()
208 #ModelAPI.ModelAPI_Session.get().save(aPathToStore, results)
209 # END DEBUG PURPOSES
210 b4 = body_4()
211
212 boolean = model.addFuse(part, boolean.result() + b4.result())
213 model.end()
214
215 assert(model.checkPythonDump())