]> SALOME platform Git repositories - modules/shaper.git/blob - src/GeomAPI/Test/TestTorus.py
Salome HOME
[Code coverage GeomAPI]: Improve coverage of primitives
[modules/shaper.git] / src / GeomAPI / Test / TestTorus.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 assertTorus(theTorus, theCenter, theAxis, theMajorRadius, theMinorRadius):
31     assert(theTorus is not None)
32     aCenter = theTorus.center()
33     aDir = theTorus.direction()
34     assert(aCenter.distance(theCenter) < TOLERANCE), "({}, {}, {}) != expected ({}, {}, {})".format(aCenter.x(), aCenter.y(), aCenter.z(), theCenter.x(), theCenter.y(), theCenter.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(theTorus.majorRadius() - theMajorRadius) < TOLERANCE), "Major radius {} != {}".format(theTorus.majorRadius(), theMajorRadius)
37     assert(math.fabs(theTorus.minorRadius() - theMinorRadius) < TOLERANCE), "Minor radius {} != {}".format(theTorus.minorRadius(), theMinorRadius)
38
39 def checkTorusFace(theDocument, theFaceName, theCenter, theAxis, theMajorRadius, theMinorRadius):
40     aFace = model.addFace(theDocument, [model.selection("FACE", theFaceName)])
41     aShape = aFace.result().resultSubShapePair()[0].shape()
42     assert(aShape.isFace())
43     assertTorus(aShape.face().getTorus(), theCenter, theAxis, theMajorRadius, theMinorRadius)
44     theDocument.removeFeature(aFace.feature())
45
46 def checkTorusShell(theDocument, theFaceNames, theCenter, theAxis, theMajorRadius, theMinorRadius):
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     assertTorus(aShape.shell().getTorus(), theCenter, theAxis, theMajorRadius, theMinorRadius)
54     theDocument.removeFeature(aShell.feature())
55
56 def checkTorusAll(theDocument, theFeature, theFaceName, theCenter, theAxis, theMajorRadius, theMinorRadius):
57     aShape = theFeature.result().resultSubShapePair()[0].shape()
58     assert(aShape.isSolid())
59     assertTorus(aShape.solid().getTorus(), theCenter, theAxis, theMajorRadius, theMinorRadius)
60
61     checkTorusShell(theDocument, [theFaceName], theCenter, theAxis, theMajorRadius, theMinorRadius)
62     checkTorusFace(theDocument, theFaceName, theCenter, theAxis, theMajorRadius, theMinorRadius)
63
64 def checkNonTorusShell(theFeature):
65     aShape = theFeature.result().resultSubShapePair()[0].shape()
66     assert(aShape.isShell())
67     assert(aShape.shell().getTorus() 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 ParamRMax = model.addParameter(Part_1_doc, "RMax", "15")
75 ParamRMin = model.addParameter(Part_1_doc, "RMin", "5")
76 ParamAngle = model.addParameter(Part_1_doc, "Angle", "30")
77 Torus_1 = model.addTorus(Part_1_doc, model.selection("VERTEX", "PartSet/Origin"), model.selection("EDGE", "PartSet/OZ"), "RMax", "RMin")
78 model.do()
79
80 # Test 1. Check torus
81 aCenter = GeomAPI.GeomAPI_Pnt(0, 0, 0)
82 anAxis = GeomAPI.GeomAPI_Dir(0, 0, 1)
83 checkTorusAll(Part_1_doc, Torus_1, "Torus_1_1/Face_1", aCenter, anAxis, ParamRMax.value(), ParamRMin.value())
84
85 # Test 2. Rotate torus
86 Rotation_1 = model.addRotation(Part_1_doc, [model.selection("SOLID", "Torus_1_1")], model.selection("EDGE", "PartSet/OX"), "Angle")
87 anAngle = ParamAngle.value() * math.pi / 180.0
88 aCosAngle = math.cos(anAngle)
89 aSinAngle = math.sin(anAngle)
90 aCenter = GeomAPI.GeomAPI_Pnt(0, aCenter.y() * aCosAngle - aCenter.z() * aSinAngle, aCenter.y() * aSinAngle + aCenter.z() * aCosAngle)
91 anAxis = GeomAPI.GeomAPI_Dir(0, anAxis.y() * aCosAngle - anAxis.z() * aSinAngle, anAxis.y() * aSinAngle + anAxis.z() * aCosAngle)
92 checkTorusAll(Part_1_doc, Rotation_1, "Rotation_1_1/MF:Rotated&Torus_1_1/Face_1", aCenter, anAxis, ParamRMax.value(), ParamRMin.value())
93
94 # Test 3. Split torus and compose a shell
95 Partition_1_objects = [model.selection("SOLID", "Rotation_1_1"), model.selection("FACE", "PartSet/YOZ"), model.selection("FACE", "PartSet/XOZ"), model.selection("FACE", "PartSet/XOY")]
96 Partition_1 = model.addPartition(Part_1_doc, Partition_1_objects)
97 Shell_1_objects = ["Partition_1_1_6/Modified_Face&Torus_1_1/Face_1",
98                    "Partition_1_1_7/Modified_Face&Torus_1_1/Face_1&weak_name_2",
99                    "Partition_1_1_7/Modified_Face&Torus_1_1/Face_1&weak_name_1",
100                    "Partition_1_1_5/Modified_Face&Torus_1_1/Face_1"]
101 checkTorusShell(Part_1_doc, Shell_1_objects, aCenter, anAxis, ParamRMax.value(), ParamRMin.value())
102
103 # Test 4. Check non-torus shell
104 Shell_1 = model.addShell(Part_1_doc, [model.selection("FACE", "Partition_1_1_1/Modified_Face&Torus_1_1/Face_1"), model.selection("FACE", "Partition_1_1_1/Modified_Face&PartSet/YOZ/YOZ")])
105 checkNonTorusShell(Shell_1)
106
107 Symmetry_1 = model.addSymmetry(Part_1_doc, [model.selection("SHELL", "Shell_1_1")], model.selection("FACE", "PartSet/XOY"), True)
108 Shell_2 = model.addShell(Part_1_doc, [model.selection("FACE", "_weak_name_1_Symmetry_1_1_1"), model.selection("FACE", "_weak_name_1_Symmetry_1_1_2")])
109 checkNonTorusShell(Shell_2)
110
111 model.end()