From d53cd037401ca37f75e19e59201e1b816f251c6a Mon Sep 17 00:00:00 2001 From: Artem Zhidkov Date: Mon, 29 Mar 2021 22:15:04 +0300 Subject: [PATCH] Rework Sketch Rectangle feature. --- src/ModelHighAPI/ModelHighAPI_RefAttr.h | 3 + src/PythonAddons/CMakeLists.txt | 6 +- .../Test/TestRectangleCentered.py | 75 ++++ src/PythonAddons/addons_Features.py | 9 +- src/PythonAddons/addons_Features.xml.in | 2 +- src/PythonAddons/macros/rectangle/feature.py | 139 ++++++- .../rectangle/icons/rectangle_center_end.png | Bin 0 -> 808 bytes .../rectangle/icons/rectangle_start_end.png | Bin 0 -> 379 bytes src/PythonAddons/macros/rectangle/widget.xml | 46 ++- src/SketchAPI/CMakeLists.txt | 2 - src/SketchAPI/SketchAPI.i | 2 - src/SketchAPI/SketchAPI_MacroRectangle.cpp | 151 ------- src/SketchAPI/SketchAPI_MacroRectangle.h | 95 ----- src/SketchAPI/SketchAPI_Rectangle.cpp | 80 +--- src/SketchAPI/SketchAPI_Rectangle.h | 33 +- src/SketchAPI/SketchAPI_Sketch.cpp | 59 +-- src/SketchAPI/SketchAPI_Sketch.h | 18 +- src/SketchAPI/SketchAPI_swig.h | 1 - src/SketchPlugin/CMakeLists.txt | 4 - .../SketchPlugin_MacroRectangle.cpp | 281 ------------- .../SketchPlugin_MacroRectangle.h | 157 -------- src/SketchPlugin/SketchPlugin_Plugin.cpp | 11 +- src/SketchPlugin/SketchPlugin_Rectangle.cpp | 369 ------------------ src/SketchPlugin/SketchPlugin_Rectangle.h | 124 ------ src/SketchPlugin/doc/examples/rectangle.py | 5 +- src/SketchPlugin/plugin-Sketch.xml | 78 +--- 26 files changed, 323 insertions(+), 1427 deletions(-) create mode 100644 src/PythonAddons/Test/TestRectangleCentered.py create mode 100644 src/PythonAddons/macros/rectangle/icons/rectangle_center_end.png create mode 100644 src/PythonAddons/macros/rectangle/icons/rectangle_start_end.png delete mode 100644 src/SketchAPI/SketchAPI_MacroRectangle.cpp delete mode 100644 src/SketchAPI/SketchAPI_MacroRectangle.h delete mode 100644 src/SketchPlugin/SketchPlugin_MacroRectangle.cpp delete mode 100644 src/SketchPlugin/SketchPlugin_MacroRectangle.h delete mode 100644 src/SketchPlugin/SketchPlugin_Rectangle.cpp delete mode 100644 src/SketchPlugin/SketchPlugin_Rectangle.h diff --git a/src/ModelHighAPI/ModelHighAPI_RefAttr.h b/src/ModelHighAPI/ModelHighAPI_RefAttr.h index 5a50446ea..97c8f85f6 100644 --- a/src/ModelHighAPI/ModelHighAPI_RefAttr.h +++ b/src/ModelHighAPI/ModelHighAPI_RefAttr.h @@ -72,6 +72,9 @@ public: MODELHIGHAPI_EXPORT void fillMessage(const std::shared_ptr& theMessage) const; + std::shared_ptr attr() const { return myAttribute; } + std::shared_ptr object() const { return myObject; } + private: enum VariantType { VT_ATTRIBUTE, VT_OBJECT } myVariantType; std::shared_ptr myAttribute; diff --git a/src/PythonAddons/CMakeLists.txt b/src/PythonAddons/CMakeLists.txt index c81edf33f..571a2810f 100644 --- a/src/PythonAddons/CMakeLists.txt +++ b/src/PythonAddons/CMakeLists.txt @@ -37,8 +37,7 @@ 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/compoundVertices DESTINATION ${SHAPER_INSTALL_ADDONS}/macros) -INSTALL(DIRECTORY macros/importParameters DESTINATION ${SHAPER_INSTALL_ADDONS}/macros) +INSTALL(DIRECTORY macros DESTINATION ${SHAPER_INSTALL_ADDONS}) INSTALL(DIRECTORY macros/rectangle/icons/ DESTINATION ${SHAPER_INSTALL_XML_RESOURCES}/icons/Addons) INSTALL(DIRECTORY macros/compoundVertices/icons/ DESTINATION ${SHAPER_INSTALL_XML_RESOURCES}/icons/Addons) @@ -48,7 +47,8 @@ INSTALL(FILES ${TEXT_RESOURCES} DESTINATION ${SHAPER_INSTALL_XML_RESOURCES}) INCLUDE(UnitTest) ADD_UNIT_TESTS( - #TestRectangle.py + TestRectangle.py + TestRectangleCentered.py TestcompoundVertices.py TestimportParameters.py ) diff --git a/src/PythonAddons/Test/TestRectangleCentered.py b/src/PythonAddons/Test/TestRectangleCentered.py new file mode 100644 index 000000000..ff47827f0 --- /dev/null +++ b/src/PythonAddons/Test/TestRectangleCentered.py @@ -0,0 +1,75 @@ +# Copyright (C) 2021 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 +from salome.shaper import geom + +from SketchAPI import * + +import math + +def checkRectangle(lines, center, corner, tolerance = 1.e-7): + dx = corner.x() - center.x() + dy = corner.y() - center.y() + points = [geom.Pnt2d(center.x() - dx, center.y() - dy), + geom.Pnt2d(center.x() - dx, center.y() + dy), + geom.Pnt2d(center.x() + dx, center.y() + dy), + geom.Pnt2d(center.x() + dx, center.y() - dy)] + for i in range(0, 4): + line = SketchAPI_Line(lines[i]) + sp = line.startPoint().pnt() + sp_ref = points[i-1] + assert(sp.distance(sp_ref) <= tolerance) + ep = line.endPoint().pnt() + ep_ref = points[i] + assert(ep.distance(ep_ref) <= tolerance) + + +model.begin() +partSet = model.moduleDocument() +part = model.addPart(partSet).document() + +centerPoint = geom.Pnt2d(50, 50) +endPoint = geom.Pnt2d(100, 100) + +sketch = model.addSketch(part, model.defaultPlane("XOY")) +rectangle_1 = sketch.addRectangleCentered(centerPoint, endPoint) +lines_1 = rectangle_1.lines() +model.end() + +checkRectangle(lines_1, centerPoint, endPoint) + +model.begin() +projection_1 = sketch.addProjection(model.selection("VERTEX", "PartSet/Origin"), False) +point_1 = SketchAPI_Point(projection_1.createdFeature()) + +rectangle_2 = sketch.addRectangleCentered(point_1.coordinates(), endPoint) +lines_2 = rectangle_2.lines() +model.end() + +checkRectangle(lines_2, geom.Pnt2d(0.0, 0.0), endPoint) + +model.begin() +rectangle_3 = sketch.addRectangleCentered(SketchAPI_Line(lines_1[0]).startPoint(), SketchAPI_Line(lines_2[0]).endPoint()) +lines_3 = rectangle_3.lines() +model.end() + +checkRectangle(lines_3, SketchAPI_Line(lines_1[0]).startPoint().pnt(), SketchAPI_Line(lines_2[0]).endPoint().pnt()) + +assert(model.checkPythonDump()) diff --git a/src/PythonAddons/addons_Features.py b/src/PythonAddons/addons_Features.py index 207dd78c4..4c302a012 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,10 +43,9 @@ class PythonFeaturesPlugin(ModelAPI.ModelAPI_Plugin): """Override ModelAPI_Plugin.createFeature()""" aFeature = None - #if theFeatureID == SketchPlugin_Rectangle.ID(): - # aFeature = SketchPlugin_Rectangle().__disown__() - #el - if theFeatureID == compoundVertices.ID(): + if theFeatureID == SketchPlugin_Rectangle.ID(): + aFeature = SketchPlugin_Rectangle().__disown__() + elif 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 6235ee56e..3f3b7666d 100644 --- a/src/PythonAddons/addons_Features.xml.in +++ b/src/PythonAddons/addons_Features.xml.in @@ -1,5 +1,5 @@ - + diff --git a/src/PythonAddons/macros/rectangle/feature.py b/src/PythonAddons/macros/rectangle/feature.py index 116c22dba..9e0956408 100644 --- a/src/PythonAddons/macros/rectangle/feature.py +++ b/src/PythonAddons/macros/rectangle/feature.py @@ -64,6 +64,37 @@ class SketchPlugin_Rectangle(model.Feature): """Returns ID of list containing lines created.""" return "RectangleList" + @staticmethod + def RECTANGLE_TYPE_ID(): + """Returns ID of type of rectangle creation (by corners or by center and corner).""" + return "RectangleType" + + @staticmethod + def RECTANGLE_BY_CORNERS_ID(): + """Returns ID of creation type by opposite corners.""" + return "RectangleTypeByCorners" + + @staticmethod + def RECTANGLE_CENTERED_ID(): + """Returns ID of creation type by center point and a corner.""" + return "RectangleTypeCentered" + + @staticmethod + def CENTER_ID(): + """Returns ID of center point.""" + return "RectCenterPoint" + + @staticmethod + def CENTER_REF_ID(): + """Returns ID of the reference to the center point.""" + return "RectCenterPointRef" + + @staticmethod + def CORNER_ID(): + """Returns ID of a corner.""" + return "RectCornerPoint" + + def getKind(self): """Override Feature.getKind()""" return SketchPlugin_Rectangle.ID() @@ -81,6 +112,14 @@ class SketchPlugin_Rectangle(model.Feature): # Creating list to store lines self.data().addAttribute(self.LINES_LIST_ID(), ModelAPI.ModelAPI_AttributeRefList_typeId()) ModelAPI.ModelAPI_Session.get().validators().registerNotObligatory(self.getKind(), self.LINES_LIST_ID()) + # Type of rectangle + self.data().addAttribute(self.RECTANGLE_TYPE_ID(), ModelAPI.ModelAPI_AttributeString_typeId()) + # Center and corner of the rectangle + self.data().addAttribute(self.CENTER_ID(), GeomDataAPI.GeomDataAPI_Point2D_typeId()) + self.data().addAttribute(self.CORNER_ID(), GeomDataAPI.GeomDataAPI_Point2D_typeId()) + + self.data().addAttribute(self.CENTER_REF_ID(), ModelAPI.ModelAPI_AttributeRefAttr_typeId()) + ModelAPI.ModelAPI_Session.get().validators().registerNotObligatory(self.getKind(), self.CENTER_REF_ID()) def isMacro(self): """ @@ -102,6 +141,7 @@ class SketchPlugin_Rectangle(model.Feature): aLinesList.append(aLine) self.updateLines() aNbLines = aLinesList.size() + aStartPoints = [] # Create constraints to keep the rectangle for i in range (0, aNbLines): aLine = ModelAPI.objectToFeature(aLinesList.object(i)) @@ -115,12 +155,39 @@ class SketchPlugin_Rectangle(model.Feature): aRefAttrB = aCoincidence.refattr("ConstraintEntityB") aRefAttrA.setAttr(aPrevLine.attribute("EndPoint")) aRefAttrB.setAttr(aLine.attribute("StartPoint")) + aStartPoints.append(aLine.attribute("StartPoint")) # Flags which show horizontal or vertical constraint is build for correponding line self.__isHV = [False, False, False, False] # Update coordinates of created lines self.updateLines() + # Create auxiliary diagonals in case of centered rectangle + if self.string(self.RECTANGLE_TYPE_ID()).value() == self.RECTANGLE_CENTERED_ID(): + aDiag1 = self.__sketch.addFeature("SketchLine") + aLinesList.append(aDiag1) + aDiag2 = self.__sketch.addFeature("SketchLine") + aLinesList.append(aDiag2) + # coincidences in corners + aPoints = [aDiag1.attribute("StartPoint"), aDiag2.attribute("StartPoint"), + aDiag1.attribute("EndPoint"), aDiag2.attribute("EndPoint")] + for i in range (0, len(aPoints)): + aCoincidence = self.__sketch.addFeature("SketchConstraintCoincidence") + aRefAttrA = aCoincidence.refattr("ConstraintEntityA") + aRefAttrB = aCoincidence.refattr("ConstraintEntityB") + aRefAttrA.setAttr(aStartPoints[i]) + aRefAttrB.setAttr(aPoints[i]) + # Update coordinates of created lines + self.updateLines() + aDiag1.execute() + aDiag2.execute() + # coincidences between center point and diagonals + refPnt = self.getReferencePoint(self.refattr(self.CENTER_REF_ID())) + if refPnt is not None: + for line in [aDiag1.lastResult(), aDiag2.lastResult()]: + aCoincidence = self.__sketch.addFeature("SketchConstraintCoincidence") + aCoincidence.refattr("ConstraintEntityA").setAttr(refPnt) + aCoincidence.refattr("ConstraintEntityB").setObject(line) # Add horizontal and vertical constraint for the lines which already have result - for i in range (0, aNbLines): + for i in range (0, 4): if self.__isHV[i]: continue aLine = ModelAPI.objectToFeature(aLinesList.object(i)) @@ -136,7 +203,7 @@ class SketchPlugin_Rectangle(model.Feature): self.__isHV[i] = True def attributeChanged(self, theID): - if theID == self.START_ID() or theID == self.END_ID(): + if theID == self.START_ID() or theID == self.END_ID() or theID == self.CENTER_ID() or theID == self.CENTER_REF_ID() or theID == self.CORNER_ID(): # Search the sketch containing this rectangle self.__sketch = None aRefs = self.data().refsToMe(); @@ -156,7 +223,9 @@ class SketchPlugin_Rectangle(model.Feature): aStartPoint = GeomDataAPI.geomDataAPI_Point2D(self.attribute(self.START_ID())) aEndPoint = GeomDataAPI.geomDataAPI_Point2D(self.attribute(self.END_ID())) - if aStartPoint.isInitialized() and aEndPoint.isInitialized(): + aCenter = self.getPointByRef(self.attribute(self.CENTER_ID()), self.refattr(self.CENTER_REF_ID())) + aCorner = GeomDataAPI.geomDataAPI_Point2D(self.attribute(self.CORNER_ID())) + if (aStartPoint.isInitialized() and aEndPoint.isInitialized()) or (aCenter is not None and aCorner.isInitialized()): self.updateLines() else: self.updateStartPoint() @@ -169,17 +238,48 @@ class SketchPlugin_Rectangle(model.Feature): aLine = ModelAPI.objectToFeature(aLinesList.object(i)) aLine.data().boolean("Auxiliary").setValue(anAuxiliary) + def getReferencePoint(self, theRef): + if theRef.isObject() and theRef.object() is not None: + feature = ModelAPI.ModelAPI_Feature.feature(theRef.object()) + if feature.getKind() == "SketchPoint": + return feature.attribute("PointCoordinates") + else: + return theRef.attr() + return None + + def getPointByRef(self, thePoint, theRef): + attr = thePoint + if theRef.isInitialized(): + refPnt = self.getReferencePoint(theRef) + if refPnt is not None: + attr = refPnt + if attr is None or not attr.isInitialized(): + return None + return GeomDataAPI.geomDataAPI_Point2D(attr).pnt() def updateLines(self): # Retrieving list of already created lines aLinesList = self.reflist(self.LINES_LIST_ID()) - aNbLines = aLinesList.size() - aStartPoint = GeomDataAPI.geomDataAPI_Point2D(self.attribute(self.START_ID())) - aEndPoint = GeomDataAPI.geomDataAPI_Point2D(self.attribute(self.END_ID())) - aX = [aStartPoint.x(), aStartPoint.x(), aEndPoint.x(), aEndPoint.x()] - aY = [aStartPoint.y(), aEndPoint.y(), aEndPoint.y(), aStartPoint.y()] + aNbLines = min(aLinesList.size(), 4) + if self.string(self.RECTANGLE_TYPE_ID()).value() == self.RECTANGLE_CENTERED_ID(): + aCenter = self.getPointByRef(self.attribute(self.CENTER_ID()), self.refattr(self.CENTER_REF_ID())) + aCorner = GeomDataAPI.geomDataAPI_Point2D(self.attribute(self.CORNER_ID())) + aStartX = 2.0 * aCenter.x() - aCorner.x() + aStartY = 2.0 * aCenter.y() - aCorner.y() + aX = [aStartX, aStartX, aCorner.x(), aCorner.x()] + aY = [aStartY, aCorner.y(), aCorner.y(), aStartY] + else: + aStartPoint = GeomDataAPI.geomDataAPI_Point2D(self.attribute(self.START_ID())) + aEndPoint = GeomDataAPI.geomDataAPI_Point2D(self.attribute(self.END_ID())) + aX = [aStartPoint.x(), aStartPoint.x(), aEndPoint.x(), aEndPoint.x()] + aY = [aStartPoint.y(), aEndPoint.y(), aEndPoint.y(), aStartPoint.y()] anAuxiliary = self.data().boolean(self.AUXILIARY_ID()).value() + # do not recalculate the rectrangle after each update + wasBlocked = [] + for i in range (0, aLinesList.size()): + wasBlocked.append(aLinesList.object(i).data().blockSendAttributeUpdated(True)) + # Update coordinates of rectangle lines for i in range (0, aNbLines): aLine = ModelAPI.objectToFeature(aLinesList.object(i)) @@ -189,14 +289,33 @@ class SketchPlugin_Rectangle(model.Feature): aLineEnd.setValue(aX[i], aY[i]) aLine.data().boolean("Auxiliary").setValue(anAuxiliary) + # Update auxiliary diagonals + if self.string(self.RECTANGLE_TYPE_ID()).value() == self.RECTANGLE_CENTERED_ID(): + for i in range (aNbLines, aLinesList.size()): + aLine = ModelAPI.objectToFeature(aLinesList.object(i)) + aLineStart = GeomDataAPI.geomDataAPI_Point2D(aLine.attribute("StartPoint")) + aLineEnd = GeomDataAPI.geomDataAPI_Point2D(aLine.attribute("EndPoint")) + aLineStart.setValue(aX[i-aNbLines-1], aY[i-aNbLines-1]) + aLineEnd.setValue(aX[i-aNbLines+1], aY[i-aNbLines+1]) + aLine.data().boolean("Auxiliary").setValue(True) + + # update the rectangle + for i in range (0, aLinesList.size()): + aLinesList.object(i).data().blockSendAttributeUpdated(wasBlocked[i], True) + def updateStartPoint(self): # Retrieving list of already created lines aLinesList = self.reflist(self.LINES_LIST_ID()) aNbLines = aLinesList.size() aStartPoint = GeomDataAPI.geomDataAPI_Point2D(self.attribute(self.START_ID())) - aX = aStartPoint.x() - aY = aStartPoint.y() + if aStartPoint.isInitialized: + aX = aStartPoint.x() + aY = aStartPoint.y() + else: + aCenter = self.getPointByRef(self.attribute(self.CENTER_ID()), self.refattr(self.CENTER_REF_ID())) + aX = aCenter.x() + aY = aCenter.y() # Update coordinates of rectangle lines for i in range (0, aNbLines): diff --git a/src/PythonAddons/macros/rectangle/icons/rectangle_center_end.png b/src/PythonAddons/macros/rectangle/icons/rectangle_center_end.png new file mode 100644 index 0000000000000000000000000000000000000000..2a97c78d56c6acdf4baff685a4efa92a4892c828 GIT binary patch literal 808 zcmV+@1K0eCP)f+5oW5G8f(5RrHYNil`ctYD#nNDuu330H`<-3T?%adu~ZuS4x3 z>+HHCs~7#kFwEo4`+k`B=J5kIwNXTXlYmYFrU-}-&_RH6Jpnoh&@-+MmP$~3t+bW^ zAHy%8B=q#N#CZZh%(0fHOGRK-VNcD1G+U$9+i47M$e zW%^jPAT3jLCK#C)m8RA@vB*3h9T-^1$GGy)nWA}N?g0lK6hOyw<>nm1U&FlIXwIkr#$%Ff?)&+&|4UvkS+&4Ys+twr8weEJ^~u=+%L3uCo5c-BofD%& ziz4_*0A#??Q`K-pp4WsO00MUE*#^*E#*MpUpWhRsoggFu0E{kO|Muzoh1%-V3NX#-pw%uK}uBC{L=-n>r3&3G{doISvtycO=u<5X=vX`rBFiEBhjaDG}zd16s2LwR|*US?i)adKios$PCk`s{Z$Qb0vNJY5_^ zB3j>0H_SU^Akwy9JaDq`0=~U0NoPAQZ7pxqb>_~RQN&wz&7)_grL%;@rFjhDA9ksP zx?En_W&FT@lTNgM)%-UH_a7|Q*N|xKJruYw#6z^lyD{Wpjd4&Wm)d9Xx4{nrp7^}^ znf|w6Z`1;=UpcqjCa!yX`bF9Ekljvoj&dB zoE-uJt;!#6_-V~gJ7ZGyZfoJ>JxmY6CH8vguUs?R{?Vu1-i*QCQ<`feY@9#yer^hR VyrgIC3Sby8c)I$ztaD0e0stD5l+OSF literal 0 HcmV?d00001 diff --git a/src/PythonAddons/macros/rectangle/widget.xml b/src/PythonAddons/macros/rectangle/widget.xml index b50603f45..468675f77 100644 --- a/src/PythonAddons/macros/rectangle/widget.xml +++ b/src/PythonAddons/macros/rectangle/widget.xml @@ -3,16 +3,42 @@ - - - + + + + + + + + + + + diff --git a/src/SketchAPI/CMakeLists.txt b/src/SketchAPI/CMakeLists.txt index 4f3e19189..a1ea1da27 100644 --- a/src/SketchAPI/CMakeLists.txt +++ b/src/SketchAPI/CMakeLists.txt @@ -34,7 +34,6 @@ SET(PROJECT_HEADERS SketchAPI_MacroCircle.h SketchAPI_MacroEllipse.h SketchAPI_MacroEllipticArc.h - SketchAPI_MacroRectangle.h SketchAPI_Mirror.h SketchAPI_Offset.h SketchAPI_Point.h @@ -60,7 +59,6 @@ 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 bc36f1cac..f98f8929b 100644 --- a/src/SketchAPI/SketchAPI.i +++ b/src/SketchAPI/SketchAPI.i @@ -77,7 +77,6 @@ %shared_ptr(SketchAPI_Point) %shared_ptr(SketchAPI_Projection) %shared_ptr(SketchAPI_Rectangle) -%shared_ptr(SketchAPI_MacroRectangle) %shared_ptr(SketchAPI_Rotation) %shared_ptr(SketchAPI_Translation) @@ -584,7 +583,6 @@ %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 deleted file mode 100644 index b143c1e64..000000000 --- a/src/SketchAPI/SketchAPI_MacroRectangle.cpp +++ /dev/null @@ -1,151 +0,0 @@ -// 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 isFirstPointCenter): - SketchAPI_SketchEntity(theFeature) -{ - if(initialize()) { - if(isFirstPointCenter) - setByCenterAndEndPoints(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 isFirstPointCenter): - SketchAPI_SketchEntity(theFeature) -{ - if(initialize()) { - if(isFirstPointCenter) - setByCenterAndEndPoints(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::setByCenterAndEndPoints(double theCenterX, double theCenterY, - double theEndX, double theEndY) -{ - fillAttribute(SketchPlugin_MacroRectangle::CENTER_END_POINT_TYPE_ID(), rectangleType()); - fillAttribute(endPoint2(), theEndX, theEndY); - fillAttribute(centerPoint(), theCenterX, theCenterY); - execute(); -} - -//================================================================================================== -void SketchAPI_MacroRectangle::setByCenterAndEndPoints( - const std::shared_ptr& theCenterPoint, - const std::shared_ptr& theEndPoint){ - fillAttribute(SketchPlugin_MacroRectangle::CENTER_END_POINT_TYPE_ID(), rectangleType()); - fillAttribute(theEndPoint, endPoint2()); - 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 deleted file mode 100644 index dc271f3bc..000000000 --- a/src/SketchAPI/SketchAPI_MacroRectangle.h +++ /dev/null @@ -1,95 +0,0 @@ -// 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 isFirstPointCenter = false); - - /// Constructor with values. - SKETCHAPI_EXPORT - SketchAPI_MacroRectangle(const std::shared_ptr& theFeature, - const std::shared_ptr& theStartPoint, - const std::shared_ptr& theSecondPoint, - bool isFirstPointCenter = false); - - - /// Destructor. - SKETCHAPI_EXPORT - virtual ~SketchAPI_MacroRectangle(); - - INTERFACE_5(SketchPlugin_MacroRectangle::ID(), - rectangleType, SketchPlugin_MacroRectangle::RECTANGLE_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 */, - endPoint2, SketchPlugin_MacroRectangle::END2_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 setByCenterAndEndPoints(double theCenterX, double theCenterY, - double theEndX, double theEndY); - - /// Set by start and center points. - void setByCenterAndEndPoints(const std::shared_ptr& theCenterPoint, - const std::shared_ptr& theEndPoint); -}; - -/// 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 af0b62a83..aafddf63e 100644 --- a/src/SketchAPI/SketchAPI_Rectangle.cpp +++ b/src/SketchAPI/SketchAPI_Rectangle.cpp @@ -34,23 +34,21 @@ SketchAPI_Rectangle::SketchAPI_Rectangle( } SketchAPI_Rectangle::SketchAPI_Rectangle(const std::shared_ptr & theFeature, - double theX1, double theY1, double theX2, double theY2, - bool isFirstPointCenter) + double theX1, double theY1, double theX2, double theY2) : SketchAPI_SketchEntity(theFeature) { if (initialize()) { - setByCoordinates(theX1, theY1, theX2, theY2, isFirstPointCenter); + setByCoordinates(theX1, theY1, theX2, theY2); } } SketchAPI_Rectangle::SketchAPI_Rectangle(const std::shared_ptr & theFeature, const std::shared_ptr & theFirstPoint, - const std::shared_ptr & theEndPoint, - bool isFirstPointCenter) + const std::shared_ptr & theEndPoint) : SketchAPI_SketchEntity(theFeature) { if (initialize()) { - setByPoints(theFirstPoint, theEndPoint, isFirstPointCenter); + setByPoints(theFirstPoint, theEndPoint); } } @@ -60,38 +58,21 @@ SketchAPI_Rectangle::~SketchAPI_Rectangle() //-------------------------------------------------------------------------------------- void SketchAPI_Rectangle::setByCoordinates( - double theX1, double theY1, double theX2, double theY2, bool isFirstPointCenter) + double theX1, double theY1, double theX2, double theY2) { - if(isFirstPointCenter){ - fillAttribute(centerPoint(), theX1, theY1); - double xStart = 2.0*theX1 - theX2; - double yStart = 2.0*theY1 - theY2; - fillAttribute(startPoint(), xStart, yStart); - } - else - fillAttribute(startPoint(), theX1, theY1); - + fillAttribute("RectangleTypeByCorners", type()); + fillAttribute(startPoint(), theX1, theY1); fillAttribute(endPoint(), theX2, theY2); - execute(true); + execute(); } void SketchAPI_Rectangle::setByPoints(const std::shared_ptr & theFirstPoint, - const std::shared_ptr & theSecondPoint, - bool isFirstPointCenter) -{ - if(isFirstPointCenter){ - fillAttribute(theFirstPoint, centerPoint()); - double xStart = 2.0*theFirstPoint->x() - theSecondPoint->x(); - double yStart = 2.0*theFirstPoint->y() - theSecondPoint->y(); - - std::shared_ptr theStartPoint = std::make_shared(xStart, yStart); - fillAttribute(theStartPoint, startPoint()); - } - else - fillAttribute(theFirstPoint, startPoint()); - + const std::shared_ptr & theSecondPoint) +{ + fillAttribute("RectangleTypeByCorners", type()); + fillAttribute(theFirstPoint, startPoint()); fillAttribute(theSecondPoint, endPoint()); - execute(true); + execute(); } //-------------------------------------------------------------------------------------- @@ -105,38 +86,3 @@ 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); - - FeaturePtr aCenterPointFeature = std::dynamic_pointer_cast( - aBase->refattr(SketchPlugin_Rectangle::CENTER_REF_ID())->object()); - if(aCenterPointFeature){ - // rectangle has center - theDumper << aBase << " = " << aSketchName << ".addRectangle(" - << startPoint() << ", " << centerPoint() << ", 1)" << 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 2ef409332..3d17f2961 100644 --- a/src/SketchAPI/SketchAPI_Rectangle.h +++ b/src/SketchAPI/SketchAPI_Rectangle.h @@ -24,7 +24,6 @@ #include "SketchAPI.h" #include "SketchAPI_SketchEntity.h" -#include "SketchPlugin_Rectangle.h" //-------------------------------------------------------------------------------------- class ModelHighAPI_Selection; @@ -42,45 +41,45 @@ public: /// Constructor with values SKETCHAPI_EXPORT SketchAPI_Rectangle(const std::shared_ptr & theFeature, - double theX1, double theY1, double theX2, double theY2, - bool isFirstPointCenter = false); + double theX1, double theY1, double theX2, double theY2); /// Constructor with values SKETCHAPI_EXPORT SketchAPI_Rectangle(const std::shared_ptr & theFeature, const std::shared_ptr & theFirstPoint, - const std::shared_ptr & theEndPoint, - bool isFirstPointCenter = false); + const std::shared_ptr & theEndPoint); /// Destructor SKETCHAPI_EXPORT virtual ~SketchAPI_Rectangle(); - INTERFACE_4(SketchPlugin_Rectangle::ID(), - startPoint, SketchPlugin_Rectangle::START_ID(), GeomDataAPI_Point2D, + INTERFACE_7("SketchRectangle", + type, "RectangleType", ModelAPI_AttributeString, + /** Creation type of rectangle */, + startPoint, "RectStartPoint", GeomDataAPI_Point2D, /** Start point */, - endPoint, SketchPlugin_Rectangle::END_ID(), GeomDataAPI_Point2D, + endPoint, "RectEndPoint", GeomDataAPI_Point2D, /** End point */, - centerPoint, SketchPlugin_Rectangle::CENTER_ID(), GeomDataAPI_Point2D, + centerPoint, "RectCenterPoint", GeomDataAPI_Point2D, /** Center point */, - linesList, SketchPlugin_Rectangle::LINES_LIST_ID(), ModelAPI_AttributeRefList, + centerPointRef, "RectCenterPointRef", ModelAPI_AttributeRefAttr, + /** Reference to center point */, + cornerPoint, "RectCornerPoint", GeomDataAPI_Point2D, + /** Corner point */, + linesList, "RectangleList", ModelAPI_AttributeRefList, /** Lines list */ ) /// Set by coordinates SKETCHAPI_EXPORT - void setByCoordinates(double theX1, double theY1, double theX2, double theY2, - bool isFirstPointCenter); + void setByCoordinates(double theX1, double theY1, double theX2, double theY2); /// Set by points SKETCHAPI_EXPORT void setByPoints(const std::shared_ptr & theFirstPoint, - const std::shared_ptr & theSecondPoint, bool isFirstPointCenter); + const std::shared_ptr & theSecondPoint); /// 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 de5799715..fa4790974 100644 --- a/src/SketchAPI/SketchAPI_Sketch.cpp +++ b/src/SketchAPI/SketchAPI_Sketch.cpp @@ -425,38 +425,47 @@ std::shared_ptr SketchAPI_Sketch::addRectangle( return RectanglePtr(new SketchAPI_Rectangle(aFeature, theStartPoint, theEndPoint)); } -std::shared_ptr SketchAPI_Sketch::addRectangle( - double theX1, double theY1, double theX2, double theY2, bool isFirstPointCenter) +static std::shared_ptr pointCoordinates( + const std::pair, ModelHighAPI_RefAttr> & thePoint) { - std::shared_ptr aFeature = - compositeFeature()->addFeature(SketchAPI_Rectangle::ID()); - return RectanglePtr(new SketchAPI_Rectangle(aFeature, theX1, theY1, - theX2, theY2, isFirstPointCenter)); -} -std::shared_ptr SketchAPI_Sketch::addRectangle( - const std::shared_ptr & theFirstPoint, - const std::shared_ptr & theEndPoint, bool isFirstPointCenter) -{ - std::shared_ptr aFeature = - compositeFeature()->addFeature(SketchAPI_Rectangle::ID()); - return RectanglePtr(new SketchAPI_Rectangle(aFeature, theFirstPoint, theEndPoint, - isFirstPointCenter)); -} + if (thePoint.first) + return thePoint.first; -std::shared_ptr SketchAPI_Sketch::addRectangleCentered( - double theX1, double theY1, double theX2, double theY2) -{ - std::shared_ptr aFeature = - compositeFeature()->addFeature(SketchAPI_Rectangle::ID()); - return RectanglePtr(new SketchAPI_Rectangle(aFeature, theX1, theY1, theX2, theY2, true)); + AttributePtr anAttr = thePoint.second.attr(); + if (thePoint.second.object()) { + FeaturePtr aFeature = ModelAPI_Feature::feature(thePoint.second.object()); + if (aFeature) + anAttr = aFeature->attribute(SketchPlugin_Point::COORD_ID()); + } + + std::shared_ptr aPntAttr = + std::dynamic_pointer_cast(anAttr); + if (aPntAttr) + return aPntAttr->pnt(); + return std::shared_ptr(); } + std::shared_ptr SketchAPI_Sketch::addRectangleCentered( - const std::shared_ptr & theFirstPoint, - const std::shared_ptr & theEndPoint) + const std::pair, ModelHighAPI_RefAttr> & theCenter, + const std::pair, ModelHighAPI_RefAttr> & theCorner) { std::shared_ptr aFeature = compositeFeature()->addFeature(SketchAPI_Rectangle::ID()); - return RectanglePtr(new SketchAPI_Rectangle(aFeature, theFirstPoint, theEndPoint, true)); + RectanglePtr aRect(new SketchAPI_Rectangle(aFeature)); + fillAttribute("RectangleTypeCentered", aRect->type()); + if (!theCenter.second.isEmpty()) + fillAttribute(theCenter.second, aRect->centerPointRef()); + fillAttribute(pointCoordinates(theCenter), aRect->centerPoint()); + fillAttribute(pointCoordinates(theCorner), aRect->cornerPoint()); + aRect->execute(); + + if (!theCorner.second.isEmpty() && aRect->linesList()->size() > 1) { + // get start point of the last line in rectangle and apply coindidence constraint + FeaturePtr aLine = ModelAPI_Feature::feature(aRect->linesList()->object(3)); + AttributePtr aEndPnt = aLine->attribute(SketchPlugin_Line::START_ID()); + setCoincident(ModelHighAPI_RefAttr(aEndPnt), theCorner.second); + } + return aRect; } //-------------------------------------------------------------------------------------- diff --git a/src/SketchAPI/SketchAPI_Sketch.h b/src/SketchAPI/SketchAPI_Sketch.h index 866986e72..5f3b4e430 100644 --- a/src/SketchAPI/SketchAPI_Sketch.h +++ b/src/SketchAPI/SketchAPI_Sketch.h @@ -181,22 +181,10 @@ public: const std::shared_ptr & theEndPoint); /// Add rectangle SKETCHAPI_EXPORT - std::shared_ptr addRectangle( - double theX1, double theY1, double theX2, double theY2, bool isFirstPointCenter); - /// Add rectangle - SKETCHAPI_EXPORT - std::shared_ptr addRectangle( - const std::shared_ptr & theFirstPoint, - const std::shared_ptr & theEndPoint, bool isFirstPointCenter); - /// Add rectangle - SKETCHAPI_EXPORT std::shared_ptr addRectangleCentered( - double theX1, double theY1, double theX2, double theY2); - /// Add rectangle - SKETCHAPI_EXPORT - std::shared_ptr addRectangleCentered( - const std::shared_ptr & theFirstPoint, - const std::shared_ptr & theEndPoint); + const std::pair, ModelHighAPI_RefAttr> & theCenter, + const std::pair, ModelHighAPI_RefAttr> & theCorner); + /// Add circle SKETCHAPI_EXPORT std::shared_ptr addCircle( diff --git a/src/SketchAPI/SketchAPI_swig.h b/src/SketchAPI/SketchAPI_swig.h index 3c7d0f2dc..e43232dd3 100644 --- a/src/SketchAPI/SketchAPI_swig.h +++ b/src/SketchAPI/SketchAPI_swig.h @@ -43,7 +43,6 @@ #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 ee77bf732..32983bd0b 100644 --- a/src/SketchPlugin/CMakeLists.txt +++ b/src/SketchPlugin/CMakeLists.txt @@ -62,14 +62,12 @@ 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 @@ -119,14 +117,12 @@ 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 diff --git a/src/SketchPlugin/SketchPlugin_MacroRectangle.cpp b/src/SketchPlugin/SketchPlugin_MacroRectangle.cpp deleted file mode 100644 index 1541f8433..000000000 --- a/src/SketchPlugin/SketchPlugin_MacroRectangle.cpp +++ /dev/null @@ -1,281 +0,0 @@ -// 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 "SketchPlugin_Line.h" - -#include -#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(), myHasCenterPoint(false) -{} - -void SketchPlugin_MacroRectangle::initAttributes() -{ - data()->addAttribute(AUXILIARY_ID(), ModelAPI_AttributeBoolean::typeId()); - data()->addAttribute(START1_ID(), GeomDataAPI_Point2D::typeId()); - data()->addAttribute(START1_REF_ID(), ModelAPI_AttributeRefAttr::typeId()); - data()->addAttribute(END1_ID(), GeomDataAPI_Point2D::typeId()); - data()->addAttribute(END1_REF_ID(), ModelAPI_AttributeRefAttr::typeId()); - data()->addAttribute(END2_ID(), GeomDataAPI_Point2D::typeId()); - data()->addAttribute(END2_REF_ID(), ModelAPI_AttributeRefAttr::typeId()); - data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::typeId()); - data()->addAttribute(CENTER_REF_ID(), ModelAPI_AttributeRefAttr::typeId()); - data()->addAttribute(RECTANGLE_TYPE_ID(), ModelAPI_AttributeString::typeId()); - data()->addAttribute(EDIT_RECTANGLE_TYPE_ID(), ModelAPI_AttributeString::typeId()); - ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EDIT_RECTANGLE_TYPE_ID()); - ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), CENTER_REF_ID()); - ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), END2_REF_ID()); - ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), END1_REF_ID()); - ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), START1_REF_ID()); - - string(EDIT_RECTANGLE_TYPE_ID())->setValue(""); -} - -void SketchPlugin_MacroRectangle::endPoint() -{ - std::shared_ptr aEndPoint; - if(string(RECTANGLE_TYPE_ID())->value() == START_END_POINT_TYPE_ID()) - aEndPoint = std::dynamic_pointer_cast(data()->attribute(END1_ID())); - else - aEndPoint = std::dynamic_pointer_cast(data()->attribute(END2_ID())); - if(aEndPoint->isInitialized()) - myEndPoint = std::make_shared(aEndPoint->x(), aEndPoint->y()); - else - myEndPoint.reset(); -} - -void SketchPlugin_MacroRectangle::startPoint() -{ - if(string(RECTANGLE_TYPE_ID())->value() == START_END_POINT_TYPE_ID()) - { - std::shared_ptr aStartPoint = - std::dynamic_pointer_cast(data()->attribute(START1_ID())); - if(aStartPoint->isInitialized()) - myStartPoint = std::make_shared(aStartPoint->x(), aStartPoint->y()); - else - myStartPoint.reset(); - } - else - { - /// Compute end point as the symmetric of start point w.r.t. center - std::shared_ptr aEndPoint = - std::dynamic_pointer_cast(data()->attribute(END2_ID())); - std::shared_ptr aCenterPoint = - std::dynamic_pointer_cast(data()->attribute(CENTER_ID())); - - if(aCenterPoint->isInitialized()) - { - myCenterPoint = std::make_shared(aCenterPoint->x(), aCenterPoint->y()); - myHasCenterPoint = true; - } - double xStart = 2.0*aCenterPoint->x() - aEndPoint->x(); - double yStart = 2.0*aCenterPoint->y() - aEndPoint->y(); - - if(aEndPoint->isInitialized() && aCenterPoint->isInitialized()) - myStartPoint = std::make_shared(xStart, yStart); - else - myStartPoint.reset(); - } -} - - -void SketchPlugin_MacroRectangle::execute() -{ - SketchPlugin_Sketch* aSketch = sketch(); - if(!myStartPoint || !myEndPoint || !aSketch) { - return ; - } - // Wait all constraints being created, then send update events - static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED); - bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent); - if (isUpdateFlushed) - Events_Loop::loop()->setFlushed(anUpdateEvent, false); - - /// create a rectangle sketch - FeaturePtr myRectangleFeature = aSketch->addFeature(SketchPlugin_Rectangle::ID()); - if(!myRectangleFeature) - return; - - if(myHasCenterPoint){ - std::dynamic_pointer_cast( - myRectangleFeature->attribute( - SketchPlugin_Rectangle::CENTER_ID()))->setValue(myCenterPoint->x(), - myCenterPoint->y()); - } - - 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(); - - /// create coincidences with rectangle start/center and end points - AttributeRefListPtr aLinesList = - myRectangleFeature->reflist(SketchPlugin_Rectangle::LINES_LIST_ID()); - FeaturePtr aRectangleStartLineFeature = - std::dynamic_pointer_cast(aLinesList->object(0)); - FeaturePtr aRectangleEndLineFeature = - std::dynamic_pointer_cast(aLinesList->object(2)); - std::shared_ptr aRectanglePointEndAttr = - std::dynamic_pointer_cast( - aRectangleEndLineFeature->attribute(SketchPlugin_Line::END_ID())); - std::shared_ptr aRectanglePointStartAttr = - std::dynamic_pointer_cast( - aRectangleStartLineFeature->attribute(SketchPlugin_Line::END_ID())); - - if(myHasCenterPoint){ - FeaturePtr aCenterPointFeature = std::dynamic_pointer_cast( - myRectangleFeature->refattr(CENTER_REF_ID())->object()); - SketchPlugin_Tools::createCoincidenceOrTangency( - this, CENTER_REF_ID(), AttributePtr(), aCenterPointFeature, false); - SketchPlugin_Tools::createCoincidenceOrTangency( - this, END2_REF_ID(), aRectanglePointEndAttr, ObjectPtr(), false); - } - else{ - SketchPlugin_Tools::createCoincidenceOrTangency( - this, START1_REF_ID(), aRectanglePointStartAttr, ObjectPtr(), false); - SketchPlugin_Tools::createCoincidenceOrTangency( - this, END1_REF_ID(), aRectanglePointEndAttr, ObjectPtr(), false); - } - - /// Send events to update the sub-features by the solver. - if (isUpdateFlushed) - Events_Loop::loop()->setFlushed(anUpdateEvent, true); -} - -void SketchPlugin_MacroRectangle::attributeChanged(const std::string& theID) -{ - if(theID == RECTANGLE_TYPE_ID()) { - SketchPlugin_Tools::resetAttribute(this, START1_ID()); - SketchPlugin_Tools::resetAttribute(this, END1_ID()); - SketchPlugin_Tools::resetAttribute(this, CENTER_ID()); - SketchPlugin_Tools::resetAttribute(this, END2_ID()); - SketchPlugin_Tools::resetAttribute(this, START1_REF_ID()); - SketchPlugin_Tools::resetAttribute(this, END1_REF_ID()); - SketchPlugin_Tools::resetAttribute(this, CENTER_REF_ID()); - SketchPlugin_Tools::resetAttribute(this, END2_REF_ID()); - myStartPoint.reset(); - myEndPoint.reset(); - myCenterPoint.reset(); - myHasCenterPoint = false; - } - else if (theID == START1_ID() || theID == END1_ID() || - theID == START1_REF_ID() || theID == END1_REF_ID() || - theID == END2_ID() || theID == CENTER_ID() || - theID == END2_REF_ID() || theID == CENTER_REF_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(RECTANGLE_TYPE_ID())->value() == CENTER_END_POINT_TYPE_ID()){ - /// draw a line center->end - std::shared_ptr aCenterPoint = - std::dynamic_pointer_cast(data()->attribute(CENTER_ID())); - - std::shared_ptr theEnd(aSketch->to3D(myEndPoint->x(), myEndPoint->y())); - std::shared_ptr theStart(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 deleted file mode 100644 index fc78a1cde..000000000 --- a/src/SketchPlugin/SketchPlugin_MacroRectangle.h +++ /dev/null @@ -1,157 +0,0 @@ -// 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& RECTANGLE_TYPE_ID() - { - static const std::string ID("rectangle_type"); - return ID; - } - - inline static const std::string& EDIT_RECTANGLE_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& CENTER_END_POINT_TYPE_ID() - { - static const std::string ID("rectangle_type_by_center_and_end_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; - } - - inline static const std::string& START1_REF_ID() - { - static const std::string ID("rectangle_start_point1_ref"); - 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; - } - - inline static const std::string& END1_REF_ID() - { - static const std::string ID("rectangle_end_point1_ref"); - return ID; - } - - /// 2D point - start point of the second Rectangle type - inline static const std::string& END2_ID() - { - static const std::string ID("rectangle_end_point2"); - return ID; - } - - inline static const std::string& END2_REF_ID() - { - static const std::string ID("rectangle_end_point2_ref"); - 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; - } - /// Reference for center point selection. - inline static const std::string& CENTER_REF_ID() - { - static const std::string ID("rectangle_center_point_ref"); - 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; - std::shared_ptr myCenterPoint; - bool myHasCenterPoint; - void startPoint(); - void endPoint(); - FeaturePtr createRectangle(); -}; - -#endif diff --git a/src/SketchPlugin/SketchPlugin_Plugin.cpp b/src/SketchPlugin/SketchPlugin_Plugin.cpp index 49d1328ed..b71ff3c37 100644 --- a/src/SketchPlugin/SketchPlugin_Plugin.cpp +++ b/src/SketchPlugin/SketchPlugin_Plugin.cpp @@ -23,7 +23,6 @@ #include #include #include -#include #include #include #include @@ -50,7 +49,6 @@ #include #include #include -#include #include #include #include @@ -289,12 +287,7 @@ 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(); } @@ -373,10 +366,8 @@ 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 deleted file mode 100644 index b598158b3..000000000 --- a/src/SketchPlugin/SketchPlugin_Rectangle.cpp +++ /dev/null @@ -1,369 +0,0 @@ -// 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(AUXILIARY_ID(), ModelAPI_AttributeBoolean::typeId()); - data()->addAttribute(START_ID(), GeomDataAPI_Point2D::typeId()); - data()->addAttribute(END_ID(), GeomDataAPI_Point2D::typeId()); - data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::typeId()); - ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), CENTER_ID()); - data()->addAttribute(CENTER_REF_ID(), ModelAPI_AttributeRefAttr::typeId()); - ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), CENTER_REF_ID()); - data()->addAttribute(LINES_LIST_ID(), ModelAPI_AttributeRefList::typeId()); - ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), LINES_LIST_ID()); - data()->addAttribute(DIAGONAL_LIST_ID(), ModelAPI_AttributeRefList::typeId()); - ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), DIAGONAL_LIST_ID()); - data()->addAttribute(ISHV_LIST_ID(), ModelAPI_AttributeIntArray::typeId()); - ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), ISHV_LIST_ID()); - data()->addAttribute(NOT_TO_DUMP_LIST_ID(), ModelAPI_AttributeRefList::typeId()); - ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), NOT_TO_DUMP_LIST_ID()); -} - -namespace { - static const std::pair cornerToDiagonalLinePoints[4] - = { - {0, SketchPlugin_Line::START_ID()}, - {1, SketchPlugin_Line::START_ID()}, - {0, SketchPlugin_Line::END_ID()}, - {1, SketchPlugin_Line::END_ID()} - }; -} - -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 aXStart = aStartPoint->x(); - double aYStart = aStartPoint->y(); - double aXEnd = aEndPoint->x(); - double aYEnd = aEndPoint->y(); - - std::vector aX = {aXStart, aXStart, aXEnd, aXEnd}; - std::vector aY = {aYStart, aYEnd, aYEnd, aYStart}; - - bool anAuxiliary = data()->boolean(AUXILIARY_ID())->value(); - AttributeRefListPtr aDiagonalList = reflist(DIAGONAL_LIST_ID()); - - - /// 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+3)%4], aY[(i+3)%4]); - aLineEnd->setValue(aX[i], aY[i]); - aLine->data()->boolean(AUXILIARY_ID())->setValue(anAuxiliary); - /// Cooordinates of diagonals - if(aDiagonalList->size()) - { - auto aDiagonalPoint = cornerToDiagonalLinePoints[i]; - FeaturePtr aDiagonal = std::dynamic_pointer_cast( - aDiagonalList->object(aDiagonalPoint.first)); - std::dynamic_pointer_cast( - aDiagonal->attribute(aDiagonalPoint.second))->setValue(aX[(i+3)%4], aY[(i+3)%4]); - } - } -} - -void SketchPlugin_Rectangle::updateStartPoint() -{ - /// 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())); - - double aXStart = aStartPoint->x(); - double aYStart = aStartPoint->y(); - - /// 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::END_ID())); - aLineStart->setValue(aXStart, aYStart); - } -} - -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()); - AttributeRefListPtr aDiagonalList = reflist(DIAGONAL_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(); - FeaturePtr aCenterPointFeature; - - if(aDiagonalList->size()) - { - /// compute diagonals intersection point - std::shared_ptr aCenterAttr = - std::dynamic_pointer_cast(data()->attribute(CENTER_ID())); - - aCenterPointFeature = aSketch->addFeature(SketchPlugin_Point::ID()); - aNotToDumpList->append(aCenterPointFeature); - AttributePoint2DPtr aCoord = std::dynamic_pointer_cast( - aCenterPointFeature->attribute(SketchPlugin_Point::COORD_ID())); - aCoord->setValue(aCenterAttr->x(), aCenterAttr->y()); - aCenterPointFeature->boolean(SketchPlugin_Point::AUXILIARY_ID())->setValue(true); - refattr(CENTER_REF_ID())->setObject(aCenterPointFeature); - - for(int i = 0; i < 2; i++) - { - FeaturePtr aDiagonal = std::dynamic_pointer_cast( - aDiagonalList->object(i)); - FeaturePtr aConstraint = SketchPlugin_Tools::createConstraintAttrObject( - aSketch, SketchPlugin_ConstraintCoincidence::ID(), - aCoord, aDiagonal); - aNotToDumpList->append(aConstraint); - } - } - - /// 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 aConstraint = SketchPlugin_Tools::createConstraintAttrAttr( - aSketch, SketchPlugin_ConstraintCoincidence::ID(), - aPrevLine->attribute(SketchPlugin_Line::END_ID()), - aLine->attribute(SketchPlugin_Line::START_ID())); - aNotToDumpList->append(aConstraint); - - /// case of rectangle created from its center - if(aDiagonalList->size()) - { - auto aDiagonalPoint = cornerToDiagonalLinePoints[i]; - FeaturePtr aDiagonal = std::dynamic_pointer_cast( - aDiagonalList->object(aDiagonalPoint.first)); - FeaturePtr aConstraint = SketchPlugin_Tools::createConstraintAttrAttr( - aSketch, SketchPlugin_ConstraintCoincidence::ID(), - aDiagonal->attribute(aDiagonalPoint.second), - aLine->attribute(SketchPlugin_Line::START_ID())); - aNotToDumpList->append(aConstraint); - } - } - /// Update coordinates of created lines - updateLines(); - } - - /// Add horizontal and vertical constraint for the lines which already have result - if(isHVList->size() == 0) - { - isHVList->setSize(4, false); - for(int i = 0; i< 4;) - isHVList->setValue(i++, 0, false); - } - - 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_Constraint::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()); - } - - for(int i = 0; i< aDiagonalList->size(); i++) - { - FeaturePtr aDiagonal = std::dynamic_pointer_cast(aDiagonalList->object(i)); - ResultPtr aDiagonalResult = aDiagonal->lastResult(); - if(!aDiagonalResult.get()) - continue; - aSubs.push_back(aDiagonalResult->shape()); - } - - FeaturePtr aCenterPointFeature = std::dynamic_pointer_cast( - refattr(CENTER_REF_ID())->object()); - if(aCenterPointFeature) - { - ResultPtr aCenterResult = aCenterPointFeature->lastResult(); - if(aCenterResult.get()) - aSubs.push_back(aCenterResult->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); -} - -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); - - /// if rectangle has center, add 2 iagonals - std::shared_ptr aCenterAttr = - std::dynamic_pointer_cast(data()->attribute(CENTER_ID())); - if(aCenterAttr->isInitialized()) - { - AttributeRefListPtr aDiagonalList = reflist(DIAGONAL_LIST_ID()); - for(int i = 0; i < 2; i++) - { - FeaturePtr aDiagonalLine = aSketch->addFeature(SketchPlugin_Line::ID()); - aDiagonalLine->boolean(SketchPlugin_Point::AUXILIARY_ID())->setValue(true); - aDiagonalList->append(aDiagonalLine); - aNotToDumpList->append(aDiagonalLine); - } - } - } - 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(); - else - updateStartPoint(); - } - - 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 deleted file mode 100644 index a39621729..000000000 --- a/src/SketchPlugin/SketchPlugin_Rectangle.h +++ /dev/null @@ -1,124 +0,0 @@ -// 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" - -/**\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 - center point of the Rectangle - inline static const std::string& CENTER_ID() - { - static const std::string ID("rectangle_center_point"); - return ID; - } - - /// 2D point - center point of the Rectangle - inline static const std::string& CENTER_REF_ID() - { - static const std::string ID("rectangle_center_point_ref"); - return ID; - } - - /// 2D point - list of Rectangle lines - inline static const std::string& LINES_LIST_ID() - { - static const std::string ID("RectangleLinesList"); - return ID; - } - - /// 2D point - list of Diagonal lines - inline static const std::string& DIAGONAL_LIST_ID() - { - static const std::string ID("RectangleDiagonalLinesList"); - 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; - } - - SKETCHPLUGIN_EXPORT virtual bool isMacro() const - { return true;} - - /// 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(); - void updateStartPoint(); -}; - - -#endif diff --git a/src/SketchPlugin/doc/examples/rectangle.py b/src/SketchPlugin/doc/examples/rectangle.py index 4cb5b4d89..c9f785103 100644 --- a/src/SketchPlugin/doc/examples/rectangle.py +++ b/src/SketchPlugin/doc/examples/rectangle.py @@ -1,4 +1,5 @@ from salome.shaper import model +from salome.shaper import geom model.begin() partSet = model.moduleDocument() @@ -7,7 +8,9 @@ Part_1_doc = Part_1.document() Sketch_1 = model.addSketch(Part_1_doc, model.defaultPlane("XOY")) Rectangle_1 = Sketch_1.addRectangle(5.5, 8.5, 31.3, 78.9) # rectangle from center and end points -rectangle_2 = sketch.addRectangleCentered(10, 5, 25, 75) +center = geom.Pnt2d(10, 5) +corner = geom.Pnt2d(25, 75) +rectangle_2 = sketch.addRectangleCentered(center, corner) model.do() model.end() diff --git a/src/SketchPlugin/plugin-Sketch.xml b/src/SketchPlugin/plugin-Sketch.xml index 628801e68..824e6281b 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 SketchMacroRectangle + SketchRectangle SketchProjection SketchCurveFitting SketchConstraintLength SketchConstraintRadius SketchConstraintDistance SketchConstraintDistanceHorizontal SketchConstraintDistanceVertical @@ -52,82 +52,6 @@ - - - - - - - - - - - - - - - - - - - - - - - -- 2.30.2