Salome HOME
b223da5af72aeded79ec9b91b87dbd70d3d6bf2d
[modules/shaper.git] / src / ExchangePlugin / Test / TestImportPart_AfterLast_1.py
1 # Copyright (C) 2019-2023  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 # store the reference data
71 features = [Point_2.feature(), Axis_4.feature()]
72 refData = []
73 for feat in features:
74     res = []
75     for r in feat.results():
76         res.append(GeomAlgoAPI_ShapeTools.volume(r.shape()))
77     refData.append( (feat.getKind(), res) )
78
79 # export feature from PartSet
80 featureToExport = Axis_4
81 model.begin()
82 model.exportPart(partSet, filename, [featureToExport.result()])
83 model.end()
84 assert os.path.exists(filename), "ERROR: Cannot export feature {}".format(featureToExport.name())
85
86 # close all documents
87 model.reset()
88
89 # create new Part
90 model.begin()
91 partSet = model.moduleDocument()
92 Part_1 = model.addPart(partSet)
93 Part_1_doc = Part_1.document()
94 Sketch_1 = model.addSketch(Part_1_doc, model.defaultPlane("XOY"))
95 SketchLine_1 = Sketch_1.addLine(-20, -40, -50, -40)
96 SketchLine_2 = Sketch_1.addLine(-50, -40, -50, -10)
97 SketchLine_3 = Sketch_1.addLine(-50, -10, -20, -10)
98 SketchLine_4 = Sketch_1.addLine(-20, -10, -20, -40)
99 SketchConstraintCoincidence_1 = Sketch_1.setCoincident(SketchLine_4.endPoint(), SketchLine_1.startPoint())
100 SketchConstraintCoincidence_2 = Sketch_1.setCoincident(SketchLine_1.endPoint(), SketchLine_2.startPoint())
101 SketchConstraintCoincidence_3 = Sketch_1.setCoincident(SketchLine_2.endPoint(), SketchLine_3.startPoint())
102 SketchConstraintCoincidence_4 = Sketch_1.setCoincident(SketchLine_3.endPoint(), SketchLine_4.startPoint())
103 SketchConstraintHorizontal_1 = Sketch_1.setHorizontal(SketchLine_1.result())
104 SketchConstraintVertical_1 = Sketch_1.setVertical(SketchLine_2.result())
105 SketchConstraintHorizontal_2 = Sketch_1.setHorizontal(SketchLine_3.result())
106 SketchConstraintVertical_2 = Sketch_1.setVertical(SketchLine_4.result())
107 SketchConstraintEqual_1 = Sketch_1.setEqual(SketchLine_1.result(), SketchLine_2.result())
108 SketchConstraintLength_1 = Sketch_1.setLength(SketchLine_1.result(), 30)
109 SketchProjection_1 = Sketch_1.addProjection(model.selection("VERTEX", "PartSet/Origin"), False)
110 SketchPoint_1 = SketchProjection_1.createdFeature()
111 SketchConstraintDistanceHorizontal_1 = Sketch_1.setHorizontalDistance(SketchLine_3.endPoint(), SketchAPI_Point(SketchPoint_1).coordinates(), 20)
112 SketchConstraintDistanceVertical_1 = Sketch_1.setVerticalDistance(SketchLine_3.endPoint(), SketchAPI_Point(SketchPoint_1).coordinates(), 10)
113 model.do()
114 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)
115 model.end()
116
117 # store features before the import
118 featuresBeforeImport = Part_1_doc.allFeatures()
119
120 # import the document
121 model.begin()
122 model.importPart(Part_1_doc, filename)
123 model.end()
124
125 model.removeTmpFile(filename)
126
127 # compare results with the reference data
128 TOLERANCE = 1.e-7
129 features = Part_1_doc.allFeatures()
130 for feat in featuresBeforeImport:
131     if features.front().getKind() == feat.getKind() and features.front().name() == feat.name():
132         features.pop_front()
133 assert(len(features) == len(refData))
134 for feat, ref in zip(features, refData):
135     assert(feat.getKind() == ref[0])
136     for fv, rv in zip(feat.results(), ref[1]):
137         assert(math.fabs(GeomAlgoAPI_ShapeTools.volume(fv.shape()) - rv) < TOLERANCE)
138
139 assert(model.checkPythonDump())