]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/Test/TestConstraintCollinear.py
Salome HOME
Test cases for issues #1673, #1966 and #1967
[modules/shaper.git] / src / SketchPlugin / Test / TestConstraintCollinear.py
1 """
2     TestConstraintCollinear.py
3     Unit test of SketchPlugin_ConstraintCollinear class
4         
5     SketchPlugin_ConstraintCollinear
6         static const std::string MY_CONSTRAINT_COLLINEAR_ID("SketchConstraintCollinear");
7         data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
8         data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
9
10 """
11 from GeomDataAPI import *
12 from ModelAPI import *
13 import math
14 from salome.shaper import model
15
16 #=========================================================================
17 # Initialization of the test
18 #=========================================================================
19
20 __updated__ = "2016-01-28"
21
22
23 def checkCollinearVec(theX1, theY1, theX2, theY2):
24     TOL = 2.e-6
25     aLen1 = math.hypot(theX1, theY1)
26     aLen2 = math.hypot(theX2, theY2)
27     aDot = theX1 * theX2 + theY1 * theY2
28     assert (math.fabs(math.fabs(aDot) - aLen1 * aLen2) < TOL**2)
29
30 def checkCollinear(theLine1, theLine2):
31     aStartPoint1 = geomDataAPI_Point2D(theLine1.attribute("StartPoint"))
32     aEndPoint1   = geomDataAPI_Point2D(theLine1.attribute("EndPoint"))
33     aStartPoint2 = geomDataAPI_Point2D(theLine2.attribute("StartPoint"))
34     aEndPoint2   = geomDataAPI_Point2D(theLine2.attribute("EndPoint"))
35     
36     aDir1x, aDir1y = aEndPoint1.x() - aStartPoint1.x(), aEndPoint1.y() - aStartPoint1.y()
37     aDir2x, aDir2y = aEndPoint2.x() - aStartPoint1.x(), aEndPoint2.y() - aStartPoint1.y()
38     aDir3x, aDir3y = aStartPoint2.x() - aStartPoint1.x(), aStartPoint2.y() - aStartPoint1.y()
39     checkCollinearVec(aDir1x, aDir1y, aDir2x, aDir2y)
40     checkCollinearVec(aDir1x, aDir1y, aDir3x, aDir3y)
41
42
43
44 aSession = ModelAPI_Session.get()
45 aDocument = aSession.moduleDocument()
46 #=========================================================================
47 # Creation of a sketch
48 #=========================================================================
49 aSession.startOperation()
50 aSketchCommonFeature = aDocument.addFeature("Sketch")
51 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
52 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
53 origin.setValue(0, 0, 0)
54 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
55 dirx.setValue(1, 0, 0)
56 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
57 norm.setValue(0, 0, 1)
58 aSession.finishOperation()
59 #=========================================================================
60 # Create two lines
61 #=========================================================================
62 aSession.startOperation()
63 # line A
64 aSketchLineA = aSketchFeature.addFeature("SketchLine")
65 aLineAStartPoint = geomDataAPI_Point2D(aSketchLineA.attribute("StartPoint"))
66 aLineAEndPoint = geomDataAPI_Point2D(aSketchLineA.attribute("EndPoint"))
67 aSketchLineB = aSketchFeature.addFeature("SketchLine")
68 aLineAStartPoint.setValue(0., 25)
69 aLineAEndPoint.setValue(85., 25)
70 # line B
71 aLineBStartPoint = geomDataAPI_Point2D(aSketchLineB.attribute("StartPoint"))
72 aLineBEndPoint = geomDataAPI_Point2D(aSketchLineB.attribute("EndPoint"))
73 aLineBStartPoint.setValue(0., 50)
74 aLineBEndPoint.setValue(80., 75)
75 aSession.finishOperation()
76 assert (model.dof(aSketchFeature) == 8)
77 #=========================================================================
78 # Link lines with collinear constraint
79 #=========================================================================
80 aSession.startOperation()
81 aParallelConstraint = aSketchFeature.addFeature("SketchConstraintCollinear")
82 refattrA = aParallelConstraint.refattr("ConstraintEntityA")
83 refattrB = aParallelConstraint.refattr("ConstraintEntityB")
84 refattrA.setObject(aSketchLineA.firstResult())
85 refattrB.setObject(aSketchLineB.firstResult())
86 aParallelConstraint.execute()
87 aSession.finishOperation()
88 checkCollinear(aSketchLineA, aSketchLineB)
89 assert (model.dof(aSketchFeature) == 6)
90 #=========================================================================
91 # Check values and move one constrainted object
92 #=========================================================================
93 deltaX = deltaY = 10.
94 # rotate line, check that reference's line points are moved also
95 aLineBStartPointPrev = (aLineBStartPoint.x(), aLineBStartPoint.y())
96 aLineBEndPointPrev = (aLineBEndPoint.x(), aLineBEndPoint.y())
97 aSession.startOperation()
98 aLineAStartPoint.setValue(aLineAStartPoint.x() + deltaX,
99                           aLineAStartPoint.y() + deltaY)
100 aLineAEndPoint.setValue(aLineAEndPoint.x() - deltaX,
101                         aLineAEndPoint.y() - deltaY)
102 aSession.finishOperation()
103 checkCollinear(aSketchLineA, aSketchLineB)
104 assert (model.dof(aSketchFeature) == 6)
105 #=========================================================================
106 # End of test
107 #=========================================================================
108
109 assert(model.checkPythonDump())