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::typeId());
16 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
17 data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
19 from GeomDataAPI import *
20 from ModelAPI import *
21 from salome.shaper import model
23 #=========================================================================
24 # Initialization of the test
25 #=========================================================================
27 __updated__ = "2014-10-28"
29 aSession = ModelAPI_Session.get()
30 aDocument = aSession.moduleDocument()
31 #=========================================================================
32 # Creation of a sketch
33 #=========================================================================
34 aSession.startOperation()
35 aSketchCommonFeature = aDocument.addFeature("Sketch")
36 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
37 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
38 origin.setValue(0, 0, 0)
39 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
40 dirx.setValue(1, 0, 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"))
52 aLineAStartPoint.setValue(0., 25)
53 aLineAEndPoint.setValue(85., 25)
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 assert (model.dof(aSketchFeature) == 8)
62 #=========================================================================
63 # Make a constraint to keep the length of the line constant
64 # to prevent perpendicular constraint collapsing line to point
65 #=========================================================================
66 aSession.startOperation()
67 for eachFeature in [aSketchLineA, aSketchLineB]:
68 aLengthConstraint = aSketchFeature.addFeature("SketchConstraintLength")
69 eachRefattr = aLengthConstraint.refattr("ConstraintEntityA")
70 eachResult = modelAPI_ResultConstruction(eachFeature.firstResult())
71 assert (eachResult is not None)
72 eachRefattr.setObject(eachResult)
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 assert (model.dof(aSketchFeature) == 6)
86 #=========================================================================
87 # Link lines with perpendicular constraint
88 #=========================================================================
89 aSession.startOperation()
90 aPerpendicularConstraint = aSketchFeature.addFeature("SketchConstraintPerpendicular")
91 refattrA = aPerpendicularConstraint.refattr("ConstraintEntityA")
92 refattrB = aPerpendicularConstraint.refattr("ConstraintEntityB")
93 aResultA = modelAPI_ResultConstruction(aSketchLineA.firstResult())
94 aResultB = modelAPI_ResultConstruction(aSketchLineB.firstResult())
95 assert (aResultA is not None)
96 assert (aResultB is not None)
97 refattrA.setObject(aResultA)
98 refattrB.setObject(aResultB)
99 aPerpendicularConstraint.execute()
100 aSession.finishOperation()
101 assert (model.dof(aSketchFeature) == 5)
102 #=========================================================================
103 # Check values and move one constrainted object
104 #=========================================================================
105 aLineBStartPointPrev = (aLineBStartPoint.x(),aLineBStartPoint.y())
106 aLineBEndPointPrev = (aLineBEndPoint.x(),aLineBEndPoint.y())
108 # move line without rotation,
109 # check that reference's line points are not changed also
110 aSession.startOperation()
111 aLineAStartPoint.setValue(aLineAStartPoint.x() + deltaX, aLineAStartPoint.y() + deltaY)
112 aLineAEndPoint.setValue(aLineAEndPoint.x() + deltaX, aLineAEndPoint.y() + deltaY)
113 aSession.finishOperation()
114 assert (aLineBStartPointPrev == (aLineBStartPoint.x(), aLineBStartPoint.y()))
115 assert (aLineBEndPointPrev == (aLineBEndPoint.x(), aLineBEndPoint.y()))
116 aLineBStartPointPrev = (aLineBStartPoint.x(),aLineBStartPoint.y())
117 aLineBEndPointPrev = (aLineBEndPoint.x(),aLineBEndPoint.y())
119 # check that reference's line points are moved also
120 aSession.startOperation()
121 aLineAStartPoint.setValue(aLineAStartPoint.x() + deltaX, aLineAStartPoint.y() + deltaY)
122 aLineAEndPoint.setValue(aLineAEndPoint.x() - deltaX, aLineAEndPoint.y() - deltaY)
123 aSession.finishOperation()
124 assert (aLineBStartPointPrev != (aLineBStartPoint.x(), aLineBStartPoint.y()))
125 assert (aLineBEndPointPrev != (aLineBEndPoint.x(), aLineBEndPoint.y()))
126 assert (model.dof(aSketchFeature) == 5)
127 #=========================================================================
129 #=========================================================================
131 assert(model.checkPythonDump())