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