From: spo Date: Thu, 22 Oct 2015 07:49:10 +0000 (+0300) Subject: Improve Extrusion feature and add tests. X-Git-Tag: V_2.1.0~206^2~79^2~2 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=2038719060cd2da532ebdde1185fb64cd5cb95df;p=modules%2Fshaper.git Improve Extrusion feature and add tests. --- diff --git a/src/PythonAPI/CMakeLists.txt b/src/PythonAPI/CMakeLists.txt index 02fe15075..8247ab6bf 100644 --- a/src/PythonAPI/CMakeLists.txt +++ b/src/PythonAPI/CMakeLists.txt @@ -12,6 +12,7 @@ INCLUDE(UnitTest) ADD_UNIT_TESTS( TestShaper.py TestModel.py + TestSketcherAddPoint.py TestSketcherAddLine.py TestSketcherAddArc.py @@ -26,4 +27,6 @@ ADD_UNIT_TESTS( TestSketcherSetAngle.py TestSketcherSetEqual.py TestSketcherSetFillet.py - ) + + TestFeaturesExtrusion.py + ) diff --git a/src/PythonAPI/Test/TestFeaturesExtrusion.py b/src/PythonAPI/Test/TestFeaturesExtrusion.py new file mode 100644 index 000000000..1d506590b --- /dev/null +++ b/src/PythonAPI/Test/TestFeaturesExtrusion.py @@ -0,0 +1,107 @@ +import unittest +import model +import ModelAPI + +#----------------------------------------------------------------------------- +# Fixtures + +class FeaturesAddExtrusionFixture(unittest.TestCase): + + def setUp(self): + model.begin() + # Create part + partset = model.moduleDocument() + self.part = model.addPart(partset).document() + model.do() + + def tearDown(self): + model.end() + model.reset() + + +class FeaturesExtrusionFixture(FeaturesAddExtrusionFixture): + + def setUp(self): + FeaturesAddExtrusionFixture.setUp(self) + # Create extrusion + sketch = model.addSketch(self.part, model.defaultPlane("XOY")) + circle = sketch.addCircle(0, 0, 10) + self.extrusion = model.addExtrusion(self.part, + sketch.selectFace(circle.result()), + 10, 0) + model.do() + + def tearDown(self): + FeaturesAddExtrusionFixture.tearDown(self) + +#----------------------------------------------------------------------------- +# TestCases + +class FeaturesAddExtrusionTestCase(FeaturesAddExtrusionFixture): + + def test_add_extrusion_no_base(self): + try: + extrusion = model.addExtrusion(self.part) + fail("addExtrusion should assert if base is not None") + except AssertionError: + pass + + def test_add_extrusion_by_face_and_size(self): + sketch = model.addSketch(self.part, model.defaultPlane("XOY")) + circle = sketch.addCircle(0, 0, 10) + extrusion = model.addExtrusion(self.part, + sketch.selectFace(circle.result()), + 10, 0) + + def test_add_extrusion_by_face_and_planes(self): + # base + base_sketch = model.addSketch(self.part, model.defaultPlane("XOY")) + base_circle = base_sketch.addCircle(0, 0, 10) + base_sketch.selectFace(base_circle.result()) + # to + to_plane = model.defaultPlane("XOY") + to_plane.location().setZ(10) + to_sketch = model.addSketch(self.part, to_plane) + to_circle = to_sketch.addCircle(0, 0, 10) + to_sketch.selectFace(to_circle.result()) + # from + from_plane = model.defaultPlane("XOY") + from_plane.location().setZ(-10) + from_sketch = model.addSketch(self.part, from_plane) + from_circle = from_sketch.addCircle(0, 0, 10) + from_sketch.selectFace(from_circle.result()) + + extrusion = model.addExtrusion(self.part, + base_sketch, + to_sketch, 15, + from_sketch, 20) + + +class FeaturesExtrusionTestCase(FeaturesExtrusionFixture): + + def test_extrusion_set_sizes(self): + self.extrusion.setSize(15, 20) + + def test_extrusion_set_planes_and_offsets(self): + # base + base_sketch = model.addSketch(self.part, model.defaultPlane("XOY")) + base_circle = base_sketch.addCircle(0, 0, 10) + base_sketch.selectFace(base_circle.result()) + # to + to_plane = model.defaultPlane("XOY") + to_plane.location().setZ(10) + to_sketch = model.addSketch(self.part, to_plane) + to_circle = to_sketch.addCircle(0, 0, 10) + to_sketch.selectFace(to_circle.result()) + # from + from_plane = model.defaultPlane("XOY") + from_plane.location().setZ(-10) + from_sketch = model.addSketch(self.part, from_plane) + from_circle = from_sketch.addCircle(0, 0, 10) + from_sketch.selectFace(from_circle.result()) + + self.extrusion.setPlanesAndOffsets(to_sketch, 15, from_sketch, 20) + + +if __name__ == "__main__": + unittest.main() diff --git a/src/PythonAPI/model/__init__.py b/src/PythonAPI/model/__init__.py index a84a16515..28faea654 100644 --- a/src/PythonAPI/model/__init__.py +++ b/src/PythonAPI/model/__init__.py @@ -10,7 +10,7 @@ from roots import * from part import Part as addPart from sketcher.sketch import addSketch -from extrusion import Extrusion as addExtrusion +from extrusion import addExtrusion from boolean import Addition as addAddition from boolean import Subtraction as addSubtraction from boolean import Intersection as addIntersection diff --git a/src/PythonAPI/model/extrusion.py b/src/PythonAPI/model/extrusion.py index 9b9b6de32..88f79182d 100644 --- a/src/PythonAPI/model/extrusion.py +++ b/src/PythonAPI/model/extrusion.py @@ -1,43 +1,151 @@ """Extrusion Interface Author: Daniel Brunier-Coulin with contribution by Mikhail Ponikarov + and Sergey Pokhodenko Copyright (C) 2014-20xx CEA/DEN, EDF R&D """ -from ModelAPI import * +from .roots import Interface +from .sketcher.sketch import Sketch -class Extrusion(): +def addExtrusion(part, *args): + """Add an Extrusion feature to the Part and return Extrusion. - def __init__ (self, part, sketch, size): - """Inserts an extrusion of the given Sketch to the given Part and executes the operation.""" - self.my = part.addFeature("Extrusion") - self.my.string("CreationMethod").setValue("BySizes") - self.my.data().selectionList("base").append(sketch.result(), sketch.buildShape()) - if size < 0: - self.my.data().real("from_size").setValue(-size) - self.my.data().real("to_size").setValue(0) - else: - self.my.data().real("to_size").setValue(size) - self.my.data().real("from_size").setValue(0) + Pass all args to Extrusion __init__ function. + """ + assert(len(args) > 0 and args[0] is not None) + feature = part.addFeature("Extrusion") + return Extrusion(feature, *args) - if ModelAPI_Session.get().validators().validate(self.my): - self.my.execute() - else: - raise Exception("cannot make the Extrusion") +class Extrusion(Interface): + """Interface on an Extrusion feature.""" + def __init__(self, feature, base=None, *args): + """Initialize an Extrusion feature with given parameters. - def setSize (self, size): - """Modifies the size of this extrusion according to the given size.""" - if size < 0: - self.my.data().real("from_size").setValue(-size) - self.my.data().real("to_size").setValue(0) - else: - self.my.data().real("to_size").setValue(size) - self.my.data().real("from_size").setValue(0) + Expected arguments for all modes: + feature -- an Extrusion feature - self.my.execute() + Expected arguments for initializing the feature: + base -- name, sketch or list of names and sketches. + If base is None then don't change the feature. - def result (self): - """Returns the result data of this Feature.""" - return self.my.firstResult() + For BySize mode (expect 2 arguments): + to_size -- upper size + from_size -- lower size + + For ByPlanesAndOffsets mode (expect 4 arguments): + to_object -- upper object (plane) + to_offset -- offset from upper object + from_object -- lower object (plane) + from_offset -- offset from lower object + """ + Interface.__init__(self, feature) + assert(self._feature.getKind() == "Extrusion") + + self._base = self._feature.data().selectionList("base") + self._CreationMethod = self._feature.string("CreationMethod") + self._to_size = self._feature.data().real("to_size") + self._from_size = self._feature.data().real("from_size") + self._to_object = self._feature.data().selection("to_object") + self._to_offset = self._feature.data().real("to_offset") + self._from_object = self._feature.data().selection("from_object") + self._from_offset = self._feature.data().real("from_offset") + + if base is None: + return + + self.__setBase(base) + + if len(args) == 4: + self.__createByPlanesAndOffsets(*args) + elif len(args) == 2: + self.__createBySize(*args) + else: + raise Exception("cannot create the Extrusion") + + self.__execute() + pass + + def __setBase(self, base): + """Initialize base attribute of the feature. + + Expected arguments: + base -- could be object of this types: + - string - topology name of object + - Sketch - sketch object + - iterable - list or tuple with string and/or Sketch objects + """ + self._base.clear() + if isinstance(base, basestring): + self._base.append(base) + elif isinstance(base, Sketch): + self._base.append(base.result(), base.buildShape()) + else: + for item in base: + if isinstance(item, basestring): + self._base.append(item) + else: + self._base.append(item.result(), item.buildShape()) + pass + + def __clear(self): + self._CreationMethod.setValue("BySizes") + self._to_size.setValue(0) + self._from_size.setValue(0) + self._to_object.setValue(None, None) + self._to_offset.setValue(0) + self._from_object.setValue(None, None) + self._from_offset.setValue(0) + pass + + def __createBySize(self, to_size, from_size): + self.__clear() + self._CreationMethod.setValue("BySizes") + self._to_size.setValue(to_size) + self._from_size.setValue(from_size) + pass + + def __createByPlanesAndOffsets(self, to_object, to_offset, + from_object, from_offset): + self.__clear() + self._CreationMethod.setValue("ByPlanesAndOffsets") + self._to_object.setValue(to_object.result(), to_object.buildShape()) + self._to_offset.setValue(to_offset) + self._from_object.setValue(from_object.result(), from_object.buildShape()) + self._from_offset.setValue(from_offset) + pass + + def __execute(self): + if self.areInputValid(): + self.execute() + else: + raise Exception("cannot execute Extrusion") + + def setBase(self, base): + """Modify base attribute of the feature. + + See __init__. + """ + self.__setBase(base) + self.__execute() + pass + + def setSize(self, *args): + """Modify the to_size, from_size attributes of the feature. + + See __init__. + """ + self.__createBySize(*args) + self.__execute() + pass + + def setPlanesAndOffsets(self, *args): + """Modify planes and offsets attributes of the feature. + + See __init__. + """ + self.__createByPlanesAndOffsets(*args) + self.__execute() + pass