Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom.git into Dev_1.1.0
[modules/shaper.git] / src / SketchPlugin / Test / TestConstraintPerpendicular.py
1 """
2     TestConstraintPerpendicular.py
3     Unit test of SketchPlugin_ConstraintPerpendicular class
4     
5     SketchPlugin_Constraint
6         static const std::string MY_CONSTRAINT_VALUE("ConstraintValue");
7         static const std::string MY_FLYOUT_VALUE_PNT("ConstraintFlyoutValuePnt");
8         static const std::string MY_ENTITY_A("ConstraintEntityA");
9         static const std::string MY_ENTITY_B("ConstraintEntityB");
10         static const std::string MY_ENTITY_C("ConstraintEntityC");
11         static const std::string MY_ENTITY_D("ConstraintEntityD");
12         
13     SketchPlugin_ConstraintPerpendicular
14         static const std::string MY_CONSTRAINT_PERPENDICULAR_ID("SketchConstraintPerpendicular");
15         data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
16         data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
17         data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId()); 
18 """
19 from GeomDataAPI import *
20 from ModelAPI import *
21 #=========================================================================
22 # Initialization of the test
23 #=========================================================================
24
25 __updated__ = "2014-10-28"
26
27 aSession = ModelAPI_Session.get()
28 aDocument = aSession.moduleDocument()
29 #=========================================================================
30 # Creation of a sketch
31 #=========================================================================
32 aSession.startOperation()
33 aSketchCommonFeature = aDocument.addFeature("Sketch")
34 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
35 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
36 origin.setValue(0, 0, 0)
37 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
38 dirx.setValue(1, 0, 0)
39 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
40 norm.setValue(0, 0, 1)
41 aSession.finishOperation()
42 #=========================================================================
43 # Create two lines which are already perpendicular
44 #=========================================================================
45 aSession.startOperation()
46 # line A
47 aSketchLineA = aSketchFeature.addFeature("SketchLine")
48 aLineAStartPoint = geomDataAPI_Point2D(aSketchLineA.attribute("StartPoint"))
49 aLineAEndPoint = geomDataAPI_Point2D(aSketchLineA.attribute("EndPoint"))
50 aLineAStartPoint.setValue(0., 25)
51 aLineAEndPoint.setValue(85., 25)
52 # line B
53 aSketchLineB = aSketchFeature.addFeature("SketchLine")
54 aLineBStartPoint = geomDataAPI_Point2D(aSketchLineB.attribute("StartPoint"))
55 aLineBEndPoint = geomDataAPI_Point2D(aSketchLineB.attribute("EndPoint"))
56 aLineBStartPoint.setValue(25., 40.)
57 aLineBEndPoint.setValue(25., 125.)
58 aSession.finishOperation()
59 #=========================================================================
60 # Make a constraint to keep the length of the line constant
61 # to prevent perpendicular constraint collapsing line to point
62 #=========================================================================
63 aSession.startOperation()
64 for eachFeature in [aSketchLineA, aSketchLineB]:
65     aLengthConstraint = aSketchFeature.addFeature("SketchConstraintLength")
66     eachRefattr = aLengthConstraint.refattr("ConstraintEntityA")
67     eachResult = modelAPI_ResultConstruction(eachFeature.firstResult())
68     assert (eachResult is not None)
69     eachRefattr.setObject(eachResult)
70     aLengthConstraint.execute()
71 aSession.finishOperation()
72
73 # Coordinates of lines should not be changed after this constraint
74 assert (aLineAStartPoint.x() == 0)
75 assert (aLineAStartPoint.y() == 25)
76 assert (aLineAEndPoint.x() == 85)
77 assert (aLineAEndPoint.y() == 25)
78 assert (aLineBStartPoint.x() == 25)
79 assert (aLineBStartPoint.y() == 40)
80 assert (aLineBEndPoint.x() == 25)
81 assert (aLineBEndPoint.y() == 125)
82 #=========================================================================
83 # Link lines with perpendicular constraint
84 #=========================================================================
85 aSession.startOperation()
86 aPerpendicularConstraint = aSketchFeature.addFeature("SketchConstraintPerpendicular")
87 refattrA = aPerpendicularConstraint.refattr("ConstraintEntityA")
88 refattrB = aPerpendicularConstraint.refattr("ConstraintEntityB")
89 aResultA = modelAPI_ResultConstruction(aSketchLineA.firstResult())
90 aResultB = modelAPI_ResultConstruction(aSketchLineB.firstResult())
91 assert (aResultA is not None)
92 assert (aResultB is not None)
93 refattrA.setObject(aResultA)
94 refattrB.setObject(aResultB)
95 aPerpendicularConstraint.execute()
96 aSession.finishOperation()
97 #=========================================================================
98 # Check values and move one constrainted object
99 #=========================================================================
100 aLineBStartPointPrev = (aLineBStartPoint.x(),aLineBStartPoint.y())
101 aLineBEndPointPrev = (aLineBEndPoint.x(),aLineBEndPoint.y())
102 deltaX = deltaY = 5.
103 # move line without rotation,
104 # check that reference's line points are not changed also
105 aSession.startOperation()
106 aLineAStartPoint.setValue(aLineAStartPoint.x() + deltaX, aLineAStartPoint.y() + deltaY)
107 aLineAEndPoint.setValue(aLineAEndPoint.x() + deltaX, aLineAEndPoint.y() + deltaY)
108 aSession.finishOperation()
109 assert (aLineBStartPointPrev == (aLineBStartPoint.x(), aLineBStartPoint.y()))
110 assert (aLineBEndPointPrev   == (aLineBEndPoint.x(),   aLineBEndPoint.y()))
111 aLineBStartPointPrev = (aLineBStartPoint.x(),aLineBStartPoint.y())
112 aLineBEndPointPrev = (aLineBEndPoint.x(),aLineBEndPoint.y())
113 # rotate line, 
114 # check that reference's line points are moved also
115 aSession.startOperation()
116 aLineAStartPoint.setValue(aLineAStartPoint.x() + deltaX, aLineAStartPoint.y() + deltaY)
117 aLineAEndPoint.setValue(aLineAEndPoint.x() - deltaX, aLineAEndPoint.y() - deltaY)
118 aSession.finishOperation()
119 assert (aLineBStartPointPrev != (aLineBStartPoint.x(), aLineBStartPoint.y()))
120 assert (aLineBEndPointPrev   != (aLineBEndPoint.x(),   aLineBEndPoint.y()))
121 #=========================================================================
122 # End of test
123 #=========================================================================