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