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