Salome HOME
Copyright update 2021
[modules/shaper.git] / src / SketchPlugin / Test / TestRectangle.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     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 aStartCorner = geomDataAPI_Point2D(aRectangle.attribute("RectStartPoint"))
74 aEndCorner = geomDataAPI_Point2D(aRectangle.attribute("RectEndPoint"))
75 aStartCorner.setValue(10., 10.)
76 aEndCorner.setValue(40., 30.)
77 aSession.finishOperation()
78 #=========================================================================
79 # Check the lines of rectangle are parallel to the axes
80 #=========================================================================
81 aNbSubs = aSketchFeature.numberOfSubs()
82 aNbLines = 0
83 for i in range (0, aNbSubs):
84     aFeature = objectToFeature(aSketchFeature.subFeature(i))
85     if aFeature.getKind() == "SketchLine":
86         aLastLine = aFeature
87         assert (isHorizontal(aLastLine) or isVertical(aLastLine))
88         aNbLines = aNbLines + 1
89 assert (aNbLines == 4)
90 assert (model.dof(aSketchFeature) == 4)
91 #=========================================================================
92 # Move one of lines
93 #=========================================================================
94 aSession.startOperation()
95 aLineEnd = geomDataAPI_Point2D(aLastLine.attribute("EndPoint"))
96 aLineEnd.setValue(50., 50.)
97 aSession.finishOperation()
98 #=========================================================================
99 # Check the lines of rectangle are parallel to the axes
100 #=========================================================================
101 aNbSubs = aSketchFeature.numberOfSubs()
102 aNbLines = 0
103 for i in range (0, aNbSubs):
104     aFeature = objectToFeature(aSketchFeature.subFeature(i))
105     if aFeature.getKind() == "SketchLine":
106         aLastLine = aFeature
107         assert (isHorizontal(aLastLine) or isVertical(aLastLine))
108         aNbLines = aNbLines + 1
109 assert (aNbLines == 4)
110 assert (model.dof(aSketchFeature) == 4)
111 #=========================================================================
112 # End of test
113 #=========================================================================
114
115 assert(model.checkPythonDump())