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