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