Salome HOME
Removed setColor for Sketch entities from test scripts.
[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 SketchLine_1 = Sketch_1.addLine(0, 50, 0, -50)
85 SketchConstraintCoincidence_1 = Sketch_1.setCoincident(SketchArc_1.endPoint(), SketchLine_1.startPoint())
86 SketchConstraintCoincidence_2 = Sketch_1.setCoincident(SketchArc_1.startPoint(), SketchLine_1.endPoint())
87 SketchProjection_1 = Sketch_1.addProjection(model.selection("VERTEX", "PartSet/Origin"), False)
88 SketchPoint_1 = SketchProjection_1.createdFeature()
89 SketchConstraintCoincidence_3 = Sketch_1.setCoincident(SketchAPI_Point(SketchPoint_1).coordinates(), SketchArc_1.center())
90 SketchConstraintCoincidence_4 = Sketch_1.setCoincident(SketchArc_1.center(), SketchLine_1.result())
91 SketchConstraintVertical_1 = Sketch_1.setVertical(SketchLine_1.result())
92 SketchConstraintRadius_1 = Sketch_1.setRadius(SketchArc_1.results()[1], "R")
93 model.do()
94
95 # Test 1. Compose sphere
96 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)
97 aCenter = GeomAPI.GeomAPI_Pnt(0, 0, 0)
98 checkSphereAll(Part_1_doc, Revolution_1, "Revolution_1_1/Generated_Face_2", aCenter, ParamR.value())
99
100 # Test 2. Translate sphere
101 Translation_1 = model.addTranslation(Part_1_doc, [model.selection("SOLID", "Revolution_1_1")], model.selection("EDGE", "PartSet/OX"), "Shift")
102 aCenter.setX(aCenter.x() + ParamShift.value())
103 checkSphereAll(Part_1_doc, Translation_1, "Translation_1_1/Translated_Face_1", aCenter, ParamR.value())
104
105 # Test 3. Rotate sphere
106 Rotation_1 = model.addRotation(Part_1_doc, [model.selection("SOLID", "Translation_1_1")], model.selection("EDGE", "PartSet/OY"), "Angle")
107 anAngle = ParamAngle.value() * math.pi / 180.0
108 aCenter.setX(ParamShift.value() * math.cos(anAngle))
109 aCenter.setZ(-ParamShift.value() * math.sin(anAngle))
110 checkSphereAll(Part_1_doc, Rotation_1, "Rotation_1_1/Rotated_Face_1", aCenter, ParamR.value())
111
112 # Test 4. Check result by changing parameters
113 ParamR.setValue(100)
114 model.do()
115 checkSphereAll(Part_1_doc, Rotation_1, "Rotation_1_1/Rotated_Face_1", aCenter, ParamR.value())
116
117 ParamAperture.setValue(270)
118 model.do()
119 checkSphereFace(Part_1_doc, "Rotation_1_1/Rotated_Face_3", aCenter, ParamR.value())
120 checkSphereShell(Part_1_doc, "Rotation_1_1/Rotated_Face_3", aCenter, ParamR.value())
121 checkArc(Part_1_doc, "Rotation_1_1/Rotated_Face_3&Rotation_1_1/Rotated_Face_1", aCenter, ParamR.value())
122 checkArc(Part_1_doc, "Rotation_1_1/Rotated_Face_3&Rotation_1_1/Rotated_Face_2", aCenter, ParamR.value())
123
124 model.end()