Salome HOME
Fix memory leaks
[modules/geom.git] / src / GEOM_SWIG / gsketcher.py
index 783bfffca5303e43055b84498232295c01838eb9..cde94b39cbb60de71a8a69dcac4a8d9b2c4780af 100644 (file)
@@ -1,5 +1,5 @@
 #  -*- coding: iso-8859-1 -*-
-# Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
+# Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -1160,11 +1160,16 @@ class Sketcher2D:
         if self.closed:
             self.myCommand = self.myCommand + ":WW"
 
+        from salome.geom.geomBuilder import ParseSketcherCommand, RaiseIfFailed
+        Command,Parameters = ParseSketcherCommand(self.myCommand)
+
         import GEOM
-        if isinstance(WorkingPlane, list): wire = self.geompyD.CurvesOp.MakeSketcher(self.myCommand, WorkingPlane)
-        if isinstance(WorkingPlane, GEOM._objref_GEOM_Object): wire = self.geompyD.CurvesOp.MakeSketcherOnPlane(self.myCommand, WorkingPlane)
+        if isinstance(WorkingPlane, list): wire = self.geompyD.CurvesOp.MakeSketcher(Command, WorkingPlane)
+        if isinstance(WorkingPlane, GEOM._objref_GEOM_Object): wire = self.geompyD.CurvesOp.MakeSketcherOnPlane(Command, WorkingPlane)
 
         self.myCommand = "Sketcher"
+        RaiseIfFailed("Sketcher", self.geompyD.CurvesOp)
+        wire.SetParameters(Parameters)
         self.geompyD._autoPublish(wire, theName, "wire")
         return wire
         
@@ -1204,10 +1209,157 @@ class Sketcher2D:
         else:
             raise RuntimeError, "Sketcher2D.close() : can't build face on unclosed wire"
 
+        from salome.geom.geomBuilder import ParseSketcherCommand, RaiseIfFailed
+        Command,Parameters = ParseSketcherCommand(self.myCommand)
+
         import GEOM
-        if isinstance(WorkingPlane, list): face = self.geompyD.CurvesOp.MakeSketcher(self.myCommand, WorkingPlane)
-        if isinstance(WorkingPlane, GEOM._objref_GEOM_Object): face = self.geompyD.CurvesOp.MakeSketcherOnPlane(self.myCommand, WorkingPlane)
+        if isinstance(WorkingPlane, list): face = self.geompyD.CurvesOp.MakeSketcher(Command, WorkingPlane)
+        if isinstance(WorkingPlane, GEOM._objref_GEOM_Object): face = self.geompyD.CurvesOp.MakeSketcherOnPlane(Command, WorkingPlane)
 
         self.myCommand = "Sketcher"
+        RaiseIfFailed("Sketcher", self.geompyD.CurvesOp)
+        face.SetParameters(Parameters)
         self.geompyD._autoPublish(face, theName, "face")
         return face
+
+## An interface to build a 2D polyline step-by-step. The polyline can contain
+#  several sections. Each section represents a list of 2d points. As well it
+#  has a name, curve type, either polyline or interpolation (BSpline curve) and
+#  Closed flag.
+#  Use geompy.Polyline2D() method to obtain an instance of this class.
+#  @ingroup sketcher
+class Polyline2D:
+    """
+    An interface to build a 2D polyline step-by-step. The polyline can contain
+    several sections. Each section represents a list of 2d points. As well it
+    has a name, curve type, either polyline or interpolation (BSpline curve) and
+    Closed flag.
+    Use geompy.Polyline2D() method to obtain an instance of this class.
+
+    Example of usage:
+        pl = geompy.Polyline2D()
+        pl.addSection("section 1", GEOM.Polyline, True, [0, 0, 10, 0, 10, 10])
+        pl.addSection("section 2", GEOM.Interpolation, False)
+        pl.addPoints([20, 0, 30, 0, 30, 10])
+        resultObj = pl.result(WorkingPlane)
+    """
+
+    def __init__(self, geompyD):
+        self.geompyD      = geompyD
+        self.myNameList   = []
+        self.myTypeList   = []
+        self.myClosedList = []
+        self.myCoordsList = []
+        pass
+
+    ## Add a new section to the polyline.
+    #
+    #  @param theName the name
+    #  @param theType the type. It can have either CORBA enumeration type
+    #         GEOM.curve_type or a value of type long. Possible input values
+    #         are: GEOM.Polyline(0) and GEOM.Interpolation(2).
+    #  @param theClosed True for closed section; False otherwise
+    #  @param thePoints the list of 2D points coordinates in the form:
+    #         [x1, y1, x2, y2, ..., xN, yN] for N points.
+    def addSection(self, theName, theType, theClosed, thePoints = None):
+        """
+        Add a new section to the polyline.
+
+        Parameters:
+            theName the name
+            theType the type. It can have either CORBA enumeration type
+                    GEOM.curve_type or a value of type long. Possible input
+                    values are: GEOM.Polyline(0) and GEOM.Interpolation(2).
+            theClosed True for closed section; False otherwise
+            thePoints the list of 2D points coordinates in the form:
+                      [x1, y1, x2, y2, ..., xN, yN] for N points.
+
+        Example of usage:
+            pl = geompy.Polyline2D()
+            pl.addSection("section 1", GEOM.Polyline, True, [0, 0, 10, 0, 10, 10])
+            resultObj = pl.result(WorkingPlane)
+        """
+        from salome.geom.geomBuilder import EnumToLong
+        self.myNameList.append(theName)
+        self.myTypeList.append(EnumToLong(theType))
+        self.myClosedList.append(theClosed)
+        if thePoints is None:
+            self.myCoordsList.append([])
+        else:
+            self.myCoordsList.append(thePoints)
+        pass
+
+    ## Add a points to the last added section of the polyline. If there are
+    #  no sections in the polyline it does nothing.
+    #
+    #  @param thePoints the list of 2D points coordinates in the form:
+    #         [x1, y1, x2, y2, ..., xN, yN] for N points.
+    def addPoints(self, thePoints):
+        """
+        Add a points to the last added section of the polyline. If there are
+        no sections in the polyline it does nothing.
+
+        Parameters:
+            thePoints the list of 2D points coordinates in the form:
+                      [x1, y1, x2, y2, ..., xN, yN] for N points.
+
+        Example of usage:
+            pl = geompy.Polyline2D()
+            pl.addSection("section 1", GEOM.Polyline, True)
+            pl.addPoints([0, 0, 10, 0, 10, 10])
+            pl.addPoints([20, 0, 30, 0, 30, 10])
+            resultObj = pl.result(WorkingPlane)
+        """
+        if self.myNameList:
+            self.myCoordsList[-1].extend(thePoints)
+        pass
+
+    ## Obtain the 2D polyline result as a wire or a compound of wires in case
+    #  of several sections defined.
+    #
+    #  @param theWorkingPlane - current Working Plane used for this 2D polyline
+    #  @param theName Object name; when specified, this parameter is used
+    #         for result publication in the study. Otherwise, if automatic
+    #         publication is switched on, default value is used for result name.
+    #
+    #  @return New GEOM_Object, containing the created shape.
+    def result(self, theWorkingPlane=[0, 0, 0, 0, 0, 1, 1, 0, 0], theName=None):
+        """
+        Obtain the 2D polyline result as a wire or a compound of wires in case
+        of several sections defined.
+
+        Parameters:
+            theWorkingPlane current Working Plane used for this 2D polyline
+            theName Object name; when specified, this parameter is used
+                    for result publication in the study. Otherwise, if automatic
+                    publication is switched on, default value is used for result name.
+
+        Returns:
+            New GEOM_Object, containing the created shape.
+
+        Example of usage:
+            pl = geompy.Polyline2D()
+            pl.addSection("section 1", GEOM.Polyline, True, [0, 0, 10, 0, 10, 10])
+            pl.addSection("section 2", GEOM.Interpolation, False)
+            pl.addPoints([20, 0, 30, 0, 30, 10])
+            resultObj = pl.result(WorkingPlane)
+        """
+        from salome.geom.geomBuilder import RaiseIfFailed
+        import GEOM
+        if isinstance(theWorkingPlane, list):
+            aResult = self.geompyD.CurvesOp.MakePolyline2D(
+                         self.myCoordsList, self.myNameList, self.myTypeList,
+                         self.myClosedList, theWorkingPlane)
+        if isinstance(theWorkingPlane, GEOM._objref_GEOM_Object):
+            aResult = self.geompyD.CurvesOp.MakePolyline2DOnPlane(
+                        self.myCoordsList, self.myNameList, self.myTypeList,
+                        self.myClosedList, theWorkingPlane)
+
+        self.myNameList   = []
+        self.myTypeList   = []
+        self.myClosedList = []
+        self.myCoordsList = []
+        RaiseIfFailed("Polyline2D.result", self.geompyD.CurvesOp)
+        self.geompyD._autoPublish(aResult, theName, "polyline")
+
+        return aResult