From d743de3536e0b7ce66e85d901a917e467dbad0e7 Mon Sep 17 00:00:00 2001 From: azv Date: Tue, 4 Jun 2019 16:16:12 +0300 Subject: [PATCH] Implement changing sketch plane through Python API. Add unit test. --- src/SketchAPI/SketchAPI_Sketch.cpp | 20 ++++ src/SketchAPI/SketchAPI_Sketch.h | 5 + src/SketchPlugin/CMakeLists.txt | 1 + .../Test/TestChangeSketchPlane1.py | 107 ++++++++++++++++++ 4 files changed, 133 insertions(+) create mode 100644 src/SketchPlugin/Test/TestChangeSketchPlane1.py diff --git a/src/SketchAPI/SketchAPI_Sketch.cpp b/src/SketchAPI/SketchAPI_Sketch.cpp index 380ec2469..7476380df 100644 --- a/src/SketchAPI/SketchAPI_Sketch.cpp +++ b/src/SketchAPI/SketchAPI_Sketch.cpp @@ -127,6 +127,26 @@ void SketchAPI_Sketch::setPlane(const std::shared_ptr & thePlane) execute(); } +void SketchAPI_Sketch::setPlane(const ModelHighAPI_Selection & thePlane, + bool theRemoveExternalDependency) +{ + std::shared_ptr aSketch = + std::dynamic_pointer_cast(feature()); + + DocumentPtr aDoc = aSketch->document(); + bool useVisible = false; + FeaturePtr aCurFeatureBefore = aDoc->currentFeature(useVisible); + aDoc->setCurrentFeature(aSketch, useVisible); + + if (theRemoveExternalDependency) + aSketch->customAction(SketchPlugin_Sketch::ACTION_REMOVE_EXTERNAL()); + + setExternal(thePlane); + + aDoc->setCurrentFeature(aCurFeatureBefore, useVisible); +} + +//-------------------------------------------------------------------------------------- void SketchAPI_Sketch::setExternal(const ModelHighAPI_Selection & theExternal) { fillAttribute(theExternal, myexternal); diff --git a/src/SketchAPI/SketchAPI_Sketch.h b/src/SketchAPI/SketchAPI_Sketch.h index 2ec522a9a..1d3f762a6 100644 --- a/src/SketchAPI/SketchAPI_Sketch.h +++ b/src/SketchAPI/SketchAPI_Sketch.h @@ -98,6 +98,11 @@ public: SKETCHAPI_EXPORT void setPlane(const std::shared_ptr & thePlane); + /// Change sketch plane + SKETCHAPI_EXPORT + void setPlane(const ModelHighAPI_Selection & thePlane, + bool theRemoveExternalDependency = false); + /// Set external SKETCHAPI_EXPORT void setExternal(const ModelHighAPI_Selection & theExternal); diff --git a/src/SketchPlugin/CMakeLists.txt b/src/SketchPlugin/CMakeLists.txt index 629a49ba0..164e2c430 100644 --- a/src/SketchPlugin/CMakeLists.txt +++ b/src/SketchPlugin/CMakeLists.txt @@ -196,6 +196,7 @@ ADD_UNIT_TESTS( Test2860.py Test2894.py TestArcBehavior.py + TestChangeSketchPlane1.py TestConstraintAngle.py TestConstraintCoincidence.py TestConstraintCollinear.py diff --git a/src/SketchPlugin/Test/TestChangeSketchPlane1.py b/src/SketchPlugin/Test/TestChangeSketchPlane1.py new file mode 100644 index 000000000..dd4105fba --- /dev/null +++ b/src/SketchPlugin/Test/TestChangeSketchPlane1.py @@ -0,0 +1,107 @@ +# Copyright (C) 2019 CEA/DEN, EDF R&D +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +# + +from SketchAPI import * +from salome.shaper import model +import math + +def checkMiddlePoint(feature, x, y, z, tolerance = 1.e-7): + shape = feature.results()[0].resultSubShapePair()[0].shape() + assert(shape is not None) + middlePoint = shape.middlePoint() + assert(math.fabs(middlePoint.x() - x) < tolerance), "X: {} != {}".format(middlePoint.x(), x) + assert(math.fabs(middlePoint.y() - y) < tolerance), "Y: {} != {}".format(middlePoint.y(), y) + assert(math.fabs(middlePoint.z() - z) < tolerance), "Z: {} != {}".format(middlePoint.z(), z) + + +model.begin() +partSet = model.moduleDocument() +Part_1 = model.addPart(partSet) +Part_1_doc = Part_1.document() +Box_1 = model.addBox(Part_1_doc, 10, 10, 10) +Sketch_1 = model.addSketch(Part_1_doc, model.selection("FACE", "Box_1_1/Left")) +SketchCircle_1 = Sketch_1.addCircle(4, 5.52786404500042, 3) +SketchProjection_1 = Sketch_1.addProjection(model.selection("EDGE", "[Box_1_1/Front][Box_1_1/Left]"), False) +SketchLine_1 = SketchProjection_1.createdFeature() +SketchConstraintDistance_1 = Sketch_1.setDistance(SketchCircle_1.center(), SketchLine_1.result(), 6, True) +SketchConstraintRadius_1 = Sketch_1.setRadius(SketchCircle_1.results()[1], 3) +SketchIntersectionPoint_1 = Sketch_1.addIntersectionPoint(model.selection("EDGE", "[Box_1_1/Back][Box_1_1/Top]"), False) +[SketchPoint_1] = SketchIntersectionPoint_1.intersectionPoints() +SketchConstraintDistance_2 = Sketch_1.setDistance(SketchAPI_Point(SketchPoint_1).coordinates(), SketchCircle_1.center(), 6, True) +model.do() +ExtrusionFuse_1 = model.addExtrusionFuse(Part_1_doc, [model.selection("WIRE", "Sketch_1/Face-SketchCircle_1_2r_wire")], model.selection(), 1, 0, [model.selection("SOLID", "Box_1_1")]) +model.end() + +SKETCH_DOF = 0 +model.checkSketch(Sketch_1, SKETCH_DOF) + +from GeomAPI import GeomAPI_Shape + +model.testNbResults(ExtrusionFuse_1, 1) +model.testNbSubResults(ExtrusionFuse_1, [0]) +model.testNbSubShapes(ExtrusionFuse_1, GeomAPI_Shape.SOLID, [1]) +model.testNbSubShapes(ExtrusionFuse_1, GeomAPI_Shape.FACE, [8]) +model.testNbSubShapes(ExtrusionFuse_1, GeomAPI_Shape.EDGE, [30]) +model.testNbSubShapes(ExtrusionFuse_1, GeomAPI_Shape.VERTEX, [60]) +model.testResultsVolumes(ExtrusionFuse_1, [1028.2743338823]) + +checkMiddlePoint(ExtrusionFuse_1, 4.96954097, 4.7867868, 5.0160782) + +# change plane and check error +model.begin() +Sketch_1.setPlane(model.selection("FACE", "Box_1_1/Front")) +model.end() +assert(SketchIntersectionPoint_1.feature().error() != "") +# revert error +model.undo() + +# change plane to get correct sketch +model.begin() +Sketch_1.setPlane(model.selection("FACE", "Box_1_1/Right")) +model.end() +model.checkSketch(Sketch_1, SKETCH_DOF) + +model.testNbResults(ExtrusionFuse_1, 1) +model.testNbSubResults(ExtrusionFuse_1, [0]) +model.testNbSubShapes(ExtrusionFuse_1, GeomAPI_Shape.SOLID, [1]) +model.testNbSubShapes(ExtrusionFuse_1, GeomAPI_Shape.FACE, [8]) +model.testNbSubShapes(ExtrusionFuse_1, GeomAPI_Shape.EDGE, [30]) +model.testNbSubShapes(ExtrusionFuse_1, GeomAPI_Shape.VERTEX, [60]) +model.testResultsVolumes(ExtrusionFuse_1, [1028.2743338823]) + +checkMiddlePoint(ExtrusionFuse_1, 4.96954097, 5.2132132, 5.0160782) + +# change plane removing features related to external objects +model.begin() +Sketch_1.setPlane(model.selection("FACE", "Box_1_1/Top"), True) +model.end() +SKETCH_DOF = 6 +model.checkSketch(Sketch_1, SKETCH_DOF) + +model.testNbResults(ExtrusionFuse_1, 1) +model.testNbSubResults(ExtrusionFuse_1, [2]) +model.testNbSubShapes(ExtrusionFuse_1, GeomAPI_Shape.SOLID, [2]) +model.testNbSubShapes(ExtrusionFuse_1, GeomAPI_Shape.FACE, [9]) +model.testNbSubShapes(ExtrusionFuse_1, GeomAPI_Shape.EDGE, [30]) +model.testNbSubShapes(ExtrusionFuse_1, GeomAPI_Shape.VERTEX, [60]) +model.testResultsVolumes(ExtrusionFuse_1, [1028.2743338823]) + +checkMiddlePoint(ExtrusionFuse_1, 5.0, 0.73606797, 5.5) + +assert(model.checkPythonDump()) -- 2.39.2