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