Salome HOME
Issue 2556: Functionality of inspection “WhatIs”
[modules/shaper.git] / src / GeomAPI / Test / TestBox.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 checkBox(theFeature, theCorner, theWidth, theDepth, theHeight):
31     aShape = theFeature.result().resultSubShapePair()[0].shape()
32     assert(aShape.isSolid())
33     aBox = aShape.solid().getParallelepiped()
34     assert(aBox is not None)
35     assert(aBox.isAxesAligned())
36     aCorner = aBox.corner()
37     assert(aCorner.distance(theCorner) < TOLERANCE), "({}, {}, {}) != expected ({}, {}, {})".format(aCorner.x(), aCorner.y(), aCorner.z(), theCorner.x(), theCorner.y(), theCorner.z())
38     assert(math.fabs(aBox.width() - theWidth) < TOLERANCE), "Width {} != {}".format(aBox.width(), theWidth)
39     assert(math.fabs(aBox.depth() - theDepth) < TOLERANCE), "Depth {} != {}".format(aBox.depth(), theDepth)
40     assert(math.fabs(aBox.height() - theHeight) < TOLERANCE), "Height {} != {}".format(aBox.height(), theHeight)
41
42 def assertParallelepiped(theBox, theCorner1, theCorner2):
43     assert(theBox is not None)
44     assert(theBox.isAxesAligned() == False)
45     aCorner1 = theBox.corner()
46     axes = theBox.axes()
47     dirX = axes.dirX()
48     dirY = axes.dirY()
49     dirZ = axes.normal()
50     aCorner2 = GeomAPI.GeomAPI_Pnt(aCorner1.x() + dirX.x() * theBox.width() + dirY.x() * theBox.depth() + dirZ.x() * theBox.height(),
51                                    aCorner1.y() + dirX.y() * theBox.width() + dirY.y() * theBox.depth() + dirZ.y() * theBox.height(),
52                                    aCorner1.z() + dirX.z() * theBox.width() + dirY.z() * theBox.depth() + dirZ.z() * theBox.height())
53     assert(aCorner1.distance(theCorner1) < TOLERANCE), "({}, {}, {}) != expected ({}, {}, {})".format(aCorner1.x(), aCorner1.y(), aCorner1.z(), theCorner1.x(), theCorner1.y(), theCorner1.z())
54     assert(aCorner2.distance(theCorner2) < TOLERANCE), "({}, {}, {}) != expected ({}, {}, {})".format(aCorner2.x(), aCorner2.y(), aCorner2.z(), theCorner2.x(), theCorner2.y(), theCorner2.z())
55
56 def checkRotatedBox(theFeature, theCorner1, theCorner2):
57     aShape = theFeature.result().resultSubShapePair()[0].shape()
58     assert(aShape.isSolid())
59     aBox = aShape.solid().getParallelepiped()
60     assertParallelepiped(aBox, theCorner1, theCorner2)
61
62 def checkShellRotatedBox(theDocument, theFaceNames, theCorner1, theCorner2):
63     aSelection = []
64     for name in theFaceNames:
65         aSelection.append(model.selection("FACE", name))
66     aShell = model.addShell(theDocument, aSelection)
67     aShape = aShell.result().resultSubShapePair()[0].shape()
68     assert(aShape.isShell())
69     assertParallelepiped(aShape.shell().getParallelepiped(), theCorner1, theCorner2)
70     theDocument.removeFeature(aShell.feature())
71
72 def checkShellNotBox(theDocument, theFaceNames):
73     aSelection = []
74     for name in theFaceNames:
75         aSelection.append(model.selection("FACE", name))
76     aShell = model.addShell(theDocument, aSelection)
77     aShape = aShell.result().resultSubShapePair()[0].shape()
78     assert(aShape.isShell())
79     assert(aShape.shell().getParallelepiped() is None)
80     theDocument.removeFeature(aShell.feature())
81
82
83 model.begin()
84 partSet = model.moduleDocument()
85 Part_1 = model.addPart(partSet)
86 Part_1_doc = Part_1.document()
87 ParamSize = model.addParameter(Part_1_doc, "BoxSize", "10")
88 ParamWidth = model.addParameter(Part_1_doc, "Width", "20")
89 ParamDepth = model.addParameter(Part_1_doc, "Depth", "10")
90 ParamHeight = model.addParameter(Part_1_doc, "Height", "25")
91 ParamAngle = model.addParameter(Part_1_doc, "Angle", "30")
92 Box_1 = model.addBox(Part_1_doc, "BoxSize", "BoxSize", "BoxSize")
93 Sketch_1 = model.addSketch(Part_1_doc, model.selection("FACE", "Box_1_1/Top"))
94 SketchProjection_1 = Sketch_1.addProjection(model.selection("VERTEX", "Box_1_1/Front&Box_1_1/Right&Box_1_1/Top"), False)
95 SketchPoint_1 = SketchProjection_1.createdFeature()
96 SketchLine_1 = Sketch_1.addLine(30, 10, 10, 10)
97 SketchLine_2 = Sketch_1.addLine(10, 10, 10, 20)
98 SketchLine_3 = Sketch_1.addLine(10, 20, 30, 20)
99 SketchLine_4 = Sketch_1.addLine(30, 20, 30, 10)
100 SketchConstraintCoincidence_1 = Sketch_1.setCoincident(SketchLine_1.endPoint(), SketchPoint_1.result())
101 SketchConstraintCoincidence_2 = Sketch_1.setCoincident(SketchLine_4.endPoint(), SketchLine_1.startPoint())
102 SketchConstraintCoincidence_3 = Sketch_1.setCoincident(SketchLine_1.endPoint(), SketchLine_2.startPoint())
103 SketchConstraintCoincidence_4 = Sketch_1.setCoincident(SketchLine_2.endPoint(), SketchLine_3.startPoint())
104 SketchConstraintCoincidence_5 = Sketch_1.setCoincident(SketchLine_3.endPoint(), SketchLine_4.startPoint())
105 SketchConstraintHorizontal_1 = Sketch_1.setHorizontal(SketchLine_1.result())
106 SketchConstraintVertical_1 = Sketch_1.setVertical(SketchLine_2.result())
107 SketchConstraintHorizontal_2 = Sketch_1.setHorizontal(SketchLine_3.result())
108 SketchConstraintVertical_2 = Sketch_1.setVertical(SketchLine_4.result())
109 SketchConstraintLength_1 = Sketch_1.setLength(SketchLine_1.result(), "Width")
110 SketchConstraintLength_2 = Sketch_1.setLength(SketchLine_2.result(), "Depth")
111 model.do()
112 Extrusion_1 = model.addExtrusion(Part_1_doc, [model.selection("FACE", "Sketch_1/Face-SketchLine_1r-SketchLine_2r-SketchLine_3r-SketchLine_4r")], model.selection(), "Height", 0)
113 model.do()
114
115 # Test 1. Check boxes
116 aCornerBox = GeomAPI.GeomAPI_Pnt(0, 0, 0)
117 checkBox(Box_1, aCornerBox, ParamSize.value(), ParamSize.value(), ParamSize.value())
118 aCornerPara = GeomAPI.GeomAPI_Pnt(ParamSize.value(), ParamSize.value(), ParamSize.value())
119 checkBox(Extrusion_1, aCornerPara, ParamWidth.value(), ParamDepth.value(), ParamHeight.value())
120
121 # Test 2. Rotate box to keep it still axes-aligned
122 Rotation_1 = model.addRotation(Part_1_doc, [model.selection("SOLID", "Extrusion_1_1")], model.selection("EDGE", "Extrusion_1_1/Generated_Face_4&Extrusion_1_1/Generated_Face_1"), 90)
123 aCornerPara.setX(aCornerPara.x() + ParamWidth.value() - ParamDepth.value())
124 aCornerPara.setY(aCornerPara.y() - ParamWidth.value())
125 checkBox(Rotation_1, aCornerPara, ParamDepth.value(), ParamWidth.value(), ParamHeight.value())
126
127 # Test 3. Rotate boxes
128 Axis_4 = model.addAxis(Part_1_doc, model.selection("VERTEX", "Box_1_1/Back&Box_1_1/Left&Box_1_1/Bottom"), model.selection("VERTEX", "Box_1_1/Front&Box_1_1/Right&Box_1_1/Top"))
129 Rotation_2 = model.addRotation(Part_1_doc, [model.selection("SOLID", "Box_1_1")], model.selection("EDGE", "Axis_1"), "Angle")
130 Rotation_3 = model.addRotation(Part_1_doc, [model.selection("SOLID", "Rotation_1_1")], model.selection("EDGE", "Axis_1"), "Angle")
131
132 aRotDir = GeomAPI.GeomAPI_Dir(1, 1, 1)
133 anAngle = ParamAngle.value() * math.pi / 180.0
134 aCosAngle = math.cos(anAngle)
135 aSinAngle = math.sin(anAngle)
136
137 aCornerBox = GeomAPI.GeomAPI_Pnt(0, ParamSize.value(), 0)
138 aCornerBox1 = GeomAPI.GeomAPI_Pnt(ParamSize.value(), 0, ParamSize.value())
139 aCornerBox = GeomAPI.GeomAPI_Pnt(aCornerBox.x() * (aCosAngle + (1 - aCosAngle) * aRotDir.x()**2) + aCornerBox.y() * ((1 - aCosAngle) * aRotDir.x() * aRotDir.y() - aSinAngle * aRotDir.z()) + aCornerBox.z() * ((1 - aCosAngle) * aRotDir.x() * aRotDir.z() + aSinAngle * aRotDir.y()),
140                                  aCornerBox.x() * ((1 - aCosAngle) * aRotDir.x() * aRotDir.y() + aSinAngle * aRotDir.z()) + aCornerBox.y() * (aCosAngle + (1 - aCosAngle) * aRotDir.y()**2) + aCornerBox.z() * ((1 - aCosAngle) * aRotDir.y() * aRotDir.z() - aSinAngle * aRotDir.x()),
141                                  aCornerBox.x() * ((1 - aCosAngle) * aRotDir.x() * aRotDir.z() - aSinAngle * aRotDir.y()) + aCornerBox.y() * ((1 - aCosAngle) * aRotDir.y() * aRotDir.z() + aSinAngle * aRotDir.x()) + aCornerBox.z() * (aCosAngle + (1 - aCosAngle) * aRotDir.z()**2))
142 aCornerBox1 = GeomAPI.GeomAPI_Pnt(aCornerBox1.x() * (aCosAngle + (1 - aCosAngle) * aRotDir.x()**2) + aCornerBox1.y() * ((1 - aCosAngle) * aRotDir.x() * aRotDir.y() - aSinAngle * aRotDir.z()) + aCornerBox1.z() * ((1 - aCosAngle) * aRotDir.x() * aRotDir.z() + aSinAngle * aRotDir.y()),
143                                   aCornerBox1.x() * ((1 - aCosAngle) * aRotDir.x() * aRotDir.y() + aSinAngle * aRotDir.z()) + aCornerBox1.y() * (aCosAngle + (1 - aCosAngle) * aRotDir.y()**2) + aCornerBox1.z() * ((1 - aCosAngle) * aRotDir.y() * aRotDir.z() - aSinAngle * aRotDir.x()),
144                                   aCornerBox1.x() * ((1 - aCosAngle) * aRotDir.x() * aRotDir.z() - aSinAngle * aRotDir.y()) + aCornerBox1.y() * ((1 - aCosAngle) * aRotDir.y() * aRotDir.z() + aSinAngle * aRotDir.x()) + aCornerBox1.z() * (aCosAngle + (1 - aCosAngle) * aRotDir.z()**2))
145 checkRotatedBox(Rotation_2, aCornerBox, aCornerBox1)
146
147 aCornerPara.setY(aCornerPara.y() + ParamWidth.value())
148 aCornerPara1 = GeomAPI.GeomAPI_Pnt(aCornerPara.x() + ParamDepth.value(), aCornerPara.y() - ParamWidth.value(), aCornerPara.z() + ParamHeight.value())
149 aCornerPara = GeomAPI.GeomAPI_Pnt(aCornerPara.x() * (aCosAngle + (1 - aCosAngle) * aRotDir.x()**2) + aCornerPara.y() * ((1 - aCosAngle) * aRotDir.x() * aRotDir.y() - aSinAngle * aRotDir.z()) + aCornerPara.z() * ((1 - aCosAngle) * aRotDir.x() * aRotDir.z() + aSinAngle * aRotDir.y()),
150                                   aCornerPara.x() * ((1 - aCosAngle) * aRotDir.x() * aRotDir.y() + aSinAngle * aRotDir.z()) + aCornerPara.y() * (aCosAngle + (1 - aCosAngle) * aRotDir.y()**2) + aCornerPara.z() * ((1 - aCosAngle) * aRotDir.y() * aRotDir.z() - aSinAngle * aRotDir.x()),
151                                   aCornerPara.x() * ((1 - aCosAngle) * aRotDir.x() * aRotDir.z() - aSinAngle * aRotDir.y()) + aCornerPara.y() * ((1 - aCosAngle) * aRotDir.y() * aRotDir.z() + aSinAngle * aRotDir.x()) + aCornerPara.z() * (aCosAngle + (1 - aCosAngle) * aRotDir.z()**2))
152 aCornerPara1 = GeomAPI.GeomAPI_Pnt(aCornerPara1.x() * (aCosAngle + (1 - aCosAngle) * aRotDir.x()**2) + aCornerPara1.y() * ((1 - aCosAngle) * aRotDir.x() * aRotDir.y() - aSinAngle * aRotDir.z()) + aCornerPara1.z() * ((1 - aCosAngle) * aRotDir.x() * aRotDir.z() + aSinAngle * aRotDir.y()),
153                                    aCornerPara1.x() * ((1 - aCosAngle) * aRotDir.x() * aRotDir.y() + aSinAngle * aRotDir.z()) + aCornerPara1.y() * (aCosAngle + (1 - aCosAngle) * aRotDir.y()**2) + aCornerPara1.z() * ((1 - aCosAngle) * aRotDir.y() * aRotDir.z() - aSinAngle * aRotDir.x()),
154                                    aCornerPara1.x() * ((1 - aCosAngle) * aRotDir.x() * aRotDir.z() - aSinAngle * aRotDir.y()) + aCornerPara1.y() * ((1 - aCosAngle) * aRotDir.y() * aRotDir.z() + aSinAngle * aRotDir.x()) + aCornerPara1.z() * (aCosAngle + (1 - aCosAngle) * aRotDir.z()**2))
155 checkRotatedBox(Rotation_3, aCornerPara, aCornerPara1)
156
157 # Test 4. Compose a non-closed shell of the box faces and check it is not a box
158 Shell_objects = ["Rotation_3_1/Rotated_Face_1", "Rotation_3_1/Rotated_Face_2", "Rotation_3_1/Rotated_Face_3", "Rotation_3_1/Rotated_Face_4"]
159 checkShellNotBox(Part_1_doc, Shell_objects)
160
161 # Test 5. Compose a shell of all box faces
162 Shell_objects = ["Rotation_3_1/Rotated_Face_1", "Rotation_3_1/Rotated_Face_2", "Rotation_3_1/Rotated_Face_3", "Rotation_3_1/Rotated_Face_4", "Rotation_3_1/Rotated_Face_5", "Rotation_3_1/Rotated_Face_6"]
163 checkShellRotatedBox(Part_1_doc, Shell_objects, aCornerPara, aCornerPara1)
164
165 model.end()