1 ## Copyright (C) 2014-2017 CEA/DEN, EDF R&D
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.
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.
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
17 ## See http:##www.salome-platform.org/ or
18 ## email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
22 TestConstraintCollinear.py
23 Unit test of SketchPlugin_ConstraintCollinear class
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());
32 #=========================================================================
33 # Initialization of the test
34 #=========================================================================
36 from GeomDataAPI import *
37 from ModelAPI import *
40 from salome.shaper import model
42 __updated__ = "2017-03-06"
44 class TestConstraintCollinear(unittest.TestCase):
47 self.myDocument = model.moduleDocument()
48 self.mySketch = model.addSketch(self.myDocument, model.defaultPlane("XOY"))
49 self.myTolerance = 1.e-6
54 assert(model.checkPythonDump())
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))
62 def checkLineCollinearity(self, theLine1, theLine2):
63 aStartPoint1 = theLine1.startPoint()
64 aEndPoint1 = theLine1.endPoint()
65 aStartPoint2 = theLine2.startPoint()
66 aEndPoint2 = theLine2.endPoint()
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)
74 def moveLineAndCheckCollinearity(self, theLine1, theLine2):
77 theLine1.startPoint().setValue(theLine1.startPoint().x() + deltaX, theLine1.startPoint().y() + deltaY)
79 self.checkLineCollinearity(theLine1, theLine2)
80 self.assertEqual(model.dof(self.mySketch), self.myDOF)
82 theLine1.endPoint().setValue(theLine1.endPoint().x() - deltaX, theLine1.endPoint().y() - deltaY)
84 self.checkLineCollinearity(theLine1, theLine2)
85 self.assertEqual(model.dof(self.mySketch), self.myDOF)
87 def test_collinear_base(self):
88 """ Test 1. Collinearity two independent lines
90 aSketchLineA = self.mySketch.addLine(0., 25., 85., 25.)
91 aSketchLineB = self.mySketch.addLine(0., 50., 80., 75.)
93 self.assertEqual(model.dof(self.mySketch), self.myDOF)
94 self.mySketch.setCollinear(aSketchLineA, aSketchLineB)
97 self.assertEqual(model.dof(self.mySketch), self.myDOF)
99 self.checkLineCollinearity(aSketchLineA, aSketchLineB)
100 self.moveLineAndCheckCollinearity(aSketchLineA, aSketchLineB)
102 def test_collinear_connected_lines(self):
103 """ Test 2. Collinearity of two lines in polyline
105 aSketchLineA = self.mySketch.addLine(10., 20., 30., 40.)
106 aSketchLineB = self.mySketch.addLine(30., 40., 30., 70.)
108 self.assertEqual(model.dof(self.mySketch), self.myDOF)
109 self.mySketch.setCoincident(aSketchLineA.endPoint(), aSketchLineB.startPoint())
112 self.assertEqual(model.dof(self.mySketch), self.myDOF)
114 self.mySketch.setCollinear(aSketchLineA, aSketchLineB)
117 self.assertEqual(model.dof(self.mySketch), self.myDOF)
119 self.checkLineCollinearity(aSketchLineA, aSketchLineB)
120 self.moveLineAndCheckCollinearity(aSketchLineA, aSketchLineB)
122 def test_collinear_point_on_line(self):
123 """ Test 3. Collinearity for line which extremity is coincident with other line
125 aSketchLineA = self.mySketch.addLine(10., 20., 30., 40.)
126 aSketchLineB = self.mySketch.addLine(20., 40., 30., 70.)
128 self.assertEqual(model.dof(self.mySketch), self.myDOF)
129 self.mySketch.setCoincident(aSketchLineA.result(), aSketchLineB.startPoint())
132 self.assertEqual(model.dof(self.mySketch), self.myDOF)
134 self.mySketch.setCollinear(aSketchLineA, aSketchLineB)
137 self.assertEqual(model.dof(self.mySketch), self.myDOF)
139 self.checkLineCollinearity(aSketchLineA, aSketchLineB)
140 self.moveLineAndCheckCollinearity(aSketchLineA, aSketchLineB)
142 def test_already_collinear(self):
143 """ Test 4. Collinearity two lines connected by extremities to each other
145 aSketchLineA = self.mySketch.addLine(10., 20., 40., 50.)
146 aSketchLineB = self.mySketch.addLine(20., 40., 40., 70.)
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())
153 self.assertEqual(model.dof(self.mySketch), self.myDOF)
155 aCollinear = self.mySketch.setCollinear(aSketchLineA, aSketchLineB)
157 self.assertEqual(model.dof(self.mySketch), self.myDOF)
159 # check error message and remove non valid constraint
160 self.assertNotEqual(self.mySketch.solverError().value(), "")
161 self.myDocument.removeFeature(aCollinear.feature())
163 self.assertEqual(model.dof(self.mySketch), self.myDOF)
165 def test_already_collinear2(self):
166 """ Test 5. Collinearity of two lines when one line is fully on other
168 aSketchLineA = self.mySketch.addLine(10., 20., 30., 40.)
169 aSketchLineB = self.mySketch.addLine(20., 40., 30., 70.)
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())
176 self.assertEqual(model.dof(self.mySketch), self.myDOF)
178 aCollinear = self.mySketch.setCollinear(aSketchLineA, aSketchLineB)
180 self.assertEqual(model.dof(self.mySketch), self.myDOF)
182 # check error message and remove non valid constraint
183 self.assertNotEqual(self.mySketch.solverError().value(), "")
184 self.myDocument.removeFeature(aCollinear.feature())
186 self.assertEqual(model.dof(self.mySketch), self.myDOF)
188 if __name__ == '__main__':
189 unittest.main(exit=False)
190 #=========================================================================
192 #=========================================================================