2 TestConstraintDistance.py
3 Unit test of SketchPlugin_ConstraintDistance class
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");
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());
22 from GeomDataAPI import *
23 from ModelAPI import *
25 #=========================================================================
26 # Initialization of the test
27 #=========================================================================
29 __updated__ = "2014-10-28"
32 def distancePointPoint(pointA, pointB):
34 subroutine to calculate distance between two points
35 result of calculated distance is has 10**-5 precision
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)
41 def distancePointLine(point, line):
43 subroutine to calculate distance between point and line
44 result of calculated distance is has 10**-5 precision
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)
54 aVecX = point.x() - aStartPoint.x()
55 aVecY = point.y() - aStartPoint.y()
56 return round(math.fabs(aVecX * aDirX + aVecY * aDirY), 5)
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 #=========================================================================
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 # set flyout point then abort operation, after that check the Distance is correct
109 aSession.startOperation()
110 aFlyoutPoint = geomDataAPI_Point2D(aConstraint.attribute("ConstraintFlyoutValuePnt"))
111 aFlyoutPoint.setValue(50.0, 100.0)
112 aSession.finishOperation()
113 aSession.abortOperation()
114 assert (refattrA.isInitialized())
115 assert (refattrB.isInitialized())
116 assert (aDistance.isInitialized())
117 assert math.fabs(aDistance.value() - aDist) < 1.e-4, "Distance values are different: {0} != {1}".format(aDistance.value(), aDist)
118 #=========================================================================
119 # Change distance value
120 #=========================================================================
121 aSession.startOperation()
122 aDistance.setValue(PT_PT_DIST)
123 aSession.finishOperation()
124 assert (math.fabs(distancePointPoint(aSketchPointCoords, aLineAStartPoint) - PT_PT_DIST) < 1.e-10)
125 #=========================================================================
126 # Move line, check that distance is constant
127 #=========================================================================
128 aSession.startOperation()
129 aLineAStartPoint.setValue(0., 40.)
130 aLineAEndPoint.setValue(100., 40.)
131 aSession.finishOperation()
132 assert (math.fabs(distancePointPoint(aSketchPointCoords, aLineAStartPoint) - PT_PT_DIST) < 1.e-10)
133 #=========================================================================
134 # Remove constraint, check the points are unconstrained now
135 #=========================================================================
136 aSession.startOperation()
137 aDocument.removeFeature(aConstraint)
138 aSession.finishOperation()
139 aSession.startOperation()
140 aSketchPointCoords.setValue(0., 0.)
141 aSession.finishOperation()
142 assert (math.fabs(distancePointPoint(aSketchPointCoords, aLineAStartPoint) - PT_PT_DIST) > 1.e-10)
144 #=========================================================================
145 # Add distance between point and line
146 #=========================================================================
148 aDist = distancePointLine(aSketchPointCoords, aSketchLine)
149 aSession.startOperation()
150 aConstraint = aSketchFeature.addFeature("SketchConstraintDistance")
151 aDistance = aConstraint.real("ConstraintValue")
152 refattrA = aConstraint.refattr("ConstraintEntityA")
153 refattrB = aConstraint.refattr("ConstraintEntityB")
154 assert (not aDistance.isInitialized())
155 assert (not refattrA.isInitialized())
156 assert (not refattrB.isInitialized())
157 aLineResult = aSketchLine.firstResult()
158 assert (aLineResult is not None)
159 refattrA.setObject(aLineResult)
160 refattrB.setAttr(aSketchPointCoords)
161 aSession.finishOperation()
162 # set flyout point then abort operation, after that check the Distance is correct
163 aSession.startOperation()
164 aFlyoutPoint = geomDataAPI_Point2D(aConstraint.attribute("ConstraintFlyoutValuePnt"))
165 aFlyoutPoint.setValue(50.0, 100.0)
166 aSession.finishOperation()
167 aSession.abortOperation()
168 assert (refattrA.isInitialized())
169 assert (refattrB.isInitialized())
170 assert (aDistance.isInitialized())
171 assert math.fabs(aDistance.value() - aDist) < 1.e-4, "Distance values are different: {0} != {1}".format(aDistance.value(), aDist)
172 #=========================================================================
173 # Change distance value
174 #=========================================================================
175 aSession.startOperation()
176 aDistance.setValue(PT_LINE_DIST)
177 aSession.finishOperation()
178 assert (math.fabs(distancePointLine(aSketchPointCoords, aSketchLine) - PT_LINE_DIST) < 1.e-10)
179 #=========================================================================
180 # Set distance between line boundaries
181 #=========================================================================
182 aSession.startOperation()
183 refattrA.setAttr(aLineAStartPoint)
184 refattrB.setAttr(aLineAEndPoint)
185 aSession.finishOperation()
186 assert (math.fabs(distancePointPoint(aLineAStartPoint, aLineAEndPoint) - PT_LINE_DIST) < 1.e-10)
187 #=========================================================================
189 #=========================================================================
192 assert(model.checkPythonDump())