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