Salome HOME
Avoid crash if there is no parent of selection attribute.
[modules/shaper.git] / src / GeomAPI / Test / TestCone.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 assertCircle(theEdge, theCenter, theRadius):
31     assert(theEdge.isCircle())
32     aCircle = theEdge.circle()
33     assert(aCircle.center().distance(theCenter) < TOLERANCE), "({}, {}, {}) != expected ({}, {}, {})".format(aCircle.center().x(), aCircle.center().y(), aCircle.center().z(), theCenter.x(), theCenter.y(), theCenter.z())
34     assert(math.fabs(aCircle.radius() - theRadius) < TOLERANCE), "Radius {} != {}".format(aCircle.radius(), theRadius)
35
36 def checkCircleEdge(theDocument, theEdgeName, theCenter, theRadius):
37     anEdge = model.addEdge(theDocument, [model.selection("EDGE", theEdgeName)])
38     aShape = anEdge.result().resultSubShapePair()[0].shape()
39     assert(aShape.isEdge())
40     assertCircle(aShape.edge(), theCenter, theRadius)
41     theDocument.removeFeature(anEdge.feature())
42
43 def checkCircleFace(theDocument, theFaceName, theCenter, theRadius):
44     aFaceFeature = model.addFace(theDocument, [model.selection("FACE", theFaceName)])
45     aShape = aFaceFeature.result().resultSubShapePair()[0].shape()
46     assert(aShape.isFace())
47     aFace = aShape.face();
48     aSubs = aFace.subShapes(GeomAPI.GeomAPI_Shape.EDGE)
49     assert(aSubs.size() == 1)
50     assertCircle(aSubs[0].edge(), theCenter, theRadius)
51     theDocument.removeFeature(aFaceFeature.feature())
52
53 def assertEllipse(theEdge, theFirstFocus, theSecondFocus, theMajorRadius, theMinorRadius):
54     assert(theEdge.isEllipse())
55     anEllipse = theEdge.ellipse()
56     assert(anEllipse.firstFocus().distance(theFirstFocus) < TOLERANCE), "({}, {}, {}) != expected ({}, {}, {})".format(anEllipse.firstFocus().x(), anEllipse.firstFocus().y(), anEllipse.firstFocus().z(), theFirstFocus.x(), theFirstFocus.y(), theFirstFocus.z())
57     assert(anEllipse.secondFocus().distance(theSecondFocus) < TOLERANCE), "({}, {}, {}) != expected ({}, {}, {})".format(anEllipse.secondFocus().x(), anEllipse.secondFocus().y(), anEllipse.secondFocus().z(), theSecondFocus.x(), theSecondFocus.y(), theSecondFocus.z())
58     assert(math.fabs(anEllipse.majorRadius() - theMajorRadius) < TOLERANCE), "Major radius {} != {}".format(anEllipse.majorRadius(), theMajorRadius)
59     assert(math.fabs(anEllipse.minorRadius() - theMinorRadius) < TOLERANCE), "Minor radius {} != {}".format(anEllipse.minorRadius(), theMinorRadius)
60
61 def checkEllipseEdge(theDocument, theEdgeName, theFirstFocus, theSecondFocus, theMajorRadius, theMinorRadius):
62     anEdge = model.addEdge(theDocument, [model.selection("EDGE", theEdgeName)])
63     aShape = anEdge.result().resultSubShapePair()[0].shape()
64     assert(aShape.isEdge())
65     assertEllipse(aShape.edge(), theFirstFocus, theSecondFocus, theMajorRadius, theMinorRadius)
66     theDocument.removeFeature(anEdge.feature())
67
68 def checkEllipseFace(theDocument, theFaceName, theFirstFocus, theSecondFocus, theMajorRadius, theMinorRadius):
69     aFaceFeature = model.addFace(theDocument, [model.selection("FACE", theFaceName)])
70     aShape = aFaceFeature.result().resultSubShapePair()[0].shape()
71     assert(aShape.isFace())
72     aFace = aShape.face();
73     aSubs = aFace.subShapes(GeomAPI.GeomAPI_Shape.EDGE)
74     assert(aSubs.size() == 1)
75     assertEllipse(aSubs[0].edge(), theFirstFocus, theSecondFocus, theMajorRadius, theMinorRadius)
76     theDocument.removeFeature(aFaceFeature.feature())
77
78 def assertCone(theCone, theApex, theAxis, theSemiAngle, theRadius1, theRadius2, theHeight):
79     assert(theCone is not None)
80     anApex = theCone.apex()
81     anAxis = theCone.axis()
82     assert(anApex.distance(theApex) < TOLERANCE), "({}, {}, {}) != expected ({}, {}, {})".format(anApex.x(), anApex.y(), anApex.z(), theApex.x(), theApex.y(), theApex.z())
83     assert(anAxis.isParallel(theAxis, TOLERANCE)), "dir({}, {}, {}) is not parallel to dir({}, {}, {})".format(anAxis.x(), anAxis.y(), anAxis.z(), theAxis.x(), theAxis.y(), theAxis.z())
84     assert(math.fabs(theCone.semiAngle() - theSemiAngle) < TOLERANCE), "SemiAngle {} != {}".format(theCone.semiAngle(), theSemiAngle)
85     assert(math.fabs(theCone.radius1() - theRadius1) < TOLERANCE), "Radius1 {} != {}".format(theCone.radius1(), theRadius1)
86     assert(math.fabs(theCone.radius2() - theRadius2) < TOLERANCE), "Radius2 {} != {}".format(theCone.radius2(), theRadius2)
87     assert(math.fabs(theCone.height() - theHeight) < TOLERANCE), "Height {} != {}".format(theCone.height(), theHeight)
88
89 def checkConeFace(theDocument, theFaceName, theApex, theAxis, theSemiAngle, theRadius1, theRadius2, theHeight):
90     # check conical face
91     aFace = model.addFace(theDocument, [model.selection("FACE", theFaceName)])
92     aShape = aFace.result().resultSubShapePair()[0].shape()
93     assert(aShape.isFace())
94     assertCone(aShape.face().getCone(), theApex, theAxis, theSemiAngle, theRadius1, theRadius2, theHeight)
95     theDocument.removeFeature(aFace.feature())
96
97 def checkConeShell(theDocument, theFaceNames, theApex, theAxis, theSemiAngle, theRadius1, theRadius2, theHeight):
98     # check conical shell
99     aSelection = []
100     for name in theFaceNames:
101         aSelection.append(model.selection("FACE", name))
102     aShell = model.addShell(theDocument, aSelection)
103     aShape = aShell.result().resultSubShapePair()[0].shape()
104     assert(aShape.isShell())
105     assertCone(aShape.shell().getCone(), theApex, theAxis, theSemiAngle, theRadius1, theRadius2, theHeight)
106     theDocument.removeFeature(aShell.feature())
107
108 def checkConeAll(theDocument, theFeature, theFaceName, theApex, theAxis, theSemiAngle, theRadius1, theRadius2, theHeight):
109     # check solid
110     aShape = theFeature.result().resultSubShapePair()[0].shape()
111     assert(aShape.isSolid())
112     assertCone(aShape.solid().getCone(), theApex, theAxis, theSemiAngle, theRadius1, theRadius2, theHeight)
113
114     checkConeShell(theDocument, [theFaceName], theApex, theAxis, theSemiAngle, theRadius1, theRadius2, theHeight)
115     checkConeFace(theDocument, theFaceName, theApex, theAxis, theSemiAngle, theRadius1, theRadius2, theHeight)
116
117 def checkSegment(theDocument, theEdgeName, theStartPoint, theEndPoint):
118     anEdgeFeature = model.addEdge(theDocument, [model.selection("EDGE", theEdgeName)])
119     aShape = anEdgeFeature.result().resultSubShapePair()[0].shape()
120     assert(aShape.isEdge())
121     anEdge = aShape.edge()
122     assert(anEdge.isLine())
123     assert(anEdge.firstPoint().distance(theStartPoint) < TOLERANCE)
124     assert(anEdge.lastPoint().distance(theEndPoint) < TOLERANCE)
125     theDocument.removeFeature(anEdgeFeature.feature())
126
127 def checkVertex(theDocument, theVertexName, theCoordinates):
128     aVertex = model.addVertex(theDocument, [model.selection("VERTEX", theVertexName)])
129     aShape = aVertex.result().resultSubShapePair()[0].shape()
130     assert(aShape.isVertex())
131     assert(aShape.vertex().point().distance(theCoordinates) < TOLERANCE)
132     theDocument.removeFeature(aVertex.feature())
133
134 def semiAngle(theRadius1, theRadius2, theHeight):
135     return math.atan(math.fabs(theRadius1 - theRadius2) / theHeight)
136
137
138 model.begin()
139 partSet = model.moduleDocument()
140 Part_1 = model.addPart(partSet)
141 Part_1_doc = Part_1.document()
142 ParamR1 = model.addParameter(Part_1_doc, "R1", "50")
143 ParamR2 = model.addParameter(Part_1_doc, "R2", "5")
144 ParamH = model.addParameter(Part_1_doc, "H", "70")
145 ParamShift = model.addParameter(Part_1_doc, "Shift", "5")
146 ParamAngle = model.addParameter(Part_1_doc, "Angle", "60")
147 Cone_1 = model.addCone(Part_1_doc, model.selection("VERTEX", "PartSet/Origin"), model.selection("EDGE", "PartSet/OZ"), "R1", "R2", "H")
148 model.do()
149
150 # Test 1. Check cone
151 aSemiAngle = semiAngle(ParamR1.value(), ParamR2.value(), ParamH.value())
152 anApex = GeomAPI.GeomAPI_Pnt(0, 0, ParamR1.value() / math.tan(aSemiAngle))
153 anAxis = GeomAPI.GeomAPI_Dir(0, 0, 1)
154 checkConeAll(Part_1_doc, Cone_1, "Cone_1_1/Face_1", anApex, anAxis, aSemiAngle, ParamR2.value(), ParamR1.value(), ParamH.value())
155 checkCircleFace(Part_1_doc, "Cone_1_1/Face_2", GeomAPI.GeomAPI_Pnt(0, 0, ParamH.value()), ParamR2.value())
156 checkCircleEdge(Part_1_doc, "[Cone_1_1/Face_1][Cone_1_1/Face_2]", GeomAPI.GeomAPI_Pnt(0, 0, ParamH.value()), ParamR2.value())
157 checkCircleFace(Part_1_doc, "Cone_1_1/Face_3", GeomAPI.GeomAPI_Pnt(0, 0, 0), ParamR1.value())
158 checkCircleEdge(Part_1_doc, "[Cone_1_1/Face_1][Cone_1_1/Face_3]", GeomAPI.GeomAPI_Pnt(0, 0, 0), ParamR1.value())
159 checkSegment(Part_1_doc, "[Cone_1_1/Face_1][weak_name_3]", GeomAPI.GeomAPI_Pnt(ParamR1.value(), 0, 0), GeomAPI.GeomAPI_Pnt(ParamR2.value(), 0, ParamH.value()))
160
161 # Test 2. Update cone radii
162 ParamR1.setValue(0)
163 model.do()
164 aSemiAngle = semiAngle(ParamR1.value(), ParamR2.value(), ParamH.value())
165 anApex.setZ(0)
166 checkConeAll(Part_1_doc, Cone_1, "Cone_1_1/Face_1", anApex, anAxis, aSemiAngle, ParamR1.value(), ParamR2.value(), ParamH.value())
167 checkCircleFace(Part_1_doc, "Cone_1_1/Face_2", GeomAPI.GeomAPI_Pnt(0, 0, ParamH.value()), ParamR2.value())
168 checkCircleEdge(Part_1_doc, "[Cone_1_1/Face_1][Cone_1_1/Face_2]", GeomAPI.GeomAPI_Pnt(0, 0, ParamH.value()), ParamR2.value())
169 checkSegment(Part_1_doc, "[Cone_1_1/Face_1][weak_name_3]", GeomAPI.GeomAPI_Pnt(0, 0, 0), GeomAPI.GeomAPI_Pnt(ParamR2.value(), 0, ParamH.value()))
170 checkVertex(Part_1_doc, "Cone_1_1/Vertex_2", GeomAPI.GeomAPI_Pnt(0, 0, 0))
171
172 ParamR2.setValue(50)
173 ParamR1.setValue(10)
174 model.do()
175 aSemiAngle = semiAngle(ParamR1.value(), ParamR2.value(), ParamH.value())
176 anApex.setZ(-ParamR1.value() / math.tan(aSemiAngle))
177 checkConeAll(Part_1_doc, Cone_1, "Cone_1_1/Face_1", anApex, anAxis, aSemiAngle, ParamR1.value(), ParamR2.value(), ParamH.value())
178 checkCircleFace(Part_1_doc, "Cone_1_1/Face_2", GeomAPI.GeomAPI_Pnt(0, 0, ParamH.value()), ParamR2.value())
179 checkCircleEdge(Part_1_doc, "[Cone_1_1/Face_1][Cone_1_1/Face_2]", GeomAPI.GeomAPI_Pnt(0, 0, ParamH.value()), ParamR2.value())
180 checkCircleFace(Part_1_doc, "Cone_1_1/Face_3", GeomAPI.GeomAPI_Pnt(0, 0, 0), ParamR1.value())
181 checkCircleEdge(Part_1_doc, "[Cone_1_1/Face_1][Cone_1_1/Face_3]", GeomAPI.GeomAPI_Pnt(0, 0, 0), ParamR1.value())
182 checkSegment(Part_1_doc, "[Cone_1_1/Face_1][weak_name_3]", GeomAPI.GeomAPI_Pnt(ParamR1.value(), 0, 0), GeomAPI.GeomAPI_Pnt(ParamR2.value(), 0, ParamH.value()))
183
184 # Test 3. Translate cone
185 Translation_1 = model.addTranslation(Part_1_doc, [model.selection("SOLID", "Cone_1_1")], model.selection("EDGE", "PartSet/OX"), "Shift")
186 anApex.setX(anApex.x() + ParamShift.value())
187 checkConeAll(Part_1_doc, Translation_1, "Translation_1_1/MF:Translated&Cone_1_1/Face_1", anApex, anAxis, aSemiAngle, ParamR1.value(), ParamR2.value(), ParamH.value())
188 checkCircleFace(Part_1_doc, "Translation_1_1/MF:Translated&Cone_1_1/Face_2", GeomAPI.GeomAPI_Pnt(ParamShift.value(), 0, ParamH.value()), ParamR2.value())
189 checkCircleEdge(Part_1_doc, "[Translation_1_1/MF:Translated&Cone_1_1/Face_1][Translation_1_1/MF:Translated&Cone_1_1/Face_2]", GeomAPI.GeomAPI_Pnt(ParamShift.value(), 0, ParamH.value()), ParamR2.value())
190 checkCircleFace(Part_1_doc, "Translation_1_1/MF:Translated&Cone_1_1/Face_3", GeomAPI.GeomAPI_Pnt(ParamShift.value(), 0, 0), ParamR1.value())
191 checkCircleEdge(Part_1_doc, "[Translation_1_1/MF:Translated&Cone_1_1/Face_1][Translation_1_1/MF:Translated&Cone_1_1/Face_3]", GeomAPI.GeomAPI_Pnt(ParamShift.value(), 0, 0), ParamR1.value())
192 checkSegment(Part_1_doc, "[Translation_1_1/MF:Translated&Cone_1_1/Face_1][weak_name_3]", GeomAPI.GeomAPI_Pnt(ParamR1.value() + ParamShift.value(), 0, 0), GeomAPI.GeomAPI_Pnt(ParamR2.value() + ParamShift.value(), 0, ParamH.value()))
193
194 # Test 4. Rotate cone
195 Rotation_1 = model.addRotation(Part_1_doc, [model.selection("SOLID", "Translation_1_1")], model.selection("EDGE", "PartSet/OY"), "Angle")
196 anAngle = ParamAngle.value() * math.pi / 180.0
197 anAxis = GeomAPI.GeomAPI_Dir(math.sin(anAngle), 0, math.cos(anAngle))
198 x, z = anApex.x(), anApex.z()
199 anApex.setX(x * math.cos(anAngle) + z * math.sin(anAngle))
200 anApex.setZ(-x * math.sin(anAngle) + z * math.cos(anAngle))
201 checkConeAll(Part_1_doc, Rotation_1, "Rotation_1_1/MF:Rotated&Cone_1_1/Face_1", anApex, anAxis, aSemiAngle, ParamR1.value(), ParamR2.value(), ParamH.value())
202 aCenter = GeomAPI.GeomAPI_Pnt(ParamShift.value() * math.cos(anAngle) + ParamH.value() * math.sin(anAngle), 0, -ParamShift.value() * math.sin(anAngle) + ParamH.value() * math.cos(anAngle))
203 checkCircleFace(Part_1_doc, "Rotation_1_1/MF:Rotated&Cone_1_1/Face_2", aCenter, ParamR2.value())
204 checkCircleEdge(Part_1_doc, "[Rotation_1_1/MF:Rotated&Cone_1_1/Face_1][Rotation_1_1/MF:Rotated&Cone_1_1/Face_2]", aCenter, ParamR2.value())
205 aCenter = GeomAPI.GeomAPI_Pnt(ParamShift.value() * math.cos(anAngle), 0, -ParamShift.value() * math.sin(anAngle))
206 checkCircleFace(Part_1_doc, "Rotation_1_1/MF:Rotated&Cone_1_1/Face_3", aCenter, ParamR1.value())
207 checkCircleEdge(Part_1_doc, "[Rotation_1_1/MF:Rotated&Cone_1_1/Face_1][Rotation_1_1/MF:Rotated&Cone_1_1/Face_3]", aCenter, ParamR1.value())
208
209 # Test 5. Split cone by plane and check conical shell and elliptic face
210 Plane_4 = model.addPlane(Part_1_doc, model.selection("FACE", "PartSet/YOZ"), 20, False)
211 Partition_1 = model.addPartition(Part_1_doc, [model.selection("SOLID", "Rotation_1_1"), model.selection("FACE", "Plane_1")])
212 checkConeShell(Part_1_doc, ["Partition_1_1_1/Modified_Face&Cone_1_1/Face_1", "Partition_1_1_2/Modified_Face&Cone_1_1/Face_1"], anApex, anAxis, aSemiAngle, ParamR1.value(), ParamR2.value(), ParamH.value())
213
214 aFirstFocus = GeomAPI.GeomAPI_Pnt(20, 0, 31.062397266842858)
215 aSecondFocus = GeomAPI.GeomAPI_Pnt(20, 0, -1.0935246846933797)
216 aMajorRadius = 27.91915871311068
217 aMinorRadius = 22.824955511666207
218 checkEllipseFace(Part_1_doc, "_weak_name_1_Partition_1_1_2", aFirstFocus, aSecondFocus, aMajorRadius, aMinorRadius)
219 checkEllipseEdge(Part_1_doc, "Partition_1_1_1/Generated_Edge&Cone_1_1/Face_1", aFirstFocus, aSecondFocus, aMajorRadius, aMinorRadius)
220
221 model.end()