Salome HOME
Issue #849 (#775) Crash in second study during sketch operation
[modules/shaper.git] / src / PartSet / PartSet_WidgetSketchLabel.cpp
index c2ca8a4d2d863cefb118caf7f7dd05e92a5cb639..e7f7e0d1c564ee074d1d91a4d3b4b3abf130eb74 100644 (file)
@@ -15,6 +15,7 @@
 #include <XGUI_Selection.h>
 #include <XGUI_ViewerProxy.h>
 #include <XGUI_ActionsMgr.h>
+#include <XGUI_ModuleConnector.h>
 
 #include <ModuleBase_Operation.h>
 #include <ModuleBase_ViewerPrs.h>
@@ -25,6 +26,7 @@
 #include <ModelAPI_Tools.h>
 
 #include <GeomAlgoAPI_FaceBuilder.h>
+#include <GeomAlgoAPI_ShapeTools.h>
 #include <GeomDataAPI_Point.h>
 #include <GeomDataAPI_Dir.h>
 #include <GeomAPI_XYZ.h>
@@ -32,6 +34,7 @@
 #include <SketchPlugin_Sketch.h>
 #include <SketcherPrs_Tools.h>
 
+#include <BRepClass3d_SolidClassifier.hxx>
 #include <Precision.hxx>
 #include <gp_Pln.hxx>
 #include <gp_Pnt.hxx>
 #include <Config_PropManager.h>
 
 #include <QLabel>
-//#include <QTimer>
 #include <QApplication>
 #include <QVBoxLayout>
 #include <QCheckBox>
 
 
 PartSet_WidgetSketchLabel::PartSet_WidgetSketchLabel(QWidget* theParent,
+                                                     ModuleBase_IWorkshop* theWorkshop,
                                                      const Config_WidgetAPI* theData,
                                                      const std::string& theParentId,
                                                      bool toShowConstraints)
-    : ModuleBase_WidgetValidated(theParent, theData, theParentId),
-      myPreviewDisplayed(false),
-      myWorkshop(NULL)
+: ModuleBase_WidgetValidated(theParent, theWorkshop, theData, theParentId),
+  myPreviewDisplayed(false)
 {
   myText = QString::fromStdString(theData->getProperty("title"));
   myLabel = new QLabel("", theParent);
@@ -64,10 +66,6 @@ PartSet_WidgetSketchLabel::PartSet_WidgetSketchLabel(QWidget* theParent,
   myLabel->setToolTip("");
   myLabel->setIndent(5);
 
-  //mySelectionTimer = new QTimer(this);
-  //connect(mySelectionTimer, SIGNAL(timeout()), SLOT(setSketchingMode()));
-  //mySelectionTimer->setSingleShot(true);
-
   QVBoxLayout* aLayout = new QVBoxLayout(this);
   ModuleBase_Tools::zeroMargins(aLayout);
   aLayout->addWidget(myLabel);
@@ -85,7 +83,8 @@ PartSet_WidgetSketchLabel::~PartSet_WidgetSketchLabel()
   erasePreviewPlanes();
 }
 
-bool PartSet_WidgetSketchLabel::setSelection(const QList<ModuleBase_ViewerPrs>& theValues, int& thePosition)
+bool PartSet_WidgetSketchLabel::setSelection(QList<ModuleBase_ViewerPrs>& theValues,
+                                             const bool theToValidate)
 {
   // do not use the given selection if the plane of the sketch has been already set.
   // If this check is absent, a selected plane in the viewer can be set in the sketch
@@ -93,7 +92,12 @@ bool PartSet_WidgetSketchLabel::setSelection(const QList<ModuleBase_ViewerPrs>&
   if (plane().get())
     return true;
 
-  return ModuleBase_WidgetValidated::setSelection(theValues, thePosition);
+  ModuleBase_ViewerPrs aPrs = theValues.first();
+  bool aDone = ModuleBase_WidgetValidated::setSelection(theValues, theToValidate);
+  if (aDone)
+    updateByPlaneSelected(aPrs);
+
+  return aDone;
 }
 
 QList<QWidget*> PartSet_WidgetSketchLabel::getControls() const
@@ -105,30 +109,40 @@ QList<QWidget*> PartSet_WidgetSketchLabel::getControls() const
 
 void PartSet_WidgetSketchLabel::onSelectionChanged()
 {
-  QList<ModuleBase_ViewerPrs> aSelectedPrs = getSelectedEntitiesOrObjects(
-                                             myWorkshop->selector()->selection());
-  if (aSelectedPrs.empty())
-    return;
-  ModuleBase_ViewerPrs aPrs = aSelectedPrs.first();
-  if (aPrs.isEmpty() || !isValidSelection(aPrs))
+  QList<ModuleBase_ViewerPrs> aSelected = getFilteredSelected();
+
+  if (aSelected.empty())
     return;
+  ModuleBase_ViewerPrs aPrs = aSelected.first();
+
+  bool aDone = ModuleBase_WidgetValidated::setSelection(aSelected, false);
+  if (aDone)
+    updateByPlaneSelected(aPrs);
+}
 
-  // 2. set the selection to sketch
-  setSelectionCustom(aPrs);
-  // 3. hide main planes if they have been displayed
+void PartSet_WidgetSketchLabel::updateByPlaneSelected(const ModuleBase_ViewerPrs& thePrs)
+{
+  // 1. hide main planes if they have been displayed
   erasePreviewPlanes();
-  // 4. if the planes were displayed, change the view projection
-  TopoDS_Shape aShape = aPrs.shape();
+  // 2. if the planes were displayed, change the view projection
+  TopoDS_Shape aShape = thePrs.shape();
   std::shared_ptr<GeomAPI_Shape> aGShape;
+  std::shared_ptr<GeomAPI_Shape> aBaseShape;
+
+  DataPtr aData = feature()->data();
+  AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
+                            (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
+
   // selection happens in OCC viewer
   if (!aShape.IsNull()) {
-    aGShape =  std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
+    aGShape =  std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
     aGShape->setImpl(new TopoDS_Shape(aShape));
+
+    if (aSelAttr && aSelAttr->context()) {
+      aBaseShape = aSelAttr->context()->shape();
+    }
   }
   else { // selection happens in OCC viewer(on body) of in the OB browser
-    DataPtr aData = feature()->data();
-    AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
-                              (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
     if (aSelAttr) {
       aGShape = aSelAttr->value();
     }
@@ -137,25 +151,44 @@ void PartSet_WidgetSketchLabel::onSelectionChanged()
     // get plane parameters
     std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
     std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
+    gp_XYZ aXYZ = aDir->impl<gp_Dir>().XYZ();
+
+    // orienting projection
+    if(aBaseShape.get() != NULL) {
+      std::shared_ptr<GeomAPI_Pnt> aCenterPnt = GeomAlgoAPI_ShapeTools::centreOfMass(aGShape);
+      gp_Pnt aPnt = aCenterPnt->impl<gp_Pnt>();
+      aPnt.Translate(aDir->impl<gp_Dir>().XYZ() * (10 * Precision::Confusion()));
+
+      BRepClass3d_SolidClassifier aClassifier;
+      aClassifier.Load(aBaseShape->impl<TopoDS_Shape>());
+      aClassifier.Perform(aPnt, Precision::Confusion());
+
+      if(aClassifier.State() == TopAbs_IN) {
+        aXYZ.Reverse();
+      }
+    }
 
-    myWorkshop->viewer()->setViewProjection(aDir->x(), aDir->y(), aDir->z());
+    myWorkshop->viewer()->setViewProjection(aXYZ.X(), aXYZ.Y(), aXYZ.Z());
   }
-  // 5. Clear text in the label
+  // 3. Clear text in the label
   myLabel->setText("");
   myLabel->setToolTip("");
-  disconnect(myWorkshop->selector(), SIGNAL(selectionChanged()), 
+  disconnect(workshop()->selector(), SIGNAL(selectionChanged()), 
               this, SLOT(onSelectionChanged()));
-  // 6. deactivate face selection filter
-  activateFilters(myWorkshop->module()->workshop(), false);
+  // 4. deactivate face selection filter
+  activateFilters(false);
 
-  // 7. Clear selection mode and define sketching mode
-  //XGUI_Displayer* aDisp = myWorkshop->displayer();
+  // 5. Clear selection mode and define sketching mode
+  //XGUI_Displayer* aDisp = workshop()->displayer();
   //aDisp->closeLocalContexts();
   emit planeSelected(plane());
-  //setSketchingMode();
+  // after the plane is selected in the sketch, the sketch selection should be activated
+  // it can not be performed in the sketch label widget because, we don't need to switch off
+  // the selection by any label deactivation, but need to switch it off by stop the sketch
+  activateSelection(true);
 
-  // 8. Update sketcher actions
-  XGUI_ActionsMgr* anActMgr = myWorkshop->actionsMgr();
+  // 6. Update sketcher actions
+  XGUI_ActionsMgr* anActMgr = workshop()->actionsMgr();
   anActMgr->update();
   myWorkshop->viewer()->update();
 }
@@ -164,7 +197,6 @@ std::shared_ptr<GeomAPI_Pln> PartSet_WidgetSketchLabel::plane() const
 {
   CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
   return PartSet_Tools::sketchPlane(aSketch);
-
 }
 
 bool PartSet_WidgetSketchLabel::focusTo()
@@ -180,10 +212,13 @@ void PartSet_WidgetSketchLabel::enableFocusProcessing()
 
 void PartSet_WidgetSketchLabel::storeAttributeValue()
 {
+  ModuleBase_WidgetValidated::storeAttributeValue();
 }
 
 void PartSet_WidgetSketchLabel::restoreAttributeValue(const bool theValid)
 {
+  ModuleBase_WidgetValidated::restoreAttributeValue(theValid);
+
   // it is not necessary to save the previous plane value because the plane is chosen once
   DataPtr aData = feature()->data();
   AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
@@ -226,7 +261,7 @@ bool PartSet_WidgetSketchLabel::setSelectionCustom(const ModuleBase_ViewerPrs& t
   }
   else if (!aShape.IsNull()) {
     aDir = setSketchPlane(aShape);
-    isOwnerSet = aDir;
+    isOwnerSet = aDir.get();
   }
   return isOwnerSet;
 }
@@ -234,11 +269,13 @@ bool PartSet_WidgetSketchLabel::setSelectionCustom(const ModuleBase_ViewerPrs& t
 void PartSet_WidgetSketchLabel::activateCustom()
 {
   std::shared_ptr<GeomAPI_Pln> aPlane = plane();
-  if (aPlane.get())
+  if (aPlane.get()) {
+    activateSelection(true);
     return;
+  }
 
   bool aBodyIsVisualized = false;
-  XGUI_Displayer* aDisp = myWorkshop->displayer();
+  XGUI_Displayer* aDisp = workshop()->displayer();
   QObjectPtrList aDisplayed = aDisp->displayedObjects();
   foreach (ObjectPtr anObj, aDisplayed) {
     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
@@ -253,33 +290,46 @@ void PartSet_WidgetSketchLabel::activateCustom()
     // We have to select a plane before any operation
     showPreviewPlanes();
   }
-  QIntList aModes;
-  aModes << TopAbs_FACE;
-  aDisp->activateObjects(aModes);
+  activateSelection(true);
 
   myLabel->setText(myText);
   myLabel->setToolTip(myTooltip);
 
-  connect(myWorkshop->selector(), SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
-  activateFilters(myWorkshop->module()->workshop(), true);
+  connect(workshop()->selector(), SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
+  activateFilters(true);
 
   aDisp->updateViewer();
 }
 
 void PartSet_WidgetSketchLabel::deactivate()
 {
-  // Do not set selection mode if the widget was activated for a small moment 
-  //mySelectionTimer->stop();
-  //XGUI_Displayer* aDisp = myWorkshop->displayer();
-  //aDisp->closeLocalContexts();
   erasePreviewPlanes();
-  activateFilters(myWorkshop->module()->workshop(), false);
+  activateSelection(false);
+
+  activateFilters(false);
+}
+
+void PartSet_WidgetSketchLabel::activateSelection(bool toActivate)
+{
+  if (toActivate) {
+    QIntList aModes;
+    std::shared_ptr<GeomAPI_Pln> aPlane = plane();
+    if (aPlane.get()) {
+      myWorkshop->module()->activeSelectionModes(aModes);
+    }
+    else {
+      aModes << TopAbs_FACE;
+    }
+    myWorkshop->activateSubShapesSelection(aModes);
+  } else {
+    myWorkshop->deactivateSubShapesSelection();
+  }
 }
 
 void PartSet_WidgetSketchLabel::erasePreviewPlanes()
 {
   if (myPreviewDisplayed) {
-    XGUI_Displayer* aDisp = myWorkshop->displayer();
+    XGUI_Displayer* aDisp = workshop()->displayer();
     aDisp->eraseAIS(myYZPlane, false);
     aDisp->eraseAIS(myXZPlane, false);
     aDisp->eraseAIS(myXYPlane, false);
@@ -314,10 +364,10 @@ void PartSet_WidgetSketchLabel::showPreviewPlanes()
     myXZPlane = createPreviewPlane(anOrigin, aXZDir, aG);
     myXYPlane = createPreviewPlane(anOrigin, aXYDir, aB);
   }
-  XGUI_Displayer* aDisp = myWorkshop->displayer();
-  aDisp->displayAIS(myYZPlane, false);
-  aDisp->displayAIS(myXZPlane, false);
-  aDisp->displayAIS(myXYPlane, false);
+  XGUI_Displayer* aDisp = workshop()->displayer();
+  aDisp->displayAIS(myYZPlane, true, false);
+  aDisp->displayAIS(myXZPlane, true, false);
+  aDisp->displayAIS(myXYPlane, true, false);
   myPreviewDisplayed = true;
 }
 
@@ -345,6 +395,8 @@ std::shared_ptr<GeomAPI_Dir> PartSet_WidgetSketchLabel::setSketchPlane(const Top
   std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
   aGShape->setImpl(new TopoDS_Shape(theShape));
 
+
+
   // get plane parameters
   std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
 
@@ -380,28 +432,14 @@ std::shared_ptr<GeomAPI_Dir> PartSet_WidgetSketchLabel::setSketchPlane(const Top
   return aDir;
 }
 
-
-/*void PartSet_WidgetSketchLabel::setSketchingMode()
-{
-  XGUI_Displayer* aDisp = myWorkshop->displayer();
-  // Clear standard selection modes if they are defined
-  //aDisp->activateObjects(aModes);
-  //aDisp->openLocalContext();
-
-  // Get default selection modes
-  
-  QIntList aModes;
-  aModes.append(SketcherPrs_Tools::Sel_Dimension_Text);
-  aModes.append(SketcherPrs_Tools::Sel_Dimension_Line);
-  aModes.append(SketcherPrs_Tools::Sel_Constraint);
-  aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_VERTEX));
-  aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_EDGE));
-
-  aDisp->activateObjects(aModes);
-}*/
-
 void PartSet_WidgetSketchLabel::showConstraints(bool theOn)
 {
   myShowConstraints->setChecked(theOn);
   emit showConstraintToggled(theOn);
 }
+
+XGUI_Workshop* PartSet_WidgetSketchLabel::workshop() const
+{
+  XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
+  return aConnector->workshop();
+}