Salome HOME
updated copyright message
[modules/shaper.git] / src / ExchangePlugin / Test / TestImportPart_AfterLast_2.py
1 # Copyright (C) 2019-2023  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 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 Point_2 = model.addPoint(partSet, 100, 100, 100)
31 Axis_4 = model.addAxis(partSet, model.selection("VERTEX", "Origin"), model.selection("VERTEX", "Point_2"))
32 Part_1 = model.addPart(partSet)
33 Part_1_doc = Part_1.document()
34 Sketch_1 = model.addSketch(Part_1_doc, model.defaultPlane("XOY"))
35 SketchProjection_1 = Sketch_1.addProjection(model.selection("VERTEX", "PartSet/Origin"), False)
36 SketchPoint_1 = SketchProjection_1.createdFeature()
37 SketchCircle_1 = Sketch_1.addCircle(0, 0, 30)
38 SketchConstraintCoincidence_1 = Sketch_1.setCoincident(SketchPoint_1.result(), SketchCircle_1.center())
39 SketchConstraintRadius_1 = Sketch_1.setRadius(SketchCircle_1.results()[1], 30)
40 model.do()
41 Extrusion_1 = model.addExtrusion(Part_1_doc, [model.selection("FACE", "Sketch_1/Face-SketchCircle_1_2f")], model.selection("EDGE", "PartSet/Axis_4"), 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
60 Part_2 = model.addPart(partSet)
61 Part_2_doc = Part_2.document()
62 Box_1 = model.addBox(Part_2_doc, 10, 10, 10)
63 Translation_1 = model.addTranslation(Part_2_doc, [model.selection("SOLID", "Box_1_1")], model.selection("EDGE", "PartSet/OX"), 100)
64 model.do()
65
66 model.end()
67
68 filename = model.getTmpFileName('check_export', '.shaperpart')
69 model.removeTmpFile(filename)
70
71 featureToExport = Sketch_1
72
73 # store the reference data
74 sketch = featureToCompositeFeature(featureToExport.feature())
75 features = [sketch]
76 for i in range(0, sketch.numberOfSubs()):
77     features.append(sketch.subFeature(i))
78 refData = []
79 for feat in features:
80     res = []
81     for r in feat.results():
82         res.append(GeomAlgoAPI_ShapeTools.volume(r.shape()))
83     refData.append( (feat.getKind(), res) )
84
85 # export sketch from Part_1
86 model.begin()
87 model.exportPart(Part_1_doc, filename, [featureToExport.result()])
88 model.end()
89 assert os.path.exists(filename), "ERROR: Cannot export feature {}".format(featureToExport.name())
90
91 # close all documents
92 model.reset()
93
94 # create new Part
95 model.begin()
96 partSet = model.moduleDocument()
97 Part_1 = model.addPart(partSet)
98 Part_1_doc = Part_1.document()
99 Sketch_1 = model.addSketch(Part_1_doc, model.defaultPlane("XOY"))
100 SketchLine_1 = Sketch_1.addLine(-20, -40, -50, -40)
101 SketchLine_2 = Sketch_1.addLine(-50, -40, -50, -10)
102 SketchLine_3 = Sketch_1.addLine(-50, -10, -20, -10)
103 SketchLine_4 = Sketch_1.addLine(-20, -10, -20, -40)
104 SketchConstraintCoincidence_1 = Sketch_1.setCoincident(SketchLine_4.endPoint(), SketchLine_1.startPoint())
105 SketchConstraintCoincidence_2 = Sketch_1.setCoincident(SketchLine_1.endPoint(), SketchLine_2.startPoint())
106 SketchConstraintCoincidence_3 = Sketch_1.setCoincident(SketchLine_2.endPoint(), SketchLine_3.startPoint())
107 SketchConstraintCoincidence_4 = Sketch_1.setCoincident(SketchLine_3.endPoint(), SketchLine_4.startPoint())
108 SketchConstraintHorizontal_1 = Sketch_1.setHorizontal(SketchLine_1.result())
109 SketchConstraintVertical_1 = Sketch_1.setVertical(SketchLine_2.result())
110 SketchConstraintHorizontal_2 = Sketch_1.setHorizontal(SketchLine_3.result())
111 SketchConstraintVertical_2 = Sketch_1.setVertical(SketchLine_4.result())
112 SketchConstraintEqual_1 = Sketch_1.setEqual(SketchLine_1.result(), SketchLine_2.result())
113 SketchConstraintLength_1 = Sketch_1.setLength(SketchLine_1.result(), 30)
114 SketchProjection_1 = Sketch_1.addProjection(model.selection("VERTEX", "PartSet/Origin"), False)
115 SketchPoint_1 = SketchProjection_1.createdFeature()
116 SketchConstraintDistanceHorizontal_1 = Sketch_1.setHorizontalDistance(SketchLine_3.endPoint(), SketchAPI_Point(SketchPoint_1).coordinates(), 20)
117 SketchConstraintDistanceVertical_1 = Sketch_1.setVerticalDistance(SketchLine_3.endPoint(), SketchAPI_Point(SketchPoint_1).coordinates(), 10)
118 model.do()
119 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)
120 model.end()
121
122 # store features before the import
123 featuresBeforeImport = Part_1_doc.allFeatures()
124
125 # import the document
126 model.begin()
127 model.importPart(Part_1_doc, filename)
128 model.end()
129
130 model.removeTmpFile(filename)
131
132 # compare results with the reference data
133 TOLERANCE = 1.e-7
134 features = Part_1_doc.allFeatures()
135 for feat in featuresBeforeImport:
136     if features.front().getKind() == feat.getKind() and features.front().name() == feat.name():
137         features.pop_front()
138 assert(len(features) == len(refData))
139 for feat, ref in zip(features, refData):
140     assert(feat.getKind() == ref[0])
141     for fv, rv in zip(feat.results(), ref[1]):
142         assert(math.fabs(GeomAlgoAPI_ShapeTools.volume(fv.shape()) - rv) < TOLERANCE)
143
144 assert(model.checkPythonDump())