Salome HOME
Merge branch 'csgroup_IS2'
[modules/shaper.git] / src / FeaturesPlugin / Test / TestScale_MultiLevelCompound_v95_2.py
1 # Copyright (C) 2020-2021  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 salome.shaper import model
21
22 from GeomAPI import *
23 from SketchAPI import *
24
25 model.begin()
26 partSet = model.moduleDocument()
27 Part_1 = model.addPart(partSet)
28 Part_1_doc = Part_1.document()
29 Sketch_1 = model.addSketch(Part_1_doc, model.standardPlane("XOY"))
30 SketchEllipse_1 = Sketch_1.addEllipse(11.18033988749894, -50, 22.36067977499789, -50, 10)
31 [SketchPoint_1, SketchPoint_2, SketchPoint_3, SketchPoint_4, SketchPoint_5, SketchPoint_6, SketchPoint_7, SketchLine_1, SketchLine_2] = SketchEllipse_1.construction(center = "aux", firstFocus = "aux", secondFocus = "aux", majorAxisStart = "aux", majorAxisEnd = "aux", minorAxisStart = "aux", minorAxisEnd = "aux", majorAxis = "aux", minorAxis = "aux")
32 SketchConstraintHorizontal_1 = Sketch_1.setHorizontal(SketchLine_1.result())
33 SketchConstraintLength_1 = Sketch_1.setLength(SketchLine_1.result(), 30)
34 SketchConstraintLength_2 = Sketch_1.setLength(SketchLine_2.result(), 20)
35 SketchProjection_1 = Sketch_1.addProjection(model.selection("EDGE", "PartSet/OY"), False)
36 SketchLine_3 = SketchProjection_1.createdFeature()
37 SketchConstraintCoincidence_1 = Sketch_1.setCoincident(SketchAPI_Point(SketchPoint_3).coordinates(), SketchLine_3.result())
38 SketchConstraintDistance_1 = Sketch_1.setDistance(SketchAPI_Line(SketchLine_3).startPoint(), SketchAPI_Point(SketchPoint_3).coordinates(), 50, True)
39 SketchCircle_1 = Sketch_1.addCircle(41.18033988749897, -50, 5)
40 SketchConstraintCoincidence_2 = Sketch_1.setCoincident(SketchCircle_1.center(), SketchLine_1.result())
41 SketchConstraintDistance_2 = Sketch_1.setDistance(SketchEllipse_1.majorAxisPositive(), SketchCircle_1.center(), 15, True)
42 SketchConstraintRadius_1 = Sketch_1.setRadius(SketchCircle_1.results()[1], 5)
43 SketchMultiRotation_1 = Sketch_1.addRotation([SketchEllipse_1.result(), SketchCircle_1.results()[1]], SketchAPI_Point(SketchPoint_3).coordinates(), 360, 3, True)
44 [SketchEllipse_2, SketchEllipse_3, SketchCircle_2, SketchCircle_3] = SketchMultiRotation_1.rotated()
45 model.do()
46 Sketch_1.changeFacesOrder([[SketchEllipse_1.result(), SketchEllipse_1.result(), SketchEllipse_2.result(), SketchEllipse_3.result()],
47                            [SketchCircle_1.results()[1]],
48                            [SketchEllipse_2.result(), SketchEllipse_2.result(), SketchEllipse_3.result(), SketchEllipse_1.result()],
49                            [SketchEllipse_1.result(), SketchEllipse_3.result(), SketchEllipse_2.result()],
50                            [SketchEllipse_3.result(), SketchEllipse_2.result(), SketchEllipse_1.result()],
51                            [SketchEllipse_1.result(), SketchEllipse_2.result(), SketchEllipse_3.result()],
52                            [SketchEllipse_1.result(), SketchEllipse_3.result(), SketchEllipse_2.result()],
53                            [SketchEllipse_2.result(), SketchEllipse_3.result(), SketchEllipse_3.result(), SketchEllipse_1.result()],
54                            [SketchCircle_2.results()[1]],
55                            [SketchCircle_3.results()[1]]
56                           ])
57 model.do()
58 Extrusion_1 = model.addExtrusion(Part_1_doc, [model.selection("COMPOUND", "Sketch_1")], model.selection(), 10, 0)
59 Compound_1 = model.addCompound(Part_1_doc, [model.selection("SOLID", "Extrusion_1_2"), model.selection("SOLID", "Extrusion_1_3")])
60 Compound_2_objects = [model.selection("COMPSOLID", "Extrusion_1_1"), model.selection("COMPOUND", "Compound_1_1"), model.selection("SOLID", "Extrusion_1_4")]
61 Compound_2 = model.addCompound(Part_1_doc, Compound_2_objects)
62 model.end()
63
64 SCALE_X = 1
65 SCALE_Y = 2
66 SCALE_Z = 3
67 TOLERANCE = 1.e-4
68
69 def average(thePoints):
70     x = 0; y = 0; z = 0
71     for p in thePoints:
72         x += p.x(); y += p.y(); z+= p.z()
73     x /= len(thePoints)
74     y /= len(thePoints)
75     z /= len(thePoints)
76     return GeomAPI_Pnt(x, y, z)
77
78 def subsRefPoints(theResult):
79     res = []
80     for ind in range(0, theResult.numberOfSubs()):
81         res.append(theResult.subResult(ind).resultSubShapePair()[0].shape().middlePoint())
82     return res
83
84 REFERENCE = subsRefPoints(Compound_2.result())
85
86 def assertResult(theFeature, theVolume):
87     model.testNbResults(theFeature, 1)
88     model.testNbSubResults(theFeature, [3])
89     model.testNbSubShapes(theFeature, GeomAPI_Shape.SOLID, [10])
90     model.testNbSubShapes(theFeature, GeomAPI_Shape.FACE, [47])
91     model.testNbSubShapes(theFeature, GeomAPI_Shape.EDGE, [162])
92     model.testNbSubShapes(theFeature, GeomAPI_Shape.VERTEX, [324])
93     model.testResultsVolumes(theFeature, [theVolume])
94
95     for ind in range(0, theFeature.result().numberOfSubs()):
96         ref = REFERENCE[ind]
97         midPoint = theFeature.result().subResult(ind).resultSubShapePair()[0].shape().middlePoint()
98         assert(midPoint.distance(ref) < TOLERANCE), "Sub-result {}; actual ({}, {}, {}) != expected ({}, {}, {})".format(ind, midPoint.x(), midPoint.y(), midPoint.z(), ref.x(), ref.y(), ref.z())
99
100
101 model.begin()
102 Scale_1 = model.addScale(Part_1_doc, [model.selection("SOLID", "Compound_2_1_1_5")], model.selection("VERTEX", "PartSet/Origin"), SCALE_X, SCALE_Y, SCALE_Z, keepSubResults = True)
103 model.end()
104 # selection of a compsolid part is prohibited
105 assert(Scale_1.feature().error() != "")
106
107 model.begin()
108 Scale_1.setMainObjects([model.selection("COMPSOLID", "Compound_2_1_1")])
109 REFERENCE[0] = GeomAPI_Pnt(REFERENCE[0].x() * SCALE_X, REFERENCE[0].y() * SCALE_Y, REFERENCE[0].z() * SCALE_Z)
110 REFERENCE[0].setX(0) # <= B-spline poles are used to calculate the bounding box and thus the middle point
111 assertResult(Scale_1, 74139.07479)
112
113 Scale_2 = model.addScale(Part_1_doc, [model.selection("SOLID", "Scale_1_1_2_1")], model.selection("VERTEX", "PartSet/Origin"), SCALE_X, SCALE_Y, SCALE_Z, keepSubResults = True)
114 subs = subsRefPoints(Compound_2.result().subResult(1))
115 subs[0] = GeomAPI_Pnt(subs[0].xyz().added(GeomAPI_XYZ(5, -5, 5)))
116 subs[0] = GeomAPI_Pnt(subs[0].x() * SCALE_X, subs[0].y() * SCALE_Y, subs[0].z() * SCALE_Z)
117 subs[0].setY(-117.3204876) # <= B-spline poles are used to calculate the bounding box and thus the middle point
118 subs[1] = GeomAPI_Pnt(subs[1].xyz().added(GeomAPI_XYZ(-5, 5, -5)))
119 REFERENCE[1] = average(subs)
120 assertResult(Scale_2, 78066.07487341)
121
122 Scale_3 = model.addScale(Part_1_doc, [model.selection("SOLID", "Scale_2_1_3")], model.selection("VERTEX", "PartSet/Origin"), SCALE_X, SCALE_Y, SCALE_Z, keepSubResults = True)
123 REFERENCE[2] = GeomAPI_Pnt(REFERENCE[2].x() * SCALE_X, REFERENCE[2].y() * SCALE_Y, REFERENCE[2].z() * SCALE_Z)
124 assertResult(Scale_3, 81993.0749561)
125
126 Scale_4 = model.addScale(Part_1_doc, [model.selection("SOLID", "Scale_3_1_1_1"), model.selection("SOLID", "Scale_3_1_2_2"), model.selection("SOLID", "Scale_3_1_3")], model.selection("VERTEX", "PartSet/Origin"), 1 / SCALE_X, 1 / SCALE_Y, 1 / SCALE_Z, keepSubResults = True)
127 model.end()
128 # selection of a compsolid part is prohibited
129 assert(Scale_4.feature().error() != "")
130
131 model.begin()
132 Scale_4.setMainObjects([model.selection("COMPSOLID", "Scale_3_1_1"), model.selection("SOLID", "Scale_3_1_2_2"), model.selection("SOLID", "Scale_3_1_3")])
133 REFERENCE[0] = GeomAPI_Pnt(REFERENCE[0].x() / SCALE_X, REFERENCE[0].y() / SCALE_Y, REFERENCE[0].z() / SCALE_Z)
134 subs[1] = GeomAPI_Pnt(subs[1].x() / SCALE_X, subs[1].y() / SCALE_Y, subs[1].z() / SCALE_Z)
135 # B-spline poles are used to calculate the bounding box and thus the middle point
136 subs[1].setX(-30.59017)
137 subs[1].setY(-2.83826244)
138 REFERENCE[1] = average(subs)
139 REFERENCE[2] = GeomAPI_Pnt(REFERENCE[2].x() / SCALE_X, REFERENCE[2].y() / SCALE_Y, REFERENCE[2].z() / SCALE_Z)
140 assertResult(Scale_4, 17592.5112883)
141
142 model.end()
143
144 assert(model.checkPythonDump())