Salome HOME
updated copyright message
[modules/shaper.git] / src / BuildPlugin / Test / TestWire.py
1 # Copyright (C) 2014-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 # Initialization of the test
21 from ModelAPI import *
22 from GeomDataAPI import *
23 from GeomAlgoAPI import *
24 from GeomAPI import *
25
26 # Get document
27 aSession = ModelAPI_Session.get()
28 aDocument = aSession.moduleDocument()
29
30 # Create a part
31 aSession.startOperation()
32 aPartFeature = aDocument.addFeature("Part")
33 aSession.finishOperation()
34 aPartResult = modelAPI_ResultPart(aPartFeature.firstResult())
35 aPart = aPartResult.partDoc()
36
37 # =============================================================================
38 # Test 1. Create wire from edges of sketch
39 # =============================================================================
40
41 # Create a sketch
42 aSession.startOperation()
43 aSketchFeature = featureToCompositeFeature(aPart.addFeature("Sketch"))
44 anOrigin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
45 anOrigin.setValue(0, 0, 0)
46 aDirX = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
47 aDirX.setValue(1, 0, 0)
48 aNorm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
49 aNorm.setValue(0, 0, 1)
50
51 # Create lines
52 aSketchLineFeature = aSketchFeature.addFeature("SketchLine")
53 aSketchLineStartPoint = geomDataAPI_Point2D(aSketchLineFeature.attribute("StartPoint"))
54 aSketchLineEndPoint = geomDataAPI_Point2D(aSketchLineFeature.attribute("EndPoint"))
55 aSketchLineStartPoint.setValue(0, 0)
56 aSketchLineEndPoint.setValue(0, 50)
57 aSketchLineFeature = aSketchFeature.addFeature("SketchLine")
58 aSketchLineStartPoint = geomDataAPI_Point2D(aSketchLineFeature.attribute("StartPoint"))
59 aSketchLineEndPoint = geomDataAPI_Point2D(aSketchLineFeature.attribute("EndPoint"))
60 aSketchLineStartPoint.setValue(0, 50)
61 aSketchLineEndPoint.setValue(50, 50)
62 aSketchLineFeature = aSketchFeature.addFeature("SketchLine")
63 aSketchLineStartPoint = geomDataAPI_Point2D(aSketchLineFeature.attribute("StartPoint"))
64 aSketchLineEndPoint = geomDataAPI_Point2D(aSketchLineFeature.attribute("EndPoint"))
65 aSketchLineStartPoint.setValue(50, 50)
66 aSketchLineEndPoint.setValue(50, 0)
67 aSketchLineFeature = aSketchFeature.addFeature("SketchLine")
68 aSketchLineStartPoint = geomDataAPI_Point2D(aSketchLineFeature.attribute("StartPoint"))
69 aSketchLineEndPoint = geomDataAPI_Point2D(aSketchLineFeature.attribute("EndPoint"))
70 aSketchLineStartPoint.setValue(50, 0)
71 aSketchLineEndPoint.setValue(0, 0)
72 aSession.finishOperation()
73 aSketchResult = aSketchFeature.firstResult()
74 aSketchShape = aSketchResult.shape()
75
76 # Create wire
77 aSession.startOperation()
78 aWireFeature = aPart.addFeature("Wire")
79 aBaseObjectsList = aWireFeature.selectionList("base_objects")
80 aShapeExplorer = GeomAPI_ShapeExplorer(aSketchShape, GeomAPI_Shape.EDGE)
81 if aShapeExplorer.more():
82     aBaseObjectsList.append(aSketchResult, aShapeExplorer.current())
83 aWireFeature.customAction("add_contour")
84 aSession.finishOperation()
85
86 # Test results
87 assert (len(aWireFeature.results()) > 0)
88
89 from salome.shaper import model
90
91 # =============================================================================
92 # Test 2. Complete contour by selecting only one edge
93 # =============================================================================
94
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(-68.61063464837, 232.418524871355, -488.85077186964, 232.418524871355)
101 SketchLine_2 = Sketch_1.addLine(-488.85077186964, 232.418524871355, -488.85077186964, -153.516295025729)
102 SketchLine_3 = Sketch_1.addLine(-488.85077186964, -153.516295025729, -68.61063464837, -153.516295025729)
103 SketchLine_4 = Sketch_1.addLine(-68.61063464837, -153.516295025729, -68.61063464837, 232.418524871355)
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 model.do()
113 Wire_1 = model.addWire(Part_1_doc, [model.selection("EDGE", "Sketch_1/SketchLine_1")])
114 Wire_1.addContour()
115 model.end()
116
117 # =============================================================================
118 # Test 3. Create wire from edges of solid
119 # =============================================================================
120
121 # Box
122 aSession.startOperation()
123 aBox = aPart.addFeature("Box")
124 aBox.string("CreationMethod").setValue("BoxByDimensions")
125 aBox.real("dx").setValue(20)
126 aBox.real("dy").setValue(20)
127 aBox.real("dz").setValue(20)
128 aSession.finishOperation()
129 aBoxResult = aBox.firstResult()
130 aBoxShape = aBoxResult.shape()
131
132 # Wire
133 aSession.startOperation()
134 aWireFeature2 = aPart.addFeature("Wire")
135 aBaseObjectsList = aWireFeature2.selectionList("base_objects")
136 aBaseObjectsList.append("[Box_1_1/Front][Box_1_1/Bottom]", "EDGE")
137 aBaseObjectsList.append("[Box_1_1/Front][Box_1_1/Right]", "EDGE")
138 aBaseObjectsList.append("[Box_1_1/Right][Box_1_1/Top]", "EDGE")
139 aBaseObjectsList.append("[Box_1_1/Back][Box_1_1/Top]", "EDGE")
140 aBaseObjectsList.append("[Box_1_1/Back][Box_1_1/Left]", "EDGE")
141 aBaseObjectsList.append("[Box_1_1/Left][Box_1_1/Bottom]", "EDGE")
142 aSession.finishOperation()
143
144 # Test results
145 assert (len(aWireFeature2.results()) == 1)
146
147 # =============================================================================
148 # Test 4. Check Wire feature on the whole sketch
149 # =============================================================================
150
151 aSession.startOperation()
152 aWireFeature3 = aPart.addFeature("Wire")
153 aBaseObjectsList = aWireFeature3.selectionList("base_objects")
154 aBaseObjectsList.append(aSketchResult, None)
155 aSession.finishOperation()
156 assert (len(aWireFeature3.results()) == 1)
157
158 # =============================================================================
159 # Test 5. Check Wire feature failed on incorrect input
160 # =============================================================================
161
162 aSession.startOperation()
163 aWireFeature3 = aPart.addFeature("Wire")
164 aBaseObjectsList = aWireFeature3.selectionList("base_objects")
165 aShapeExplorer = GeomAPI_ShapeExplorer(aBoxShape, GeomAPI_Shape.VERTEX)
166 aShape = aShapeExplorer.current()
167 aBaseObjectsList.append(aBoxResult, aShape)
168 aSession.finishOperation()
169 assert (len(aWireFeature3.results()) == 0)
170
171 aSession.startOperation()
172 aBaseObjectsList.clear()
173 aShapeExplorer = GeomAPI_ShapeExplorer(aBoxShape, GeomAPI_Shape.FACE)
174 aShape = aShapeExplorer.current()
175 aBaseObjectsList.append(aBoxResult, aShape)
176 aSession.finishOperation()
177 assert (len(aWireFeature3.results()) == 0)
178
179 # remove failed feature
180 aSession.startOperation()
181 aPart.removeFeature(aWireFeature3)
182 aSession.finishOperation()
183
184 assert(model.checkPythonDump(model.ModelHighAPI.CHECK_NAMING))