Salome HOME
Copyright update 2021
[modules/shaper.git] / src / ConnectorAPI / Test / TestExportSTL.py
1 # Copyright (C) 2014-2021  CEA/DEN, EDF R&D
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       TestExport.py
22       Unit test of ExchangePlugin_TestExport class
23 """
24 #=========================================================================
25 # Initialization of the test
26 #=========================================================================
27
28 import salome
29
30 import os
31 import math
32 from tempfile import TemporaryDirectory
33
34 import GEOM
35
36 from ModelAPI import *
37
38 from salome.shaper import model
39
40 from salome.geom import geomBuilder
41
42 from GeomAlgoAPI import *
43
44 __updated__ = "2015-05-22"
45
46 salome.salome_init(1)
47 geompy = geomBuilder.New()
48
49 #=========================================================================
50 # Help functions
51 #=========================================================================
52 def removeFile(theFileName):
53     try: os.remove(theFileName)
54     except OSError: pass
55     assert not os.path.exists(theFileName), \
56             "Can not remove file {0}".format(theFileName)
57
58 #=========================================================================
59 # test Export STL 
60 #=========================================================================
61 def testExportSTL(theFile, theDelta, theErrorExpected = False):
62
63     model.begin()
64     partSet = model.moduleDocument()
65     Part_1 = model.addPart(partSet)
66     Part_1_doc = Part_1.document()
67     Box_1 = model.addBox(Part_1_doc, 10, 10, 10)
68     Box_2 = model.addBox(Part_1_doc, 20, 20, 20)
69     model.do()
70
71     # First export to GEOM
72     model.exportToGEOM(Part_1_doc)
73     model.end()
74
75     theSurface = 600
76
77     print("theFile=",theFile)
78
79     # deflection relative 0.0001 et Ascii 
80     model.exportToSTL(Part_1_doc, theFile, model.selection("SOLID","Box_1_1"),0.0001, 0.5, True,False)
81
82  #==   assert os.path.exists(theFile)
83         
84     # Check results
85     test_stl_1 = geompy.ImportSTL(theFile)
86     Props = geompy.BasicProperties(test_stl_1)
87     print("\nBasic Properties:")
88     print(" Wires length: ", Props[0])
89     print(" Surface area: ", Props[1])
90     print(" Volume      : ", Props[2])
91
92     aRefSurface = theSurface
93     aResSurface = Props[1]
94     assert (math.fabs(aResSurface - aRefSurface) < theDelta), "The volume is wrong: expected = {0}, real = {1}".format(aRefSurface, aResSurface)
95     
96     removeFile(theFile)
97
98     theSurface =  600
99     # deflection relative 0.0001 et binaire
100     model.exportToSTL(Part_1_doc, theFile, model.selection("SOLID", "Box_1_1" ),0.0001, 0.5, True,True)
101
102     # Check results
103     test_stl_1 = geompy.ImportSTL(theFile)
104     Props = geompy.BasicProperties(test_stl_1)
105     print("\nBasic Properties:")
106     print(" Wires length: ", Props[0])
107     print(" Surface area: ", Props[1])
108     print(" Volume      : ", Props[2])
109
110     aRefSurface = theSurface
111     aResSurface = Props[1]
112     assert (math.fabs(aResSurface - aRefSurface) < theDelta), "The volume is wrong: expected = {0}, real = {1}".format(aRefSurface, aResSurface)
113     
114     removeFile(theFile)
115
116     theSurface =  600
117     # deflection absolue et AScii
118     model.exportToSTL(Part_1_doc, theFile, model.selection("SOLID", "Box_1_1" ),0.0001, 0.5, False, False)
119
120     # Check results
121     test_stl_1 = geompy.ImportSTL(theFile)
122     Props = geompy.BasicProperties(test_stl_1)
123     print("\nBasic Properties:")
124     print(" Wires length: ", Props[0])
125     print(" Surface area: ", Props[1])
126     print(" Volume      : ", Props[2])
127
128     aRefSurface = theSurface
129     aResSurface = Props[1]
130     assert (math.fabs(aResSurface - aRefSurface) < theDelta), "The volume is wrong: expected = {0}, real = {1}".format(aRefSurface, aResSurface)
131
132     theSurface =  600
133     # deflection absolue et binaire
134     model.exportToSTL(Part_1_doc, theFile, model.selection("SOLID", "Box_1_1" ),0.0001, 0.5, False,True)
135
136     # Check results
137     test_stl_1 = geompy.ImportSTL(theFile)
138     Props = geompy.BasicProperties(test_stl_1)
139     print("\nBasic Properties:")
140     print(" Wires length: ", Props[0])
141     print(" Surface area: ", Props[1])
142     print(" Volume      : ", Props[2])
143
144     aRefSurface = theSurface
145     aResSurface = Props[1]
146     assert (math.fabs(aResSurface - aRefSurface) < theDelta), "The volume is wrong: expected = {0}, real = {1}".format(aRefSurface, aResSurface)
147
148     model.end()
149
150 if __name__ == '__main__':
151     with TemporaryDirectory() as tmp_dir:
152         aRealSurface = 0.00192878
153         #=========================================================================
154         # Export a shape into STL
155         #=========================================================================
156         testExportSTL(os.path.join(tmp_dir, "export.stl"), 10 ** -5, True)
157         #=========================================================================
158         # End of test
159         #=========================================================================