Salome HOME
24b16abc77b91af80b127069e9de3aea119861ba
[modules/shaper.git] / src / ConnectorAPI / Test / TestExportToGEOMAllGroupsAndFields.py
1 # Copyright (C) 2014-2022  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
21 import tempfile
22
23 import sys
24 import salome
25
26 salome.standalone()
27 salome.salome_init()
28 theStudy = salome.myStudy
29
30 from salome.shaper import model
31
32 from salome.geom import geomBuilder
33 geompy = geomBuilder.New()
34
35 ## Get the last object published in the GEOM section of the object browser
36 def getLastGEOMShape():
37   sb = salome.myStudy.NewBuilder()
38   comp = salome.myStudy.FindComponent("GEOM")
39   obj = None
40   if comp:
41     iterator = salome.myStudy.NewChildIterator( comp )
42     sobj = None
43     while iterator.More():
44       sobj = iterator.Value()
45       iterator.Next()
46     if sobj:
47       obj = sobj.GetObject()
48   else:
49     raise Exception("GEOM component not found.")
50   return obj
51
52 ## Get the sub-object i of an object in the object browser
53 # Numerotation starts at 1
54 def getSubObject(obj, i):
55   ok, sub_sobj = salome.ObjectToSObject(obj).FindSubObject(i)
56   if not ok:
57     raise Exception("No child found at %i for %s"%(i, obj.GetName()))
58   sub_obj = sub_sobj.GetObject()
59   return sub_obj
60
61 def dumpShaper(fileName):
62   model.begin()
63   dump=model.moduleDocument().addFeature("Dump")
64   dump.string("file_path").setValue(fileName)
65   dump.string("file_format").setValue("py")
66   dump.boolean("topological_naming").setValue(True)
67   dump.boolean("geometric_selection").setValue(False)
68   dump.boolean("weak_naming").setValue(False)
69   model.do()
70   model.end()
71   pass
72
73 def testGroupsAndFieldsExportToGEOM():
74   model.begin()
75   partSet = model.moduleDocument()
76   Part_1 = model.addPart(partSet)
77   Part_1_doc = Part_1.document()
78   Box_1 = model.addBox(Part_1_doc, 10, 10, 10)
79   Box_2 = model.addBox(Part_1_doc, 20, 20, 20)
80   Translation_1 = model.addTranslation(Part_1_doc, [model.selection("SOLID", "Box_1_1")], model.selection("EDGE", "PartSet/OX"), -10)
81   Translation_1.result().setName("Translation_1_1")
82   Partition_1 = model.addPartition(Part_1_doc, [model.selection("SOLID", "Translation_1_1"), model.selection("SOLID", "Box_2_1")])
83   Partition_1.result().setName("Partition_1_1")
84   Box_3 = model.addBox(Part_1_doc, 10, 10, 10)
85   Translation_2 = model.addTranslation(Part_1_doc, [model.selection("SOLID", "Box_3_1")], model.selection("EDGE", "PartSet/OX"), 20)
86   Translation_2.result().setName("Translation_2_1")
87   Group_1_objects = [model.selection("FACE", "Translation_1_1/MF:Translated&Box_1_1/Top"), model.selection("FACE", "Box_2_1/Top"), model.selection("FACE", "Translation_2_1/MF:Translated&Box_3_1/Top")]
88   Group_1 = model.addGroup(Part_1_doc, Group_1_objects)
89   Group_1.result().setName("faces_top")
90   Group_2_objects = [model.selection("EDGE", "[Partition_1_1_1/Modified_Face&Box_1_1/Left][Partition_1_1_1/Modified_Face&Box_1_1/Bottom]"), model.selection("EDGE", "[Partition_1_1_2/Modified_Face&Box_2_1/Left][Partition_1_1_2/Modified_Face&Box_2_1/Bottom]"), model.selection("EDGE", "[Translation_2_1/MF:Translated&Box_3_1/Left][Translation_2_1/MF:Translated&Box_3_1/Bottom]")]
91   Group_2 = model.addGroup(Part_1_doc, Group_2_objects)
92   Group_2.result().setName("edges_x")
93   Group_3_objects = [model.selection("VERTEX", "[Translation_2_1/MF:Translated&Box_3_1/Front][Translation_2_1/MF:Translated&Box_3_1/Left][Translation_2_1/MF:Translated&Box_3_1/Bottom]"), model.selection("VERTEX", "[Translation_2_1/MF:Translated&Box_3_1/Front][Translation_2_1/MF:Translated&Box_3_1/Right][Translation_2_1/MF:Translated&Box_3_1/Bottom]"), model.selection("VERTEX", "[Translation_2_1/MF:Translated&Box_3_1/Back][Translation_2_1/MF:Translated&Box_3_1/Right][Translation_2_1/MF:Translated&Box_3_1/Bottom]"), model.selection("VERTEX", "[Box_2_1/Front][Box_2_1/Right][Partition_1_1_2/Modified_Face&Box_2_1/Bottom]"), model.selection("VERTEX", "[(Partition_1_1_2/Modified_Face&Box_2_1/Back&Box_1_1/Front)(Partition_1_1_2/Modified_Face&Box_2_1/Left)(Box_2_1/Right)(Partition_1_1_2/Modified_Face&Box_2_1/Bottom)(Box_2_1/Top)][Box_2_1/Right][Partition_1_1_2/Modified_Face&Box_2_1/Bottom]"), model.selection("VERTEX", "[Partition_1_1_1/Modified_Face&Box_2_1/Back&Box_1_1/Front][Translation_1_1/MF:Translated&Box_1_1/Right][Partition_1_1_1/Modified_Face&Box_1_1/Bottom]"), model.selection("VERTEX", "[Translation_1_1/MF:Translated&Box_1_1/Back][Translation_1_1/MF:Translated&Box_1_1/Right][Partition_1_1_1/Modified_Face&Box_1_1/Bottom]"), model.selection("VERTEX", "[Translation_1_1/MF:Translated&Box_1_1/Back][Partition_1_1_1/Modified_Face&Box_1_1/Left][Partition_1_1_1/Modified_Face&Box_1_1/Bottom]")]
94   Group_3 = model.addGroup(Part_1_doc, Group_3_objects)
95   Group_3.result().setName("vertices_bottom")
96   Group_4 = model.addGroup(Part_1_doc, [model.selection("SOLID", "Partition_1_1_1"), model.selection("SOLID", "Translation_2_1")])
97   Group_4.result().setName("solids_small")
98   Field_1_objects = [model.selection("SOLID", "Partition_1_1_1"), model.selection("SOLID", "Partition_1_1_2"), model.selection("SOLID", "Translation_2_1")]
99   Field_1 = model.addField(Part_1_doc, 1, "DOUBLE", 1, ["Comp 1"], Field_1_objects)
100   Field_1.result().setName("Field_solids")
101   Field_1.addStep(0, 0, [[0], [1], [2], [3]])
102   Field_2_objects = [model.selection("FACE", "Partition_1_1_1/Modified_Face&Box_1_1/Bottom"), model.selection("FACE", "Partition_1_1_2/Modified_Face&Box_2_1/Bottom"), model.selection("FACE", "Translation_2_1/MF:Translated&Box_3_1/Bottom"), model.selection("FACE", "Translation_1_1/MF:Translated&Box_1_1/Top"), model.selection("FACE", "Box_2_1/Top"), model.selection("FACE", "Translation_2_1/MF:Translated&Box_3_1/Top")]
103   Field_2 = model.addField(Part_1_doc, 1, "DOUBLE", 1, ["Comp 1"], Field_2_objects)
104   Field_2.result().setName("Field_faces")
105   Field_2.addStep(0, 0, [[0], [1], [1], [1], [2], [2], [2]])
106   Field_3_objects = [model.selection("EDGE", "[Translation_2_1/MF:Translated&Box_3_1/Front][Translation_2_1/MF:Translated&Box_3_1/Left]"), model.selection("EDGE", "[Translation_2_1/MF:Translated&Box_3_1/Left][Translation_2_1/MF:Translated&Box_3_1/Bottom]"), model.selection("EDGE", "[Partition_1_1_2/Modified_Face&Box_2_1/Left][Partition_1_1_2/Modified_Face&Box_2_1/Bottom]"), model.selection("EDGE", "[Partition_1_1_1/Modified_Face&Box_1_1/Left][Partition_1_1_1/Modified_Face&Box_1_1/Bottom]"), model.selection("EDGE", "[Translation_1_1/MF:Translated&Box_1_1/Back][Partition_1_1_1/Modified_Face&Box_1_1/Left]"), model.selection("EDGE", "[Translation_2_1/MF:Translated&Box_3_1/Front][Translation_2_1/MF:Translated&Box_3_1/Right]"), model.selection("EDGE", "[Translation_1_1/MF:Translated&Box_1_1/Back][Translation_1_1/MF:Translated&Box_1_1/Right]"), model.selection("EDGE", "[Box_2_1/Front][Box_2_1/Right]"), model.selection("EDGE", "[Box_2_1/Right][Box_2_1/Top]"), model.selection("EDGE", "[(Partition_1_1_2/Modified_Face&Box_2_1/Back&Box_1_1/Front)(Partition_1_1_2/Modified_Face&Box_2_1/Left)(Box_2_1/Right)(Partition_1_1_2/Modified_Face&Box_2_1/Bottom)(Box_2_1/Top)][Box_2_1/Right]"), model.selection("EDGE", "[Box_2_1/Right][Partition_1_1_2/Modified_Face&Box_2_1/Bottom]")]
107   Field_3 = model.addField(Part_1_doc, 1, "DOUBLE", 1, ["Comp 1"], Field_3_objects)
108   Field_3.result().setName("Field_edges")
109   Field_3.addStep(0, 0, [[0], [1], [1], [1], [2], [2], [2], [2], [3], [3], [3], [3]])
110   Field_4_objects = [model.selection("VERTEX", "[Translation_1_1/MF:Translated&Box_1_1/Back][Partition_1_1_1/Modified_Face&Box_1_1/Left][Partition_1_1_1/Modified_Face&Box_1_1/Bottom]"), model.selection("VERTEX", "[Translation_1_1/MF:Translated&Box_1_1/Back][Translation_1_1/MF:Translated&Box_1_1/Right][Partition_1_1_1/Modified_Face&Box_1_1/Bottom]"), model.selection("VERTEX", "[Partition_1_1_1/Modified_Face&Box_2_1/Back&Box_1_1/Front][Translation_1_1/MF:Translated&Box_1_1/Right][Partition_1_1_1/Modified_Face&Box_1_1/Bottom]"), model.selection("VERTEX", "[(Partition_1_1_2/Modified_Face&Box_2_1/Back&Box_1_1/Front)(Partition_1_1_2/Modified_Face&Box_2_1/Left)(Box_2_1/Right)(Partition_1_1_2/Modified_Face&Box_2_1/Bottom)(Box_2_1/Top)][Box_2_1/Right][Partition_1_1_2/Modified_Face&Box_2_1/Bottom]"), model.selection("VERTEX", "[Box_2_1/Front][Box_2_1/Right][Partition_1_1_2/Modified_Face&Box_2_1/Bottom]"), model.selection("VERTEX", "[Translation_2_1/MF:Translated&Box_3_1/Back][Translation_2_1/MF:Translated&Box_3_1/Right][Translation_2_1/MF:Translated&Box_3_1/Bottom]"), model.selection("VERTEX", "[Translation_2_1/MF:Translated&Box_3_1/Front][Translation_2_1/MF:Translated&Box_3_1/Right][Translation_2_1/MF:Translated&Box_3_1/Bottom]"), model.selection("VERTEX", "[Translation_2_1/MF:Translated&Box_3_1/Front][Translation_2_1/MF:Translated&Box_3_1/Left][Translation_2_1/MF:Translated&Box_3_1/Bottom]"), model.selection("VERTEX", "[Translation_1_1/MF:Translated&Box_1_1/Back][Partition_1_1_1/Modified_Face&Box_1_1/Left][Translation_1_1/MF:Translated&Box_1_1/Top]"), model.selection("VERTEX", "[Translation_1_1/MF:Translated&Box_1_1/Back][Translation_1_1/MF:Translated&Box_1_1/Right][Translation_1_1/MF:Translated&Box_1_1/Top]"), model.selection("VERTEX", "[(Partition_1_1_2/Modified_Face&Box_2_1/Back&Box_1_1/Front)(Partition_1_1_2/Modified_Face&Box_2_1/Left)(Box_2_1/Right)(Partition_1_1_2/Modified_Face&Box_2_1/Bottom)(Box_2_1/Top)][Partition_1_1_2/Modified_Face&Box_2_1/Left][Box_2_1/Top]"), model.selection("VERTEX", "[(Partition_1_1_2/Modified_Face&Box_2_1/Back&Box_1_1/Front)(Partition_1_1_2/Modified_Face&Box_2_1/Left)(Box_2_1/Right)(Partition_1_1_2/Modified_Face&Box_2_1/Bottom)(Box_2_1/Top)][Box_2_1/Right][Box_2_1/Top]"), model.selection("VERTEX", "[Box_2_1/Front][Partition_1_1_2/Modified_Face&Box_2_1/Left][Box_2_1/Top]"), model.selection("VERTEX", "[Box_2_1/Front][Box_2_1/Right][Box_2_1/Top]"), model.selection("VERTEX", "[Translation_2_1/MF:Translated&Box_3_1/Front][Translation_2_1/MF:Translated&Box_3_1/Left][Translation_2_1/MF:Translated&Box_3_1/Top]"), model.selection("VERTEX", "[Translation_2_1/MF:Translated&Box_3_1/Front][Translation_2_1/MF:Translated&Box_3_1/Right][Translation_2_1/MF:Translated&Box_3_1/Top]")]
111   Field_4 = model.addField(Part_1_doc, 1, "DOUBLE", 1, ["Comp 1"], Field_4_objects)
112   Field_4.result().setName("Field_vertices")
113   Field_4.addStep(0, 0, [[0], [1], [1], [1], [1], [1], [1], [1], [1], [2], [2], [2], [2], [3], [3], [3], [3]])
114   model.do()
115   model.exportToGEOM(Part_1_doc)
116   model.end()
117
118 # check the groups content by the coordinates of a point on its sub-shapes
119 def checkGroupByCoords(group, coords, tolerance=1e-7):
120   for coord in coords:
121     p = geompy.MakeVertex(*coord)
122     assert geompy.MinDistance(group, p) < tolerance
123   pass
124
125 ## Check the result imported in GEOM
126 def checkResultInGEOM():
127   geomObject_1 = getLastGEOMShape() # the second, translation result
128
129   group_1_GEOM = getSubObject(geomObject_1, 1)
130   assert group_1_GEOM.GetName() == 'faces_top'
131   assert geompy.NumberOfFaces(group_1_GEOM) == 1 # 2 faces are in the first result, only one here
132
133   # coordinates of the barycenters of the faces of Group_1
134   coords_1 = [[25, 5, 10]]
135   checkGroupByCoords(group_1_GEOM, coords_1)
136
137   group_2_GEOM = getSubObject(geomObject_1, 2)
138   assert group_2_GEOM.GetName() == 'edges_x'
139   assert geompy.NumberOfEdges(group_2_GEOM) == 1 # 2 edges are in the first result, only one here
140
141   # coordinates of the barycenters of the edges of Group_2
142   coords_2 = [[25, 0, 0]]
143   checkGroupByCoords(group_2_GEOM, coords_2)
144
145   group_3_GEOM = getSubObject(geomObject_1, 3)
146   assert group_3_GEOM.GetName() == 'vertices_bottom'
147   assert geompy.NumberOfSubShapes(group_3_GEOM, geompy.ShapeType["VERTEX"]) == 3 # 3 of 8
148
149   # coordinates of the points of of Group_3
150   coords_3 = [[20, 10, 0], [30, 10, 0], [30, 0, 0]]
151   checkGroupByCoords(group_3_GEOM, coords_3)
152
153   group_4_GEOM = getSubObject(geomObject_1, 4)
154   assert group_4_GEOM.GetName() == 'solids_small'
155   assert geompy.NumberOfSolids(group_4_GEOM) == 1 # 1 of 2
156
157   # coordinates of the barycenters of the solids of Group_4
158   coords_4 = [[25, 5, 5]]
159   checkGroupByCoords(group_4_GEOM, coords_4)
160
161   field_1_GEOM = getSubObject(geomObject_1, 5)
162   assert field_1_GEOM.GetName() == 'Field_solids'
163   assert field_1_GEOM.GetStep(1).GetValues() == [3.0]
164
165   field_2_GEOM = getSubObject(geomObject_1, 6)
166   assert field_2_GEOM.GetName() == 'Field_faces'
167   assert field_2_GEOM.GetStep(1).GetValues() == [0.0, 0.0, 0.0, 0.0, 1.0, 2.0]
168
169   field_3_GEOM = getSubObject(geomObject_1, 7)
170   assert field_3_GEOM.GetName() == 'Field_edges'
171   assert field_3_GEOM.GetStep(1).GetValues() == [0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 2.0, 0.0, 1.0, 0.0, 0.0, 0.0]
172
173   field_4_GEOM = getSubObject(geomObject_1, 8)
174   assert field_4_GEOM.GetName() == 'Field_vertices'
175   assert field_4_GEOM.GetStep(1).GetValues() == [0.0, 0.0, 0.0, 1.0, 3.0, 1.0, 3.0, 1.0]
176
177   pass
178
179 def checkDump():
180   # Dump the salome study (only CORBA modules, SHAPER dump is not in it)
181   with tempfile.TemporaryDirectory() as tempdir:
182     dumpFileGeomBase = "dump_test_geom"
183     dumpFileGeom = os.path.join(tempdir, "%s.py"%dumpFileGeomBase)
184     salome.myStudy.DumpStudy(tempdir, dumpFileGeomBase, True, False)
185
186     # Dump SHAPER
187     dumpFileShaper = os.path.join(tempdir, "dump_test_shaper.py")
188     dumpShaper(dumpFileShaper)
189
190     # Load SHAPER dump
191     exec(compile(open(dumpFileShaper).read(), dumpFileShaper, 'exec'))
192
193     # Load GEOM dump
194     exec(compile(open(dumpFileGeom).read(), dumpFileGeom, 'exec'))
195
196     # Clean files
197     files = [dumpFileGeom, dumpFileShaper]
198   pass
199
200 if __name__ == '__main__':
201   # create 3 boxes with groups and fields
202   testGroupsAndFieldsExportToGEOM()
203   # check the result in GEOM
204   checkResultInGEOM()
205   # check that dump produces no error and can be reloaded
206   checkDump()
207   # check the result of the dump
208   checkResultInGEOM()