From 1a3af271e2c2990667767e1d31198d71acba593b Mon Sep 17 00:00:00 2001 From: spo Date: Fri, 30 Oct 2015 17:26:33 +0300 Subject: [PATCH] Improve style and structure of many features. --- src/PythonAPI/model/construction/axis.py | 4 +- src/PythonAPI/model/construction/plane.py | 4 +- src/PythonAPI/model/construction/point.py | 3 + src/PythonAPI/model/exchange/exchange.py | 6 +- src/PythonAPI/model/features/boolean.py | 10 ++- src/PythonAPI/model/features/extrusion.py | 7 +- .../model/features/extrusion_boolean.py | 7 +- .../model/features/extrusion_sketch.py | 7 +- src/PythonAPI/model/features/group.py | 5 ++ src/PythonAPI/model/features/partition.py | 16 +++-- src/PythonAPI/model/features/placement.py | 5 ++ src/PythonAPI/model/features/revolution.py | 68 ++++++++----------- .../model/features/revolution_boolean.py | 15 ++-- .../model/features/revolution_sketch.py | 15 ++-- src/PythonAPI/model/features/roots.py | 44 +++++++++++- src/PythonAPI/model/features/rotation.py | 5 ++ src/PythonAPI/model/features/translation.py | 5 ++ src/PythonAPI/model/parameter/parameter.py | 2 + src/PythonAPI/model/partset/part.py | 2 + src/PythonAPI/model/sketcher/arc.py | 21 +++--- src/PythonAPI/model/sketcher/sketch.py | 1 + 21 files changed, 172 insertions(+), 80 deletions(-) diff --git a/src/PythonAPI/model/construction/axis.py b/src/PythonAPI/model/construction/axis.py index 6bbaea87f..9152310b6 100644 --- a/src/PythonAPI/model/construction/axis.py +++ b/src/PythonAPI/model/construction/axis.py @@ -47,11 +47,13 @@ class Axis(Interface): if not args: return - assert(len(args) == 2 or len(args) == 1) + assert(len(args) in (1, 2)) if len(args) == 2: self.setPoints(*args) elif len(args) == 1: self.setCylindricalFace(*args) + + self._execute() pass def __clear(self): diff --git a/src/PythonAPI/model/construction/plane.py b/src/PythonAPI/model/construction/plane.py index f98c57b3e..0da506006 100644 --- a/src/PythonAPI/model/construction/plane.py +++ b/src/PythonAPI/model/construction/plane.py @@ -53,11 +53,13 @@ class Plane(Interface): if not args: return - assert(len(args) == 2 or len(args) == 4) + assert(len(args) in (2, 4)) if len(args) == 2: self.setFaceAndDistance(*args) elif len(args) == 4: self.setGeneralEquation(*args) + + self._execute() pass def __clear(self): diff --git a/src/PythonAPI/model/construction/point.py b/src/PythonAPI/model/construction/point.py index c3c649784..fa81fcf1f 100644 --- a/src/PythonAPI/model/construction/point.py +++ b/src/PythonAPI/model/construction/point.py @@ -41,7 +41,10 @@ class Point(Interface): if not args: return + assert(len(args) == 3) self.setPoint(*args) + + self._execute() pass def setPoint(self, x, y, z): diff --git a/src/PythonAPI/model/exchange/exchange.py b/src/PythonAPI/model/exchange/exchange.py index fdeaa4cca..375c6fd01 100644 --- a/src/PythonAPI/model/exchange/exchange.py +++ b/src/PythonAPI/model/exchange/exchange.py @@ -38,7 +38,9 @@ class Import(Interface): return assert(len(args) == 1) - self.setFilePath(*args) + self.setFilePath(args[0]) + + self._execute() pass def setFilePath(self, file_path): @@ -91,6 +93,8 @@ class Export(Interface): self.setFilePath(args[0]) self.setFileFormat(args[1]) self.setObjects(args[2]) + + self._execute() pass def setFilePath(self, file_path): diff --git a/src/PythonAPI/model/features/boolean.py b/src/PythonAPI/model/features/boolean.py index ce04f2f31..78cfbefb8 100644 --- a/src/PythonAPI/model/features/boolean.py +++ b/src/PythonAPI/model/features/boolean.py @@ -36,7 +36,7 @@ def addIntersection(part, object, tool): class Boolean(Interface): """Abstract root class of Boolean Features.""" - def __init__(self, feature, main_objects, tool_objects, bool_type): + def __init__(self, feature, *args): Interface.__init__(self, feature) assert(self._feature.getKind() == "Boolean") @@ -48,9 +48,17 @@ class Boolean(Interface): assert(self._tool_objects) assert(self._bool_type) + if not args: + return + + assert(len(args) == 3) + main_objects, tool_objects, bool_type = args + self.setMainObjects(main_objects) self.setToolObjects(tool_objects) self.setBoolType(bool_type) + + self._execute() pass def setMainObjects(self, main_objects): diff --git a/src/PythonAPI/model/features/extrusion.py b/src/PythonAPI/model/features/extrusion.py index 2e0390813..0f1cbea83 100644 --- a/src/PythonAPI/model/features/extrusion.py +++ b/src/PythonAPI/model/features/extrusion.py @@ -64,10 +64,11 @@ class Extrusion(Interface): if not args: return - self.setBase(args[0]) - + assert(len(args) in (2, 3, 5)) + base = args[0] args = args[1:] - assert(len(args) in (1, 2, 4)) + + self.setBase(base) if len(args) == 4: self.setPlanesAndOffsets(*args) diff --git a/src/PythonAPI/model/features/extrusion_boolean.py b/src/PythonAPI/model/features/extrusion_boolean.py index a5b3f8804..f7e168fde 100644 --- a/src/PythonAPI/model/features/extrusion_boolean.py +++ b/src/PythonAPI/model/features/extrusion_boolean.py @@ -28,7 +28,8 @@ def addExtrusionFuse(part, *args): class ExtrusionBoolean(CompositeBoolean): def __init__(self, feature, *args): - CompositeBoolean.__init__(self, feature, *args) + CompositeBoolean.__init__(self, feature, *args[:3]) + args = args[3:] self._CreationMethod = self._feature.string("CreationMethod") self._to_size = self._feature.data().real("to_size") @@ -49,11 +50,13 @@ class ExtrusionBoolean(CompositeBoolean): if not args: return - assert(len(args) == 4 or len(args) == 2) + assert(len(args) in (2, 4)) if len(args) == 4: self.setPlanesAndOffsets(*args) elif len(args) == 2: self.setSizes(*args) + + self._execute() pass def __clear(self): diff --git a/src/PythonAPI/model/features/extrusion_sketch.py b/src/PythonAPI/model/features/extrusion_sketch.py index dd161f367..bf8ae8fe1 100644 --- a/src/PythonAPI/model/features/extrusion_sketch.py +++ b/src/PythonAPI/model/features/extrusion_sketch.py @@ -19,7 +19,8 @@ def addExtrusionSketch(part, *args): class ExtrusionSketch(CompositeSketch): def __init__(self, feature, *args): - CompositeSketch.__init__(self, feature, *args) + CompositeSketch.__init__(self, feature, *args[:2]) + args = args[2:] self._CreationMethod = self._feature.string("CreationMethod") self._to_size = self._feature.data().real("to_size") @@ -40,11 +41,13 @@ class ExtrusionSketch(CompositeSketch): if not args: return - assert(len(args) == 4 or len(args) == 2) + assert(len(args) in (2, 4)) if len(args) == 4: self.setPlanesAndOffsets(*args) elif len(args) == 2: self.setSizes(*args) + + self._execute() pass def __clear(self): diff --git a/src/PythonAPI/model/features/group.py b/src/PythonAPI/model/features/group.py index a9fbfbe76..e6a7602ac 100644 --- a/src/PythonAPI/model/features/group.py +++ b/src/PythonAPI/model/features/group.py @@ -26,8 +26,13 @@ class Group(Interface): assert(self._group_list) + if not args: + return + assert(len(args) == 1) self.setGroupList(args[0]) + + self._execute() pass def setGroupList(self, main_objects): diff --git a/src/PythonAPI/model/features/partition.py b/src/PythonAPI/model/features/partition.py index 6fa638d7a..f1064152c 100644 --- a/src/PythonAPI/model/features/partition.py +++ b/src/PythonAPI/model/features/partition.py @@ -19,8 +19,7 @@ def addPartition(part, *args): class Partition(Interface): """Interface on an Partition feature.""" - def __init__(self, feature, main_objects=None, - tool_objects=None, partition_combine=None): + def __init__(self, feature, *args): """Initialize an Partition feature with given parameters. Expected arguments: @@ -42,12 +41,17 @@ class Partition(Interface): assert(self._tool_objects) assert(self._partition_combine) - if main_objects is None: + if not args: return - self._fill_attribute(self._main_objects, main_objects) - self._fill_attribute(self._tool_objects, tool_objects) - self._fill_attribute(self._partition_combine, partition_combine) + assert(len(args) == 3) + main_objects, tool_objects, partition_combine = args + + self.setMainObjects(main_objects) + self.setToolObjects(tool_objects) + self.setPartitionCombine(partition_combine) + + self._execute() pass def setMainObjects(self, main_objects): diff --git a/src/PythonAPI/model/features/placement.py b/src/PythonAPI/model/features/placement.py index 78f2bf9ec..ab8ac046f 100644 --- a/src/PythonAPI/model/features/placement.py +++ b/src/PythonAPI/model/features/placement.py @@ -34,12 +34,17 @@ class Placement(Interface): assert(self._reverse_direction) assert(self._centering) + if not args: + return + assert(len(args) == 5) self.setObjectList(args[0]) self.setStartShape(args[1]) self.setEndShape(args[2]) self.setReverseDirection(args[3]) self.setCentering(args[4]) + + self._execute() pass def setObjectList(self, objects_list): diff --git a/src/PythonAPI/model/features/revolution.py b/src/PythonAPI/model/features/revolution.py index c2eaaae58..6b0fdd21a 100644 --- a/src/PythonAPI/model/features/revolution.py +++ b/src/PythonAPI/model/features/revolution.py @@ -19,7 +19,7 @@ def addRevolution(part, *args): class Revolution(Interface): """Interface on an Revolution feature.""" - def __init__(self, feature, base=None, axis_object=None, *args): + def __init__(self, feature, *args): """Initialize an Revolution feature with given parameters. Expected arguments for all modes: @@ -63,29 +63,23 @@ class Revolution(Interface): assert(self._from_object) assert(self._from_offset) - if base is None: + if not args: return - self.__setBase(base) - self.__setAxisObject(axis_object) + assert(len(args) in (4, 6)) + + base, axis_object = args[:2] + args = args[2:] + + self.setBase(base) + self.setAxisObject(axis_object) if len(args) == 4: - self.__createByPlanesAndOffsets(*args) + self.setPlanesAndOffsets(*args) elif len(args) == 2: - self.__createByAngles(*args) - else: - raise AssertionError( - "Revolution takes 5 or 7 arguments (%s given)" % - (len(args) + 3) - ) - pass + self.setAngles(*args) - def __setBase(self, base): - self._fill_attribute(self._base, base) - pass - - def __setAxisObject(self, axis_object): - self._fill_attribute(self._axis_object, axis_object) + self._execute() pass def __clear(self): @@ -98,29 +92,12 @@ class Revolution(Interface): self._fill_attribute(self._from_offset, 0) pass - def __createByAngles(self, to_angle, from_angle): - self.__clear() - self._CreationMethod.setValue("ByAngles") - self._fill_attribute(self._to_angle, to_angle) - self._fill_attribute(self._from_angle, from_angle) - pass - - def __createByPlanesAndOffsets(self, to_object, to_offset, - from_object, from_offset): - self.__clear() - self._CreationMethod.setValue("ByPlanesAndOffsets") - self._fill_attribute(self._to_object, to_object) - self._fill_attribute(self._to_offset, to_offset) - self._fill_attribute(self._from_object, from_object) - self._fill_attribute(self._from_offset, from_offset) - pass - def setBase(self, base): """Modify base attribute of the feature. See __init__. """ - self.__setBase(base) + self._fill_attribute(self._base, base) pass def setAxisObject(self, axis_object): @@ -128,21 +105,30 @@ class Revolution(Interface): See __init__. """ - self.__setAxisObject(axis_object) + self._fill_attribute(self._axis_object, axis_object) pass - def setAngles(self, *args): + def setAngles(self, to_angle, from_angle): """Modify the to_angle, from_angle attributes of the feature. See __init__. """ - self.__createByAngles(*args) + self.__clear() + self._CreationMethod.setValue("ByAngles") + self._fill_attribute(self._to_angle, to_angle) + self._fill_attribute(self._from_angle, from_angle) pass - def setPlanesAndOffsets(self, *args): + def setPlanesAndOffsets(self, to_object, to_offset, + from_object, from_offset): """Modify planes and offsets attributes of the feature. See __init__. """ - self.__createByPlanesAndOffsets(*args) + self.__clear() + self._CreationMethod.setValue("ByPlanesAndOffsets") + self._fill_attribute(self._to_object, to_object) + self._fill_attribute(self._to_offset, to_offset) + self._fill_attribute(self._from_object, from_object) + self._fill_attribute(self._from_offset, from_offset) pass diff --git a/src/PythonAPI/model/features/revolution_boolean.py b/src/PythonAPI/model/features/revolution_boolean.py index 3dd8ef7df..d93b9f034 100644 --- a/src/PythonAPI/model/features/revolution_boolean.py +++ b/src/PythonAPI/model/features/revolution_boolean.py @@ -28,7 +28,8 @@ def addRevolutionFuse(part, *args): class RevolutionBoolean(CompositeBoolean): def __init__(self, feature, *args): - CompositeBoolean.__init__(self, feature, *args) + CompositeBoolean.__init__(self, feature, *args[:3]) + args = args[3:] self._axis_object = self._feature.data().selection("axis_object") self._CreationMethod = self._feature.string("CreationMethod") @@ -51,12 +52,18 @@ class RevolutionBoolean(CompositeBoolean): if not args: return - self.setAxisObject(args[0]) + assert(len(args) in (3, 5)) + axis_object = args[0] + args = args[1:] + + self.setAxisObject(axis_object) if len(args) == 4: - self.setPlanesAndOffsets(*args[1:]) + self.setPlanesAndOffsets(*args) elif len(args) == 2: - self.setAngles(*args[1:]) + self.setAngles(*args) + + self._execute() pass def __clear(self): diff --git a/src/PythonAPI/model/features/revolution_sketch.py b/src/PythonAPI/model/features/revolution_sketch.py index 95de94dd3..20d896e74 100644 --- a/src/PythonAPI/model/features/revolution_sketch.py +++ b/src/PythonAPI/model/features/revolution_sketch.py @@ -19,7 +19,8 @@ def addRevolutionSketch(part, *args): class RevolutionSketch(CompositeSketch): def __init__(self, feature, *args): - CompositeSketch.__init__(self, feature, *args) + CompositeSketch.__init__(self, feature, *args[:2]) + args = args[2:] self._axis_object = self._feature.data().selection("axis_object") self._CreationMethod = self._feature.string("CreationMethod") @@ -42,12 +43,18 @@ class RevolutionSketch(CompositeSketch): if not args: return - self.setAxisObject(args[0]) + assert(len(args) in (3, 5)) + axis_object = args[0] + args = args[1:] + + self.setAxisObject(axis_object) if len(args) == 4: - self.setPlanesAndOffsets(*args[1:]) + self.setPlanesAndOffsets(*args) elif len(args) == 2: - self.setAngles(*args[1:]) + self.setAngles(*args) + + self._execute() pass def __clear(self): diff --git a/src/PythonAPI/model/features/roots.py b/src/PythonAPI/model/features/roots.py index ab7ac3e32..a69cdd734 100644 --- a/src/PythonAPI/model/features/roots.py +++ b/src/PythonAPI/model/features/roots.py @@ -1,5 +1,4 @@ - from model.roots import Interface @@ -12,9 +11,34 @@ class CompositeBoolean(Interface): self._sketch_selection = self._feature.selection("sketch_selection") self._boolean_objects = self._feature.selectionList("boolean_objects") + assert(self._sketch) + assert(self._sketch_selection) + assert(self._boolean_objects) + if not args: return + assert(len(args) == 3) + sketch, sketch_selection, boolean_objects = args + + self.setSketch(sketch) + self.setSketchSelection(sketch_selection) + self.setBooleanObjects(boolean_objects) + pass + + def setSketch(self, sketch): + self._fill_attribute(self._sketch, sketch) + pass + + def setSketchSelection(self, sketch_selection): + self._fill_attribute(self._sketch_selection, sketch_selection) + pass + + def setBooleanObjects(self, boolean_objects): + self._fill_attribute(self._boolean_objects, boolean_objects) + pass + + class CompositeSketch(Interface): def __init__(self, feature, *args): @@ -23,5 +47,23 @@ class CompositeSketch(Interface): self._sketch = self._feature.reference("sketch") self._sketch_selection = self._feature.selection("sketch_selection") + assert(self._sketch) + assert(self._sketch_selection) + if not args: return + + assert(len(args) == 2) + sketch, sketch_selection = args + + self.setSketch(sketch) + self.setSketchSelection(sketch_selection) + pass + + def setSketch(self, sketch): + self._fill_attribute(self._sketch, sketch) + pass + + def setSketchSelection(self, sketch_selection): + self._fill_attribute(self._sketch_selection, sketch_selection) + pass diff --git a/src/PythonAPI/model/features/rotation.py b/src/PythonAPI/model/features/rotation.py index bbec2c31e..3318cd16b 100644 --- a/src/PythonAPI/model/features/rotation.py +++ b/src/PythonAPI/model/features/rotation.py @@ -30,10 +30,15 @@ class Rotation(Interface): assert(self._axis_object) assert(self._angle) + if not args: + return + assert(len(args) == 3) self.setMainObjects(args[0]) self.setAxisObject(args[1]) self.setAngle(args[2]) + + self._execute() pass def setMainObjects(self, main_objects): diff --git a/src/PythonAPI/model/features/translation.py b/src/PythonAPI/model/features/translation.py index 0260c643d..1004b8ea6 100644 --- a/src/PythonAPI/model/features/translation.py +++ b/src/PythonAPI/model/features/translation.py @@ -30,10 +30,15 @@ class Translation(Interface): assert(self._axis_object) assert(self._distance) + if not args: + return + assert(len(args) == 3) self.setMainObjects(args[0]) self.setAxisObject(args[1]) self.setDistance(args[2]) + + self._execute() pass def setMainObjects(self, main_objects): diff --git a/src/PythonAPI/model/parameter/parameter.py b/src/PythonAPI/model/parameter/parameter.py index 377992d7c..51c9024fd 100644 --- a/src/PythonAPI/model/parameter/parameter.py +++ b/src/PythonAPI/model/parameter/parameter.py @@ -43,6 +43,8 @@ class Parameter(Interface): assert(len(args) == 2) self.setName(args[0]) self.setExpression(args[1]) + + self._execute() pass def setName(self, name): diff --git a/src/PythonAPI/model/partset/part.py b/src/PythonAPI/model/partset/part.py index 50eb352b9..e916975ff 100644 --- a/src/PythonAPI/model/partset/part.py +++ b/src/PythonAPI/model/partset/part.py @@ -34,6 +34,8 @@ class Part(Interface): """Adds a new Part to the given Partset and activates the Part.""" Interface.__init__(self, feature) assert(self._feature.getKind() == "Part") + + self._execute() pass def document(self): diff --git a/src/PythonAPI/model/sketcher/arc.py b/src/PythonAPI/model/sketcher/arc.py index e4d12e3ba..d685befe4 100644 --- a/src/PythonAPI/model/sketcher/arc.py +++ b/src/PythonAPI/model/sketcher/arc.py @@ -9,7 +9,7 @@ class Arc(Interface): def __init__(self, feature, *args): Interface.__init__(self, feature) assert(self._feature.getKind() == "SketchArc") - + self._center = geomDataAPI_Point2D( self._feature.data().attribute("ArcCenter") ) @@ -31,11 +31,11 @@ class Arc(Interface): def centerData(self): """Return the center point data.""" return self._center - + def startPointData(self): """Return the start point data.""" return self._start_point - + def endPointData(self): """Return the end point data.""" return self._end_point @@ -43,30 +43,25 @@ class Arc(Interface): def result(self): """Return the arc circular line attribute.""" return self._feature.lastResult() - + ######## # # Private methods # ######## - - def __createByCoordinates(self, center_x, center_y, - start_x, start_y, + + def __createByCoordinates(self, center_x, center_y, + start_x, start_y, end_x, end_y): """Create an arc by point coordinates.""" self._center.setValue(center_x, center_y) self._start_point.setValue(start_x, start_y) self._end_point.setValue(end_x, end_y) self._feature.execute() - + def __createByPoints(self, center, start, end): """Create an arc with point objects.""" self._center.setValue(center.x(), center.y()) self._start_point.setValue(start.x(), start.y()) self._end_point.setValue(end.x(), end.y()) self._feature.execute() - - - - - diff --git a/src/PythonAPI/model/sketcher/sketch.py b/src/PythonAPI/model/sketcher/sketch.py index f8e162c9b..58b582d4d 100644 --- a/src/PythonAPI/model/sketcher/sketch.py +++ b/src/PythonAPI/model/sketcher/sketch.py @@ -67,6 +67,7 @@ class Sketch(Interface): self.__sketchOnFace(plane) else: self.__sketchOnPlane(plane) + pass def __sketchOnPlane(self, plane): """Create the sketch on a plane.""" -- 2.39.2