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