From 466c8631b6e6e189c94373902e8601ab823b6fe6 Mon Sep 17 00:00:00 2001 From: rraphael Date: Mon, 4 Jan 2021 02:24:46 +0100 Subject: [PATCH] move rectangle plugin from python addons to C++. Add a rectangle feature from corner and center points. Change rectangle feature dump into SketchRectangle instead of SketchLines. --- src/CollectionPlugin/Test/TestGroupShape.py | 41 +++ src/PartSet/PartSet_Module.h | 2 +- src/PartSet/PartSet_SketcherMgr.cpp | 2 +- src/PythonAddons/CMakeLists.txt | 6 +- src/PythonAddons/addons_Features.py | 9 +- src/PythonAddons/addons_Features.xml.in | 2 +- src/SketchAPI/CMakeLists.txt | 2 + src/SketchAPI/SketchAPI.i | 2 + src/SketchAPI/SketchAPI_MacroRectangle.cpp | 145 +++++++++++ src/SketchAPI/SketchAPI_MacroRectangle.h | 94 +++++++ src/SketchAPI/SketchAPI_Rectangle.cpp | 39 ++- src/SketchAPI/SketchAPI_Rectangle.h | 15 +- src/SketchAPI/SketchAPI_Sketch.cpp | 47 +++- src/SketchAPI/SketchAPI_Sketch.h | 12 +- src/SketchAPI/SketchAPI_swig.h | 1 + src/SketchPlugin/CMakeLists.txt | 6 +- .../SketchPlugin_MacroRectangle.cpp | 211 +++++++++++++++ .../SketchPlugin_MacroRectangle.h | 129 +++++++++ src/SketchPlugin/SketchPlugin_Plugin.cpp | 11 +- src/SketchPlugin/SketchPlugin_Rectangle.cpp | 245 ++++++++++++++++++ src/SketchPlugin/SketchPlugin_Rectangle.h | 103 ++++++++ src/SketchPlugin/plugin-Sketch.xml | 74 +++++- 22 files changed, 1176 insertions(+), 22 deletions(-) create mode 100644 src/CollectionPlugin/Test/TestGroupShape.py create mode 100644 src/SketchAPI/SketchAPI_MacroRectangle.cpp create mode 100644 src/SketchAPI/SketchAPI_MacroRectangle.h create mode 100644 src/SketchPlugin/SketchPlugin_MacroRectangle.cpp create mode 100644 src/SketchPlugin/SketchPlugin_MacroRectangle.h create mode 100644 src/SketchPlugin/SketchPlugin_Rectangle.cpp create mode 100644 src/SketchPlugin/SketchPlugin_Rectangle.h diff --git a/src/CollectionPlugin/Test/TestGroupShape.py b/src/CollectionPlugin/Test/TestGroupShape.py new file mode 100644 index 000000000..5ec06a958 --- /dev/null +++ b/src/CollectionPlugin/Test/TestGroupShape.py @@ -0,0 +1,41 @@ +# Copyright (C) 2014-2020 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 salome.shaper import model + +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) +Group_1 = model.addGroup(Part_1_doc, [model.selection("FACE", "Box_1_1/Top"), model.selection("FACE", "Box_1_1/Left")]) +Group_2 = model.addGroup(Part_1_doc, [model.selection("FACE", "Box_1_1/Front"), model.selection("FACE", "Box_1_1/Top")]) +GroupShape_1 = model.addGroupShape(Part_1_doc, [model.selection("COMPOUND", "Group_1"), model.selection("COMPOUND", "Group_2")]) +model.end() + +from GeomAPI import * + +model.testNbResults(GroupShape_1, 1) +model.testNbSubShapes(GroupShape_1, GeomAPI_Shape.SOLID, [0]) +model.testNbSubShapes(GroupShape_1, GeomAPI_Shape.FACE, [3]) +model.testNbSubShapes(GroupShape_1, GeomAPI_Shape.EDGE, [12]) +model.testNbSubShapes(GroupShape_1, GeomAPI_Shape.VERTEX, [24]) +model.testResultsVolumes(GroupShape_1, [300]) + +assert(model.checkPythonDump()) diff --git a/src/PartSet/PartSet_Module.h b/src/PartSet/PartSet_Module.h index ced4f9325..75467a3c4 100644 --- a/src/PartSet/PartSet_Module.h +++ b/src/PartSet/PartSet_Module.h @@ -400,7 +400,7 @@ public: virtual void createFeatures(); /// add texture - PARTSET_EXPORT static void setTexture(const std::string &theTextureFile, const AISObjectPtr& thePrs); + static void setTexture(const std::string &theTextureFile, const AISObjectPtr& thePrs); public slots: /// Slolt called on object display diff --git a/src/PartSet/PartSet_SketcherMgr.cpp b/src/PartSet/PartSet_SketcherMgr.cpp index ecc6f36d8..c85093877 100644 --- a/src/PartSet/PartSet_SketcherMgr.cpp +++ b/src/PartSet/PartSet_SketcherMgr.cpp @@ -368,7 +368,7 @@ bool PartSet_SketcherMgr::isDragModeCreation() const // Acceptable features; QStringList aList; aList << "SketchLine" << "SketchMacroCircle" << "SketchMacroArc" << - "SketchMacroEllipse" << "SketchMacroEllipticArc" << "SketchRectangle"; + "SketchMacroEllipse" << "SketchMacroEllipticArc" << "SketchMacroRectangle"; return aList.contains(aId); } diff --git a/src/PythonAddons/CMakeLists.txt b/src/PythonAddons/CMakeLists.txt index 9f40500b2..c81edf33f 100644 --- a/src/PythonAddons/CMakeLists.txt +++ b/src/PythonAddons/CMakeLists.txt @@ -37,7 +37,9 @@ INSTALL(FILES addons_Features.py DESTINATION ${SHAPER_INSTALL_ADDONS}) INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/addons_Features.xml DESTINATION ${SHAPER_INSTALL_XML_RESOURCES}) INSTALL(FILES __init__.py DESTINATION ${SHAPER_INSTALL_ADDONS}) -INSTALL(DIRECTORY macros DESTINATION ${SHAPER_INSTALL_ADDONS}) +INSTALL(DIRECTORY macros/compoundVertices DESTINATION ${SHAPER_INSTALL_ADDONS}/macros) +INSTALL(DIRECTORY macros/importParameters DESTINATION ${SHAPER_INSTALL_ADDONS}/macros) + INSTALL(DIRECTORY macros/rectangle/icons/ DESTINATION ${SHAPER_INSTALL_XML_RESOURCES}/icons/Addons) INSTALL(DIRECTORY macros/compoundVertices/icons/ DESTINATION ${SHAPER_INSTALL_XML_RESOURCES}/icons/Addons) INSTALL(DIRECTORY macros/importParameters/icons/ DESTINATION ${SHAPER_INSTALL_XML_RESOURCES}/icons/Addons) @@ -46,7 +48,7 @@ INSTALL(FILES ${TEXT_RESOURCES} DESTINATION ${SHAPER_INSTALL_XML_RESOURCES}) INCLUDE(UnitTest) ADD_UNIT_TESTS( - TestRectangle.py + #TestRectangle.py TestcompoundVertices.py TestimportParameters.py ) diff --git a/src/PythonAddons/addons_Features.py b/src/PythonAddons/addons_Features.py index 4c302a012..207dd78c4 100644 --- a/src/PythonAddons/addons_Features.py +++ b/src/PythonAddons/addons_Features.py @@ -21,7 +21,7 @@ """ import ModelAPI -from macros.rectangle.feature import SketchPlugin_Rectangle +#from macros.rectangle.feature import SketchPlugin_Rectangle from macros.compoundVertices.feature import compoundVertices from macros.importParameters.feature import importParameters @@ -43,9 +43,10 @@ class PythonFeaturesPlugin(ModelAPI.ModelAPI_Plugin): """Override ModelAPI_Plugin.createFeature()""" aFeature = None - if theFeatureID == SketchPlugin_Rectangle.ID(): - aFeature = SketchPlugin_Rectangle().__disown__() - elif theFeatureID == compoundVertices.ID(): + #if theFeatureID == SketchPlugin_Rectangle.ID(): + # aFeature = SketchPlugin_Rectangle().__disown__() + #el + if theFeatureID == compoundVertices.ID(): aFeature = compoundVertices().__disown__() elif theFeatureID == importParameters.ID(): aFeature = importParameters().__disown__() diff --git a/src/PythonAddons/addons_Features.xml.in b/src/PythonAddons/addons_Features.xml.in index 3f3b7666d..6235ee56e 100644 --- a/src/PythonAddons/addons_Features.xml.in +++ b/src/PythonAddons/addons_Features.xml.in @@ -1,5 +1,5 @@ - + diff --git a/src/SketchAPI/CMakeLists.txt b/src/SketchAPI/CMakeLists.txt index a1ea1da27..4f3e19189 100644 --- a/src/SketchAPI/CMakeLists.txt +++ b/src/SketchAPI/CMakeLists.txt @@ -34,6 +34,7 @@ SET(PROJECT_HEADERS SketchAPI_MacroCircle.h SketchAPI_MacroEllipse.h SketchAPI_MacroEllipticArc.h + SketchAPI_MacroRectangle.h SketchAPI_Mirror.h SketchAPI_Offset.h SketchAPI_Point.h @@ -59,6 +60,7 @@ SET(PROJECT_SOURCES SketchAPI_MacroCircle.cpp SketchAPI_MacroEllipse.cpp SketchAPI_MacroEllipticArc.cpp + SketchAPI_MacroRectangle.cpp SketchAPI_Mirror.cpp SketchAPI_Offset.cpp SketchAPI_Point.cpp diff --git a/src/SketchAPI/SketchAPI.i b/src/SketchAPI/SketchAPI.i index f98f8929b..bc36f1cac 100644 --- a/src/SketchAPI/SketchAPI.i +++ b/src/SketchAPI/SketchAPI.i @@ -77,6 +77,7 @@ %shared_ptr(SketchAPI_Point) %shared_ptr(SketchAPI_Projection) %shared_ptr(SketchAPI_Rectangle) +%shared_ptr(SketchAPI_MacroRectangle) %shared_ptr(SketchAPI_Rotation) %shared_ptr(SketchAPI_Translation) @@ -583,6 +584,7 @@ %include "SketchAPI_Offset.h" %include "SketchAPI_Translation.h" %include "SketchAPI_Rectangle.h" +%include "SketchAPI_MacroRectangle.h" %include "SketchAPI_Rotation.h" %include "SketchAPI_Sketch.h" %include "SketchAPI_Constraint.h" diff --git a/src/SketchAPI/SketchAPI_MacroRectangle.cpp b/src/SketchAPI/SketchAPI_MacroRectangle.cpp new file mode 100644 index 000000000..d2614f452 --- /dev/null +++ b/src/SketchAPI/SketchAPI_MacroRectangle.cpp @@ -0,0 +1,145 @@ +// Copyright (C) 2014-2020 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 +// + +#include "SketchAPI_MacroRectangle.h" +#include "SketchAPI_Rectangle.h" + +#include + +#include +#include +#include +#include + +//================================================================================================== +SketchAPI_MacroRectangle::SketchAPI_MacroRectangle(const std::shared_ptr& theFeature) + : SketchAPI_SketchEntity(theFeature) +{ + initialize(); +} + +//================================================================================================== +SketchAPI_MacroRectangle::SketchAPI_MacroRectangle(const std::shared_ptr& theFeature, + double theStartX, + double theStartY, + double theSecondX, + double theSecondY, bool isSecondPointCenter): + SketchAPI_SketchEntity(theFeature) +{ + if(initialize()) { + if(isSecondPointCenter) + setByStartAndCenterPoints(theStartX, theStartY, theSecondX, theSecondY); + else + setByStartAndEndPoints(theStartX, theStartY, theSecondX, theSecondY); + } +} + +//================================================================================================== +SketchAPI_MacroRectangle::SketchAPI_MacroRectangle(const std::shared_ptr& theFeature, + const std::shared_ptr& theStartPoint, + const std::shared_ptr& theSecondPoint, bool isSecondPointCenter): + SketchAPI_SketchEntity(theFeature) +{ + if(initialize()) { + if(isSecondPointCenter) + setByStartAndCenterPoints(theStartPoint, theSecondPoint); + else + setByStartAndEndPoints(theStartPoint, theSecondPoint); + } +} + +//================================================================================================== +SketchAPI_MacroRectangle::~SketchAPI_MacroRectangle() +{ +} + +//================================================================================================== +void SketchAPI_MacroRectangle::setByStartAndEndPoints(double theStartX, double theStartY, + double theEndX, double theEndY) +{ + fillAttribute(SketchPlugin_MacroRectangle::START_END_POINT_TYPE_ID(), rectangleType()); + fillAttribute(startPoint1(), theStartX, theStartY); + fillAttribute(endPoint1(), theEndX, theEndY); + execute(); +} + +//================================================================================================== +void SketchAPI_MacroRectangle::setByStartAndEndPoints(const std::shared_ptr& theStartPoint, + const std::shared_ptr& theEndPoint) +{ + fillAttribute(SketchPlugin_MacroRectangle::START_END_POINT_TYPE_ID(), rectangleType()); + fillAttribute(theStartPoint, startPoint1()); + fillAttribute(theEndPoint, endPoint1()); + + execute(); +} + +//================================================================================================== +void SketchAPI_MacroRectangle::setByStartAndCenterPoints(double theStartX, double theStartY, + double theCenterX, double theCenterY) +{ + fillAttribute(SketchPlugin_MacroRectangle::START_CENTER_POINT_TYPE_ID(), rectangleType()); + fillAttribute(startPoint2(), theStartX, theStartY); + fillAttribute(centerPoint(), theCenterX, theCenterY); + execute(); +} + +//================================================================================================== +void SketchAPI_MacroRectangle::setByStartAndCenterPoints(const std::shared_ptr& theStartPoint, + const std::shared_ptr& theCenterPoint){ + fillAttribute(SketchPlugin_MacroRectangle::START_END_POINT_TYPE_ID(), rectangleType()); + fillAttribute(theStartPoint, startPoint2()); + fillAttribute(theCenterPoint, centerPoint()); + + execute(); +} + +//================================================================================================== +/* +void SketchAPI_MacroRectangle::dump(ModelHighAPI_Dumper& theDumper) const +{ + FeaturePtr aBase = feature(); + + std::shared_ptr myRectangle = std::dynamic_pointer_cast(aBase); + if(!myRectangle) + return; + + if (isCopy()) + return; // no need to dump copied feature + + const std::string& aSketchName = theDumper.parentName(aBase); + + AttributeSelectionPtr anExternal = aBase->selection(SketchPlugin_SketchEntity::EXTERNAL_ID()); + if (anExternal->context()) { + // rectangle is external + theDumper << aBase << " = " << aSketchName << ".addRectangle(" << anExternal << ")" << std::endl; + } else { + theDumper << aBase << " = " << aSketchName << ".addRectangle("; + + if(myRectangle->TYPE_ID() == SketchPlugin_MacroRectangle::START_CENTER_POINT_TYPE_ID()) + // rectangle given by start and center points + theDumper << startPoint2() << ", " << centerPoint() << ", 1)" << std::endl; + else + // rectangle given by start and end points + theDumper << startPoint1() << ", " << endPoint1() << ", 0)" << std::endl; + } + // dump "auxiliary" flag if necessary + SketchAPI_SketchEntity::dump(theDumper); +} +*/ diff --git a/src/SketchAPI/SketchAPI_MacroRectangle.h b/src/SketchAPI/SketchAPI_MacroRectangle.h new file mode 100644 index 000000000..773c849d1 --- /dev/null +++ b/src/SketchAPI/SketchAPI_MacroRectangle.h @@ -0,0 +1,94 @@ +// Copyright (C) 2014-2020 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 +// + +#ifndef SketchAPI_MacroRectangle_H_ +#define SketchAPI_MacroRectangle_H_ + +#include "SketchAPI.h" +#include "SketchAPI_SketchEntity.h" +#include + +class ModelHighAPI_Selection; + +/// \class SketchAPI_MacroRectangle +/// \ingroup CPPHighAPI +/// \brief Interface for Rectangle feature. +class SketchAPI_MacroRectangle: public SketchAPI_SketchEntity +{ +public: + /// Constructor without values. + SKETCHAPI_EXPORT + explicit SketchAPI_MacroRectangle(const std::shared_ptr& theFeature); + + /// Constructor with values. + SKETCHAPI_EXPORT + SketchAPI_MacroRectangle(const std::shared_ptr& theFeature, + double theStartX, + double theStartY, + double theSecondX, + double theSecondY, bool isSecondPointCenter = false); + + /// Constructor with values. + SKETCHAPI_EXPORT + SketchAPI_MacroRectangle(const std::shared_ptr& theFeature, + const std::shared_ptr& theStartPoint, + const std::shared_ptr& theSecondPoint, bool isSecondPointCenter = false); + + + /// Destructor. + SKETCHAPI_EXPORT + virtual ~SketchAPI_MacroRectangle(); + + INTERFACE_5(SketchPlugin_MacroRectangle::ID(), + rectangleType, SketchPlugin_MacroRectangle::TYPE_ID(), + ModelAPI_AttributeString, /** Rectangle type */, + startPoint1, SketchPlugin_MacroRectangle::START1_ID(), + GeomDataAPI_Point2D, /** Start point 1 */, + endPoint1, SketchPlugin_MacroRectangle::END1_ID(), + GeomDataAPI_Point2D, /** End point 1 */, + startPoint2, SketchPlugin_MacroRectangle::START2_ID(), + GeomDataAPI_Point2D, /** First point 2 */, + centerPoint, SketchPlugin_MacroRectangle::CENTER_ID(), + GeomDataAPI_Point2D, /** Center point */) + + /*SKETCHAPI_EXPORT + void dump(ModelHighAPI_Dumper& theDumper) const; +*/ +private: + /// Set by start and end points. + void setByStartAndEndPoints(double theStartX, double theStartY, + double theEndX, double theEndY); + + /// Set by start and end points. + void setByStartAndEndPoints(const std::shared_ptr& theStartPoint, + const std::shared_ptr& theEndPoint); + + /// Set by start and center points. + void setByStartAndCenterPoints(double theStartX, double theStartY, + double theCenterX, double theCenterY); + + /// Set by start and center points. + void setByStartAndCenterPoints(const std::shared_ptr& theStartPoint, + const std::shared_ptr& theCenterPoint); +}; + +/// Pointer on Rectangle object. +typedef std::shared_ptr MacroRectanglePtr; + +#endif // SketchAPI_MacroRectangle_H_ diff --git a/src/SketchAPI/SketchAPI_Rectangle.cpp b/src/SketchAPI/SketchAPI_Rectangle.cpp index c7faac8b0..f9ee374e5 100644 --- a/src/SketchAPI/SketchAPI_Rectangle.cpp +++ b/src/SketchAPI/SketchAPI_Rectangle.cpp @@ -23,10 +23,12 @@ //-------------------------------------------------------------------------------------- #include #include +#include + //-------------------------------------------------------------------------------------- SketchAPI_Rectangle::SketchAPI_Rectangle( const std::shared_ptr & theFeature) -: SketchAPI_SketchEntity(theFeature) + : SketchAPI_SketchEntity(theFeature) { initialize(); } @@ -34,7 +36,7 @@ SketchAPI_Rectangle::SketchAPI_Rectangle( SketchAPI_Rectangle::SketchAPI_Rectangle( const std::shared_ptr & theFeature, double theX1, double theY1, double theX2, double theY2) -: SketchAPI_SketchEntity(theFeature) + : SketchAPI_SketchEntity(theFeature) { if (initialize()) { setByCoordinates(theX1, theY1, theX2, theY2); @@ -45,7 +47,7 @@ SketchAPI_Rectangle::SketchAPI_Rectangle( const std::shared_ptr & theFeature, const std::shared_ptr & theStartPoint, const std::shared_ptr & theEndPoint) -: SketchAPI_SketchEntity(theFeature) + : SketchAPI_SketchEntity(theFeature) { if (initialize()) { setByPoints(theStartPoint, theEndPoint); @@ -87,3 +89,34 @@ std::list > SketchAPI_Rectangle::lines() aFeatures.push_back(ModelAPI_Feature::feature(*anIt)); return SketchAPI_SketchEntity::wrap(aFeatures); } + +//================================================================================================== +void SketchAPI_Rectangle::dump(ModelHighAPI_Dumper& theDumper) const +{ + + FeaturePtr aBase = feature(); + + /// do not dump sub-features eg: lines and lines constraints + AttributeRefListPtr noToDumpList = aBase->reflist(SketchPlugin_Rectangle::NOT_TO_DUMP_LIST_ID()); + for( int i = 0; i < noToDumpList->size(); i++){ + theDumper.doNotDumpFeature(std::dynamic_pointer_cast(noToDumpList->object(i))); + } + + if (isCopy()) + return; // no need to dump copied feature + + const std::string& aSketchName = theDumper.parentName(aBase); + + AttributeSelectionPtr anExternal = aBase->selection(SketchPlugin_SketchEntity::EXTERNAL_ID()); + if (anExternal->context()) { + // rectangle is external + theDumper << aBase << " = " << aSketchName << ".addRectangle(" << anExternal << ")" << std::endl; + } else { + // rectangle given by start and end points + theDumper << aBase << " = " << aSketchName << ".addRectangle(" + << startPoint() << ", " << endPoint() << ")" << std::endl; + } + // dump "auxiliary" flag if necessary + SketchAPI_SketchEntity::dump(theDumper); + +} diff --git a/src/SketchAPI/SketchAPI_Rectangle.h b/src/SketchAPI/SketchAPI_Rectangle.h index 0c523fb5d..a1e6fea6b 100644 --- a/src/SketchAPI/SketchAPI_Rectangle.h +++ b/src/SketchAPI/SketchAPI_Rectangle.h @@ -24,6 +24,8 @@ #include "SketchAPI.h" #include "SketchAPI_SketchEntity.h" +#include "SketchPlugin_Rectangle.h" + //-------------------------------------------------------------------------------------- class ModelHighAPI_Selection; //-------------------------------------------------------------------------------------- @@ -50,10 +52,10 @@ public: SKETCHAPI_EXPORT virtual ~SketchAPI_Rectangle(); - INTERFACE_3("SketchRectangle", - startPoint, "RectStartPoint", GeomDataAPI_Point2D, /** Start point */, - endPoint, "RectEndPoint", GeomDataAPI_Point2D, /** End point */, - linesList, "RectangleList", ModelAPI_AttributeRefList, /** Lines list */ + INTERFACE_3(SketchPlugin_Rectangle::ID(), + startPoint, SketchPlugin_Rectangle::START_ID(), GeomDataAPI_Point2D, /** Start point */, + endPoint, SketchPlugin_Rectangle::END_ID(), GeomDataAPI_Point2D, /** End point */, + linesList, SketchPlugin_Rectangle::LINES_LIST_ID(), ModelAPI_AttributeRefList, /** Lines list */ ) /// Set by coordinates @@ -67,7 +69,10 @@ public: /// List of lines composing rectangle SKETCHAPI_EXPORT std::list > lines() const; -}; + + SKETCHAPI_EXPORT + virtual void dump(ModelHighAPI_Dumper& theDumper) const; + }; //! Pointer on Rectangle object typedef std::shared_ptr RectanglePtr; diff --git a/src/SketchAPI/SketchAPI_Sketch.cpp b/src/SketchAPI/SketchAPI_Sketch.cpp index 5f60ae2a7..db60b1826 100644 --- a/src/SketchAPI/SketchAPI_Sketch.cpp +++ b/src/SketchAPI/SketchAPI_Sketch.cpp @@ -67,6 +67,7 @@ #include "SketchAPI_MacroCircle.h" #include "SketchAPI_MacroEllipse.h" #include "SketchAPI_MacroEllipticArc.h" +#include "SketchAPI_MacroRectangle.h" #include "SketchAPI_Mirror.h" #include "SketchAPI_Offset.h" #include "SketchAPI_Point.h" @@ -424,7 +425,23 @@ std::shared_ptr SketchAPI_Sketch::addRectangle( compositeFeature()->addFeature(SketchAPI_Rectangle::ID()); return RectanglePtr(new SketchAPI_Rectangle(aFeature, theStartPoint, theEndPoint)); } - +/* +std::shared_ptr SketchAPI_Sketch::addRectangle( + double theX1, double theY1, double theX2, double theY2, bool thePoint2IsCenter) +{ + std::shared_ptr aFeature = + compositeFeature()->addFeature(SketchAPI_MacroRectangle::ID()); + return MacroRectanglePtr(new SketchAPI_MacroRectangle(aFeature, theX1, theY1, theX2, theY2, thePoint2IsCenter)); +} +std::shared_ptr SketchAPI_Sketch::addRectangle( + const std::shared_ptr & theStartPoint, + const std::shared_ptr & theEndPoint, bool theEndPointIsCenter) +{ + std::shared_ptr aFeature = + compositeFeature()->addFeature(SketchAPI_MacroRectangle::ID()); + return MacroRectanglePtr(new SketchAPI_MacroRectangle(aFeature, theStartPoint, theEndPoint, theEndPointIsCenter)); +} +*/ //-------------------------------------------------------------------------------------- std::shared_ptr SketchAPI_Sketch::addCircle(double theCenterX, double theCenterY, @@ -1588,6 +1605,34 @@ void SketchAPI_Sketch::dump(ModelHighAPI_Dumper& theDumper) const std::list > aFaces; edgesOfSketchFaces(aCompFeat, aFaces); + /// remove faces that must not be dumped + std::vector< std::list>::iterator> aFacesToRemove; + for(auto itFaces = aFaces.begin(); itFaces != aFaces.end(); ++itFaces) + { + auto & facesGroup = *itFaces; + std::vector::iterator> subFacestoRemove; + for(auto itGroup = facesGroup.begin(); itGroup != facesGroup.end(); ++itGroup) + { + FeaturePtr aFeature = ModelAPI_Feature::feature(*itGroup); + if(theDumper.isDumped(aFeature)){ + subFacestoRemove.push_back(itGroup); + } + } + for(auto itGroup :subFacestoRemove){ + facesGroup.erase(itGroup); + } + + if(!facesGroup.size()){ + aFacesToRemove.push_back(itFaces); + } + } + for(auto itFaces :aFacesToRemove){ + aFaces.erase(itFaces); + } + + if(!aFaces.size()) + return; + const std::string& aSketchName = theDumper.name(aBase); std::string aMethodName(".changeFacesOrder"); std::string aSpaceShift(aSketchName.size() + aMethodName.size(), ' '); diff --git a/src/SketchAPI/SketchAPI_Sketch.h b/src/SketchAPI/SketchAPI_Sketch.h index 11a263908..d26472f28 100644 --- a/src/SketchAPI/SketchAPI_Sketch.h +++ b/src/SketchAPI/SketchAPI_Sketch.h @@ -56,6 +56,7 @@ class SketchAPI_Projection; class SketchAPI_Rectangle; class SketchAPI_Rotation; class SketchAPI_Translation; +class SketchAPI_MacroRectangle; //-------------------------------------------------------------------------------------- typedef std::pair, ModelHighAPI_RefAttr> PointOrReference; //-------------------------------------------------------------------------------------- @@ -178,7 +179,16 @@ public: std::shared_ptr addRectangle( const std::shared_ptr & theStartPoint, const std::shared_ptr & theEndPoint); - + /// Add rectangle + /*SKETCHAPI_EXPORT + std::shared_ptr addRectangle( + double theX1, double theY1, double theX2, double theY2, bool thePoint2IsCenter); + /// Add rectangle + SKETCHAPI_EXPORT + std::shared_ptr addRectangle( + const std::shared_ptr & theStartPoint, + const std::shared_ptr & theEndPoint, bool theEndPointIsCenter); +*/ /// Add circle SKETCHAPI_EXPORT std::shared_ptr addCircle( diff --git a/src/SketchAPI/SketchAPI_swig.h b/src/SketchAPI/SketchAPI_swig.h index e43232dd3..3c7d0f2dc 100644 --- a/src/SketchAPI/SketchAPI_swig.h +++ b/src/SketchAPI/SketchAPI_swig.h @@ -43,6 +43,7 @@ #include "SketchAPI_Point.h" #include "SketchAPI_Projection.h" #include "SketchAPI_Rectangle.h" + #include "SketchAPI_MacroRectangle.h" #include "SketchAPI_Rotation.h" #include "SketchAPI_Translation.h" diff --git a/src/SketchPlugin/CMakeLists.txt b/src/SketchPlugin/CMakeLists.txt index b5cbdd3d3..ee77bf732 100644 --- a/src/SketchPlugin/CMakeLists.txt +++ b/src/SketchPlugin/CMakeLists.txt @@ -62,12 +62,14 @@ SET(PROJECT_HEADERS SketchPlugin_MacroCircle.h SketchPlugin_MacroEllipse.h SketchPlugin_MacroEllipticArc.h + SketchPlugin_MacroRectangle.h SketchPlugin_MultiRotation.h SketchPlugin_MultiTranslation.h SketchPlugin_Offset.h SketchPlugin_Plugin.h SketchPlugin_Point.h SketchPlugin_Projection.h + SketchPlugin_Rectangle.h SketchPlugin_Sketch.h SketchPlugin_SketchCopy.h SketchPlugin_SketchDrawer.h @@ -117,12 +119,14 @@ SET(PROJECT_SOURCES SketchPlugin_MacroCircle.cpp SketchPlugin_MacroEllipse.cpp SketchPlugin_MacroEllipticArc.cpp + SketchPlugin_MacroRectangle.cpp SketchPlugin_MultiRotation.cpp SketchPlugin_MultiTranslation.cpp SketchPlugin_Offset.cpp SketchPlugin_Plugin.cpp SketchPlugin_Point.cpp SketchPlugin_Projection.cpp + SketchPlugin_Rectangle.cpp SketchPlugin_Sketch.cpp SketchPlugin_SketchCopy.cpp SketchPlugin_SketchDrawer.cpp @@ -413,4 +417,4 @@ endif() ADD_RESTRICTED_TESTS( Test20204.py -) \ No newline at end of file +) diff --git a/src/SketchPlugin/SketchPlugin_MacroRectangle.cpp b/src/SketchPlugin/SketchPlugin_MacroRectangle.cpp new file mode 100644 index 000000000..fae735f63 --- /dev/null +++ b/src/SketchPlugin/SketchPlugin_MacroRectangle.cpp @@ -0,0 +1,211 @@ +// Copyright (C) 2014-2020 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 +// + +#include "SketchPlugin_MacroRectangle.h" +#include "SketchPlugin_Rectangle.h" +#include "SketchPlugin_Sketch.h" +#include "SketchPlugin_Tools.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include + +const double tolerance = 1e-7; + + +SketchPlugin_MacroRectangle::SketchPlugin_MacroRectangle() + : SketchPlugin_SketchEntity() +{ +} + +void SketchPlugin_MacroRectangle::initAttributes() +{ + data()->addAttribute(AUXILIARY_ID(), ModelAPI_AttributeBoolean::typeId()); + + data()->addAttribute(START1_ID(), GeomDataAPI_Point2D::typeId()); + data()->addAttribute(END1_ID(), GeomDataAPI_Point2D::typeId()); + + data()->addAttribute(START2_ID(), GeomDataAPI_Point2D::typeId()); + data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::typeId()); + + data()->addAttribute(TYPE_ID(), ModelAPI_AttributeString::typeId()); + data()->addAttribute(EDIT_TYPE_ID(), ModelAPI_AttributeString::typeId()); + + string(EDIT_TYPE_ID())->setValue(""); + ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EDIT_TYPE_ID()); +} + +void SketchPlugin_MacroRectangle::startPoint() +{ + std::shared_ptr aStartPoint; + if(string(TYPE_ID())->value() == START_END_POINT_TYPE_ID()) + aStartPoint = std::dynamic_pointer_cast(data()->attribute(START1_ID())); + else + aStartPoint = std::dynamic_pointer_cast(data()->attribute(START2_ID())); + if(aStartPoint->isInitialized()) + myStartPoint = std::make_shared(aStartPoint->x(), aStartPoint->y()); + else + myStartPoint.reset(); +} + +void SketchPlugin_MacroRectangle::endPoint() +{ + if(string(TYPE_ID())->value() == START_END_POINT_TYPE_ID()) + { + std::shared_ptr aEndPoint = + std::dynamic_pointer_cast(data()->attribute(END1_ID())); + if(aEndPoint->isInitialized()) + myEndPoint = std::make_shared(aEndPoint->x(), aEndPoint->y()); + else + myEndPoint.reset(); + } + else + { + /// Compute end point as the symmetric of start point w.r.t. center + std::shared_ptr aStartPoint = + std::dynamic_pointer_cast(data()->attribute(START2_ID())); + std::shared_ptr aCenterPoint = + std::dynamic_pointer_cast(data()->attribute(CENTER_ID())); + double xEnd = 2.0*aCenterPoint->x() - aStartPoint->x(); + double yEnd = 2.0*aCenterPoint->y() - aStartPoint->y(); + + if(aStartPoint ->isInitialized() && aCenterPoint->isInitialized()) + myEndPoint = std::make_shared(xEnd, yEnd); + else + myEndPoint.reset(); + } +} + + +void SketchPlugin_MacroRectangle::execute() +{ + SketchPlugin_Sketch* aSketch = sketch(); + if(!myStartPoint || !myEndPoint || !aSketch) { + return ; + } + + /// create a rectangle sketch + FeaturePtr myRectangleFeature = aSketch->addFeature(SketchPlugin_Rectangle::ID()); + if(!myRectangleFeature) + return; + + std::dynamic_pointer_cast( + myRectangleFeature->attribute(SketchPlugin_Rectangle::START_ID()))->setValue(myStartPoint->x(), + myStartPoint->y()); + std::dynamic_pointer_cast( + myRectangleFeature->attribute(SketchPlugin_Rectangle::END_ID()))->setValue(myEndPoint->x(), + myEndPoint->y()); + + myRectangleFeature->boolean(SketchPlugin_Rectangle::AUXILIARY_ID()) + ->setValue(boolean(AUXILIARY_ID())->value()); + myRectangleFeature->execute(); +} + +void SketchPlugin_MacroRectangle::attributeChanged(const std::string& theID) +{ + if(theID == TYPE_ID()) { + SketchPlugin_Tools::resetAttribute(this, START1_ID()); + SketchPlugin_Tools::resetAttribute(this, END1_ID()); + SketchPlugin_Tools::resetAttribute(this, CENTER_ID()); + SketchPlugin_Tools::resetAttribute(this, START2_ID()); + } + else if (theID == START1_ID() || theID == END1_ID() || + theID == START2_ID() || theID == CENTER_ID()) + { + // update points + startPoint(); + endPoint(); + } + bool aWasBlocked = data()->blockSendAttributeUpdated(true); + data()->blockSendAttributeUpdated(aWasBlocked, false); +} + +AISObjectPtr SketchPlugin_MacroRectangle::getAISObject(AISObjectPtr thePrevious) +{ + SketchPlugin_Sketch* aSketch = sketch(); + + if(!aSketch || !myEndPoint || ! myStartPoint) + return AISObjectPtr(); + + std::vector aX = {myStartPoint->x(), myStartPoint->x(), myEndPoint->x(), myEndPoint->x()}; + std::vector aY = {myStartPoint->y(), myEndPoint->y(), myEndPoint->y(), myStartPoint->y()}; + + std::list > aShapes; + /// Update coordinates of rectangle lines + + std::set createdPointIndex; + for(unsigned i = 0; i < 4; i++) + { + std::shared_ptr theStart(aSketch->to3D(aX[(i+3)%4], aY[(i+3)%4])); + std::shared_ptr theEnd(aSketch->to3D(aX[i], aY[i])); + GeomShapePtr aLine = GeomAlgoAPI_EdgeBuilder::line(theStart, theEnd); + + if(aLine) + { + aShapes.push_back(aLine); + if(createdPointIndex.insert(i).second){ + GeomShapePtr aPointShape = GeomAlgoAPI_PointBuilder::vertex(theStart); + aShapes.push_back(aPointShape); + } + if(createdPointIndex.insert((i+1)%4).second){ + GeomShapePtr aPointShape = GeomAlgoAPI_PointBuilder::vertex(theEnd); + aShapes.push_back(aPointShape); + } + } + } + + if(string(TYPE_ID())->value() == START_CENTER_POINT_TYPE_ID()){ + /// draw a line start->center + std::shared_ptr aCenterPoint = + std::dynamic_pointer_cast(data()->attribute(CENTER_ID())); + + std::shared_ptr theStart(aSketch->to3D(myStartPoint->x(), myStartPoint->y())); + std::shared_ptr theEnd(aSketch->to3D(aCenterPoint->x(), aCenterPoint->y())); + GeomShapePtr aLine = GeomAlgoAPI_EdgeBuilder::line(theStart, theEnd); + if(aLine) + aShapes.push_back(aLine); + } + + // Compute a rectangle in 3D view. + + std::shared_ptr aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes); + AISObjectPtr anAIS = thePrevious; + if(!anAIS.get()) { + anAIS.reset(new GeomAPI_AISObject()); + } + anAIS->createShape(aCompound); + + // Modify attributes + SketchPlugin_Tools::customizeFeaturePrs(anAIS, boolean(AUXILIARY_ID())->value()); + + return anAIS; +} diff --git a/src/SketchPlugin/SketchPlugin_MacroRectangle.h b/src/SketchPlugin/SketchPlugin_MacroRectangle.h new file mode 100644 index 000000000..fc196f98f --- /dev/null +++ b/src/SketchPlugin/SketchPlugin_MacroRectangle.h @@ -0,0 +1,129 @@ +// Copyright (C) 2014-2020 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 +// + +#ifndef SketchPlugin_MacroRectangle_H_ +#define SketchPlugin_MacroRectangle_H_ + +#include "SketchPlugin.h" +#include +#include + +class GeomAPI_Pnt2d; + +/**\class SketchPlugin_MacroRectangle + * \ingroup Plugins + * \brief Feature for creation of the new Rectangle in Sketch. + */ +class SketchPlugin_MacroRectangle: public SketchPlugin_SketchEntity, public GeomAPI_IPresentable +{ +public: + /// Rectangle feature kind + inline static const std::string& ID() + { + static const std::string ID("SketchMacroRectangle"); + return ID; + } + + inline static const std::string& TYPE_ID() + { + static const std::string ID("rectangle_type"); + return ID; + } + + inline static const std::string& EDIT_TYPE_ID() + { + static const std::string ID("edit_rectangle_type"); + return ID; + } + + inline static const std::string& START_END_POINT_TYPE_ID() + { + static const std::string ID("rectangle_type_by_start_and_end_points"); + return ID; + } + + inline static const std::string& START_CENTER_POINT_TYPE_ID() + { + static const std::string ID("rectangle_type_by_start_and_center_points"); + return ID; + } + + /// 2D point - start point of the Rectangle + inline static const std::string& START1_ID() + { + static const std::string ID("rectangle_start_point1"); + return ID; + } + /// 2D point - end point of the Rectangle + inline static const std::string& END1_ID() + { + static const std::string ID("rectangle_end_point1"); + return ID; + } + /// 2D point - start point of the second Rectangle type + inline static const std::string& START2_ID() + { + static const std::string ID("rectangle_start_point2"); + return ID; + } + + /// 2D point - center point of the second Rectangle type + inline static const std::string& CENTER_ID() + { + static const std::string ID("rectangle_center_point"); + return ID; + } + + /// Returns the kind of a feature + SKETCHPLUGIN_EXPORT virtual const std::string& getKind() + { + static std::string MY_KIND = SketchPlugin_MacroRectangle::ID(); + return MY_KIND; + } + + /// Called on change of any argument-attribute of this object + SKETCHPLUGIN_EXPORT virtual void attributeChanged(const std::string& theID); + + /// Creates a new part document if needed + SKETCHPLUGIN_EXPORT virtual void execute(); + + /// Use plugin manager for features creation + SketchPlugin_MacroRectangle(); + + SKETCHPLUGIN_EXPORT virtual bool isMacro() const + {return true;} + + SKETCHPLUGIN_EXPORT virtual void initAttributes(); + + SKETCHPLUGIN_EXPORT virtual bool isPreviewNeeded() const {return false;} + + /// Returns the AIS preview + virtual AISObjectPtr getAISObject(AISObjectPtr thePrevious); + +private: + + std::shared_ptr myStartPoint; + std::shared_ptr myEndPoint; + + void startPoint(); + void endPoint(); + FeaturePtr createRectangle(); +}; + +#endif diff --git a/src/SketchPlugin/SketchPlugin_Plugin.cpp b/src/SketchPlugin/SketchPlugin_Plugin.cpp index b71ff3c37..49d1328ed 100644 --- a/src/SketchPlugin/SketchPlugin_Plugin.cpp +++ b/src/SketchPlugin/SketchPlugin_Plugin.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -49,6 +50,7 @@ #include #include #include +#include #include #include #include @@ -287,7 +289,12 @@ FeaturePtr SketchPlugin_Plugin::createFeature(std::string theFeatureID) return FeaturePtr(new SketchPlugin_SketchCopy); } else if (theFeatureID == SketchPlugin_Offset::ID()) { return FeaturePtr(new SketchPlugin_Offset); + }else if (theFeatureID == SketchPlugin_MacroRectangle::ID()) { + return FeaturePtr(new SketchPlugin_MacroRectangle); + }else if (theFeatureID == SketchPlugin_Rectangle::ID()) { + return FeaturePtr(new SketchPlugin_Rectangle); } + // feature of such kind is not found return FeaturePtr(); } @@ -366,8 +373,10 @@ std::shared_ptr SketchPlugin_Plugin aMsg->setState(SketchPlugin_ConstraintDistanceVertical::ID(), aHasSketchPlane); aMsg->setState(SketchPlugin_CurveFitting::ID(), aHasSketchPlane); aMsg->setState(SketchPlugin_Offset::ID(), aHasSketchPlane); + aMsg->setState(SketchPlugin_MacroRectangle::ID(), aHasSketchPlane); + aMsg->setState(SketchPlugin_Rectangle::ID(), aHasSketchPlane); // SketchRectangle is a python feature, so its ID is passed just as a string - aMsg->setState("SketchRectangle", aHasSketchPlane); + //aMsg->setState("SketchRectangle", aHasSketchPlane); } } return aMsg; diff --git a/src/SketchPlugin/SketchPlugin_Rectangle.cpp b/src/SketchPlugin/SketchPlugin_Rectangle.cpp new file mode 100644 index 000000000..3933f997c --- /dev/null +++ b/src/SketchPlugin/SketchPlugin_Rectangle.cpp @@ -0,0 +1,245 @@ +// Copyright (C) 2014-2020 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 +// + +#include "SketchPlugin_Rectangle.h" +#include "SketchPlugin_Sketch.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include + +const double tolerance = 1e-7; + + +SketchPlugin_Rectangle::SketchPlugin_Rectangle() + : SketchPlugin_SketchEntity() +{ +} + +void SketchPlugin_Rectangle::initDerivedClassAttributes() +{ + data()->addAttribute(START_ID(), GeomDataAPI_Point2D::typeId()); + data()->addAttribute(END_ID(), GeomDataAPI_Point2D::typeId()); + data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId()); + ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID()); + data()->addAttribute(LINES_LIST_ID(), ModelAPI_AttributeRefList::typeId()); + ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), LINES_LIST_ID()); + data()->addAttribute(ISHV_LIST_ID(), ModelAPI_AttributeIntArray::typeId()); + + data()->addAttribute(NOT_TO_DUMP_LIST_ID(), ModelAPI_AttributeRefList::typeId()); + ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), NOT_TO_DUMP_LIST_ID()); + + AttributeIntArrayPtr isHVList = intArray(ISHV_LIST_ID()); + isHVList->setSize(4, false); + for(int i = 0; i< 4;) + isHVList->setValue(i++, 0, false); + } + +void SketchPlugin_Rectangle::updateLines() +{ + // Retrieving list of already created lines + AttributeRefListPtr aLinesList = reflist(LINES_LIST_ID()); + unsigned aNbLines = aLinesList->size(); + std::shared_ptr aStartPoint = + std::dynamic_pointer_cast(data()->attribute(START_ID())); + std::shared_ptr aEndPoint = + std::dynamic_pointer_cast(data()->attribute(END_ID())); + + double xMin = std::min(aStartPoint->x(), aEndPoint->x()); + double xMax = std::max(aStartPoint->x(), aEndPoint->x()); + double yMin = std::min(aStartPoint->y(), aEndPoint->y()); + double yMax = std::max(aStartPoint->y(), aEndPoint->y()); + std::vector aX = {xMin, xMax, xMax, xMin}; + std::vector aY = {yMin, yMin, yMax, yMax}; + + bool anAuxiliary = data()->boolean(AUXILIARY_ID())->value(); + + /// Update coordinates of rectangle lines + for(unsigned i = 0; i < aNbLines; i++) + { + FeaturePtr aLine = std::dynamic_pointer_cast(aLinesList->object(i)); + std::shared_ptr aLineStart = + std::dynamic_pointer_cast(aLine->attribute(SketchPlugin_Line::START_ID())); + std::shared_ptr aLineEnd = + std::dynamic_pointer_cast(aLine->attribute(SketchPlugin_Line::END_ID())); + aLineStart->setValue(aX[i], aY[i]); + aLineEnd->setValue(aX[(i+1)%4], aY[(i+1)%4]); + aLine->data()->boolean(AUXILIARY_ID())->setValue(anAuxiliary); + } +} + +void SketchPlugin_Rectangle::execute() +{ + SketchPlugin_Sketch* aSketch = sketch(); + if(!aSketch) { + return; + } + + // Compute a Rectangle in 3D view. + std::shared_ptr aStartAttr = + std::dynamic_pointer_cast(data()->attribute(START_ID())); + + std::shared_ptr aEndAttr = + std::dynamic_pointer_cast(data()->attribute(END_ID())); + + if(!aStartAttr->isInitialized() || !aEndAttr->isInitialized()) { + return; + } + + AttributeRefListPtr aLinesList = reflist(LINES_LIST_ID()); + unsigned aNbLines = aLinesList->size(); + AttributeIntArrayPtr isHVList = intArray(ISHV_LIST_ID()); + AttributeRefListPtr aNotToDumpList = reflist(NOT_TO_DUMP_LIST_ID()); + + if(aNbLines == 1) + { + /// Create 1-4 lines to compose the rectangle + for( unsigned i = 0; i < 3; i++) + { + FeaturePtr aLine = aSketch->addFeature(SketchPlugin_Line::ID()); + aLinesList->append(aLine); + aNotToDumpList->append(aLine); + } + updateLines(); + aNbLines = aLinesList->size(); + /// Create constraints to keep the rectangle + for( unsigned i = 0; i < aNbLines; i++) + { + FeaturePtr aLine = std::dynamic_pointer_cast(aLinesList->object(i)); + /// connect neighbor lines by coincidence + unsigned iPrev = (i+3)%4; + FeaturePtr aPrevLine = std::dynamic_pointer_cast(aLinesList->object(iPrev)); + FeaturePtr aCoincidence = aSketch->addFeature(SketchPlugin_ConstraintCoincidence::ID()); + aNotToDumpList->append(aCoincidence); + AttributeRefAttrPtr aRefAttrA = aCoincidence->refattr(SketchPlugin_ConstraintCoincidence::ENTITY_A()); + AttributeRefAttrPtr aRefAttrB = aCoincidence->refattr(SketchPlugin_ConstraintCoincidence::ENTITY_B()); + aRefAttrA->setAttr(aPrevLine->attribute(SketchPlugin_Line::END_ID())); + aRefAttrB->setAttr(aLine->attribute(SketchPlugin_Line::START_ID())); + } + + /// Update coordinates of created lines + updateLines(); + } + + /// Add horizontal and vertical constraint for the lines which already have result + for(unsigned i = 0; i< aNbLines; i++) + { + if(isHVList->value(i)) + continue; + FeaturePtr aLine = std::dynamic_pointer_cast(aLinesList->object(i)); + ResultPtr aLineResult = aLine->lastResult(); + if(!aLineResult.get()) + continue; + std::string aHVName = SketchPlugin_ConstraintHorizontal::ID(); + if(i%2) + aHVName = SketchPlugin_ConstraintVertical::ID(); + FeaturePtr aHVConstraint = aSketch->addFeature(aHVName); + aNotToDumpList->append(aHVConstraint); + AttributeRefAttrPtr aRefAttrA = aHVConstraint->refattr(SketchPlugin_ConstraintCoincidence::ENTITY_A()); + aRefAttrA->setObject(aLine->lastResult()); + isHVList->setValue(i, 1, false); + } + + double aDiag = std::pow(aStartAttr->x() - aEndAttr->x(), 2.0); + aDiag += std::pow(aStartAttr->y() - aEndAttr->y(), 2.0); + aDiag = std::sqrt(aDiag); + if(aDiag < tolerance) { + return; + } + + // store results. + + GeomShapePtr aRectangleShape; + ListOfShape aSubs; + + for(unsigned i = 0; i< aNbLines; i++) + { + FeaturePtr aLine = std::dynamic_pointer_cast(aLinesList->object(i)); + ResultPtr aLineResult = aLine->lastResult(); + if(!aLineResult.get()) + continue; + aSubs.push_back(aLineResult->shape()); + } + + aRectangleShape = aSubs.empty() ? GeomShapePtr() : GeomAlgoAPI_CompoundBuilder::compound(aSubs); + + std::shared_ptr aResult = document()->createConstruction(data(), 0); + aResult->setShape(aRectangleShape); + aResult->setIsInHistory(false); + setResult(aResult, 1); +} + + +bool SketchPlugin_Rectangle::isFixed() { + return data()->selection(EXTERNAL_ID())->context().get() != NULL; +} + +void SketchPlugin_Rectangle::attributeChanged(const std::string& theID) +{ + if (theID == START_ID() || theID == END_ID()) + { + AttributeRefListPtr aLinesList = reflist(LINES_LIST_ID()); + AttributeRefListPtr aNotToDumpList = reflist(NOT_TO_DUMP_LIST_ID()); + unsigned aNbLines = aLinesList->size(); + if(aNbLines == 0) + { + SketchPlugin_Sketch* aSketch = sketch(); + if(!aSketch) { + return; + } + /// Create first line to be able to create a coincidence with selected point/feature + FeaturePtr aLine = aSketch->addFeature(SketchPlugin_Line::ID()); + aLinesList->append(aLine); + aNotToDumpList->append(aLine); + } + + std::shared_ptr aStartPoint = + std::dynamic_pointer_cast(data()->attribute(START_ID())); + std::shared_ptr aEndPoint = + std::dynamic_pointer_cast(data()->attribute(END_ID())); + + if (aStartPoint->isInitialized() && aEndPoint->isInitialized()) + updateLines(); + } + if (theID == AUXILIARY_ID()) + { + bool anAuxiliary = data()->boolean(AUXILIARY_ID())->value(); + AttributeRefListPtr aLinesList = reflist(LINES_LIST_ID()); + unsigned aNbLines = aLinesList->size(); + + /// Update coordinates of rectangle lines + for(unsigned i = 0; i < aNbLines; i++) + { + FeaturePtr aLine = std::dynamic_pointer_cast(aLinesList->object(i)); + aLine->data()->boolean(AUXILIARY_ID())->setValue(anAuxiliary); + } + } +} diff --git a/src/SketchPlugin/SketchPlugin_Rectangle.h b/src/SketchPlugin/SketchPlugin_Rectangle.h new file mode 100644 index 000000000..7d3e2f464 --- /dev/null +++ b/src/SketchPlugin/SketchPlugin_Rectangle.h @@ -0,0 +1,103 @@ +// Copyright (C) 2014-2020 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 +// + +#ifndef SketchPlugin_Rectangle_H_ +#define SketchPlugin_Rectangle_H_ + +#include "SketchPlugin.h" +#include "SketchPlugin_SketchEntity.h" +#include "SketchPlugin_Sketch.h" + +/**\class SketchPlugin_Rectangle + * \ingroup Plugins + * \brief Feature for creation of the new Rectangle in Sketch. + */ +class SketchPlugin_Rectangle: public SketchPlugin_SketchEntity + +{ + public: + /// Rectangle feature kind + inline static const std::string& ID() + { + static const std::string ID("SketchRectangle"); + return ID; + } + + /// 2D point - start point of the Rectangle + inline static const std::string& START_ID() + { + static const std::string ID("rectangle_start_point"); + return ID; + } + + /// 2D point - end point of the Rectangle + inline static const std::string& END_ID() + { + static const std::string ID("rectangle_end_point"); + return ID; + } + + /// 2D point - list of Rectangle lines + inline static const std::string& LINES_LIST_ID() + { + static const std::string ID("RectangleList"); + return ID; + } + + inline static const std::string& ISHV_LIST_ID() + { + static const std::string ID("IsHVList"); + return ID; + } + + inline static const std::string& NOT_TO_DUMP_LIST_ID() + { + static const std::string ID("NotToDumpList"); + return ID; + } + + /// Returns the kind of a feature + SKETCHPLUGIN_EXPORT virtual const std::string& getKind() + { + static std::string MY_KIND = SketchPlugin_Rectangle::ID(); + return MY_KIND; + } + + /// Returns true is sketch element is under the rigid constraint + SKETCHPLUGIN_EXPORT virtual bool isFixed(); + + /// Called on change of any argument-attribute of this object + SKETCHPLUGIN_EXPORT virtual void attributeChanged(const std::string& theID); + + /// Creates a new part document if needed + SKETCHPLUGIN_EXPORT virtual void execute(); + + /// Use plugin manager for features creation + SketchPlugin_Rectangle(); + +protected: + /// \brief Initializes attributes of derived class. + virtual void initDerivedClassAttributes(); + +private: + /// \brief updateLines crates lines from start and en points + void updateLines(); +}; + +#endif diff --git a/src/SketchPlugin/plugin-Sketch.xml b/src/SketchPlugin/plugin-Sketch.xml index 824e6281b..ea64ddfe7 100644 --- a/src/SketchPlugin/plugin-Sketch.xml +++ b/src/SketchPlugin/plugin-Sketch.xml @@ -7,7 +7,7 @@ SketchCircle SketchMacroCircle SketchArc SketchMacroArc SketchEllipse SketchMacroEllipse SketchEllipticArc SketchMacroEllipticArc SketchBSpline SketchMacroBSpline SketchMacroBSplinePeriodic SketchBSplinePeriodic - SketchRectangle + SketchRectangle SketchMacroRectangle SketchProjection SketchCurveFitting SketchConstraintLength SketchConstraintRadius SketchConstraintDistance SketchConstraintDistanceHorizontal SketchConstraintDistanceVertical @@ -52,6 +52,78 @@ + + + + + + + + + + + + + + + + + + + + + + + -- 2.30.2