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