]> SALOME platform Git repositories - modules/shaper.git/commitdiff
Salome HOME
[PythonAPI / sketcher] modified addLine and added setAngle method
authorRenaud NEDELEC <renaud.nedelec@opencascade.com>
Tue, 20 Oct 2015 14:08:08 +0000 (16:08 +0200)
committerRenaud NEDELEC <renaud.nedelec@opencascade.com>
Tue, 20 Oct 2015 14:08:08 +0000 (16:08 +0200)
src/PythonAPI/CMakeLists.txt
src/PythonAPI/Test/TestSketcherSetAngle.py [new file with mode: 0644]
src/PythonAPI/model/sketcher/line.py
src/PythonAPI/model/sketcher/sketch.py

index f1ab36274eb75ef74e55ab0ab93d56cf553c3c87..551a8506e4a6cf97ef51f7225578f94fcf9c27b2 100644 (file)
@@ -20,4 +20,5 @@ ADD_UNIT_TESTS(
   TestSketcherSetHorizontal.py
   TestSketcherSetVertical.py
   TestSketcherSetRadius.py
+  TestSketcherSetAngle.py
   ) 
diff --git a/src/PythonAPI/Test/TestSketcherSetAngle.py b/src/PythonAPI/Test/TestSketcherSetAngle.py
new file mode 100644 (file)
index 0000000..cbdd5fc
--- /dev/null
@@ -0,0 +1,34 @@
+import unittest
+import model
+import math
+import TestSketcher
+from TestSketcher import SketcherTestCase
+
+class SketcherSetAngle(SketcherTestCase):   
+    def runTest(self):
+        # Set the constraint
+        l1 = self.sketch.addLine(0, 0, 0, 1)
+        l2 = self.sketch.addLine(0, 0, 1, 1)
+        self.sketch.setAngle(l1.result(), l2.result(), 30.0)
+        # Commit the transaction
+        model.do()
+        # Check the result
+        dot_product = (l1.endPointData().x() - l1.startPointData().x()) * \
+                      (l2.endPointData().x() - l2.startPointData().x()) + \
+                      (l1.endPointData().y() - l1.startPointData().y()) * \
+                      (l2.endPointData().y() - l2.startPointData().y())
+        norm_1 = math.sqrt(
+            math.pow((l1.endPointData().x() - l1.startPointData().x()), 2) + 
+            math.pow((l1.endPointData().y() - l1.startPointData().y()), 2)
+            )
+        norm_2 = math.sqrt(
+            math.pow((l2.endPointData().x() - l2.startPointData().x()), 2) + 
+            math.pow((l2.endPointData().y() - l2.startPointData().y()), 2)
+            )
+        angle = math.acos(dot_product / (norm_1 * norm_2))
+        self.assertAlmostEqual(
+            angle * 180 / math.pi, 30.0, delta=TestSketcher.DELTA
+            )
+        
+if __name__ == "__main__":
+    unittest.main()
\ No newline at end of file
index dfe8a1c93cecd569627772ab391160d108794f3a..22c322f2c986b0520f85cf7f4e861835fd0fdc44 100644 (file)
@@ -15,7 +15,7 @@ class Line():
         elif len(args) == 2:
             self.__createByPoints(*args)
         elif len(args) == 1:
-            self.__createByName(sketch, *args)
+            self.__createByName(*args)
         else:
             raise Exception("cannot create the Line")
 
@@ -29,11 +29,9 @@ class Line():
         self._end_point.setValue(p2.x(), p2.y())
         self._feature.execute()
 
-    def __createByName(self, sketch, name):
+    def __createByName(self, name):
         self._feature.data().selection("External").selectSubShape("EDGE", name)
         self._feature.execute()
-        rigid = sketch.addFeature("SketchConstraintRigid")
-        rigid.refattr("ConstraintEntityA").setObject(self._feature.firstResult())
 
     def startPointData(self):
         return self._start_point
index 4b4ea723337537fc5a90bb9896783fc066d0cdeb..4a809f0b4c006f0e95597ee5369b01f3bacbd13e 100644 (file)
@@ -3,7 +3,7 @@ Author: Daniel Brunier-Coulin with contribution by Mikhail Ponikarov
 Copyright (C) 2014-20xx CEA/DEN, EDF R&D
 """
 
-from ModelAPI    import modelAPI_ResultConstruction, featureToCompositeFeature
+from ModelAPI import modelAPI_ResultConstruction, featureToCompositeFeature
 from GeomDataAPI import geomDataAPI_Point, geomDataAPI_Dir
 from GeomAlgoAPI import GeomAlgoAPI_SketchBuilder
 from model.sketcher.point import Point
@@ -18,7 +18,7 @@ def addSketch(doc, plane):
     A Sketch object is instanciated with a feature as input parameter
     it provides an interface for manipulation of the feature data.
     :return: interface on the feature
-    :rtype: Sketch object"""
+    :rtype: Sketch"""
     feature = featureToCompositeFeature(doc.addFeature("Sketch"))
     return Sketch(feature, plane)
 
@@ -41,21 +41,23 @@ class Sketch():
             self.__sketchOnPlane(plane)
 
     def __sketchOnPlane(self, plane):
-        o  = plane.location()
-        d  = plane.direction()
-        dx = plane.xDirection()
+        """Create the sketch on a plane."""
+        origin = plane.location()
+        normal = plane.direction()
+        x_direction = plane.xDirection()
         geomDataAPI_Point( 
             self._feature.data().attribute("Origin") 
-            ).setValue( o.x(), o.y(), o.z() )
+            ).setValue(origin.x(), origin.y(), origin.z())
         geomDataAPI_Dir( 
             self._feature.data().attribute("DirX") 
-            ).setValue( dx.x(), dx.y(), dx.z() )
+            ).setValue(x_direction.x(), x_direction.y(), x_direction.z())
         geomDataAPI_Dir( 
             self._feature.data().attribute("Norm") 
-            ).setValue( d.x(),  d.y(),  d.z()  )
+            ).setValue(normal.x(), normal.y(), normal.z() )
 
-    def __sketchOnFace(self, plane):
-        self._feature.data().selection("External").selectSubShape("FACE", plane)
+    def __sketchOnFace(self, name):
+        """Initialize the sketch on a face given by its name."""
+        self._feature.data().selection("External").selectSubShape("FACE", name)
 
     #-------------------------------------------------------------
     #
@@ -71,7 +73,15 @@ class Sketch():
     def addLine(self, *args):
         """Add a line to this Sketch."""
         line_feature = self._feature.addFeature("SketchLine")
-        return Line(line_feature, *args)
+        line_interface = Line(line_feature, *args)
+        # if the line is created by name add a rigid constraint
+        # to the created line
+        if len(args) == 1 and isinstance(args[0], str):
+            constraint = sketch.addFeature("SketchConstraintRigid")
+            constraint.refattr("ConstraintEntityA").setObject(
+                line_feature.firstResult()
+                )
+        return line_interface
     
     def addCircle(self, *args):
         """Add a circle to this Sketch."""
@@ -149,6 +159,16 @@ class Sketch():
         constraint.data().real("ConstraintValue").setValue(length)
         self._feature.execute()
         return constraint
+    
+    def setAngle(self, line_1, line_2, angle):
+        """Set the angle between the given 2 lines and add the corresponding 
+        constraint to the sketch."""
+        constraint = self._feature.addFeature("SketchConstraintAngle")
+        constraint.data().refattr("ConstraintEntityA").setObject(line_1)
+        constraint.data().refattr("ConstraintEntityB").setObject(line_2)
+        constraint.data().real("ConstraintValue").setValue(angle)
+        self._feature.execute()
+        return constraint
 
     def setRadius(self, circle, radius):
         """Set the radius of the given circle and add the corresponding