1 ## Copyright (C) 2014-2017 CEA/DEN, EDF R&D
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.
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.
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
17 ## See http:##www.salome-platform.org/ or
18 ## email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
20 # -*- coding: utf-8 -*-
29 theStudy = salome.myStudy
31 from salome.shaper import model
33 from salome.geom import geomBuilder
34 geompy = geomBuilder.New(salome.myStudy)
36 ## Get the last object published in the GEOM section of the object browser
37 def getLastGEOMShape():
38 sb = salome.myStudy.NewBuilder()
39 comp = salome.myStudy.FindComponent("GEOM")
42 iterator = salome.myStudy.NewChildIterator( comp )
44 while iterator.More():
45 sobj = iterator.Value()
48 obj = sobj.GetObject()
50 raise Exception("GEOM component not found.")
53 ## Get the sub-object i of an object in the object browser
54 # Numerotation starts at 1
55 def getSubObject(obj, i):
56 ok, sub_sobj = salome.ObjectToSObject(obj).FindSubObject(i)
58 raise Exception("No child found at %i for %s"%(i, obj.GetName()))
59 sub_obj = sub_sobj.GetObject()
62 def dumpShaper(fileName):
64 dump=model.moduleDocument().addFeature("Dump")
65 dump.string("file_path").setValue(fileName)
66 dump.string("file_format").setValue("py")
71 def getTmpFileName(ext):
72 tempdir = tempfile.gettempdir()
73 tmp_file = tempfile.NamedTemporaryFile(suffix=".%s"%ext, prefix='shaper_', dir=tempdir, delete=False)
74 tmp_filename = tmp_file.name
77 def testGroupsAndFieldsExportToGEOM():
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 Translation_1.result().setName("Translation_1_1")
86 Partition_1 = model.addPartition(Part_1_doc, [model.selection("SOLID", "Translation_1_1"), model.selection("SOLID", "Box_2_1")])
87 Partition_1.result().setName("Partition_1_1")
88 Box_3 = model.addBox(Part_1_doc, 10, 10, 10)
89 Translation_2 = model.addTranslation(Part_1_doc, [model.selection("SOLID", "Box_3_1")], model.selection("EDGE", "PartSet/OX"), 20)
90 Translation_2.result().setName("Translation_2_1")
91 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")])
92 Group_1.result().setName("faces_top")
93 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")])
94 Group_2.result().setName("edges_x")
95 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_1/Modified_Face_2_1"), model.selection("VERTEX", "Translation_2_1/Translated_Face_6&Translation_2_1/Translated_Face_3&Translation_2_1/Translated_Face_2")])
96 Group_3.result().setName("vertices_bottom")
97 Group_4 = model.addGroup(Part_1_doc, [model.selection("SOLID", "Partition_1_1_1"), model.selection("SOLID", "Translation_2_1")])
98 Group_4.result().setName("solids_small")
99 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")])
100 Field_1.result().setName("Field_solids")
101 Field_1.addStep(0, 0, [[0], [1], [2], [3]])
102 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")])
103 Field_2.result().setName("Field_faces")
104 Field_2.addStep(0, 0, [[0], [1], [1], [1], [2], [2], [2]])
105 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")])
106 Field_3.result().setName("Field_edges")
107 Field_3.addStep(0, 0, [[0], [1], [1], [1], [2], [2], [2], [2], [3], [3], [3], [3]])
108 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_1/Modified_Face_2_1"), 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")])
109 Field_4.result().setName("Field_vertices")
110 Field_4.addStep(0, 0, [[0], [1], [1], [1], [1], [1], [1], [1], [1], [2], [2], [2], [2], [3], [3], [3], [3]])
111 model.exportToGEOM(Part_1_doc)
115 # check the groups content by the coordinates of a point on its sub-shapes
116 def checkGroupByCoords(group, coords, tolerance=1e-7):
118 p = geompy.MakeVertex(*coord)
119 assert geompy.MinDistance(group, p) < tolerance
122 ## Check the result imported in GEOM
123 def checkResultInGEOM():
124 geomObject_1 = getLastGEOMShape()
126 group_1_GEOM = getSubObject(geomObject_1, 1)
127 assert group_1_GEOM.GetName() == 'faces_top'
128 assert geompy.NumberOfFaces(group_1_GEOM) == 3
130 # coordinates of the barycenters of the faces of Group_1
131 coords_1 = [[-5, 5, 10], [10, 10, 20], [25, 5, 10]]
132 checkGroupByCoords(group_1_GEOM, coords_1)
134 group_2_GEOM = getSubObject(geomObject_1, 2)
135 assert group_2_GEOM.GetName() == 'edges_x'
136 assert geompy.NumberOfEdges(group_2_GEOM) == 3
138 # coordinates of the barycenters of the edges of Group_2
139 coords_2 = [[-5, 0, 0], [10, 0, 0], [25, 0, 0]]
140 checkGroupByCoords(group_2_GEOM, coords_2)
142 group_3_GEOM = getSubObject(geomObject_1, 3)
143 assert group_3_GEOM.GetName() == 'vertices_bottom'
144 assert geompy.NumberOfSubShapes(group_3_GEOM, geompy.ShapeType["VERTEX"]) == 8
146 # coordinates of the points of of Group_3
147 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]]
148 checkGroupByCoords(group_3_GEOM, coords_3)
150 group_4_GEOM = getSubObject(geomObject_1, 4)
151 assert group_4_GEOM.GetName() == 'solids_small'
152 assert geompy.NumberOfSolids(group_4_GEOM) == 2
154 # coordinates of the barycenters of the solids of Group_4
155 coords_4 = [[-5, 5, 5], [25, 5, 5]]
156 checkGroupByCoords(group_4_GEOM, coords_4)
158 field_1_GEOM = getSubObject(geomObject_1, 5)
159 assert field_1_GEOM.GetName() == 'Field_solids'
160 assert field_1_GEOM.GetStep(0).GetValues() == [1.0, 2.0, 3.0]
162 field_2_GEOM = getSubObject(geomObject_1, 6)
163 assert field_2_GEOM.GetName() == 'Field_faces'
164 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]
166 field_3_GEOM = getSubObject(geomObject_1, 7)
167 assert field_3_GEOM.GetName() == 'Field_edges'
168 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]
170 field_4_GEOM = getSubObject(geomObject_1, 8)
171 assert field_4_GEOM.GetName() == 'Field_vertices'
172 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]
177 # Dump the salome study (only CORBA modules, SHAPER dump is not in it)
178 tempdir = tempfile.gettempdir()
179 dumpFileGeomBase = "dump_test_geom"
180 dumpFileGeom = os.path.join(tempdir, "%s.py"%dumpFileGeomBase)
181 salome.myStudy.DumpStudy(tempdir, dumpFileGeomBase, True, False)
184 dumpFileShaper = os.path.join(tempdir, "dump_test_shaper.py")
185 dumpShaper(dumpFileShaper)
188 execfile(dumpFileShaper)
191 execfile(dumpFileGeom)
194 files = [dumpFileGeom, dumpFileShaper]
200 if __name__ == '__main__':
201 # create 3 boxes with groups and fields
202 testGroupsAndFieldsExportToGEOM()
203 # check the result in GEOM
205 # check that dump produces no error and can be reloaded
207 # check the result of the dump