Salome HOME
Copyright update 2022
[modules/shaper.git] / src / FeaturesPlugin / Test / TestSymmetry_MultiLevelCompound_v95_1.py
1 # Copyright (C) 2020-2022  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 import math
26
27 model.begin()
28 partSet = model.moduleDocument()
29 Part_1 = model.addPart(partSet)
30 Part_1_doc = Part_1.document()
31 Sketch_1 = model.addSketch(Part_1_doc, model.standardPlane("XOY"))
32 SketchEllipse_1 = Sketch_1.addEllipse(11.18033988749894, -50, 22.36067977499789, -50, 10)
33 [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")
34 SketchConstraintHorizontal_1 = Sketch_1.setHorizontal(SketchLine_1.result())
35 SketchConstraintLength_1 = Sketch_1.setLength(SketchLine_1.result(), 30)
36 SketchConstraintLength_2 = Sketch_1.setLength(SketchLine_2.result(), 20)
37 SketchProjection_1 = Sketch_1.addProjection(model.selection("EDGE", "PartSet/OY"), False)
38 SketchLine_3 = SketchProjection_1.createdFeature()
39 SketchConstraintCoincidence_1 = Sketch_1.setCoincident(SketchAPI_Point(SketchPoint_3).coordinates(), SketchLine_3.result())
40 SketchConstraintDistance_1 = Sketch_1.setDistance(SketchAPI_Line(SketchLine_3).startPoint(), SketchAPI_Point(SketchPoint_3).coordinates(), 50, True)
41 SketchCircle_1 = Sketch_1.addCircle(41.18033988749897, -50, 5)
42 SketchConstraintCoincidence_2 = Sketch_1.setCoincident(SketchCircle_1.center(), SketchLine_1.result())
43 SketchConstraintDistance_2 = Sketch_1.setDistance(SketchEllipse_1.majorAxisPositive(), SketchCircle_1.center(), 15, True)
44 SketchConstraintRadius_1 = Sketch_1.setRadius(SketchCircle_1.results()[1], 5)
45 SketchMultiRotation_1 = Sketch_1.addRotation([SketchEllipse_1.result(), SketchCircle_1.results()[1]], SketchAPI_Point(SketchPoint_3).coordinates(), 360, 3, True)
46 [SketchEllipse_2, SketchEllipse_3, SketchCircle_2, SketchCircle_3] = SketchMultiRotation_1.rotated()
47 model.do()
48 Sketch_1.changeFacesOrder([[SketchEllipse_1.result(), SketchEllipse_1.result(), SketchEllipse_2.result(), SketchEllipse_3.result()],
49                            [SketchCircle_1.results()[1]],
50                            [SketchEllipse_2.result(), SketchEllipse_2.result(), SketchEllipse_3.result(), SketchEllipse_1.result()],
51                            [SketchEllipse_1.result(), SketchEllipse_3.result(), SketchEllipse_2.result()],
52                            [SketchEllipse_3.result(), SketchEllipse_2.result(), SketchEllipse_1.result()],
53                            [SketchEllipse_1.result(), SketchEllipse_2.result(), SketchEllipse_3.result()],
54                            [SketchEllipse_1.result(), SketchEllipse_3.result(), SketchEllipse_2.result()],
55                            [SketchEllipse_2.result(), SketchEllipse_3.result(), SketchEllipse_3.result(), SketchEllipse_1.result()],
56                            [SketchCircle_2.results()[1]],
57                            [SketchCircle_3.results()[1]]
58                           ])
59 model.do()
60 Extrusion_1 = model.addExtrusion(Part_1_doc, [model.selection("COMPOUND", "Sketch_1")], model.selection(), 10, 0)
61 Compound_1 = model.addCompound(Part_1_doc, [model.selection("SOLID", "Extrusion_1_2"), model.selection("SOLID", "Extrusion_1_3")])
62 Compound_2_objects = [model.selection("COMPSOLID", "Extrusion_1_1"), model.selection("COMPOUND", "Compound_1_1"), model.selection("SOLID", "Extrusion_1_4")]
63 Compound_2 = model.addCompound(Part_1_doc, Compound_2_objects)
64 model.end()
65
66 def symmetry(thePoint, theCenter):
67     return GeomAPI_Pnt(2 * theCenter.x() - thePoint.x(), 2 * theCenter.y() - thePoint.y(), 2 * theCenter.z() - thePoint.z())
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
85 TOLERANCE = 1.e-7
86 ORIGIN = GeomAPI_Pnt(0, 0, 0)
87 CENTER_1 = GeomAPI_Pnt(SketchEllipse_1.center().pnt().x(), SketchEllipse_1.center().pnt().y(), 0)
88 CENTER_2 = GeomAPI_Pnt(SketchCircle_1.center().pnt().x(), SketchCircle_1.center().pnt().y(), 0)
89 REFERENCE = subsRefPoints(Compound_2.result())
90
91
92 def assertResult(theFeature, theNbSolids, theNbFaces, theNbEdges, theNbVertices, theVolume):
93     model.testNbResults(theFeature, 1)
94     model.testNbSubResults(theFeature, [3])
95     model.testNbSubShapes(theFeature, GeomAPI_Shape.SOLID, [theNbSolids])
96     model.testNbSubShapes(theFeature, GeomAPI_Shape.FACE, [theNbFaces])
97     model.testNbSubShapes(theFeature, GeomAPI_Shape.EDGE, [theNbEdges])
98     model.testNbSubShapes(theFeature, GeomAPI_Shape.VERTEX, [theNbVertices])
99     model.testResultsVolumes(theFeature, [theVolume])
100
101     for ind in range(0, theFeature.result().numberOfSubs()):
102         ref = REFERENCE[ind]
103         midPoint = theFeature.result().subResult(ind).resultSubShapePair()[0].shape().middlePoint()
104         assert(midPoint.distance(ref) < TOLERANCE), "Sub-result {}; actual ({}, {}, {}) != expected ({}, {}, {})".format(ind, midPoint.x(), midPoint.y(), midPoint.z(), ref.x(), ref.y(), ref.z())
105
106
107 model.begin()
108 Symmetry_1 = model.addSymmetry(Part_1_doc, [model.selection("SOLID", "Compound_2_1_1_5")], model.selection("VERTEX", "PartSet/Origin"), keepOriginal = False, keepSubResults = True)
109 model.end()
110 # selection of a compsolid part is prohibited
111 assert(Symmetry_1.feature().error() != "")
112
113 model.begin()
114 Symmetry_1.setMainObjects([model.selection("COMPSOLID", "Compound_2_1_1")])
115 REFERENCE[0] = symmetry(REFERENCE[0], ORIGIN)
116 assertResult(Symmetry_1, 10, 47, 162, 324, 14319.99602674256)
117
118 Symmetry_2 = model.addSymmetry(Part_1_doc, [model.selection("SOLID", "Symmetry_1_1_2_1")], model.selection("VERTEX", "PartSet/Origin"), keepOriginal = False, keepSubResults = True)
119 subs = subsRefPoints(Compound_2.result().subResult(1))
120 subs[0] = symmetry(subs[0], ORIGIN)
121 REFERENCE[1] = average(subs)
122 assertResult(Symmetry_2, 10, 47, 162, 324, 14319.99602674256)
123
124 Symmetry_3 = model.addSymmetry(Part_1_doc, [model.selection("SOLID", "Symmetry_2_1_3")], model.selection("VERTEX", "PartSet/Origin"), keepOriginal = False, keepSubResults = True)
125 REFERENCE[2] = symmetry(REFERENCE[2], ORIGIN)
126 assertResult(Symmetry_3, 10, 47, 162, 324, 14319.99602674256)
127
128 Symmetry_4 = model.addSymmetry(Part_1_doc, [model.selection("SOLID", "Symmetry_3_1_1_1"), model.selection("SOLID", "Symmetry_3_1_2_2"), model.selection("SOLID", "Symmetry_3_1_3")], model.selection("VERTEX", "PartSet/Origin"), keepOriginal = False, keepSubResults = True)
129 model.end()
130 # selection of a compsolid part is prohibited
131 assert(Symmetry_4.feature().error() != "")
132
133 model.begin()
134 Symmetry_4.setMainObjects([model.selection("COMPSOLID", "Symmetry_3_1_1"), model.selection("SOLID", "Symmetry_3_1_2_2"), model.selection("SOLID", "Symmetry_3_1_3")])
135 REFERENCE[0] = symmetry(REFERENCE[0], ORIGIN)
136 subs = subsRefPoints(Compound_2.result().subResult(1))
137 for ind in range(0, len(subs)):
138     subs[ind] = symmetry(subs[ind], ORIGIN)
139 REFERENCE[1] = average(subs)
140 REFERENCE[2] = symmetry(REFERENCE[2], ORIGIN)
141 assertResult(Symmetry_4, 10, 47, 162, 324, 14319.99602674256)
142
143
144 Symmetry_5 = model.addSymmetry(Part_1_doc, [model.selection("COMPSOLID", "Symmetry_4_1_1")], model.selection("VERTEX", "Sketch_1/SketchEllipse_1_ellipse_center"), keepOriginal = True, keepSubResults = True)
145 REFERENCE[0] = CENTER_1
146 assertResult(Symmetry_5, 17, 85, 306, 612, 26283.79756329278)
147
148 Symmetry_6 = model.addSymmetry(Part_1_doc, [model.selection("SOLID", "Symmetry_5_1_2_1")], model.selection("VERTEX", "Sketch_1/SketchEllipse_1_ellipse_center"), keepOriginal = True, keepSubResults = True)
149 REFERENCE[1] = CENTER_1
150 assertResult(Symmetry_6, 18, 88, 312, 624, 27069.19572669)
151
152 Symmetry_7 = model.addSymmetry(Part_1_doc, [model.selection("SOLID", "Symmetry_6_1_3")], model.selection("VERTEX", "Sketch_1/SketchEllipse_1_ellipse_center"), keepOriginal = True, keepSubResults = True)
153 REFERENCE[2] = CENTER_1
154 assertResult(Symmetry_7, 19, 91, 318, 636, 27854.59389)
155
156 Symmetry_8 = model.addSymmetry(Part_1_doc, [model.selection("COMPSOLID", "Symmetry_7_1_1"), model.selection("SOLID", "Symmetry_7_1_2_2"), model.selection("SOLID", "Symmetry_7_1_3")], model.selection("VERTEX", "Sketch_1/SketchCircle_1"), keepOriginal = True, keepSubResults = True)
157 REFERENCE[0] = CENTER_2
158 REFERENCE[2] = CENTER_2
159 assertResult(Symmetry_8, 36, 176, 624, 1248, 54138.39145338)
160
161 model.end()
162
163 assert(model.checkPythonDump())