Salome HOME
Issue 2556: Functionality of inspection “WhatIs”
[modules/shaper.git] / src / GeomAPI / Test / TestSphere.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 checkArc(theDocument, theEdgeName, theCenter, theRadius):
31     # check edge (arc of circle)
32     anEdge = model.addEdge(theDocument, [model.selection("EDGE", theEdgeName)])
33     aShape = anEdge.result().resultSubShapePair()[0].shape()
34     assert(aShape.isEdge())
35     anArcEdge = aShape.edge()
36     assert(anArcEdge.isArc())
37     aCircle = anArcEdge.circle()
38     assert(aCircle.center().distance(theCenter) < TOLERANCE)
39     assert(math.fabs(aCircle.radius() - theRadius) < TOLERANCE)
40     theDocument.removeFeature(anEdge.feature())
41
42 def assertSphere(theSphere, theCenter, theRadius):
43     assert(theSphere is not None)
44     assert(theSphere.center().distance(theCenter) < TOLERANCE)
45     assert(math.fabs(theSphere.radius() - theRadius) < TOLERANCE)
46
47 def checkSphereFace(theDocument, theFaceName, theCenter, theRadius):
48     # check spherical face
49     aFace = model.addFace(theDocument, [model.selection("FACE", theFaceName)])
50     aShape = aFace.result().resultSubShapePair()[0].shape()
51     assert(aShape.isFace())
52     assertSphere(aShape.face().getSphere(), theCenter, theRadius)
53     theDocument.removeFeature(aFace.feature())
54
55 def checkSphereShell(theDocument, theFaceName, theCenter, theRadius):
56     # check spherical shell
57     aShell = model.addShell(theDocument, [model.selection("FACE", theFaceName)])
58     aShape = aShell.result().resultSubShapePair()[0].shape()
59     assert(aShape.isShell())
60     assertSphere(aShape.shell().getSphere(), theCenter, theRadius)
61     theDocument.removeFeature(aShell.feature())
62
63 def checkSphereAll(theDocument, theFeature, theFaceName, theCenter, theRadius):
64     # check solid
65     aShape = theFeature.result().resultSubShapePair()[0].shape()
66     assert(aShape.isSolid())
67     assertSphere(aShape.solid().getSphere(), theCenter, theRadius)
68
69     checkSphereShell(theDocument, theFaceName, theCenter, theRadius)
70     checkSphereFace(theDocument, theFaceName, theCenter, theRadius)
71     checkArc(theDocument, theFaceName, theCenter, theRadius)
72
73
74 model.begin()
75 partSet = model.moduleDocument()
76 Part_1 = model.addPart(partSet)
77 Part_1_doc = Part_1.document()
78 ParamR = model.addParameter(Part_1_doc, "R", "50")
79 ParamShift = model.addParameter(Part_1_doc, "Shift", "30")
80 ParamAngle = model.addParameter(Part_1_doc, "Angle", "30")
81 ParamAperture = model.addParameter(Part_1_doc, "Aperture", "360")
82 Sketch_1 = model.addSketch(Part_1_doc, model.defaultPlane("XOY"))
83 SketchArc_1 = Sketch_1.addArc(0, 0, 0, -50, 0, 50, False)
84 SketchArc_1.results()[1].setColor(225, 0, 0)
85 SketchLine_1 = Sketch_1.addLine(0, 50, 0, -50)
86 SketchConstraintCoincidence_1 = Sketch_1.setCoincident(SketchArc_1.endPoint(), SketchLine_1.startPoint())
87 SketchConstraintCoincidence_2 = Sketch_1.setCoincident(SketchArc_1.startPoint(), SketchLine_1.endPoint())
88 SketchProjection_1 = Sketch_1.addProjection(model.selection("VERTEX", "PartSet/Origin"), False)
89 SketchPoint_1 = SketchProjection_1.createdFeature()
90 SketchConstraintCoincidence_3 = Sketch_1.setCoincident(SketchAPI_Point(SketchPoint_1).coordinates(), SketchArc_1.center())
91 SketchConstraintCoincidence_4 = Sketch_1.setCoincident(SketchArc_1.center(), SketchLine_1.result())
92 SketchConstraintVertical_1 = Sketch_1.setVertical(SketchLine_1.result())
93 SketchConstraintRadius_1 = Sketch_1.setRadius(SketchArc_1.results()[1], "R")
94 model.do()
95
96 # Test 1. Compose sphere
97 Revolution_1 = model.addRevolution(Part_1_doc, [model.selection("FACE", "Sketch_1/Face-SketchArc_1_2f-SketchLine_1f")], model.selection("EDGE", "Sketch_1/Edge-SketchLine_1"), "Aperture", 0)
98 aCenter = GeomAPI.GeomAPI_Pnt(0, 0, 0)
99 checkSphereAll(Part_1_doc, Revolution_1, "Revolution_1_1/Generated_Face_2", aCenter, ParamR.value())
100
101 # Test 2. Translate sphere
102 Translation_1 = model.addTranslation(Part_1_doc, [model.selection("SOLID", "Revolution_1_1")], model.selection("EDGE", "PartSet/OX"), "Shift")
103 aCenter.setX(aCenter.x() + ParamShift.value())
104 checkSphereAll(Part_1_doc, Translation_1, "Translation_1_1/Translated_Face_1", aCenter, ParamR.value())
105
106 # Test 3. Rotate sphere
107 Rotation_1 = model.addRotation(Part_1_doc, [model.selection("SOLID", "Translation_1_1")], model.selection("EDGE", "PartSet/OY"), "Angle")
108 anAngle = ParamAngle.value() * math.pi / 180.0
109 aCenter.setX(ParamShift.value() * math.cos(anAngle))
110 aCenter.setZ(-ParamShift.value() * math.sin(anAngle))
111 checkSphereAll(Part_1_doc, Rotation_1, "Rotation_1_1/Rotated_Face_1", aCenter, ParamR.value())
112
113 # Test 4. Check result by changing parameters
114 ParamR.setValue(100)
115 model.do()
116 checkSphereAll(Part_1_doc, Rotation_1, "Rotation_1_1/Rotated_Face_1", aCenter, ParamR.value())
117
118 ParamAperture.setValue(270)
119 model.do()
120 checkSphereFace(Part_1_doc, "Rotation_1_1/Rotated_Face_3", aCenter, ParamR.value())
121 checkSphereShell(Part_1_doc, "Rotation_1_1/Rotated_Face_3", aCenter, ParamR.value())
122 checkArc(Part_1_doc, "Rotation_1_1/Rotated_Face_3&Rotation_1_1/Rotated_Face_1", aCenter, ParamR.value())
123 checkArc(Part_1_doc, "Rotation_1_1/Rotated_Face_3&Rotation_1_1/Rotated_Face_2", aCenter, ParamR.value())
124
125 model.end()