1 ## Copyright (C) 2014-2017 CEA/DEN, EDF R&D
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.
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.
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
17 ## See http:##www.salome-platform.org/ or
18 ## email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
22 TestConstraintAngle.py
23 Unit test of SketchPlugin_ConstraintAngle class
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());
34 from GeomDataAPI import *
35 from ModelAPI import *
37 from salome.shaper import model
39 #=========================================================================
41 #=========================================================================
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"))
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)
57 aDot = aDirX1 * aDirX2 + aDirY1 * aDirY2
59 anAngle = math.acos(aDot / aLen1 / aLen2)
60 return round(anAngle * 180. / math.pi, 6)
63 #=========================================================================
64 # Initialization of the test
65 #=========================================================================
67 __updated__ = "2015-09-18"
70 ANGLE_COMPLEMENTARY = 1
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 #=========================================================================
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.)
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 #=========================================================================
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 #=========================================================================
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 #=========================================================================
179 #=========================================================================
181 assert(model.checkPythonDump())