2 TestConstraintLength.py
3 Unit test of SketchPlugin_ConstraintLength class
5 SketchPlugin_ConstraintLength
6 static const std::string MY_CONSTRAINT_LENGTH_ID("SketchConstraintLength");
7 data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::type());
8 data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::type());
9 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::type());
12 from GeomDataAPI import *
13 from ModelAPI import *
14 #=========================================================================
15 # Initialization of the test
16 #=========================================================================
18 __updated__ = "2014-09-26"
20 aSession = ModelAPI_Session.get()
21 aDocument = aSession.moduleDocument()
22 #=========================================================================
23 # Creation of a sketch
24 #=========================================================================
25 aSession.startOperation()
26 aSketchFeature = aDocument.addFeature("Sketch")
27 aSketchFeatureData = aSketchFeature.data()
28 origin = geomDataAPI_Point(aSketchFeatureData.attribute("Origin"))
29 origin.setValue(0, 0, 0)
30 dirx = geomDataAPI_Dir(aSketchFeatureData.attribute("DirX"))
31 dirx.setValue(1, 0, 0)
32 diry = geomDataAPI_Dir(aSketchFeatureData.attribute("DirY"))
33 diry.setValue(0, 1, 0)
34 norm = geomDataAPI_Dir(aSketchFeatureData.attribute("Norm"))
35 norm.setValue(0, 0, 1)
36 aSession.finishOperation()
37 #=========================================================================
38 # Create a line with length 100
39 #=========================================================================
40 aSession.startOperation()
41 aSketchReflist = aSketchFeatureData.reflist("Features")
42 aSketchLineA = aDocument.addFeature("SketchLine")
43 aSketchReflist.append(aSketchLineA)
44 aLineAStartPoint = geomDataAPI_Point2D(
45 aSketchLineA.data().attribute("StartPoint"))
46 aLineAEndPoint = geomDataAPI_Point2D(aSketchLineA.data().attribute("EndPoint"))
47 aLineAStartPoint.setValue(0., 25.)
48 aLineAEndPoint.setValue(100., 25.)
49 aSession.finishOperation()
50 #=========================================================================
51 # Make a constraint to keep the length
52 #=========================================================================
53 aSession.startOperation()
54 aLengthConstraint = aDocument.addFeature("SketchConstraintLength")
55 aSketchReflist.append(aLengthConstraint)
56 aConstraintData = aLengthConstraint.data()
57 refattrA = aConstraintData.refattr("ConstraintEntityA")
58 aLength = aConstraintData.real("ConstraintValue")
59 assert (not refattrA.isInitialized())
60 assert (not aLength.isInitialized())
62 aResult = aSketchLineA.firstResult()
63 assert (aResult is not None)
64 refattrA.setObject(modelAPI_ResultConstruction(aResult))
65 # aLength.setValue(100.)
66 aLengthConstraint.execute()
67 aSession.finishOperation()
68 assert (aLength.isInitialized())
69 assert (refattrA.isInitialized())
70 #=========================================================================
71 # Check values and move one constrainted object
72 #=========================================================================
74 # move start point, check that end point are moved also
75 assert (aLineAStartPoint.x() == 0)
76 assert (aLineAStartPoint.y() == 25)
77 assert (aLineAEndPoint.x() == 100)
78 assert (aLineAEndPoint.y() == 25)
79 aSession.startOperation()
80 aLineAStartPoint.setValue(aLineAStartPoint.x() + deltaX,
82 aSession.finishOperation()
83 assert (aLineAStartPoint.y() == 25)
84 assert (aLineAEndPoint.y() == 25)
85 # length of the line is the same
86 assert (aLineAEndPoint.x() - aLineAStartPoint.x() == 100)
87 #=========================================================================
88 # Change the length value of the constraint
89 #=========================================================================
90 aSession.startOperation()
91 aLength.setValue(140.)
92 aLengthConstraint.execute()
93 aSession.finishOperation()
94 assert (aLineAEndPoint.x() - aLineAStartPoint.x() == 140)
95 #=========================================================================
97 # 1. remove constraint, move line's start point to
98 # check that constraint are not applied
99 #=========================================================================
100 #=========================================================================
102 #=========================================================================