Salome HOME
Porting to SALOME_8.2.0
[modules/shaper.git] / src / SketchPlugin / Test / TestConstraintRadius.py
1 """
2     TestConstraintRadius.py
3     Unit test of SketchPlugin_ConstraintRadius class
4     
5     SketchPlugin_Constraint
6         static const std::string MY_CONSTRAINT_VALUE("ConstraintValue");
7         static const std::string MY_FLYOUT_VALUE_PNT("ConstraintFlyoutValuePnt");
8         static const std::string MY_ENTITY_A("ConstraintEntityA");
9         static const std::string MY_ENTITY_B("ConstraintEntityB");
10         static const std::string MY_ENTITY_C("ConstraintEntityC");
11         static const std::string MY_ENTITY_D("ConstraintEntityD");
12         
13     SketchPlugin_ConstraintRadius
14         static const std::string MY_CONSTRAINT_RADIUS_ID("SketchConstraintRadius");
15         data()->addAttribute(SketchPlugin_Constraint::VALUE(),    ModelAPI_AttributeDouble::typeId());
16         data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
17         data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
18
19 """
20 from GeomDataAPI import *
21 from ModelAPI import *
22 import math
23 #=========================================================================
24 # Initialization of the test
25 #=========================================================================
26
27 __updated__ = "2014-10-28"
28
29 def distancePointPoint(pointA, pointB):
30     """
31     subroutine to calculate distance between two points
32     result of calculated distance is has 10**-5 precision
33     """
34     xdiff = math.pow((pointA.x() - pointB.x()), 2)
35     ydiff = math.pow((pointA.y() - pointB.y()), 2)
36     return round(math.sqrt(xdiff + ydiff), 5)
37
38
39 aSession = ModelAPI_Session.get()
40 aDocument = aSession.moduleDocument()
41 #=========================================================================
42 # Creation of a sketch
43 #=========================================================================
44 aSession.startOperation()
45 aSketchCommonFeature = aDocument.addFeature("Sketch")
46 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
47 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
48 origin.setValue(0, 0, 0)
49 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
50 dirx.setValue(1, 0, 0)
51 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
52 norm.setValue(0, 0, 1)
53 aSession.finishOperation()
54 #=========================================================================
55 # Creation of an arc and a circle
56 #=========================================================================
57 aSession.startOperation()
58 aSketchArc = aSketchFeature.addFeature("SketchArc")
59 anArcCentr = geomDataAPI_Point2D(aSketchArc.attribute("ArcCenter"))
60 anArcCentr.setValue(10., 10.)
61 anArcStartPoint = geomDataAPI_Point2D(aSketchArc.attribute("ArcStartPoint"))
62 anArcStartPoint.setValue(0., 50.)
63 anArcEndPoint = geomDataAPI_Point2D(aSketchArc.attribute("ArcEndPoint"))
64 anArcEndPoint.setValue(50., 0.)
65 aSession.finishOperation()
66 # Test changing the arc start/end point
67 aSession.startOperation()
68 anArcStartPoint.setValue(anArcStartPoint.x(), 40.)
69 anArcStartPoint.setValue(0., 50.)
70 assert (math.hypot(anArcStartPoint.x() - 0., anArcStartPoint.y() - 50.) < 1.e-10)
71 aSession.finishOperation()
72 aSession.startOperation()
73 anArcEndPoint.setValue(40., anArcEndPoint.y())
74 anArcEndPoint.setValue(50., 0.)
75 assert (math.hypot(anArcEndPoint.x() - 50., anArcEndPoint.y() - 0.) < 1.e-10)
76 aSession.finishOperation()
77 # Circle
78 aSession.startOperation()
79 aSketchCircle = aSketchFeature.addFeature("SketchCircle")
80 anCircleCentr = geomDataAPI_Point2D(aSketchCircle.attribute("CircleCenter"))
81 aCircleRadius = aSketchCircle.real("CircleRadius")
82 anCircleCentr.setValue(-25., -25)
83 aCircleRadius.setValue(25.)
84 aSession.finishOperation()
85 # Change the radius of the arc
86 aSession.startOperation()
87 RADIUS = 40
88 anArcRadius = aSketchArc.real("ArcRadius")
89 anArcRadius.setValue(RADIUS)
90 aSession.finishOperation()
91 #=========================================================================
92 # Make a constraint to keep the radius of the arc
93 #=========================================================================
94 aSession.startOperation()
95 aConstraint = aSketchFeature.addFeature("SketchConstraintRadius")
96 aRadius = aConstraint.real("ConstraintValue")
97 aRefObject = aConstraint.refattr("ConstraintEntityA")
98 aResult = aSketchArc.lastResult()
99 assert (aResult is not None)
100 aRefObject.setObject(modelAPI_ResultConstruction(aResult))
101 aConstraint.execute()
102 aSession.finishOperation()
103 assert (aRadius.isInitialized())
104 assert (aRefObject.isInitialized())
105 #=========================================================================
106 # Make a constraint to keep the radius of the circle
107 #=========================================================================
108 aSession.startOperation()
109 aConstraint = aSketchFeature.addFeature("SketchConstraintRadius")
110 aRadius = aConstraint.real("ConstraintValue")
111 aRefObject = aConstraint.refattr("ConstraintEntityA")
112 aResult = aSketchCircle.lastResult()
113 assert (aResult is not None)
114 aRefObject.setObject(modelAPI_ResultConstruction(aResult))
115 aConstraint.execute()
116 aSession.finishOperation()
117 assert (aRadius.isInitialized())
118 assert (aRefObject.isInitialized())
119 #=========================================================================
120 # Perform some actions and checks:
121 # 1. Check that constraints does not changes values
122 # 2. Move one point of the arc
123 # 3. Check that second point is moved also
124 #=========================================================================
125 assert (math.fabs(distancePointPoint(anArcCentr, anArcStartPoint) - RADIUS) < 1.e-10)
126 assert (math.fabs(distancePointPoint(anArcCentr, anArcEndPoint) - RADIUS) < 1.e-10)
127 anArcPrevEndPointX = anArcEndPoint.x()
128 anArcPrevEndPointY = anArcEndPoint.y()
129 # Move one point of the arc
130 aSession.startOperation()
131 anArcStartPoint.setValue(0, 60)
132 aSession.finishOperation()
133 assert (anArcEndPoint.x() != anArcPrevEndPointX)
134 assert (anArcEndPoint.y() != anArcPrevEndPointY)
135 assert (math.fabs(distancePointPoint(anArcCentr, anArcStartPoint) - RADIUS) < 1.e-10)
136 assert (math.fabs(distancePointPoint(anArcCentr, anArcEndPoint) - RADIUS) < 1.e-10)
137 #=========================================================================
138 # 4. Move the centr or the point of the arc
139 # 5. Check radius is the same
140 #=========================================================================
141 assert (anCircleCentr.x() == -25)
142 assert (anCircleCentr.y() == -25)
143 assert (aCircleRadius.value() == 25)
144 aSession.startOperation()
145 anCircleCentr.setValue(100., 100.)
146 aSession.finishOperation()
147 assert (anCircleCentr.x() == 100)
148 assert (anCircleCentr.y() == 100)
149 assert (aCircleRadius.value() == 25)
150 #=========================================================================
151 # End of test
152 #=========================================================================
153
154 import model
155 assert(model.checkPythonDump())