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 distance(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 aSession = ModelAPI_Session.get()
42 aDocument = aSession.moduleDocument()
43 #=========================================================================
44 # Creation of a sketch
45 #=========================================================================
46 aSession.startOperation()
47 aSketchCommonFeature = aDocument.addFeature("Sketch")
48 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
49 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
50 origin.setValue(0, 0, 0)
51 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
52 dirx.setValue(1, 0, 0)
53 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
54 norm.setValue(0, 0, 1)
55 aSession.finishOperation()
56 #=========================================================================
57 # Create a point and a line
58 #=========================================================================
59 aSession.startOperation()
60 aSketchPoint = aSketchFeature.addFeature("SketchPoint")
61 aSketchPointCoords = geomDataAPI_Point2D(
62 aSketchPoint.attribute("PointCoordindates"))
63 aSketchPointCoords.setValue(50., 50.)
64 aSketchLine = aSketchFeature.addFeature("SketchLine")
65 aLineAStartPoint = geomDataAPI_Point2D(aSketchLine.attribute("StartPoint"))
66 aLineAEndPoint = geomDataAPI_Point2D(aSketchLine.attribute("EndPoint"))
67 aLineAStartPoint.setValue(0., 25.)
68 aLineAEndPoint.setValue(100., 25.)
69 aSession.finishOperation()
70 #=========================================================================
71 # Make a constraint to keep the distance
72 #=========================================================================
73 assert (distance(aSketchPointCoords, aLineAStartPoint) != 25.)
74 aSession.startOperation()
75 aConstraint = aSketchFeature.addFeature("SketchConstraintDistance")
76 aDistance = aConstraint.real("ConstraintValue")
77 refattrA = aConstraint.refattr("ConstraintEntityA")
78 refattrB = aConstraint.refattr("ConstraintEntityB")
79 assert (not aDistance.isInitialized())
80 assert (not refattrA.isInitialized())
81 assert (not refattrB.isInitialized())
82 aDistance.setValue(25.)
83 aLineResult = aSketchLine.firstResult()
84 assert (aLineResult is not None)
85 refattrA.setAttr(aSketchPointCoords)
86 refattrB.setAttr(aLineAStartPoint)
87 aSession.finishOperation()
88 assert (aDistance.isInitialized())
89 assert (refattrA.isInitialized())
90 assert (refattrB.isInitialized())
91 #=========================================================================
92 # Move line, check that distance is constant
93 #=========================================================================
94 assert (distance(aSketchPointCoords, aLineAStartPoint) == 25.)
95 aSession.startOperation()
96 aLineAStartPoint.setValue(0., 40.)
97 aLineAEndPoint.setValue(100., 40.)
98 aSession.finishOperation()
99 assert (distance(aSketchPointCoords, aLineAStartPoint) == 25.)
100 #=========================================================================
102 # 1. remove constraint, move line's start point to
103 # check that constraint are not applied
104 # 2. check constrained distance between:
107 #=========================================================================
108 #=========================================================================
110 #=========================================================================