]> SALOME platform Git repositories - modules/shaper.git/commitdiff
Salome HOME
[PythonAPI] refactoring sketcher API further
authorRenaud NEDELEC <renaud.nedelec@opencascade.com>
Thu, 15 Oct 2015 17:18:49 +0000 (19:18 +0200)
committerRenaud NEDELEC <renaud.nedelec@opencascade.com>
Thu, 15 Oct 2015 17:18:49 +0000 (19:18 +0200)
src/PythonAPI/Test/TestSketcherAddLine.py
src/PythonAPI/modeler/sketcher/line.py
src/PythonAPI/modeler/sketcher/point.py
src/PythonAPI/modeler/sketcher/sketch.py

index 959827a6ac55fc6b3fb3cc4bf5e00f3b6230d1a2..41322e80fbbe59852ab3de39e38b4ed3ea6b2a54 100644 (file)
@@ -4,7 +4,10 @@ from TestSketcher import SketcherTestCase
 
 class SketcherAddLineTestCase(SketcherTestCase):    
     def runTest(self):
-        self.sketch.addLine(0, 0, 0, 1)
+        line = self.sketch.addLine(0, 0, 0, 1)
+        self.assertEqual(line.startPointData().x(), line.endPointData().x())        
+        self.assertNotEqual(line.startPointData().y(), line.endPointData().y())
+        
     
 if __name__ == "__main__":
     unittest.main()
\ No newline at end of file
index 731a238fde8e7a10c655c95e8c7a79c7e5dc3ae8..543051b29f907979e0e5e2a1755404237625ce74 100644 (file)
@@ -1,40 +1,50 @@
 from GeomDataAPI import geomDataAPI_Point2D
 
-
-class Line():
-
-  def __init__(self, sketch, *args):
-    self.my = sketch.addFeature("SketchLine")
-    if   len(args) == 4:
-      self.__createByCoordinates(*args)
-    elif len(args) == 2:
-      self.__createByPoints(*args)
-    elif len(args) == 1:
-      self.__createByName(sketch, *args)
-    else:
-      raise Exception("cannot create the Line")
-
-  def __createByCoordinates(self, x1, y1, x2, y2):
-    geomDataAPI_Point2D( self.my.data().attribute("StartPoint") ).setValue(x1, y1)
-    geomDataAPI_Point2D( self.my.data().attribute("EndPoint") ).setValue(x2, y2)
-    self.my.execute()
-
-  def __createByPoints(self, p1, p2):
-    geomDataAPI_Point2D( self.my.data().attribute("StartPoint") ).setValue(p1.x(), p1.y())
-    geomDataAPI_Point2D( self.my.data().attribute("EndPoint") ).setValue(p2.x(), p2.y())
-    self.my.execute()
-
-  def __createByName(self, sketch, name):
-    self.my.data().selection("External").selectSubShape("EDGE", name)
-    self.my.execute()
-    rigid = sketch.addFeature("SketchConstraintRigid")
-    rigid.refattr("ConstraintEntityA").setObject( self.my.firstResult() )
-
-  def startPointData (self):
-    return geomDataAPI_Point2D( self.my.data().attribute("StartPoint") )
-
-  def endPointData (self):
-    return geomDataAPI_Point2D( self.my.data().attribute("EndPoint") )
-
-  def result (self):
-    return self.my.firstResult()
\ No newline at end of file
+def addLine(sketch, *args):
+    sketch_feature = sketch.addFeature("SketchLine")
+    return LineInterface(sketch_feature, *args)
+    
+    
+class LineInterface():
+    """Interface for editing of a sketch line feature."""
+    def __init__(self, line_feature, *args):
+        self._feature = line_feature
+        self._start_point = geomDataAPI_Point2D(
+            self._feature.data().attribute("StartPoint")
+            )
+        self._end_point = geomDataAPI_Point2D(
+            self._feature.data().attribute("EndPoint")
+            )
+        if len(args) == 4:
+            self.__createByCoordinates(*args)
+        elif len(args) == 2:
+            self.__createByPoints(*args)
+        elif len(args) == 1:
+            self.__createByName(sketch, *args)
+        else:
+            raise Exception("cannot create the Line")
+
+    def __createByCoordinates(self, x1, y1, x2, y2):
+        self._start_point.setValue(x1, y1)
+        self._end_point.setValue(x2, y2)
+        self._feature.execute()
+
+    def __createByPoints(self, p1, p2):
+        self._start_point.setValue(p1.x(), p1.y())
+        self._end_point.setValue(p2.x(), p2.y())
+        self._feature.execute()
+
+    def __createByName(self, sketch, 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
+
+    def endPointData (self):
+        return self._end_point
+
+    def result (self):
+        return self._feature.firstResult()
\ No newline at end of file
index acd88be81d46496e3fac94ccaa7e982277368695..2a99aa7a15db826cd4a506d97530c944f48cf8f7 100644 (file)
@@ -1,16 +1,31 @@
-# Class definitions of Sketch features
+"""Sketch point feature interface."""
 
 from GeomDataAPI import geomDataAPI_Point2D
 
-class Point():
+def addPoint(sketch, x, y):
+    """Add a sketch point feature to the sketch.
+    
+    :return: Interface object on this feature
+    :rtype: sketcher.Point
+    """
+    sketch_point_feature = sketch.addFeature("SketchPoint")
+    return PointInterface(sketch_feature, x, y)
 
-  def __init__(self, sketch, x, y):
-    self.my = sketch.addFeature("SketchPoint")
-    geomDataAPI_Point2D( self.my.data().attribute("PointCoordindates") ).setValue(x, y)
-    self.my.execute()
 
-  def pointData (self):
-    return geomDataAPI_Point2D( self.my.data().attribute("PointCoordindates") )
+class PointInterface():
+    """Interface on point feature for data manipulation."""
+    def __init__(self, point_feature, x, y):
+        self._point_feature = point_feature
+        self._point_data = geomDataAPI_Point2D(
+            self._point_feature.data().attribute("PointCoordinates")
+            )
+        self._point_data.setValue(x, y)
+        self._point_feature.execute()
 
-  def result (self):
-    return self.my.firstResult()
+    def pointData (self):
+        """Return the point data."""
+        return self._point_data
+
+    def result (self):
+        """Return the feature result."""
+        return self._point_feature.firstResult()
index b497ff1199914530c8ad31d6c91080eddad84543..20ddfd842805cb7d5fc89fa04628c1af910c0037 100644 (file)
@@ -6,15 +6,16 @@ Copyright (C) 2014-20xx CEA/DEN, EDF R&D
 from ModelAPI    import *
 from GeomDataAPI import *
 from GeomAlgoAPI import *
-from modeler.sketcher.point import Point
-from modeler.sketcher.line import Line
+import modeler.sketcher.point as sk_point
+import modeler.sketcher.line as sk_line
 from modeler.sketcher.circle import Circle
 
 def addSketch(doc, plane):
-    """Add a Sketch feature to the Part or PartSet.
+    """Add a Sketch feature to the Part or PartSet and return an interface
+    on it.
     
-    A Sketch object is instanciated with the built feature
-    it provides an interface for manipulating the feature data.
+    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"""
     feature = featureToCompositeFeature( doc.addFeature("Sketch") )
@@ -29,8 +30,8 @@ class Sketch():
         - an existing face identified by its topological name.
         """
         self._feature = feature
-        self._selection = None       # Entities used for building the result shape
-        #   self.resultype ="Face"  # Type of Sketch result
+        self._selection = None     # Entities used for building the result shape
+        #   self.resultype ="Face" # Type of Sketch result
         if isinstance(plane, str):
             self.__sketchOnFace(plane)
         else:
@@ -58,14 +59,17 @@ class Sketch():
 
     def addPoint (self, *args):
         """Adds a point to this Sketch."""
-        return Point(self._feature, *args)
+        point_interface = sk_point.addPoint(self._feature, *args)
+        return point_interface
 
     def addLine (self, *args):
         """Adds a line to this Sketch."""
-        return Line(self._feature, *args)
+        line_interface = sk_line.addLine(self._feature, *args)
+        return line_interface
 
     def addPolyline (self, *coords):
         """Adds a poly-line to this Sketch.
+        
         The end of consecutive segments are defined as coincident.
         """
         c0 = coords[0]
@@ -76,7 +80,7 @@ class Sketch():
         # 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.endPointData(), line_2.startPointData())
             polyline.append(line_2)
             c1 = c2
             line_1 = line_2
@@ -169,12 +173,16 @@ class Sketch():
 # Getters
 
     def selectFace (self, *args):
-        """Selects the geometrical entities of this Sketch on which the result Face must be built.
-        When no entity is given, the face is based on all existing geometry of this Sketch.
+        """Select the geometrical entities of this Sketch on which 
+        the result Face must be built.
+        
+        When no entity is given, the face is based on all existing 
+        geometry of this Sketch.
         """
         #self.resultype ="Face"
         if   len(args) == 0:
-            self._selection = modelAPI_ResultConstruction( self._feature.firstResult() ).shape()
+            self._selection = modelAPI_ResultConstruction( 
+                self._feature.firstResult()).shape()
         elif len(args) == 1:
             self._selection = args[0].shape()
         else: