Salome HOME
af662c831dd7149d7d298c857f10c271fdb33ae6
[modules/shaper.git] / src / ConnectorAPI / Test / TestExportToGEOMWholeResult.py
1 # Copyright (C) 2014-2024  CEA, EDF
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
24 import os
25 import tempfile
26
27 salome.standalone()
28 from salome.geom import geomBuilder
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.boolean("topological_naming").setValue(True)
64   dump.boolean("geometric_selection").setValue(False)
65   dump.boolean("weak_naming").setValue(False)
66   model.do()
67   model.end()
68   pass
69
70 # Create 2 boxes
71 # Create a group of all edges of the first box (whole result)
72 # Create a group of all faces of the whole partition result (whole result)
73 # exportToGEOM
74 # Check the result
75 # Check the dump
76 def testExportToGEOM():
77
78   model.begin()
79   partSet = model.moduleDocument()
80   Part_1 = model.addPart(partSet)
81   Part_1_doc = Part_1.document()
82   Box_1 = model.addBox(Part_1_doc, 10, 10, 10)
83   Box_2 = model.addBox(Part_1_doc, 20, 20, 20)
84   Translation_1 = model.addTranslation(Part_1_doc, [model.selection("SOLID", "Box_1_1")], model.selection("EDGE", "PartSet/OX"), -10)
85   Partition_1 = model.addPartition(Part_1_doc, [model.selection("SOLID", "Translation_1_1"), model.selection("SOLID", "Box_2_1")])
86   Group_1 = model.addGroup(Part_1_doc, "EDGE", [model.selection("COMPSOLID", "Partition_1_1_1")])
87   Group_2 = model.addGroup(Part_1_doc, "FACE", [model.selection("COMPSOLID", "Partition_1_1")])
88   model.do()
89
90   model.exportToGEOM(Part_1_doc)
91   model.end()
92
93   # Check that the GEOM object has 1 compsolid and 2 solids
94   geomObject_1 = getLastGEOMShape()
95   assert geompy.NumberOfSubShapes(geomObject_1, geompy.ShapeType["COMPSOLID"]) == 1
96   assert geompy.NumberOfSolids(geomObject_1) == 2
97
98   # Check that the group has 12 edges
99   geomGroup_1 = getSubObject(geomObject_1, 1)
100   assert geompy.NumberOfEdges(geomGroup_1) == 12
101
102   # Check that the group has 6+6 faces
103   geomGroup_2 = getSubObject(geomObject_1, 2)
104   assert geompy.NumberOfFaces(geomGroup_2) == 12
105   
106   with tempfile.TemporaryDirectory() as tempdir:
107     # Dump the salome study (only CORBA modules, SHAPER dump is not in it)
108     dumpFileGeomBase = "dump_test_geom"
109     dumpFileGeom = os.path.join(tempdir, "%s.py"%dumpFileGeomBase)
110     salome.myStudy.DumpStudy(tempdir, dumpFileGeomBase, True, False)
111
112     # Dump SHAPER
113     dumpFileShaper = os.path.join(tempdir, "dump_test_shaper.py")
114     dumpShaper(dumpFileShaper)
115
116     # Load SHAPER dump
117     exec(compile(open(dumpFileShaper).read(), dumpFileShaper, 'exec'))
118
119     # Load GEOM dump
120     exec(compile(open(dumpFileGeom).read(), dumpFileGeom, 'exec'))
121
122     # Clean files
123     files = [dumpFileGeom, dumpFileShaper]
124
125   pass
126
127 if __name__ == '__main__':
128   testExportToGEOM()