Salome HOME
[PythonAPI] small fix
[modules/shaper.git] / src / PythonAPI / model / sketcher / sketch.py
index 58b582d4d694656ba1d8e8f6abad49aec7ff75e2..9e92a8e26e234ee1c22068b6b7e2940bdb485957 100644 (file)
@@ -1,5 +1,6 @@
 """Sketch Feature Interface
 Author: Daniel Brunier-Coulin with contribution by Mikhail Ponikarov
+        finalized by Renaud Nedelec and Sergey Pokhodenko
 Copyright (C) 2014-20xx CEA/DEN, EDF R&D
 """
 
@@ -27,7 +28,7 @@ def addSketch(doc, plane):
     return Sketch(feature, plane)
 
 class Sketch(Interface):
-    """Interface on a Sketch feature."""
+    """Interface class for Sketch feature."""
     def __init__(self, feature, *args):
         """Initialize a 2D Sketch on the given plane.
 
@@ -49,25 +50,14 @@ class Sketch(Interface):
             )
         self._external = self._feature.data().selection("External")
 
-        assert(self._origin)
-        assert(self._dir_x)
-        assert(self._norm)
-        assert(self._external)
-
-        # Entities used for building the result shape
-        self._selection = None
-
-        if not args:
-            return
-
-        plane = args[0]
-
-        #   self.resultype ="Face" # Type of Sketch result
-        if isinstance(plane, str):
-            self.__sketchOnFace(plane)
-        else:
-            self.__sketchOnPlane(plane)
-        pass
+        # If no arguments are given the attributes of the feature 
+        # are not Initialized
+        if args is not None:
+            plane = args[0]
+            if isinstance(plane, str):
+                self.__sketchOnFace(plane)
+            else:
+                self.__sketchOnPlane(plane)
 
     def __sketchOnPlane(self, plane):
         """Create the sketch on a plane."""
@@ -90,11 +80,15 @@ class Sketch(Interface):
 
     def addPoint(self, *args):
         """Add a point to this Sketch."""
+        if not args:
+            raise TypeError("No arguments given")
         point_feature = self._feature.addFeature("SketchPoint")
         return Point(point_feature, *args)
 
     def addLine(self, *args):
         """Add a line to this Sketch."""
+        if not args:
+            raise TypeError("No arguments given")
         line_feature = self._feature.addFeature("SketchLine")
         line_interface = Line(line_feature, *args)
         # if the line is created by name add a rigid constraint
@@ -108,11 +102,15 @@ class Sketch(Interface):
 
     def addCircle(self, *args):
         """Add a circle to this Sketch."""
+        if not args:
+            raise TypeError("No arguments given")
         circle_feature = self._feature.addFeature("SketchCircle")
         return Circle(circle_feature, *args)
 
     def addArc(self, *args):
         """Add an arc to this Sketch."""
+        if not args:
+            raise TypeError("No arguments given")
         arc_feature = self._feature.addFeature("SketchArc")
         return Arc(arc_feature, *args)
 
@@ -125,73 +123,85 @@ class Sketch(Interface):
     def setCoincident(self, p1, p2):
         """Set coincident the two given points and add the corresponding
         constraint to this Sketch."""
-        assert(p1 and p2)
+        # assert(p1 and p2) NOTE : if an argument is missing python
+        # will raise TypeError by itself.
+        # It seems better to check only that provided arguments are not 
+        # None
+        if p1 is None or p2 is None:
+            raise TypeError("NoneType argument given")
         constraint = self._feature.addFeature("SketchConstraintCoincidence")
         constraint.data().refattr("ConstraintEntityA").setAttr(p1)
         constraint.data().refattr("ConstraintEntityB").setAttr(p2)
-        self._execute()
+        self.execute()
         return constraint
 
     def setParallel(self, l1, l2):
         """Set parallel the two given lines and add the corresponding
         constraint to this Sketch."""
-        assert(l1 and l2)
+        if l1 is None or l2 is None:
+            raise TypeError("NoneType argument given")
         constraint = self._feature.addFeature("SketchConstraintParallel")
         constraint.data().refattr("ConstraintEntityA").setObject(l1)
         constraint.data().refattr("ConstraintEntityB").setObject(l2)
-        self._execute()
+        self.execute()
         return constraint
 
     def setPerpendicular(self, l1, l2):
         """Set perpendicular the two given lines and add the corresponding
         constraint to this Sketch."""
-        assert(l1 and l2)
+        if l1 is None or l2 is None:
+            raise TypeError("NoneType argument given")
         constraint = self._feature.addFeature("SketchConstraintPerpendicular")
         constraint.data().refattr("ConstraintEntityA").setObject(l1)
         constraint.data().refattr("ConstraintEntityB").setObject(l2)
-        self._execute()
+        self.execute()
         return constraint
 
     def setHorizontal(self, line):
         """Set horizontal the given line and add the corresponding
         constraint to this Sketch."""
+        if line is None:
+            raise TypeError("NoneType argument given")
         constraint = self._feature.addFeature("SketchConstraintHorizontal")
         constraint.data().refattr("ConstraintEntityA").setObject(line)
-        self._execute()
+        self.execute()
         return constraint
 
     def setVertical(self, line):
         """Set vertical the given line and add the corresponding
         constraint to this Sketch."""
+        if line is None:
+            raise TypeError("NoneType argument given")
         constraint = self._feature.addFeature("SketchConstraintVertical")
         constraint.data().refattr("ConstraintEntityA").setObject(line)
-        self._execute()
+        self.execute()
         return constraint
 
     def setDistance(self, point, line, length):
         """Set the distance between the given point and line, and add
         the corresponding constraint to this Sketch."""
-        assert(point and line)
+        if point is None or line is None:
+            raise TypeError("NoneType argument given")
         constraint = self._feature.addFeature("SketchConstraintDistance")
         if isinstance(line, basestring):
             # Add the edge identified by the given topological name
             # to this Sketch
             line = self.addLine(line).result()
-            assert(line)
         constraint.data().refattr("ConstraintEntityA").setAttr(point)
         constraint.data().refattr("ConstraintEntityB").setObject(line)
         constraint.data().real("ConstraintValue").setValue(length)
-        self._execute()
+        self.execute()
         return constraint
 
     def setLength(self, line, length):
         """Set the length of the given line and add the corresponding
         constraint to this Sketch."""
-        assert(line)
+        if line is None:
+            raise TypeError("NoneType argument given")
         constraint = self._feature.addFeature("SketchConstraintLength")
         constraint.data().refattr("ConstraintEntityA").setObject(line)
         constraint.data().real("ConstraintValue").setValue(length)
-        self._execute()
+        self.execute()
         return constraint
 
     def setRadius(self, circle, radius):
@@ -200,7 +210,7 @@ class Sketch(Interface):
         constraint = self._feature.addFeature("SketchConstraintRadius")
         constraint.data().refattr("ConstraintEntityA").setObject(circle)
         constraint.data().real("ConstraintValue").setValue(radius)
-        self._execute()
+        self.execute()
         return constraint
 
     def setEqual(self, object_1, object_2):
@@ -210,7 +220,7 @@ class Sketch(Interface):
         constraint = self._feature.addFeature("SketchConstraintEqual")
         constraint.data().refattr("ConstraintEntityA").setObject(object_1)
         constraint.data().refattr("ConstraintEntityB").setObject(object_2)
-        self._execute()
+        self.execute()
         return constraint
 
     def setAngle(self, line_1, line_2, angle):
@@ -220,7 +230,7 @@ class Sketch(Interface):
         constraint.data().refattr("ConstraintEntityA").setObject(line_1)
         constraint.data().refattr("ConstraintEntityB").setObject(line_2)
         constraint.data().real("ConstraintValue").setValue(angle)
-        self._execute()
+        self.execute()
         return constraint
 
     def setTangent(self, object_1, object_2):
@@ -229,17 +239,24 @@ class Sketch(Interface):
         constraint = self._feature.addFeature("SketchConstraintTangent")
         constraint.data().refattr("ConstraintEntityA").setObject(object_1)
         constraint.data().refattr("ConstraintEntityB").setObject(object_2)
-        self._execute()
+        self.execute()
         return constraint
 
     def setFillet(self, line_1, line_2, radius):
-        """Set a fillet constraint between the 3 given lines with the given
+        """Set a fillet constraint between the 2 given lines with the given
         filleting radius."""
         constraint = self._feature.addFeature("SketchConstraintFillet")
         constraint.data().refattr("ConstraintEntityA").setObject(line_1)
-        constraint.data().refattr("ConstraintEntityB").setObject(line_2)
-        constraint.data().real("ConstraintValue").setValue(radius)
-        self._execute()
+        constraint.data().reflist("ConstraintEntityB").clear()
+        constraint.data().reflist("ConstraintEntityB").append(line_2)
+        self.execute()
+        return constraint
+    
+    def setRigid(self, object_):
+        """Set a rigid constraint on a given object."""
+        constraint = self._feature.addFeature("SketchConstraintRigid")
+        constraint.data().refattr("ConstraintEntityA").setObject(object_)
+        self.execute()
         return constraint
 
     #-------------------------------------------------------------
@@ -310,17 +327,17 @@ class Sketch(Interface):
         geometry of this Sketch.
         """
         if len(args) == 0:
-            self._selection = modelAPI_ResultConstruction(
+            wire = modelAPI_ResultConstruction(
                 self._feature.firstResult()
                 ).shape()
         elif len(args) == 1:
-            self._selection = args[0].shape()
+            wire = args[0].shape()
         else:
             raise Exception("not yet implemented")
         # TODO: simple version now, should be a list of selected faces
-        return [Selection(self.result(), self.buildShape())]
+        return [Selection(self.result(), self.buildShape(wire))]
 
-    def buildShape(self):
+    def buildShape(self, wire):
         """Build the result Shape of this Sketch according to the
         selected geometrical entities."""
         o = self._origin.pnt()
@@ -329,7 +346,7 @@ class Sketch(Interface):
 
         # The faces are kept otherwise they are destroyed at exit
         faces = ShapeList()
-        GeomAlgoAPI_SketchBuilder.createFaces(o, dx, n, self._selection, faces)
+        GeomAlgoAPI_SketchBuilder.createFaces(o, dx, n, wire, faces)
         # TODO: Deal with several faces
         return faces[0]