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