Salome HOME
6c47c06ed6b2c30b852ec43beae518b71f074ae0
[modules/shaper.git] / src / SketchPlugin / Test / TestRectangle1.py
1 # Copyright (C) 2014-2024  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 """
21     TestRectangle1.py
22     Unit test of SketchPlugin_Rectangle class
23
24 """
25 from GeomDataAPI import *
26 from ModelAPI import *
27 from GeomAPI import *
28 import math
29 from salome.shaper import model
30
31 #=========================================================================
32 # Initialization of the test
33 #=========================================================================
34
35 __updated__ = "2016-02-05"
36
37
38 #=========================================================================
39 # Auxiliary functions
40 #=========================================================================
41 def isHorizontal(line):
42     aStart = geomDataAPI_Point2D(line.attribute("StartPoint"))
43     aEnd   = geomDataAPI_Point2D(line.attribute("EndPoint"))
44     return aStart.y() == aEnd.y()
45
46 def isVertical(line):
47     aStart = geomDataAPI_Point2D(line.attribute("StartPoint"))
48     aEnd   = geomDataAPI_Point2D(line.attribute("EndPoint"))
49     return aStart.x() == aEnd.x()
50
51 def isPerpendicular(line1, line2, tolerance):
52     aStart1 = geomDataAPI_Point2D(line1.attribute("StartPoint"))
53     aEnd1   = geomDataAPI_Point2D(line1.attribute("EndPoint"))
54     aStart2 = geomDataAPI_Point2D(line2.attribute("StartPoint"))
55     aEnd2   = geomDataAPI_Point2D(line2.attribute("EndPoint"))
56     anAngle = abs(GeomAPI_Angle2d(aStart1.pnt(), aEnd1.pnt(), aStart2.pnt(), aEnd2.pnt()).angleDegree())
57     #print("Angle = ", anAngle)
58     return abs(anAngle-90) < tolerance # or abs(anAngle-270) < tolerance
59
60 #=========================================================================
61 # Start of test
62 #=========================================================================
63 aSession = ModelAPI_Session.get()
64 aDocument = aSession.moduleDocument()
65 #=========================================================================
66 # Creation of a sketch
67 #=========================================================================
68 aSession.startOperation()
69 aSketchFeature = featureToCompositeFeature(aDocument.addFeature("Sketch"))
70 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
71 origin.setValue(0, 0, 0)
72 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
73 dirx.setValue(1, 0, 0)
74 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
75 norm.setValue(0, 0, 1)
76 aSession.finishOperation()
77 #=========================================================================
78 # Create a rectangle
79 #=========================================================================
80 aSession.startOperation()
81 aRectangle = aSketchFeature.addFeature("SketchRectangle")
82 aRectangle.string("RectangleType").setValue("RectangleTypeByCorners")
83 aStartCorner = geomDataAPI_Point2D(aRectangle.attribute("RectStartPoint"))
84 aEndCorner = geomDataAPI_Point2D(aRectangle.attribute("RectEndPoint"))
85 aStartCorner.setValue(10., 10.)
86 aEndCorner.setValue(40., 30.)
87 aSession.finishOperation()
88 #=========================================================================
89 # Check the lines of rectangle are parallel to the axes
90 #=========================================================================
91 aNbSubs = aSketchFeature.numberOfSubs()
92 aNbLines = 0
93 for i in range (0, aNbSubs):
94     aFeature = objectToFeature(aSketchFeature.subFeature(i))
95     if aFeature.getKind() == "SketchLine":
96         aLastLine = aFeature
97         assert (isHorizontal(aLastLine) or isVertical(aLastLine))
98         aNbLines = aNbLines + 1
99 assert (aNbLines == 4)
100 assert (model.dof(aSketchFeature) == 5)
101 #=========================================================================
102 # Move one of lines
103 #=========================================================================
104 aSession.startOperation()
105 aLineEnd = geomDataAPI_Point2D(aLastLine.attribute("EndPoint"))
106 aLineEnd.setValue(41., 30.)
107 aSession.finishOperation()
108 #=========================================================================
109 # Check that we have still two pairs of lines with same length
110 # and that connected lines are perpendicular
111 #=========================================================================
112 aNbSubs = aSketchFeature.numberOfSubs()
113 aNbLines = 0
114 tolerance = 1.e-5
115 valref = [0, 0]
116 aPrevLine = None
117 for i in range (0, aNbSubs):
118     aFeature = objectToFeature(aSketchFeature.subFeature(i))
119     if aFeature.getKind() == "SketchLine":
120         aLastLine = aFeature
121         # Opposite lines must have same length
122         edgeLen = aLastLine.lastResult().shape().edge().length()
123         if (valref[i%2] == 0):
124             valref[i%2] = edgeLen
125         else:
126             assert(abs(edgeLen-valref[i%2]) <= tolerance)
127         # Connected lines must be perpendicular
128         if aPrevLine != None:
129             assert(isPerpendicular(aPrevLine, aLastLine, tolerance))
130         aPrevLine = aLastLine
131         aNbLines = aNbLines + 1
132 assert (aNbLines == 4)
133 assert (model.dof(aSketchFeature) == 5)
134 #=========================================================================
135 # End of test
136 #=========================================================================
137
138 assert(model.checkPythonDump())