1 # Copyright (C) 2018-2024 CEA, EDF
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.
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.
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
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 from salome.shaper import model
29 partSet = model.moduleDocument()
30 Part_1 = model.addPart(partSet)
31 Part_1_doc = Part_1.document()
33 Cyl_1 = model.addCylinder(Part_1_doc, 7, 13)
34 Box_1 = model.addBox(Part_1_doc, 10, 10, 10)
36 Group_1 = model.addGroup(Part_1_doc, "Faces", [model.selection("FACE", "Box_1_1/Top")])
38 Field_1 = model.addField(Part_1_doc, 1, "STRING", 1, ["Comp 1"], [model.selection("SOLID", "Box_1_1")])
39 Field_1.addStep(0, 0, [[""], ["box"]])
41 with tempfile.TemporaryDirectory() as tmpdirname:
43 tmp_file1 = os.path.join(tmpdirname, "Part.xao")
44 tmp_file2 = os.path.join(tmpdirname, "Part_shape.brep")
46 Export_1 = model.exportToXAO(Part_1_doc, tmp_file1, "", "", tmp_file2)
47 # due to the issue 2530, the export feature is removed after execution, so,
48 # if export was performed correctly, the feature must become invalid
49 #assert(Export_1.feature().error() == "")
50 assert(not Export_1.feature().data().isValid())
51 assert(os.path.isfile(tmp_file1))
52 assert(os.path.isfile(tmp_file2))
54 # Add two fields and one group to the Box_1
55 Field_2 = model.addField(Part_1_doc, 1, "BOOLEAN", 1, ["Comp 1"], [model.selection("FACE", "Box_1_1/Front")])
56 Field_2.addStep(0, 0, [[False], [True]])
58 Field_3_objects = [model.selection("VERTEX", "[Box_1_1/Front][Box_1_1/Left][Box_1_1/Top]"), model.selection("VERTEX", "[Box_1_1/Front][Box_1_1/Left][Box_1_1/Bottom]"), model.selection("VERTEX", "[Box_1_1/Front][Box_1_1/Right][Box_1_1/Bottom]"), model.selection("VERTEX", "[Box_1_1/Front][Box_1_1/Right][Box_1_1/Top]")]
59 Field_3 = model.addField(Part_1_doc, 1, "DOUBLE", 1, ["Comp 1"], Field_3_objects)
60 Field_3.addStep(0, 0, [[0], [1], [0], [0], [0]])
62 Group_2 = model.addGroup(Part_1_doc, "Edges", [model.selection("EDGE", "[Box_1_1/Top][Box_1_1/Left]"), model.selection("EDGE", "[Box_1_1/Top][Box_1_1/Right]")])
64 tmp_file3 = os.path.join(tmpdirname, "Box.xao")
65 tmp_file4 = os.path.join(tmpdirname, "Box_shape.brep")
67 Export_2 = model.exportToXAO(Part_1_doc, tmp_file3, model.selection("SOLID", "Box_1_1"),
69 assert(Export_2.feature().error() == "")
70 assert(os.path.isfile(tmp_file3))
71 assert(os.path.isfile(tmp_file4))
75 # Try to import the exported data
77 partSet = model.moduleDocument()
78 Part_1 = model.addPart(partSet)
79 Part_1_doc = Part_1.document()
81 Import_1 = model.addImport(Part_1_doc, tmp_file1)
83 assert(Import_1.feature().error() == "")
85 model.testNbResults(Import_1, 1)
86 model.testNbSubResults(Import_1, [2])
87 model.testNbSubShapes(Import_1, GeomAPI_Shape.SOLID, [2])
88 model.testNbSubShapes(Import_1, GeomAPI_Shape.FACE, [9])
89 model.testNbSubShapes(Import_1, GeomAPI_Shape.EDGE, [30])
90 model.testNbSubShapes(Import_1, GeomAPI_Shape.VERTEX, [60])
91 model.testResultsVolumes(Import_1, [3001.1945203])
92 model.testResultsAreas(Import_1, [1479.645943])
94 assert(Import_1.name() == "Part")
95 assert(Import_1.result().name() == "Part_1")
96 assert(Import_1.subFeature(0).name() == "Group_1")
97 assert(Import_1.subFeature(1).name() == "Field_1")
98 assert(Import_1.subFeature(0).result().name() == "Group_1")
99 assert(Import_1.subFeature(1).result().name() == "Field_1")
102 assert(Part_1_doc.size("Groups") == 1)
103 res1 = model.objectToResult(Part_1_doc.object("Groups", 0))
104 assert(res1 is not None)
105 res1It = GeomAPI_ShapeExplorer(res1.shape(), GeomAPI_Shape.FACE)
106 assert(res1It.more())
107 shape1 = res1It.current()
109 assert(not res1It.more())
110 p1 = res1.shape().middlePoint()
112 assert(math.fabs(p1.x() - 5) <= aTol and math.fabs(p1.y() - 5) <= aTol and math.fabs(p1.z() - 10) <= aTol), "({}, {}, {}) != ({}, {}, {})".format(p1.x(), p1.y(), p1.z(), 5, 5, 10)
114 Import_2 = model.addImport(Part_1_doc, tmp_file3)
116 assert(Import_2.feature().error() == "")
118 model.testNbResults(Import_2, 1)
119 model.testNbSubResults(Import_2, [0])
120 model.testNbSubShapes(Import_2, GeomAPI_Shape.SOLID, [1])
121 model.testNbSubShapes(Import_2, GeomAPI_Shape.FACE, [6])
122 model.testNbSubShapes(Import_2, GeomAPI_Shape.EDGE, [24])
123 model.testNbSubShapes(Import_2, GeomAPI_Shape.VERTEX, [48])
124 model.testResultsVolumes(Import_2, [1000])
125 model.testResultsAreas(Import_2, [600])
127 assert(Import_2.name() == "Box_1_1")
128 assert(Import_2.result().name() == "Box_1_1_1")
129 assert(Import_2.subFeature(0).name() == "Group_1")
130 assert(Import_2.subFeature(1).name() == "Group_2")
131 assert(Import_2.subFeature(2).name() == "Field_1")
132 assert(Import_2.subFeature(3).name() == "Field_2")
133 assert(Import_2.subFeature(4).name() == "Field_3")
134 assert(Import_2.subFeature(0).result().name() == "Group_1")
135 assert(Import_2.subFeature(1).result().name() == "Group_2")
136 assert(Import_2.subFeature(2).result().name() == "Field_1")
137 assert(Import_2.subFeature(3).result().name() == "Field_2")
138 assert(Import_2.subFeature(4).result().name() == "Field_3")
141 assert(Part_1_doc.size("Groups") == 3)
143 res1 = model.objectToResult(Part_1_doc.object("Groups", 1))
144 assert(res1 is not None)
145 res1It = GeomAPI_ShapeExplorer(res1.shape(), GeomAPI_Shape.FACE)
146 assert(res1It.more())
147 shape1 = res1It.current()
149 assert(not res1It.more())
150 p1 = res1.shape().middlePoint()
152 assert(math.fabs(p1.x() - 5) <= aTol and math.fabs(p1.y() - 5) <= aTol and math.fabs(p1.z() - 10) <= aTol), "({}, {}, {}) != ({}, {}, {})".format(p1.x(), p1.y(), p1.z(), 5, 5, 10)
154 res2 = model.objectToResult(Part_1_doc.object("Groups", 2))
155 assert(res2 is not None)
156 res2It = GeomAPI_ShapeExplorer(res2.shape(), GeomAPI_Shape.EDGE)
157 assert(res2It.more())
158 edge1 = res2It.current()
160 edge2 = res2It.current()
162 assert(not res2It.more())
163 p2 = res2.shape().middlePoint()
165 assert(math.fabs(p1.x() - 5) <= aTol and math.fabs(p1.y() - 5) <= aTol and math.fabs(p1.z() - 10) <= aTol), "({}, {}, {}) != ({}, {}, {})".format(p1.x(), p1.y(), p1.z(), 5, 5, 10)