2 TestConstraintPerpendicular.py
3 Unit test of SketchPlugin_ConstraintPerpendicular class
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");
13 SketchPlugin_ConstraintPerpendicular
14 static const std::string MY_CONSTRAINT_PERPENDICULAR_ID("SketchConstraintPerpendicular");
15 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::type());
16 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::type());
17 data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::type());
19 from GeomDataAPI import *
20 from ModelAPI import *
21 #=========================================================================
22 # Initialization of the test
23 #=========================================================================
25 __updated__ = "2014-10-28"
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 = modelAPI_CompositeFeature(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()
49 aSketchLineA = aSketchFeature.addFeature("SketchLine")
50 aLineAStartPoint = geomDataAPI_Point2D(aSketchLineA.attribute("StartPoint"))
51 aLineAEndPoint = geomDataAPI_Point2D(aSketchLineA.attribute("EndPoint"))
53 aSketchLineB = aSketchFeature.addFeature("SketchLine")
54 aLineAStartPoint.setValue(0., 25)
55 aLineAEndPoint.setValue(85., 25)
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 #=========================================================================
66 for eachFeature in [aSketchLineA, aSketchLineB]:
67 aSession.startOperation()
68 aLengthConstraint = aSketchFeature.addFeature("SketchConstraintLength")
69 refattrA = aLengthConstraint.refattr("ConstraintEntityA")
70 aResultA = modelAPI_ResultConstruction(eachFeature.firstResult())
71 assert (aResultA is not None)
72 refattrA.setObject(aResultA)
73 aLengthConstraint.execute()
74 aSession.finishOperation()
76 # Coordinates of lines should not be changed after this constraint
77 assert (aLineAStartPoint.x() == 0)
78 assert (aLineAStartPoint.y() == 25)
79 assert (aLineAEndPoint.x() == 85)
80 assert (aLineAEndPoint.y() == 25)
81 assert (aLineBStartPoint.x() == 25)
82 assert (aLineBStartPoint.y() == 40)
83 assert (aLineBEndPoint.x() == 25)
84 assert (aLineBEndPoint.y() == 125)
85 #=========================================================================
86 # Link lines with perpendicular constraint
87 #=========================================================================
88 aSession.startOperation()
89 aPerpendicularConstraint = aSketchFeature.addFeature(
90 "SketchConstraintPerpendicular")
91 refattrA = aPerpendicularConstraint.refattr("ConstraintEntityA")
92 refattrB = aPerpendicularConstraint.refattr("ConstraintEntityB")
93 # aResultA is already defined for the length constraint
94 aResultB = modelAPI_ResultConstruction(aSketchLineB.firstResult())
95 assert (aResultB is not None)
96 refattrA.setObject(aResultA)
97 refattrB.setObject(aResultB)
98 aPerpendicularConstraint.execute()
99 aSession.finishOperation()
100 #=========================================================================
101 # Check values and move one constrainted object
102 #=========================================================================
103 # print "Check values and move one constrainted object"
104 # print "assert (aLineAStartPoint.x() == %d)" % aLineAStartPoint.x()
105 # print "assert (aLineAStartPoint.y() == %d)" % aLineAStartPoint.y()
106 # print "assert (aLineAEndPoint.x() == %d)" % aLineAEndPoint.x()
107 # print "assert (aLineAEndPoint.y() == %d)" % aLineAEndPoint.y()
108 # print "assert (aLineBStartPoint.x() == %d)" % aLineBStartPoint.x()
109 # print "assert (aLineBStartPoint.y() == %d)" % aLineBStartPoint.y()
110 # print "assert (aLineBEndPoint.x() == %d)" % aLineBEndPoint.x()
111 # print "assert (aLineBEndPoint.y() == %d)" % aLineBEndPoint.y()
113 # move line without rotation,
114 # check that reference's line points are not changed also
115 aLineAStartPoint.setValue(aLineAStartPoint.x() + deltaX,
116 aLineAStartPoint.y() + deltaY)
117 aLineAEndPoint.setValue(aLineAEndPoint.x() + deltaX,
118 aLineAEndPoint.y() + deltaY)
119 # print "move line without rotation"
120 # print "assert (aLineAStartPoint.x() == %d)" % aLineAStartPoint.x()
121 # print "assert (aLineAStartPoint.y() == %d)" % aLineAStartPoint.y()
122 # print "assert (aLineAEndPoint.x() == %d)" % aLineAEndPoint.x()
123 # print "assert (aLineAEndPoint.y() == %d)" % aLineAEndPoint.y()
124 # print "assert (aLineBStartPoint.x() == %d)" % aLineBStartPoint.x()
125 # print "assert (aLineBStartPoint.y() == %d)" % aLineBStartPoint.y()
126 # print "assert (aLineBEndPoint.x() == %d)" % aLineBEndPoint.x()
127 # print "assert (aLineBEndPoint.y() == %d)" % aLineBEndPoint.y()
129 # rotate line, check that reference's line points are moved also
130 aLineAStartPoint.setValue(aLineAStartPoint.x() + deltaX,
131 aLineAStartPoint.y() + deltaY)
132 aLineAEndPoint.setValue(aLineAEndPoint.x() - deltaX,
133 aLineAEndPoint.y() - deltaY)
134 # print "Rotate line, check that reference's line points are moved also"
135 # print "assert (aLineAStartPoint.x() == %d)" % aLineAStartPoint.x()
136 # print "assert (aLineAStartPoint.y() == %d)" % aLineAStartPoint.y()
137 # print "assert (aLineAEndPoint.x() == %d)" % aLineAEndPoint.x()
138 # print "assert (aLineAEndPoint.y() == %d)" % aLineAEndPoint.y()
139 # print "assert (aLineBStartPoint.x() == %d)" % aLineBStartPoint.x()
140 # print "assert (aLineBStartPoint.y() == %d)" % aLineBStartPoint.y()
141 # print "assert (aLineBEndPoint.x() == %d)" % aLineBEndPoint.x()
142 # print "assert (aLineBEndPoint.y() == %d)" % aLineBEndPoint.y()
143 #=========================================================================
145 #=========================================================================