Salome HOME
Issue #2431 - remove application exit from unit tests
[modules/shaper.git] / src / SketchPlugin / Test / TestConstraintCollinear.py
index cb7e090ecec8447803b1c69e1aec14b63b2400b8..6f3d2dac5d4ccb72d9d8f3856efdd1dd81787a5f 100644 (file)
+## Copyright (C) 2014-2017  CEA/DEN, EDF R&D
+##
+## This library is free software; you can redistribute it and/or
+## modify it under the terms of the GNU Lesser General Public
+## License as published by the Free Software Foundation; either
+## version 2.1 of the License, or (at your option) any later version.
+##
+## This library is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+## Lesser General Public License for more details.
+##
+## You should have received a copy of the GNU Lesser General Public
+## License along with this library; if not, write to the Free Software
+## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+##
+## See http:##www.salome-platform.org/ or
+## email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
+##
+
 """
     TestConstraintCollinear.py
     Unit test of SketchPlugin_ConstraintCollinear class
-        
+
     SketchPlugin_ConstraintCollinear
         static const std::string MY_CONSTRAINT_COLLINEAR_ID("SketchConstraintCollinear");
         data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
         data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
 
 """
-from GeomDataAPI import *
-from ModelAPI import *
-import math
+
 #=========================================================================
 # Initialization of the test
 #=========================================================================
 
-__updated__ = "2016-01-28"
+from GeomDataAPI import *
+from ModelAPI import *
+import math
+import unittest
+from salome.shaper import model
+
+__updated__ = "2017-03-06"
+
+class TestConstraintCollinear(unittest.TestCase):
+  def setUp(self):
+    model.begin()
+    self.myDocument = model.moduleDocument()
+    self.mySketch = model.addSketch(self.myDocument, model.defaultPlane("XOY"))
+    self.myTolerance = 1.e-6
+    self.myDOF = 0
 
+  def tearDown(self):
+    model.end()
+    assert(model.checkPythonDump())
 
-def checkCollinearVec(theX1, theY1, theX2, theY2):
-    TOL = 2.e-6
+  def checkVectorCollinearity(self, theX1, theY1, theX2, theY2):
     aLen1 = math.hypot(theX1, theY1)
     aLen2 = math.hypot(theX2, theY2)
     aDot = theX1 * theX2 + theY1 * theY2
-    assert (math.fabs(math.fabs(aDot) - aLen1 * aLen2) < TOL**2)
-
-def checkCollinear(theLine1, theLine2):
-    aStartPoint1 = geomDataAPI_Point2D(theLine1.attribute("StartPoint"))
-    aEndPoint1   = geomDataAPI_Point2D(theLine1.attribute("EndPoint"))
-    aStartPoint2 = geomDataAPI_Point2D(theLine2.attribute("StartPoint"))
-    aEndPoint2   = geomDataAPI_Point2D(theLine2.attribute("EndPoint"))
-    
+    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))
+
+  def checkLineCollinearity(self, theLine1, theLine2):
+    aStartPoint1 = theLine1.startPoint()
+    aEndPoint1   = theLine1.endPoint()
+    aStartPoint2 = theLine2.startPoint()
+    aEndPoint2   = theLine2.endPoint()
+
     aDir1x, aDir1y = aEndPoint1.x() - aStartPoint1.x(), aEndPoint1.y() - aStartPoint1.y()
     aDir2x, aDir2y = aEndPoint2.x() - aStartPoint1.x(), aEndPoint2.y() - aStartPoint1.y()
     aDir3x, aDir3y = aStartPoint2.x() - aStartPoint1.x(), aStartPoint2.y() - aStartPoint1.y()
-    checkCollinearVec(aDir1x, aDir1y, aDir2x, aDir2y)
-    checkCollinearVec(aDir1x, aDir1y, aDir3x, aDir3y)
+    self.checkVectorCollinearity(aDir1x, aDir1y, aDir2x, aDir2y)
+    self.checkVectorCollinearity(aDir1x, aDir1y, aDir3x, aDir3y)
 
+  def moveLineAndCheckCollinearity(self, theLine1, theLine2):
+    deltaX = deltaY = 10.
 
+    theLine1.startPoint().setValue(theLine1.startPoint().x() + deltaX, theLine1.startPoint().y() + deltaY)
+    model.do()
+    self.checkLineCollinearity(theLine1, theLine2)
+    self.assertEqual(model.dof(self.mySketch), self.myDOF)
 
-aSession = ModelAPI_Session.get()
-aDocument = aSession.moduleDocument()
-#=========================================================================
-# Creation of a sketch
-#=========================================================================
-aSession.startOperation()
-aSketchCommonFeature = aDocument.addFeature("Sketch")
-aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
-origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
-origin.setValue(0, 0, 0)
-dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
-dirx.setValue(1, 0, 0)
-norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
-norm.setValue(0, 0, 1)
-aSession.finishOperation()
-#=========================================================================
-# Create two lines
-#=========================================================================
-aSession.startOperation()
-# line A
-aSketchLineA = aSketchFeature.addFeature("SketchLine")
-aLineAStartPoint = geomDataAPI_Point2D(aSketchLineA.attribute("StartPoint"))
-aLineAEndPoint = geomDataAPI_Point2D(aSketchLineA.attribute("EndPoint"))
-aSketchLineB = aSketchFeature.addFeature("SketchLine")
-aLineAStartPoint.setValue(0., 25)
-aLineAEndPoint.setValue(85., 25)
-# line B
-aLineBStartPoint = geomDataAPI_Point2D(aSketchLineB.attribute("StartPoint"))
-aLineBEndPoint = geomDataAPI_Point2D(aSketchLineB.attribute("EndPoint"))
-aLineBStartPoint.setValue(0., 50)
-aLineBEndPoint.setValue(80., 75)
-aSession.finishOperation()
-#=========================================================================
-# Link lines with collinear constraint
-#=========================================================================
-aSession.startOperation()
-aParallelConstraint = aSketchFeature.addFeature("SketchConstraintCollinear")
-refattrA = aParallelConstraint.refattr("ConstraintEntityA")
-refattrB = aParallelConstraint.refattr("ConstraintEntityB")
-refattrA.setObject(aSketchLineA.firstResult())
-refattrB.setObject(aSketchLineB.firstResult())
-aParallelConstraint.execute()
-aSession.finishOperation()
-checkCollinear(aSketchLineA, aSketchLineB)
-#=========================================================================
-# Check values and move one constrainted object
-#=========================================================================
-deltaX = deltaY = 10.
-# rotate line, check that reference's line points are moved also
-aLineBStartPointPrev = (aLineBStartPoint.x(), aLineBStartPoint.y())
-aLineBEndPointPrev = (aLineBEndPoint.x(), aLineBEndPoint.y())
-aSession.startOperation()
-aLineAStartPoint.setValue(aLineAStartPoint.x() + deltaX,
-                          aLineAStartPoint.y() + deltaY)
-aLineAEndPoint.setValue(aLineAEndPoint.x() - deltaX,
-                        aLineAEndPoint.y() - deltaY)
-aSession.finishOperation()
-checkCollinear(aSketchLineA, aSketchLineB)
+    theLine1.endPoint().setValue(theLine1.endPoint().x() - deltaX, theLine1.endPoint().y() - deltaY)
+    model.do()
+    self.checkLineCollinearity(theLine1, theLine2)
+    self.assertEqual(model.dof(self.mySketch), self.myDOF)
+
+  def test_collinear_base(self):
+    """ Test 1. Collinearity two independent lines
+    """
+    aSketchLineA = self.mySketch.addLine(0., 25., 85., 25.)
+    aSketchLineB = self.mySketch.addLine(0., 50., 80., 75.)
+    self.myDOF += 8
+    self.assertEqual(model.dof(self.mySketch), self.myDOF)
+    self.mySketch.setCollinear(aSketchLineA, aSketchLineB)
+    self.myDOF -= 2
+    model.do()
+    self.assertEqual(model.dof(self.mySketch), self.myDOF)
+
+    self.checkLineCollinearity(aSketchLineA, aSketchLineB)
+    self.moveLineAndCheckCollinearity(aSketchLineA, aSketchLineB)
+
+  def test_collinear_connected_lines(self):
+    """ Test 2. Collinearity of two lines in polyline
+    """
+    aSketchLineA = self.mySketch.addLine(10., 20., 30., 40.)
+    aSketchLineB = self.mySketch.addLine(30., 40., 30., 70.)
+    self.myDOF += 8
+    self.assertEqual(model.dof(self.mySketch), self.myDOF)
+    self.mySketch.setCoincident(aSketchLineA.endPoint(), aSketchLineB.startPoint())
+    self.myDOF -= 2
+    model.do()
+    self.assertEqual(model.dof(self.mySketch), self.myDOF)
+
+    self.mySketch.setCollinear(aSketchLineA, aSketchLineB)
+    self.myDOF -= 1
+    model.do()
+    self.assertEqual(model.dof(self.mySketch), self.myDOF)
+
+    self.checkLineCollinearity(aSketchLineA, aSketchLineB)
+    self.moveLineAndCheckCollinearity(aSketchLineA, aSketchLineB)
+
+  def test_collinear_point_on_line(self):
+    """ Test 3. Collinearity for line which extremity is coincident with other line
+    """
+    aSketchLineA = self.mySketch.addLine(10., 20., 30., 40.)
+    aSketchLineB = self.mySketch.addLine(20., 40., 30., 70.)
+    self.myDOF += 8
+    self.assertEqual(model.dof(self.mySketch), self.myDOF)
+    self.mySketch.setCoincident(aSketchLineA.result(), aSketchLineB.startPoint())
+    self.myDOF -= 1
+    model.do()
+    self.assertEqual(model.dof(self.mySketch), self.myDOF)
+
+    self.mySketch.setCollinear(aSketchLineA, aSketchLineB)
+    self.myDOF -= 1
+    model.do()
+    self.assertEqual(model.dof(self.mySketch), self.myDOF)
+
+    self.checkLineCollinearity(aSketchLineA, aSketchLineB)
+    self.moveLineAndCheckCollinearity(aSketchLineA, aSketchLineB)
+
+  def test_already_collinear(self):
+    """ Test 4. Collinearity two lines connected by extremities to each other
+    """
+    aSketchLineA = self.mySketch.addLine(10., 20., 40., 50.)
+    aSketchLineB = self.mySketch.addLine(20., 40., 40., 70.)
+    self.myDOF += 8
+    self.assertEqual(model.dof(self.mySketch), self.myDOF)
+    self.mySketch.setCoincident(aSketchLineA.result(), aSketchLineB.startPoint())
+    self.mySketch.setCoincident(aSketchLineA.endPoint(), aSketchLineB.result())
+    self.myDOF -= 2
+    model.do()
+    self.assertEqual(model.dof(self.mySketch), self.myDOF)
+
+    aCollinear = self.mySketch.setCollinear(aSketchLineA, aSketchLineB)
+    model.do()
+    self.assertEqual(model.dof(self.mySketch), self.myDOF)
+
+    # check error message and remove non valid constraint
+    self.assertNotEqual(self.mySketch.solverError().value(), "")
+    self.myDocument.removeFeature(aCollinear.feature())
+    model.do()
+    self.assertEqual(model.dof(self.mySketch), self.myDOF)
+
+  def test_already_collinear2(self):
+    """ Test 5. Collinearity of two lines when one line is fully on other
+    """
+    aSketchLineA = self.mySketch.addLine(10., 20., 30., 40.)
+    aSketchLineB = self.mySketch.addLine(20., 40., 30., 70.)
+    self.myDOF += 8
+    self.assertEqual(model.dof(self.mySketch), self.myDOF)
+    self.mySketch.setCoincident(aSketchLineA.result(), aSketchLineB.startPoint())
+    self.mySketch.setCoincident(aSketchLineA.result(), aSketchLineB.endPoint())
+    self.myDOF -= 2
+    model.do()
+    self.assertEqual(model.dof(self.mySketch), self.myDOF)
+
+    aCollinear = self.mySketch.setCollinear(aSketchLineA, aSketchLineB)
+    model.do()
+    self.assertEqual(model.dof(self.mySketch), self.myDOF)
+
+    # check error message and remove non valid constraint
+    self.assertNotEqual(self.mySketch.solverError().value(), "")
+    self.myDocument.removeFeature(aCollinear.feature())
+    model.do()
+    self.assertEqual(model.dof(self.mySketch), self.myDOF)
+
+if __name__ == '__main__':
+  unittest.main(exit=False)
 #=========================================================================
 # End of test
 #=========================================================================
-
-from salome.shaper import model
-assert(model.checkPythonDump())