Salome HOME
Remove temporary files after test execution
[modules/shaper.git] / src / ExchangePlugin / Test / TestImportPart_ToEmptyPart.py
1 # Copyright (C) 2019-2022  CEA/DEN, EDF R&D
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 from GeomAlgoAPI import *
21 from ModelAPI import *
22 from SketchAPI import *
23 from salome.shaper import model
24
25 import os
26 import math
27
28 model.begin()
29 partSet = model.moduleDocument()
30 Part_1 = model.addPart(partSet)
31 Part_1_doc = Part_1.document()
32 Point_1 = model.addPoint(Part_1_doc, 100, 100, 100)
33 Sketch_1 = model.addSketch(Part_1_doc, model.defaultPlane("XOY"))
34 SketchProjection_1 = Sketch_1.addProjection(model.selection("VERTEX", "PartSet/Origin"), False)
35 SketchPoint_1 = SketchProjection_1.createdFeature()
36 SketchCircle_1 = Sketch_1.addCircle(0, 0, 30)
37 SketchConstraintCoincidence_1 = Sketch_1.setCoincident(SketchPoint_1.result(), SketchCircle_1.center())
38 SketchConstraintRadius_1 = Sketch_1.setRadius(SketchCircle_1.results()[1], 30)
39 model.do()
40 Axis_1 = model.addAxis(Part_1_doc, model.selection("VERTEX", "PartSet/Origin"), model.selection("VERTEX", "Point_1"))
41 Extrusion_1 = model.addExtrusion(Part_1_doc, [model.selection("FACE", "Sketch_1/Face-SketchCircle_1_2f")], model.selection("EDGE", "Axis_1"), 100, 0)
42 Sketch_2 = model.addSketch(Part_1_doc, model.selection("FACE", "Extrusion_1_1/To_Face"))
43 SketchLine_1 = Sketch_2.addLine(57.73502691896258, 57.73502691896258, 71.87716254269353, 43.59289129523163)
44 SketchLine_2 = Sketch_2.addLine(71.87716254269353, 43.59289129523163, 71.87716254269353, 71.87716254269353)
45 SketchConstraintCoincidence_2 = Sketch_2.setCoincident(SketchLine_1.endPoint(), SketchLine_2.startPoint())
46 SketchLine_3 = Sketch_2.addLine(71.87716254269353, 71.87716254269353, 57.73502691896258, 57.73502691896258)
47 SketchConstraintCoincidence_3 = Sketch_2.setCoincident(SketchLine_2.endPoint(), SketchLine_3.startPoint())
48 SketchConstraintCoincidence_4 = Sketch_2.setCoincident(SketchLine_1.startPoint(), SketchLine_3.endPoint())
49 SketchConstraintPerpendicular_1 = Sketch_2.setPerpendicular(SketchLine_1.result(), SketchLine_3.result())
50 SketchConstraintVertical_1 = Sketch_2.setVertical(SketchLine_2.result())
51 SketchConstraintEqual_1 = Sketch_2.setEqual(SketchLine_1.result(), SketchLine_3.result())
52 SketchProjection_2 = Sketch_2.addProjection(model.selection("VERTEX", "[Extrusion_1_1/Generated_Face&Sketch_1/SketchCircle_1_2][Extrusion_1_1/To_Face]__cc"), False)
53 SketchPoint_2 = SketchProjection_2.createdFeature()
54 SketchConstraintCoincidence_5 = Sketch_2.setCoincident(SketchAPI_Point(SketchPoint_2).coordinates(), SketchLine_1.startPoint())
55 SketchConstraintLength_1 = Sketch_2.setLength(SketchLine_1.result(), 20)
56 model.do()
57 Revolution_1 = model.addRevolution(Part_1_doc, [model.selection("FACE", "Sketch_2/Face-SketchLine_1r-SketchLine_2f-SketchLine_3f")], model.selection("EDGE", "PartSet/OX"), 180, 0)
58 model.do()
59 model.end()
60
61 # store the reference data
62 features = Part_1_doc.allFeatures()
63 refData = []
64 for feat in features:
65     res = []
66     for r in feat.results():
67         res.append(GeomAlgoAPI_ShapeTools.volume(r.shape()))
68     refData.append( (feat.getKind(), res) )
69
70 filename = model.getTmpFileName('check_export', '.shaperpart')
71 model.removeTmpFile(filename)
72
73 # emport the document
74 model.begin()
75 model.exportPart(Part_1_doc, filename)
76 model.end()
77 assert os.path.exists(filename), "ERROR: Cannot export {}".format(Part_1.name())
78
79 # close all documents
80 model.reset()
81
82 # import the document
83 model.begin()
84 partSet = model.moduleDocument()
85 Part_1 = model.addPart(partSet)
86 Part_1_doc = Part_1.document()
87 model.importPart(Part_1_doc, filename)
88 model.end()
89
90 model.removeTmpFile(filename)
91
92 # Test 1. No new Part should be created
93 assert(partSet.size(ModelAPI_ResultPart.group()) == 1)
94
95 # Test 2. Compare results with the reference data
96 TOLERANCE = 1.e-7
97 features = Part_1_doc.allFeatures()
98 assert(len(features) == len(refData))
99 for feat, ref in zip(features, refData):
100     assert(feat.getKind() == ref[0])
101     for fv, rv in zip(feat.results(), ref[1]):
102         assert(math.fabs(GeomAlgoAPI_ShapeTools.volume(fv.shape()) - rv) < TOLERANCE)
103
104 assert(model.checkPythonDump())