Salome HOME
Merge branch 'master' into Dev_1.1.0
[modules/shaper.git] / src / SketchPlugin / Test / TestConstraintLength.py
1 """
2     TestConstraintLength.py
3     Unit test of SketchPlugin_ConstraintLength class
4         
5     SketchPlugin_ConstraintLength
6         static const std::string MY_CONSTRAINT_LENGTH_ID("SketchConstraintLength");
7         data()->addAttribute(SketchPlugin_Constraint::VALUE(),    ModelAPI_AttributeDouble::typeId());
8         data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
9         data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
10
11 """
12 from GeomDataAPI import *
13 from ModelAPI import *
14 #=========================================================================
15 # Initialization of the test
16 #=========================================================================
17
18 __updated__ = "2014-10-28"
19
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 = featureToCompositeFeature(aSketchCommonFeature)
28 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
29 origin.setValue(0, 0, 0)
30 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
31 dirx.setValue(1, 0, 0)
32 diry = geomDataAPI_Dir(aSketchFeature.attribute("DirY"))
33 diry.setValue(0, 1, 0)
34 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
35 norm.setValue(0, 0, 1)
36 aSession.finishOperation()
37 #=========================================================================
38 # Create a line with length 100
39 #=========================================================================
40 aSession.startOperation()
41 aSketchLineA = aSketchFeature.addFeature("SketchLine")
42 aLineAStartPoint = geomDataAPI_Point2D(aSketchLineA.attribute("StartPoint"))
43 aLineAEndPoint = geomDataAPI_Point2D(aSketchLineA.attribute("EndPoint"))
44 aLineAStartPoint.setValue(0., 25.)
45 aLineAEndPoint.setValue(100., 25.)
46 aSession.finishOperation()
47 #=========================================================================
48 # Make a constraint to keep the length
49 #=========================================================================
50 aSession.startOperation()
51 aLengthConstraint = aSketchFeature.addFeature("SketchConstraintLength")
52 refattrA = aLengthConstraint.refattr("ConstraintEntityA")
53 aLength = aLengthConstraint.real("ConstraintValue")
54 assert (not refattrA.isInitialized())
55 assert (not aLength.isInitialized())
56
57 aResult = aSketchLineA.firstResult()
58 assert (aResult is not None)
59 refattrA.setObject(modelAPI_ResultConstruction(aResult))
60 # aLength.setValue(100.)
61 aLengthConstraint.execute()
62 aSession.finishOperation()
63 assert (aLength.isInitialized())
64 assert (refattrA.isInitialized())
65 #=========================================================================
66 # Check values and move one constrainted object
67 #=========================================================================
68 deltaX = 40.
69 # move start point, check that end point are moved also
70 assert (aLineAStartPoint.x() == 0)
71 assert (aLineAStartPoint.y() == 25)
72 assert (aLineAEndPoint.x() == 100)
73 assert (aLineAEndPoint.y() == 25)
74 aSession.startOperation()
75 aLineAStartPoint.setValue(aLineAStartPoint.x() + deltaX,
76                           aLineAStartPoint.y())
77 aSession.finishOperation()
78 assert (aLineAStartPoint.y() == 25)
79 assert (aLineAEndPoint.y() == 25)
80 # length of the line is the same
81 assert (aLineAEndPoint.x() - aLineAStartPoint.x() == 100)
82 #=========================================================================
83 # Change the length value of the constraint
84 #=========================================================================
85 aSession.startOperation()
86 aLength.setValue(140.)
87 aLengthConstraint.execute()
88 aSession.finishOperation()
89 assert (aLineAEndPoint.x() - aLineAStartPoint.x() == 140)
90 #=========================================================================
91 # TODO: improve test
92 # 1. remove constraint, move line's start point to
93 #    check that constraint are not applied
94 #=========================================================================
95 #=========================================================================
96 # End of test
97 #=========================================================================