Salome HOME
updated copyright message
[modules/shaper.git] / src / SketchPlugin / Test / TestConstraintLength.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     TestConstraintLength.py
22     Unit test of SketchPlugin_ConstraintLength class
23
24     SketchPlugin_ConstraintLength
25         static const std::string MY_CONSTRAINT_LENGTH_ID("SketchConstraintLength");
26         data()->addAttribute(SketchPlugin_Constraint::VALUE(),    ModelAPI_AttributeDouble::typeId());
27         data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
28         data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
29
30 """
31 from GeomDataAPI import *
32 from ModelAPI import *
33 import math
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 a line with length 100
59 #=========================================================================
60 aSession.startOperation()
61 aSketchLineA = aSketchFeature.addFeature("SketchLine")
62 aLineAStartPoint = geomDataAPI_Point2D(aSketchLineA.attribute("StartPoint"))
63 aLineAEndPoint = geomDataAPI_Point2D(aSketchLineA.attribute("EndPoint"))
64 aLineAStartPoint.setValue(0., 25.)
65 aLineAEndPoint.setValue(100., 25.)
66 aSession.finishOperation()
67 assert (model.dof(aSketchFeature) == 4)
68 #=========================================================================
69 # Make a constraint to keep the length
70 #=========================================================================
71 aSession.startOperation()
72 aLengthConstraint = aSketchFeature.addFeature("SketchConstraintLength")
73 refattrA = aLengthConstraint.refattr("ConstraintEntityA")
74 aLength = aLengthConstraint.real("ConstraintValue")
75 assert (not refattrA.isInitialized())
76 assert (not aLength.isInitialized())
77
78 aResult = aSketchLineA.firstResult()
79 assert (aResult is not None)
80 refattrA.setObject(modelAPI_ResultConstruction(aResult))
81 # aLength.setValue(100.)
82 aLengthConstraint.execute()
83 aSession.finishOperation()
84 assert (aLength.isInitialized())
85 assert (refattrA.isInitialized())
86 assert (model.dof(aSketchFeature) == 3)
87 #=========================================================================
88 # Check values and move one constrainted object
89 #=========================================================================
90 deltaX = 40.
91 # move start point, check that end point are moved also
92 assert (aLineAStartPoint.x() == 0)
93 assert (aLineAStartPoint.y() == 25)
94 assert (aLineAEndPoint.x() == 100)
95 assert (aLineAEndPoint.y() == 25)
96 aSession.startOperation()
97 aLineAStartPoint.setValue(aLineAStartPoint.x() + deltaX,
98                           aLineAStartPoint.y())
99 aSession.finishOperation()
100 assert (model.dof(aSketchFeature) == 3)
101 assert (aLineAStartPoint.y() == 25)
102 assert (aLineAEndPoint.y() == 25)
103 # length of the line is the same
104 assert (math.fabs(aLineAEndPoint.x() - aLineAStartPoint.x() - 100) < 1.e-10)
105 #=========================================================================
106 # Change the length value of the constraint
107 #=========================================================================
108 aSession.startOperation()
109 aLength.setValue(140.)
110 aLengthConstraint.execute()
111 aSession.finishOperation()
112 assert (math.fabs(aLineAEndPoint.x() - aLineAStartPoint.x() - 140) < 1.e-10)
113 assert (model.dof(aSketchFeature) == 3)
114 #=========================================================================
115 # TODO: improve test
116 # 1. remove constraint, move line's start point to
117 #    check that constraint are not applied
118 #=========================================================================
119 #=========================================================================
120 # End of test
121 #=========================================================================
122
123 assert(model.checkPythonDump())