Salome HOME
Copyright update 2022
[modules/shaper.git] / src / SketchPlugin / Test / TestRectangle.py
1 # Copyright (C) 2014-2022  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     TestRectangle.py
22     Unit test of SketchPlugin_Ractangle class
23
24 """
25 from GeomDataAPI import *
26 from ModelAPI import *
27 import math
28 from salome.shaper import model
29
30 #=========================================================================
31 # Initialization of the test
32 #=========================================================================
33
34 __updated__ = "2016-02-05"
35
36
37 #=========================================================================
38 # Auxiliary functions
39 #=========================================================================
40 def isHorizontal(line):
41     aStart = geomDataAPI_Point2D(line.attribute("StartPoint"))
42     aEnd   = geomDataAPI_Point2D(line.attribute("EndPoint"))
43     return aStart.y() == aEnd.y()
44
45 def isVertical(line):
46     aStart = geomDataAPI_Point2D(line.attribute("StartPoint"))
47     aEnd   = geomDataAPI_Point2D(line.attribute("EndPoint"))
48     return aStart.x() == aEnd.x()
49
50
51 #=========================================================================
52 # Start of test
53 #=========================================================================
54 aSession = ModelAPI_Session.get()
55 aDocument = aSession.moduleDocument()
56 #=========================================================================
57 # Creation of a sketch
58 #=========================================================================
59 aSession.startOperation()
60 aSketchFeature = featureToCompositeFeature(aDocument.addFeature("Sketch"))
61 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
62 origin.setValue(0, 0, 0)
63 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
64 dirx.setValue(1, 0, 0)
65 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
66 norm.setValue(0, 0, 1)
67 aSession.finishOperation()
68 #=========================================================================
69 # Create a rectangle
70 #=========================================================================
71 aSession.startOperation()
72 aRectangle = aSketchFeature.addFeature("SketchRectangle")
73 aRectangle.string("RectangleType").setValue("RectangleTypeByCorners")
74 aStartCorner = geomDataAPI_Point2D(aRectangle.attribute("RectStartPoint"))
75 aEndCorner = geomDataAPI_Point2D(aRectangle.attribute("RectEndPoint"))
76 aStartCorner.setValue(10., 10.)
77 aEndCorner.setValue(40., 30.)
78 aSession.finishOperation()
79 #=========================================================================
80 # Check the lines of rectangle are parallel to the axes
81 #=========================================================================
82 aNbSubs = aSketchFeature.numberOfSubs()
83 aNbLines = 0
84 for i in range (0, aNbSubs):
85     aFeature = objectToFeature(aSketchFeature.subFeature(i))
86     if aFeature.getKind() == "SketchLine":
87         aLastLine = aFeature
88         assert (isHorizontal(aLastLine) or isVertical(aLastLine))
89         aNbLines = aNbLines + 1
90 assert (aNbLines == 4)
91 assert (model.dof(aSketchFeature) == 4)
92 #=========================================================================
93 # Move one of lines
94 #=========================================================================
95 aSession.startOperation()
96 aLineEnd = geomDataAPI_Point2D(aLastLine.attribute("EndPoint"))
97 aLineEnd.setValue(50., 50.)
98 aSession.finishOperation()
99 #=========================================================================
100 # Check the lines of rectangle are parallel to the axes
101 #=========================================================================
102 aNbSubs = aSketchFeature.numberOfSubs()
103 aNbLines = 0
104 for i in range (0, aNbSubs):
105     aFeature = objectToFeature(aSketchFeature.subFeature(i))
106     if aFeature.getKind() == "SketchLine":
107         aLastLine = aFeature
108         assert (isHorizontal(aLastLine) or isVertical(aLastLine))
109         aNbLines = aNbLines + 1
110 assert (aNbLines == 4)
111 assert (model.dof(aSketchFeature) == 4)
112 #=========================================================================
113 # End of test
114 #=========================================================================
115
116 assert(model.checkPythonDump())