Salome HOME
using a better solution to do compare doubles for shape physical properties
[modules/shaper.git] / src / ConnectorAPI / Test / TestExportXAOMem.py
1 # Copyright (C) 2022-2024  CEA, EDF, OPEN CASCADE
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 """
21       TestExportXAOMem.py
22       Unit test of ExchangePlugin_ExportFeature class
23 """
24
25 #=========================================================================
26 # Initialization of the test
27 #=========================================================================
28
29 import salome
30 salome.standalone()
31 salome.salome_init(embedded=True)
32
33 import os
34 import math
35 import tempfile
36
37 from ModelAPI import *
38 from GeomAlgoAPI import *
39 from salome.shaper import model
40
41 import GEOM
42 from salome.geom import geomBuilder
43 geompy = geomBuilder.New()
44
45 #=========================================================================
46 # test Export XAO to memory buffer (bytes array) 
47 #=========================================================================
48 def testExportXAOMem():
49
50     model.begin()
51     partSet = model.moduleDocument()
52     Part_1 = model.addPart(partSet)
53     Part_1_doc = Part_1.document()
54     Box_1 = model.addBox(Part_1_doc, 10, 10, 10)
55     Group_1 = model.addGroup(Part_1_doc, "Faces", [model.selection("FACE", "Box_1_1/Top")])
56     Box_2 = model.addBox(Part_1_doc, 20, 20, 20)
57     model.do()
58
59     # Export to memory buffer (bytes array)
60     Export_buff1 = model.exportToXAOMem(Part_1_doc, model.selection("SOLID", "Box_1_1"), 'XAO')
61     Export_buff2 = model.exportToXAOMem(Part_1_doc, model.selection("SOLID", "Box_2_1"), 'XAO')
62     model.end()
63
64     # check buffer length
65     # export to XAO file and compare size of file and size of buffer
66     #assert(len(Export_buff1) == 4392)
67     #assert(len(Export_buff2) == 4287)
68     with tempfile.TemporaryDirectory() as tmpdirname:
69         tmpfilename = os.path.join(tmpdirname, "Box.xao")
70         Export_1 = model.exportToXAO(Part_1_doc, tmpfilename, model.selection("SOLID", "Box_1_1"), 'XAO')
71         file_stats = os.stat(tmpfilename)
72         assert(len(Export_buff1) == file_stats.st_size)
73
74         Export_2 = model.exportToXAO(Part_1_doc, tmpfilename, model.selection("SOLID", "Box_2_1"), 'XAO')
75         file_stats = os.stat(tmpfilename)
76         assert(len(Export_buff2) == file_stats.st_size)
77         pass
78
79     # Import to GEOM
80     (imported1, b1, [], [Group_1], []) = geompy.ImportXAOMem(Export_buff1, theName="Box1")
81     (imported2, b2, [], []       , []) = geompy.ImportXAOMem(Export_buff2, theName="Box2")
82
83     # Check result 1
84     aTol = 1.e-7
85     Props = geompy.BasicProperties(b1)
86     # surface area
87     aSurface = 600
88     assert (math.fabs(Props[1] - aSurface) < aTol), "The surface is wrong: expected = {0}, real = {1}".format(aSurface, Props[1])
89
90     Props = geompy.BasicProperties(Group_1)
91     # surface area
92     aSurface = 100
93     assert (math.fabs(Props[1] - aSurface) < aTol), "The surface is wrong: expected = {0}, real = {1}".format(aSurface, Props[1])
94
95     # Check result 2
96     Props = geompy.BasicProperties(b2)
97     # surface area
98     aSurface = 2400
99     assert (math.fabs(Props[1] - aSurface) < aTol), "The surface is wrong: expected = {0}, real = {1}".format(aSurface, Props[1])
100
101 if __name__ == '__main__':
102     #=========================================================================
103     # Export a shape into XAO memory buffer
104     #=========================================================================
105     testExportXAOMem()
106     #=========================================================================
107     # End of test
108     #=========================================================================