Salome HOME
Merge branch 'master' of https://codev-tuleap.cea.fr/plugins/git/salome/shaper
[modules/shaper.git] / src / ConnectorAPI / Test / TestExportToGEOM.py
1 ## Copyright (C) 2014-2017  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
18 ## email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 ##
20
21 import salome
22 from salome.shaper import model
23
24 from salome.geom import geomBuilder
25
26 import os
27 import tempfile
28
29 salome.salome_init(1)
30 geompy = geomBuilder.New()
31
32 ## Get the last object published in the GEOM section of the object browser
33 def getLastGEOMShape():
34   sb = salome.myStudy.NewBuilder()
35   comp = salome.myStudy.FindComponent("GEOM")
36   obj = None
37   if comp:
38     iterator = salome.myStudy.NewChildIterator( comp )
39     sobj = None
40     while iterator.More():
41       sobj = iterator.Value()
42       iterator.Next()
43     if sobj:
44       obj = sobj.GetObject()
45   else:
46     raise Exception("GEOM component not found.")
47   return obj
48
49 ## Get the sub-object i of an object in the object browser
50 # Numerotation starts at 1
51 def getSubObject(obj, i):
52   ok, sub_sobj = salome.ObjectToSObject(obj).FindSubObject(i)
53   if not ok:
54     raise Exception("No child found at %i for %s"%(i, obj.GetName()))
55   sub_obj = sub_sobj.GetObject()
56   return sub_obj
57
58 def dumpShaper(fileName):
59   model.begin()
60   dump=model.moduleDocument().addFeature("Dump")
61   dump.string("file_path").setValue(fileName)
62   dump.string("file_format").setValue("py")
63   dump.string("selection_type").setValue("topological_naming")
64   model.do()
65   model.end()
66   pass
67
68 def getTmpFileName(ext):
69   tempdir = tempfile.gettempdir()
70   tmp_file = tempfile.NamedTemporaryFile(suffix=".%s"%ext, prefix='shaper_', dir=tempdir, delete=False)
71   tmp_filename = tmp_file.name
72   return tmp_filename
73
74 # Create 2 boxes
75 # Create a group of faces
76 # Create a field of faces
77 # exportToGEOM
78 # Check the result
79 # Check the dump
80 def testSeveralExportsToGEOM():
81
82   model.begin()
83   partSet = model.moduleDocument()
84   Part_1 = model.addPart(partSet)
85   Part_1_doc = Part_1.document()
86   Box_1 = model.addBox(Part_1_doc, 10, 10, 10)
87   Box_2 = model.addBox(Part_1_doc, 20, 20, 20)
88   Translation_1 = model.addTranslation(Part_1_doc, [model.selection("SOLID", "Box_1_1")], model.selection("EDGE", "PartSet/OX"), -10)
89   Partition_1 = model.addPartition(Part_1_doc, [model.selection("SOLID", "Box_1_1"), model.selection("SOLID", "Box_2_1")])
90   Group_1 = model.addGroup(Part_1_doc, [model.selection("FACE", "Partition_1_1_1/Modified_Face_1_2"), model.selection("FACE", "Box_2_1/Top")])
91   model.do()
92   model.end()
93
94   # First export to GEOM
95   model.exportToGEOM(Part_1_doc)
96
97   # Check that the GEOM object has 1 compsolid and 2 solids
98   geomObject_1 = getLastGEOMShape()
99   assert geompy.NumberOfSubShapes(geomObject_1, geompy.ShapeType["COMPSOLID"]) == 1
100   assert geompy.NumberOfSolids(geomObject_1) == 2
101
102   # Check that the group has 2 faces
103   geomGroup_1 = getSubObject(geomObject_1, 1)
104   assert geompy.NumberOfFaces(geomGroup_1) == 2
105
106   # Add a third box
107   Box_3 = model.addBox(Part_1_doc, 10, 10, 10)
108   Translation_2 = model.addTranslation(Part_1_doc, [model.selection("SOLID", "Box_3_1")], model.selection("EDGE", "PartSet/OX"), 20)
109
110   # Second export to GEOM
111   model.exportToGEOM(Part_1_doc)
112
113   # Check that the GEOM object has 3 solids
114   geomObject_2 = getLastGEOMShape()
115   assert geompy.NumberOfSolids(geomObject_2) == 3
116
117   # Dump the salome study (only CORBA modules, SHAPER dump is not in it)
118   tempdir = tempfile.gettempdir()
119   dumpFileGeomBase = "dump_test_geom"
120   dumpFileGeom = os.path.join(tempdir, "%s.py"%dumpFileGeomBase)
121   salome.myStudy.DumpStudy(tempdir, dumpFileGeomBase, True, False)
122
123   # Dump SHAPER
124   dumpFileShaper = os.path.join(tempdir, "dump_test_shaper.py")
125   dumpShaper(dumpFileShaper)
126
127   # Load SHAPER dump
128   exec(compile(open(dumpFileShaper).read(), dumpFileShaper, 'exec'))
129
130   # Load GEOM dump
131   exec(compile(open(dumpFileGeom).read(), dumpFileGeom, 'exec'))
132
133   # Clean files
134   files = [dumpFileGeom, dumpFileShaper]
135   for f in files:
136     os.remove(f)
137
138   pass
139
140
141
142 if __name__ == '__main__':
143   testSeveralExportsToGEOM()