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