Salome HOME
Merge commit 'refs/tags/V9_2_0^{}'
[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
72     anArcName = "[" + theFaceName + "][weak_name_3]"
73     checkArc(theDocument, anArcName, theCenter, theRadius)
74
75
76 model.begin()
77 partSet = model.moduleDocument()
78 Part_1 = model.addPart(partSet)
79 Part_1_doc = Part_1.document()
80 ParamR = model.addParameter(Part_1_doc, "R", "50")
81 ParamShift = model.addParameter(Part_1_doc, "Shift", "30")
82 ParamAngle = model.addParameter(Part_1_doc, "Angle", "30")
83 ParamAperture = model.addParameter(Part_1_doc, "Aperture", "360")
84 Sketch_1 = model.addSketch(Part_1_doc, model.defaultPlane("XOY"))
85 SketchArc_1 = Sketch_1.addArc(0, 0, 0, -50, 0, 50, False)
86 SketchLine_1 = Sketch_1.addLine(0, 50, 0, -50)
87 SketchConstraintCoincidence_1 = Sketch_1.setCoincident(SketchArc_1.endPoint(), SketchLine_1.startPoint())
88 SketchConstraintCoincidence_2 = Sketch_1.setCoincident(SketchArc_1.startPoint(), SketchLine_1.endPoint())
89 SketchProjection_1 = Sketch_1.addProjection(model.selection("VERTEX", "PartSet/Origin"), False)
90 SketchPoint_1 = SketchProjection_1.createdFeature()
91 SketchConstraintCoincidence_3 = Sketch_1.setCoincident(SketchAPI_Point(SketchPoint_1).coordinates(), SketchArc_1.center())
92 SketchConstraintCoincidence_4 = Sketch_1.setCoincident(SketchArc_1.center(), SketchLine_1.result())
93 SketchConstraintVertical_1 = Sketch_1.setVertical(SketchLine_1.result())
94 SketchConstraintRadius_1 = Sketch_1.setRadius(SketchArc_1.results()[1], "R")
95 model.do()
96
97 # Test 1. Compose sphere
98 Revolution_1 = model.addRevolution(Part_1_doc, [model.selection("FACE", "Sketch_1/Face-SketchArc_1_2r-SketchLine_1f")], model.selection("EDGE", "Sketch_1/SketchLine_1"), "Aperture", 0)
99 aCenter = GeomAPI.GeomAPI_Pnt(0, 0, 0)
100 checkSphereAll(Part_1_doc, Revolution_1, "Revolution_1_1/Generated_Face&Sketch_1/SketchArc_1_2", aCenter, ParamR.value())
101
102 # Test 2. Translate sphere
103 Translation_1 = model.addTranslation(Part_1_doc, [model.selection("SOLID", "Revolution_1_1")], model.selection("EDGE", "PartSet/OX"), "Shift")
104 aCenter.setX(aCenter.x() + ParamShift.value())
105 checkSphereAll(Part_1_doc, Translation_1, "Translation_1_1/MF:Translated&Sketch_1/SketchArc_1_2", aCenter, ParamR.value())
106
107 # Test 3. Rotate sphere
108 Rotation_1 = model.addRotation(Part_1_doc, [model.selection("SOLID", "Translation_1_1")], model.selection("EDGE", "PartSet/OY"), "Angle")
109 anAngle = ParamAngle.value() * math.pi / 180.0
110 aCenter.setX(ParamShift.value() * math.cos(anAngle))
111 aCenter.setZ(-ParamShift.value() * math.sin(anAngle))
112 checkSphereAll(Part_1_doc, Rotation_1, "Rotation_1_1/MF:Rotated&Sketch_1/SketchArc_1_2", aCenter, ParamR.value())
113
114 # Test 4. Check result by changing parameters
115 ParamR.setValue(100)
116 model.do()
117 checkSphereAll(Part_1_doc, Rotation_1, "Rotation_1_1/MF:Rotated&Sketch_1/SketchArc_1_2", aCenter, ParamR.value())
118
119 ParamAperture.setValue(270)
120 model.do()
121 checkSphereFace(Part_1_doc, "Rotation_1_1/MF:Rotated&Sketch_1/SketchArc_1_2", aCenter, ParamR.value())
122 checkSphereShell(Part_1_doc, "Rotation_1_1/MF:Rotated&Sketch_1/SketchArc_1_2", aCenter, ParamR.value())
123 checkArc(Part_1_doc, "[Rotation_1_1/MF:Rotated&Sketch_1/SketchArc_1_2][Rotation_1_1/MF:Rotated&Revolution_1_1/From_Face]", aCenter, ParamR.value())
124 checkArc(Part_1_doc, "[Rotation_1_1/MF:Rotated&Sketch_1/SketchArc_1_2][Rotation_1_1/MF:Rotated&Revolution_1_1/To_Face]", aCenter, ParamR.value())
125
126 model.end()