Salome HOME
Extend unit tests for Vertex, Edge, Wire, Face and Shell features
[modules/shaper.git] / src / BuildPlugin / Test / TestEdge.py
1 ## Copyright (C) 2014-2017  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
18 ## email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 ##
20
21 # Initialization of the test
22 from ModelAPI import *
23 from GeomDataAPI import *
24 from GeomAlgoAPI import *
25 from GeomAPI import *
26
27 import random
28
29 def createLine(theSketchFeature):
30     aSketchLineFeature = theSketchFeature.addFeature("SketchLine")
31     aSketchLineStartPoint = geomDataAPI_Point2D(aSketchLineFeature.attribute("StartPoint"))
32     aSketchLineEndPoint = geomDataAPI_Point2D(aSketchLineFeature.attribute("EndPoint"))
33     aSketchLineStartPoint.setValue(random.uniform(0, 100), random.uniform(0, 100))
34     aSketchLineEndPoint.setValue(random.uniform(0, 100), random.uniform(0, 100))
35
36 # Get document
37 aSession = ModelAPI_Session.get()
38 aDocument = aSession.moduleDocument()
39
40 # Create a part
41 aSession.startOperation()
42 aPartFeature = aDocument.addFeature("Part")
43 aSession.finishOperation()
44 aPartResult = modelAPI_ResultPart(aPartFeature.firstResult())
45 aPart = aPartResult.partDoc()
46
47 # Create a sketch
48 aSession.startOperation()
49 aSketchFeature = featureToCompositeFeature(aPart.addFeature("Sketch"))
50 anOrigin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
51 anOrigin.setValue(0, 0, 0)
52 aDirX = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
53 aDirX.setValue(1, 0, 0)
54 aNorm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
55 aNorm.setValue(0, 0, 1)
56
57 # Create lines
58 aNumOfLines = 10
59 for i in range(aNumOfLines):
60     createLine(aSketchFeature)
61 aSession.finishOperation()
62 aSketchResult = aSketchFeature.firstResult()
63 aSketchShape = aSketchResult.shape()
64
65 # Create edges
66 aSession.startOperation()
67 anEdgeFeature = aPart.addFeature("Edge")
68 aBaseObjectsList = anEdgeFeature.selectionList("base_objects")
69 aShapeExplorer = GeomAPI_ShapeExplorer(aSketchShape, GeomAPI_Shape.EDGE)
70 while aShapeExplorer.more():
71     aBaseObjectsList.append(aSketchResult, aShapeExplorer.current())
72     aShapeExplorer.next()
73 aSession.finishOperation()
74
75 # Test results
76 assert (len(anEdgeFeature.results()) == aNumOfLines)
77
78 # Test edge building on edge of another result
79 aSession.startOperation()
80 aBox = aPart.addFeature("Box")
81 aBox.string("CreationMethod").setValue("BoxByDimensions")
82 aBox.real("dx").setValue(50)
83 aBox.real("dy").setValue(50)
84 aBox.real("dz").setValue(50)
85 aSession.finishOperation()
86 aBoxResult = aBox.firstResult()
87 aBoxShape = aBoxResult.shape()
88
89 # Create edges
90 aSession.startOperation()
91 anEdgeFeature2 = aPart.addFeature("Edge")
92 aBaseObjectsList = anEdgeFeature2.selectionList("base_objects")
93 aShapeExplorer = GeomAPI_ShapeExplorer(aBoxShape, GeomAPI_Shape.EDGE)
94 aShapes = []
95 while aShapeExplorer.more():
96     # keep unique shapes only
97     aCurrent = aShapeExplorer.current()
98     isNewShape = True
99     for s in aShapes:
100         if s.isSame(aCurrent):
101             isNewShape = False
102             break
103     if isNewShape:
104         aShapes.append(aCurrent)
105     aShapeExplorer.next()
106
107 for s in aShapes:
108     aBaseObjectsList.append(aBoxResult, s)
109 aSession.finishOperation()
110
111 # Test results
112 assert (len(anEdgeFeature2.results()) == 12)
113
114 # Check Edge feature failed on incorrect input
115 aSession.startOperation()
116 anEdgeFeature3 = aPart.addFeature("Edge")
117 aBaseObjectsList = anEdgeFeature3.selectionList("base_objects")
118 aBaseObjectsList.append(aSketchResult, None)
119 aSession.finishOperation()
120 assert (len(anEdgeFeature3.results()) == 0)
121
122 aSession.startOperation()
123 aBaseObjectsList.clear()
124 aShapeExplorer = GeomAPI_ShapeExplorer(aBoxShape, GeomAPI_Shape.VERTEX)
125 aShape = aShapeExplorer.current()
126 aBaseObjectsList.append(aBoxResult, aShape)
127 aSession.finishOperation()
128 assert (len(anEdgeFeature3.results()) == 0)
129
130 aSession.startOperation()
131 aBaseObjectsList.clear()
132 aShapeExplorer = GeomAPI_ShapeExplorer(aBoxShape, GeomAPI_Shape.FACE)
133 aShape = aShapeExplorer.current()
134 aBaseObjectsList.append(aBoxResult, aShape)
135 aSession.finishOperation()
136 assert (len(anEdgeFeature3.results()) == 0)
137
138 # remove failed feature
139 aSession.startOperation()
140 aPart.removeFeature(anEdgeFeature3)
141 aSession.finishOperation()
142
143 from salome.shaper import model
144 assert(model.checkPythonDump())