Salome HOME
9349352b32b4f4512ec674be73e694b5b1252517
[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 # Circle
57 aSession.startOperation()
58 aSketchCircle = aSketchFeature.addFeature("SketchCircle")
59 anCircleCentr = geomDataAPI_Point2D(aSketchCircle.attribute("CircleCenter"))
60 aCircleRadius = aSketchCircle.real("CircleRadius")
61 anCircleCentr.setValue(-25., -25)
62 aCircleRadius.setValue(25.)
63 aSession.finishOperation()
64 #=========================================================================
65 # Make a constraint to keep the radius of the arc
66 #=========================================================================
67 aSession.startOperation()
68 aConstraint = aSketchFeature.addFeature("SketchConstraintRadius")
69 aRadius = aConstraint.real("ConstraintValue")
70 aRefObject = aConstraint.refattr("ConstraintEntityA")
71 aResult = aSketchArc.firstResult()
72 assert (aResult is not None)
73 aRefObject.setObject(modelAPI_ResultConstruction(aResult))
74 aConstraint.execute()
75 aSession.finishOperation()
76 assert (aRadius.isInitialized())
77 assert (aRefObject.isInitialized())
78 #=========================================================================
79 # Make a constraint to keep the radius of the circle
80 #=========================================================================
81 aSession.startOperation()
82 aConstraint = aSketchFeature.addFeature("SketchConstraintRadius")
83 aRadius = aConstraint.real("ConstraintValue")
84 aRefObject = aConstraint.refattr("ConstraintEntityA")
85 aResult = aSketchCircle.firstResult()
86 assert (aResult is not None)
87 aRefObject.setObject(modelAPI_ResultConstruction(aResult))
88 aConstraint.execute()
89 aSession.finishOperation()
90 assert (aRadius.isInitialized())
91 assert (aRefObject.isInitialized())
92 #=========================================================================
93 # Perform some actions and checks:
94 # 1. Check that constraints does not changes values
95 # 2. Move one point of the arc
96 # 3. Check that second point is moved also
97 #=========================================================================
98 assert (anArcCentr.x() == 10.)
99 assert (anArcCentr.y() == 10.)
100 assert (anArcStartPoint.x() == 0.)
101 assert (anArcStartPoint.y() == 50.)
102 anArcPrevEndPointX = anArcEndPoint.x()
103 anArcPrevEndPointY = anArcEndPoint.y()
104 assert (anArcPrevEndPointX == 50.)
105 assert (anArcPrevEndPointY == 0.)
106 # Move one point of the arc
107 aSession.startOperation()
108 anArcStartPoint.setValue(0, 60)
109 aSession.finishOperation()
110 assert (anArcCentr.x() == 10.)
111 assert (anArcCentr.y() == 10.)
112 # MPV: it just projects back to the circle the moved start point
113 #assert (anArcEndPoint.x() != anArcPrevEndPointX)
114 #assert (anArcEndPoint.y() != anArcPrevEndPointY)
115 #=========================================================================
116 # 4. Move the centr or the point of the arc
117 # 5. Check radius is the same
118 #=========================================================================
119 assert (anCircleCentr.x() == -25)
120 assert (anCircleCentr.y() == -25)
121 assert (aCircleRadius.value() == 25)
122 aSession.startOperation()
123 anCircleCentr.setValue(100., 100.)
124 aSession.finishOperation()
125 assert (anCircleCentr.x() == 100)
126 assert (anCircleCentr.y() == 100)
127 assert (aCircleRadius.value() == 25)
128 #=========================================================================
129 # End of test
130 #=========================================================================