Salome HOME
Use temporary name of exported file in tests related to Part's export/import
[modules/shaper.git] / src / ExchangePlugin / Test / TestImportPart_AfterLast_4.py
1 # Copyright (C) 2019-2021  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 SketchAPI import *
22 from salome.shaper import model
23
24 import os
25 import math
26
27 model.begin()
28 partSet = model.moduleDocument()
29 Point_2 = model.addPoint(partSet, 100, 100, 100)
30 Axis_4 = model.addAxis(partSet, model.selection("VERTEX", "Origin"), model.selection("VERTEX", "Point_2"))
31 Part_1 = model.addPart(partSet)
32 Part_1_doc = Part_1.document()
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 Extrusion_1 = model.addExtrusion(Part_1_doc, [model.selection("FACE", "Sketch_1/Face-SketchCircle_1_2f")], model.selection("EDGE", "PartSet/Axis_4"), 100, 0)
41 Sketch_2 = model.addSketch(Part_1_doc, model.selection("FACE", "Extrusion_1_1/To_Face"))
42 SketchLine_1 = Sketch_2.addLine(57.73502691896258, 57.73502691896258, 71.87716254269353, 43.59289129523163)
43 SketchLine_2 = Sketch_2.addLine(71.87716254269353, 43.59289129523163, 71.87716254269353, 71.87716254269353)
44 SketchConstraintCoincidence_2 = Sketch_2.setCoincident(SketchLine_1.endPoint(), SketchLine_2.startPoint())
45 SketchLine_3 = Sketch_2.addLine(71.87716254269353, 71.87716254269353, 57.73502691896258, 57.73502691896258)
46 SketchConstraintCoincidence_3 = Sketch_2.setCoincident(SketchLine_2.endPoint(), SketchLine_3.startPoint())
47 SketchConstraintCoincidence_4 = Sketch_2.setCoincident(SketchLine_1.startPoint(), SketchLine_3.endPoint())
48 SketchConstraintPerpendicular_1 = Sketch_2.setPerpendicular(SketchLine_1.result(), SketchLine_3.result())
49 SketchConstraintVertical_1 = Sketch_2.setVertical(SketchLine_2.result())
50 SketchConstraintEqual_1 = Sketch_2.setEqual(SketchLine_1.result(), SketchLine_3.result())
51 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)
52 SketchPoint_2 = SketchProjection_2.createdFeature()
53 SketchConstraintCoincidence_5 = Sketch_2.setCoincident(SketchAPI_Point(SketchPoint_2).coordinates(), SketchLine_1.startPoint())
54 SketchConstraintLength_1 = Sketch_2.setLength(SketchLine_1.result(), 20)
55 model.do()
56 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)
57 model.do()
58
59 Part_2 = model.addPart(partSet)
60 Part_2_doc = Part_2.document()
61 Box_1 = model.addBox(Part_2_doc, 10, 10, 10)
62 Translation_1 = model.addTranslation(Part_2_doc, [model.selection("SOLID", "Box_1_1")], model.selection("EDGE", "PartSet/OX"), 100)
63 model.do()
64
65 model.end()
66
67 filename = model.getTmpFileName('check_export', '.shaperpart')
68 model.removeTmpFile(filename)
69
70 featureToExport = Translation_1
71
72 # store the reference data
73 features = [Box_1.feature(), Translation_1.feature()]
74 refData = []
75 for feat in features:
76     res = []
77     for r in feat.results():
78         res.append(GeomAlgoAPI_ShapeTools.volume(r.shape()))
79     refData.append( (feat.getKind(), res) )
80
81 # export all features from Part_2
82 model.begin()
83 model.exportPart(Part_2_doc, filename)
84 model.end()
85 assert os.path.exists(filename), "ERROR: Cannot export feature {}".format(featureToExport.name())
86
87 # close all documents
88 model.reset()
89
90 # create new Part
91 model.begin()
92 partSet = model.moduleDocument()
93 Part_1 = model.addPart(partSet)
94 Part_1_doc = Part_1.document()
95 Sketch_1 = model.addSketch(Part_1_doc, model.defaultPlane("XOY"))
96 SketchLine_1 = Sketch_1.addLine(-20, -40, -50, -40)
97 SketchLine_2 = Sketch_1.addLine(-50, -40, -50, -10)
98 SketchLine_3 = Sketch_1.addLine(-50, -10, -20, -10)
99 SketchLine_4 = Sketch_1.addLine(-20, -10, -20, -40)
100 SketchConstraintCoincidence_1 = Sketch_1.setCoincident(SketchLine_4.endPoint(), SketchLine_1.startPoint())
101 SketchConstraintCoincidence_2 = Sketch_1.setCoincident(SketchLine_1.endPoint(), SketchLine_2.startPoint())
102 SketchConstraintCoincidence_3 = Sketch_1.setCoincident(SketchLine_2.endPoint(), SketchLine_3.startPoint())
103 SketchConstraintCoincidence_4 = Sketch_1.setCoincident(SketchLine_3.endPoint(), SketchLine_4.startPoint())
104 SketchConstraintHorizontal_1 = Sketch_1.setHorizontal(SketchLine_1.result())
105 SketchConstraintVertical_1 = Sketch_1.setVertical(SketchLine_2.result())
106 SketchConstraintHorizontal_2 = Sketch_1.setHorizontal(SketchLine_3.result())
107 SketchConstraintVertical_2 = Sketch_1.setVertical(SketchLine_4.result())
108 SketchConstraintEqual_1 = Sketch_1.setEqual(SketchLine_1.result(), SketchLine_2.result())
109 SketchConstraintLength_1 = Sketch_1.setLength(SketchLine_1.result(), 30)
110 SketchProjection_1 = Sketch_1.addProjection(model.selection("VERTEX", "PartSet/Origin"), False)
111 SketchPoint_1 = SketchProjection_1.createdFeature()
112 SketchConstraintDistanceHorizontal_1 = Sketch_1.setHorizontalDistance(SketchLine_3.endPoint(), SketchAPI_Point(SketchPoint_1).coordinates(), 20)
113 SketchConstraintDistanceVertical_1 = Sketch_1.setVerticalDistance(SketchLine_3.endPoint(), SketchAPI_Point(SketchPoint_1).coordinates(), 10)
114 model.do()
115 Extrusion_1 = model.addExtrusion(Part_1_doc, [model.selection("FACE", "Sketch_1/Face-SketchLine_4r-SketchLine_3r-SketchLine_2r-SketchLine_1r")], model.selection(), 30, 0)
116 model.end()
117
118 # store features before the import
119 featuresBeforeImport = Part_1_doc.allFeatures()
120
121 # import the document
122 model.begin()
123 model.importPart(Part_1_doc, filename)
124 model.end()
125
126 # compare results with the reference data
127 TOLERANCE = 1.e-7
128 features = Part_1_doc.allFeatures()
129 for feat in featuresBeforeImport:
130     if features.front().getKind() == feat.getKind() and features.front().name() == feat.name():
131         features.pop_front()
132 assert(len(features) == len(refData))
133 for feat, ref in zip(features, refData):
134     assert(feat.getKind() == ref[0])
135     for fv, rv in zip(feat.results(), ref[1]):
136         assert(math.fabs(GeomAlgoAPI_ShapeTools.volume(fv.shape()) - rv) < TOLERANCE)
137
138 assert(model.checkPythonDump())