Salome HOME
Test case for SubShapes
[modules/shaper.git] / src / BuildPlugin / Test / TestSubShapes.py
1 # Initialization of the test
2 from ModelAPI import *
3 from GeomDataAPI import *
4 from GeomAlgoAPI import *
5 from GeomAPI import *
6
7 import random
8
9 def createPoint(theSketchFeature):
10     aSketchPointFeature = theSketchFeature.addFeature("SketchPoint")
11     aPointCoordindates = geomDataAPI_Point2D(aSketchPointFeature.attribute("PointCoordindates"))
12     aPointCoordindates.setValue(random.uniform(0, 50), random.uniform(0, 50))
13     return aSketchPointFeature
14
15 def createLine(theSketchFeature):
16     aSketchLineFeature = theSketchFeature.addFeature("SketchLine")
17     aSketchLineStartPoint = geomDataAPI_Point2D(aSketchLineFeature.attribute("StartPoint"))
18     aSketchLineEndPoint = geomDataAPI_Point2D(aSketchLineFeature.attribute("EndPoint"))
19     aSketchLineStartPoint.setValue(random.uniform(0, 50), random.uniform(0, 50))
20     aSketchLineEndPoint.setValue(random.uniform(0, 50), random.uniform(0, 50))
21     return aSketchLineFeature
22
23 # Get document
24 aSession = ModelAPI_Session.get()
25 aDocument = aSession.moduleDocument()
26
27 # Create a part
28 aSession.startOperation()
29 aPartFeature = aDocument.addFeature("Part")
30 aSession.finishOperation()
31 aPartResult = modelAPI_ResultPart(aPartFeature.firstResult())
32 aPart = aPartResult.partDoc()
33
34 # Create a sketch
35 aSession.startOperation()
36 aSketchFeature = featureToCompositeFeature(aPart.addFeature("Sketch"))
37 anOrigin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
38 anOrigin.setValue(0, 0, 0)
39 aDirX = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
40 aDirX.setValue(1, 0, 0)
41 aNorm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
42 aNorm.setValue(0, 0, 1)
43
44 # Create lines
45 aSketchLineFeature = aSketchFeature.addFeature("SketchLine")
46 aSketchLineStartPoint = geomDataAPI_Point2D(aSketchLineFeature.attribute("StartPoint"))
47 aSketchLineEndPoint = geomDataAPI_Point2D(aSketchLineFeature.attribute("EndPoint"))
48 aSketchLineStartPoint.setValue(0, 0)
49 aSketchLineEndPoint.setValue(0, 50)
50 aSketchLineFeature = aSketchFeature.addFeature("SketchLine")
51 aSketchLineStartPoint = geomDataAPI_Point2D(aSketchLineFeature.attribute("StartPoint"))
52 aSketchLineEndPoint = geomDataAPI_Point2D(aSketchLineFeature.attribute("EndPoint"))
53 aSketchLineStartPoint.setValue(0, 50)
54 aSketchLineEndPoint.setValue(50, 50)
55 aSketchLineFeature = aSketchFeature.addFeature("SketchLine")
56 aSketchLineStartPoint = geomDataAPI_Point2D(aSketchLineFeature.attribute("StartPoint"))
57 aSketchLineEndPoint = geomDataAPI_Point2D(aSketchLineFeature.attribute("EndPoint"))
58 aSketchLineStartPoint.setValue(50, 50)
59 aSketchLineEndPoint.setValue(50, 0)
60 aSketchLineFeature = aSketchFeature.addFeature("SketchLine")
61 aSketchLineStartPoint = geomDataAPI_Point2D(aSketchLineFeature.attribute("StartPoint"))
62 aSketchLineEndPoint = geomDataAPI_Point2D(aSketchLineFeature.attribute("EndPoint"))
63 aSketchLineStartPoint.setValue(50, 0)
64 aSketchLineEndPoint.setValue(0, 0)
65
66 aSession.finishOperation()
67 aSketchResult = aSketchFeature.firstResult()
68 aSketchShape = aSketchResult.shape()
69
70 # Create face
71 aSession.startOperation()
72 aFaceFeature = aPart.addFeature("Face")
73 aBaseObjectsList = aFaceFeature.selectionList("base_objects")
74 aShapeExplorer = GeomAPI_ShapeExplorer(aSketchShape, GeomAPI_Shape.EDGE)
75 while aShapeExplorer.more():
76     aBaseObjectsList.append(aSketchResult, aShapeExplorer.current())
77     aShapeExplorer.next()
78 aSession.finishOperation()
79 aFaceResult = aFaceFeature.firstResult()
80
81 # Create a sketch with points and lines
82 aSession.startOperation()
83 aSketchFeature = featureToCompositeFeature(aPart.addFeature("Sketch"))
84 anOrigin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
85 anOrigin.setValue(0, 0, 0)
86 aDirX = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
87 aDirX.setValue(1, 0, 0)
88 aNorm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
89 aNorm.setValue(0, 0, 1)
90
91 # Create points
92 aNumOfPoints = 10
93 aPoints = []
94 for i in range(aNumOfPoints):
95     aSession.startOperation()
96     aSketchPointFeature = createPoint(aSketchFeature)
97     aSession.finishOperation()
98     aPoints.append(aSketchPointFeature.firstResult().shape())
99
100 # Create lines
101 aNumOfLines = 10
102 aLines = []
103 for i in range(aNumOfLines):
104     aSession.startOperation()
105     aSketchLineFeature = createLine(aSketchFeature)
106     aSession.finishOperation()
107     aLines.append(aSketchLineFeature.firstResult().shape())
108
109 aSession.finishOperation()
110 aSketchResult = aSketchFeature.firstResult()
111 aSketchShape = aSketchResult.shape()
112
113 # Create sub-shapes
114 aSession.startOperation()
115 aSubShapesFeature = aPart.addFeature("SubShapes")
116 aBaseShapeSelection = aSubShapesFeature.selection("base_shape")
117 aBaseShapeSelection.setValue(aFaceResult, None)
118 aSubShapesList = aSubShapesFeature.selectionList("sub_shapes")
119 for aPoint in aPoints:
120     aSubShapesList.append(aSketchResult, aPoint)
121 aShapeExplorer = GeomAPI_ShapeExplorer(aSketchShape, GeomAPI_Shape.EDGE)
122 while aShapeExplorer.more():
123     aSubShapesList.append(aSketchResult, aShapeExplorer.current())
124     aShapeExplorer.next()
125 aSession.finishOperation()
126
127 # Test results
128 assert (len(aSubShapesFeature.results()) > 0)