]> SALOME platform Git repositories - modules/shaper.git/blob - src/ConnectorAPI/Test/TestImportXAOMem.py
Salome HOME
ea79eadce1d9e334507dbaf4a3894f572fb3f588
[modules/shaper.git] / src / ConnectorAPI / Test / TestImportXAOMem.py
1 # Copyright (C) 2022-2023  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       TestImportXAOMem.py
22       Unit test of ExchangePlugin_ImportFeature class
23 """
24
25 #=========================================================================
26 # Initialization of the test
27 #=========================================================================
28
29 import salome
30 salome.standalone()
31 salome.salome_init(1)
32
33 import os
34 import math
35 import tempfile
36
37 from ModelAPI import *
38 from GeomAPI import *
39 from GeomAlgoAPI import *
40 from salome.shaper import model
41
42 import GEOM
43 from salome.geom import geomBuilder
44 geompy = geomBuilder.New()
45
46 #=========================================================================
47 # test Import XAO from memory buffer (bytes array) 
48 #=========================================================================
49 def testImportXAOMem():
50
51     # Export from GEOM
52     Box_1 = geompy.MakeBoxDXDYDZ(10, 10, 10)
53     Group_1 = geompy.CreateGroup(Box_1, geompy.ShapeType["FACE"])
54     geompy.UnionIDs(Group_1, [33])
55     geompy.addToStudy( Box_1, 'Box_1_1' )
56     geompy.addToStudyInFather( Box_1, Group_1, 'Group_1' )
57
58     Export_buff = geompy.ExportXAOMem(Box_1, [Group_1], [], "XAO")
59
60     # check buffer length
61     # export to XAO file and compare size of file and size of buffer
62     #assert(len(Export_buff) == 4392)
63     with tempfile.TemporaryDirectory() as tmpdirname:
64         tmpfilename = os.path.join(tmpdirname, "Box.xao")
65         exported = geompy.ExportXAO(Box_1, [Group_1], [], "XAO", tmpfilename, "")
66         file_stats = os.stat(tmpfilename)
67         print(file_stats.st_size)
68         assert(len(Export_buff) == file_stats.st_size)
69         pass
70
71     # Import to SHAPER
72     model.begin()
73     partSet = model.moduleDocument()
74     Part_1 = model.addPart(partSet)
75     Part_1_doc = Part_1.document()
76
77     Import_1 = model.addImportXAOMem(Part_1_doc, Export_buff)
78     model.do()
79     Import_1.subFeature(0).setName("Group_1")
80     Import_1.subFeature(0).result().setName("Group_1")
81     Import_1.setName("Box_1")
82     Import_1.result().setName("Box_1_1")
83
84     model.end()
85
86     # Check result
87     assert(Import_1.feature().error() == "")
88     model.testNbResults(Import_1, 1)
89     model.testNbSubResults(Import_1, [0])
90     model.testNbSubShapes(Import_1, GeomAPI_Shape.SOLID, [1])
91     model.testNbSubShapes(Import_1, GeomAPI_Shape.FACE, [6])
92     model.testNbSubShapes(Import_1, GeomAPI_Shape.EDGE, [24])
93     model.testNbSubShapes(Import_1, GeomAPI_Shape.VERTEX, [48])
94     model.testResultsVolumes(Import_1, [1000])
95     model.testResultsAreas(Import_1, [600])
96
97     # check groups are the same in both parts
98     assert(Part_1_doc.size("Groups") == 1)
99     res1 = objectToResult(Part_1_doc.object("Groups", 0))
100     assert(res1 is not None)
101     res1It = GeomAPI_ShapeExplorer(res1.shape(), GeomAPI_Shape.FACE)
102     assert(res1It.more())
103     shape1 = res1It.current()
104     res1It.next()
105     assert(not res1It.more())
106     p1 = res1.shape().middlePoint()
107     aTol = 1.e-7
108     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)
109
110 if __name__ == '__main__':
111     #=========================================================================
112     # Import a shape from XAO memory buffer
113     #=========================================================================
114     testImportXAOMem()
115     #=========================================================================
116     # End of test
117     #=========================================================================