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