Salome HOME
Fix for the issue #2753 : error when dump/load script
[modules/shaper.git] / src / SketchPlugin / Test / TestConstraintRadius.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     TestConstraintRadius.py
23     Unit test of SketchPlugin_ConstraintRadius class
24
25     SketchPlugin_Constraint
26         static const std::string MY_CONSTRAINT_VALUE("ConstraintValue");
27         static const std::string MY_FLYOUT_VALUE_PNT("ConstraintFlyoutValuePnt");
28         static const std::string MY_ENTITY_A("ConstraintEntityA");
29         static const std::string MY_ENTITY_B("ConstraintEntityB");
30         static const std::string MY_ENTITY_C("ConstraintEntityC");
31         static const std::string MY_ENTITY_D("ConstraintEntityD");
32
33     SketchPlugin_ConstraintRadius
34         static const std::string MY_CONSTRAINT_RADIUS_ID("SketchConstraintRadius");
35         data()->addAttribute(SketchPlugin_Constraint::VALUE(),    ModelAPI_AttributeDouble::typeId());
36         data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
37         data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
38
39 """
40 from GeomDataAPI import *
41 from ModelAPI import *
42 import math
43 from salome.shaper import model
44
45 #=========================================================================
46 # Initialization of the test
47 #=========================================================================
48
49 __updated__ = "2014-10-28"
50
51
52 aSession = ModelAPI_Session.get()
53 aDocument = aSession.moduleDocument()
54 #=========================================================================
55 # Creation of a sketch
56 #=========================================================================
57 aSession.startOperation()
58 aSketchCommonFeature = aDocument.addFeature("Sketch")
59 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
60 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
61 origin.setValue(0, 0, 0)
62 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
63 dirx.setValue(1, 0, 0)
64 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
65 norm.setValue(0, 0, 1)
66 aSession.finishOperation()
67 #=========================================================================
68 # Creation of an arc and a circle
69 #=========================================================================
70 aSession.startOperation()
71 aSketchArc = aSketchFeature.addFeature("SketchArc")
72 anArcCentr = geomDataAPI_Point2D(aSketchArc.attribute("center_point"))
73 anArcCentr.setValue(10., 10.)
74 anArcStartPoint = geomDataAPI_Point2D(aSketchArc.attribute("start_point"))
75 anArcStartPoint.setValue(0., 50.)
76 anArcEndPoint = geomDataAPI_Point2D(aSketchArc.attribute("end_point"))
77 anArcEndPoint.setValue(50., 0.)
78 aSession.finishOperation()
79 assert (model.dof(aSketchFeature) == 5)
80 # Test changing the arc start/end point
81 aSession.startOperation()
82 anArcStartPoint.setValue(anArcStartPoint.x(), 40.)
83 anArcStartPoint.setValue(0., 50.)
84 assert (math.hypot(anArcStartPoint.x() - 0., anArcStartPoint.y() - 50.) < 1.e-10)
85 aSession.finishOperation()
86 aSession.startOperation()
87 anArcEndPoint.setValue(40., anArcEndPoint.y())
88 anArcEndPoint.setValue(50., 0.)
89 assert (math.hypot(anArcEndPoint.x() - 50., anArcEndPoint.y() - 0.) < 1.e-10)
90 aSession.finishOperation()
91 assert (model.dof(aSketchFeature) == 5)
92 # Circle
93 aSession.startOperation()
94 aSketchCircle = aSketchFeature.addFeature("SketchCircle")
95 anCircleCentr = geomDataAPI_Point2D(aSketchCircle.attribute("circle_center"))
96 aCircleRadius = aSketchCircle.real("circle_radius")
97 anCircleCentr.setValue(-25., -25)
98 aCircleRadius.setValue(25.)
99 aSession.finishOperation()
100 assert (model.dof(aSketchFeature) == 8)
101 #=========================================================================
102 # Make a constraint to keep the radius of the arc
103 #=========================================================================
104 RADIUS = 40
105 aSession.startOperation()
106 aConstraint = aSketchFeature.addFeature("SketchConstraintRadius")
107 aRadius = aConstraint.real("ConstraintValue")
108 aRefObject = aConstraint.refattr("ConstraintEntityA")
109 aResult = aSketchArc.lastResult()
110 assert (aResult is not None)
111 aRefObject.setObject(modelAPI_ResultConstruction(aResult))
112 aRadius.setValue(RADIUS)
113 aSession.finishOperation()
114 assert (aRadius.isInitialized())
115 assert (aRefObject.isInitialized())
116 assert (model.dof(aSketchFeature) == 7)
117 #=========================================================================
118 # Make a constraint to keep the radius of the circle
119 #=========================================================================
120 aSession.startOperation()
121 aConstraint = aSketchFeature.addFeature("SketchConstraintRadius")
122 aRadius = aConstraint.real("ConstraintValue")
123 aRefObject = aConstraint.refattr("ConstraintEntityA")
124 aResult = aSketchCircle.lastResult()
125 assert (aResult is not None)
126 aRefObject.setObject(modelAPI_ResultConstruction(aResult))
127 aRadius.setValue(RADIUS)
128 aSession.finishOperation()
129 assert (aRadius.isInitialized())
130 assert (aRefObject.isInitialized())
131 assert (model.dof(aSketchFeature) == 6)
132 #=========================================================================
133 # Perform some actions and checks:
134 # 1. Check that constraints does not change values
135 # 2. Move one point of the arc
136 # 3. Check that second point is moved also
137 #=========================================================================
138 distCS = model.distancePointPoint(anArcCentr, anArcStartPoint)
139 distCE = model.distancePointPoint(anArcCentr, anArcEndPoint)
140 assert (math.fabs(distCS - RADIUS) < 1.e-10)
141 assert (math.fabs(distCE - RADIUS) < 1.e-10)
142 anArcPrevEndPointX = anArcEndPoint.x()
143 anArcPrevEndPointY = anArcEndPoint.y()
144 # Move one point of the arc
145 aSession.startOperation()
146 anArcStartPoint.setValue(0, 60)
147 aSession.finishOperation()
148 assert (anArcEndPoint.x() != anArcPrevEndPointX)
149 assert (anArcEndPoint.y() != anArcPrevEndPointY)
150 distCS = model.distancePointPoint(anArcCentr, anArcStartPoint)
151 distCE = model.distancePointPoint(anArcCentr, anArcEndPoint)
152 assert (math.fabs(distCS - RADIUS) < 1.e-10)
153 assert (math.fabs(distCE - RADIUS) < 1.e-10)
154 assert (model.dof(aSketchFeature) == 6)
155 #=========================================================================
156 # 4. Move the centr or the point of the arc
157 # 5. Check radius is the same
158 #=========================================================================
159 assert (anCircleCentr.x() == -25)
160 assert (anCircleCentr.y() == -25)
161 assert (aCircleRadius.value() == RADIUS)
162 aSession.startOperation()
163 anCircleCentr.setValue(100., 100.)
164 aSession.finishOperation()
165 assert (anCircleCentr.x() == 100)
166 assert (anCircleCentr.y() == 100)
167 assert (aCircleRadius.value() == RADIUS)
168 assert (model.dof(aSketchFeature) == 6)
169 #=========================================================================
170 # End of test
171 #=========================================================================
172
173 assert(model.checkPythonDump())