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