Salome HOME
[Code coverage GeomAPI]: Improve coverage quality
[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 checkCylinderAll(theDocument, theFeature, theFaceName, theLocation, theAxis, theRadius, theHeight):
58     aShape = theFeature.result().resultSubShapePair()[0].shape()
59     assert(aShape.isSolid())
60     assertCylinder(aShape.solid().getCylinder(), theLocation, theAxis, theRadius, theHeight)
61
62     checkCylinderShell(theDocument, [theFaceName], theLocation, theAxis, theRadius, theHeight)
63     checkCylinderFace(theDocument, theFaceName, theLocation, theAxis, theRadius, theHeight)
64
65 def checkNonCylinder(theFeature):
66     aShape = theFeature.result().resultSubShapePair()[0].shape()
67     assert(aShape.isSolid())
68     assert(aShape.solid().getCylinder() is None)
69
70
71 model.begin()
72 partSet = model.moduleDocument()
73 Part_1 = model.addPart(partSet)
74 Part_1_doc = Part_1.document()
75 ParamH = model.addParameter(Part_1_doc, "H", "10")
76 ParamR = model.addParameter(Part_1_doc, "R", "10")
77 ParamAngle = model.addParameter(Part_1_doc, "Angle", "30")
78 Cylinder_1 = model.addCylinder(Part_1_doc, model.selection("VERTEX", "PartSet/Origin"), model.selection("EDGE", "PartSet/OZ"), "2*R", "H")
79 Sketch_1 = model.addSketch(Part_1_doc, model.selection("FACE", "Cylinder_1_1/Face_2"))
80 SketchProjection_1 = Sketch_1.addProjection(model.selection("VERTEX", "[Cylinder_1_1/Face_1][Cylinder_1_1/Face_2]__cc"), False)
81 SketchPoint_1 = SketchProjection_1.createdFeature()
82 SketchCircle_1 = Sketch_1.addCircle(0, 0, 10)
83 SketchConstraintCoincidence_1 = Sketch_1.setCoincident(SketchPoint_1.result(), SketchCircle_1.center())
84 SketchConstraintRadius_1 = Sketch_1.setRadius(SketchCircle_1.results()[1], "R")
85 model.do()
86 Extrusion_1 = model.addExtrusion(Part_1_doc, [model.selection("FACE", "Sketch_1/Face-SketchCircle_1_2f")], model.selection(), "H", 0)
87 Sketch_2 = model.addSketch(Part_1_doc, model.standardPlane("XOZ"))
88 SketchLine_1 = Sketch_2.addLine(5, 20, 0, 20)
89 SketchLine_2 = Sketch_2.addLine(0, 20, 0, 30)
90 SketchLine_3 = Sketch_2.addLine(0, 30, 5, 30)
91 SketchLine_4 = Sketch_2.addLine(5, 30, 5, 20)
92 SketchConstraintCoincidence_2 = Sketch_2.setCoincident(SketchLine_4.endPoint(), SketchLine_1.startPoint())
93 SketchConstraintCoincidence_3 = Sketch_2.setCoincident(SketchLine_1.endPoint(), SketchLine_2.startPoint())
94 SketchConstraintCoincidence_4 = Sketch_2.setCoincident(SketchLine_2.endPoint(), SketchLine_3.startPoint())
95 SketchConstraintCoincidence_5 = Sketch_2.setCoincident(SketchLine_3.endPoint(), SketchLine_4.startPoint())
96 SketchConstraintHorizontal_1 = Sketch_2.setHorizontal(SketchLine_1.result())
97 SketchConstraintVertical_1 = Sketch_2.setVertical(SketchLine_2.result())
98 SketchConstraintHorizontal_2 = Sketch_2.setHorizontal(SketchLine_3.result())
99 SketchConstraintVertical_2 = Sketch_2.setVertical(SketchLine_4.result())
100 SketchConstraintLength_1 = Sketch_2.setLength(SketchLine_1.result(), "R/2")
101 SketchConstraintLength_2 = Sketch_2.setLength(SketchLine_2.result(), "H")
102 SketchIntersectionPoint_1 = Sketch_2.addIntersectionPoint(model.selection("EDGE", "[Extrusion_1_1/Generated_Face&Sketch_1/SketchCircle_1_2][Extrusion_1_1/To_Face]"), False)
103 [SketchPoint_2, SketchPoint_3] = SketchIntersectionPoint_1.intersectionPoints()
104 SketchConstraintCoincidence_6 = Sketch_2.setCoincident(SketchAPI_Point(SketchPoint_2).coordinates(), SketchLine_1.result())
105 SketchProjection_2 = Sketch_2.addProjection(model.selection("EDGE", "PartSet/OZ"), False)
106 SketchLine_5 = SketchProjection_2.createdFeature()
107 SketchConstraintCoincidence_7 = Sketch_2.setCoincident(SketchLine_2.endPoint(), SketchLine_5.result())
108 model.do()
109 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)
110 model.do()
111
112 # Test 1. Check cylinders
113 aLoc1 = GeomAPI.GeomAPI_Pnt(0, 0, 0)
114 aLoc2 = GeomAPI.GeomAPI_Pnt(0, 0, 2 * ParamH.value())
115 aLoc3 = GeomAPI.GeomAPI_Pnt(0, 0, 3 * ParamH.value())
116 anAxis = GeomAPI.GeomAPI_Dir(0, 0, 1)
117 checkCylinderAll(Part_1_doc, Cylinder_1, "Cylinder_1_1/Face_1", aLoc1, anAxis, 2 * ParamR.value(), ParamH.value())
118 checkCylinderAll(Part_1_doc, Extrusion_1, "Extrusion_1_1/Generated_Face&Sketch_1/SketchCircle_1_2", aLoc2, anAxis, ParamR.value(), ParamH.value())
119 checkNonCylinder(Revolution_1)
120 checkCylinderShell(Part_1_doc, ["Revolution_1_1/Generated_Face&Sketch_2/SketchLine_4"], aLoc3, anAxis, 0.5 * ParamR.value(), ParamH.value())
121 checkCylinderFace(Part_1_doc, "Revolution_1_1/Generated_Face&Sketch_2/SketchLine_4", aLoc3, anAxis, 0.5 * ParamR.value(), ParamH.value())
122
123 # Test 2. Rotate cylinders
124 Rotation_1 = model.addRotation(Part_1_doc, [model.selection("SOLID", "Cylinder_1_1")], model.selection("EDGE", "PartSet/OX"), "Angle")
125 Rotation_2 = model.addRotation(Part_1_doc, [model.selection("SOLID", "Extrusion_1_1")], model.selection("EDGE", "PartSet/OX"), "Angle")
126 Rotation_3 = model.addRotation(Part_1_doc, [model.selection("SOLID", "Revolution_1_1")], model.selection("EDGE", "PartSet/OX"), "Angle")
127
128 anAngle = ParamAngle.value() * math.pi / 180.0
129 aCosAngle = math.cos(anAngle)
130 aSinAngle = math.sin(anAngle)
131 anAxis = GeomAPI.GeomAPI_Dir(0, anAxis.y() * aCosAngle - anAxis.z() * aSinAngle, anAxis.y() * aSinAngle + anAxis.z() * aCosAngle)
132 aLoc1 = GeomAPI.GeomAPI_Pnt(0, aLoc1.y() * aCosAngle - aLoc1.z() * aSinAngle, aLoc1.y() * aSinAngle + aLoc1.z() * aCosAngle)
133 aLoc2 = GeomAPI.GeomAPI_Pnt(0, aLoc2.y() * aCosAngle - aLoc2.z() * aSinAngle, aLoc2.y() * aSinAngle + aLoc2.z() * aCosAngle)
134 aLoc3 = GeomAPI.GeomAPI_Pnt(0, aLoc3.y() * aCosAngle - aLoc3.z() * aSinAngle, aLoc3.y() * aSinAngle + aLoc3.z() * aCosAngle)
135 checkCylinderAll(Part_1_doc, Rotation_1, "Rotation_1_1/MF:Rotated&Cylinder_1_1/Face_1", aLoc1, anAxis, 2 * ParamR.value(), ParamH.value())
136 checkCylinderAll(Part_1_doc, Rotation_2, "Rotation_2_1/MF:Rotated&Sketch_1/SketchCircle_1_2", aLoc2, anAxis, ParamR.value(), ParamH.value())
137 checkNonCylinder(Rotation_3)
138 checkCylinderShell(Part_1_doc, ["Rotation_3_1/MF:Rotated&Sketch_2/SketchLine_4"], aLoc3, anAxis, 0.5 * ParamR.value(), ParamH.value())
139 checkCylinderFace(Part_1_doc, "Rotation_3_1/MF:Rotated&Sketch_2/SketchLine_4", aLoc3, anAxis, 0.5 * ParamR.value(), ParamH.value())
140
141 # Test 3. Split cylinder and compose a shell
142 Plane_4 = model.addPlane(Part_1_doc, model.selection("FACE", "PartSet/XOY"), "2.2*H", False)
143 Plane_5 = model.addPlane(Part_1_doc, model.selection("FACE", "PartSet/XOZ"), "H", False)
144 Partition_1_objects = [model.selection("SOLID", "Rotation_3_1"), model.selection("FACE", "Plane_1"), model.selection("FACE", "Plane_2")]
145 Partition_1 = model.addPartition(Part_1_doc, Partition_1_objects)
146
147 Shell_1_objects = ["Partition_1_1_1/Modified_Face&Sketch_2/SketchLine_4",
148                    "Partition_1_1_4/Modified_Face&Sketch_2/SketchLine_4",
149                    "(Partition_1_1_2/Modified_Face&Revolution_1_1/To_Face)(Partition_1_1_2/Modified_Face&Sketch_2/SketchLine_1)"]
150 checkCylinderShell(Part_1_doc, Shell_1_objects, aLoc3, anAxis, 0.5 * ParamR.value(), ParamH.value())
151
152 model.end()