Salome HOME
Set relative path to store document in test Platine
[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 import ModelAPI
8
9 # Initialisation
10 model.begin()
11 partset = model.moduleDocument()
12
13 # Create a new Part
14 part = model.addPart(partset).document()
15
16 L = 64
17 E = 16
18 P = 80
19
20 # Create Parameters
21 model.addParameter(part, "L", L)
22 model.addParameter(part, "E", E)
23 model.addParameter(part, "P", P)
24
25 def vertical_body():
26     # Create YOZ sketch
27     sketch = model.addSketch(part, model.defaultPlane("YOZ"))
28
29     points = [(0, 0), (0, L), (L, L), (L, 0)]
30     geom_points = [geom.Pnt2d(*p) for p in points]
31     left, top, right, bottom = sketch.addPolygon(*geom_points)
32
33     # Set constraints
34     sketch.setRigid(left.startPoint())
35
36     sketch.setHorizontal(bottom.result())
37     sketch.setHorizontal(top.result())
38
39     sketch.setVertical(right.result())
40     sketch.setVertical(left.result())
41
42     sketch.setLength(top.result(), "L")
43     sketch.setLength(left.result(), "L")
44
45     sketch.setFillet(left.endPoint(), 32)
46
47     model.do()  #!!!
48
49     # Create extrusion
50     body = model.addExtrusion(part, sketch.selectFace(), "E")
51
52     model.do()
53
54     return body
55
56 def bottom_body():
57     # Create XOY sketch
58     sketch = model.addSketch(part, "Extrusion_1_1/Generated_Face_4")
59
60     # Create base polygon
61     points = [(0, 0), (0, L), (P, L), (P, 16 + 16), (P - 20, 16 + 16), (P - 20, 16), (P, 16), (P, 0)]
62     points = [(p[0], -p[1]) for p in points]  # as we look to back of the face
63     geom_points = [geom.Pnt2d(*p) for p in points]
64     left, top, v2, h2, v1, h1, right, bottom = sketch.addPolygon(*geom_points)
65
66     points = [(P - 20, 16 + 16 / 2), (P - 20, 16), (P - 20, 16 + 16)]
67     points = [(p[0], -p[1]) for p in points]  # as we look to back of the face
68     center, start, end = [geom.Pnt2d(*p) for p in points]
69     arc = sketch.addArc(center, start, end, inversed=True)
70
71     # Set Auxiliarity
72     v1.setAuxiliary(True)
73
74     # Set constraints
75     sketch.setParallel(left.result(), right.result())
76     sketch.setParallel(left.result(), v2.result())
77     sketch.setParallel(bottom.result(), h1.result())
78     sketch.setParallel(top.result(), h2.result())
79
80     sketch.setPerpendicular(left.result(), bottom.result())
81     sketch.setPerpendicular(left.result(), top.result())
82
83     sketch.setEqual(top.result(), bottom.result())
84     sketch.setEqual(h1.result(), h2.result())
85
86     sketch.setCoincident(arc.center(), v1.result())
87     sketch.setCoincident(arc.startPoint(), h2.endPoint())
88     sketch.setCoincident(arc.endPoint(), h1.startPoint())
89
90     # Binding
91     left_e = sketch.addLine("Extrusion_1_1/Generated_Face_4&Extrusion_1_1/To_Face_1_1")
92     sketch.setCoincident(left_e.startPoint(), left.endPoint())
93     sketch.setCoincident(left_e.endPoint(), left.startPoint())
94
95     model.do()  #!!!
96
97     # Dimensions
98     sketch.setLength(v1.result(), 16)
99     sketch.setLength(h2.result(), 20)
100     sketch.setLength(right.result(), 16)
101     sketch.setLength(top.result(), "P")
102     model.do()
103
104     # Create extrusion
105     body = model.addExtrusion(part, sketch.selectFace(), "-E")
106
107     model.do()
108
109     return body
110
111 def body_3():
112     # Create XOZ sketch
113     sketch = model.addSketch(part, "Boolean_1_1/Modified_4")
114
115     # Create base polygon
116     H, L, l, r = 28, 40, 8, 12
117
118     points = [(0, 0), (0, H), (l, H), (l + 2 * r, H), (L, H), (L, 0)]
119     points = [(p[0], -p[1]) for p in points]  # as we look to back of the face
120     geom_points = [geom.Pnt2d(*p) for p in points]
121     left, top_left, top_middle, top_right, right, bottom, = sketch.addPolygon(*geom_points)
122
123     points = [(l + r, H), (l, H), (l + 2 * r, H)]
124     points = [(p[0], -p[1]) for p in points]  # as we look to back of the face
125     center, start, end = [geom.Pnt2d(*p) for p in points]
126     arc = sketch.addArc(center, start, end)
127
128     # Set Auxiliarity
129     top_middle.setAuxiliary(True)
130
131     # Set constraints
132     sketch.setParallel(bottom.result(), top_left.result())
133     sketch.setParallel(bottom.result(), top_right.result())
134
135     sketch.setPerpendicular(bottom.result(), left.result())
136     sketch.setPerpendicular(bottom.result(), right.result())
137
138     sketch.setEqual(left.result(), right.result())
139
140     sketch.setLength(bottom.result(), L)
141     sketch.setLength(right.result(), H)
142     sketch.setLength(top_left.result(), l)
143
144     sketch.setCoincident(top_middle.result(), arc.center())
145     sketch.setCoincident(top_middle.endPoint(), arc.startPoint())
146     sketch.setCoincident(top_middle.startPoint(), arc.endPoint())
147
148     sketch.setRadius(arc.result(), r)
149
150     # Binding
151     bottom_e = sketch.addLine("Boolean_1_1/Modified_5&Boolean_1_1/Modified_8")
152     sketch.setCoincident(bottom_e.result(), bottom.startPoint())
153     sketch.setCoincident(bottom_e.startPoint(), bottom.endPoint())
154
155     model.do()  #!!!
156
157     # Create extrusion
158     body = model.addExtrusion(part, sketch.selectFace(), "-(L-22)")
159
160     model.do()  #!!!
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 = sketch.addPolygon(*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     model.do()  #!!!
188
189     return body
190
191
192 b1 = vertical_body()
193 b2 = bottom_body()
194
195 boolean = model.addAddition(part, b1.result() + b2.result())
196 model.do()
197
198 b3 = body_3()
199
200 boolean = model.addAddition(part, boolean.result() + b3.result())
201 model.do()
202
203 # START DEBUG PURPOSES
204 # prepare a study without last operation to trap floating problem with degenerated line
205 results = ModelAPI.StringList()
206 ModelAPI.ModelAPI_Session.get().save("Data", results)
207 # END DEBUG PURPOSES
208 b4 = body_4()
209
210 boolean = model.addAddition(part, boolean.result() + b4.result())
211 model.do()