Salome HOME
f8c73f4ba6eb2d54cdbc845d6e25832ce22863c9
[modules/shaper.git] / src / ExchangePlugin / Test / TestExportToXAOWithGroupNotUpdated.py
1 # Copyright (C) 2018-2023  CEA, EDF
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 # This test checks that exportToXAO works even if a group or a field is not valid
21
22 from salome.shaper import model
23 from ModelAPI import featureToCompositeFeature
24 from GeomAPI import GeomAPI_Shape
25 import tempfile
26 import os
27
28 def getTmpFileName(ext):
29   tempdir = tempfile.gettempdir()
30   tmp_file = tempfile.NamedTemporaryFile(suffix=".%s"%ext, prefix='shaper_', dir=tempdir, delete=False)
31   tmp_filename = tmp_file.name
32   return tmp_filename
33
34 tmp_file1 = getTmpFileName("xao")
35 tmp_file2 = getTmpFileName("xao")
36
37 model.begin()
38 partSet = model.moduleDocument()
39 Part_1 = model.addPart(partSet)
40 Part_1_doc = Part_1.document()
41 # Create a box
42 Sketch_1 = model.addSketch(Part_1_doc, model.defaultPlane("XOY"))
43 SketchLine_1 = Sketch_1.addLine(0, 0, 0, 50)
44 SketchLine_2 = Sketch_1.addLine(0, 50, 50, 50)
45 SketchConstraintCoincidence_1 = Sketch_1.setCoincident(SketchLine_1.endPoint(), SketchLine_2.startPoint())
46 SketchLine_3 = Sketch_1.addLine(50, 50, 50, 0)
47 SketchConstraintCoincidence_2 = Sketch_1.setCoincident(SketchLine_2.endPoint(), SketchLine_3.startPoint())
48 SketchLine_4 = Sketch_1.addLine(50, 0, 0, 0)
49 SketchConstraintCoincidence_3 = Sketch_1.setCoincident(SketchLine_3.endPoint(), SketchLine_4.startPoint())
50 SketchConstraintCoincidence_4 = Sketch_1.setCoincident(SketchLine_4.endPoint(), SketchLine_1.startPoint())
51 SketchConstraintRigid_1 = Sketch_1.setFixed(SketchLine_1.startPoint())
52 SketchConstraintVertical_1 = Sketch_1.setVertical(SketchLine_1.result())
53 SketchConstraintLength_1 = Sketch_1.setLength(SketchLine_4.result(), 50)
54 SketchConstraintLength_2 = Sketch_1.setLength(SketchLine_1.result(), 50)
55 SketchConstraintParallel_1 = Sketch_1.setParallel(SketchLine_1.result(), SketchLine_3.result())
56 SketchConstraintParallel_2 = Sketch_1.setParallel(SketchLine_2.result(), SketchLine_4.result())
57 SketchConstraintPerpendicular_1 = Sketch_1.setPerpendicular(SketchLine_1.result(), SketchLine_4.result())
58 model.do()
59 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)
60 # Create a group with the faces of the box
61 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")]
62 Group_1 = model.addGroup(Part_1_doc, Group_1_objects)
63 Group_1.setName("Group_faces")
64 Group_1.result().setName("Group_faces")
65 # Create a field on the box
66 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")]
67 Field_1 = model.addField(Part_1_doc, 1, "DOUBLE", 1, ["Comp 1"], Field_1_objects)
68 Field_1.addStep(0, 0, [[0], [1], [2], [3], [4], [5], [6]])
69 # Create a plane to split the box in two parts
70 Plane_4 = model.addPlane(Part_1_doc, model.selection("FACE", "PartSet/XOY"), 25, False)
71 # Split the Box in two parts
72 Partition_1 = model.addPartition(Part_1_doc, [model.selection("SOLID", "Extrusion_1_1"), model.selection("FACE", "Plane_1")])
73 # Export the shape with the group and field not updated with the new result
74 Export_1 = model.exportToXAO(Part_1_doc, tmp_file1)
75 # Update the group and the field with the last result
76 Part_1_doc.moveFeature(Group_1.feature(), Partition_1.feature())
77 Part_1_doc.moveFeature(Field_1.feature(), Group_1.feature())
78 # Export the shape with the group and field updated
79 Export_2 = model.exportToXAO(Part_1_doc, tmp_file2)
80 model.do()
81
82 # Create a new part
83 Part_2 = model.addPart(partSet)
84 Part_2_doc = Part_2.document()
85 # Import the first XAO file
86 Import_1 = model.addImport(Part_2_doc, tmp_file1)
87 model.do()
88
89 # Check results
90 Import_1_Feature = Import_1.feature()
91 assert Import_1_Feature.error() == ''
92 assert Import_1_Feature.name() == "Partition_1_1"
93 assert len(Import_1_Feature.results()) == 1
94 model.testNbSubShapes(Import_1, GeomAPI_Shape.SOLID, [2])
95 # No group, no field
96 aCompositeFeature = featureToCompositeFeature(Import_1_Feature)
97 assert aCompositeFeature.numberOfSubs(False) == 0
98
99 # Create a new part
100 Part_3 = model.addPart(partSet)
101 Part_3_doc = Part_3.document()
102
103 # Import the second XAO file
104 Import_2 = model.addImport(Part_3_doc, tmp_file2)
105 model.do()
106
107 # Check results
108 Import_2_Feature = Import_2.feature()
109 assert Import_2_Feature.error() == ''
110 assert Import_2_Feature.name() == "Partition_1_1"
111 assert len(Import_2_Feature.results()) == 1
112 model.testNbSubShapes(Import_2, GeomAPI_Shape.SOLID, [2])
113
114 # One group, one field
115 aCompositeFeature = featureToCompositeFeature(Import_2_Feature)
116 assert aCompositeFeature.numberOfSubs(False) == 2
117
118 aFeature1 = aCompositeFeature.subFeature(0, False)
119 assert aFeature1.getKind() == "Group"
120 assert aFeature1.name() == "Group_faces"
121
122 aSelectionList = aFeature1.selectionList("group_list")
123 assert aSelectionList.selectionType() == "face"
124 assert aSelectionList.size() == 10
125
126 aFeature2 = aCompositeFeature.subFeature(1, False)
127 assert aFeature2.getKind() == "Field"
128 assert aFeature2.name() == "Field_1"
129 assert aFeature2.intArray("stamps").size() == 1
130 assert aFeature2.tables("values").rows() == 7
131 assert aFeature2.tables("values").columns() == 1
132 assert aFeature2.tables("values").tables() == 1
133 assert aFeature2.tables("values").type() == 2 # double
134 assert aFeature2.selectionList("selected").size() == 6
135 model.do()
136 model.end()
137
138 os.remove(tmp_file1)
139 os.remove(tmp_file2)