Salome HOME
Unit tests:
[modules/shaper.git] / src / PythonAPI / model / sketcher / sketch.py
index 447e1722433a221114329b97edc307380081c6a3..5303f7df5c335e67554367d39c8ebc4241666953 100644 (file)
@@ -2,12 +2,27 @@
 #         finalized by Renaud Nedelec and Sergey Pokhodenko
 # Copyright (C) 2014-20xx CEA/DEN, EDF R&D
 
-"""Sketch Feature Interface
-
-This interface allows to add a Sketch
-in a Part.
-The created sketch object provides all the needed methods 
+"""Sketcher interface.
+This interface allows to add a sketch
+in a part or partset.
+The created sketch object provides all the needed methods
 for sketch modification and constraint edition.
+
+Example of code:
+
+.. doctest::
+
+   >>> import model
+   >>> model.begin()
+   >>> partset = model.moduleDocument()
+   >>> part = model.addPart(partset).document()
+   >>> plane = model.defaultPlane("XOY")
+   >>> sketch = model.addSketch(part, plane)
+   >>> line = sketch.addLine(0, 0, 0, 1)
+   >>> line.endPoint().x()
+   0.0
+   >>> line.endPoint().y()
+   1.0
 """
 
 from ModelAPI import modelAPI_ResultConstruction, featureToCompositeFeature
@@ -23,16 +38,17 @@ from model.roots import Interface
 from model.tools import Selection
 
 
-def addSketch(doc, plane):
-    """Add a Sketch feature to the Part or PartSet and return an interface
-    on it.
+def addSketch(document, plane):
+    """Add a sketch to a Part or PartSet.
+
+    Arguments:
+       document(ModelAPI_Document): part or partset document
+       plane(geom.Ax3): plane on wich the sketch is built
 
-    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"""
-    feature = featureToCompositeFeature(doc.addFeature("Sketch"))
+    Returns:
+       Sketch: sketch object
+    """
+    feature = featureToCompositeFeature(document.addFeature("Sketch"))
     return Sketch(feature, plane)
 
 class Sketch(Interface):
@@ -58,7 +74,7 @@ class Sketch(Interface):
             )
         self._external = self._feature.data().selection("External")
 
-        # If no arguments are given the attributes of the feature 
+        # If no arguments are given the attributes of the feature
         # are not Initialized
         if args is not None:
             plane = args[0]
@@ -87,14 +103,35 @@ class Sketch(Interface):
     #-------------------------------------------------------------
 
     def addPoint(self, *args):
-        """Add a point to this Sketch."""
+        """Add a point to the 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."""
+        """Add a line to the sketch.
+
+        .. function:: addLine(name)
+        Select an existing line. The line is added to the sketch with a rigid
+        constraint (it cannot be modified by the sketch)
+
+        Arguments:
+            name(str): name of an existing line
+
+        .. function:: addLine(start, end)
+        Create a line by points
+
+        Arguments:
+           start(point): start point of the line
+           end(point): end point of the line
+
+        .. function:: addLine(start_x, start_y, end_x, end_y)
+        Create a line by coordinates
+
+        Arguments:
+           start_x(double): start point x coordinate
+        """
         if not args:
             raise TypeError("No arguments given")
         line_feature = self._feature.addFeature("SketchLine")
@@ -115,21 +152,31 @@ class Sketch(Interface):
         circle_feature = self._feature.addFeature("SketchCircle")
         return Circle(circle_feature, *args)
 
-    def addArc(self, *args):
-        """Add an arc to the sketch.
-        
-        :param sequence args: A sequence of arguments that can be:
-        
-           * The center, start and end points
-           * The center, start and end points coordinates
-        :return: arc object
-        :rtype: :class:`model.sketcher.Arc`
-        :raises TypeError: if no argument is provided
+    def addArc(self, *args, **kwargs):
+        """Add an arc of circle to the sketch and return an arc object.
+
+        Two different syntaxes are allowed:
+
+        .. function:: addArc(center, start, end)
+
+        Arguments:
+            center (point): center of the arc
+            start (point): start point of the arc
+            end (point): end point of the arc
+
+        .. function:: addArc(center_x, center_y, start_x, start_y, end_x, end_y)
+
+        Same as above but with coordinates
+
+        Returns:
+            Arc: arc object
+        Raises:
+            TypeError: if no argument is provided
         """
         if not args:
             raise TypeError("No arguments given")
         arc_feature = self._feature.addFeature("SketchArc")
-        return Arc(arc_feature, *args)
+        return Arc(arc_feature, *args, **kwargs)
 
     #-------------------------------------------------------------
     #
@@ -142,13 +189,13 @@ class Sketch(Interface):
         constraint to this Sketch."""
         # 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 
+        # 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._fillAttribute(constraint.refattr("ConstraintEntityA"), p1)
+        self._fillAttribute(constraint.refattr("ConstraintEntityB"), p2)
         self.execute()
         return constraint
 
@@ -217,7 +264,7 @@ class Sketch(Interface):
             raise TypeError("NoneType argument given")
         constraint = self._feature.addFeature("SketchConstraintLength")
         constraint.data().refattr("ConstraintEntityA").setObject(line)
-        constraint.data().real("ConstraintValue").setValue(length)
+        self._fillAttribute(constraint.real("ConstraintValue"), length)
         self.execute()
         return constraint
 
@@ -225,8 +272,8 @@ class Sketch(Interface):
         """Set the radius of the given circle and add the corresponding
         constraint to this Sketch."""
         constraint = self._feature.addFeature("SketchConstraintRadius")
-        constraint.data().refattr("ConstraintEntityA").setObject(circle)
-        constraint.data().real("ConstraintValue").setValue(radius)
+        self._fillAttribute(constraint.refattr("ConstraintEntityA"), circle)
+        self._fillAttribute(constraint.real("ConstraintValue"), radius)
         self.execute()
         return constraint
 
@@ -261,34 +308,41 @@ class Sketch(Interface):
         self.execute()
         return constraint
 
-    def setFillet(self, line_1, line_2, radius):
+    def setFillet(self, *args):
         """Set a fillet constraint between the 2 given lines with the given
         filleting radius."""
+        assert(args)
         constraint = self._feature.addFeature("SketchConstraintFillet")
-        constraint.data().refattr("ConstraintEntityA").setObject(line_1)
-        constraint.data().reflist("ConstraintEntityB").clear()
-        constraint.data().reflist("ConstraintEntityB").append(line_2)
+        if len(args) == 3:
+            line_1, line_2, radius = args
+            constraint.data().refattr("ConstraintEntityA").setObject(line_1)
+            constraint.data().reflist("ConstraintEntityB").clear()
+            constraint.data().reflist("ConstraintEntityB").append(line_2)
+        elif len(args) == 2:
+            point, radius = args
+            self._fillAttribute(constraint.data().refattrlist("ConstraintEntityA"), [point])
+            self._fillAttribute(constraint.real("ConstraintValue"), radius)
         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._fillAttribute(constraint.refattr("ConstraintEntityA"), object_)
         self.execute()
         return constraint
-    
+
     #-------------------------------------------------------------
     #
     # Transformation constraints
     #
     #-------------------------------------------------------------
-    
+
     def addMirror(self, mirror_line, sketch_objects):
         """Add a mirror transformation of the given objects to the sketch.
-        
+
         This transformation is a constraint.
-        
+
         :return: interface to the constraint
         :rtype: Mirror object
         """
@@ -296,7 +350,7 @@ class Sketch(Interface):
         mirror_interface = Mirror(mirror_constraint, mirror_line, sketch_objects)
         self.execute()
         return mirror_interface
-        
+
 
     #-------------------------------------------------------------
     #
@@ -327,7 +381,7 @@ class Sketch(Interface):
         # Adding and connecting next lines
         for c2 in coords[2:]:
             line_2 = self.addLine(c1, c2)
-            self.setCoincident(line_1.endPointData(), line_2.startPointData())
+            self.setCoincident(line_1.endPoint(), line_2.startPoint())
             polyline.append(line_2)
             c1 = c2
             line_1 = line_2
@@ -344,10 +398,10 @@ class Sketch(Interface):
         cn = coords[len(coords) - 1]
         ln = self.addLine(cn, c0)
         self.setCoincident(
-            pg[len(coords) - 2].endPointData(), ln.startPointData()
+            pg[len(coords) - 2].endPoint(), ln.startPoint()
             )
         self.setCoincident(
-            ln.endPointData(), pg[0].startPointData()
+            ln.endPoint(), pg[0].startPoint()
             )
         pg.append(ln)
         return pg