Salome HOME
Merge branch 'master' into Dev_1.1.0
[modules/shaper.git] / src / SketchPlugin / Test / TestConstraintParallel.py
1 """
2     TestConstraintParallel.py
3     Unit test of SketchPlugin_ConstraintParallel class
4         
5     SketchPlugin_ConstraintParallel
6         static const std::string MY_CONSTRAINT_PARALLEL_ID("SketchConstraintParallel");
7         data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
8         data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
9         data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::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 two lines which are not parallel
39 #=========================================================================
40 aSession.startOperation()
41 # line A
42 aSketchLineA = aSketchFeature.addFeature("SketchLine")
43 aLineAStartPoint = geomDataAPI_Point2D(aSketchLineA.attribute("StartPoint"))
44 aLineAEndPoint = geomDataAPI_Point2D(aSketchLineA.attribute("EndPoint"))
45 aSketchLineB = aSketchFeature.addFeature("SketchLine")
46 aLineAStartPoint.setValue(0., 25)
47 aLineAEndPoint.setValue(85., 25)
48 # line B
49 aLineBStartPoint = geomDataAPI_Point2D(aSketchLineB.attribute("StartPoint"))
50 aLineBEndPoint = geomDataAPI_Point2D(aSketchLineB.attribute("EndPoint"))
51 aLineBStartPoint.setValue(0., 50)
52 aLineBEndPoint.setValue(80., 75)
53 aSession.finishOperation()
54 #=========================================================================
55 # Make a constraint to keep the length of the line constant
56 # to parallel perpendicular constraint collapsing line to point
57 #=========================================================================
58 aSession.startOperation()
59 for eachFeature in [aSketchLineA, aSketchLineB]:
60     aLengthConstraint = aSketchFeature.addFeature("SketchConstraintLength")
61     refattrA = aLengthConstraint.refattr("ConstraintEntityA")
62     aResultA = modelAPI_ResultConstruction(eachFeature.firstResult())
63     assert (aResultA is not None)
64     refattrA.setObject(aResultA)
65     aLengthConstraint.execute()
66 aSession.finishOperation()
67 # Coordinates of lines should not be changed after this constraint
68 assert (aLineAStartPoint.x() == 0)
69 assert (aLineAStartPoint.y() == 25)
70 assert (aLineAEndPoint.x() == 85)
71 assert (aLineAEndPoint.y() == 25)
72 assert (aLineBStartPoint.x() == 0)
73 assert (aLineBStartPoint.y() == 50)
74 assert (aLineBEndPoint.x() == 80)
75 assert (aLineBEndPoint.y() == 75)
76 #=========================================================================
77 # Link lines with parallel constraint
78 #=========================================================================
79 aSession.startOperation()
80 aParallelConstraint = aSketchFeature.addFeature("SketchConstraintParallel")
81 refattrA = aParallelConstraint.refattr("ConstraintEntityA")
82 refattrB = aParallelConstraint.refattr("ConstraintEntityB")
83 # aResultA is already defined for the length constraint
84 aResultA = modelAPI_ResultConstruction(aSketchLineA.firstResult())
85 aResultB = modelAPI_ResultConstruction(aSketchLineB.firstResult())
86 assert (aResultA is not None)
87 assert (aResultB is not None)
88 refattrA.setObject(aResultA)
89 refattrB.setObject(aResultB)
90 aParallelConstraint.execute()
91 aSession.finishOperation()
92 #=========================================================================
93 # Check values and move one constrainted object
94 #=========================================================================
95 deltaX = deltaY = 10.
96 # rotate line, check that reference's line points are moved also
97 aLineBStartPointPrev = (aLineBStartPoint.x(), aLineBStartPoint.y())
98 aLineBEndPointPrev = (aLineBEndPoint.x(), aLineBEndPoint.y())
99 aSession.startOperation()
100 aLineAStartPoint.setValue(aLineAStartPoint.x() + deltaX,
101                           aLineAStartPoint.y() + deltaY)
102 aLineAEndPoint.setValue(aLineAEndPoint.x() - deltaX,
103                         aLineAEndPoint.y() - deltaY)
104 aSession.finishOperation()
105 aLineBStartPointNew = (aLineBStartPoint.x(), aLineBStartPoint.y())
106 aLineBEndPointNew = (aLineBEndPoint.x(), aLineBEndPoint.y())
107
108 assert (aLineBStartPointPrev != aLineBStartPointNew)
109 assert (aLineBEndPointPrev != aLineBEndPointNew)
110 #=========================================================================
111 # End of test
112 #=========================================================================