Salome HOME
2dd6ce202acedd6c1608ce9ad27df7b984705d0f
[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 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
33 norm.setValue(0, 0, 1)
34 aSession.finishOperation()
35 #=========================================================================
36 # Create two lines which are not parallel
37 #=========================================================================
38 aSession.startOperation()
39 # line A
40 aSketchLineA = aSketchFeature.addFeature("SketchLine")
41 aLineAStartPoint = geomDataAPI_Point2D(aSketchLineA.attribute("StartPoint"))
42 aLineAEndPoint = geomDataAPI_Point2D(aSketchLineA.attribute("EndPoint"))
43 aSketchLineB = aSketchFeature.addFeature("SketchLine")
44 aLineAStartPoint.setValue(0., 25)
45 aLineAEndPoint.setValue(85., 25)
46 # line B
47 aLineBStartPoint = geomDataAPI_Point2D(aSketchLineB.attribute("StartPoint"))
48 aLineBEndPoint = geomDataAPI_Point2D(aSketchLineB.attribute("EndPoint"))
49 aLineBStartPoint.setValue(0., 50)
50 aLineBEndPoint.setValue(80., 75)
51 aSession.finishOperation()
52 #=========================================================================
53 # Make a constraint to keep the length of the line constant
54 # to parallel perpendicular constraint collapsing line to point
55 #=========================================================================
56 aSession.startOperation()
57 for eachFeature in [aSketchLineA, aSketchLineB]:
58     aLengthConstraint = aSketchFeature.addFeature("SketchConstraintLength")
59     refattrA = aLengthConstraint.refattr("ConstraintEntityA")
60     aResultA = modelAPI_ResultConstruction(eachFeature.firstResult())
61     assert (aResultA is not None)
62     refattrA.setObject(aResultA)
63     aLengthConstraint.execute()
64 aSession.finishOperation()
65 # Coordinates of lines should not be changed after this constraint
66 assert (aLineAStartPoint.x() == 0)
67 assert (aLineAStartPoint.y() == 25)
68 assert (aLineAEndPoint.x() == 85)
69 assert (aLineAEndPoint.y() == 25)
70 assert (aLineBStartPoint.x() == 0)
71 assert (aLineBStartPoint.y() == 50)
72 assert (aLineBEndPoint.x() == 80)
73 assert (aLineBEndPoint.y() == 75)
74 #=========================================================================
75 # Link lines with parallel constraint
76 #=========================================================================
77 aSession.startOperation()
78 aParallelConstraint = aSketchFeature.addFeature("SketchConstraintParallel")
79 refattrA = aParallelConstraint.refattr("ConstraintEntityA")
80 refattrB = aParallelConstraint.refattr("ConstraintEntityB")
81 # aResultA is already defined for the length constraint
82 aResultA = modelAPI_ResultConstruction(aSketchLineA.firstResult())
83 aResultB = modelAPI_ResultConstruction(aSketchLineB.firstResult())
84 assert (aResultA is not None)
85 assert (aResultB is not None)
86 refattrA.setObject(aResultA)
87 refattrB.setObject(aResultB)
88 aParallelConstraint.execute()
89 aSession.finishOperation()
90 #=========================================================================
91 # Check values and move one constrainted object
92 #=========================================================================
93 deltaX = deltaY = 10.
94 # rotate line, check that reference's line points are moved also
95 aLineBStartPointPrev = (aLineBStartPoint.x(), aLineBStartPoint.y())
96 aLineBEndPointPrev = (aLineBEndPoint.x(), aLineBEndPoint.y())
97 aSession.startOperation()
98 aLineAStartPoint.setValue(aLineAStartPoint.x() + deltaX,
99                           aLineAStartPoint.y() + deltaY)
100 aLineAEndPoint.setValue(aLineAEndPoint.x() - deltaX,
101                         aLineAEndPoint.y() - deltaY)
102 aSession.finishOperation()
103 aLineBStartPointNew = (aLineBStartPoint.x(), aLineBStartPoint.y())
104 aLineBEndPointNew = (aLineBEndPoint.x(), aLineBEndPoint.y())
105
106 assert (aLineBStartPointPrev != aLineBStartPointNew)
107 assert (aLineBEndPointPrev != aLineBEndPointNew)
108 #=========================================================================
109 # End of test
110 #=========================================================================
111
112 from salome.shaper import model
113 assert(model.checkPythonDump())