Salome HOME
3a64ec297bbc6c725855dca6ee1185e7513f99e0
[modules/shaper.git] / src / ExchangePlugin / Test / TestExportToXAOWithGroupNotUpdated.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2018-2019  CEA/DEN, EDF R&D
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 # This test checks that exportToXAO works even if a group or a field is not valid
22
23 from salome.shaper import model
24 from ModelAPI import featureToCompositeFeature
25 from GeomAPI import GeomAPI_Shape
26 import tempfile
27 import os
28
29 def getTmpFileName(ext):
30   tempdir = tempfile.gettempdir()
31   tmp_file = tempfile.NamedTemporaryFile(suffix=".%s"%ext, prefix='shaper_', dir=tempdir, delete=False)
32   tmp_filename = tmp_file.name
33   return tmp_filename
34
35 tmp_file1 = getTmpFileName("xao")
36 tmp_file2 = getTmpFileName("xao")
37
38 model.begin()
39 partSet = model.moduleDocument()
40 Part_1 = model.addPart(partSet)
41 Part_1_doc = Part_1.document()
42 # Create a box
43 Sketch_1 = model.addSketch(Part_1_doc, model.defaultPlane("XOY"))
44 SketchLine_1 = Sketch_1.addLine(0, 0, 0, 50)
45 SketchLine_2 = Sketch_1.addLine(0, 50, 50, 50)
46 SketchConstraintCoincidence_1 = Sketch_1.setCoincident(SketchLine_1.endPoint(), SketchLine_2.startPoint())
47 SketchLine_3 = Sketch_1.addLine(50, 50, 50, 0)
48 SketchConstraintCoincidence_2 = Sketch_1.setCoincident(SketchLine_2.endPoint(), SketchLine_3.startPoint())
49 SketchLine_4 = Sketch_1.addLine(50, 0, 0, 0)
50 SketchConstraintCoincidence_3 = Sketch_1.setCoincident(SketchLine_3.endPoint(), SketchLine_4.startPoint())
51 SketchConstraintCoincidence_4 = Sketch_1.setCoincident(SketchLine_4.endPoint(), SketchLine_1.startPoint())
52 SketchConstraintRigid_1 = Sketch_1.setFixed(SketchLine_1.startPoint())
53 SketchConstraintVertical_1 = Sketch_1.setVertical(SketchLine_1.result())
54 SketchConstraintLength_1 = Sketch_1.setLength(SketchLine_4.result(), 50)
55 SketchConstraintLength_2 = Sketch_1.setLength(SketchLine_1.result(), 50)
56 SketchConstraintParallel_1 = Sketch_1.setParallel(SketchLine_1.result(), SketchLine_3.result())
57 SketchConstraintParallel_2 = Sketch_1.setParallel(SketchLine_2.result(), SketchLine_4.result())
58 SketchConstraintPerpendicular_1 = Sketch_1.setPerpendicular(SketchLine_1.result(), SketchLine_4.result())
59 model.do()
60 Extrusion_1 = model.addExtrusion(Part_1_doc, [model.selection("FACE", "Sketch_1/Face-SketchLine_4r-SketchLine_3r-SketchLine_2r-SketchLine_1r")], model.selection(), 50, 0)
61 # Create a group with the faces of the box
62 Group_1_objects = [model.selection("FACE", "Extrusion_1_1/Generated_Face&Sketch_1/SketchLine_1"), model.selection("FACE", "Extrusion_1_1/Generated_Face&Sketch_1/SketchLine_2"), model.selection("FACE", "Extrusion_1_1/From_Face"), model.selection("FACE", "Extrusion_1_1/Generated_Face&Sketch_1/SketchLine_4"), model.selection("FACE", "Extrusion_1_1/To_Face"), model.selection("FACE", "Extrusion_1_1/Generated_Face&Sketch_1/SketchLine_3")]
63 Group_1 = model.addGroup(Part_1_doc, Group_1_objects)
64 Group_1.setName("Group_faces")
65 Group_1.result().setName("Group_faces")
66 # Create a field on the box
67 Field_1_objects = [model.selection("FACE", "Extrusion_1_1/Generated_Face&Sketch_1/SketchLine_1"), model.selection("FACE", "Extrusion_1_1/Generated_Face&Sketch_1/SketchLine_2"), model.selection("FACE", "Extrusion_1_1/From_Face"), model.selection("FACE", "Extrusion_1_1/Generated_Face&Sketch_1/SketchLine_4"), model.selection("FACE", "Extrusion_1_1/To_Face"), model.selection("FACE", "Extrusion_1_1/Generated_Face&Sketch_1/SketchLine_3")]
68 Field_1 = model.addField(Part_1_doc, 1, "DOUBLE", 1, ["Comp 1"], Field_1_objects)
69 Field_1.addStep(0, 0, [[0], [1], [2], [3], [4], [5], [6]])
70 # Create a plane to split the box in two parts
71 Plane_4 = model.addPlane(Part_1_doc, model.selection("FACE", "PartSet/XOY"), 25, False)
72 # Split the Box in two parts
73 Partition_1 = model.addPartition(Part_1_doc, [model.selection("SOLID", "Extrusion_1_1"), model.selection("FACE", "Plane_1")])
74 # Export the shape with the group and field not updated with the new result
75 Export_1 = model.exportToXAO(Part_1_doc, tmp_file1)
76 # Update the group and the field with the last result
77 Part_1_doc.moveFeature(Group_1.feature(), Partition_1.feature())
78 Part_1_doc.moveFeature(Field_1.feature(), Group_1.feature())
79 # Export the shape with the group and field updated
80 Export_2 = model.exportToXAO(Part_1_doc, tmp_file2)
81 model.do()
82
83 # Create a new part
84 Part_2 = model.addPart(partSet)
85 Part_2_doc = Part_2.document()
86 # Import the first XAO file
87 Import_1 = model.addImport(Part_2_doc, tmp_file1)
88 model.do()
89
90 # Check results
91 Import_1_Feature = Import_1.feature()
92 assert Import_1_Feature.error() == ''
93 assert Import_1_Feature.name() == "Partition_1_1"
94 assert len(Import_1_Feature.results()) == 1
95 model.testNbSubShapes(Import_1, GeomAPI_Shape.SOLID, [2])
96 # No group, no field
97 aCompositeFeature = featureToCompositeFeature(Import_1_Feature)
98 assert aCompositeFeature.numberOfSubs(False) == 0
99
100 # Create a new part
101 Part_3 = model.addPart(partSet)
102 Part_3_doc = Part_3.document()
103
104 # Import the second XAO file
105 Import_2 = model.addImport(Part_3_doc, tmp_file2)
106 model.do()
107
108 # Check results
109 Import_2_Feature = Import_2.feature()
110 assert Import_2_Feature.error() == ''
111 assert Import_2_Feature.name() == "Partition_1_1"
112 assert len(Import_2_Feature.results()) == 1
113 model.testNbSubShapes(Import_2, GeomAPI_Shape.SOLID, [2])
114
115 # One group, one field
116 aCompositeFeature = featureToCompositeFeature(Import_2_Feature)
117 assert aCompositeFeature.numberOfSubs(False) == 2
118
119 aFeature1 = aCompositeFeature.subFeature(0, False)
120 assert aFeature1.getKind() == "Group"
121 assert aFeature1.name() == "Group_faces"
122
123 aSelectionList = aFeature1.selectionList("group_list")
124 assert aSelectionList.selectionType() == "face"
125 assert aSelectionList.size() == 10
126
127 aFeature2 = aCompositeFeature.subFeature(1, False)
128 assert aFeature2.getKind() == "Field"
129 assert aFeature2.name() == "Field_1"
130 assert aFeature2.intArray("stamps").size() == 1
131 assert aFeature2.tables("values").rows() == 7
132 assert aFeature2.tables("values").columns() == 1
133 assert aFeature2.tables("values").tables() == 1
134 assert aFeature2.tables("values").type() == 2 # double
135 assert aFeature2.selectionList("selected").size() == 6
136 model.do()
137 model.end()
138
139 os.remove(tmp_file1)
140 os.remove(tmp_file2)