Salome HOME
Issue #2027 Sketcher Trim Feature: coincidence unit test case
[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 from salome.shaper import model
16
17 #=========================================================================
18 # Initialization of the test
19 #=========================================================================
20
21 __updated__ = "2014-10-28"
22
23 aSession = ModelAPI_Session.get()
24 aDocument = aSession.moduleDocument()
25 #=========================================================================
26 # Creation of a sketch
27 #=========================================================================
28 aSession.startOperation()
29 aSketchCommonFeature = aDocument.addFeature("Sketch")
30 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
31 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
32 origin.setValue(0, 0, 0)
33 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
34 dirx.setValue(1, 0, 0)
35 norm = geomDataAPI_Dir(aSketchFeature.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(aSketchLineA.attribute("StartPoint"))
44 aLineAEndPoint = geomDataAPI_Point2D(aSketchLineA.attribute("EndPoint"))
45 aLineAStartPoint.setValue(0., 25.)
46 aLineAEndPoint.setValue(100., 25.)
47 aSession.finishOperation()
48 assert (model.dof(aSketchFeature) == 4)
49 #=========================================================================
50 # Make a constraint to keep the length
51 #=========================================================================
52 aSession.startOperation()
53 aLengthConstraint = aSketchFeature.addFeature("SketchConstraintLength")
54 refattrA = aLengthConstraint.refattr("ConstraintEntityA")
55 aLength = aLengthConstraint.real("ConstraintValue")
56 assert (not refattrA.isInitialized())
57 assert (not aLength.isInitialized())
58
59 aResult = aSketchLineA.firstResult()
60 assert (aResult is not None)
61 refattrA.setObject(modelAPI_ResultConstruction(aResult))
62 # aLength.setValue(100.)
63 aLengthConstraint.execute()
64 aSession.finishOperation()
65 assert (aLength.isInitialized())
66 assert (refattrA.isInitialized())
67 assert (model.dof(aSketchFeature) == 3)
68 #=========================================================================
69 # Check values and move one constrainted object
70 #=========================================================================
71 deltaX = 40.
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,
79                           aLineAStartPoint.y())
80 aSession.finishOperation()
81 assert (model.dof(aSketchFeature) == 3)
82 assert (aLineAStartPoint.y() == 25)
83 assert (aLineAEndPoint.y() == 25)
84 # length of the line is the same
85 assert (math.fabs(aLineAEndPoint.x() - aLineAStartPoint.x() - 100) < 1.e-10)
86 #=========================================================================
87 # Change the length value of the constraint
88 #=========================================================================
89 aSession.startOperation()
90 aLength.setValue(140.)
91 aLengthConstraint.execute()
92 aSession.finishOperation()
93 assert (math.fabs(aLineAEndPoint.x() - aLineAStartPoint.x() - 140) < 1.e-10)
94 assert (model.dof(aSketchFeature) == 3)
95 #=========================================================================
96 # TODO: improve test
97 # 1. remove constraint, move line's start point to
98 #    check that constraint are not applied
99 #=========================================================================
100 #=========================================================================
101 # End of test
102 #=========================================================================
103
104 assert(model.checkPythonDump())