Salome HOME
Task 2.6: Creation of horizontal and vertical constraints “on a fly”
authorvsv <vsv@opencascade.com>
Mon, 12 Aug 2019 14:03:06 +0000 (17:03 +0300)
committervsv <vsv@opencascade.com>
Mon, 12 Aug 2019 14:03:06 +0000 (17:03 +0300)
src/ConstructionPlugin/ConstructionPlugin_Plugin.cpp
src/GeomAPI/GeomAPI_Dir2d.h
src/GeomAPI/GeomAPI_Lin2d.h
src/PartSet/PartSet_Module.cpp
src/PartSet/PartSet_SketcherMgr.h
src/PartSet/PartSet_SketcherReentrantMgr.cpp
src/PartSet/PartSet_SketcherReentrantMgr.h
src/PartSet/PartSet_WidgetSketchLabel.cpp
src/PartSet/PartSet_WidgetSketchLabel.h

index 0b0dc4bb297c7c602e1d9093af5f0e8fde747a91..637e37b4e8de2f9edff675305ab4198034e474f2 100644 (file)
@@ -58,6 +58,8 @@ ConstructionPlugin_Plugin::ConstructionPlugin_Plugin()
                                    PLANE_SIZE, "0", "1000");
   Config_PropManager::registerProp(SKETCH_TAB_NAME, "planes_thickness", "Thickness",
     Config_Prop::IntSpin, SKETCH_WIDTH);
+  Config_PropManager::registerProp(SKETCH_TAB_NAME, "angular_tolerance", "Angular tolerance",
+    Config_Prop::DblSpin, "0.04");
   Config_PropManager::registerProp(SKETCH_TAB_NAME, "rotate_to_plane",
     "Rotate to plane when selected", Config_Prop::Boolean, "false");
 
index c525e8c3fc2a86e691d352b38d155c553db87e8b..1b7a0d35d0416ff7f1dda68865735a03d6c6bbe0 100644 (file)
@@ -66,5 +66,8 @@ class GeomAPI_Dir2d : public GeomAPI_Interface
   double angle(const std::shared_ptr<GeomAPI_Dir2d>& theArg) const;
 };
 
+//! Pointer on the object
+typedef std::shared_ptr<GeomAPI_Dir2d> GeomDir2dPtr;
+
 #endif
 
index 3fdda3048163340b611f9800183732be0c186845..725c28f1f09103db7cc48c6059e32de340cf6e3f 100644 (file)
@@ -76,5 +76,9 @@ class GeomAPI_Lin2d : public GeomAPI_Interface
   std::shared_ptr<GeomAPI_Pnt2d> shiftedLocation(double theShift) const;
 };
 
+
+//! Pointer on the object
+typedef std::shared_ptr<GeomAPI_Lin2d> GeomLine2dPtr;
+
 #endif
 
index e9dbb6a38ace075ebd901aeec175cc17aa78e3f9..8d1b0a0242704ee83a3381f18f0237862b43720c 100644 (file)
@@ -895,6 +895,7 @@ ModuleBase_ModelWidget* PartSet_Module::createWidgetByType(const std::string& th
     connect(aLabelWgt, SIGNAL(showConstraintToggled(int, bool)),
       mySketchMgr, SLOT(onShowConstraintsToggle(int, bool)));
     connect(aLabelWgt, SIGNAL(showFreePoints(bool)), mySketchMgr, SLOT(onShowPoints(bool)));
+    connect(aLabelWgt, SIGNAL(autoConstraints(bool)), sketchReentranceMgr(), SLOT(onAutoConstraints(bool)));
     aLabelWgt->setShowPointsState(mySketchMgr->isShowFreePointsShown());
     aWgt = aLabelWgt;
   } else if (theType == "sketch-2dpoint_selector") {
index 07c3160e6b226cdee4c875af93cdc460881f359c..3f29ba66117cfe1fea716fdfc3e563336ccf32e5 100644 (file)
@@ -344,6 +344,8 @@ public slots:
   /// Process sketch plane selected event
   void onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>& thePln);
 
+  /// The slot is called when user checks "Show free points" button
+  /// \param toShow a state of the check box
   void onShowPoints(bool toShow);
 
 private slots:
index 1e117834c1c9001ea113dde6092dbed8f8f04625..cd95a10e44c522b2f1dacab1ded701ed9c467088 100644 (file)
@@ -31,6 +31,9 @@
 
 #include "GeomDataAPI_Point2D.h"
 
+#include "GeomAPI_Lin2d.h"
+#include "GeomAPI_Dir2d.h"
+
 #include <ModuleBase_IPropertyPanel.h>
 #include <ModuleBase_ISelectionActivate.h>
 #include <ModuleBase_OperationFeature.h>
@@ -51,6 +54,8 @@
 #include <SketchPlugin_Point.h>
 #include <SketchPlugin_Trim.h>
 #include <SketchPlugin_Split.h>
+#include <SketchPlugin_ConstraintHorizontal.h>
+#include <SketchPlugin_ConstraintVertical.h>
 
 #include <XGUI_Workshop.h>
 #include <XGUI_ModuleConnector.h>
@@ -69,7 +74,8 @@ PartSet_SketcherReentrantMgr::PartSet_SketcherReentrantMgr(ModuleBase_IWorkshop*
   myRestartingMode(RM_None),
   myIsFlagsBlocked(false),
   myIsInternalEditOperation(false),
-  myNoMoreWidgetsAttribute("")
+  myNoMoreWidgetsAttribute(""),
+  myIsAutoConstraints(true)
 {
 }
 
@@ -171,6 +177,8 @@ bool PartSet_SketcherReentrantMgr::processMouseMoved(ModuleBase_IViewWindow* the
         workshop()->selector()->clearSelection();
 
         myPreviousFeature = aFOperation->feature();
+        if (myIsAutoConstraints)
+          addConstraints(myPreviousFeature);
         restartOperation();
         myPreviousFeature = FeaturePtr();
 
@@ -821,3 +829,45 @@ void PartSet_SketcherReentrantMgr::setInternalActiveWidget(ModuleBase_ModelWidge
       aPropertyPanel->setInternalActiveWidget(theWidget);
   }
 }
+
+void PartSet_SketcherReentrantMgr::onAutoConstraints(bool isOn)
+{
+  myIsAutoConstraints = isOn;
+}
+
+void PartSet_SketcherReentrantMgr::addConstraints(const FeaturePtr& theFeature)
+{
+  static GeomDir2dPtr myHorDir(new GeomAPI_Dir2d(1, 0));
+  static GeomDir2dPtr myVertDir(new GeomAPI_Dir2d(0, 1));
+
+  if (theFeature->getKind() == SketchPlugin_Line::ID()) {
+    std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
+    std::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
+      std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::START_ID()));
+    std::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
+      std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
+    if (aPoint1.get() && aPoint2.get()) {
+      GeomLine2dPtr aLine(new GeomAPI_Lin2d(aPoint1->pnt(), aPoint2->pnt()));
+      GeomDir2dPtr aDir = aLine->direction();
+      double aHorAngle = fabs(myHorDir->angle(aDir));
+      double aVertAngle = fabs(myVertDir->angle(aDir));
+      if (aHorAngle > M_PI/2.)
+        aHorAngle = M_PI - aHorAngle;
+      if (aVertAngle > M_PI/2.)
+        aVertAngle = M_PI - aVertAngle;
+
+      double aTolerance = Config_PropManager::real(SKETCH_TAB_NAME, "angular_tolerance");
+      CompositeFeaturePtr aSketch = module()->sketchMgr()->activeSketch();
+      FeaturePtr aFeature;
+      if (aHorAngle < aTolerance)
+        // Add horizontal constraint
+        aFeature = aSketch->addFeature(SketchPlugin_ConstraintHorizontal::ID());
+      else if (aVertAngle < aTolerance)
+        // Add vertical constraint
+        aFeature = aSketch->addFeature(SketchPlugin_ConstraintVertical::ID());
+
+      if (aFeature.get())
+        aFeature->refattr(SketchPlugin_Constraint::ENTITY_A())->setObject(theFeature->firstResult());
+    }
+  }
+}
index fc2f6723cc9440e9edfa4e1b5cc1943b86d9ebbb..91ab34ebcef3c26f8cdefe4dca02fb8db0facd8c 100644 (file)
@@ -131,6 +131,15 @@ public:
   /// \param theMessage a message of reentrant operation
   void setReentrantPreSelection(const std::shared_ptr<Events_Message>& theMessage);
 
+  bool isAutoConstraints() const { return myIsAutoConstraints; }
+
+
+public slots:
+  /// The slot is called when user checks "Automatic constraints" button
+  /// \param isOn a state of the check box
+  void onAutoConstraints(bool isOn);
+
+
 private slots:
   /// SLOT, that is called by a widget activating in the property panel
   /// If the 'internal' edit operation is started, it activates the first widget selection
@@ -211,6 +220,8 @@ private:
 
   void setInternalActiveWidget(ModuleBase_ModelWidget* theWidget);
 
+  void addConstraints(const FeaturePtr& theFeature);
+
 private:
   ModuleBase_IWorkshop* myWorkshop; /// the workshop
 
@@ -227,6 +238,8 @@ private:
   ObjectPtr mySelectedObject; /// cashed selected object
   std::shared_ptr<ModelAPI_Attribute> mySelectedAttribute; /// cashed selected attribute
   std::shared_ptr<GeomAPI_Pnt2d> myClickedSketchPoint; /// cashed clicked point
+
+  bool myIsAutoConstraints;
 };
 
 #endif
index 231c512fa895b2770608c9b24c5c84b1e9240fd0..3f4c8d82990cdffa479015483714a616858e2a38 100644 (file)
@@ -21,6 +21,7 @@
 #include "PartSet_Tools.h"
 #include "PartSet_Module.h"
 #include "PartSet_PreviewPlanes.h"
+#include "PartSet_SketcherReentrantMgr.h"
 
 #include "SketchPlugin_SketchEntity.h"
 
@@ -171,6 +172,10 @@ myIsSelection(false)
   connect(myShowPoints, SIGNAL(toggled(bool)), this, SIGNAL(showFreePoints(bool)));
   aLayout->addWidget(myShowPoints);
 
+  myAutoConstraints = new QCheckBox(tr("Automatic constraints"), this);
+  connect(myAutoConstraints, SIGNAL(toggled(bool)), this, SIGNAL(autoConstraints(bool)));
+  aLayout->addWidget(myAutoConstraints);
+
   QPushButton* aPlaneBtn = new QPushButton(tr("Change sketch plane"), aSecondWgt);
   connect(aPlaneBtn, SIGNAL(clicked(bool)), SLOT(onChangePlane()));
   aLayout->addWidget(aPlaneBtn);
@@ -508,6 +513,13 @@ bool PartSet_WidgetSketchLabel::fillSketchPlaneBySelection(const ModuleBase_View
 
 void PartSet_WidgetSketchLabel::activateCustom()
 {
+  PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
+  if (aModule) {
+    bool isBlocked = myAutoConstraints->blockSignals(true);
+    myAutoConstraints->setChecked(aModule->sketchReentranceMgr()->isAutoConstraints());
+    myAutoConstraints->blockSignals(isBlocked);
+  }
+
   std::shared_ptr<GeomAPI_Pln> aPlane = plane();
   if (aPlane.get()) {
     myStackWidget->setCurrentIndex(1);
index 74a16f2248f33199e47f8d54c402636130499e05..b4158194bf4f8947b666b5cf621aeb5d9f63086c 100644 (file)
@@ -118,8 +118,14 @@ signals:
   /// \param theState a state of the check box
   void showConstraintToggled(int theType, bool theState);
 
+  /// The signal is emitted when user checks "Show free points" button
+  /// \param toShow a state of the check box
   void showFreePoints(bool toShow);
 
+  /// The signal is emitted when user checks "Automatic constraints" button
+  /// \param isOn a state of the check box
+  void autoConstraints(bool isOn);
+
 protected:
   /// Creates a backup of the current values of the attribute
   /// It should be realized in the specific widget because of different
@@ -223,6 +229,7 @@ private:
   QCheckBox* myViewInverted;
   QCheckBox* myRemoveExternal;
   QCheckBox* myShowPoints;
+  QCheckBox* myAutoConstraints;
 
   QMap<PartSet_Tools::ConstraintVisibleState, QCheckBox*> myShowConstraints;