Salome HOME
7f26b06b2c46836c9126b54b6991ff8bfa5f76a3
[modules/shaper.git] / src / ExchangePlugin / Test / TestImportImage_2.py
1 # Copyright (C) 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 import os, inspect
21 from salome.shaper import model
22
23 from PyQt5.Qt import QApplication
24
25 import salome
26 salome.salome_init_without_session()
27 salome.salome_init(1)
28 if QApplication.instance() is None:
29   app = QApplication([])
30
31 data_dir = os.path.join(os.path.dirname(inspect.getfile(lambda: None)), "data")
32
33 from tempfile import TemporaryDirectory
34 tmp_dir = TemporaryDirectory()
35 img_file = os.path.join(tmp_dir.name, "1.jpg")
36
37 from shutil import copyfile
38 copyfile(os.path.join(data_dir, "1.jpg"), img_file)
39
40
41 model.begin()
42 partSet = model.moduleDocument()
43
44 ### Create Part
45 Part_1 = model.addPart(partSet)
46 Part_1_doc = Part_1.document()
47
48 ### Create ImportImage
49 ImportImage_1 = model.addImportImage(Part_1_doc, img_file)
50 model.do()
51 ImportImage_1.setName("drawing")
52 ImportImage_1.result().setName("drawing")
53
54 ### Create Translation
55 Translation_1 = model.addTranslation(Part_1_doc, [model.selection("FACE", "drawing")], startPoint = model.selection("VERTEX", "[drawing/Shape_1]e[drawing/Shape_4]e"), endPoint = model.selection("VERTEX", "PartSet/Origin"), keepSubResults = True)
56
57 ### Create Scale
58 Scale_1 = model.addScale(Part_1_doc, [model.selection("FACE", "drawing")] , model.selection("VERTEX", "PartSet/Origin"), 0.5, keepSubResults = True)
59
60 model.end()
61
62 #=============================================================================
63 # Tests :
64 #=============================================================================
65 model.checkResult(Scale_1, model, 1, [0], [0], [1], [4], [8])
66 r=Scale_1.defaultResult()
67 s=r.shape()
68 dim=s.computeSize()
69 dim=dim[1:]
70 dx=abs(dim[3]-dim[0])
71 dy=abs(dim[4]-dim[1])
72 dz=abs(dim[5]-dim[2])
73 tol=1e-06
74 assert(abs(dx-400) <= tol)
75 assert(abs(dy-258.5) <= tol)
76 assert(abs(dz-0) <= tol)
77
78 #=============================================================================
79 # Change the image :
80 #=============================================================================
81 os.rename(img_file, os.path.join(tmp_dir.name, "1_new.jpg"))
82
83 model.begin()
84 Scale_1.setScaleFactor(0.7)
85 Scale_1.execute()
86 model.do()
87 model.end()
88
89 #=============================================================================
90 # Tests :
91 #=============================================================================
92 model.checkResult(Scale_1, model, 1, [0], [0], [1], [4], [8])
93 r=Scale_1.defaultResult()
94 s=r.shape()
95 dim=s.computeSize()
96 dim=dim[1:]
97 dx=abs(dim[3]-dim[0])
98 dy=abs(dim[4]-dim[1])
99 dz=abs(dim[5]-dim[2])
100 tol=1e-06
101 assert(abs(dx-560) <= tol)
102 assert(abs(dy-361.9) <= tol)
103 assert(abs(dz-0) <= tol)
104
105
106 # Close SALOME GUI
107 import salome_utils
108 import killSalomeWithPort
109
110 port = salome_utils.getPortNumber()
111 killSalomeWithPort.killMyPort(port)