Salome HOME
Merge branch 'master' of newgeom:newgeom
[modules/shaper.git] / src / PartSet / PartSet_Module.cpp
index 4cea1a7bf62bd349bb79d047ca87f9a28540947b..b2f838d1a9a7d72038c3b586a9613c81c4cb8865 100644 (file)
@@ -3,30 +3,31 @@
 #include <PartSet_OperationFeatureCreate.h>
 #include <PartSet_OperationFeatureEditMulti.h>
 #include <PartSet_OperationFeatureEdit.h>
-#include <ModuleBase_Operation.h>
-#include <ModuleBase_OperationDescription.h>
-#include <ModuleBase_WidgetFactory.h>
 #include <PartSet_Listener.h>
 #include <PartSet_TestOCC.h>
 #include <PartSet_WidgetSketchLabel.h>
 #include <PartSet_Validators.h>
+#include <PartSet_Tools.h>
 
 #include <ModuleBase_Operation.h>
+#include <ModuleBase_OperationDescription.h>
+#include <ModuleBase_WidgetFactory.h>
+#include <ModuleBase_Operation.h>
+
 #include <ModelAPI_Object.h>
 #include <ModelAPI_Events.h>
 #include <ModelAPI_Validator.h>
-
 #include <ModelAPI_Data.h>
+
 #include <GeomDataAPI_Point2D.h>
-#include <PartSet_Tools.h>
+#include <GeomDataAPI_Point.h>
+#include <GeomDataAPI_Dir.h>
 
 #include <XGUI_MainWindow.h>
 #include <XGUI_Displayer.h>
 #include <XGUI_Viewer.h>
 #include <XGUI_Workshop.h>
 #include <XGUI_OperationMgr.h>
-#include <XGUI_SelectionMgr.h>
-#include <XGUI_Selection.h>
 #include <XGUI_ViewPort.h>
 #include <XGUI_ActionsMgr.h>
 #include <XGUI_ViewerProxy.h>
 #include <XGUI_Tools.h>
 
 #include <SketchPlugin_Line.h>
+#include <SketchPlugin_Sketch.h>
 
 #include <Config_PointerMessage.h>
 #include <Config_ModuleReader.h>
 #include <Config_WidgetReader.h>
 #include <Events_Loop.h>
-#include <Events_Message.h>
-#include <Events_Error.h>
+//#include <Events_Message.h>
+//#include <Events_Error.h>
 
 #include <GeomAPI_Shape.h>
 #include <GeomAPI_AISObject.h>
 #include <AIS_Shape.hxx>
+#include <AIS_DimensionSelectionMode.hxx>
+
+#include <StdSelect_TypeOfFace.hxx>
 
 #include <QObject>
 #include <QMouseEvent>
 #include <QDebug>
 #endif
 
-
 /*!Create and return new instance of XGUI_Module*/
-extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(XGUI_Workshop* theWshop)
+extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(ModuleBase_IWorkshop* theWshop)
 {
   return new PartSet_Module(theWshop);
 }
 
-PartSet_Module::PartSet_Module(XGUI_Workshop* theWshop)
+PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
+  : ModuleBase_IModule(theWshop)
 {
-  myWorkshop = theWshop;
+  //myWorkshop = theWshop;
   myListener = new PartSet_Listener(this);
 
-  XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
+  connect(myWorkshop, SIGNAL(operationStarted(ModuleBase_Operation*)), 
+    this, SLOT(onOperationStarted(ModuleBase_Operation*)));
 
-  connect(anOperationMgr, SIGNAL(operationStarted()),
-          this, SLOT(onOperationStarted()));
+  connect(myWorkshop, SIGNAL(operationStopped(ModuleBase_Operation*)), this,
+          SLOT(onOperationStopped(ModuleBase_Operation*)));
 
-  connect(anOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
-          this, SLOT(onOperationStopped(ModuleBase_Operation*)));
+  XGUI_Workshop* aXWshop = xWorkshop();
+  XGUI_ContextMenuMgr* aContextMenuMgr = aXWshop->contextMenuMgr();
+  connect(aContextMenuMgr, SIGNAL(actionTriggered(const QString&, bool)), this,
+          SLOT(onContextMenuCommand(const QString&, bool)));
 
-  XGUI_ContextMenuMgr* aContextMenuMgr = myWorkshop->contextMenuMgr();
-  connect(aContextMenuMgr, SIGNAL(actionTriggered(const QString&, bool)), 
-          this, SLOT(onContextMenuCommand(const QString&, bool)));
-
-  connect(myWorkshop->viewer(), SIGNAL(mousePress(QMouseEvent*)),
-          this, SLOT(onMousePressed(QMouseEvent*)));
-  connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)),
-          this, SLOT(onMouseReleased(QMouseEvent*)));
-  connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)),
-          this, SLOT(onMouseMoved(QMouseEvent*)));
-  connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)),
-          this, SLOT(onKeyRelease(QKeyEvent*)));
-  connect(myWorkshop->viewer(), SIGNAL(mouseDoubleClick(QMouseEvent*)),
-          this, SLOT(onMouseDoubleClick(QMouseEvent*)));
+  connect(myWorkshop->viewer(), SIGNAL(mousePress(QMouseEvent*)), this,
+          SLOT(onMousePressed(QMouseEvent*)));
+  connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)), this,
+          SLOT(onMouseReleased(QMouseEvent*)));
+  connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)), this,
+          SLOT(onMouseMoved(QMouseEvent*)));
+  connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)), this,
+          SLOT(onKeyRelease(QKeyEvent*)));
+  connect(myWorkshop->viewer(), SIGNAL(mouseDoubleClick(QMouseEvent*)), this,
+          SLOT(onMouseDoubleClick(QMouseEvent*)));
 }
 
 PartSet_Module::~PartSet_Module()
 {
 }
 
-XGUI_Workshop* PartSet_Module::workshop() const
-{
-  return myWorkshop;
-}
-
 void PartSet_Module::createFeatures()
 {
-  //!! Test registering of validators
-  PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
+  //Registering of validators
+  SessionPtr aMgr = ModelAPI_Session::get();
   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
   aFactory->registerValidator("PartSet_DistanceValidator", new PartSet_DistanceValidator);
   aFactory->registerValidator("PartSet_LengthValidator", new PartSet_LengthValidator);
@@ -125,11 +124,6 @@ void PartSet_Module::featureCreated(QAction* theFeature)
   connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
 }
 
-QStringList PartSet_Module::nestedFeatures(QString)
-{
-  return QStringList();
-}
-
 std::string PartSet_Module::featureFile(const std::string& theFeatureId)
 {
   return myFeaturesInFiles[theFeatureId];
@@ -140,40 +134,38 @@ std::string PartSet_Module::featureFile(const std::string& theFeatureId)
  */
 void PartSet_Module::onFeatureTriggered()
 {
-  //PartSet_TestOCC::local_selection_change_shape(myWorkshop->viewer()->AISContext(),
-  //                                   myWorkshop->viewer()->activeView());
-
-  //PartSet_TestOCC::local_selection_erase(myWorkshop->viewer()->AISContext(),
-  //                                       myWorkshop->viewer()->activeView());
   QAction* aCmd = dynamic_cast<QAction*>(sender());
   //Do nothing on uncheck
-  if(aCmd->isCheckable() && !aCmd->isChecked())
+  if (aCmd->isCheckable() && !aCmd->isChecked())
     return;
   launchOperation(aCmd->data().toString());
 }
-  
-void PartSet_Module::launchOperation(const QString& theCmdId)
-{
-  ModuleBase_Operation* anOperation = createOperation(theCmdId.toStdString());
-  PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
-  if (aPreviewOp) {
-    XGUI_Selection* aSelection = myWorkshop->selector()->selection();
-    // Initialise operation with preliminary selection
-    std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
-    std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
-    aPreviewOp->initSelection(aSelected, aHighlighted);
-  } 
-  sendOperation(anOperation);
-}
 
-void PartSet_Module::onOperationStarted()
+
+void PartSet_Module::onOperationStarted(ModuleBase_Operation* theOperation)
 {
-  PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
-                                       myWorkshop->operationMgr()->currentOperation());
+  XGUI_Workshop* aXWshp = xWorkshop();
+  XGUI_Displayer* aDisplayer = aXWshp->displayer();
+  PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
   if (aPreviewOp) {
-    XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
-    connect(aPropPanel, SIGNAL(storedPoint2D(ObjectPtr, const std::string&)),
-      this, SLOT(onStorePoint2D(ObjectPtr, const std::string&)), Qt::UniqueConnection);
+    XGUI_PropertyPanel* aPropPanel = aXWshp->propertyPanel();
+    connect(aPropPanel, SIGNAL(storedPoint2D(ObjectPtr, const std::string&)), this,
+            SLOT(onStorePoint2D(ObjectPtr, const std::string&)), Qt::UniqueConnection);
+
+    //aDisplayer->deactivateObjectsOutOfContext();
+    PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
+    if (aSketchOp) {
+      if (aSketchOp->isEditOperation()) {
+        setSketchingMode(getSketchPlane(aSketchOp->feature()));
+      } else {
+        aDisplayer->openLocalContext();
+        aDisplayer->activateObjectsOutOfContext(QIntList());
+        myPlaneFilter = new StdSelect_FaceFilter(StdSelect_Plane);
+        aDisplayer->addSelectionFilter(myPlaneFilter);
+        QIntList aModes = sketchSelectionModes(aPreviewOp->feature());
+        aDisplayer->setSelectionModes(aModes);
+      } 
+    }
   }
 }
 
@@ -181,17 +173,45 @@ void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
 {
   if (!theOperation)
     return;
+  XGUI_Workshop* aXWshp = xWorkshop();
+  XGUI_Displayer* aDisplayer = aXWshp->displayer();
   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
   if (aPreviewOp) {
-    XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
-    //disconnect(aPropPanel, SIGNAL(storedPoint2D(ObjectPtr, const std::string&)),
-    //           this, SLOT(onStorePoint2D(ObjectPtr, const std::string&)));
-  }
+    XGUI_PropertyPanel* aPropPanel = aXWshp->propertyPanel();
+
+    PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
+    if (aSketchOp) {
+      aDisplayer->closeLocalContexts();
+    } else {
+      PartSet_OperationFeatureCreate* aCreationOp = 
+        dynamic_cast<PartSet_OperationFeatureCreate*>(aPreviewOp);
+      if (aCreationOp) {
+        // Activate just created object for selection
+        FeaturePtr aFeature = aCreationOp->feature();
+        QIntList aModes = sketchSelectionModes(aFeature);
+        const std::list<ResultPtr>& aResults = aFeature->results();
+        std::list<ResultPtr>::const_iterator anIt, aLast = aResults.end();
+        for (anIt = aResults.begin(); anIt != aLast; anIt++) {
+          aDisplayer->activate(*anIt, aModes);
+        }
+        aDisplayer->activate(aFeature, aModes);
+      }
+    }
+  }// else {
+    // Activate results of current feature for selection
+    //FeaturePtr aFeature = theOperation->feature();
+    //XGUI_Displayer* aDisplayer = aXWshp->displayer();
+    //std::list<ResultPtr> aResults = aFeature->results();
+    //std::list<ResultPtr>::const_iterator aIt;
+    //for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
+    //  aDisplayer->activate(*aIt);
+    //}    
+  //}
 }
 
 void PartSet_Module::onContextMenuCommand(const QString& theId, bool isChecked)
 {
-  QList<ObjectPtr> aFeatures = myWorkshop->selector()->selection()->selectedObjects();
+  QList<ObjectPtr> aFeatures = workshop()->selection()->selectedObjects();
   if (theId == "EDIT_CMD" && (aFeatures.size() > 0)) {
     FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aFeatures.first());
     if (aFeature)
@@ -201,43 +221,47 @@ void PartSet_Module::onContextMenuCommand(const QString& theId, bool isChecked)
 
 void PartSet_Module::onMousePressed(QMouseEvent* theEvent)
 {
-  PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
-                                       myWorkshop->operationMgr()->currentOperation());
-  if (aPreviewOp) {
-    XGUI_Selection* aSelection = myWorkshop->selector()->selection();
+  XGUI_Workshop* aXWshp = xWorkshop();
+  PartSet_OperationSketchBase* aPreviewOp = 
+    dynamic_cast<PartSet_OperationSketchBase*>(workshop()->currentOperation());
+  Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
+  if (aPreviewOp && (!aView.IsNull())) {
+    ModuleBase_ISelection* aSelection = workshop()->selection();
     // Initialise operation with preliminary selection
     std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
     std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
 
-    aPreviewOp->mousePressed(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
+    aPreviewOp->mousePressed(theEvent, aView, aSelected, aHighlighted);
   }
 }
 
 void PartSet_Module::onMouseReleased(QMouseEvent* theEvent)
 {
-  PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
-                                       myWorkshop->operationMgr()->currentOperation());
-  if (aPreviewOp) {
-    XGUI_Selection* aSelection = myWorkshop->selector()->selection();
+  PartSet_OperationSketchBase* aPreviewOp = 
+    dynamic_cast<PartSet_OperationSketchBase*>(myWorkshop->currentOperation());
+  Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
+  if (aPreviewOp && (!aView.IsNull())) {
+    ModuleBase_ISelection* aSelection = workshop()->selection();
     // Initialise operation with preliminary selection
     std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
     std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
 
-    aPreviewOp->mouseReleased(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
+    aPreviewOp->mouseReleased(theEvent, aView, aSelected, aHighlighted);
   }
 }
 
 void PartSet_Module::onMouseMoved(QMouseEvent* theEvent)
 {
-  PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
-                                       myWorkshop->operationMgr()->currentOperation());
-  if (aPreviewOp)
-    aPreviewOp->mouseMoved(theEvent, myWorkshop->viewer()->activeView());
+  PartSet_OperationSketchBase* aPreviewOp = 
+    dynamic_cast<PartSet_OperationSketchBase*>(myWorkshop->currentOperation());
+  Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
+  if (aPreviewOp && (!aView.IsNull()))
+    aPreviewOp->mouseMoved(theEvent, aView);
 }
 
 void PartSet_Module::onKeyRelease(QKeyEvent* theEvent)
 {
-  ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
+  ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
   if (aPreviewOp) {
     aPreviewOp->keyReleased(theEvent->key());
@@ -246,26 +270,26 @@ void PartSet_Module::onKeyRelease(QKeyEvent* theEvent)
 
 void PartSet_Module::onMouseDoubleClick(QMouseEvent* theEvent)
 {
-  PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
-                                       myWorkshop->operationMgr()->currentOperation());
-  if (aPreviewOp)
-  {
-    XGUI_Selection* aSelection = myWorkshop->selector()->selection();
+  PartSet_OperationSketchBase* aPreviewOp = 
+    dynamic_cast<PartSet_OperationSketchBase*>(myWorkshop->currentOperation());
+  Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
+  if (aPreviewOp && (!aView.IsNull())) {
+    ModuleBase_ISelection* aSelection = workshop()->selection();
     // Initialise operation with preliminary selection
     std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
     std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
-    aPreviewOp->mouseDoubleClick(theEvent, myWorkshop->viewer()->activeView(), aSelected,
-                                 aHighlighted);
+    aPreviewOp->mouseDoubleClick(theEvent, aView, aSelected, aHighlighted);
   }
 }
 
 void PartSet_Module::onPlaneSelected(double theX, double theY, double theZ)
 {
-  //erasePlanes();
   myWorkshop->viewer()->setViewProjection(theX, theY, theZ);
-  myWorkshop->actionsMgr()->update();
-
-  //PartSet_TestOCC::testSelection(myWorkshop);
+  xWorkshop()->actionsMgr()->update();
+  // Set working plane
+  ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
+  FeaturePtr aSketch = anOperation->feature();
+  setSketchingMode(getSketchPlane(aSketch));
 }
 
 void PartSet_Module::onFitAllView()
@@ -273,64 +297,57 @@ void PartSet_Module::onFitAllView()
   myWorkshop->viewer()->fitAll();
 }
 
-void PartSet_Module::onLaunchOperation(std::string theName, ObjectPtr theObject)
+void PartSet_Module::onRestartOperation(std::string theName, ObjectPtr theObject)
 {
-  FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
-  if (!aFeature) {
-    ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theObject);
-    if (aResult) {
-      PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
-      DocumentPtr aDoc = aMgr->rootDocument();
-      aFeature = aDoc->feature(aResult);
-    } else {
-      qDebug("Warning! Restart operation without feature!");
-      return;
+  FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
+
+  std::string aKind = aFeature ? aFeature->getKind() : "";
+  ModuleBase_Operation* anOperation = createOperation(theName, aKind);
+
+  PartSet_OperationSketchBase* aSketchOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
+  if (aSketchOp) {
+    PartSet_OperationFeatureCreate* aCreateOp = dynamic_cast<PartSet_OperationFeatureCreate*>(anOperation);
+    if (aCreateOp)
+      aCreateOp->initFeature(aFeature);
+    else {
+      anOperation->setFeature(aFeature);
     }
-  }
-  ModuleBase_Operation* anOperation = createOperation(theName.c_str(),
-                                                      aFeature ? aFeature->getKind() : "");
-  PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
-  if (aPreviewOp)
-  {
-    XGUI_Selection* aSelection = myWorkshop->selector()->selection();
+    ModuleBase_ISelection* aSelection = workshop()->selection();
     // Initialise operation with preliminary selection
     std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
     std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
-    aPreviewOp->initFeature(aFeature);
-    aPreviewOp->initSelection(aSelected, aHighlighted);
-  } else {
-    anOperation->setEditingFeature(aFeature);
-  }
+    aSketchOp->initSelection(aSelected, aHighlighted);
+  } //else if (aFeature) {
+    //anOperation->setFeature(aFeature);
+    ////Deactivate result of current feature in order to avoid its selection
+    //XGUI_Displayer* aDisplayer = xWorkshop()->displayer();
+    //std::list<ResultPtr> aResults = aFeature->results();
+    //std::list<ResultPtr>::const_iterator aIt;
+    //for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
+    //  aDisplayer->deactivate(*aIt);
+    //}
+  //}
   sendOperation(anOperation);
-  myWorkshop->actionsMgr()->updateCheckState();
+  xWorkshop()->actionsMgr()->updateCheckState();
 }
 
 void PartSet_Module::onMultiSelectionEnabled(bool theEnabled)
 {
-  XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
+  ModuleBase_IViewer* aViewer = myWorkshop->viewer();
   aViewer->enableMultiselection(theEnabled);
 }
 
 void PartSet_Module::onStopSelection(const QList<ObjectPtr>& theFeatures, const bool isStop)
 {
-  XGUI_Displayer* aDisplayer = myWorkshop->displayer();
-  if (!isStop) {
-    foreach(ObjectPtr aObject, theFeatures) {
-      activateFeature(aObject, false);
-    }
-  }
-  //ObjectPtr aResults;
-  //foreach(ObjectPtr aFeature, theFeatures) {
-/* TODO    if (aFeature->results().size() > 0) {
-      const std::list<ResultPtr>& aResList = aFeature->results();
-      std::list<ResultPtr>::const_iterator aIt;
-      for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt)
-        aResults.append(*aIt);
-    }
-  }*/
+  XGUI_Displayer* aDisplayer = xWorkshop()->displayer();
+  //if (!isStop) {
+  //  foreach(ObjectPtr aObject, theFeatures) {
+  //    activateFeature(aObject);
+  //  }
+  //}
   aDisplayer->stopSelection(theFeatures, isStop, false);
 
-  XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
+  ModuleBase_IViewer* aViewer = myWorkshop->viewer();
   aViewer->enableSelection(!isStop);
 
   aDisplayer->updateViewer();
@@ -338,63 +355,56 @@ void PartSet_Module::onStopSelection(const QList<ObjectPtr>& theFeatures, const
 
 void PartSet_Module::onSetSelection(const QList<ObjectPtr>& theFeatures)
 {
-  XGUI_Displayer* aDisplayer = myWorkshop->displayer();
+  XGUI_Displayer* aDisplayer = xWorkshop()->displayer();
   aDisplayer->setSelected(theFeatures, false);
   aDisplayer->updateViewer();
 }
 
-void PartSet_Module::onCloseLocalContext()
+void PartSet_Module::setSketchingMode(const gp_Pln& thePln)
 {
-  XGUI_Displayer* aDisplayer = myWorkshop->displayer();
-  aDisplayer->closeLocalContexts();
-}
-
-void PartSet_Module::onFeatureConstructed(FeaturePtr theFeature, int theMode)
-{
-//  bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Hide;
-//  if (isDisplay) {
-    ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
-    PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
-    if (aPrevOp) {
-      std::list<FeaturePtr> aList = aPrevOp->subFeatures();
-      XGUI_Displayer* aDisplayer = myWorkshop->displayer();
-      std::list<int> aModes = aPrevOp->getSelectionModes(aPrevOp->feature());
-      std::list<FeaturePtr>::iterator aSFIt; 
-      for (aSFIt = aList.begin(); aSFIt != aList.end(); ++aSFIt) {
-        std::list<ResultPtr> aResults = (*aSFIt)->results();
-        std::list<ResultPtr>::iterator aIt;
-        for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
-          aDisplayer->activateInLocalContext((*aIt), aModes, false);
-        }
+  XGUI_Displayer* aDisplayer = xWorkshop()->displayer();
+  if (!myPlaneFilter.IsNull()) {
+    aDisplayer->removeSelectionFilter(myPlaneFilter);
+    myPlaneFilter.Nullify();
+  }
+  QIntList aModes;
+  // Clear standard selection modes
+  aDisplayer->setSelectionModes(aModes);
+  aDisplayer->openLocalContext();
+  // Get default selection modes
+  aModes = sketchSelectionModes(ObjectPtr());
+  aDisplayer->activateObjectsOutOfContext(aModes);
+
+  // Set filter
+  mySketchFilter = new ModuleBase_ShapeInPlaneFilter(thePln);
+  aDisplayer->addSelectionFilter(mySketchFilter);
+}
+
+void PartSet_Module::onFeatureConstructed(ObjectPtr theFeature, int theMode)
+{
+  bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Hide;
+  ModuleBase_Operation* aCurOperation = myWorkshop->currentOperation();
+  PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
+  if (aPrevOp) {
+    std::list<FeaturePtr> aList = aPrevOp->subFeatures();
+    XGUI_Displayer* aDisplayer = xWorkshop()->displayer();
+    QIntList aModes = sketchSelectionModes(aPrevOp->feature());
+    std::list<FeaturePtr>::iterator aSFIt;
+    for (aSFIt = aList.begin(); aSFIt != aList.end(); ++aSFIt) {
+      std::list<ResultPtr> aResults = (*aSFIt)->results();
+      std::list<ResultPtr>::iterator aIt;
+      for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
+        if (!isDisplay)
+          aDisplayer->erase((*aIt), false);
       }
+      if (!isDisplay)
+        aDisplayer->erase((*aSFIt), false);
     }
-    ModelAPI_EventCreator::get()->sendUpdated(theFeature, 
-        Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
-//  }
-//  else
-//    ->erase(theFeature->firstResult(), true);
-/*  bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Hide;
-  // TODO visualizePreview(theFeature, isDisplay, false);
-  if (!isDisplay) {
-    ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
-    FeaturePtr aSketch;
-    PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
-    if (aPrevOp) {
-      std::list<FeaturePtr> aList = aPrevOp->subFeatures();
-      XGUI_Displayer* aDisplayer = myWorkshop->displayer();
-      std::list<int> aModes = aPrevOp->getSelectionModes(aPrevOp->feature());
-
-      std::list<FeaturePtr>::const_iterator anIt = aList.begin(),
-                                            aLast = aList.end();
-      //TODO for (; anIt != aLast; anIt++)
-      //  visualizePreview((*anIt), false, false);
-      //aDisplayer->updateViewer();
-    }
+    //aDisplayer->deactivateObjectsOutOfContext();
   }
-
-  if (theMode == PartSet_OperationSketchBase::FM_Activation ||
-      theMode == PartSet_OperationSketchBase::FM_Deactivation)
-    activateFeature(theFeature, true);*/
+  if (isDisplay)
+    ModelAPI_EventCreator::get()->sendUpdated(
+        theFeature, Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
 }
 
 ModuleBase_Operation* PartSet_Module::createOperation(const std::string& theCmdId,
@@ -404,19 +414,20 @@ ModuleBase_Operation* PartSet_Module::createOperation(const std::string& theCmdI
   ModuleBase_Operation* anOperation = 0;
   if (theCmdId == PartSet_OperationSketch::Type()) {
     anOperation = new PartSet_OperationSketch(theCmdId.c_str(), this);
-  }
-  else {
-    ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
-    FeaturePtr aSketch;
+  } else {
+    ModuleBase_Operation* aCurOperation = myWorkshop->currentOperation();
+    CompositeFeaturePtr aSketch;
     PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
-    if (aPrevOp)
+    if (aPrevOp) {
       aSketch = aPrevOp->sketch();
-    if (PartSet_OperationFeatureCreate::canProcessKind(theCmdId))
+    }
+    if (PartSet_OperationFeatureCreate::canProcessKind(theCmdId)) {
       anOperation = new PartSet_OperationFeatureCreate(theCmdId.c_str(), this, aSketch);
-    else if (theCmdId == PartSet_OperationFeatureEditMulti::Type())
-               anOperation = new PartSet_OperationFeatureEditMulti(theCmdId.c_str(), this, aSketch);
-    else if (theCmdId == PartSet_OperationFeatureEdit::Type())
+    } else if (theCmdId == PartSet_OperationFeatureEditMulti::Type()) {
+      anOperation = new PartSet_OperationFeatureEditMulti(theCmdId.c_str(), this, aSketch);
+    } else if (theCmdId == PartSet_OperationFeatureEdit::Type()) {
       anOperation = new PartSet_OperationFeatureEdit(theCmdId.c_str(), this, aSketch);
+    }
   }
 
   if (!anOperation) {
@@ -432,100 +443,39 @@ ModuleBase_Operation* PartSet_Module::createOperation(const std::string& theCmdI
   std::string aXmlCfg = aWdgReader.featureWidgetCfg(aFeatureKind);
   std::string aDescription = aWdgReader.featureDescription(aFeatureKind);
 
-  //QString aXmlRepr = QString::fromStdString(aXmlCfg);
-  //ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aXmlRepr.toStdString(),
-  //                                                             myWorkshop->moduleConnector());
-  //QWidget* aContent = myWorkshop->propertyPanel()->contentWidget();
-  //qDeleteAll(aContent->children());
-  //aFactory.createWidget(aContent);
-
   anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
   anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
 
-  //anOperation->setModelWidgets(aXmlRepr.toStdString(), aFactory.getModelWidgets());
-
   // connect the operation
   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
   if (aPreviewOp) {
-    connect(aPreviewOp, SIGNAL(featureConstructed(FeaturePtr, int)),
-            this, SLOT(onFeatureConstructed(FeaturePtr, int)));
-    connect(aPreviewOp, SIGNAL(launchOperation(std::string, ObjectPtr)),
-            this, SLOT(onLaunchOperation(std::string, ObjectPtr)));
-    connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
-            this, SLOT(onMultiSelectionEnabled(bool)));
-
-    connect(aPreviewOp, SIGNAL(stopSelection(const QList<ObjectPtr>&, const bool)),
-            this, SLOT(onStopSelection(const QList<ObjectPtr>&, const bool)));
-    connect(aPreviewOp, SIGNAL(setSelection(const QList<ObjectPtr>&)),
-            this, SLOT(onSetSelection(const QList<ObjectPtr>&)));
-
-     connect(aPreviewOp, SIGNAL(closeLocalContext()),
-             this, SLOT(onCloseLocalContext()));
+    connect(aPreviewOp, SIGNAL(featureConstructed(ObjectPtr, int)), this,
+            SLOT(onFeatureConstructed(ObjectPtr, int)));
+    connect(aPreviewOp, SIGNAL(restartRequired(std::string, ObjectPtr)), this,
+            SLOT(onRestartOperation(std::string, ObjectPtr)));
+    connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)), this,
+            SLOT(onMultiSelectionEnabled(bool)));
+
+    connect(aPreviewOp, SIGNAL(stopSelection(const QList<ObjectPtr>&, const bool)), this,
+            SLOT(onStopSelection(const QList<ObjectPtr>&, const bool)));
+    connect(aPreviewOp, SIGNAL(setSelection(const QList<ObjectPtr>&)), this,
+            SLOT(onSetSelection(const QList<ObjectPtr>&)));
 
     PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
     if (aSketchOp) {
-      connect(aSketchOp, SIGNAL(planeSelected(double, double, double)),
-              this, SLOT(onPlaneSelected(double, double, double)));
-      connect(aSketchOp, SIGNAL(fitAllView()),
-              this, SLOT(onFitAllView()));
+      connect(aSketchOp, SIGNAL(planeSelected(double, double, double)), this,
+              SLOT(onPlaneSelected(double, double, double)));
+      connect(aSketchOp, SIGNAL(fitAllView()), this, SLOT(onFitAllView()));
     }
   }
 
   return anOperation;
 }
 
-void PartSet_Module::sendOperation(ModuleBase_Operation* theOperation)
-{
-  static Events_ID aModuleEvent = Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED);
-  Config_PointerMessage aMessage(aModuleEvent, this);
-  aMessage.setPointer(theOperation);
-  Events_Loop::loop()->send(aMessage);
-}
-
-
-/*void PartSet_Module::visualizePreview(FeaturePtr theFeature, bool isDisplay,
-                                      const bool isUpdateViewer)
-{
-  ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
-  if (!anOperation)
-    return;
-
-  PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
-  if (!aPreviewOp)
-    return;
-
-  ResultPtr aResult = theFeature->firstResult();
-  XGUI_Displayer* aDisplayer = myWorkshop->displayer();
-  if (isDisplay) {
-    boost::shared_ptr<SketchPlugin_Feature> aSPFeature = 
-      boost::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature);
-    if (aSPFeature) {
-      PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
-      if (aSketchOp && !aSketchOp->hasSketchPlane())
-        showPlanes();
-    }
-  }
-  else
-    aDisplayer->erase(aResult, false);
-
-  if (isUpdateViewer)
-    aDisplayer->updateViewer();
-}*/
-
-void PartSet_Module::activateFeature(ObjectPtr theFeature, const bool isUpdateViewer)
-{
-  ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
-  PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
-  if (aPreviewOp) {
-    XGUI_Displayer* aDisplayer = myWorkshop->displayer();
-    std::list<int> aModes = aPreviewOp->getSelectionModes(theFeature);
-    aDisplayer->activateInLocalContext(theFeature, aModes, isUpdateViewer);
-  }
-}
 
 void PartSet_Module::updateCurrentPreview(const std::string& theCmdId)
 {
-  ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
+  ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
   if (!anOperation)
     return;
 
@@ -537,19 +487,29 @@ void PartSet_Module::updateCurrentPreview(const std::string& theCmdId)
   if (!aFeature || aFeature->getKind() != theCmdId)
     return;
 
+  XGUI_Displayer* aDisplayer = xWorkshop()->displayer();
+  // Hide result of sketch
+  std::list<ResultPtr> aResults = aFeature->results();
+  std::list<ResultPtr>::const_iterator aIt;
+  for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt)
+    aDisplayer->erase(*aIt, false);
+
   std::list<FeaturePtr> aList = aPreviewOp->subFeatures();
-  XGUI_Displayer* aDisplayer = myWorkshop->displayer();
-  std::list<int> aModes = aPreviewOp->getSelectionModes(aPreviewOp->feature());
 
-  std::list<FeaturePtr>::const_iterator anIt = aList.begin(), 
-                                        aLast = aList.end();
+  std::list<FeaturePtr>::const_iterator anIt = aList.begin(), aLast = aList.end();
   for (; anIt != aLast; anIt++) {
-    boost::shared_ptr<SketchPlugin_Feature> aSPFeature = 
-      boost::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
+    boost::shared_ptr<SketchPlugin_Feature> aSPFeature = boost::dynamic_pointer_cast<
+        SketchPlugin_Feature>(*anIt);
     if (!aSPFeature)
       continue;
-    //visualizePreview((*anIt), true, false);
-    aDisplayer->activateInLocalContext((*anIt), aModes, false);
+    std::list<ResultPtr> aResults = aSPFeature->results();
+    std::list<ResultPtr>::const_iterator aRIt;
+    for (aRIt = aResults.cbegin(); aRIt != aResults.cend(); ++aRIt) {
+      aDisplayer->display((*aRIt), false);
+      aDisplayer->activate((*aRIt), sketchSelectionModes((*aRIt)));
+    }
+    aDisplayer->display(aSPFeature, false);
+    aDisplayer->activate(aSPFeature, sketchSelectionModes(aSPFeature));
   }
   aDisplayer->updateViewer();
 }
@@ -560,16 +520,16 @@ void PartSet_Module::editFeature(FeaturePtr theFeature)
     return;
 
 //  if (theFeature->getKind() == SKETCH_KIND) {
-    //FeaturePtr aFeature = theFeature;
-    //if (XGUI_Tools::isModelObject(aFeature)) {
-    //  ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
-    //  aFeature = aObject->featureRef();
-    //}
-
-    //if (aFeature) {
-      onLaunchOperation(theFeature->getKind(), theFeature);
-      updateCurrentPreview(theFeature->getKind());
-    //}
+  //FeaturePtr aFeature = theFeature;
+  //if (XGUI_Tools::isModelObject(aFeature)) {
+  //  ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
+  //  aFeature = aObject->featureRef();
+  //}
+
+  //if (aFeature) {
+  onRestartOperation(theFeature->getKind(), theFeature);
+  updateCurrentPreview(theFeature->getKind());
+  //}
 //  }
 }
 
@@ -577,46 +537,71 @@ void PartSet_Module::onStorePoint2D(ObjectPtr theFeature, const std::string& the
 {
   FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theFeature);
 
-  PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
-                                       myWorkshop->operationMgr()->currentOperation());
+  PartSet_OperationSketchBase* aPreviewOp = 
+    dynamic_cast<PartSet_OperationSketchBase*>(myWorkshop->currentOperation());
   if (!aPreviewOp)
     return;
 
-  boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
-        boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFeature->data()->attribute(theAttribute));
+  boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+      aFeature->data()->attribute(theAttribute));
 
-  PartSet_Tools::setConstraints(aPreviewOp->sketch(), aFeature, theAttribute,
-                                aPoint->x(), aPoint->y());
+  PartSet_Tools::setConstraints(aPreviewOp->sketch(), aFeature, theAttribute, aPoint->x(),
+                                aPoint->y());
 }
 
-/*bool PartSet_Module::isFeatureEnabled(const QString& theCmdId) const
-{
-  XGUI_OperationMgr* aOpMgr = myWorkshop->operationMgr();
-  XGUI_ActionsMgr* aActMgr = myWorkshop->actionsMgr();
-
-  ModuleBase_Operation* aOperation = aOpMgr->currentOperation();
-  if (!aOperation)
-    return !aActMgr->isNested(theCmdId);
-
-  PartSet_OperationFeatureEdit* aSketchEdtOp = dynamic_cast<PartSet_OperationFeatureEdit*>(aOperation);
-  if (aSketchEdtOp) {
-    QStringList aConstraintList;
-    aConstraintList<<"SketchConstraintDistance"<<"SketchConstraintLength"
-      <<"SketchConstraintRadius"<<"SketchConstraintParallel"<<"SketchConstraintPerpendicular";
-    return aConstraintList.contains(theCmdId);
-  }
-  QStringList aList = aActMgr->nestedCommands(aOperation->id());
-  return aList.contains(theCmdId);
-}*/
-
-QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent, 
-                         Config_WidgetAPI* theWidgetApi, QList<ModuleBase_ModelWidget*>& theModelWidgets)
+QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
+                                            Config_WidgetAPI* theWidgetApi,
+                                            QList<ModuleBase_ModelWidget*>& theModelWidgets)
 {
   if (theType == "sketch-start-label") {
     PartSet_WidgetSketchLabel* aWgt = new PartSet_WidgetSketchLabel(theParent, theWidgetApi, "");
-    aWgt->setOperationsMgr(myWorkshop->operationMgr());
+    aWgt->setOperationsMgr(xWorkshop()->operationMgr());
     theModelWidgets.append(aWgt);
     return aWgt->getControl();
   } else
     return 0;
 }
+
+
+XGUI_Workshop* PartSet_Module::xWorkshop() const
+{
+  XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
+  if (aConnector) {
+    return aConnector->workshop();
+  }
+  return 0;
+}
+
+
+QIntList PartSet_Module::sketchSelectionModes(ObjectPtr theFeature)
+{
+  QIntList aModes;
+  FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theFeature);
+  if (aFeature) {
+    if (aFeature->getKind() == SketchPlugin_Sketch::ID()) {
+      aModes.append(TopAbs_FACE);
+      return aModes;
+    } else if (PartSet_Tools::isConstraintFeature(aFeature->getKind())) {
+      aModes.append(AIS_DSM_Text);
+      aModes.append(AIS_DSM_Line);
+      return aModes;
+    }
+  } 
+  aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_VERTEX));
+  aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_EDGE));
+  return aModes;
+}
+
+
+gp_Pln PartSet_Module::getSketchPlane(FeaturePtr theSketch) const
+{
+  DataPtr aData = theSketch->data();
+  boost::shared_ptr<GeomDataAPI_Point> anOrigin = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
+      aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
+  boost::shared_ptr<GeomDataAPI_Dir> aNorm = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
+      aData->attribute(SketchPlugin_Sketch::NORM_ID()));
+  gp_Pnt aOrig(anOrigin->x(), anOrigin->y(), anOrigin->z());
+  gp_Dir aDir(aNorm->x(), aNorm->y(), aNorm->z());
+  return gp_Pln(aOrig, aDir);
+}
+