1 # Copyright (C) 2014-2019 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 email : webmaster.salome@opencascade.com
21 from salome.shaper import model
23 from salome.geom import geomBuilder
29 geompy = geomBuilder.New()
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")
37 iterator = salome.myStudy.NewChildIterator( comp )
39 while iterator.More():
40 sobj = iterator.Value()
43 obj = sobj.GetObject()
45 raise Exception("GEOM component not found.")
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)
53 raise Exception("No child found at %i for %s"%(i, obj.GetName()))
54 sub_obj = sub_sobj.GetObject()
57 def dumpShaper(fileName):
59 dump=model.moduleDocument().addFeature("Dump")
60 dump.string("file_path").setValue(fileName)
61 dump.string("file_format").setValue("py")
62 dump.string("selection_type").setValue("topological_naming")
67 def getTmpFileName(ext):
68 tempdir = tempfile.gettempdir()
69 tmp_file = tempfile.NamedTemporaryFile(suffix=".%s"%ext, prefix='shaper_', dir=tempdir, delete=False)
70 tmp_filename = tmp_file.name
74 # Create a group of faces
75 # Create a field of faces
79 def testSeveralExportsToGEOM():
82 partSet = model.moduleDocument()
83 Part_1 = model.addPart(partSet)
84 Part_1_doc = Part_1.document()
85 Box_1 = model.addBox(Part_1_doc, 10, 10, 10)
86 Box_2 = model.addBox(Part_1_doc, 20, 20, 20)
87 Translation_1 = model.addTranslation(Part_1_doc, [model.selection("SOLID", "Box_1_1")], model.selection("EDGE", "PartSet/OX"), -10)
88 Partition_1 = model.addPartition(Part_1_doc, [model.selection("SOLID", "Translation_1_1"), model.selection("SOLID", "Box_2_1")])
89 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")])
93 # First export to GEOM
94 model.exportToGEOM(Part_1_doc)
96 # Check that the GEOM object has 1 compsolid and 2 solids
97 geomObject_1 = getLastGEOMShape()
98 assert geompy.NumberOfSubShapes(geomObject_1, geompy.ShapeType["COMPSOLID"]) == 1
99 assert geompy.NumberOfSolids(geomObject_1) == 2
101 # Check that the group has 2 faces
102 geomGroup_1 = getSubObject(geomObject_1, 1)
103 assert geompy.NumberOfFaces(geomGroup_1) == 2
106 Box_3 = model.addBox(Part_1_doc, 10, 10, 10)
107 Translation_2 = model.addTranslation(Part_1_doc, [model.selection("SOLID", "Box_3_1")], model.selection("EDGE", "PartSet/OX"), 20)
109 # Second export to GEOM
110 model.exportToGEOM(Part_1_doc)
112 # Check that the GEOM object has 3 solids
113 geomObject_2 = getLastGEOMShape()
114 assert geompy.NumberOfSolids(geomObject_2) == 3
116 # Dump the salome study (only CORBA modules, SHAPER dump is not in it)
117 tempdir = tempfile.gettempdir()
118 dumpFileGeomBase = "dump_test_geom"
119 dumpFileGeom = os.path.join(tempdir, "%s.py"%dumpFileGeomBase)
120 salome.myStudy.DumpStudy(tempdir, dumpFileGeomBase, True, False)
123 dumpFileShaper = os.path.join(tempdir, "dump_test_shaper.py")
124 dumpShaper(dumpFileShaper)
127 exec(compile(open(dumpFileShaper).read(), dumpFileShaper, 'exec'))
130 exec(compile(open(dumpFileGeom).read(), dumpFileGeom, 'exec'))
133 files = [dumpFileGeom, dumpFileShaper]
141 if __name__ == '__main__':
142 testSeveralExportsToGEOM()