Salome HOME
using a better solution to do compare doubles for shape physical properties
[modules/shaper.git] / src / ExchangePlugin / Test / TestExportToXAO_SeparateShapeFile.py
1 # Copyright (C) 2018-2024  CEA, EDF
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
24 import tempfile
25 import os
26 import math
27
28 model.begin()
29 partSet = model.moduleDocument()
30 Part_1 = model.addPart(partSet)
31 Part_1_doc = Part_1.document()
32
33 Cyl_1 = model.addCylinder(Part_1_doc, 7, 13)
34 Box_1 = model.addBox(Part_1_doc, 10, 10, 10)
35
36 Group_1 = model.addGroup(Part_1_doc, "Faces", [model.selection("FACE", "Box_1_1/Top")])
37
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"]])
40
41 with tempfile.TemporaryDirectory() as tmpdirname:
42   # Export entire Part
43   tmp_file1 = os.path.join(tmpdirname, "Part.xao")
44   tmp_file2 = os.path.join(tmpdirname, "Part_shape.brep")
45
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))
53
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]])
57
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]])
61
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]")])
63
64   tmp_file3 = os.path.join(tmpdirname, "Box.xao")
65   tmp_file4 = os.path.join(tmpdirname, "Box_shape.brep")
66
67   Export_2 = model.exportToXAO(Part_1_doc, tmp_file3, model.selection("SOLID", "Box_1_1"),
68                                "", "", tmp_file4)
69   assert(Export_2.feature().error() == "")
70   assert(os.path.isfile(tmp_file3))
71   assert(os.path.isfile(tmp_file4))
72
73   model.end()
74
75   # Try to import the exported data
76   model.begin()
77   partSet = model.moduleDocument()
78   Part_1 = model.addPart(partSet)
79   Part_1_doc = Part_1.document()
80
81   Import_1 = model.addImport(Part_1_doc, tmp_file1)
82   model.do()
83   assert(Import_1.feature().error() == "")
84
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])
93
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")
100
101   # check group
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()
108   res1It.next()
109   assert(not res1It.more())
110   p1 = res1.shape().middlePoint()
111   aTol = 1.e-7
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)
113
114   Import_2 = model.addImport(Part_1_doc, tmp_file3)
115   model.do()
116   assert(Import_2.feature().error() == "")
117
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])
126
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")
139
140   # check groups
141   assert(Part_1_doc.size("Groups") == 3)
142
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()
148   res1It.next()
149   assert(not res1It.more())
150   p1 = res1.shape().middlePoint()
151   aTol = 1.e-7
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)
153
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()
159   res2It.next()
160   edge2 = res2It.current()
161   res2It.next()
162   assert(not res2It.more())
163   p2 = res2.shape().middlePoint()
164   aTol = 1.e-7
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)
166
167   pass
168
169 model.end()