Salome HOME
Copyright update 2022
[modules/shaper.git] / src / PythonAPI / examples / Platine.py
index 556832c6dba763acedf41fe010c261eb1bd85e23..4c8d4fcdd3a2956ed0f81dbf7b1c33ccfc347159 100644 (file)
@@ -1,10 +1,34 @@
+# Copyright (C) 2014-2022  CEA/DEN, EDF R&D
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+#
+# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+#
+
 # Creation of Platine model using the end-user API
 # Author: Sergey POKHODENKO
 # -----------------------------
 
-from GeomDataAPI import geomDataAPI_Point2D
-import geom
-import model
+from salome.shaper import geom
+from salome.shaper import model
+
+# START DEBUG PURPOSES
+# Should be removed
+import os
+import ModelAPI
+# END DEBUG PURPOSES
 
 # Initialisation
 model.begin()
@@ -22,169 +46,191 @@ model.addParameter(part, "L", str(L))
 model.addParameter(part, "E", str(E))
 model.addParameter(part, "P", str(P))
 
-# Create YOZ sketch
-sketch = model.addSketch(part, model.defaultPlane("YOZ"))
+def vertical_body():
+    # Create YOZ sketch
+    sketch = model.addSketch(part, model.defaultPlane("YOZ"))
 
-model.do()
+    points = [(0, 0), (0, L), (L, L), (L, 0)]
+    geom_points = [geom.Pnt2d(*p) for p in points]
+    left, top, right, bottom = model.addPolygon(sketch, *geom_points)
 
-# Create base polygon
-H, L, r = 64, 64, 32
-dx, dy = 0, 0
+    # Set constraints
+    sketch.setFixed(left.startPoint())
 
-points = [(0, 0), (0, H), (L, H), (L, 0)]
-points = [(p[0] + dx, p[1] + dy) for p in points]
-geom_points = [geom.Pnt2d(*p) for p in points]
-left, top, right, bottom = sketch.addPolygon(*geom_points)
+    sketch.setHorizontal(bottom)
+    sketch.setHorizontal(top)
 
-model.do()
+    sketch.setVertical(right)
+    sketch.setVertical(left)
 
-# Set constraints
-sketch.setRigid(left.startPointData())
+    sketch.setLength(top, "L")
+    sketch.setLength(left, "L")
 
-sketch.setHorizontal(bottom.result())
-sketch.setHorizontal(top.result())
+    sketch.setFilletWithRadius(left.endPoint(), 32)
 
-sketch.setVertical(right.result())
-sketch.setVertical(left.result())
+    model.do()  #!!!
 
-sketch.setLength(top.result(), "L")
-sketch.setLength(left.result(), "L")
+    # Create extrusion
+    body = model.addExtrusion(part, sketch.selectFace(), "E")
 
-sketch.setFillet(left.endPointData(), r)
+    return body
 
-model.do()
+def bottom_body():
+    # Create XOY sketch
+    sketch = model.addSketch(part, "Extrusion_1_1/Generated_Face&Sketch_1/SketchLine_4")
 
-# Create extrusion
-YOZ_body = model.addExtrusion(part, sketch.selectFace(), "E")
+    # Create base polygon
+    points = [(0, 0), (0, L), (P, L), (P, 16 + 16), (P - 20, 16 + 16), (P - 20, 16), (P, 16), (P, 0)]
+    points = [(p[0], -p[1]) for p in points]  # as we look to back of the face
+    geom_points = [geom.Pnt2d(*p) for p in points]
+    left, top, v2, h2, v1, h1, right, bottom = model.addPolygon(sketch, *geom_points)
 
-model.do()
+    points = [(P - 20, 16 + 16 / 2), (P - 20, 16 + 16), (P - 20, 16)]
+    points = [(p[0], -p[1]) for p in points]  # as we look to back of the face
+    center, start, end = [geom.Pnt2d(*p) for p in points]
+    arc = sketch.addArc(center, start, end, True)
 
-# Create XOY sketch
-sketch = model.addSketch(part, model.defaultPlane("XOY"))
+    # Set Auxiliarity
+    v1.setAuxiliary(True)
 
-# Create base polygon
-L, P = 64, P
-dx, dy = E, 0
+    # Set constraints
+    sketch.setParallel(left, right)
+    sketch.setParallel(left, v2)
+    sketch.setParallel(bottom, h1)
+    sketch.setParallel(top, h2)
 
-points = [(0, 0), (0, L), (P, L), (P, 16 + 16), (P - 20, 16 + 16), (P - 20, 16), (P, 16), (P, 0)]
-points = [(p[0] + dx, p[1] + dy) for p in points]
-geom_points = [geom.Pnt2d(*p) for p in points]
-left, top, v2, h2, v1, h1, right, bottom = sketch.addPolygon(*geom_points)
+    sketch.setPerpendicular(left, bottom)
+    sketch.setPerpendicular(left, top)
 
-points = [(P - 20, 16 + 16 / 2), (P - 20, 16), (P - 20, 16 + 16)]
-points = [(p[0] + dx, p[1] + dy) for p in points]
-center, start, end = [geom.Pnt2d(*p) for p in points]
-arc = sketch.addArc(center, start, end)
+    sketch.setEqual(top, bottom)
+    sketch.setEqual(h1, h2)
 
-model.do()
+    sketch.setCoincident(arc.center(), v1)
+    sketch.setCoincident(arc.startPoint(), h2.endPoint())
+    sketch.setCoincident(arc.endPoint(), h1.startPoint())
 
-# Set Auxiliarity
-v1.setAuxiliary(True)
+    # Binding
+    left_e = sketch.addLine("[Extrusion_1_1/Generated_Face&Sketch_1/SketchLine_4][Extrusion_1_1/To_Face]")
+    sketch.setCoincident(left_e.startPoint(), left.endPoint())
+    sketch.setCoincident(left_e.endPoint(), left.startPoint())
 
-# Set constraints
-sketch.setCoincident(arc.center(), v1.result())
-sketch.setCoincident(h2.endPointData(), arc.startPoint())
-sketch.setCoincident(arc.endPoint(), h1.startPointData())
+    # Dimensions
+    #sketch.setLength(v1, 16)
+    sketch.setLength(v2, 32)
+    sketch.setLength(h2, 20)
+    sketch.setLength(right, 16)
+    sketch.setLength(top, "P")
+    model.do()
 
-sketch.setPerpendicular(bottom.result(), right.result())
-sketch.setPerpendicular(top.result(), left.result())
-sketch.setPerpendicular(top.result(), v2.result())
+    # Create extrusion
+    body = model.addExtrusion(part, sketch.selectFace(), "-E")
 
-sketch.setParallel(bottom.result(), h1.result())
-sketch.setParallel(h1.result(), h2.result())
-sketch.setParallel(h2.result(), top.result())
-sketch.setParallel(v1.result(), v2.result())
+    model.do()
 
-sketch.setLength(top.result(), "P")
-sketch.setLength(h2.result(), "20")
-sketch.setLength(v1.result(), "16")
-sketch.setLength(right.result(), "16")
+    return body
 
-model.do()
+def body_3():
+    # Create XOZ sketch
+    sketch = model.addSketch(part, "Boolean_1_1/Modified_Face&Sketch_1/SketchLine_3")
 
-# Create extrusion
-XOY_body = model.addExtrusion(part, sketch.selectFace(), "E")
-# TODO(spo): collision with first object. Move object to the right place.
+    # Create base polygon
+    H, L, l, r = 28, 40, 8, 12
 
-# Create XOZ sketch
-plane = model.defaultPlane("XOZ")
-plane.o.setY(64)
-sketch = model.addSketch(part, plane)
+    points = [(0, 0), (0, H), (l, H), (l + 2 * r, H), (L, H), (L, 0)]
+    points = [(p[0], -p[1]) for p in points]  # as we look to back of the face
+    geom_points = [geom.Pnt2d(*p) for p in points]
+    left, top_left, top_middle, top_right, right, bottom, = model.addPolygon(sketch, *geom_points)
 
-# TODO(spo): select origin point
+    points = [(l + r, H), (l + 2 * r, H), (l, H)]
+    points = [(p[0], -p[1]) for p in points]  # as we look to back of the face
+    center, start, end = [geom.Pnt2d(*p) for p in points]
+    arc = sketch.addArc(center, start, end, False)
 
-# Create base polygon
-H, L, l, r = 28, 40, 8, 12
-dx, dy = E, E
+    # Set Auxiliarity
+    top_middle.setAuxiliary(True)
 
-points = [(l + 2 * r, H), (L, H), (L, 0), (0, 0), (0, H), (l, H)]
-points = [(p[0] + dx, p[1] + dy) for p in points]
-geom_points = [geom.Pnt2d(*p) for p in points]
-top_right, right, bottom, left, top_left = sketch.addPolyline(*geom_points)
+    # Set constraints
+    sketch.setParallel(bottom, top_left)
+    sketch.setParallel(bottom, top_right)
 
-points = [(l + r, H), (l, H), (l + 2 * r, H)]
-points = [(p[0] + dx, p[1] + dy) for p in points]
-center, start, end = [geom.Pnt2d(*p) for p in points]
-arc = sketch.addArc(center, start, end)
+    sketch.setPerpendicular(bottom, left)
+    sketch.setPerpendicular(bottom, right)
 
-model.do()
+    sketch.setEqual(left, right)
 
-# Set constraints
-sketch.setCoincident(top_left.endPointData(), arc.startPoint())
-sketch.setCoincident(arc.endPoint(), top_right.endPointData())
+    sketch.setLength(bottom, L)
+    sketch.setLength(right, H)
+    sketch.setLength(top_left, l)
 
-sketch.setPerpendicular(bottom.result(), right.result())
+    sketch.setCoincident(top_middle, arc.center())
+    sketch.setCoincident(top_middle.endPoint(), arc.startPoint())
+    sketch.setCoincident(top_middle.startPoint(), arc.endPoint())
 
-sketch.setParallel(bottom.result(), top_left.result())
-sketch.setParallel(bottom.result(), top_right.result())
-sketch.setParallel(left.result(), right.result())
+    sketch.setRadius(arc, r)
 
-sketch.setLength(bottom.result(), L)
-sketch.setLength(right.result(), H)
-sketch.setLength(top_left.result(), l)
+    # Binding
+    bottom_e = sketch.addLine("[Boolean_1_1/Modified_Face&Sketch_2/SketchLine_6][Extrusion_2_1/To_Face]")
+    sketch.setCoincident(bottom_e, bottom.startPoint())
+    sketch.setCoincident(bottom_e.startPoint(), bottom.endPoint())
 
-sketch.setRadius(arc.result(), r)
+    model.do()  #!!!
 
-model.do()
+    # Create extrusion
+    body = model.addExtrusion(part, sketch.selectFace(), "-(L-22)")
 
-# Create extrusion
-XOZ_body = model.addExtrusion(part, sketch.selectFace(), "42")
+    return body
 
-model.do()
+def body_4():
+    # Create XOZ 2nd sketch
+    sketch = model.addSketch(part, "Boolean_2_1/Modified_Face&Sketch_2/SketchLine_6")
 
-# Create XOZ 2nd sketch
-plane = model.defaultPlane("XOZ")
-plane.o.setY(64)
-sketch = model.addSketch(part, plane)
+    # Create base polygon
+    points = [(0, 0), (0, 1), (1, 0)]
+    points = [(p[0], -p[1]) for p in points]  # as we look to back of the face
+    geom_points = [geom.Pnt2d(*p) for p in points]
+    left, diagonal, bottom = model.addPolygon(sketch, *geom_points)
 
-# TODO(spo): make coincidence with existing points
+    # Binding
+    bottom_e = sketch.addLine("[Boolean_2_1/Modified_Face&Extrusion_2_1/To_Face][Boolean_2_1/Modified_Face&Sketch_2/SketchLine_6]")
+    sketch.setCoincident(bottom_e.endPoint(), bottom.startPoint())
+    sketch.setCoincident(bottom_e.startPoint(), left.startPoint())
 
-# Create base polygon
-dx, dy = E + L, E
-H, L = 28, 96 - dx
+    left_e = sketch.addLine("[Boolean_2_1/Modified_Face&Extrusion_3_1/From_Face][Extrusion_3_1/Generated_Face&Sketch_3/SketchLine_18]")
+    sketch.setCoincident(left_e.startPoint(), left.endPoint())
 
-points = [(0, 0), (0, H), (L, 0)]
-points = [(p[0] + dx, p[1] + dy) for p in points]
-geom_points = [geom.Pnt2d(*p) for p in points]
-left, diagonal, bottom = sketch.addPolygon(*geom_points)
+    model.do()  #!!!
 
-model.do()
+    # Create extrusion
+    body = model.addExtrusion(part, sketch.selectFace(), "-12")
 
-# TODO(spo): make coincidence instead of constraints
-sketch.setLength(bottom.result(), L)
-sketch.setLength(left.result(), H)
+    return body
 
-sketch.setPerpendicular(bottom.result(), left.result())
 
-model.do()
-
-# Create extrusion
-XOZ_2nd_body = model.addExtrusion(part, sketch.selectFace(), "12")
+b1 = vertical_body()
+b2 = bottom_body()
 
+boolean = model.addFuse(part, b1.results() + b2.results())
+boolean.result().setName("Boolean_1_1")
 model.do()
 
-# Create Boolean
-boolean = model.addAddition(part, YOZ_body.result() + XOY_body.result() +
-                                  XOZ_body.result() + XOZ_2nd_body.result(), [])
+b3 = body_3()
 
+boolean = model.addFuse(part, boolean.results() + b3.results())
+boolean.result().setName("Boolean_2_1")
 model.do()
+
+# START DEBUG PURPOSES
+# prepare a study without last operation to trap floating problem with degenerated line
+#aPathToStore = os.path.join(os.getcwd(), "Data")
+#print aPathToStore
+#if not os.path.exists(aPathToStore):
+#    os.mkdir(aPathToStore)
+#results = ModelAPI.StringList()
+#ModelAPI.ModelAPI_Session.get().save(aPathToStore, results)
+# END DEBUG PURPOSES
+b4 = body_4()
+
+boolean = model.addFuse(part, boolean.results() + b4.results())
+model.end()
+
+assert(model.checkPythonDump())