Salome HOME
bos #26449: SHAPER: save imported images
[modules/shaper.git] / src / ExchangePlugin / Test / TestImportImage_3.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 from tempfile import TemporaryDirectory
32 from ModelAPI import *
33
34 data_dir = os.path.join(os.path.dirname(inspect.getfile(lambda: None)), "data")
35
36 model.begin()
37 partSet = model.moduleDocument()
38
39 ### Create Part
40 Part_1 = model.addPart(partSet)
41 Part_1_doc = Part_1.document()
42
43 ### Create ImportImage
44 ImportImage_1 = model.addImportImage(Part_1_doc, os.path.join(data_dir, "1.jpg"))
45 model.do()
46 ImportImage_1.setName("drawing")
47 ImportImage_1.result().setName("drawing")
48
49 ### Create Translation
50 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)
51 Translation_1.setName("translation")
52
53 ### Create Scale
54 Scale_1 = model.addScale(Part_1_doc, [model.selection("FACE", "drawing")] , model.selection("VERTEX", "PartSet/Origin"), 0.5, keepSubResults = True)
55 Scale_1.setName("scale")
56
57 model.end()
58
59 #=============================================================================
60 # Tests :
61 #=============================================================================
62 model.checkResult(Scale_1, model, 1, [0], [0], [1], [4], [8])
63 r=Scale_1.defaultResult()
64 s=r.shape()
65 dim=s.computeSize()
66 dim=dim[1:]
67 dx=abs(dim[3]-dim[0])
68 dy=abs(dim[4]-dim[1])
69 dz=abs(dim[5]-dim[2])
70 tol=1e-06
71 assert(abs(dx-400) <= tol)
72 assert(abs(dy-258.5) <= tol)
73 assert(abs(dz-0) <= tol)
74 assert(ImportImage_1.result().resultSubShapePair()[0].hasTexture())
75 assert(Translation_1.result().resultSubShapePair()[0].hasTexture())
76 assert(Scale_1.result().resultSubShapePair()[0].hasTexture())
77
78 aDr = objectToFeature(Part_1_doc.objectByName("Features", "drawing"))
79 assert(aDr.firstResult().hasTexture())
80
81 # check save/load document with an image
82 with TemporaryDirectory() as tmp_dir:
83   aSession = ModelAPI_Session.get()
84   aFiles = StringList()
85   aSession.save(tmp_dir, aFiles)
86   aSession.closeAll()
87   assert(aSession.load(tmp_dir))
88   model.begin()
89   partSet = model.moduleDocument()
90   assert(partSet.size("Features") == 1)
91   aPart = objectToFeature(partSet.object("Features", 0))
92   aPartResult = modelAPI_ResultPart(aPart.results()[0])
93   aPartResult.activate()
94   aPartDoc = aPartResult.partDoc()
95   aSession.setActiveDocument(aPartDoc, True)
96   model.end()
97   aDr = objectToFeature(aPartDoc.objectByName("Features", "drawing"))
98   aTr = objectToFeature(aPartDoc.objectByName("Features", "translation"))
99   aSc = objectToFeature(aPartDoc.objectByName("Features", "scale"))
100   assert(aDr.firstResult().hasTexture())
101   assert(aTr.firstResult().hasTexture())
102   assert(aSc.firstResult().hasTexture())
103
104 # Close SALOME GUI
105 import salome_utils
106 import subprocess
107 port = salome_utils.getPortNumber()
108 proc = subprocess.Popen(["killSalomeWithPort.py", "{}".format(port)])