Salome HOME
76f4646c8e0b7c1007be1d4d98bac97956036eee
[modules/shaper.git] / src / ConnectorAPI / Test / TestImportXAOMem.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       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, 'Box10x10x10' )
56     geompy.addToStudyInFather( Box_1, Group_1, 'GroupTopFace' )
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     with tempfile.TemporaryDirectory() as tmpdirname:
63         tmpfilename = os.path.join(tmpdirname, "Box.xao")
64         exported = geompy.ExportXAO(Box_1, [Group_1], [], "XAO", tmpfilename, "")
65         file_stats = os.stat(tmpfilename)
66         assert(len(Export_buff) == file_stats.st_size)
67         pass
68
69     # Import to SHAPER
70     model.begin()
71     partSet = model.moduleDocument()
72     Part_1 = model.addPart(partSet)
73     Part_1_doc = Part_1.document()
74
75     Import_1 = model.addImportXAOMem(Part_1_doc, Export_buff)
76     model.end()
77
78     # Check result
79     assert(Import_1.feature().error() == "")
80     model.testNbResults(Import_1, 1)
81     model.testNbSubResults(Import_1, [0])
82     model.testNbSubShapes(Import_1, GeomAPI_Shape.SOLID, [1])
83     model.testNbSubShapes(Import_1, GeomAPI_Shape.FACE, [6])
84     model.testNbSubShapes(Import_1, GeomAPI_Shape.EDGE, [24])
85     model.testNbSubShapes(Import_1, GeomAPI_Shape.VERTEX, [48])
86     model.testResultsVolumes(Import_1, [1000])
87     model.testResultsAreas(Import_1, [600])
88
89     assert(Import_1.name() == "Box10x10x10")
90     assert(Import_1.result().name() == "Box10x10x10_1")
91     assert(Import_1.subFeature(0).name() == "GroupTopFace")
92     assert(Import_1.subFeature(0).result().name() == "GroupTopFace")
93
94     # check group
95     assert(Part_1_doc.size("Groups") == 1)
96     res1 = objectToResult(Part_1_doc.object("Groups", 0))
97     assert(res1 is not None)
98     res1It = GeomAPI_ShapeExplorer(res1.shape(), GeomAPI_Shape.FACE)
99     assert(res1It.more())
100     shape1 = res1It.current()
101     res1It.next()
102     assert(not res1It.more())
103     p1 = res1.shape().middlePoint()
104     aTol = 1.e-7
105     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)
106     pass
107
108 def testImportXAOMem_EmptyName():
109
110     # Export from GEOM
111     Box_1 = geompy.MakeBoxDXDYDZ(10, 10, 10)
112     Group_1 = geompy.CreateGroup(Box_1, geompy.ShapeType["FACE"])
113     geompy.UnionIDs(Group_1, [33])
114     # here we do not add box to study to check special case of empty shape name in XAO
115
116     Export_buff = geompy.ExportXAOMem(Box_1, [Group_1], [], "XAO")
117
118     # Import to SHAPER
119     model.begin()
120     partSet = model.moduleDocument()
121     Part_1 = model.addPart(partSet)
122     Part_1_doc = Part_1.document()
123
124     Import_1 = model.addImportXAOMem(Part_1_doc, Export_buff)
125     model.end()
126
127     # Check result
128     assert(Import_1.feature().error() == "")
129     model.testNbResults(Import_1, 1)
130
131     assert(Import_1.name() == "ImportXAOMem")
132     assert(Import_1.result().name() == "ImportXAOMem_1")
133     assert(Import_1.subFeature(0).name() == "Group_1")
134     assert(Import_1.subFeature(0).result().name() == "Group_1")
135
136 if __name__ == '__main__':
137     #=========================================================================
138     # Import a shape from XAO memory buffer
139     #=========================================================================
140     testImportXAOMem()
141     testImportXAOMem_EmptyName()
142     #=========================================================================
143     # End of test
144     #=========================================================================