]> SALOME platform Git repositories - modules/shaper.git/commitdiff
Salome HOME
move rectangle plugin from python addons to C++.
authorrraphael <raphael.raphael@c-s.fr>
Mon, 4 Jan 2021 01:24:46 +0000 (02:24 +0100)
committerrraphael <raphael.raphael@c-s.fr>
Fri, 15 Jan 2021 11:10:10 +0000 (12:10 +0100)
Add a rectangle feature from corner and center points.
Change rectangle feature dump into SketchRectangle instead of  SketchLines.

22 files changed:
src/CollectionPlugin/Test/TestGroupShape.py [new file with mode: 0644]
src/PartSet/PartSet_Module.h
src/PartSet/PartSet_SketcherMgr.cpp
src/PythonAddons/CMakeLists.txt
src/PythonAddons/addons_Features.py
src/PythonAddons/addons_Features.xml.in
src/SketchAPI/CMakeLists.txt
src/SketchAPI/SketchAPI.i
src/SketchAPI/SketchAPI_MacroRectangle.cpp [new file with mode: 0644]
src/SketchAPI/SketchAPI_MacroRectangle.h [new file with mode: 0644]
src/SketchAPI/SketchAPI_Rectangle.cpp
src/SketchAPI/SketchAPI_Rectangle.h
src/SketchAPI/SketchAPI_Sketch.cpp
src/SketchAPI/SketchAPI_Sketch.h
src/SketchAPI/SketchAPI_swig.h
src/SketchPlugin/CMakeLists.txt
src/SketchPlugin/SketchPlugin_MacroRectangle.cpp [new file with mode: 0644]
src/SketchPlugin/SketchPlugin_MacroRectangle.h [new file with mode: 0644]
src/SketchPlugin/SketchPlugin_Plugin.cpp
src/SketchPlugin/SketchPlugin_Rectangle.cpp [new file with mode: 0644]
src/SketchPlugin/SketchPlugin_Rectangle.h [new file with mode: 0644]
src/SketchPlugin/plugin-Sketch.xml

diff --git a/src/CollectionPlugin/Test/TestGroupShape.py b/src/CollectionPlugin/Test/TestGroupShape.py
new file mode 100644 (file)
index 0000000..5ec06a9
--- /dev/null
@@ -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())
index ced4f93256dc95690016f6cf4c369507e7fd6982..75467a3c434bacaad7aa0ec4db99f9dc10c077ab 100644 (file)
@@ -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
index ecc6f36d80c3d01aa8d1fbc79d9030deb63c8721..c85093877c2f58ca8a12e3d36a589e9a9033c78c 100644 (file)
@@ -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);
 }
 
index 9f40500b2da80a570238217c81c4b9216cc7ed48..c81edf33f3800463cf8a69de87139cfce4ad41f7 100644 (file)
@@ -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
 )
index 4c302a0128c37cad3fd1c5a70690cb04561c4a1c..207dd78c4e0faf71cbb0ec191b937cb3dde2b2aa 100644 (file)
@@ -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__()
index 3f3b7666d7f1b41f33820f09202eaf5ddc8597c1..6235ee56ec64e9f67f7e53befabd80f3bc0ee343 100644 (file)
@@ -1,5 +1,5 @@
 <plugin>
-  <source path="@ADDONS_FOLDER_PATH@/macros/rectangle/widget.xml"/>
+  <!--source path="@ADDONS_FOLDER_PATH@/macros/rectangle/widget.xml"/-->
   <source path="@ADDONS_FOLDER_PATH@/macros/compoundVertices/widget.xml"/>
   <source path="@ADDONS_FOLDER_PATH@/macros/importParameters/widget.xml"/>
 </plugin>
index a1ea1da27678f648ac4eb2e22fa2c677144fa806..4f3e191898ceb93418726fb34ee704c3554c48bb 100644 (file)
@@ -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
index f98f8929bc54a635bb75f87972656c03bedba29d..bc36f1cac6a8a126a1e563b7d947aee929fe4e38 100644 (file)
@@ -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)
 
 %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 (file)
index 0000000..d2614f4
--- /dev/null
@@ -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 <GeomAPI_Pnt2d.h>
+
+#include <ModelHighAPI_Double.h>
+#include <ModelHighAPI_Dumper.h>
+#include <ModelHighAPI_Selection.h>
+#include <ModelHighAPI_Tools.h>
+
+//==================================================================================================
+SketchAPI_MacroRectangle::SketchAPI_MacroRectangle(const std::shared_ptr<ModelAPI_Feature>& theFeature)
+  : SketchAPI_SketchEntity(theFeature)
+{
+  initialize();
+}
+
+//==================================================================================================
+SketchAPI_MacroRectangle::SketchAPI_MacroRectangle(const std::shared_ptr<ModelAPI_Feature>& 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<ModelAPI_Feature>& theFeature,
+                                                   const std::shared_ptr<GeomAPI_Pnt2d>& theStartPoint,
+                                                   const std::shared_ptr<GeomAPI_Pnt2d>& 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<GeomAPI_Pnt2d>& theStartPoint,
+                                                      const std::shared_ptr<GeomAPI_Pnt2d>& 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<GeomAPI_Pnt2d>& theStartPoint,
+                                                         const std::shared_ptr<GeomAPI_Pnt2d>& 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<SketchPlugin_MacroRectangle> myRectangle = std::dynamic_pointer_cast<SketchPlugin_MacroRectangle>(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 (file)
index 0000000..773c849
--- /dev/null
@@ -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 <SketchPlugin_MacroRectangle.h>
+
+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<ModelAPI_Feature>& theFeature);
+
+  /// Constructor with values.
+  SKETCHAPI_EXPORT
+  SketchAPI_MacroRectangle(const std::shared_ptr<ModelAPI_Feature>& theFeature,
+                        double theStartX,
+                        double theStartY,
+                        double theSecondX,
+                        double theSecondY, bool isSecondPointCenter = false);
+
+  /// Constructor with values.
+  SKETCHAPI_EXPORT
+  SketchAPI_MacroRectangle(const std::shared_ptr<ModelAPI_Feature>& theFeature,
+                        const std::shared_ptr<GeomAPI_Pnt2d>& theStartPoint,
+                        const std::shared_ptr<GeomAPI_Pnt2d>& 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<GeomAPI_Pnt2d>& theStartPoint,
+                                  const std::shared_ptr<GeomAPI_Pnt2d>& 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<GeomAPI_Pnt2d>& theStartPoint,
+                                  const std::shared_ptr<GeomAPI_Pnt2d>& theCenterPoint);
+};
+
+/// Pointer on Rectangle object.
+typedef std::shared_ptr<SketchAPI_MacroRectangle> MacroRectanglePtr;
+
+#endif // SketchAPI_MacroRectangle_H_
index c7faac8b0664c24470b3316b7304e52cb11a01b8..f9ee374e5b93cdf0fc2eaa2da1874b85fdd8b378 100644 (file)
 //--------------------------------------------------------------------------------------
 #include <ModelHighAPI_Selection.h>
 #include <ModelHighAPI_Tools.h>
+#include <ModelHighAPI_Dumper.h>
+
 //--------------------------------------------------------------------------------------
 SketchAPI_Rectangle::SketchAPI_Rectangle(
     const std::shared_ptr<ModelAPI_Feature> & theFeature)
-: SketchAPI_SketchEntity(theFeature)
+  : SketchAPI_SketchEntity(theFeature)
 {
   initialize();
 }
@@ -34,7 +36,7 @@ SketchAPI_Rectangle::SketchAPI_Rectangle(
 SketchAPI_Rectangle::SketchAPI_Rectangle(
     const std::shared_ptr<ModelAPI_Feature> & 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<ModelAPI_Feature> & theFeature,
     const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
     const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint)
-: SketchAPI_SketchEntity(theFeature)
+  : SketchAPI_SketchEntity(theFeature)
 {
   if (initialize()) {
     setByPoints(theStartPoint, theEndPoint);
@@ -87,3 +89,34 @@ std::list<std::shared_ptr<SketchAPI_SketchEntity> > 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<ModelAPI_Feature>(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);
+
+}
index 0c523fb5d57f07974b6d1b6c90dfc16e0d29e093..a1e6fea6b18c8e51c4ca118e5f6ebe3e7a2aaeb5 100644 (file)
@@ -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<std::shared_ptr<SketchAPI_SketchEntity> > lines() const;
-};
+
+  SKETCHAPI_EXPORT
+  virtual void dump(ModelHighAPI_Dumper& theDumper) const;
+  };
 
 //! Pointer on Rectangle object
 typedef std::shared_ptr<SketchAPI_Rectangle> RectanglePtr;
index 5f60ae2a759e204024cc38743020f35587973c09..db60b1826a13c00bb7341525c158dc8be3679ea9 100644 (file)
@@ -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_Rectangle> SketchAPI_Sketch::addRectangle(
     compositeFeature()->addFeature(SketchAPI_Rectangle::ID());
   return RectanglePtr(new SketchAPI_Rectangle(aFeature, theStartPoint, theEndPoint));
 }
-
+/*
+std::shared_ptr<SketchAPI_MacroRectangle> SketchAPI_Sketch::addRectangle(
+    double theX1, double theY1, double theX2, double theY2, bool thePoint2IsCenter)
+{
+  std::shared_ptr<ModelAPI_Feature> aFeature =
+    compositeFeature()->addFeature(SketchAPI_MacroRectangle::ID());
+  return MacroRectanglePtr(new SketchAPI_MacroRectangle(aFeature, theX1, theY1, theX2, theY2, thePoint2IsCenter));
+}
+std::shared_ptr<SketchAPI_MacroRectangle> SketchAPI_Sketch::addRectangle(
+    const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
+    const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint,  bool theEndPointIsCenter)
+{
+  std::shared_ptr<ModelAPI_Feature> aFeature =
+    compositeFeature()->addFeature(SketchAPI_MacroRectangle::ID());
+  return MacroRectanglePtr(new SketchAPI_MacroRectangle(aFeature, theStartPoint, theEndPoint, theEndPointIsCenter));
+}
+*/
 //--------------------------------------------------------------------------------------
 std::shared_ptr<SketchAPI_Circle> SketchAPI_Sketch::addCircle(double theCenterX,
                                                               double theCenterY,
@@ -1588,6 +1605,34 @@ void SketchAPI_Sketch::dump(ModelHighAPI_Dumper& theDumper) const
     std::list<std::list<ResultPtr> > aFaces;
     edgesOfSketchFaces(aCompFeat, aFaces);
 
+    /// remove faces that must not be dumped
+    std::vector< std::list<std::list<ResultPtr>>::iterator> aFacesToRemove;
+    for(auto itFaces = aFaces.begin(); itFaces != aFaces.end(); ++itFaces)
+    {
+      auto & facesGroup = *itFaces;
+      std::vector<std::list<ResultPtr>::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(), ' ');
index 11a2639087700596e0518015e155b05accfd616e..d26472f28520f66f42b443b41c9dd98ee5aae231 100644 (file)
@@ -56,6 +56,7 @@ class SketchAPI_Projection;
 class SketchAPI_Rectangle;
 class SketchAPI_Rotation;
 class SketchAPI_Translation;
+class SketchAPI_MacroRectangle;
 //--------------------------------------------------------------------------------------
 typedef std::pair<std::shared_ptr<GeomAPI_Pnt2d>, ModelHighAPI_RefAttr> PointOrReference;
 //--------------------------------------------------------------------------------------
@@ -178,7 +179,16 @@ public:
   std::shared_ptr<SketchAPI_Rectangle> addRectangle(
       const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
       const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint);
-
+  /// Add rectangle
+  /*SKETCHAPI_EXPORT
+  std::shared_ptr<SketchAPI_MacroRectangle> addRectangle(
+      double theX1, double theY1, double theX2, double theY2, bool thePoint2IsCenter);
+  /// Add rectangle
+  SKETCHAPI_EXPORT
+  std::shared_ptr<SketchAPI_MacroRectangle> addRectangle(
+      const std::shared_ptr<GeomAPI_Pnt2d> & theStartPoint,
+      const std::shared_ptr<GeomAPI_Pnt2d> & theEndPoint,  bool theEndPointIsCenter);
+*/
   /// Add circle
   SKETCHAPI_EXPORT
   std::shared_ptr<SketchAPI_Circle> addCircle(
index e43232dd3b6acaae6b9fa9de0c81ec917fb72152..3c7d0f2dcb465276beaf484f3279b1afea89b984 100644 (file)
@@ -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"
 
index b5cbdd3d3d490ec995b2b1a67fbb935129265fef..ee77bf732ccc68354c1343a7e3e2c5241a052a66 100644 (file)
@@ -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 (file)
index 0000000..fae735f
--- /dev/null
@@ -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 <ModelAPI_Data.h>
+#include <ModelAPI_ResultConstruction.h>
+#include <ModelAPI_AttributeSelection.h>
+#include <ModelAPI_Validator.h>
+#include <ModelAPI_AttributeString.h>
+#include <ModelAPI_Session.h>
+#include <ModelAPI_AttributeRefList.h>
+#include <ModelAPI_AttributeRefAttr.h>
+#include <ModelAPI_AttributeReference.h>
+#include <GeomAlgoAPI_CompoundBuilder.h>
+#include <GeomAlgoAPI_EdgeBuilder.h>
+#include <GeomAlgoAPI_PointBuilder.h>
+
+#include <GeomAPI_Pnt2d.h>
+
+#include <cmath>
+
+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<GeomDataAPI_Point2D> aStartPoint;
+  if(string(TYPE_ID())->value() == START_END_POINT_TYPE_ID())
+    aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(START1_ID()));
+  else
+    aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(START2_ID()));
+  if(aStartPoint->isInitialized())
+    myStartPoint = std::make_shared<GeomAPI_Pnt2d>(aStartPoint->x(), aStartPoint->y());
+  else
+    myStartPoint.reset();
+}
+
+void SketchPlugin_MacroRectangle::endPoint()
+{
+  if(string(TYPE_ID())->value() == START_END_POINT_TYPE_ID())
+  {
+    std::shared_ptr<GeomDataAPI_Point2D> aEndPoint =
+        std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(END1_ID()));
+    if(aEndPoint->isInitialized())
+      myEndPoint = std::make_shared<GeomAPI_Pnt2d>(aEndPoint->x(), aEndPoint->y());
+    else
+      myEndPoint.reset();
+  }
+  else
+  {
+    /// Compute end point as the symmetric of start point w.r.t. center
+    std::shared_ptr<GeomDataAPI_Point2D> aStartPoint =
+        std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(START2_ID()));
+    std::shared_ptr<GeomDataAPI_Point2D> aCenterPoint =
+        std::dynamic_pointer_cast<GeomDataAPI_Point2D>(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<GeomAPI_Pnt2d>(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<GeomDataAPI_Point2D>(
+        myRectangleFeature->attribute(SketchPlugin_Rectangle::START_ID()))->setValue(myStartPoint->x(),
+                                                                                     myStartPoint->y());
+  std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+        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<double> aX = {myStartPoint->x(), myStartPoint->x(), myEndPoint->x(), myEndPoint->x()};
+  std::vector<double> aY = {myStartPoint->y(), myEndPoint->y(), myEndPoint->y(), myStartPoint->y()};
+
+  std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
+  /// Update coordinates of rectangle lines
+
+  std::set<int> createdPointIndex;
+  for(unsigned  i = 0; i < 4; i++)
+  {
+    std::shared_ptr<GeomAPI_Pnt> theStart(aSketch->to3D(aX[(i+3)%4], aY[(i+3)%4]));
+    std::shared_ptr<GeomAPI_Pnt> 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<GeomDataAPI_Point2D> aCenterPoint =
+        std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
+
+    std::shared_ptr<GeomAPI_Pnt> theStart(aSketch->to3D(myStartPoint->x(), myStartPoint->y()));
+    std::shared_ptr<GeomAPI_Pnt> 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<GeomAPI_Shape> 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 (file)
index 0000000..fc196f9
--- /dev/null
@@ -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 <SketchPlugin_SketchEntity.h>
+#include <GeomAPI_IPresentable.h>
+
+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<GeomAPI_Pnt2d> myStartPoint;
+  std::shared_ptr<GeomAPI_Pnt2d> myEndPoint;
+
+  void startPoint();
+  void endPoint();
+  FeaturePtr createRectangle();
+};
+
+#endif
index b71ff3c37889f6dda4790a9aed34e0863bab772a..49d1328edd7efb5b20b260e0c7b55d3167637e1b 100644 (file)
@@ -23,6 +23,7 @@
 #include <SketchPlugin_Point.h>
 #include <SketchPlugin_IntersectionPoint.h>
 #include <SketchPlugin_Circle.h>
+#include <SketchPlugin_Rectangle.h>
 #include <SketchPlugin_Arc.h>
 #include <SketchPlugin_BSpline.h>
 #include <SketchPlugin_BSplinePeriodic.h>
@@ -49,6 +50,7 @@
 #include <SketchPlugin_MacroArc.h>
 #include <SketchPlugin_MacroBSpline.h>
 #include <SketchPlugin_MacroCircle.h>
+#include <SketchPlugin_MacroRectangle.h>
 #include <SketchPlugin_MultiRotation.h>
 #include <SketchPlugin_MultiTranslation.h>
 #include <SketchPlugin_Offset.h>
@@ -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<ModelAPI_FeatureStateMessage> 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 (file)
index 0000000..3933f99
--- /dev/null
@@ -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 <ModelAPI_Data.h>
+#include <ModelAPI_ResultConstruction.h>
+#include <ModelAPI_AttributeSelection.h>
+#include <ModelAPI_Validator.h>
+#include <ModelAPI_Session.h>
+#include <ModelAPI_AttributeRefList.h>
+#include <ModelAPI_AttributeRefAttr.h>
+#include <GeomDataAPI_Point2D.h>
+#include <GeomAlgoAPI_CompoundBuilder.h>
+
+#include <SketchPlugin_Line.h>
+#include <SketchPlugin_ConstraintCoincidence.h>
+#include <SketchPlugin_ConstraintHorizontal.h>
+#include <SketchPlugin_ConstraintVertical.h>
+
+#include <cmath>
+
+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<GeomDataAPI_Point2D> aStartPoint =
+      std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(START_ID()));
+  std::shared_ptr<GeomDataAPI_Point2D> aEndPoint =
+      std::dynamic_pointer_cast<GeomDataAPI_Point2D>(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<double> aX = {xMin, xMax, xMax, xMin};
+  std::vector<double> 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<ModelAPI_Feature>(aLinesList->object(i));
+    std::shared_ptr<GeomDataAPI_Point2D> aLineStart =
+        std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aLine->attribute(SketchPlugin_Line::START_ID()));
+    std::shared_ptr<GeomDataAPI_Point2D> aLineEnd =
+        std::dynamic_pointer_cast<GeomDataAPI_Point2D>(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<GeomDataAPI_Point2D> aStartAttr =
+      std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(START_ID()));
+
+  std::shared_ptr<GeomDataAPI_Point2D> aEndAttr =
+      std::dynamic_pointer_cast<GeomDataAPI_Point2D>(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<ModelAPI_Feature>(aLinesList->object(i));
+      /// connect neighbor lines by coincidence
+      unsigned iPrev = (i+3)%4;
+      FeaturePtr aPrevLine = std::dynamic_pointer_cast<ModelAPI_Feature>(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<ModelAPI_Feature>(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<ModelAPI_Feature>(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<ModelAPI_ResultConstruction> 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<GeomDataAPI_Point2D> aStartPoint =
+        std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(START_ID()));
+    std::shared_ptr<GeomDataAPI_Point2D> aEndPoint =
+        std::dynamic_pointer_cast<GeomDataAPI_Point2D>(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<ModelAPI_Feature>(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 (file)
index 0000000..7d3e2f4
--- /dev/null
@@ -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
index 824e6281b2c9b60f4145dc74efe6a0fd50fbb68b..ea64ddfe7c7b168128bc3b27532024848e98a69e 100644 (file)
@@ -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
         <boolvalue id="Auxiliary" label="Auxiliary" default="false" tooltip="Construction element" obligatory="0" change_visual_attributes="true"/>
         <validator id="GeomValidators_Different" parameters="StartPoint,EndPoint"/>
       </feature>
+      <!-- SketchRectangle is a hidden feature. It is created inside SketchMacroRectangle. -->
+      <feature
+        id="SketchRectangle"
+        title="Rectangle"
+        tooltip="Create rectangle"
+        icon="icons/Addons/rectangle.png"
+        helpfile="rectangleFeature.html"
+        internal="1">
+        <sketch-2dpoint_selector id="rectangle_start_point"
+                                 accept_expressions="0"
+                                 title="Rectangle start point"
+                                 tooltip="Start point coordinates"
+                                 enable_value="enable_by_preferences"/>
+        <sketch-2dpoint_selector id="rectangle_end_point"
+                                 accept_expressions="0"
+                                 title="Rectangle end point"
+                                 tooltip="End point coordinates"
+                                 enable_value="enable_by_preferences"/>
+        <validator id="GeomValidators_Different" parameters="rectangle_start_point,rectangle_end_point"/>
+        <boolvalue id="Auxiliary"
+                   label="Auxiliary"
+                   default="false"
+                   tooltip="Construction element"
+                   obligatory="0"
+                   change_visual_attributes="true"/>
+      </feature>
+      <!-- SketchMacroRectangle -->
+      <feature
+        id="SketchMacroRectangle"
+        title="Rectangle"
+        tooltip="Create rectangle"
+        icon="icons/Addons/rectangle.png"
+        helpfile="rectangleFeature.html">
+        <toolbox id="rectangle_type" modified_in_edit="edit_rectangle_type">
+          <box id="rectangle_type_by_start_and_end_points"
+               icon="icons/Sketch/rectangle_pt_rad_32x32.png"
+               title="Start and end points">
+            <sketch-2dpoint_selector id="rectangle_start_point1"                                     
+                                     accept_expressions="0"
+                                     title="Rectangle start point"
+                                     tooltip="Start point coordinates"
+                                     enable_value="enable_by_preferences"/>
+            <sketch-2dpoint_selector id="rectangle_end_point1"
+                                     accept_expressions="0"
+                                     title="Rectangle end point"
+                                     tooltip="End point coordinates"
+                                     enable_value="enable_by_preferences"/>
+            <validator id="GeomValidators_Different" parameters="rectangle_start_point1,rectangle_end_point1"/>
+         </box>
+         <box id="rectangle_type_by_start_and_center_points"
+              icon="icons/Sketch/rectangle_pt_rad_32x32.png"
+              title="Start and center points">
+           <sketch-2dpoint_selector id="rectangle_start_point2"
+                                    accept_expressions="0"
+                                    title="Rectangle start point"
+                                    tooltip="Start point coordinates"
+                                    enable_value="enable_by_preferences"/>
+           <sketch-2dpoint_selector id="rectangle_center_point"
+                                    accept_expressions="0"
+                                    title="Rectangle center point"
+                                    tooltip="Center point coordinates"
+                                    enable_value="enable_by_preferences"/>
+           <validator id="GeomValidators_Different" parameters="rectangle_start_point2,rectangle_center_point"/>
+        </box>
+       </toolbox>
+       <boolvalue id="Auxiliary"
+                  label="Auxiliary"
+                  default="false"
+                  tooltip="Construction element"
+                  obligatory="0"
+                  change_visual_attributes="true"/>
+      </feature>
     </group>
     <group id="Conical geometry">
       <!-- SketchCircle is a hidden feature. It is created inside SketchMacroCircle. -->