Salome HOME
f1fa087b12ca7f89003731ff9660944572a664bb
[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 refattrA.setAttr(aSketchPointCoords)
104 refattrB.setAttr(aLineAStartPoint)
105 aSession.finishOperation()
106 assert (refattrA.isInitialized())
107 assert (refattrB.isInitialized())
108 assert (aDistance.isInitialized())
109 assert math.fabs(aDistance.value() - aDist) < 1.e-4, "Distance values are different: {0} != {1}".format(aDistance.value(), aDist)
110 #=========================================================================
111 # Change distance value
112 #=========================================================================
113 aSession.startOperation()
114 aDistance.setValue(PT_PT_DIST)
115 aSession.finishOperation()
116 assert (distancePointPoint(aSketchPointCoords, aLineAStartPoint) == PT_PT_DIST)
117 #=========================================================================
118 # Move line, check that distance is constant
119 #=========================================================================
120 aSession.startOperation()
121 aLineAStartPoint.setValue(0., 40.)
122 aLineAEndPoint.setValue(100., 40.)
123 aSession.finishOperation()
124 assert (distancePointPoint(aSketchPointCoords, aLineAStartPoint) == PT_PT_DIST)
125 #=========================================================================
126 # Remove constraint, check the points are unconstrained now
127 #=========================================================================
128 aSession.startOperation()
129 aDocument.removeFeature(aConstraint)
130 aSession.finishOperation()
131 aSession.startOperation()
132 aSketchPointCoords.setValue(0., 0.)
133 aSession.finishOperation()
134 assert (distancePointPoint(aSketchPointCoords, aLineAStartPoint) != PT_PT_DIST)
135
136 #=========================================================================
137 # Add distance between point and line
138 #=========================================================================
139 PT_LINE_DIST = 50.
140 aDist = distancePointLine(aSketchPointCoords, aSketchLine)
141 aSession.startOperation()
142 aConstraint = aSketchFeature.addFeature("SketchConstraintDistance")
143 aDistance = aConstraint.real("ConstraintValue")
144 refattrA = aConstraint.refattr("ConstraintEntityA")
145 refattrB = aConstraint.refattr("ConstraintEntityB")
146 assert (not aDistance.isInitialized())
147 assert (not refattrA.isInitialized())
148 assert (not refattrB.isInitialized())
149 aLineResult = aSketchLine.firstResult()
150 assert (aLineResult is not None)
151 refattrA.setObject(aLineResult)
152 refattrB.setAttr(aSketchPointCoords)
153 aSession.finishOperation()
154 assert (refattrA.isInitialized())
155 assert (refattrB.isInitialized())
156 assert (aDistance.isInitialized())
157 assert math.fabs(aDistance.value() - aDist) < 1.e-4, "Distance values are different: {0} != {1}".format(aDistance.value(), aDist)
158 #=========================================================================
159 # Change distance value
160 #=========================================================================
161 aSession.startOperation()
162 aDistance.setValue(PT_LINE_DIST)
163 aSession.finishOperation()
164 assert (distancePointLine(aSketchPointCoords, aSketchLine) == PT_LINE_DIST)
165 #=========================================================================
166 # End of test
167 #=========================================================================