Salome HOME
Issue 2556: Functionality of inspection “WhatIs”
[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
65 model.begin()
66 partSet = model.moduleDocument()
67 Part_1 = model.addPart(partSet)
68 Part_1_doc = Part_1.document()
69 ParamRMax = model.addParameter(Part_1_doc, "RMax", "15")
70 ParamRMin = model.addParameter(Part_1_doc, "RMin", "5")
71 ParamAngle = model.addParameter(Part_1_doc, "Angle", "30")
72 Torus_1 = model.addTorus(Part_1_doc, model.selection("VERTEX", "PartSet/Origin"), model.selection("EDGE", "PartSet/OZ"), "RMax", "RMin")
73 model.do()
74
75 # Test 1. Check torus
76 aCenter = GeomAPI.GeomAPI_Pnt(0, 0, 0)
77 anAxis = GeomAPI.GeomAPI_Dir(0, 0, 1)
78 checkTorusAll(Part_1_doc, Torus_1, "Torus_1_1/Face_1", aCenter, anAxis, ParamRMax.value(), ParamRMin.value())
79
80 # Test 2. Rotate torus
81 Rotation_1 = model.addRotation(Part_1_doc, [model.selection("SOLID", "Torus_1_1")], model.selection("EDGE", "PartSet/OX"), "Angle")
82 anAngle = ParamAngle.value() * math.pi / 180.0
83 aCosAngle = math.cos(anAngle)
84 aSinAngle = math.sin(anAngle)
85 aCenter = GeomAPI.GeomAPI_Pnt(0, aCenter.y() * aCosAngle - aCenter.z() * aSinAngle, aCenter.y() * aSinAngle + aCenter.z() * aCosAngle)
86 anAxis = GeomAPI.GeomAPI_Dir(0, anAxis.y() * aCosAngle - anAxis.z() * aSinAngle, anAxis.y() * aSinAngle + anAxis.z() * aCosAngle)
87 checkTorusAll(Part_1_doc, Rotation_1, "Rotation_1_1/Rotated_Face_1", aCenter, anAxis, ParamRMax.value(), ParamRMin.value())
88
89 # Test 3. Split torus and compose a shell
90 Partition_1_objects = [model.selection("SOLID", "Rotation_1_1"), model.selection("FACE", "PartSet/YOZ"), model.selection("FACE", "PartSet/XOZ"), model.selection("FACE", "PartSet/XOY")]
91 Partition_1 = model.addPartition(Part_1_doc, Partition_1_objects)
92 Shell_1_objects = ["Partition_1_1_6/Modified_Face_4_4", "Partition_1_1_7/Modified_Face_4_3", "Partition_1_1_7/Modified_Face_1_divided_2_1", "Partition_1_1_5/Modified_Face_4_4"]
93 checkTorusShell(Part_1_doc, Shell_1_objects, aCenter, anAxis, ParamRMax.value(), ParamRMin.value())
94
95 model.end()