Salome HOME
b3bacf265df1ad06a8da2a779f79d8708aa3b861
[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 aSession = ModelAPI_Session.get()
30 aDocument = aSession.moduleDocument()
31 #=========================================================================
32 # Creation of a sketch
33 #=========================================================================
34 aSession.startOperation()
35 aSketchCommonFeature = aDocument.addFeature("Sketch")
36 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
37 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
38 origin.setValue(0, 0, 0)
39 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
40 dirx.setValue(1, 0, 0)
41 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
42 norm.setValue(0, 0, 1)
43 aSession.finishOperation()
44 #=========================================================================
45 # Creation of an arc and a circle
46 #=========================================================================
47 aSession.startOperation()
48 aSketchArc = aSketchFeature.addFeature("SketchArc")
49 anArcCentr = geomDataAPI_Point2D(aSketchArc.attribute("ArcCenter"))
50 anArcCentr.setValue(10., 10.)
51 anArcStartPoint = geomDataAPI_Point2D(aSketchArc.attribute("ArcStartPoint"))
52 anArcStartPoint.setValue(0., 50.)
53 anArcEndPoint = geomDataAPI_Point2D(aSketchArc.attribute("ArcEndPoint"))
54 anArcEndPoint.setValue(50., 0.)
55 aSession.finishOperation()
56 # Test changing the arc start/end point
57 aSession.startOperation()
58 anArcStartPoint.setValue(anArcStartPoint.x(), 40.)
59 anArcStartPoint.setValue(0., 50.)
60 assert (math.hypot(anArcStartPoint.x() - 0., anArcStartPoint.y() - 50.) < 1.e-10)
61 aSession.finishOperation()
62 aSession.startOperation()
63 anArcEndPoint.setValue(40., anArcEndPoint.y())
64 anArcEndPoint.setValue(50., 0.)
65 assert (math.hypot(anArcEndPoint.x() - 50., anArcEndPoint.y() - 0.) < 1.e-10)
66 aSession.finishOperation()
67 # Circle
68 aSession.startOperation()
69 aSketchCircle = aSketchFeature.addFeature("SketchCircle")
70 anCircleCentr = geomDataAPI_Point2D(aSketchCircle.attribute("CircleCenter"))
71 aCircleRadius = aSketchCircle.real("CircleRadius")
72 anCircleCentr.setValue(-25., -25)
73 aCircleRadius.setValue(25.)
74 aSession.finishOperation()
75 #=========================================================================
76 # Make a constraint to keep the radius of the arc
77 #=========================================================================
78 aSession.startOperation()
79 aConstraint = aSketchFeature.addFeature("SketchConstraintRadius")
80 aRadius = aConstraint.real("ConstraintValue")
81 aRefObject = aConstraint.refattr("ConstraintEntityA")
82 aResult = aSketchArc.firstResult()
83 assert (aResult is not None)
84 aRefObject.setObject(modelAPI_ResultConstruction(aResult))
85 aConstraint.execute()
86 aSession.finishOperation()
87 assert (aRadius.isInitialized())
88 assert (aRefObject.isInitialized())
89 #=========================================================================
90 # Make a constraint to keep the radius of the circle
91 #=========================================================================
92 aSession.startOperation()
93 aConstraint = aSketchFeature.addFeature("SketchConstraintRadius")
94 aRadius = aConstraint.real("ConstraintValue")
95 aRefObject = aConstraint.refattr("ConstraintEntityA")
96 aResult = aSketchCircle.firstResult()
97 assert (aResult is not None)
98 aRefObject.setObject(modelAPI_ResultConstruction(aResult))
99 aConstraint.execute()
100 aSession.finishOperation()
101 assert (aRadius.isInitialized())
102 assert (aRefObject.isInitialized())
103 #=========================================================================
104 # Perform some actions and checks:
105 # 1. Check that constraints does not changes values
106 # 2. Move one point of the arc
107 # 3. Check that second point is moved also
108 #=========================================================================
109 assert (anArcCentr.x() == 10.)
110 assert (anArcCentr.y() == 10.)
111 assert (anArcStartPoint.x() == 0.)
112 assert (anArcStartPoint.y() == 50.)
113 anArcPrevEndPointX = anArcEndPoint.x()
114 anArcPrevEndPointY = anArcEndPoint.y()
115 assert (anArcPrevEndPointX == 50.)
116 assert (anArcPrevEndPointY == 0.)
117 # Move one point of the arc
118 aSession.startOperation()
119 anArcStartPoint.setValue(0, 60)
120 aSession.finishOperation()
121 assert (anArcCentr.x() == 10.)
122 assert (anArcCentr.y() == 10.)
123 # MPV: it just projects back to the circle the moved start point
124 #assert (anArcEndPoint.x() != anArcPrevEndPointX)
125 #assert (anArcEndPoint.y() != anArcPrevEndPointY)
126 #=========================================================================
127 # 4. Move the centr or the point of the arc
128 # 5. Check radius is the same
129 #=========================================================================
130 assert (anCircleCentr.x() == -25)
131 assert (anCircleCentr.y() == -25)
132 assert (aCircleRadius.value() == 25)
133 aSession.startOperation()
134 anCircleCentr.setValue(100., 100.)
135 aSession.finishOperation()
136 assert (anCircleCentr.x() == 100)
137 assert (anCircleCentr.y() == 100)
138 assert (aCircleRadius.value() == 25)
139 #=========================================================================
140 # End of test
141 #=========================================================================