]> SALOME platform Git repositories - modules/shaper.git/blob - src/GeomAPI/Test/TestSphere.py
Salome HOME
530b22c55af866a62049396b3bbd1db05a31b6cd
[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 def checkNonSphereShell(theFeature):
76     aShape = theFeature.result().resultSubShapePair()[0].shape()
77     assert(aShape.isShell())
78     assert(aShape.shell().getSphere() is None)
79
80
81 model.begin()
82 partSet = model.moduleDocument()
83 Part_1 = model.addPart(partSet)
84 Part_1_doc = Part_1.document()
85 ParamR = model.addParameter(Part_1_doc, "R", "50")
86 ParamShift = model.addParameter(Part_1_doc, "Shift", "30")
87 ParamAngle = model.addParameter(Part_1_doc, "Angle", "30")
88 ParamAperture = model.addParameter(Part_1_doc, "Aperture", "360")
89 Sketch_1 = model.addSketch(Part_1_doc, model.defaultPlane("XOY"))
90 SketchArc_1 = Sketch_1.addArc(0, 0, 0, -50, 0, 50, False)
91 SketchLine_1 = Sketch_1.addLine(0, 50, 0, -50)
92 SketchConstraintCoincidence_1 = Sketch_1.setCoincident(SketchArc_1.endPoint(), SketchLine_1.startPoint())
93 SketchConstraintCoincidence_2 = Sketch_1.setCoincident(SketchArc_1.startPoint(), SketchLine_1.endPoint())
94 SketchProjection_1 = Sketch_1.addProjection(model.selection("VERTEX", "PartSet/Origin"), False)
95 SketchPoint_1 = SketchProjection_1.createdFeature()
96 SketchConstraintCoincidence_3 = Sketch_1.setCoincident(SketchAPI_Point(SketchPoint_1).coordinates(), SketchArc_1.center())
97 SketchConstraintCoincidence_4 = Sketch_1.setCoincident(SketchArc_1.center(), SketchLine_1.result())
98 SketchConstraintVertical_1 = Sketch_1.setVertical(SketchLine_1.result())
99 SketchConstraintRadius_1 = Sketch_1.setRadius(SketchArc_1.results()[1], "R")
100 model.do()
101
102 # Test 1. Compose sphere
103 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)
104 aCenter = GeomAPI.GeomAPI_Pnt(0, 0, 0)
105 checkSphereAll(Part_1_doc, Revolution_1, "Revolution_1_1/Generated_Face&Sketch_1/SketchArc_1_2", aCenter, ParamR.value())
106
107 # Test 2. Translate sphere
108 Translation_1 = model.addTranslation(Part_1_doc, [model.selection("SOLID", "Revolution_1_1")], model.selection("EDGE", "PartSet/OX"), "Shift")
109 aCenter.setX(aCenter.x() + ParamShift.value())
110 checkSphereAll(Part_1_doc, Translation_1, "Translation_1_1/MF:Translated&Sketch_1/SketchArc_1_2", aCenter, ParamR.value())
111
112 # Test 3. Rotate sphere
113 Rotation_1 = model.addRotation(Part_1_doc, [model.selection("SOLID", "Translation_1_1")], model.selection("EDGE", "PartSet/OY"), "Angle")
114 anAngle = ParamAngle.value() * math.pi / 180.0
115 aCenter.setX(ParamShift.value() * math.cos(anAngle))
116 aCenter.setZ(-ParamShift.value() * math.sin(anAngle))
117 checkSphereAll(Part_1_doc, Rotation_1, "Rotation_1_1/MF:Rotated&Sketch_1/SketchArc_1_2", aCenter, ParamR.value())
118
119 # Test 4. Check result by changing parameters
120 ParamR.setValue(100)
121 model.do()
122 checkSphereAll(Part_1_doc, Rotation_1, "Rotation_1_1/MF:Rotated&Sketch_1/SketchArc_1_2", aCenter, ParamR.value())
123
124 ParamAperture.setValue(270)
125 model.do()
126 checkSphereFace(Part_1_doc, "Rotation_1_1/MF:Rotated&Sketch_1/SketchArc_1_2", aCenter, ParamR.value())
127 checkSphereShell(Part_1_doc, "Rotation_1_1/MF:Rotated&Sketch_1/SketchArc_1_2", aCenter, ParamR.value())
128 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())
129 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())
130
131 # Test 5. Sheck non-spherical shell
132 Shell_1 = model.addShell(Part_1_doc, [model.selection("FACE", "Rotation_1_1/MF:Rotated&Revolution_1_1/To_Face"), model.selection("FACE", "Rotation_1_1/MF:Rotated&Revolution_1_1/From_Face")])
133 checkNonSphereShell(Shell_1)
134
135 Sketch_2 = model.addSketch(Part_1_doc, model.defaultPlane("XOZ"))
136 SketchLine_2 = Sketch_2.addLine(18.12152721893265, 20.53645178481853, 73.15172297255518, 20.53645178481853)
137 SketchConstraintHorizontal_1 = Sketch_2.setHorizontal(SketchLine_2.result())
138 SketchArc_2 = Sketch_2.addArc(60.28852631862447, 20.53645178481853, 73.15172297255518, 20.53645178481853, 58.16589238004093, 33.22330534748203, False)
139 SketchConstraintCoincidence_5 = Sketch_2.setCoincident(SketchLine_2.result(), SketchArc_2.center())
140 SketchConstraintCoincidence_6 = Sketch_2.setCoincident(SketchLine_2.endPoint(), SketchArc_2.startPoint())
141 SketchArc_3 = Sketch_2.addArc(40.15343392262997, 20.53645178481853, 18.12152721893265, 20.53645178481853, 58.16589238004093, 33.22330534748203, True)
142 SketchConstraintCoincidence_7 = Sketch_2.setCoincident(SketchLine_2.result(), SketchArc_3.center())
143 SketchConstraintCoincidence_8 = Sketch_2.setCoincident(SketchLine_2.startPoint(), SketchArc_3.startPoint())
144 SketchConstraintCoincidence_9 = Sketch_2.setCoincident(SketchArc_2.endPoint(), SketchArc_3.endPoint())
145 model.do()
146 Revolution_2 = model.addRevolution(Part_1_doc, [model.selection("FACE", "Sketch_2/Face-SketchLine_2r-SketchArc_2_2f-SketchArc_3_2f")], model.selection("EDGE", "Sketch_2/SketchLine_2"), 360, 0)
147 Shell_2 = model.addShell(Part_1_doc, [model.selection("FACE", "Revolution_2_1/Generated_Face&Sketch_2/SketchArc_3_2"), model.selection("FACE", "Revolution_2_1/Generated_Face&Sketch_2/SketchArc_2_2")])
148 checkNonSphereShell(Shell_2)
149
150 model.end()