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-10-28"
20 aSession = ModelAPI_Session.get()
21 aDocument = aSession.moduleDocument()
22 #=========================================================================
23 # Creation of a sketch
24 #=========================================================================
25 aSession.startOperation()
26 aSketchCommonFeature = aDocument.addFeature("Sketch")
27 aSketchFeature = modelAPI_CompositeFeature(aSketchCommonFeature)
28 aSketchFeatureData = aSketchFeature.data()
29 origin = geomDataAPI_Point(aSketchFeatureData.attribute("Origin"))
30 origin.setValue(0, 0, 0)
31 dirx = geomDataAPI_Dir(aSketchFeatureData.attribute("DirX"))
32 dirx.setValue(1, 0, 0)
33 diry = geomDataAPI_Dir(aSketchFeatureData.attribute("DirY"))
34 diry.setValue(0, 1, 0)
35 norm = geomDataAPI_Dir(aSketchFeatureData.attribute("Norm"))
36 norm.setValue(0, 0, 1)
37 aSession.finishOperation()
38 #=========================================================================
39 # Create a line with length 100
40 #=========================================================================
41 aSession.startOperation()
42 aSketchLineA = aSketchFeature.addFeature("SketchLine")
43 aLineAStartPoint = geomDataAPI_Point2D(
44 aSketchLineA.data().attribute("StartPoint"))
45 aLineAEndPoint = geomDataAPI_Point2D(aSketchLineA.data().attribute("EndPoint"))
46 aLineAStartPoint.setValue(0., 25.)
47 aLineAEndPoint.setValue(100., 25.)
48 aSession.finishOperation()
49 #=========================================================================
50 # Make a constraint to keep the length
51 #=========================================================================
52 aSession.startOperation()
53 aLengthConstraint = aSketchFeature.addFeature("SketchConstraintLength")
54 aConstraintData = aLengthConstraint.data()
55 refattrA = aConstraintData.refattr("ConstraintEntityA")
56 aLength = aConstraintData.real("ConstraintValue")
57 assert (not refattrA.isInitialized())
58 assert (not aLength.isInitialized())
60 aResult = aSketchLineA.firstResult()
61 assert (aResult is not None)
62 refattrA.setObject(modelAPI_ResultConstruction(aResult))
63 # aLength.setValue(100.)
64 aLengthConstraint.execute()
65 aSession.finishOperation()
66 assert (aLength.isInitialized())
67 assert (refattrA.isInitialized())
68 #=========================================================================
69 # Check values and move one constrainted object
70 #=========================================================================
72 # move start point, check that end point are moved also
73 assert (aLineAStartPoint.x() == 0)
74 assert (aLineAStartPoint.y() == 25)
75 assert (aLineAEndPoint.x() == 100)
76 assert (aLineAEndPoint.y() == 25)
77 aSession.startOperation()
78 aLineAStartPoint.setValue(aLineAStartPoint.x() + deltaX,
80 aSession.finishOperation()
81 assert (aLineAStartPoint.y() == 25)
82 assert (aLineAEndPoint.y() == 25)
83 # length of the line is the same
84 assert (aLineAEndPoint.x() - aLineAStartPoint.x() == 100)
85 #=========================================================================
86 # Change the length value of the constraint
87 #=========================================================================
88 aSession.startOperation()
89 aLength.setValue(140.)
90 aLengthConstraint.execute()
91 aSession.finishOperation()
92 assert (aLineAEndPoint.x() - aLineAStartPoint.x() == 140)
93 #=========================================================================
95 # 1. remove constraint, move line's start point to
96 # check that constraint are not applied
97 #=========================================================================
98 #=========================================================================
100 #=========================================================================