Salome HOME
Update copyrights
[modules/shaper.git] / src / SketchPlugin / Test / TestConstraintAngle.py
1 # Copyright (C) 2014-2019  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     TestConstraintAngle.py
22     Unit test of SketchPlugin_ConstraintAngle class
23
24     SketchPlugin_ConstraintAngle
25         static const std::string MY_CONSTRAINT_ANGLE_ID("SketchConstraintAngle");
26         data()->addAttribute(SketchPlugin_Constraint::VALUE(),    ModelAPI_AttributeDouble::typeId());
27         data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
28         data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
29         data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
30
31
32 """
33 from GeomDataAPI import *
34 from ModelAPI import *
35 import math
36 from salome.shaper import model
37
38 #=========================================================================
39 # Auxiliary functions
40 #=========================================================================
41
42 def angle(theLine1, theLine2):
43     # subroutine to angle between two lines
44     aStartPoint1 = geomDataAPI_Point2D(theLine1.attribute("StartPoint"))
45     aEndPoint1   = geomDataAPI_Point2D(theLine1.attribute("EndPoint"))
46     aStartPoint2 = geomDataAPI_Point2D(theLine2.attribute("StartPoint"))
47     aEndPoint2   = geomDataAPI_Point2D(theLine2.attribute("EndPoint"))
48
49     aDirX1 = aEndPoint1.x() - aStartPoint1.x()
50     aDirY1 = aEndPoint1.y() - aStartPoint1.y()
51     aLen1 = math.hypot(aDirX1, aDirY1)
52     aDirX2 = aEndPoint2.x() - aStartPoint2.x()
53     aDirY2 = aEndPoint2.y() - aStartPoint2.y()
54     aLen2 = math.hypot(aDirX2, aDirY2)
55
56     aDot = aDirX1 * aDirX2 + aDirY1 * aDirY2
57
58     anAngle = math.acos(aDot / aLen1 / aLen2)
59     return round(anAngle * 180. / math.pi, 6)
60
61
62 #=========================================================================
63 # Initialization of the test
64 #=========================================================================
65
66 __updated__ = "2015-09-18"
67
68 ANGLE_DIRECT = 0
69 ANGLE_COMPLEMENTARY = 1
70 ANGLE_BACKWARD = 2
71
72 aSession = ModelAPI_Session.get()
73 aDocument = aSession.moduleDocument()
74 #=========================================================================
75 # Creation of a sketch
76 #=========================================================================
77 aSession.startOperation()
78 aSketchCommonFeature = aDocument.addFeature("Sketch")
79 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
80 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
81 origin.setValue(0, 0, 0)
82 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
83 dirx.setValue(1, 0, 0)
84 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
85 norm.setValue(0, 0, 1)
86 aSession.finishOperation()
87 #=========================================================================
88 # Create two lines
89 #=========================================================================
90 aSession.startOperation()
91 aSketchLineA = aSketchFeature.addFeature("SketchLine")
92 aStartPoint = geomDataAPI_Point2D(aSketchLineA.attribute("StartPoint"))
93 aEndPoint = geomDataAPI_Point2D(aSketchLineA.attribute("EndPoint"))
94 aStartPoint.setValue(-10., 25.)
95 aEndPoint.setValue(100., 25.)
96
97 aSketchLineB = aSketchFeature.addFeature("SketchLine")
98 aStartPoint = geomDataAPI_Point2D(aSketchLineB.attribute("StartPoint"))
99 aEndPoint = geomDataAPI_Point2D(aSketchLineB.attribute("EndPoint"))
100 aStartPoint.setValue(-10., 15.)
101 aEndPoint.setValue(80., 50.)
102 aSession.finishOperation()
103 assert (model.dof(aSketchFeature) == 8)
104 #=========================================================================
105 # Make a constraint to keep the angle
106 #=========================================================================
107 ANGLE_DEGREE = 30.
108 aSession.startOperation()
109 aConstraint = aSketchFeature.addFeature("SketchConstraintAngle")
110 aConstraint.integer("AngleType").setValue(ANGLE_DIRECT)
111 anAngleVal = aConstraint.real("AngleValue")
112 refattrA = aConstraint.refattr("ConstraintEntityA")
113 refattrB = aConstraint.refattr("ConstraintEntityB")
114 assert (not anAngleVal.isInitialized())
115 assert (not refattrA.isInitialized())
116 assert (not refattrB.isInitialized())
117 refattrA.setObject(aSketchLineA.firstResult())
118 refattrB.setObject(aSketchLineB.firstResult())
119 anAngleVal.setValue(ANGLE_DEGREE)
120 aConstraint.execute()
121 aSession.finishOperation()
122 aSession.startOperation()
123 aFlyoutPoint = geomDataAPI_Point2D(aConstraint.attribute("ConstraintFlyoutValuePnt"))
124 aFlyoutPoint.setValue(50.0, 100.0)
125 aSession.finishOperation()
126 aSession.abortOperation()
127 assert (anAngleVal.isInitialized())
128 assert (refattrA.isInitialized())
129 assert (refattrB.isInitialized())
130 assert (angle(aSketchLineA, aSketchLineB) == ANGLE_DEGREE)
131 assert (model.dof(aSketchFeature) == 7)
132 #=========================================================================
133 # Move line, check that angle is constant
134 #=========================================================================
135 aSession.startOperation()
136 aStartPoint = geomDataAPI_Point2D(aSketchLineA.attribute("StartPoint"))
137 aStartPoint.setValue(0., -30.)
138 aConstraint.execute()
139 aSession.finishOperation()
140 assert (angle(aSketchLineA, aSketchLineB) == ANGLE_DEGREE)
141 assert (model.dof(aSketchFeature) == 7)
142 #=========================================================================
143 # Change angle value and check the lines are moved
144 #=========================================================================
145 NEW_ANGLE_DEGREE = 60.
146 aSession.startOperation()
147 anAngleVal.setValue(NEW_ANGLE_DEGREE)
148 aConstraint.execute()
149 aSession.finishOperation()
150 assert (angle(aSketchLineA, aSketchLineB) == NEW_ANGLE_DEGREE)
151 assert (model.dof(aSketchFeature) == 7)
152 #=========================================================================
153 # Change angle type
154 #=========================================================================
155 aSession.startOperation()
156 aConstraint.integer("AngleType").setValue(ANGLE_COMPLEMENTARY)
157 aSession.finishOperation()
158 assert (angle(aSketchLineA, aSketchLineB) == NEW_ANGLE_DEGREE)
159 aSession.startOperation()
160 aConstraint.integer("AngleType").setValue(ANGLE_BACKWARD)
161 aSession.finishOperation()
162 assert (angle(aSketchLineA, aSketchLineB) == NEW_ANGLE_DEGREE)
163 assert (model.dof(aSketchFeature) == 7)
164 #=========================================================================
165 # Remove constraint, move line's point to check the constraint is not applied
166 #=========================================================================
167 aSession.startOperation()
168 aDocument.removeFeature(aConstraint)
169 aSession.finishOperation()
170 assert (model.dof(aSketchFeature) == 8)
171 aSession.startOperation()
172 aStartPoint.setValue(-30., 0.)
173 aSession.finishOperation()
174 assert (angle(aSketchLineA, aSketchLineB) != NEW_ANGLE_DEGREE)
175 assert (model.dof(aSketchFeature) == 8)
176 #=========================================================================
177 # End of test
178 #=========================================================================
179
180 assert(model.checkPythonDump())