1 # Copyright (C) 2014-2021 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 email : webmaster.salome@opencascade.com
21 TestConstraintCollinear.py
22 Unit test of SketchPlugin_ConstraintCollinear class
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());
31 #=========================================================================
32 # Initialization of the test
33 #=========================================================================
35 from GeomDataAPI import *
36 from ModelAPI import *
39 from salome.shaper import model
41 __updated__ = "2017-03-06"
43 class TestConstraintCollinear(unittest.TestCase):
46 self.myDocument = model.moduleDocument()
47 self.mySketch = model.addSketch(self.myDocument, model.defaultPlane("XOY"))
48 self.myTolerance = 1.e-6
53 assert(model.checkPythonDump())
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))
61 def checkLineCollinearity(self, theLine1, theLine2):
62 aStartPoint1 = theLine1.startPoint()
63 aEndPoint1 = theLine1.endPoint()
64 aStartPoint2 = theLine2.startPoint()
65 aEndPoint2 = theLine2.endPoint()
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)
73 def moveLineAndCheckCollinearity(self, theLine1, theLine2):
76 theLine1.startPoint().setValue(theLine1.startPoint().x() + deltaX, theLine1.startPoint().y() + deltaY)
78 self.checkLineCollinearity(theLine1, theLine2)
79 self.assertEqual(model.dof(self.mySketch), self.myDOF)
81 theLine1.endPoint().setValue(theLine1.endPoint().x() - deltaX, theLine1.endPoint().y() - deltaY)
83 self.checkLineCollinearity(theLine1, theLine2)
84 self.assertEqual(model.dof(self.mySketch), self.myDOF)
86 def test_collinear_base(self):
87 """ Test 1. Collinearity two independent lines
89 aSketchLineA = self.mySketch.addLine(0., 25., 85., 25.)
90 aSketchLineB = self.mySketch.addLine(0., 50., 80., 75.)
92 self.assertEqual(model.dof(self.mySketch), self.myDOF)
93 self.mySketch.setCollinear(aSketchLineA, aSketchLineB)
96 self.assertEqual(model.dof(self.mySketch), self.myDOF)
98 self.checkLineCollinearity(aSketchLineA, aSketchLineB)
99 self.moveLineAndCheckCollinearity(aSketchLineA, aSketchLineB)
101 def test_collinear_connected_lines(self):
102 """ Test 2. Collinearity of two lines in polyline
104 aSketchLineA = self.mySketch.addLine(10., 20., 30., 40.)
105 aSketchLineB = self.mySketch.addLine(30., 40., 30., 70.)
107 self.assertEqual(model.dof(self.mySketch), self.myDOF)
108 self.mySketch.setCoincident(aSketchLineA.endPoint(), aSketchLineB.startPoint())
111 self.assertEqual(model.dof(self.mySketch), self.myDOF)
113 self.mySketch.setCollinear(aSketchLineA, aSketchLineB)
116 self.assertEqual(model.dof(self.mySketch), self.myDOF)
118 self.checkLineCollinearity(aSketchLineA, aSketchLineB)
119 self.moveLineAndCheckCollinearity(aSketchLineA, aSketchLineB)
121 def test_collinear_point_on_line(self):
122 """ Test 3. Collinearity for line which extremity is coincident with other line
124 aSketchLineA = self.mySketch.addLine(10., 20., 30., 40.)
125 aSketchLineB = self.mySketch.addLine(20., 40., 30., 70.)
127 self.assertEqual(model.dof(self.mySketch), self.myDOF)
128 self.mySketch.setCoincident(aSketchLineA.result(), aSketchLineB.startPoint())
131 self.assertEqual(model.dof(self.mySketch), self.myDOF)
133 self.mySketch.setCollinear(aSketchLineA, aSketchLineB)
136 self.assertEqual(model.dof(self.mySketch), self.myDOF)
138 self.checkLineCollinearity(aSketchLineA, aSketchLineB)
139 self.moveLineAndCheckCollinearity(aSketchLineA, aSketchLineB)
141 def test_already_collinear(self):
142 """ Test 4. Collinearity two lines connected by extremities to each other
144 aSketchLineA = self.mySketch.addLine(10., 20., 40., 50.)
145 aSketchLineB = self.mySketch.addLine(20., 40., 40., 70.)
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())
152 self.assertEqual(model.dof(self.mySketch), self.myDOF)
154 aCollinear = self.mySketch.setCollinear(aSketchLineA, aSketchLineB)
156 self.assertEqual(model.dof(self.mySketch), self.myDOF)
158 # check error message and remove non valid constraint
159 self.assertNotEqual(self.mySketch.solverError().value(), "")
160 self.myDocument.removeFeature(aCollinear.feature())
162 self.assertEqual(model.dof(self.mySketch), self.myDOF)
164 def test_already_collinear2(self):
165 """ Test 5. Collinearity of two lines when one line is fully on other
167 aSketchLineA = self.mySketch.addLine(10., 20., 30., 40.)
168 aSketchLineB = self.mySketch.addLine(20., 40., 30., 70.)
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())
175 self.assertEqual(model.dof(self.mySketch), self.myDOF)
177 aCollinear = self.mySketch.setCollinear(aSketchLineA, aSketchLineB)
179 self.assertEqual(model.dof(self.mySketch), self.myDOF)
181 # check error message and remove non valid constraint
182 self.assertNotEqual(self.mySketch.solverError().value(), "")
183 self.myDocument.removeFeature(aCollinear.feature())
185 self.assertEqual(model.dof(self.mySketch), self.myDOF)
187 if __name__ == "__main__":
188 test_program = unittest.main(exit=False)
189 assert test_program.result.wasSuccessful(), "Test failed"
190 #=========================================================================
192 #=========================================================================