Salome HOME
Update copyrights
[modules/shaper.git] / src / GeomAPI / Test / TestCylinder.py
1 # Copyright (C) 2018-2019  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 email : webmaster.salome@opencascade.com
18 #
19
20 from GeomAPI import *
21 from SketchAPI import *
22
23 from salome.shaper import model
24
25 import math
26
27 TOLERANCE = 1.e-7
28
29 def assertCylinder(theCylinder, theLocation, theAxis, theRadius, theHeight):
30     assert(theCylinder is not None)
31     assert(theCylinder.isInfinite() == False)
32     aLoc = theCylinder.location()
33     aDir = theCylinder.axis()
34     assert(aLoc.distance(theLocation) < TOLERANCE), "({}, {}, {}) != expected ({}, {}, {})".format(aLoc.x(), aLoc.y(), aLoc.z(), theLocation.x(), theLocation.y(), theLocation.z())
35     assert(aDir.isParallel(theAxis, TOLERANCE)), "dir({}, {}, {}) is not parallel to dir({}, {}, {})".format(aDir.x(), aDir.y(), aDir.z(), theAxis.x(), theAxis.y(), theAxis.z())
36     assert(math.fabs(theCylinder.radius() - theRadius) < TOLERANCE), "Radius {} != {}".format(theCylinder.radius(), theRadius)
37     assert(math.fabs(theCylinder.height() - theHeight) < TOLERANCE), "Height {} != {}".format(theCylinder.height(), theHeight)
38
39 def checkCylinderFace(theDocument, theFaceName, theLocation, theAxis, theRadius, theHeight):
40     aFace = model.addFace(theDocument, [model.selection("FACE", theFaceName)])
41     aShape = aFace.result().resultSubShapePair()[0].shape()
42     assert(aShape.isFace())
43     assertCylinder(aShape.face().getCylinder(), theLocation, theAxis, theRadius, theHeight)
44     theDocument.removeFeature(aFace.feature())
45
46 def checkCylinderShell(theDocument, theFaceNames, theLocation, theAxis, theRadius, theHeight):
47     aSelection = []
48     for name in theFaceNames:
49         aSelection.append(model.selection("FACE", name))
50     aShell = model.addShell(theDocument, aSelection)
51     aShape = aShell.result().resultSubShapePair()[0].shape()
52     assert(aShape.isShell())
53     assertCylinder(aShape.shell().getCylinder(), theLocation, theAxis, theRadius, theHeight)
54     theDocument.removeFeature(aShell.feature())
55
56 def checkCylinderSolid(theDocument, theFaceNames, theLocation, theAxis, theRadius, theHeight):
57     aSelection = []
58     for name in theFaceNames:
59         aSelection.append(model.selection("FACE", name))
60     aSolid = model.addSolid(theDocument, aSelection)
61     aShape = aSolid.result().resultSubShapePair()[0].shape()
62     assert(aShape.isSolid())
63     assertCylinder(aShape.solid().getCylinder(), theLocation, theAxis, theRadius, theHeight)
64     theDocument.removeFeature(aSolid.feature())
65
66 def checkCylinderAll(theDocument, theFeature, theFaceName, theLocation, theAxis, theRadius, theHeight):
67     aShape = theFeature.result().resultSubShapePair()[0].shape()
68     assert(aShape.isSolid())
69     assertCylinder(aShape.solid().getCylinder(), theLocation, theAxis, theRadius, theHeight)
70
71     checkCylinderShell(theDocument, [theFaceName], theLocation, theAxis, theRadius, theHeight)
72     checkCylinderFace(theDocument, theFaceName, theLocation, theAxis, theRadius, theHeight)
73
74 def checkNonCylinder(theFeature):
75     aShape = theFeature.result().resultSubShapePair()[0].shape()
76     assert(aShape.isSolid())
77     assert(aShape.solid().getCylinder() is None)
78
79 def checkNonCylindricalShell(theFeature):
80     aShape = theFeature.result().resultSubShapePair()[0].shape()
81     assert(aShape.isShell())
82     assert(aShape.shell().getCylinder() is None)
83
84
85 model.begin()
86 partSet = model.moduleDocument()
87 Part_1 = model.addPart(partSet)
88 Part_1_doc = Part_1.document()
89 ParamH = model.addParameter(Part_1_doc, "H", "10")
90 ParamR = model.addParameter(Part_1_doc, "R", "10")
91 ParamAngle = model.addParameter(Part_1_doc, "Angle", "30")
92 Cylinder_1 = model.addCylinder(Part_1_doc, model.selection("VERTEX", "PartSet/Origin"), model.selection("EDGE", "PartSet/OZ"), "2*R", "H")
93 Sketch_1 = model.addSketch(Part_1_doc, model.selection("FACE", "Cylinder_1_1/Face_2"))
94 SketchProjection_1 = Sketch_1.addProjection(model.selection("VERTEX", "[Cylinder_1_1/Face_1][Cylinder_1_1/Face_2]__cc"), False)
95 SketchPoint_1 = SketchProjection_1.createdFeature()
96 SketchCircle_1 = Sketch_1.addCircle(0, 0, 10)
97 SketchConstraintCoincidence_1 = Sketch_1.setCoincident(SketchPoint_1.result(), SketchCircle_1.center())
98 SketchConstraintRadius_1 = Sketch_1.setRadius(SketchCircle_1.results()[1], "R")
99 model.do()
100 Extrusion_1 = model.addExtrusion(Part_1_doc, [model.selection("FACE", "Sketch_1/Face-SketchCircle_1_2f")], model.selection(), "H", 0)
101 Sketch_2 = model.addSketch(Part_1_doc, model.standardPlane("XOZ"))
102 SketchLine_1 = Sketch_2.addLine(5, 20, 0, 20)
103 SketchLine_2 = Sketch_2.addLine(0, 20, 0, 30)
104 SketchLine_3 = Sketch_2.addLine(0, 30, 5, 30)
105 SketchLine_4 = Sketch_2.addLine(5, 30, 5, 20)
106 SketchConstraintCoincidence_2 = Sketch_2.setCoincident(SketchLine_4.endPoint(), SketchLine_1.startPoint())
107 SketchConstraintCoincidence_3 = Sketch_2.setCoincident(SketchLine_1.endPoint(), SketchLine_2.startPoint())
108 SketchConstraintCoincidence_4 = Sketch_2.setCoincident(SketchLine_2.endPoint(), SketchLine_3.startPoint())
109 SketchConstraintCoincidence_5 = Sketch_2.setCoincident(SketchLine_3.endPoint(), SketchLine_4.startPoint())
110 SketchConstraintHorizontal_1 = Sketch_2.setHorizontal(SketchLine_1.result())
111 SketchConstraintVertical_1 = Sketch_2.setVertical(SketchLine_2.result())
112 SketchConstraintHorizontal_2 = Sketch_2.setHorizontal(SketchLine_3.result())
113 SketchConstraintVertical_2 = Sketch_2.setVertical(SketchLine_4.result())
114 SketchConstraintLength_1 = Sketch_2.setLength(SketchLine_1.result(), "R/2")
115 SketchConstraintLength_2 = Sketch_2.setLength(SketchLine_2.result(), "H")
116 SketchIntersectionPoint_1 = Sketch_2.addIntersectionPoint(model.selection("EDGE", "[Extrusion_1_1/Generated_Face&Sketch_1/SketchCircle_1_2][Extrusion_1_1/To_Face]"), False)
117 [SketchPoint_2, SketchPoint_3] = SketchIntersectionPoint_1.intersectionPoints()
118 SketchConstraintCoincidence_6 = Sketch_2.setCoincident(SketchAPI_Point(SketchPoint_2).coordinates(), SketchLine_1.result())
119 SketchProjection_2 = Sketch_2.addProjection(model.selection("EDGE", "PartSet/OZ"), False)
120 SketchLine_5 = SketchProjection_2.createdFeature()
121 SketchConstraintCoincidence_7 = Sketch_2.setCoincident(SketchLine_2.endPoint(), SketchLine_5.result())
122 model.do()
123 Revolution_1 = model.addRevolution(Part_1_doc, [model.selection("FACE", "Sketch_2/Face-SketchLine_4r-SketchLine_3r-SketchLine_2r-SketchLine_1r")], model.selection("EDGE", "PartSet/OZ"), 270, 0)
124 model.do()
125
126 # Test 1. Check cylinders
127 aLoc1 = GeomAPI.GeomAPI_Pnt(0, 0, 0)
128 aLoc2 = GeomAPI.GeomAPI_Pnt(0, 0, 2 * ParamH.value())
129 aLoc3 = GeomAPI.GeomAPI_Pnt(0, 0, 3 * ParamH.value())
130 anAxis = GeomAPI.GeomAPI_Dir(0, 0, 1)
131 checkCylinderAll(Part_1_doc, Cylinder_1, "Cylinder_1_1/Face_1", aLoc1, anAxis, 2 * ParamR.value(), ParamH.value())
132 checkCylinderAll(Part_1_doc, Extrusion_1, "Extrusion_1_1/Generated_Face&Sketch_1/SketchCircle_1_2", aLoc2, anAxis, ParamR.value(), ParamH.value())
133 checkNonCylinder(Revolution_1)
134 checkCylinderShell(Part_1_doc, ["Revolution_1_1/Generated_Face&Sketch_2/SketchLine_4"], aLoc3, anAxis, 0.5 * ParamR.value(), ParamH.value())
135 checkCylinderFace(Part_1_doc, "Revolution_1_1/Generated_Face&Sketch_2/SketchLine_4", aLoc3, anAxis, 0.5 * ParamR.value(), ParamH.value())
136
137 # Test 2. Rotate cylinders
138 Rotation_1 = model.addRotation(Part_1_doc, [model.selection("SOLID", "Cylinder_1_1")], model.selection("EDGE", "PartSet/OX"), "Angle")
139 Rotation_2 = model.addRotation(Part_1_doc, [model.selection("SOLID", "Extrusion_1_1")], model.selection("EDGE", "PartSet/OX"), "Angle")
140 Rotation_3 = model.addRotation(Part_1_doc, [model.selection("SOLID", "Revolution_1_1")], model.selection("EDGE", "PartSet/OX"), "Angle")
141
142 anAngle = ParamAngle.value() * math.pi / 180.0
143 aCosAngle = math.cos(anAngle)
144 aSinAngle = math.sin(anAngle)
145 anAxis = GeomAPI.GeomAPI_Dir(0, anAxis.y() * aCosAngle - anAxis.z() * aSinAngle, anAxis.y() * aSinAngle + anAxis.z() * aCosAngle)
146 aLoc1 = GeomAPI.GeomAPI_Pnt(0, aLoc1.y() * aCosAngle - aLoc1.z() * aSinAngle, aLoc1.y() * aSinAngle + aLoc1.z() * aCosAngle)
147 aLoc2 = GeomAPI.GeomAPI_Pnt(0, aLoc2.y() * aCosAngle - aLoc2.z() * aSinAngle, aLoc2.y() * aSinAngle + aLoc2.z() * aCosAngle)
148 aLoc3 = GeomAPI.GeomAPI_Pnt(0, aLoc3.y() * aCosAngle - aLoc3.z() * aSinAngle, aLoc3.y() * aSinAngle + aLoc3.z() * aCosAngle)
149 checkCylinderAll(Part_1_doc, Rotation_1, "Rotation_1_1/MF:Rotated&Cylinder_1_1/Face_1", aLoc1, anAxis, 2 * ParamR.value(), ParamH.value())
150 checkCylinderAll(Part_1_doc, Rotation_2, "Rotation_2_1/MF:Rotated&Sketch_1/SketchCircle_1_2", aLoc2, anAxis, ParamR.value(), ParamH.value())
151 checkNonCylinder(Rotation_3)
152 checkCylinderShell(Part_1_doc, ["Rotation_3_1/MF:Rotated&Sketch_2/SketchLine_4"], aLoc3, anAxis, 0.5 * ParamR.value(), ParamH.value())
153 checkCylinderFace(Part_1_doc, "Rotation_3_1/MF:Rotated&Sketch_2/SketchLine_4", aLoc3, anAxis, 0.5 * ParamR.value(), ParamH.value())
154
155 # Test 3. Split cylinder and compose a shell
156 Plane_4 = model.addPlane(Part_1_doc, model.selection("FACE", "PartSet/XOY"), "2.2*H", False)
157 Plane_5 = model.addPlane(Part_1_doc, model.selection("FACE", "PartSet/XOZ"), "H", False)
158 Partition_1_objects = [model.selection("SOLID", "Rotation_3_1"), model.selection("FACE", "Plane_1"), model.selection("FACE", "Plane_2")]
159 Partition_1 = model.addPartition(Part_1_doc, Partition_1_objects)
160
161 Shell_1_objects = ["Partition_1_1_1/Modified_Face&Sketch_2/SketchLine_4",
162                    "Partition_1_1_4/Modified_Face&Sketch_2/SketchLine_4",
163                    "(Partition_1_1_2/Modified_Face&Revolution_1_1/To_Face)(Partition_1_1_2/Modified_Face&Sketch_2/SketchLine_1)"]
164 checkCylinderShell(Part_1_doc, Shell_1_objects, aLoc3, anAxis, 0.5 * ParamR.value(), ParamH.value())
165
166 # Test 4. Split cylinder and compose a solid
167 Partition_2 = model.addPartition(Part_1_doc, [model.selection("SOLID", "Rotation_1_1"), model.selection("FACE", "PartSet/XOZ")])
168 Solid_1_objects = ["(Partition_2_1_1/Modified_Face&Cylinder_1_1/Face_3)(Partition_2_1_1/Modified_Face&PartSet/XOZ/XOZ)(Partition_2_1_1/Modified_Face&Cylinder_1_1/Face_2)",
169                    "Partition_2_1_1/Modified_Face&Cylinder_1_1/Face_1&weak_name_2",
170                    "Partition_2_1_1/Modified_Face&Cylinder_1_1/Face_2",
171                    "Partition_2_1_1/Modified_Face&Cylinder_1_1/Face_3",
172                    "Partition_2_1_2/Modified_Face&Cylinder_1_1/Face_1",
173                    "Partition_2_1_2/Modified_Face&Cylinder_1_1/Face_2",
174                    "Partition_2_1_2/Modified_Face&Cylinder_1_1/Face_3"]
175 checkCylinderSolid(Part_1_doc, Solid_1_objects, aLoc1, anAxis, 2 * ParamR.value(), ParamH.value())
176
177 # Test 5. Check non-cylinder
178 Sketch_3 = model.addSketch(Part_1_doc, model.defaultPlane("XOY"))
179 SketchCircle_2 = Sketch_3.addCircle(12.62721775445329, 9.188425784259302, 5)
180 SketchCircle_3 = Sketch_3.addCircle(16.49821418064359, 12.35313535520289, 5)
181 SketchConstraintRadius_2 = Sketch_3.setRadius(SketchCircle_2.results()[1], 5)
182 SketchConstraintEqual_1 = Sketch_3.setEqual(SketchCircle_2.results()[1], SketchCircle_3.results()[1])
183 SketchConstraintDistance_1 = Sketch_3.setDistance(SketchCircle_2.center(), SketchCircle_3.center(), 5, True)
184 model.do()
185 Extrusion_2 = model.addExtrusion(Part_1_doc, [model.selection("COMPOUND", "Sketch_3")], model.selection(), 10, 0)
186 Solid_1_objects = [model.selection("FACE", "Extrusion_2_1_1/From_Face"), model.selection("FACE", "Extrusion_2_1_1/To_Face"), model.selection("FACE", "Extrusion_2_1_2/From_Face"), model.selection("FACE", "Extrusion_2_1_2/Generated_Face&Sketch_3/SketchCircle_3_2&weak_name_2"), model.selection("FACE", "Extrusion_2_1_2/Generated_Face&Sketch_3/SketchCircle_3_2&weak_name_1"), model.selection("FACE", "Extrusion_2_1_2/To_Face"), model.selection("FACE", "Extrusion_2_1_3/From_Face"), model.selection("FACE", "Extrusion_2_1_3/Generated_Face&Sketch_3/SketchCircle_2_2"), model.selection("FACE", "Extrusion_2_1_3/To_Face")]
187 Solid_1 = model.addSolid(Part_1_doc, Solid_1_objects)
188 checkNonCylinder(Solid_1)
189
190 # Test 6. Check non-cylindrical shell
191 Shell_1_objects = [model.selection("FACE", "(Partition_2_1_1/Modified_Face&Cylinder_1_1/Face_3)(Partition_2_1_1/Modified_Face&PartSet/XOZ/XOZ)(Partition_2_1_1/Modified_Face&Cylinder_1_1/Face_2)"),
192                    model.selection("FACE", "Partition_2_1_1/Modified_Face&Cylinder_1_1/Face_1&weak_name_2"),
193                    model.selection("FACE", "Partition_2_1_1/Modified_Face&Cylinder_1_1/Face_2"),
194                    model.selection("FACE", "Partition_2_1_1/Modified_Face&Cylinder_1_1/Face_3"),
195                    model.selection("FACE", "Partition_2_1_2/Modified_Face&Cylinder_1_1/Face_1"),
196                    model.selection("FACE", "Partition_2_1_2/Modified_Face&Cylinder_1_1/Face_2"),
197                    model.selection("FACE", "Partition_2_1_2/Modified_Face&Cylinder_1_1/Face_3")]
198 Shell_1 = model.addShell(Part_1_doc, Shell_1_objects)
199 checkNonCylindricalShell(Shell_1)
200
201 Shell_2 = model.addShell(Part_1_doc, [model.selection("FACE", "Extrusion_2_1_3/Generated_Face&Sketch_3/SketchCircle_2_2"), model.selection("FACE", "Extrusion_2_1_2/Generated_Face&Sketch_3/SketchCircle_3_2&weak_name_1")])
202 checkNonCylindricalShell(Shell_2)
203
204 model.end()