Salome HOME
Added python dump checking for every unit test where it is useful.
[modules/shaper.git] / src / SketchPlugin / Test / TestHighload.py
1 """
2     TestHighload.py
3 """
4 from GeomAPI import *
5 from GeomDataAPI import *
6 from ModelAPI import *
7 import math
8
9
10 #=========================================================================
11 # Useful subroutines
12 #=========================================================================
13 def distance(pointA, pointB):
14     """
15     subroutine to calculate distance between two points
16     result of calculated distance is has 10**-5 precision
17     """
18     xdiff = math.pow((pointA.x() - pointB.x()), 2)
19     ydiff = math.pow((pointA.y() - pointB.y()), 2)
20     return round(math.sqrt(xdiff + ydiff), 5)
21
22
23 def createNAngle(theSketch, theN, theRadius, theEdgeLength=0):
24     # Create an N-Angle at (0,0)
25     rad = 2. * math.pi / theN
26     points = []
27     for a in xrange(theN):
28         x = round(math.cos(rad * a), 10) * theRadius
29         y = round(math.sin(rad * a), 10) * theRadius
30         points.append((x, y))
31     # Close the contour
32     points.append(points[0])
33
34     # Create lines iterating over (A,B), (B,C), ... (N,A)
35     allStartPoints = []
36     allEndPoints = []
37     allLines = []
38     for begin, end in zip(points[:-1], points[1:]):
39         aSketchLine = theSketch.addFeature("SketchLine")
40         aStartPoint = geomDataAPI_Point2D(aSketchLine.attribute("StartPoint"))
41         anEndPoint = geomDataAPI_Point2D(aSketchLine.attribute("EndPoint"))
42         aStartPoint.setValue(begin[0], begin[1])
43         anEndPoint.setValue(end[0], end[1])
44         allStartPoints.append(aStartPoint)
45         allEndPoints.append(anEndPoint)
46         allLines.append(aSketchLine)
47     theSketch.execute()
48     # Shift a start point to the end of list:
49     # (Bb, Cb, Ab) zip (Ae, Be, Ce) --> ((Ae, Bb), (Cb, Be), (Ab, Ce))
50     allStartPoints.append(allStartPoints.pop(0))
51     # Make lines coincident:
52     for pointA, pointB in zip(allStartPoints, allEndPoints):
53         aCoincidence = theSketch.addFeature("SketchConstraintCoincidence")
54         aCoincidence.refattr("ConstraintEntityA").setAttr(pointA)
55         aCoincidence.refattr("ConstraintEntityB").setAttr(pointB)
56     return allLines
57
58
59 def fixLineLength(theSketch, theLines, theEdgeLength=0):
60     aFirstLineLength = theEdgeLength
61     # Make give lines fixed length:
62     for aLine in theLines:
63         aLengthConstraint = aSketchFeature.addFeature("SketchConstraintLength")
64         refattrA = aLengthConstraint.refattr("ConstraintEntityA")
65         refattrA.setObject(modelAPI_ResultConstruction(aLine.firstResult()))
66         if aFirstLineLength == 0:
67             pointA = geomDataAPI_Point2D(aLine.attribute("StartPoint"))
68             pointB = geomDataAPI_Point2D(aLine.attribute("EndPoint"))
69             aFirstLineLength = distance(pointA, pointB)
70         aLengthConstraint.real("ConstraintValue").setValue(aFirstLineLength)
71
72
73 def moveTo(theLines, theDeltaX, theDeltaY):
74     for aLine in theLines:
75         aStart = geomDataAPI_Point2D(aLine.attribute("StartPoint"))
76         anEnd = geomDataAPI_Point2D(aLine.attribute("EndPoint"))
77         aStart.setValue(aStart.x() + theDeltaX, aStart.y() + theDeltaY)
78         anEnd.setValue(anEnd.x() + theDeltaX, anEnd.y() + theDeltaY)
79
80 #=========================================================================
81 # Initialization of the test
82 #=========================================================================
83
84 __updated__ = "2014-11-27"
85
86 aSession = ModelAPI_Session.get()
87 aDocument = aSession.moduleDocument()
88 #=========================================================================
89 # Creation of a sketch
90 #=========================================================================
91 aSession.startOperation()
92 aSketchCommonFeature = aDocument.addFeature("Sketch")
93 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
94 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
95 origin.setValue(0, 0, 0)
96 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
97 dirx.setValue(1, 0, 0)
98 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
99 norm.setValue(0, 0, 1)
100 aSession.finishOperation()
101 #=========================================================================
102 # Create 4x4 polygons N = {5, 21}
103 #=========================================================================
104 deltaX = deltaY = 50.
105 n = 5
106 aSession.startOperation()
107 for i in xrange(4):
108     for j in xrange(4):
109         allNangleLines = createNAngle(aSketchFeature, n, 50)
110         fixLineLength(aSketchFeature, allNangleLines)
111         moveTo(allNangleLines, deltaX, deltaY)
112         n += 1
113         deltaX += 110.
114     deltaY += 110.
115     deltaX = 50.
116 aSession.finishOperation()
117
118
119 #=========================================================================
120 # End of test
121 #=========================================================================
122
123 import model
124 assert(model.checkPythonDump())