Salome HOME
Merge branch 'python_parametric_api' of https://git.salome-platform.org/git/modules...
[modules/shaper.git] / src / PythonAPI / model / sketcher / sketch.py
index d4df571cbb2db09bb3f25c9911de5c45981a655c..0090b269caa4e6edb045c2332ca66ff06041b777 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,8 +28,8 @@ def addSketch(doc, plane):
     return Sketch(feature, plane)
 
 class Sketch(Interface):
-    """Interface on a Sketch feature."""
-    def __init__(self, feature, plane):
+    """Interface class for Sketch feature."""
+    def __init__(self, feature, *args):
         """Initialize a 2D Sketch on the given plane.
 
         The plane can be defined either by:
@@ -38,32 +39,43 @@ class Sketch(Interface):
         Interface.__init__(self, feature)
         assert(self._feature.getKind() == "Sketch")
 
-        # Entities used for building the result shape
-        self._selection = None
-        #   self.resultype ="Face" # Type of Sketch result
-        if isinstance(plane, str):
-            self.__sketchOnFace(plane)
-        else:
-            self.__sketchOnPlane(plane)
+        self._origin = geomDataAPI_Point(
+            self._feature.data().attribute("Origin")
+            )
+        self._dir_x = geomDataAPI_Dir(
+            self._feature.data().attribute("DirX")
+            )
+        self._norm = geomDataAPI_Dir(
+            self._feature.data().attribute("Norm")
+            )
+        self._external = self._feature.data().selection("External")
+
+        assert(self._origin)
+        assert(self._dir_x)
+        assert(self._norm)
+        assert(self._external)
+
+        # 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."""
         origin = plane.location()
         normal = plane.direction()
         x_direction = plane.xDirection()
-        geomDataAPI_Point( 
-            self._feature.data().attribute("Origin") 
-            ).setValue(origin.x(), origin.y(), origin.z())
-        geomDataAPI_Dir( 
-            self._feature.data().attribute("DirX") 
-            ).setValue(x_direction.x(), x_direction.y(), x_direction.z())
-        geomDataAPI_Dir( 
-            self._feature.data().attribute("Norm") 
-            ).setValue(normal.x(), normal.y(), normal.z() )
+        self._origin.setValue(origin.x(), origin.y(), origin.z())
+        self._norm.setValue(normal.x(), normal.y(), normal.z())
+        self._dir_x.setValue(x_direction.x(), x_direction.y(), x_direction.z())
 
     def __sketchOnFace(self, name):
         """Initialize the sketch on a face given by its name."""
-        self._feature.data().selection("External").selectSubShape("FACE", name)
+        self._external.selectSubShape("FACE", name)
 
     #-------------------------------------------------------------
     #
@@ -108,25 +120,31 @@ 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)
         constraint = self._feature.addFeature("SketchConstraintCoincidence")
         constraint.data().refattr("ConstraintEntityA").setAttr(p1)
         constraint.data().refattr("ConstraintEntityB").setAttr(p2)
+        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)
         constraint = self._feature.addFeature("SketchConstraintParallel")
         constraint.data().refattr("ConstraintEntityA").setObject(l1)
         constraint.data().refattr("ConstraintEntityB").setObject(l2)
+        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)
         constraint = self._feature.addFeature("SketchConstraintPerpendicular")
         constraint.data().refattr("ConstraintEntityA").setObject(l1)
         constraint.data().refattr("ConstraintEntityB").setObject(l2)
+        self.execute()
         return constraint
 
     def setHorizontal(self, line):
@@ -134,6 +152,7 @@ class Sketch(Interface):
         constraint to this Sketch."""
         constraint = self._feature.addFeature("SketchConstraintHorizontal")
         constraint.data().refattr("ConstraintEntityA").setObject(line)
+        self.execute()
         return constraint
 
     def setVertical(self, line):
@@ -141,29 +160,33 @@ class Sketch(Interface):
         constraint to this Sketch."""
         constraint = self._feature.addFeature("SketchConstraintVertical")
         constraint.data().refattr("ConstraintEntityA").setObject(line)
+        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)
         constraint = self._feature.addFeature("SketchConstraintDistance")
-        if isinstance(line, str):
+        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._feature.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)
         constraint = self._feature.addFeature("SketchConstraintLength")
         constraint.data().refattr("ConstraintEntityA").setObject(line)
         constraint.data().real("ConstraintValue").setValue(length)
-        self._feature.execute()
+        self.execute()
         return constraint
 
     def setRadius(self, circle, radius):
@@ -172,6 +195,7 @@ class Sketch(Interface):
         constraint = self._feature.addFeature("SketchConstraintRadius")
         constraint.data().refattr("ConstraintEntityA").setObject(circle)
         constraint.data().real("ConstraintValue").setValue(radius)
+        self.execute()
         return constraint
 
     def setEqual(self, object_1, object_2):
@@ -181,7 +205,7 @@ class Sketch(Interface):
         constraint = self._feature.addFeature("SketchConstraintEqual")
         constraint.data().refattr("ConstraintEntityA").setObject(object_1)
         constraint.data().refattr("ConstraintEntityB").setObject(object_2)
-        self._feature.execute()
+        self.execute()
         return constraint
 
     def setAngle(self, line_1, line_2, angle):
@@ -191,7 +215,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._feature.execute()
+        self.execute()
         return constraint
 
     def setTangent(self, object_1, object_2):
@@ -200,7 +224,7 @@ class Sketch(Interface):
         constraint = self._feature.addFeature("SketchConstraintTangent")
         constraint.data().refattr("ConstraintEntityA").setObject(object_1)
         constraint.data().refattr("ConstraintEntityB").setObject(object_2)
-        self._feature.execute()
+        self.execute()
         return constraint
 
     def setFillet(self, line_1, line_2, radius):
@@ -210,7 +234,7 @@ class Sketch(Interface):
         constraint.data().refattr("ConstraintEntityA").setObject(line_1)
         constraint.data().refattr("ConstraintEntityB").setObject(line_2)
         constraint.data().real("ConstraintValue").setValue(radius)
-        self._feature.execute()
+        self.execute()
         return constraint
 
     #-------------------------------------------------------------
@@ -281,28 +305,27 @@ 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 self
+        return [Selection(self.result(), self.buildShape(wire))]
 
-    def buildShape(self):
-        """Build the result Shape of this Sketch according to the 
+    def buildShape(self, wire):
+        """Build the result Shape of this Sketch according to the
         selected geometrical entities."""
-        o  = geomDataAPI_Point( self._feature.data().attribute("Origin") ).pnt()
-        dx = geomDataAPI_Dir( self._feature.data().attribute("DirX") ).dir()
-        n  = geomDataAPI_Dir( self._feature.data().attribute("Norm") ).dir()
+        o = self._origin.pnt()
+        dx = self._dir_x.dir()
+        n = self._norm.dir()
 
         # The faces are kept otherwise they are destroyed at exit
         faces = ShapeList()
-        GeomAlgoAPI_SketchBuilder.createFaces(o, dx, n, self._selection, faces)
-        #TODO: Deal with several faces 
+        GeomAlgoAPI_SketchBuilder.createFaces(o, dx, n, wire, faces)
+        # TODO: Deal with several faces
         return faces[0]
 
     def result(self):