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 7883d527907c1939ad3abe19cf838d55646aa02b..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));
 
@@ -142,6 +149,16 @@ PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
 
   myHasConstraintShown[PartSet_Tools::Geometrical] = true;
   myHasConstraintShown[PartSet_Tools::Dimensional] = true;
+
+  Config_PropManager::registerProp("Visualization", "operation_parameter_color",
+                          "Reference shape wireframe color in operation", Config_Prop::Color,
+                          PartSet_CustomPrs::OPERATION_PARAMETER_COLOR());
+  Config_PropManager::registerProp("Visualization", "operation_result_color",
+                          "Result shape wireframe color in operation", Config_Prop::Color,
+                          PartSet_CustomPrs::OPERATION_RESULT_COLOR());
+  Config_PropManager::registerProp("Visualization", "operation_highlight_color",
+                          "Multi selector item color in operation", Config_Prop::Color,
+                          PartSet_CustomPrs::OPERATION_HIGHLIGHT_COLOR());
 }
 
 PartSet_Module::~PartSet_Module()
@@ -153,6 +170,7 @@ PartSet_Module::~PartSet_Module()
       aFilter.Nullify();
   }
   delete myCustomPrs;
+  delete myOverconstraintListener;
 }
 
 void PartSet_Module::activateSelectionFilters()
@@ -192,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);
@@ -233,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();
     }
   }
 }
@@ -259,8 +277,10 @@ void PartSet_Module::operationStarted(ModuleBase_Operation* theOperation)
   }
 
   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
-  if (aFOperation)
-    myCustomPrs->activate(aFOperation->feature(), true);
+  if (aFOperation) {
+    myCustomPrs->activate(aFOperation->feature(), ModuleBase_IModule::CustomizeArguments, true);
+    myCustomPrs->activate(aFOperation->feature(), ModuleBase_IModule::CustomizeResults, true);
+  }
 }
 
 void PartSet_Module::operationResumed(ModuleBase_Operation* theOperation)
@@ -268,13 +288,16 @@ void PartSet_Module::operationResumed(ModuleBase_Operation* theOperation)
   ModuleBase_IModule::operationResumed(theOperation);
 
   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
-  if (aFOperation)
-    myCustomPrs->activate(aFOperation->feature(), true);
+  if (aFOperation) {
+    myCustomPrs->activate(aFOperation->feature(), ModuleBase_IModule::CustomizeArguments, true);
+    myCustomPrs->activate(aFOperation->feature(), ModuleBase_IModule::CustomizeResults, true);
+  }
 }
 
 void PartSet_Module::operationStopped(ModuleBase_Operation* theOperation)
 {
-  bool isModified = myCustomPrs->deactivate(false);
+  bool isModified = myCustomPrs->deactivate(ModuleBase_IModule::CustomizeArguments, false) ||
+                    myCustomPrs->deactivate(ModuleBase_IModule::CustomizeResults, false);
 
   if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
     mySketchMgr->stopNestedSketch(theOperation);
@@ -352,6 +375,11 @@ bool PartSet_Module::canDisplayObject(const ObjectPtr& theObject) const
   return mySketchMgr->canDisplayObject(theObject);
 }
 
+void PartSet_Module::processHiddenObject(const std::list<ObjectPtr>& theObjects)
+{
+  mySketchMgr->processHiddenObject(theObjects);
+}
+
 bool PartSet_Module::canActivateSelection(const ObjectPtr& theObject) const
 {
   bool aCanActivate = ModuleBase_IModule::canActivateSelection(theObject);
@@ -393,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());
   }
 }
@@ -421,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();
 }
@@ -439,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()
 {
@@ -477,7 +543,7 @@ void PartSet_Module::onKeyRelease(ModuleBase_IViewWindow* theWnd, QKeyEvent* the
 {
   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
   XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
-  anOpMgr->onKeyReleased(theEvent);
+  anOpMgr->onKeyReleased(theWnd->viewPort(), theEvent);
 }
 
 void PartSet_Module::onOperationActivatedByPreselection()
@@ -491,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,
@@ -561,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();
@@ -698,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;
@@ -716,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);
@@ -740,11 +803,69 @@ void PartSet_Module::onViewTransformed(int theTrsfType)
     aDisplayer->updateViewer();
 }
 
+void PartSet_Module::activateCustomPrs(const FeaturePtr& theFeature, const ModuleBase_CustomizeFlag& theFlag,
+                                       const bool theUpdateViewer)
+{
+  myCustomPrs->activate(theFeature, theFlag, theUpdateViewer);
+}
+
+void PartSet_Module::deactivateCustomPrs(const ModuleBase_CustomizeFlag& theFlag,
+                                         const bool theUpdateViewer)
+{
+  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)
 {
   bool isRedisplayed = false;
-  if (myCustomPrs->isActive())
+  if (myCustomPrs->isActive(theFlag))
     isRedisplayed = myCustomPrs->redisplay(theObject, theFlag, theUpdateViewer);
 
   return isRedisplayed;
@@ -772,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()));
@@ -791,15 +911,17 @@ ObjectPtr PartSet_Module::findPresentedObject(const AISObjectPtr& theAIS) const
   if (aOperation) {
     /// If last line finished on vertex the lines creation sequence has to be break
     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
-    ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
-    // if there is an active widget, find the presented object in it
-    if (!anActiveWidget)
-      anActiveWidget = aPanel->preselectionWidget();
+    if (aPanel) {
+      ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
+      // if there is an active widget, find the presented object in it
+      if (!anActiveWidget)
+        anActiveWidget = aPanel->preselectionWidget();
     
-    ModuleBase_WidgetValidated* aWidgetValidated = dynamic_cast<ModuleBase_WidgetValidated*>
-                                                                           (anActiveWidget);
-    if (aWidgetValidated)
-      anObject = aWidgetValidated->findPresentedObject(theAIS);
+      ModuleBase_WidgetValidated* aWidgetValidated = dynamic_cast<ModuleBase_WidgetValidated*>
+                                                                             (anActiveWidget);
+      if (aWidgetValidated)
+        anObject = aWidgetValidated->findPresentedObject(theAIS);
+    }
   }
   return anObject;
 }
@@ -871,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();
@@ -916,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)
@@ -1003,6 +1123,41 @@ void PartSet_Module::beforeOperationStopped(ModuleBase_Operation* theOperation)
   }
 }
 
+//******************************************************
+GeomShapePtr PartSet_Module::findShape(const AttributePtr& theAttribute)
+{
+  GeomShapePtr aGeomShape;
+
+  ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
+  if (anOperation && PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
+    aGeomShape = PartSet_Tools::findShapeBy2DPoint(theAttribute, myWorkshop);
+  }
+  return aGeomShape;
+}
+
+//******************************************************
+AttributePtr PartSet_Module::findAttribute(const ObjectPtr& theObject,
+                                           const GeomShapePtr& theGeomShape)
+{
+  AttributePtr anAttribute;
+  GeomShapePtr aGeomShape = theGeomShape;
+  if (!aGeomShape.get()) {
+    // processing shape of result, e.g. sketch circle center is selected, this is a result
+    // the geom shape is empty, the shape of result should be used
+    ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
+    if (aResult.get()) {
+      aGeomShape = aResult->shape();
+    }
+  }
+
+  if (aGeomShape.get()) {
+    TopoDS_Shape aTDSShape = aGeomShape->impl<TopoDS_Shape>();
+    return PartSet_Tools::findAttributeBy2dPoint(theObject, aTDSShape, 
+                                                 mySketchMgr->activeSketch());
+  }
+  return anAttribute;
+}
+
 //******************************************************
 void PartSet_Module::onBooleanOperationChange(int theOperation)
 {
@@ -1022,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();
+}