Salome HOME
87a388a65021fb5eaf48bd593810e67ceafbe5ab
[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 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
33 norm.setValue(0, 0, 1)
34 aSession.finishOperation()
35 #=========================================================================
36 # Create a line with length 100
37 #=========================================================================
38 aSession.startOperation()
39 aSketchLineA = aSketchFeature.addFeature("SketchLine")
40 aLineAStartPoint = geomDataAPI_Point2D(aSketchLineA.attribute("StartPoint"))
41 aLineAEndPoint = geomDataAPI_Point2D(aSketchLineA.attribute("EndPoint"))
42 aLineAStartPoint.setValue(0., 25.)
43 aLineAEndPoint.setValue(100., 25.)
44 aSession.finishOperation()
45 #=========================================================================
46 # Make a constraint to keep the length
47 #=========================================================================
48 aSession.startOperation()
49 aLengthConstraint = aSketchFeature.addFeature("SketchConstraintLength")
50 refattrA = aLengthConstraint.refattr("ConstraintEntityA")
51 aLength = aLengthConstraint.real("ConstraintValue")
52 assert (not refattrA.isInitialized())
53 assert (not aLength.isInitialized())
54
55 aResult = aSketchLineA.firstResult()
56 assert (aResult is not None)
57 refattrA.setObject(modelAPI_ResultConstruction(aResult))
58 # aLength.setValue(100.)
59 aLengthConstraint.execute()
60 aSession.finishOperation()
61 assert (aLength.isInitialized())
62 assert (refattrA.isInitialized())
63 #=========================================================================
64 # Check values and move one constrainted object
65 #=========================================================================
66 deltaX = 40.
67 # move start point, check that end point are moved also
68 assert (aLineAStartPoint.x() == 0)
69 assert (aLineAStartPoint.y() == 25)
70 assert (aLineAEndPoint.x() == 100)
71 assert (aLineAEndPoint.y() == 25)
72 aSession.startOperation()
73 aLineAStartPoint.setValue(aLineAStartPoint.x() + deltaX,
74                           aLineAStartPoint.y())
75 aSession.finishOperation()
76 assert (aLineAStartPoint.y() == 25)
77 assert (aLineAEndPoint.y() == 25)
78 # length of the line is the same
79 assert (aLineAEndPoint.x() - aLineAStartPoint.x() == 100)
80 #=========================================================================
81 # Change the length value of the constraint
82 #=========================================================================
83 aSession.startOperation()
84 aLength.setValue(140.)
85 aLengthConstraint.execute()
86 aSession.finishOperation()
87 assert (aLineAEndPoint.x() - aLineAStartPoint.x() == 140)
88 #=========================================================================
89 # TODO: improve test
90 # 1. remove constraint, move line's start point to
91 #    check that constraint are not applied
92 #=========================================================================
93 #=========================================================================
94 # End of test
95 #=========================================================================