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