Salome HOME
Merge branch 'master' into cgt/devCEA
[modules/shaper.git] / src / SketchPlugin / Test / TestConstraintPerpendicular.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     TestConstraintPerpendicular.py
23     Unit test of SketchPlugin_ConstraintPerpendicular class
24
25     SketchPlugin_Constraint
26         static const std::string MY_CONSTRAINT_VALUE("ConstraintValue");
27         static const std::string MY_FLYOUT_VALUE_PNT("ConstraintFlyoutValuePnt");
28         static const std::string MY_ENTITY_A("ConstraintEntityA");
29         static const std::string MY_ENTITY_B("ConstraintEntityB");
30         static const std::string MY_ENTITY_C("ConstraintEntityC");
31         static const std::string MY_ENTITY_D("ConstraintEntityD");
32
33     SketchPlugin_ConstraintPerpendicular
34         static const std::string MY_CONSTRAINT_PERPENDICULAR_ID("SketchConstraintPerpendicular");
35         data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
36         data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
37         data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
38 """
39 from GeomDataAPI import *
40 from ModelAPI import *
41 from salome.shaper import model
42
43 #=========================================================================
44 # Initialization of the test
45 #=========================================================================
46
47 __updated__ = "2014-10-28"
48
49 aSession = ModelAPI_Session.get()
50 aDocument = aSession.moduleDocument()
51 #=========================================================================
52 # Creation of a sketch
53 #=========================================================================
54 aSession.startOperation()
55 aSketchCommonFeature = aDocument.addFeature("Sketch")
56 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
57 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
58 origin.setValue(0, 0, 0)
59 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
60 dirx.setValue(1, 0, 0)
61 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
62 norm.setValue(0, 0, 1)
63 aSession.finishOperation()
64 #=========================================================================
65 # Create two lines which are already perpendicular
66 #=========================================================================
67 aSession.startOperation()
68 # line A
69 aSketchLineA = aSketchFeature.addFeature("SketchLine")
70 aLineAStartPoint = geomDataAPI_Point2D(aSketchLineA.attribute("StartPoint"))
71 aLineAEndPoint = geomDataAPI_Point2D(aSketchLineA.attribute("EndPoint"))
72 aLineAStartPoint.setValue(0., 25)
73 aLineAEndPoint.setValue(85., 25)
74 # line B
75 aSketchLineB = aSketchFeature.addFeature("SketchLine")
76 aLineBStartPoint = geomDataAPI_Point2D(aSketchLineB.attribute("StartPoint"))
77 aLineBEndPoint = geomDataAPI_Point2D(aSketchLineB.attribute("EndPoint"))
78 aLineBStartPoint.setValue(25., 40.)
79 aLineBEndPoint.setValue(25., 125.)
80 aSession.finishOperation()
81 assert (model.dof(aSketchFeature) == 8)
82 #=========================================================================
83 # Make a constraint to keep the length of the line constant
84 # to prevent perpendicular constraint collapsing line to point
85 #=========================================================================
86 aSession.startOperation()
87 for eachFeature in [aSketchLineA, aSketchLineB]:
88     aLengthConstraint = aSketchFeature.addFeature("SketchConstraintLength")
89     eachRefattr = aLengthConstraint.refattr("ConstraintEntityA")
90     eachResult = modelAPI_ResultConstruction(eachFeature.firstResult())
91     assert (eachResult is not None)
92     eachRefattr.setObject(eachResult)
93     aLengthConstraint.execute()
94 aSession.finishOperation()
95
96 # Coordinates of lines should not be changed after this constraint
97 assert (aLineAStartPoint.x() == 0)
98 assert (aLineAStartPoint.y() == 25)
99 assert (aLineAEndPoint.x() == 85)
100 assert (aLineAEndPoint.y() == 25)
101 assert (aLineBStartPoint.x() == 25)
102 assert (aLineBStartPoint.y() == 40)
103 assert (aLineBEndPoint.x() == 25)
104 assert (aLineBEndPoint.y() == 125)
105 assert (model.dof(aSketchFeature) == 6)
106 #=========================================================================
107 # Link lines with perpendicular constraint
108 #=========================================================================
109 aSession.startOperation()
110 aPerpendicularConstraint = aSketchFeature.addFeature("SketchConstraintPerpendicular")
111 refattrA = aPerpendicularConstraint.refattr("ConstraintEntityA")
112 refattrB = aPerpendicularConstraint.refattr("ConstraintEntityB")
113 aResultA = modelAPI_ResultConstruction(aSketchLineA.firstResult())
114 aResultB = modelAPI_ResultConstruction(aSketchLineB.firstResult())
115 assert (aResultA is not None)
116 assert (aResultB is not None)
117 refattrA.setObject(aResultA)
118 refattrB.setObject(aResultB)
119 aPerpendicularConstraint.execute()
120 aSession.finishOperation()
121 assert (model.dof(aSketchFeature) == 5)
122 #=========================================================================
123 # Check values and move one constrainted object
124 #=========================================================================
125 aLineBStartPointPrev = (aLineBStartPoint.x(),aLineBStartPoint.y())
126 aLineBEndPointPrev = (aLineBEndPoint.x(),aLineBEndPoint.y())
127 deltaX = deltaY = 5.
128 # move line without rotation,
129 # check that reference's line points are not changed also
130 aSession.startOperation()
131 aLineAStartPoint.setValue(aLineAStartPoint.x() + deltaX, aLineAStartPoint.y() + deltaY)
132 aLineAEndPoint.setValue(aLineAEndPoint.x() + deltaX, aLineAEndPoint.y() + deltaY)
133 aSession.finishOperation()
134 assert (aLineBStartPointPrev == (aLineBStartPoint.x(), aLineBStartPoint.y()))
135 assert (aLineBEndPointPrev   == (aLineBEndPoint.x(),   aLineBEndPoint.y()))
136 aLineBStartPointPrev = (aLineBStartPoint.x(),aLineBStartPoint.y())
137 aLineBEndPointPrev = (aLineBEndPoint.x(),aLineBEndPoint.y())
138 # rotate line,
139 # check that reference's line points are moved also
140 aSession.startOperation()
141 aLineAStartPoint.setValue(aLineAStartPoint.x() + deltaX, aLineAStartPoint.y() + deltaY)
142 aLineAEndPoint.setValue(aLineAEndPoint.x() - deltaX, aLineAEndPoint.y() - deltaY)
143 aSession.finishOperation()
144 assert (aLineBStartPointPrev != (aLineBStartPoint.x(), aLineBStartPoint.y()))
145 assert (aLineBEndPointPrev   != (aLineBEndPoint.x(),   aLineBEndPoint.y()))
146 assert (model.dof(aSketchFeature) == 5)
147 #=========================================================================
148 # End of test
149 #=========================================================================
150
151 assert(model.checkPythonDump())