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