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