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