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