Salome HOME
Copyright update 2022
[modules/shaper.git] / src / ExchangePlugin / Test / TestImportPart_Multiple.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 Box_1 = model.addBox(Part_1_doc, 10, 10, 10)
33 Translation_1 = model.addTranslation(Part_1_doc, [model.selection("SOLID", "Box_1_1")], model.selection("EDGE", "PartSet/OX"), 100)
34 Translation_1.setName("MovedBox")
35 Plane_4 = model.addPlane(Part_1_doc, model.selection("FACE", "PartSet/YOZ"), model.selection("EDGE", "PartSet/OY"), 45)
36 model.do()
37 model.end()
38
39 filename = model.getTmpFileName('check_export', '.shaperpart')
40 model.removeTmpFile(filename)
41
42 # export all features from Part_1
43 model.begin()
44 model.exportPart(Part_1_doc, filename)
45 model.end()
46 assert os.path.exists(filename), "ERROR: Cannot export features from {}".format(Part_1.name())
47
48 # close all documents
49 model.reset()
50
51 # create new Part
52 model.begin()
53 partSet = model.moduleDocument()
54 Part_1 = model.addPart(partSet)
55 Part_1_doc = Part_1.document()
56 Sketch_1 = model.addSketch(Part_1_doc, model.defaultPlane("XOY"))
57 SketchLine_1 = Sketch_1.addLine(-20, -40, -50, -40)
58 SketchLine_2 = Sketch_1.addLine(-50, -40, -50, -10)
59 SketchLine_3 = Sketch_1.addLine(-50, -10, -20, -10)
60 SketchLine_4 = Sketch_1.addLine(-20, -10, -20, -40)
61 SketchConstraintCoincidence_1 = Sketch_1.setCoincident(SketchLine_4.endPoint(), SketchLine_1.startPoint())
62 SketchConstraintCoincidence_2 = Sketch_1.setCoincident(SketchLine_1.endPoint(), SketchLine_2.startPoint())
63 SketchConstraintCoincidence_3 = Sketch_1.setCoincident(SketchLine_2.endPoint(), SketchLine_3.startPoint())
64 SketchConstraintCoincidence_4 = Sketch_1.setCoincident(SketchLine_3.endPoint(), SketchLine_4.startPoint())
65 SketchConstraintHorizontal_1 = Sketch_1.setHorizontal(SketchLine_1.result())
66 SketchConstraintVertical_1 = Sketch_1.setVertical(SketchLine_2.result())
67 SketchConstraintHorizontal_2 = Sketch_1.setHorizontal(SketchLine_3.result())
68 SketchConstraintVertical_2 = Sketch_1.setVertical(SketchLine_4.result())
69 SketchConstraintEqual_1 = Sketch_1.setEqual(SketchLine_1.result(), SketchLine_2.result())
70 SketchConstraintLength_1 = Sketch_1.setLength(SketchLine_1.result(), 30)
71 SketchProjection_1 = Sketch_1.addProjection(model.selection("VERTEX", "PartSet/Origin"), False)
72 SketchPoint_1 = SketchProjection_1.createdFeature()
73 SketchConstraintDistanceHorizontal_1 = Sketch_1.setHorizontalDistance(SketchLine_3.endPoint(), SketchAPI_Point(SketchPoint_1).coordinates(), 20)
74 SketchConstraintDistanceVertical_1 = Sketch_1.setVerticalDistance(SketchLine_3.endPoint(), SketchAPI_Point(SketchPoint_1).coordinates(), 10)
75 model.do()
76 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)
77 Translation_1 = model.addTranslation(Part_1_doc, [model.selection("SOLID", "Extrusion_1_1")], model.selection("EDGE", "PartSet/OX"), 50)
78 model.end()
79
80
81 def checkUniqueNames(theDocument):
82     features = theDocument.allObjects()
83     names = set()
84     for feat in features:
85         name = feat.data().name()
86         if not name == "":
87             assert(not name in names), "'{}' already exists in {}".format(name, names)
88             names.add(name)
89
90
91 # import the document at the end
92 model.begin()
93 model.importPart(Part_1_doc, filename)
94 model.end()
95 checkUniqueNames(Part_1_doc)
96
97 # import the document after Extrusion_1
98 model.begin()
99 model.importPart(Part_1_doc, filename, Extrusion_1)
100 model.end()
101 checkUniqueNames(Part_1_doc)
102
103 # import the document after Sketch_1
104 model.begin()
105 model.importPart(Part_1_doc, filename, Sketch_1)
106 model.end()
107 checkUniqueNames(Part_1_doc)
108
109 # import the document after Translation_1
110 model.begin()
111 model.importPart(Part_1_doc, filename, Translation_1)
112 model.end()
113 checkUniqueNames(Part_1_doc)
114
115 assert(model.checkPythonDump())