Salome HOME
"2.11 Constraint with a point from the intersection between an outer edge and plane...
[modules/shaper.git] / src / PartSet / PartSet_Module.cpp
index 47fa2a04f5d5d5f71da9630ad4c7816bfcef7306..641d17ac239f668baa3bd2e5efc6d8d9817dd933 100755 (executable)
@@ -18,6 +18,7 @@
 #include "PartSet_CustomPrs.h"
 #include "PartSet_IconFactory.h"
 #include "PartSet_WidgetChoice.h"
+#include "PartSet_OverconstraintListener.h"
 
 #include "PartSet_Filters.h"
 #include "PartSet_FilterInfinite.h"
@@ -36,6 +37,8 @@
 #include <ModuleBase_FilterFactory.h>
 #include <ModuleBase_Tools.h>
 #include <ModuleBase_OperationFeature.h>
+#include <ModuleBase_WidgetFactory.h>
+#include <ModuleBase_OperationDescription.h>
 
 #include <ModelAPI_Object.h>
 #include <ModelAPI_Events.h>
@@ -60,6 +63,7 @@
 #include <XGUI_SelectionMgr.h>
 #include <XGUI_DataModel.h>
 #include <XGUI_ErrorMgr.h>
+#include <XGUI_CustomPrs.h>
 
 #include <SketchPlugin_Feature.h>
 #include <SketchPlugin_Sketch.h>
@@ -92,6 +96,7 @@
 #include <QMessageBox>
 #include <QMainWindow>
 #include <QLineEdit>
+#include <QString>
 
 #include <GeomAlgoAPI_FaceBuilder.h>
 #include <GeomDataAPI_Dir.h>
@@ -134,6 +139,8 @@ PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
   myMenuMgr = new PartSet_MenuMgr(this);
   myCustomPrs = new PartSet_CustomPrs(theWshop);
 
+  myOverconstraintListener = new PartSet_OverconstraintListener(theWshop);
+
   Events_Loop* aLoop = Events_Loop::loop();
   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED));
 
@@ -163,6 +170,7 @@ PartSet_Module::~PartSet_Module()
       aFilter.Nullify();
   }
   delete myCustomPrs;
+  delete myOverconstraintListener;
 }
 
 void PartSet_Module::activateSelectionFilters()
@@ -202,6 +210,8 @@ void PartSet_Module::registerValidators()
   aFactory->registerValidator("PartSet_FilletSelection", new PartSet_FilletSelection);
   aFactory->registerValidator("PartSet_AngleSelection", new PartSet_AngleSelection);
   aFactory->registerValidator("PartSet_EqualSelection", new PartSet_EqualSelection);
+  aFactory->registerValidator("PartSet_CollinearSelection", new PartSet_CollinearSelection);
+  aFactory->registerValidator("PartSet_MiddlePointSelection", new PartSet_MiddlePointSelection);
   aFactory->registerValidator("PartSet_DifferentObjects", new PartSet_DifferentObjectsValidator);
   aFactory->registerValidator("PartSet_CoincidentAttr", new PartSet_CoincidentAttr);
   aFactory->registerValidator("PartSet_SketchEntityValidator", new PartSet_SketchEntityValidator);
@@ -243,9 +253,7 @@ void PartSet_Module::operationCommitted(ModuleBase_Operation* theOperation)
       // the selection is cleared after commit the create operation
       // in order to do not use the same selected objects in the restarted operation
       // for common behaviour, the selection is cleared even if the operation is not restarted
-      XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
-      XGUI_Workshop* aWorkshop = aConnector->workshop();
-      aWorkshop->selector()->clearSelection();
+      getWorkshop()->selector()->clearSelection();
     }
   }
 }
@@ -413,9 +421,7 @@ void PartSet_Module::grantedOperationIds(ModuleBase_Operation* theOperation,
   myMenuMgr->grantedOperationIds(theOperation, theIds);
 
   if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
-    XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
-    XGUI_Workshop* aWorkshop = aConnector->workshop();
-
+    XGUI_Workshop* aWorkshop = getWorkshop();
     theIds.append(aWorkshop->contextMenuMgr()->action("DELETE_CMD")->text());
   }
 }
@@ -441,8 +447,7 @@ void PartSet_Module::clearViewer()
 {
   myCustomPrs->clearPrs();
 
-  XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
-  XGUI_Workshop* aWorkshop = aConnector->workshop();
+  XGUI_Workshop* aWorkshop = getWorkshop();
   XGUI_Displayer* aDisplayer = aWorkshop->displayer();
   aDisplayer->deactivateSelectionFilters();
 }
@@ -459,6 +464,47 @@ void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
     aPanel->activateWidget(aPanel->modelWidgets().first());
 }
 
+bool PartSet_Module::createWidgets(ModuleBase_Operation* theOperation,
+                                   QList<ModuleBase_ModelWidget*>& theWidgets) const
+{
+  bool aProcessed = false;
+
+  ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
+  XGUI_Workshop* aWorkshop = getWorkshop();
+  XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
+  if (mySketchMgr->activeSketch().get() && aFOperation && aPropertyPanel) {
+    ModuleBase_ISelection* aSelection = workshop()->selection();
+    // click on a point in sketch leads here with the point is highlighted, not yet selected
+    QList<ModuleBase_ViewerPrs> aPreselection = aSelection->getHighlighted();
+    if (aPreselection.size() == 1) {
+      ModuleBase_ViewerPrs aSelectedPrs = aPreselection[0];
+      ObjectPtr anObject = aSelectedPrs.object();
+
+      FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
+      FeaturePtr anOpFeature = aFOperation->feature();
+      TopoDS_Shape aShape = aSelectedPrs.shape();
+      // click on the digit of dimension constrain comes here with an empty shape, so we need the check
+      if (aFeature == anOpFeature && !aShape.IsNull()) {
+        AttributePtr anAttribute = PartSet_Tools::findAttributeBy2dPoint(anObject, aShape,
+                                                                         mySketchMgr->activeSketch());
+        if (anAttribute.get()) {
+          QString aXmlRepr = aFOperation->getDescription()->xmlRepresentation();
+          ModuleBase_WidgetFactory aFactory(aXmlRepr.toStdString(), workshop());
+
+          const std::string anAttributeId = anAttribute->id();
+          aFactory.createWidget(aPropertyPanel->contentWidget(), anAttributeId);
+
+          theWidgets = aFactory.getModelWidgets();
+          // it is possible that the point does not present in XML definition,
+          // in this case, we assume that it is not processed by this module
+          // e.g. "Intersection point" feature
+          aProcessed = !theWidgets.isEmpty();
+        }
+      }
+    }
+  }
+  return aProcessed;
+}
 
 void PartSet_Module::onSelectionChanged()
 {
@@ -511,8 +557,7 @@ ModuleBase_ModelWidget* PartSet_Module::createWidgetByType(const std::string& th
                                             Config_WidgetAPI* theWidgetApi, std::string theParentId)
 {
   ModuleBase_IWorkshop* aWorkshop = workshop();
-  XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
-  XGUI_Workshop* aXUIWorkshop = aConnector->workshop();
+  XGUI_Workshop* aXUIWorkshop = getWorkshop();
   ModuleBase_ModelWidget* aWgt = NULL;
   if (theType == "sketch-start-label") {
     PartSet_WidgetSketchLabel* aLabelWgt = new PartSet_WidgetSketchLabel(theParent, aWorkshop,
@@ -581,8 +626,7 @@ bool PartSet_Module::deleteObjects()
 {
   bool isProcessed = false;
 
-  XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
-  XGUI_Workshop* aWorkshop = aConnector->workshop();
+  XGUI_Workshop* aWorkshop = getWorkshop();
   XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
 
   //SessionPtr aMgr = ModelAPI_Session::get();
@@ -718,8 +762,7 @@ void PartSet_Module::onViewTransformed(int theTrsfType)
 
   //Handle(V3d_View) aView = aViewer->activeView();
 
-  XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
-  XGUI_Workshop* aWorkshop = aConnector->workshop();
+  XGUI_Workshop* aWorkshop = getWorkshop();
   XGUI_Displayer* aDisplayer = aWorkshop->displayer();
   Handle(V3d_Viewer) aV3dViewer = aContext->CurrentViewer();
   Handle(V3d_View) aView;
@@ -736,7 +779,7 @@ void PartSet_Module::onViewTransformed(int theTrsfType)
   }
   if (aView.IsNull())
     return;
-  double aLen = aView->Convert(20);
+  double aLen = aView->Convert(SketcherPrs_Tools::getDefaultArrowSize());
 
   double aPrevLen = SketcherPrs_Tools::getArrowSize();
   SketcherPrs_Tools::setArrowSize(aLen);
@@ -772,6 +815,52 @@ void PartSet_Module::deactivateCustomPrs(const ModuleBase_CustomizeFlag& theFlag
   myCustomPrs->deactivate(theFlag, theUpdateViewer);
 }
 
+bool PartSet_Module::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
+                                           std::shared_ptr<GeomAPI_ICustomPrs> theCustomPrs)
+{
+  bool aCustomized = false;
+
+  if (theResult.get())
+    return aCustomized;
+
+  XGUI_Workshop* aWorkshop = getWorkshop();
+  XGUI_Displayer* aDisplayer = aWorkshop->displayer();
+  ObjectPtr anObject = aDisplayer->getObject(thePrs);
+  if (anObject.get()) {
+    bool isConflicting = myOverconstraintListener->isConflictingObject(anObject);
+    // customize sketcy symbol presentation
+    if (thePrs.get()) {
+      Handle(AIS_InteractiveObject) anAISIO = thePrs->impl<Handle(AIS_InteractiveObject)>();
+      if (!anAISIO.IsNull()) {
+        if (!Handle(SketcherPrs_SymbolPrs)::DownCast(anAISIO).IsNull()) {
+          Handle(SketcherPrs_SymbolPrs) aPrs = Handle(SketcherPrs_SymbolPrs)::DownCast(anAISIO);
+          if (!aPrs.IsNull()) {
+            std::vector<int> aColor;
+            myOverconstraintListener->getConflictingColor(aColor);
+            aPrs->SetConflictingConstraint(isConflicting, aColor);
+            aCustomized = true;
+          }
+        }
+      }
+    }
+    // customize sketch dimension constraint presentation
+    if (!aCustomized) {
+      std::vector<int> aColor;
+      if (isConflicting) {
+        myOverconstraintListener->getConflictingColor(aColor);
+      }
+      if (aColor.empty())
+        XGUI_CustomPrs::getDefaultColor(anObject, true, aColor);
+      if (!aColor.empty()) {
+        thePrs->setColor(aColor[0], aColor[1], aColor[2]);
+        aCustomized = true;
+      }
+    }
+  }
+
+  return aCustomized;
+}
+
 bool PartSet_Module::customizeObject(ObjectPtr theObject, const ModuleBase_CustomizeFlag& theFlag,
                                      const bool theUpdateViewer)
 {
@@ -804,8 +893,7 @@ void PartSet_Module::onActiveDocPopup(const QPoint& thePnt)
   SessionPtr aMgr = ModelAPI_Session::get();
   QAction* aActivatePartAction = myMenuMgr->action("ACTIVATE_PARTSET_CMD");
 
-  XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
-  XGUI_Workshop* aWorkshop = aConnector->workshop();
+  XGUI_Workshop* aWorkshop = getWorkshop();
   QLabel* aHeader = aWorkshop->objectBrowser()->activeDocLabel();
 
   aActivatePartAction->setEnabled((aMgr->activeDocument() != aMgr->moduleDocument()));
@@ -905,8 +993,7 @@ void PartSet_Module::processEvent(const std::shared_ptr<Events_Message>& theMess
     if (myWorkshop->currentOperation() && 
       (!aAllowActivationList.contains(myWorkshop->currentOperation()->id())))
       return;
-    XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
-    XGUI_Workshop* aWorkshop = aConnector->workshop();
+    XGUI_Workshop* aWorkshop = getWorkshop();
     XGUI_DataTree* aTreeView = aWorkshop->objectBrowser()->treeView();
     QLabel* aLabel = aWorkshop->objectBrowser()->activeDocLabel();
     QPalette aPalet = aLabel->palette();
@@ -950,8 +1037,7 @@ void PartSet_Module::onTreeViewDoubleClick(const QModelIndex& theIndex)
   if (theIndex.column() != 0) // Use only first column
     return;
 
-  XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
-  XGUI_Workshop* aWorkshop = aConnector->workshop();
+  XGUI_Workshop* aWorkshop = getWorkshop();
   XGUI_DataModel* aDataModel = aWorkshop->objectBrowser()->dataModel();
   // De not use non editable Indexes
   if ((aDataModel->flags(theIndex) & Qt::ItemIsSelectable) == 0)
@@ -1091,3 +1177,10 @@ void PartSet_Module::onBooleanOperationChange(int theOperation)
     break;
   }
 }
+
+//******************************************************
+XGUI_Workshop* PartSet_Module::getWorkshop() const
+{
+  XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
+  return aConnector->workshop();
+}