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