Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / SketchPlugin / Test / TestConstraintLength.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     TestConstraintLength.py
23     Unit test of SketchPlugin_ConstraintLength class
24
25     SketchPlugin_ConstraintLength
26         static const std::string MY_CONSTRAINT_LENGTH_ID("SketchConstraintLength");
27         data()->addAttribute(SketchPlugin_Constraint::VALUE(),    ModelAPI_AttributeDouble::typeId());
28         data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
29         data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
30
31 """
32 from GeomDataAPI import *
33 from ModelAPI import *
34 import math
35 from salome.shaper import model
36
37 #=========================================================================
38 # Initialization of the test
39 #=========================================================================
40
41 __updated__ = "2014-10-28"
42
43 aSession = ModelAPI_Session.get()
44 aDocument = aSession.moduleDocument()
45 #=========================================================================
46 # Creation of a sketch
47 #=========================================================================
48 aSession.startOperation()
49 aSketchCommonFeature = aDocument.addFeature("Sketch")
50 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
51 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
52 origin.setValue(0, 0, 0)
53 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
54 dirx.setValue(1, 0, 0)
55 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
56 norm.setValue(0, 0, 1)
57 aSession.finishOperation()
58 #=========================================================================
59 # Create a line with length 100
60 #=========================================================================
61 aSession.startOperation()
62 aSketchLineA = aSketchFeature.addFeature("SketchLine")
63 aLineAStartPoint = geomDataAPI_Point2D(aSketchLineA.attribute("StartPoint"))
64 aLineAEndPoint = geomDataAPI_Point2D(aSketchLineA.attribute("EndPoint"))
65 aLineAStartPoint.setValue(0., 25.)
66 aLineAEndPoint.setValue(100., 25.)
67 aSession.finishOperation()
68 assert (model.dof(aSketchFeature) == 4)
69 #=========================================================================
70 # Make a constraint to keep the length
71 #=========================================================================
72 aSession.startOperation()
73 aLengthConstraint = aSketchFeature.addFeature("SketchConstraintLength")
74 refattrA = aLengthConstraint.refattr("ConstraintEntityA")
75 aLength = aLengthConstraint.real("ConstraintValue")
76 assert (not refattrA.isInitialized())
77 assert (not aLength.isInitialized())
78
79 aResult = aSketchLineA.firstResult()
80 assert (aResult is not None)
81 refattrA.setObject(modelAPI_ResultConstruction(aResult))
82 # aLength.setValue(100.)
83 aLengthConstraint.execute()
84 aSession.finishOperation()
85 assert (aLength.isInitialized())
86 assert (refattrA.isInitialized())
87 assert (model.dof(aSketchFeature) == 3)
88 #=========================================================================
89 # Check values and move one constrainted object
90 #=========================================================================
91 deltaX = 40.
92 # move start point, check that end point are moved also
93 assert (aLineAStartPoint.x() == 0)
94 assert (aLineAStartPoint.y() == 25)
95 assert (aLineAEndPoint.x() == 100)
96 assert (aLineAEndPoint.y() == 25)
97 aSession.startOperation()
98 aLineAStartPoint.setValue(aLineAStartPoint.x() + deltaX,
99                           aLineAStartPoint.y())
100 aSession.finishOperation()
101 assert (model.dof(aSketchFeature) == 3)
102 assert (aLineAStartPoint.y() == 25)
103 assert (aLineAEndPoint.y() == 25)
104 # length of the line is the same
105 assert (math.fabs(aLineAEndPoint.x() - aLineAStartPoint.x() - 100) < 1.e-10)
106 #=========================================================================
107 # Change the length value of the constraint
108 #=========================================================================
109 aSession.startOperation()
110 aLength.setValue(140.)
111 aLengthConstraint.execute()
112 aSession.finishOperation()
113 assert (math.fabs(aLineAEndPoint.x() - aLineAStartPoint.x() - 140) < 1.e-10)
114 assert (model.dof(aSketchFeature) == 3)
115 #=========================================================================
116 # TODO: improve test
117 # 1. remove constraint, move line's start point to
118 #    check that constraint are not applied
119 #=========================================================================
120 #=========================================================================
121 # End of test
122 #=========================================================================
123
124 assert(model.checkPythonDump())