Salome HOME
Updated copyright comment
[modules/shaper.git] / src / ExchangePlugin / Test / TestImportPart_AfterCurrent_2.py
1 # Copyright (C) 2019-2024  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 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 Part_1 = model.addPart(partSet)
30 Part_1_doc = Part_1.document()
31 Box_1 = model.addBox(Part_1_doc, 10, 10, 10)
32 Translation_1 = model.addTranslation(Part_1_doc, [model.selection("SOLID", "Box_1_1")], model.selection("EDGE", "PartSet/OX"), 100)
33 Plane_4 = model.addPlane(Part_1_doc, model.selection("FACE", "PartSet/YOZ"), model.selection("EDGE", "PartSet/OY"), 45)
34 model.do()
35 model.end()
36
37 filename = model.getTmpFileName('check_export', '.shaperpart')
38 model.removeTmpFile(filename)
39
40 # store the reference data
41 features = Part_1_doc.allFeatures()
42 refData = []
43 for feat in features:
44     res = []
45     for r in feat.results():
46         res.append(GeomAlgoAPI_ShapeTools.volume(r.shape()))
47     refData.append( (feat.getKind(), res) )
48
49 # export all features from Part_1
50 model.begin()
51 model.exportPart(Part_1_doc, filename)
52 model.end()
53 assert os.path.exists(filename), "ERROR: Cannot export features from {}".format(Part_1.name())
54
55 # close all documents
56 model.reset()
57
58 # create new Part
59 model.begin()
60 partSet = model.moduleDocument()
61 Part_1 = model.addPart(partSet)
62 Part_1_doc = Part_1.document()
63 Sketch_1 = model.addSketch(Part_1_doc, model.defaultPlane("XOY"))
64 SketchLine_1 = Sketch_1.addLine(-20, -40, -50, -40)
65 SketchLine_2 = Sketch_1.addLine(-50, -40, -50, -10)
66 SketchLine_3 = Sketch_1.addLine(-50, -10, -20, -10)
67 SketchLine_4 = Sketch_1.addLine(-20, -10, -20, -40)
68 SketchConstraintCoincidence_1 = Sketch_1.setCoincident(SketchLine_4.endPoint(), SketchLine_1.startPoint())
69 SketchConstraintCoincidence_2 = Sketch_1.setCoincident(SketchLine_1.endPoint(), SketchLine_2.startPoint())
70 SketchConstraintCoincidence_3 = Sketch_1.setCoincident(SketchLine_2.endPoint(), SketchLine_3.startPoint())
71 SketchConstraintCoincidence_4 = Sketch_1.setCoincident(SketchLine_3.endPoint(), SketchLine_4.startPoint())
72 SketchConstraintHorizontal_1 = Sketch_1.setHorizontal(SketchLine_1.result())
73 SketchConstraintVertical_1 = Sketch_1.setVertical(SketchLine_2.result())
74 SketchConstraintHorizontal_2 = Sketch_1.setHorizontal(SketchLine_3.result())
75 SketchConstraintVertical_2 = Sketch_1.setVertical(SketchLine_4.result())
76 SketchConstraintEqual_1 = Sketch_1.setEqual(SketchLine_1.result(), SketchLine_2.result())
77 SketchConstraintLength_1 = Sketch_1.setLength(SketchLine_1.result(), 30)
78 SketchProjection_1 = Sketch_1.addProjection(model.selection("VERTEX", "PartSet/Origin"), False)
79 SketchPoint_1 = SketchProjection_1.createdFeature()
80 SketchConstraintDistanceHorizontal_1 = Sketch_1.setHorizontalDistance(SketchLine_3.endPoint(), SketchAPI_Point(SketchPoint_1).coordinates(), 20)
81 SketchConstraintDistanceVertical_1 = Sketch_1.setVerticalDistance(SketchLine_3.endPoint(), SketchAPI_Point(SketchPoint_1).coordinates(), 10)
82 model.do()
83 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)
84 Translation_1 = model.addTranslation(Part_1_doc, [model.selection("SOLID", "Extrusion_1_1")], model.selection("EDGE", "PartSet/OX"), 50)
85 model.end()
86
87 # store features before the import
88 featuresBeforeImportBegin = Part_1_doc.allFeatures()
89 featuresBeforeImportBegin.pop_back() # remove Translation_1
90 featuresBeforeImportFinish = [Translation_1.feature()]
91
92 # import the document after Extrusion_1
93 model.begin()
94 model.importPart(Part_1_doc, filename, Extrusion_1)
95 model.end()
96
97 model.removeTmpFile(filename)
98
99 # compare results with the reference data
100 TOLERANCE = 1.e-7
101 features = Part_1_doc.allFeatures()
102 for feat in featuresBeforeImportBegin:
103     if features.front().getKind() == feat.getKind() and features.front().name() == feat.name():
104         features.pop_front()
105 for feat in featuresBeforeImportFinish:
106     if features.back().getKind() == feat.getKind() and features.back().name() == feat.name():
107         features.pop_back()
108 assert(len(features) == len(refData))
109 for feat, ref in zip(features, refData):
110     assert(feat.getKind() == ref[0])
111     for fv, rv in zip(feat.results(), ref[1]):
112         assert(math.fabs(GeomAlgoAPI_ShapeTools.volume(fv.shape()) - rv) < TOLERANCE)
113
114 assert(model.checkPythonDump())