Salome HOME
Implement sending list of conflicting constraints by the Solver (issues #796, #945)
[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 #=========================================================================
26 # Initialization of the test
27 #=========================================================================
28
29 __updated__ = "2014-10-28"
30
31
32 def distancePointPoint(pointA, pointB):
33     """
34     subroutine to calculate distance between two points
35     result of calculated distance is has 10**-5 precision
36     """
37     xdiff = math.pow((pointA.x() - pointB.x()), 2)
38     ydiff = math.pow((pointA.y() - pointB.y()), 2)
39     return round(math.sqrt(xdiff + ydiff), 5)
40
41 def distancePointLine(point, line):
42     """
43     subroutine to calculate distance between point and line
44     result of calculated distance is has 10**-5 precision
45     """
46     aStartPoint = geomDataAPI_Point2D(line.attribute("StartPoint"))
47     aEndPoint = geomDataAPI_Point2D(line.attribute("EndPoint"))
48     # orthogonal direction
49     aDirX = -(aEndPoint.y() - aStartPoint.y())
50     aDirY = (aEndPoint.x() - aStartPoint.x())
51     aLen = math.sqrt(aDirX**2 + aDirY**2)
52     aDirX = aDirX / aLen
53     aDirY = aDirY / aLen
54     aVecX = point.x() - aStartPoint.x()
55     aVecY = point.y() - aStartPoint.y()
56     return round(math.fabs(aVecX * aDirX + aVecY * aDirY), 5)
57
58 aSession = ModelAPI_Session.get()
59 aDocument = aSession.moduleDocument()
60 #=========================================================================
61 # Creation of a sketch
62 #=========================================================================
63 aSession.startOperation()
64 aSketchCommonFeature = aDocument.addFeature("Sketch")
65 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
66 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
67 origin.setValue(0, 0, 0)
68 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
69 dirx.setValue(1, 0, 0)
70 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
71 norm.setValue(0, 0, 1)
72 aSession.finishOperation()
73 #=========================================================================
74 # Create a point and a line
75 #=========================================================================
76 aSession.startOperation()
77 aSketchPoint = aSketchFeature.addFeature("SketchPoint")
78 aSketchPointCoords = geomDataAPI_Point2D(
79     aSketchPoint.attribute("PointCoordindates"))
80 aSketchPointCoords.setValue(50., 50.)
81 aSketchLine = aSketchFeature.addFeature("SketchLine")
82 aLineAStartPoint = geomDataAPI_Point2D(aSketchLine.attribute("StartPoint"))
83 aLineAEndPoint = geomDataAPI_Point2D(aSketchLine.attribute("EndPoint"))
84 aLineAStartPoint.setValue(0., 25.)
85 aLineAEndPoint.setValue(100., 25.)
86 aSession.finishOperation()
87 #=========================================================================
88 # Make a constraint to keep the distance
89 #=========================================================================
90 PT_PT_DIST = 25.
91 aDist = distancePointPoint(aSketchPointCoords, aLineAStartPoint);
92 assert (aDist != PT_PT_DIST)
93 aSession.startOperation()
94 aConstraint = aSketchFeature.addFeature("SketchConstraintDistance")
95 aDistance = aConstraint.real("ConstraintValue")
96 refattrA = aConstraint.refattr("ConstraintEntityA")
97 refattrB = aConstraint.refattr("ConstraintEntityB")
98 assert (not aDistance.isInitialized())
99 assert (not refattrA.isInitialized())
100 assert (not refattrB.isInitialized())
101 aLineResult = aSketchLine.firstResult()
102 assert (aLineResult is not None)
103 # the following line is necessary to check automatic calculation of a distance
104 aConstraint.execute()
105 refattrA.setAttr(aSketchPointCoords)
106 refattrB.setAttr(aLineAStartPoint)
107 aSession.finishOperation()
108 assert (refattrA.isInitialized())
109 assert (refattrB.isInitialized())
110 assert (aDistance.isInitialized())
111 assert math.fabs(aDistance.value() - aDist) < 1.e-4, "Distance values are different: {0} != {1}".format(aDistance.value(), aDist)
112 #=========================================================================
113 # Change distance value
114 #=========================================================================
115 aSession.startOperation()
116 aDistance.setValue(PT_PT_DIST)
117 aSession.finishOperation()
118 assert (math.fabs(distancePointPoint(aSketchPointCoords, aLineAStartPoint) - PT_PT_DIST) < 1.e-10)
119 #=========================================================================
120 # Move line, check that distance is constant
121 #=========================================================================
122 aSession.startOperation()
123 aLineAStartPoint.setValue(0., 40.)
124 aLineAEndPoint.setValue(100., 40.)
125 aSession.finishOperation()
126 assert (math.fabs(distancePointPoint(aSketchPointCoords, aLineAStartPoint) - PT_PT_DIST) < 1.e-10)
127 #=========================================================================
128 # Remove constraint, check the points are unconstrained now
129 #=========================================================================
130 aSession.startOperation()
131 aDocument.removeFeature(aConstraint)
132 aSession.finishOperation()
133 aSession.startOperation()
134 aSketchPointCoords.setValue(0., 0.)
135 aSession.finishOperation()
136 assert (math.fabs(distancePointPoint(aSketchPointCoords, aLineAStartPoint) - PT_PT_DIST) > 1.e-10)
137
138 #=========================================================================
139 # Add distance between point and line
140 #=========================================================================
141 PT_LINE_DIST = 50.
142 aDist = distancePointLine(aSketchPointCoords, aSketchLine)
143 aSession.startOperation()
144 aConstraint = aSketchFeature.addFeature("SketchConstraintDistance")
145 aDistance = aConstraint.real("ConstraintValue")
146 refattrA = aConstraint.refattr("ConstraintEntityA")
147 refattrB = aConstraint.refattr("ConstraintEntityB")
148 assert (not aDistance.isInitialized())
149 assert (not refattrA.isInitialized())
150 assert (not refattrB.isInitialized())
151 aLineResult = aSketchLine.firstResult()
152 assert (aLineResult is not None)
153 refattrA.setObject(aLineResult)
154 refattrB.setAttr(aSketchPointCoords)
155 aSession.finishOperation()
156 assert (refattrA.isInitialized())
157 assert (refattrB.isInitialized())
158 assert (aDistance.isInitialized())
159 assert math.fabs(aDistance.value() - aDist) < 1.e-4, "Distance values are different: {0} != {1}".format(aDistance.value(), aDist)
160 #=========================================================================
161 # Change distance value
162 #=========================================================================
163 aSession.startOperation()
164 aDistance.setValue(PT_LINE_DIST)
165 aSession.finishOperation()
166 assert (math.fabs(distancePointLine(aSketchPointCoords, aSketchLine) - PT_LINE_DIST) < 1.e-10)
167 #=========================================================================
168 # Set distance between line boundaries
169 #=========================================================================
170 aSession.startOperation()
171 refattrA.setAttr(aLineAStartPoint)
172 refattrB.setAttr(aLineAEndPoint)
173 aSession.finishOperation()
174 assert (math.fabs(distancePointPoint(aLineAStartPoint, aLineAEndPoint) - PT_LINE_DIST) < 1.e-10)
175 #=========================================================================
176 # End of test
177 #=========================================================================