Salome HOME
6f3d2dac5d4ccb72d9d8f3856efdd1dd81787a5f
[modules/shaper.git] / src / SketchPlugin / Test / TestConstraintCollinear.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     TestConstraintCollinear.py
23     Unit test of SketchPlugin_ConstraintCollinear class
24
25     SketchPlugin_ConstraintCollinear
26         static const std::string MY_CONSTRAINT_COLLINEAR_ID("SketchConstraintCollinear");
27         data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
28         data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
29
30 """
31
32 #=========================================================================
33 # Initialization of the test
34 #=========================================================================
35
36 from GeomDataAPI import *
37 from ModelAPI import *
38 import math
39 import unittest
40 from salome.shaper import model
41
42 __updated__ = "2017-03-06"
43
44 class TestConstraintCollinear(unittest.TestCase):
45   def setUp(self):
46     model.begin()
47     self.myDocument = model.moduleDocument()
48     self.mySketch = model.addSketch(self.myDocument, model.defaultPlane("XOY"))
49     self.myTolerance = 1.e-6
50     self.myDOF = 0
51
52   def tearDown(self):
53     model.end()
54     assert(model.checkPythonDump())
55
56   def checkVectorCollinearity(self, theX1, theY1, theX2, theY2):
57     aLen1 = math.hypot(theX1, theY1)
58     aLen2 = math.hypot(theX2, theY2)
59     aDot = theX1 * theX2 + theY1 * theY2
60     self.assertTrue(math.fabs(math.fabs(aDot) - aLen1 * aLen2) < self.myTolerance**2, "Vectors ({0}, {1}) and ({2}, {3}) do not collinear".format(theX1, theY1, theX2, theY2))
61
62   def checkLineCollinearity(self, theLine1, theLine2):
63     aStartPoint1 = theLine1.startPoint()
64     aEndPoint1   = theLine1.endPoint()
65     aStartPoint2 = theLine2.startPoint()
66     aEndPoint2   = theLine2.endPoint()
67
68     aDir1x, aDir1y = aEndPoint1.x() - aStartPoint1.x(), aEndPoint1.y() - aStartPoint1.y()
69     aDir2x, aDir2y = aEndPoint2.x() - aStartPoint1.x(), aEndPoint2.y() - aStartPoint1.y()
70     aDir3x, aDir3y = aStartPoint2.x() - aStartPoint1.x(), aStartPoint2.y() - aStartPoint1.y()
71     self.checkVectorCollinearity(aDir1x, aDir1y, aDir2x, aDir2y)
72     self.checkVectorCollinearity(aDir1x, aDir1y, aDir3x, aDir3y)
73
74   def moveLineAndCheckCollinearity(self, theLine1, theLine2):
75     deltaX = deltaY = 10.
76
77     theLine1.startPoint().setValue(theLine1.startPoint().x() + deltaX, theLine1.startPoint().y() + deltaY)
78     model.do()
79     self.checkLineCollinearity(theLine1, theLine2)
80     self.assertEqual(model.dof(self.mySketch), self.myDOF)
81
82     theLine1.endPoint().setValue(theLine1.endPoint().x() - deltaX, theLine1.endPoint().y() - deltaY)
83     model.do()
84     self.checkLineCollinearity(theLine1, theLine2)
85     self.assertEqual(model.dof(self.mySketch), self.myDOF)
86
87   def test_collinear_base(self):
88     """ Test 1. Collinearity two independent lines
89     """
90     aSketchLineA = self.mySketch.addLine(0., 25., 85., 25.)
91     aSketchLineB = self.mySketch.addLine(0., 50., 80., 75.)
92     self.myDOF += 8
93     self.assertEqual(model.dof(self.mySketch), self.myDOF)
94     self.mySketch.setCollinear(aSketchLineA, aSketchLineB)
95     self.myDOF -= 2
96     model.do()
97     self.assertEqual(model.dof(self.mySketch), self.myDOF)
98
99     self.checkLineCollinearity(aSketchLineA, aSketchLineB)
100     self.moveLineAndCheckCollinearity(aSketchLineA, aSketchLineB)
101
102   def test_collinear_connected_lines(self):
103     """ Test 2. Collinearity of two lines in polyline
104     """
105     aSketchLineA = self.mySketch.addLine(10., 20., 30., 40.)
106     aSketchLineB = self.mySketch.addLine(30., 40., 30., 70.)
107     self.myDOF += 8
108     self.assertEqual(model.dof(self.mySketch), self.myDOF)
109     self.mySketch.setCoincident(aSketchLineA.endPoint(), aSketchLineB.startPoint())
110     self.myDOF -= 2
111     model.do()
112     self.assertEqual(model.dof(self.mySketch), self.myDOF)
113
114     self.mySketch.setCollinear(aSketchLineA, aSketchLineB)
115     self.myDOF -= 1
116     model.do()
117     self.assertEqual(model.dof(self.mySketch), self.myDOF)
118
119     self.checkLineCollinearity(aSketchLineA, aSketchLineB)
120     self.moveLineAndCheckCollinearity(aSketchLineA, aSketchLineB)
121
122   def test_collinear_point_on_line(self):
123     """ Test 3. Collinearity for line which extremity is coincident with other line
124     """
125     aSketchLineA = self.mySketch.addLine(10., 20., 30., 40.)
126     aSketchLineB = self.mySketch.addLine(20., 40., 30., 70.)
127     self.myDOF += 8
128     self.assertEqual(model.dof(self.mySketch), self.myDOF)
129     self.mySketch.setCoincident(aSketchLineA.result(), aSketchLineB.startPoint())
130     self.myDOF -= 1
131     model.do()
132     self.assertEqual(model.dof(self.mySketch), self.myDOF)
133
134     self.mySketch.setCollinear(aSketchLineA, aSketchLineB)
135     self.myDOF -= 1
136     model.do()
137     self.assertEqual(model.dof(self.mySketch), self.myDOF)
138
139     self.checkLineCollinearity(aSketchLineA, aSketchLineB)
140     self.moveLineAndCheckCollinearity(aSketchLineA, aSketchLineB)
141
142   def test_already_collinear(self):
143     """ Test 4. Collinearity two lines connected by extremities to each other
144     """
145     aSketchLineA = self.mySketch.addLine(10., 20., 40., 50.)
146     aSketchLineB = self.mySketch.addLine(20., 40., 40., 70.)
147     self.myDOF += 8
148     self.assertEqual(model.dof(self.mySketch), self.myDOF)
149     self.mySketch.setCoincident(aSketchLineA.result(), aSketchLineB.startPoint())
150     self.mySketch.setCoincident(aSketchLineA.endPoint(), aSketchLineB.result())
151     self.myDOF -= 2
152     model.do()
153     self.assertEqual(model.dof(self.mySketch), self.myDOF)
154
155     aCollinear = self.mySketch.setCollinear(aSketchLineA, aSketchLineB)
156     model.do()
157     self.assertEqual(model.dof(self.mySketch), self.myDOF)
158
159     # check error message and remove non valid constraint
160     self.assertNotEqual(self.mySketch.solverError().value(), "")
161     self.myDocument.removeFeature(aCollinear.feature())
162     model.do()
163     self.assertEqual(model.dof(self.mySketch), self.myDOF)
164
165   def test_already_collinear2(self):
166     """ Test 5. Collinearity of two lines when one line is fully on other
167     """
168     aSketchLineA = self.mySketch.addLine(10., 20., 30., 40.)
169     aSketchLineB = self.mySketch.addLine(20., 40., 30., 70.)
170     self.myDOF += 8
171     self.assertEqual(model.dof(self.mySketch), self.myDOF)
172     self.mySketch.setCoincident(aSketchLineA.result(), aSketchLineB.startPoint())
173     self.mySketch.setCoincident(aSketchLineA.result(), aSketchLineB.endPoint())
174     self.myDOF -= 2
175     model.do()
176     self.assertEqual(model.dof(self.mySketch), self.myDOF)
177
178     aCollinear = self.mySketch.setCollinear(aSketchLineA, aSketchLineB)
179     model.do()
180     self.assertEqual(model.dof(self.mySketch), self.myDOF)
181
182     # check error message and remove non valid constraint
183     self.assertNotEqual(self.mySketch.solverError().value(), "")
184     self.myDocument.removeFeature(aCollinear.feature())
185     model.do()
186     self.assertEqual(model.dof(self.mySketch), self.myDOF)
187
188 if __name__ == '__main__':
189   unittest.main(exit=False)
190 #=========================================================================
191 # End of test
192 #=========================================================================