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-07-29"
27 aPluginManager = ModelAPI_PluginManager.get()
28 aDocument = aPluginManager.rootDocument()
29 #=========================================================================
30 # Creation of a sketch
31 #=========================================================================
32 aDocument.startOperation()
33 aSketchFeature = aDocument.addFeature("Sketch")
34 aSketchFeatureData = aSketchFeature.data()
35 origin = geomDataAPI_Point(aSketchFeatureData.attribute("Origin"))
36 origin.setValue(0, 0, 0)
37 dirx = geomDataAPI_Dir(aSketchFeatureData.attribute("DirX"))
38 dirx.setValue(1, 0, 0)
39 diry = geomDataAPI_Dir(aSketchFeatureData.attribute("DirY"))
40 diry.setValue(0, 1, 0)
41 norm = geomDataAPI_Dir(aSketchFeatureData.attribute("Norm"))
42 norm.setValue(0, 0, 1)
43 aDocument.finishOperation()
44 #=========================================================================
45 # Create two lines which are already perpendicular
46 #=========================================================================
47 aDocument.startOperation()
48 aSketchReflist = aSketchFeatureData.reflist("Features")
50 aSketchLineA = aDocument.addFeature("SketchLine")
51 aSketchReflist.append(aSketchLineA)
52 aLineAStartPoint = geomDataAPI_Point2D(
53 aSketchLineA.data().attribute("StartPoint"))
54 aLineAEndPoint = geomDataAPI_Point2D(
55 aSketchLineA.data().attribute("EndPoint"))
56 aSketchLineB = aDocument.addFeature("SketchLine")
57 aLineAStartPoint.setValue(0., 25)
58 aLineAEndPoint.setValue(85., 25)
60 aSketchReflist.append(aSketchLineB)
61 aLineBStartPoint = geomDataAPI_Point2D(
62 aSketchLineB.data().attribute("StartPoint"))
63 aLineBEndPoint = geomDataAPI_Point2D(
64 aSketchLineB.data().attribute("EndPoint"))
65 aLineBStartPoint.setValue(25., 40.)
66 aLineBEndPoint.setValue(25., 125.)
67 aDocument.finishOperation()
68 #=========================================================================
69 # Make a constraint to keep the length of the line constant
70 # to prevent perpendicular constraint collapsing line to point
71 #=========================================================================
72 aDocument.startOperation()
73 aLengthConstraint = aDocument.addFeature("SketchConstraintLength")
74 aSketchReflist.append(aLengthConstraint)
75 aLengthConstraintData = aLengthConstraint.data()
76 refattrA = aLengthConstraintData.refattr("ConstraintEntityA")
77 aResultA = modelAPI_ResultConstruction(aSketchLineA.firstResult())
78 assert (aResultA is not None)
79 refattrA.setObject(aResultA)
80 aDocument.finishOperation()
81 # Coordinates of lines should not be changed after this constraint
82 assert (aLineAStartPoint.x() == 0)
83 assert (aLineAStartPoint.y() == 25)
84 assert (aLineAEndPoint.x() == 85)
85 assert (aLineAEndPoint.y() == 25)
86 assert (aLineBStartPoint.x() == 25)
87 assert (aLineBStartPoint.y() == 40)
88 assert (aLineBEndPoint.x() == 25)
89 assert (aLineBEndPoint.y() == 125)
90 #=========================================================================
91 # Link lines with perpendicular constraint
92 #=========================================================================
93 aDocument.startOperation()
94 aParallelConstraint = aDocument.addFeature("SketchConstraintParallel")
95 aSketchReflist.append(aParallelConstraint)
96 aConstraintData = aParallelConstraint.data()
97 refattrA = aConstraintData.refattr("ConstraintEntityA")
98 refattrB = aConstraintData.refattr("ConstraintEntityB")
99 # aResultA is already defined for the length constraint
100 aResultB = modelAPI_ResultConstruction(aSketchLineB.firstResult())
101 assert (aResultB is not None)
102 refattrA.setObject(aResultA)
103 refattrB.setObject(aResultB)
104 aDocument.finishOperation()
105 #=========================================================================
106 # Check values and move one constrainted object
107 #=========================================================================
108 # print "Check values and move one constrainted object"
109 # print "assert (aLineAStartPoint.x() == %d)" % aLineAStartPoint.x()
110 # print "assert (aLineAStartPoint.y() == %d)" % aLineAStartPoint.y()
111 # print "assert (aLineAEndPoint.x() == %d)" % aLineAEndPoint.x()
112 # print "assert (aLineAEndPoint.y() == %d)" % aLineAEndPoint.y()
113 # print "assert (aLineBStartPoint.x() == %d)" % aLineBStartPoint.x()
114 # print "assert (aLineBStartPoint.y() == %d)" % aLineBStartPoint.y()
115 # print "assert (aLineBEndPoint.x() == %d)" % aLineBEndPoint.x()
116 # print "assert (aLineBEndPoint.y() == %d)" % aLineBEndPoint.y()
118 # move line without rotation,
119 # check that reference's line points are not changed also
120 aLineAStartPoint.setValue(aLineAStartPoint.x() + deltaX,
121 aLineAStartPoint.y() + deltaY)
122 aLineAEndPoint.setValue(aLineAEndPoint.x() + deltaX,
123 aLineAEndPoint.y() + deltaY)
124 # print "move line without rotation"
125 # print "assert (aLineAStartPoint.x() == %d)" % aLineAStartPoint.x()
126 # print "assert (aLineAStartPoint.y() == %d)" % aLineAStartPoint.y()
127 # print "assert (aLineAEndPoint.x() == %d)" % aLineAEndPoint.x()
128 # print "assert (aLineAEndPoint.y() == %d)" % aLineAEndPoint.y()
129 # print "assert (aLineBStartPoint.x() == %d)" % aLineBStartPoint.x()
130 # print "assert (aLineBStartPoint.y() == %d)" % aLineBStartPoint.y()
131 # print "assert (aLineBEndPoint.x() == %d)" % aLineBEndPoint.x()
132 # print "assert (aLineBEndPoint.y() == %d)" % aLineBEndPoint.y()
134 # rotate line, check that reference's line points are moved also
135 aLineAStartPoint.setValue(aLineAStartPoint.x() + deltaX,
136 aLineAStartPoint.y() + deltaY)
137 aLineAEndPoint.setValue(aLineAEndPoint.x() - deltaX,
138 aLineAEndPoint.y() - deltaY)
139 # print "Rotate line, check that reference's line points are moved also"
140 # print "assert (aLineAStartPoint.x() == %d)" % aLineAStartPoint.x()
141 # print "assert (aLineAStartPoint.y() == %d)" % aLineAStartPoint.y()
142 # print "assert (aLineAEndPoint.x() == %d)" % aLineAEndPoint.x()
143 # print "assert (aLineAEndPoint.y() == %d)" % aLineAEndPoint.y()
144 # print "assert (aLineBStartPoint.x() == %d)" % aLineBStartPoint.x()
145 # print "assert (aLineBStartPoint.y() == %d)" % aLineBStartPoint.y()
146 # print "assert (aLineBEndPoint.x() == %d)" % aLineBEndPoint.x()
147 # print "assert (aLineBEndPoint.y() == %d)" % aLineBEndPoint.y()
148 #=========================================================================
150 #=========================================================================