Salome HOME
Fix crash while changing the argument of the Projection
[modules/shaper.git] / src / SketchPlugin / Test / TestConstraintParallel.py
1 ## Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 ##
3 ## This library is free software; you can redistribute it and/or
4 ## modify it under the terms of the GNU Lesser General Public
5 ## License as published by the Free Software Foundation; either
6 ## version 2.1 of the License, or (at your option) any later version.
7 ##
8 ## This library is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 ## Lesser General Public License for more details.
12 ##
13 ## You should have received a copy of the GNU Lesser General Public
14 ## License along with this library; if not, write to the Free Software
15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 ##
17 ## See http:##www.salome-platform.org/ or
18 ## email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 ##
20
21 """
22     TestConstraintParallel.py
23     Unit test of SketchPlugin_ConstraintParallel class
24
25     SketchPlugin_ConstraintParallel
26         static const std::string MY_CONSTRAINT_PARALLEL_ID("SketchConstraintParallel");
27         data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
28         data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
29         data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
30
31 """
32 from GeomDataAPI import *
33 from ModelAPI import *
34 from salome.shaper import model
35
36 #=========================================================================
37 # Initialization of the test
38 #=========================================================================
39
40 __updated__ = "2014-10-28"
41
42 aSession = ModelAPI_Session.get()
43 aDocument = aSession.moduleDocument()
44 #=========================================================================
45 # Creation of a sketch
46 #=========================================================================
47 aSession.startOperation()
48 aSketchCommonFeature = aDocument.addFeature("Sketch")
49 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
50 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
51 origin.setValue(0, 0, 0)
52 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
53 dirx.setValue(1, 0, 0)
54 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
55 norm.setValue(0, 0, 1)
56 aSession.finishOperation()
57 #=========================================================================
58 # Create two lines which are not parallel
59 #=========================================================================
60 aSession.startOperation()
61 # line A
62 aSketchLineA = aSketchFeature.addFeature("SketchLine")
63 aLineAStartPoint = geomDataAPI_Point2D(aSketchLineA.attribute("StartPoint"))
64 aLineAEndPoint = geomDataAPI_Point2D(aSketchLineA.attribute("EndPoint"))
65 aSketchLineB = aSketchFeature.addFeature("SketchLine")
66 aLineAStartPoint.setValue(0., 25)
67 aLineAEndPoint.setValue(85., 25)
68 # line B
69 aLineBStartPoint = geomDataAPI_Point2D(aSketchLineB.attribute("StartPoint"))
70 aLineBEndPoint = geomDataAPI_Point2D(aSketchLineB.attribute("EndPoint"))
71 aLineBStartPoint.setValue(0., 50)
72 aLineBEndPoint.setValue(80., 75)
73 aSession.finishOperation()
74 assert (model.dof(aSketchFeature) == 8)
75 #=========================================================================
76 # Make a constraint to keep the length of the line constant
77 # to parallel perpendicular constraint collapsing line to point
78 #=========================================================================
79 aSession.startOperation()
80 for eachFeature in [aSketchLineA, aSketchLineB]:
81     aLengthConstraint = aSketchFeature.addFeature("SketchConstraintLength")
82     refattrA = aLengthConstraint.refattr("ConstraintEntityA")
83     aResultA = modelAPI_ResultConstruction(eachFeature.firstResult())
84     assert (aResultA is not None)
85     refattrA.setObject(aResultA)
86     aLengthConstraint.execute()
87 aSession.finishOperation()
88 # Coordinates of lines should not be changed after this constraint
89 assert (aLineAStartPoint.x() == 0)
90 assert (aLineAStartPoint.y() == 25)
91 assert (aLineAEndPoint.x() == 85)
92 assert (aLineAEndPoint.y() == 25)
93 assert (aLineBStartPoint.x() == 0)
94 assert (aLineBStartPoint.y() == 50)
95 assert (aLineBEndPoint.x() == 80)
96 assert (aLineBEndPoint.y() == 75)
97 assert (model.dof(aSketchFeature) == 6)
98 #=========================================================================
99 # Link lines with parallel constraint
100 #=========================================================================
101 aSession.startOperation()
102 aParallelConstraint = aSketchFeature.addFeature("SketchConstraintParallel")
103 refattrA = aParallelConstraint.refattr("ConstraintEntityA")
104 refattrB = aParallelConstraint.refattr("ConstraintEntityB")
105 # aResultA is already defined for the length constraint
106 aResultA = modelAPI_ResultConstruction(aSketchLineA.firstResult())
107 aResultB = modelAPI_ResultConstruction(aSketchLineB.firstResult())
108 assert (aResultA is not None)
109 assert (aResultB is not None)
110 refattrA.setObject(aResultA)
111 refattrB.setObject(aResultB)
112 aParallelConstraint.execute()
113 aSession.finishOperation()
114 assert (model.dof(aSketchFeature) == 5)
115 #=========================================================================
116 # Check values and move one constrainted object
117 #=========================================================================
118 deltaX = deltaY = 10.
119 # rotate line, check that reference's line points are moved also
120 aLineBStartPointPrev = (aLineBStartPoint.x(), aLineBStartPoint.y())
121 aLineBEndPointPrev = (aLineBEndPoint.x(), aLineBEndPoint.y())
122 aSession.startOperation()
123 aLineAStartPoint.setValue(aLineAStartPoint.x() + deltaX,
124                           aLineAStartPoint.y() + deltaY)
125 aLineAEndPoint.setValue(aLineAEndPoint.x() - deltaX,
126                         aLineAEndPoint.y() - deltaY)
127 aSession.finishOperation()
128 aLineBStartPointNew = (aLineBStartPoint.x(), aLineBStartPoint.y())
129 aLineBEndPointNew = (aLineBEndPoint.x(), aLineBEndPoint.y())
130
131 assert (aLineBStartPointPrev != aLineBStartPointNew)
132 assert (aLineBEndPointPrev != aLineBEndPointNew)
133 assert (model.dof(aSketchFeature) == 5)
134 #=========================================================================
135 # End of test
136 #=========================================================================
137
138 assert(model.checkPythonDump())