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