Salome HOME
Simplification and refactoring of unit tests for SketchPlugin
[modules/shaper.git] / src / SketchPlugin / Test / TestConstraintDistance.py
1 """
2     TestConstraintDistance.py
3     Unit test of SketchPlugin_ConstraintDistance 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_ConstraintDistance
14         static const std::string MY_CONSTRAINT_DISTANCE_ID("SketchConstraintDistance");
15         data()->addAttribute(SketchPlugin_Constraint::VALUE(),    ModelAPI_AttributeDouble::typeId());
16         data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
17         data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
18         data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
19         
20     
21 """
22 from GeomDataAPI import *
23 from ModelAPI import *
24 import math
25 from salome.shaper import model
26
27 #=========================================================================
28 # Initialization of the test
29 #=========================================================================
30
31 __updated__ = "2014-10-28"
32
33
34 aSession = ModelAPI_Session.get()
35 aDocument = aSession.moduleDocument()
36 #=========================================================================
37 # Creation of a sketch
38 #=========================================================================
39 aSession.startOperation()
40 aSketchCommonFeature = aDocument.addFeature("Sketch")
41 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
42 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
43 origin.setValue(0, 0, 0)
44 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
45 dirx.setValue(1, 0, 0)
46 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
47 norm.setValue(0, 0, 1)
48 aSession.finishOperation()
49 #=========================================================================
50 # Create a point and a line
51 #=========================================================================
52 aSession.startOperation()
53 aSketchPoint = aSketchFeature.addFeature("SketchPoint")
54 aSketchPointCoords = geomDataAPI_Point2D(
55     aSketchPoint.attribute("PointCoordinates"))
56 aSketchPointCoords.setValue(50., 50.)
57 aSketchLine = aSketchFeature.addFeature("SketchLine")
58 aLineAStartPoint = geomDataAPI_Point2D(aSketchLine.attribute("StartPoint"))
59 aLineAEndPoint = geomDataAPI_Point2D(aSketchLine.attribute("EndPoint"))
60 aLineAStartPoint.setValue(0., 25.)
61 aLineAEndPoint.setValue(100., 25.)
62 aSession.finishOperation()
63 assert (model.dof(aSketchFeature) == 6)
64 #=========================================================================
65 # Make a constraint to keep the distance
66 #=========================================================================
67 PT_PT_DIST = 25.
68 aDist = model.distancePointPoint(aSketchPointCoords, aLineAStartPoint);
69 assert (aDist != PT_PT_DIST)
70 aSession.startOperation()
71 aConstraint = aSketchFeature.addFeature("SketchConstraintDistance")
72 aDistance = aConstraint.real("ConstraintValue")
73 refattrA = aConstraint.refattr("ConstraintEntityA")
74 refattrB = aConstraint.refattr("ConstraintEntityB")
75 assert (not aDistance.isInitialized())
76 assert (not refattrA.isInitialized())
77 assert (not refattrB.isInitialized())
78 aLineResult = aSketchLine.firstResult()
79 assert (aLineResult is not None)
80 # the following line is necessary to check automatic calculation of a distance
81 aConstraint.execute()
82 refattrA.setAttr(aSketchPointCoords)
83 refattrB.setAttr(aLineAStartPoint)
84 aSession.finishOperation()
85 assert (model.dof(aSketchFeature) == 5)
86 # set flyout point then abort operation, after that check the Distance is correct
87 aSession.startOperation()
88 aFlyoutPoint = geomDataAPI_Point2D(aConstraint.attribute("ConstraintFlyoutValuePnt"))
89 aFlyoutPoint.setValue(50.0, 100.0)
90 aSession.abortOperation()
91 assert (refattrA.isInitialized())
92 assert (refattrB.isInitialized())
93 assert (aDistance.isInitialized())
94 assert math.fabs(aDistance.value() - aDist) < 1.e-4, "Distance values are different: {0} != {1}".format(aDistance.value(), aDist)
95 assert (model.dof(aSketchFeature) == 5)
96 #=========================================================================
97 # Change distance value
98 #=========================================================================
99 aSession.startOperation()
100 aDistance.setValue(PT_PT_DIST)
101 aSession.finishOperation()
102 assert (math.fabs(model.distancePointPoint(aSketchPointCoords, aLineAStartPoint) - PT_PT_DIST) < 1.e-10)
103 assert (model.dof(aSketchFeature) == 5)
104 #=========================================================================
105 # Move line, check that distance is constant
106 #=========================================================================
107 aSession.startOperation()
108 aLineAStartPoint.setValue(0., 40.)
109 aLineAEndPoint.setValue(100., 40.)
110 aSession.finishOperation()
111 assert (math.fabs(model.distancePointPoint(aSketchPointCoords, aLineAStartPoint) - PT_PT_DIST) < 1.e-10)
112 assert (model.dof(aSketchFeature) == 5)
113 #=========================================================================
114 # Remove constraint, check the points are unconstrained now
115 #=========================================================================
116 aSession.startOperation()
117 aDocument.removeFeature(aConstraint)
118 aSession.finishOperation()
119 aSession.startOperation()
120 aSketchPointCoords.setValue(0., 0.)
121 aSession.finishOperation()
122 assert (math.fabs(model.distancePointPoint(aSketchPointCoords, aLineAStartPoint) - PT_PT_DIST) > 1.e-10)
123 assert (model.dof(aSketchFeature) == 6)
124
125 #=========================================================================
126 # Add distance between point and line
127 #=========================================================================
128 PT_LINE_DIST = 50.
129 aDist = model.distancePointLine(aSketchPointCoords, aSketchLine)
130 aSession.startOperation()
131 aConstraint = aSketchFeature.addFeature("SketchConstraintDistance")
132 aDistance = aConstraint.real("ConstraintValue")
133 refattrA = aConstraint.refattr("ConstraintEntityA")
134 refattrB = aConstraint.refattr("ConstraintEntityB")
135 assert (not aDistance.isInitialized())
136 assert (not refattrA.isInitialized())
137 assert (not refattrB.isInitialized())
138 aLineResult = aSketchLine.firstResult()
139 assert (aLineResult is not None)
140 refattrA.setObject(aLineResult)
141 refattrB.setAttr(aSketchPointCoords)
142 aSession.finishOperation()
143 assert (model.dof(aSketchFeature) == 5)
144 # set flyout point then abort operation, after that check the Distance is correct
145 aSession.startOperation()
146 aFlyoutPoint = geomDataAPI_Point2D(aConstraint.attribute("ConstraintFlyoutValuePnt"))
147 aFlyoutPoint.setValue(50.0, 100.0)
148 aSession.abortOperation()
149 assert (refattrA.isInitialized())
150 assert (refattrB.isInitialized())
151 assert (aDistance.isInitialized())
152 assert math.fabs(aDistance.value() - aDist) < 1.e-4, "Distance values are different: {0} != {1}".format(aDistance.value(), aDist)
153 assert (model.dof(aSketchFeature) == 5)
154 #=========================================================================
155 # Change distance value
156 #=========================================================================
157 aSession.startOperation()
158 aDistance.setValue(PT_LINE_DIST)
159 aSession.finishOperation()
160 assert (math.fabs(model.distancePointLine(aSketchPointCoords, aSketchLine) - PT_LINE_DIST) < 1.e-10)
161 assert (model.dof(aSketchFeature) == 5)
162 #=========================================================================
163 # Set distance between line boundaries
164 #=========================================================================
165 aSession.startOperation()
166 refattrA.setAttr(aLineAStartPoint)
167 refattrB.setAttr(aLineAEndPoint)
168 aSession.finishOperation()
169 assert (math.fabs(model.distancePointPoint(aLineAStartPoint, aLineAEndPoint) - PT_LINE_DIST) < 1.e-10)
170 assert (model.dof(aSketchFeature) == 5)
171 #=========================================================================
172 # End of test
173 #=========================================================================
174
175 assert(model.checkPythonDump())