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