Salome HOME
fe04a7f98c2e30535bf96eaa1a05f4c3a6e8f442
[modules/shaper.git] / src / PartSet / PartSet_Module.cpp
1 #include <PartSet_Module.h>
2 #include <PartSet_OperationSketch.h>
3 #include <PartSet_OperationFeatureCreate.h>
4 #include <PartSet_OperationFeatureEditMulti.h>
5 #include <PartSet_OperationFeatureEdit.h>
6 #include <PartSet_Listener.h>
7 #include <PartSet_TestOCC.h>
8 #include <PartSet_WidgetSketchLabel.h>
9 #include <PartSet_Validators.h>
10 #include <PartSet_Tools.h>
11
12 #include <ModuleBase_Operation.h>
13 #include <ModuleBase_OperationDescription.h>
14 #include <ModuleBase_WidgetFactory.h>
15 #include <ModuleBase_Operation.h>
16
17 #include <ModelAPI_Object.h>
18 #include <ModelAPI_Events.h>
19 #include <ModelAPI_Validator.h>
20 #include <ModelAPI_Data.h>
21
22 #include <GeomDataAPI_Point2D.h>
23
24 #include <XGUI_MainWindow.h>
25 #include <XGUI_Displayer.h>
26 #include <XGUI_Viewer.h>
27 #include <XGUI_Workshop.h>
28 #include <XGUI_OperationMgr.h>
29 #include <XGUI_SelectionMgr.h>
30 #include <XGUI_Selection.h>
31 #include <XGUI_ViewPort.h>
32 #include <XGUI_ActionsMgr.h>
33 #include <XGUI_ViewerProxy.h>
34 #include <XGUI_ContextMenuMgr.h>
35 #include <XGUI_PropertyPanel.h>
36 #include <XGUI_ModuleConnector.h>
37 #include <XGUI_Tools.h>
38
39 #include <SketchPlugin_Line.h>
40
41 #include <Config_PointerMessage.h>
42 #include <Config_ModuleReader.h>
43 #include <Config_WidgetReader.h>
44 #include <Events_Loop.h>
45 #include <Events_Message.h>
46 #include <Events_Error.h>
47
48 #include <GeomAPI_Shape.h>
49 #include <GeomAPI_AISObject.h>
50 #include <AIS_Shape.hxx>
51
52 #include <StdSelect_FaceFilter.hxx>
53 #include <StdSelect_TypeOfFace.hxx>
54
55 #include <QObject>
56 #include <QMouseEvent>
57 #include <QString>
58
59 #include <GeomAlgoAPI_FaceBuilder.h>
60 #include <GeomDataAPI_Dir.h>
61
62 #ifdef _DEBUG
63 #include <QDebug>
64 #endif
65
66 /*!Create and return new instance of XGUI_Module*/
67 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(XGUI_Workshop* theWshop)
68 {
69   return new PartSet_Module(theWshop);
70 }
71
72 PartSet_Module::PartSet_Module(XGUI_Workshop* theWshop)
73 {
74   myWorkshop = theWshop;
75   myListener = new PartSet_Listener(this);
76
77   XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
78
79   connect(anOperationMgr, SIGNAL(operationStarted()), this, SLOT(onOperationStarted()));
80
81   connect(anOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)), this,
82           SLOT(onOperationStopped(ModuleBase_Operation*)));
83
84   XGUI_ContextMenuMgr* aContextMenuMgr = myWorkshop->contextMenuMgr();
85   connect(aContextMenuMgr, SIGNAL(actionTriggered(const QString&, bool)), this,
86           SLOT(onContextMenuCommand(const QString&, bool)));
87
88   connect(myWorkshop->viewer(), SIGNAL(mousePress(QMouseEvent*)), this,
89           SLOT(onMousePressed(QMouseEvent*)));
90   connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)), this,
91           SLOT(onMouseReleased(QMouseEvent*)));
92   connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)), this,
93           SLOT(onMouseMoved(QMouseEvent*)));
94   connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)), this,
95           SLOT(onKeyRelease(QKeyEvent*)));
96   connect(myWorkshop->viewer(), SIGNAL(mouseDoubleClick(QMouseEvent*)), this,
97           SLOT(onMouseDoubleClick(QMouseEvent*)));
98 }
99
100 PartSet_Module::~PartSet_Module()
101 {
102 }
103
104 XGUI_Workshop* PartSet_Module::workshop() const
105 {
106   return myWorkshop;
107 }
108
109 void PartSet_Module::createFeatures()
110 {
111   //Registering of validators
112   SessionPtr aMgr = ModelAPI_Session::get();
113   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
114   aFactory->registerValidator("PartSet_DistanceValidator", new PartSet_DistanceValidator);
115   aFactory->registerValidator("PartSet_LengthValidator", new PartSet_LengthValidator);
116   aFactory->registerValidator("PartSet_PerpendicularValidator", new PartSet_PerpendicularValidator);
117   aFactory->registerValidator("PartSet_ParallelValidator", new PartSet_ParallelValidator);
118   aFactory->registerValidator("PartSet_RadiusValidator", new PartSet_RadiusValidator);
119
120   Config_ModuleReader aXMLReader = Config_ModuleReader();
121   aXMLReader.readAll();
122   myFeaturesInFiles = aXMLReader.featuresInFiles();
123 }
124
125 void PartSet_Module::featureCreated(QAction* theFeature)
126 {
127   connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
128 }
129
130 std::string PartSet_Module::featureFile(const std::string& theFeatureId)
131 {
132   return myFeaturesInFiles[theFeatureId];
133 }
134
135 /*
136  *
137  */
138 void PartSet_Module::onFeatureTriggered()
139 {
140   //PartSet_TestOCC::local_selection_change_shape(myWorkshop->viewer()->AISContext(),
141   //                                   myWorkshop->viewer()->activeView());
142
143   //PartSet_TestOCC::local_selection_erase(myWorkshop->viewer()->AISContext(),
144   //                                       myWorkshop->viewer()->activeView());
145   QAction* aCmd = dynamic_cast<QAction*>(sender());
146   //Do nothing on uncheck
147   if (aCmd->isCheckable() && !aCmd->isChecked())
148     return;
149   launchOperation(aCmd->data().toString());
150 }
151
152 void PartSet_Module::launchOperation(const QString& theCmdId)
153 {
154   ModuleBase_Operation* anOperation = createOperation(theCmdId.toStdString());
155   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
156   if (aPreviewOp) {
157     XGUI_Selection* aSelection = myWorkshop->selector()->selection();
158     // Initialise operation with preliminary selection
159     std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
160     std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
161     aPreviewOp->initSelection(aSelected, aHighlighted);
162   }
163   sendOperation(anOperation);
164 }
165
166 void PartSet_Module::onOperationStarted()
167 {
168   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(myWorkshop
169       ->operationMgr()->currentOperation());
170   if (aPreviewOp) {
171     XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
172     connect(aPropPanel, SIGNAL(storedPoint2D(ObjectPtr, const std::string&)), this,
173             SLOT(onStorePoint2D(ObjectPtr, const std::string&)), Qt::UniqueConnection);
174     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
175     aDisplayer->openLocalContext();
176     aDisplayer->deactivateObjectsOutOfContext();
177   }
178 }
179
180 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
181 {
182   if (!theOperation)
183     return;
184   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
185   if (aPreviewOp) {
186     XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
187     //disconnect(aPropPanel, SIGNAL(storedPoint2D(ObjectPtr, const std::string&)),
188     //           this, SLOT(onStorePoint2D(ObjectPtr, const std::string&)));
189   } else {
190     // Activate results of current feature for selection
191     FeaturePtr aFeature = theOperation->feature();
192     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
193     std::list<ResultPtr> aResults = aFeature->results();
194     std::list<ResultPtr>::const_iterator aIt;
195     for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
196       aDisplayer->activate(*aIt);
197     }
198   }
199 }
200
201 void PartSet_Module::onContextMenuCommand(const QString& theId, bool isChecked)
202 {
203   QList<ObjectPtr> aFeatures = myWorkshop->selector()->selection()->selectedObjects();
204   if (theId == "EDIT_CMD" && (aFeatures.size() > 0)) {
205     FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aFeatures.first());
206     if (aFeature)
207       editFeature(aFeature);
208   }
209 }
210
211 void PartSet_Module::onMousePressed(QMouseEvent* theEvent)
212 {
213   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(myWorkshop
214       ->operationMgr()->currentOperation());
215   Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
216   if (aPreviewOp && (!aView.IsNull())) {
217     XGUI_Selection* aSelection = myWorkshop->selector()->selection();
218     // Initialise operation with preliminary selection
219     std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
220     std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
221
222     aPreviewOp->mousePressed(theEvent, aView, aSelected, aHighlighted);
223   }
224 }
225
226 void PartSet_Module::onMouseReleased(QMouseEvent* theEvent)
227 {
228   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(myWorkshop
229       ->operationMgr()->currentOperation());
230   Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
231   if (aPreviewOp && (!aView.IsNull())) {
232     XGUI_Selection* aSelection = myWorkshop->selector()->selection();
233     // Initialise operation with preliminary selection
234     std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
235     std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
236
237     aPreviewOp->mouseReleased(theEvent, aView, aSelected, aHighlighted);
238   }
239 }
240
241 void PartSet_Module::onMouseMoved(QMouseEvent* theEvent)
242 {
243   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(myWorkshop
244       ->operationMgr()->currentOperation());
245   Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
246   if (aPreviewOp && (!aView.IsNull()))
247     aPreviewOp->mouseMoved(theEvent, aView);
248 }
249
250 void PartSet_Module::onKeyRelease(QKeyEvent* theEvent)
251 {
252   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
253   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
254   if (aPreviewOp) {
255     aPreviewOp->keyReleased(theEvent->key());
256   }
257 }
258
259 void PartSet_Module::onMouseDoubleClick(QMouseEvent* theEvent)
260 {
261   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(myWorkshop
262       ->operationMgr()->currentOperation());
263   Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
264   if (aPreviewOp && (!aView.IsNull())) {
265     XGUI_Selection* aSelection = myWorkshop->selector()->selection();
266     // Initialise operation with preliminary selection
267     std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
268     std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
269     aPreviewOp->mouseDoubleClick(theEvent, aView, aSelected, aHighlighted);
270   }
271 }
272
273 void PartSet_Module::onPlaneSelected(double theX, double theY, double theZ)
274 {
275   //erasePlanes();
276   myWorkshop->viewer()->setViewProjection(theX, theY, theZ);
277   myWorkshop->actionsMgr()->update();
278
279   //PartSet_TestOCC::testSelection(myWorkshop);
280 }
281
282 void PartSet_Module::onFitAllView()
283 {
284   myWorkshop->viewer()->fitAll();
285 }
286
287 void PartSet_Module::onRestartOperation(std::string theName, ObjectPtr theObject)
288 {
289   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
290
291   std::string aKind = aFeature ? aFeature->getKind() : "";
292   ModuleBase_Operation* anOperation = createOperation(theName, aKind);
293   PartSet_OperationSketchBase* aSketchOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
294   if (aSketchOp) {
295     XGUI_Selection* aSelection = myWorkshop->selector()->selection();
296     // Initialise operation with preliminary selection
297     std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
298     std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
299     aSketchOp->initFeature(aFeature);
300     aSketchOp->initSelection(aSelected, aHighlighted);
301   } else if (aFeature) {
302     anOperation->setEditingFeature(aFeature);
303     //Deactivate result of current feature in order to avoid its selection
304     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
305     std::list<ResultPtr> aResults = aFeature->results();
306     std::list<ResultPtr>::const_iterator aIt;
307     for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
308       aDisplayer->deactivate(*aIt);
309     }
310   }
311   sendOperation(anOperation);
312   myWorkshop->actionsMgr()->updateCheckState();
313 }
314
315 void PartSet_Module::onMultiSelectionEnabled(bool theEnabled)
316 {
317   XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
318   aViewer->enableMultiselection(theEnabled);
319 }
320
321 void PartSet_Module::onStopSelection(const QList<ObjectPtr>& theFeatures, const bool isStop)
322 {
323   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
324   if (!isStop) {
325     foreach(ObjectPtr aObject, theFeatures)
326     {
327       activateFeature(aObject, false);
328     }
329   }
330   aDisplayer->stopSelection(theFeatures, isStop, false);
331
332   XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
333   aViewer->enableSelection(!isStop);
334
335   aDisplayer->updateViewer();
336 }
337
338 void PartSet_Module::onSetSelection(const QList<ObjectPtr>& theFeatures)
339 {
340   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
341   aDisplayer->setSelected(theFeatures, false);
342   aDisplayer->updateViewer();
343 }
344
345 void PartSet_Module::onCloseLocalContext()
346 {
347   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
348   aDisplayer->deactivateObjectsOutOfContext();
349   aDisplayer->closeLocalContexts();
350 }
351
352 void PartSet_Module::onFeatureConstructed(ObjectPtr theFeature, int theMode)
353 {
354   bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Hide;
355   ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
356   PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
357   if (aPrevOp) {
358     std::list<FeaturePtr> aList = aPrevOp->subFeatures();
359     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
360     std::list<int> aModes = aPrevOp->getSelectionModes(aPrevOp->feature());
361     std::list<FeaturePtr>::iterator aSFIt;
362     for (aSFIt = aList.begin(); aSFIt != aList.end(); ++aSFIt) {
363       std::list<ResultPtr> aResults = (*aSFIt)->results();
364       std::list<ResultPtr>::iterator aIt;
365       for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
366         if (isDisplay)
367           aDisplayer->activateInLocalContext((*aIt), aModes, false);
368         else
369           aDisplayer->erase((*aIt), false);
370       }
371       if (!isDisplay)
372         aDisplayer->erase((*aSFIt), false);
373     }
374     aDisplayer->deactivateObjectsOutOfContext();
375   }
376   if (isDisplay)
377     ModelAPI_EventCreator::get()->sendUpdated(
378         theFeature, Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
379   /*  bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Hide;
380    // TODO visualizePreview(theFeature, isDisplay, false);
381    if (!isDisplay) {
382    ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
383    FeaturePtr aSketch;
384    PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
385    if (aPrevOp) {
386    std::list<FeaturePtr> aList = aPrevOp->subFeatures();
387    XGUI_Displayer* aDisplayer = myWorkshop->displayer();
388    std::list<int> aModes = aPrevOp->getSelectionModes(aPrevOp->feature());
389
390    std::list<FeaturePtr>::const_iterator anIt = aList.begin(),
391    aLast = aList.end();
392    //TODO for (; anIt != aLast; anIt++)
393    //  visualizePreview((*anIt), false, false);
394    //aDisplayer->updateViewer();
395    }
396    }
397
398    if (theMode == PartSet_OperationSketchBase::FM_Activation ||
399    theMode == PartSet_OperationSketchBase::FM_Deactivation)
400    activateFeature(theFeature, true);*/
401 }
402
403 ModuleBase_Operation* PartSet_Module::createOperation(const std::string& theCmdId,
404                                                       const std::string& theFeatureKind)
405 {
406   // create the operation
407   ModuleBase_Operation* anOperation = 0;
408   if (theCmdId == PartSet_OperationSketch::Type()) {
409     anOperation = new PartSet_OperationSketch(theCmdId.c_str(), this);
410   } else {
411     ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
412     FeaturePtr aSketch;
413     PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
414     if (aPrevOp) {
415       aSketch = aPrevOp->sketch();
416     }
417     if (PartSet_OperationFeatureCreate::canProcessKind(theCmdId)) {
418       anOperation = new PartSet_OperationFeatureCreate(theCmdId.c_str(), this, aSketch);
419     } else if (theCmdId == PartSet_OperationFeatureEditMulti::Type()) {
420       anOperation = new PartSet_OperationFeatureEditMulti(theCmdId.c_str(), this, aSketch);
421     } else if (theCmdId == PartSet_OperationFeatureEdit::Type()) {
422       anOperation = new PartSet_OperationFeatureEdit(theCmdId.c_str(), this, aSketch);
423     }
424   }
425
426   if (!anOperation) {
427     anOperation = new ModuleBase_Operation(theCmdId.c_str(), this);
428   }
429
430   // set operation description and list of widgets corresponding to the feature xml definition
431   std::string aFeatureKind = theFeatureKind.empty() ? theCmdId : theFeatureKind;
432
433   std::string aPluginFileName = featureFile(aFeatureKind);
434   Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
435   aWdgReader.readAll();
436   std::string aXmlCfg = aWdgReader.featureWidgetCfg(aFeatureKind);
437   std::string aDescription = aWdgReader.featureDescription(aFeatureKind);
438
439   //QString aXmlRepr = QString::fromStdString(aXmlCfg);
440   //ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aXmlRepr.toStdString(),
441   //                                                             myWorkshop->moduleConnector());
442   //QWidget* aContent = myWorkshop->propertyPanel()->contentWidget();
443   //qDeleteAll(aContent->children());
444   //aFactory.createWidget(aContent);
445
446   anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
447   anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
448
449   //anOperation->setModelWidgets(aXmlRepr.toStdString(), aFactory.getModelWidgets());
450
451   // connect the operation
452   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
453   if (aPreviewOp) {
454     connect(aPreviewOp, SIGNAL(featureConstructed(ObjectPtr, int)), this,
455             SLOT(onFeatureConstructed(ObjectPtr, int)));
456     connect(aPreviewOp, SIGNAL(restartRequired(std::string, ObjectPtr)), this,
457             SLOT(onRestartOperation(std::string, ObjectPtr)));
458     connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)), this,
459             SLOT(onMultiSelectionEnabled(bool)));
460
461     connect(aPreviewOp, SIGNAL(stopSelection(const QList<ObjectPtr>&, const bool)), this,
462             SLOT(onStopSelection(const QList<ObjectPtr>&, const bool)));
463     connect(aPreviewOp, SIGNAL(setSelection(const QList<ObjectPtr>&)), this,
464             SLOT(onSetSelection(const QList<ObjectPtr>&)));
465
466     connect(aPreviewOp, SIGNAL(closeLocalContext()), this, SLOT(onCloseLocalContext()));
467
468     PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
469     if (aSketchOp) {
470       connect(aSketchOp, SIGNAL(planeSelected(double, double, double)), this,
471               SLOT(onPlaneSelected(double, double, double)));
472       connect(aSketchOp, SIGNAL(fitAllView()), this, SLOT(onFitAllView()));
473     }
474   }
475
476   return anOperation;
477 }
478
479 void PartSet_Module::sendOperation(ModuleBase_Operation* theOperation)
480 {
481   static Events_ID aModuleEvent = Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED);
482   boost::shared_ptr<Config_PointerMessage> aMessage =
483       boost::shared_ptr<Config_PointerMessage>(new Config_PointerMessage(aModuleEvent, this));
484   aMessage->setPointer(theOperation);
485   Events_Loop::loop()->send(aMessage);
486 }
487
488 /*void PartSet_Module::visualizePreview(FeaturePtr theFeature, bool isDisplay,
489  const bool isUpdateViewer)
490  {
491  ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
492  if (!anOperation)
493  return;
494
495  PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
496  if (!aPreviewOp)
497  return;
498
499  ResultPtr aResult = theFeature->firstResult();
500  XGUI_Displayer* aDisplayer = myWorkshop->displayer();
501  if (isDisplay) {
502  boost::shared_ptr<SketchPlugin_Feature> aSPFeature = 
503  boost::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature);
504  if (aSPFeature) {
505  PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
506  if (aSketchOp && !aSketchOp->hasSketchPlane())
507  showPlanes();
508  }
509  }
510  else
511  aDisplayer->erase(aResult, false);
512
513  if (isUpdateViewer)
514  aDisplayer->updateViewer();
515  }*/
516
517 void PartSet_Module::activateFeature(ObjectPtr theFeature, const bool isUpdateViewer)
518 {
519   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
520   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
521   if (aPreviewOp) {
522     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
523     std::list<int> aModes = aPreviewOp->getSelectionModes(theFeature);
524     aDisplayer->activateInLocalContext(theFeature, aModes, isUpdateViewer);
525
526     // If this is a Sketcher then activate objects (planar faces) outside of context
527     PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
528     if (aSketchOp) {
529       Handle(StdSelect_FaceFilter) aFilter = new StdSelect_FaceFilter(StdSelect_Plane);
530       aDisplayer->activateObjectsOutOfContext(aModes, aFilter);
531     } else {
532       aDisplayer->deactivateObjectsOutOfContext();
533     }
534   }
535 }
536
537 void PartSet_Module::updateCurrentPreview(const std::string& theCmdId)
538 {
539   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
540   if (!anOperation)
541     return;
542
543   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
544   if (!aPreviewOp)
545     return;
546
547   FeaturePtr aFeature = aPreviewOp->feature();
548   if (!aFeature || aFeature->getKind() != theCmdId)
549     return;
550
551   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
552   // Hide result of sketch
553   std::list<ResultPtr> aResults = aFeature->results();
554   std::list<ResultPtr>::const_iterator aIt;
555   for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt)
556     aDisplayer->erase(*aIt, false);
557
558   std::list<FeaturePtr> aList = aPreviewOp->subFeatures();
559   std::list<int> aModes = aPreviewOp->getSelectionModes(aPreviewOp->feature());
560
561   std::list<FeaturePtr>::const_iterator anIt = aList.begin(), aLast = aList.end();
562   for (; anIt != aLast; anIt++) {
563     boost::shared_ptr<SketchPlugin_Feature> aSPFeature = boost::dynamic_pointer_cast<
564         SketchPlugin_Feature>(*anIt);
565     if (!aSPFeature)
566       continue;
567     std::list<ResultPtr> aResults = aSPFeature->results();
568     std::list<ResultPtr>::const_iterator aRIt;
569     for (aRIt = aResults.cbegin(); aRIt != aResults.cend(); ++aRIt) {
570       aDisplayer->display((*aRIt), false);
571       aDisplayer->activateInLocalContext((*aRIt), aModes, false);
572     }
573     aDisplayer->display(aSPFeature, false);
574     aDisplayer->activateInLocalContext(aSPFeature, aModes, false);
575   }
576   aDisplayer->updateViewer();
577 }
578
579 void PartSet_Module::editFeature(FeaturePtr theFeature)
580 {
581   if (!theFeature)
582     return;
583
584 //  if (theFeature->getKind() == SKETCH_KIND) {
585   //FeaturePtr aFeature = theFeature;
586   //if (XGUI_Tools::isModelObject(aFeature)) {
587   //  ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
588   //  aFeature = aObject->featureRef();
589   //}
590
591   //if (aFeature) {
592   onRestartOperation(theFeature->getKind(), theFeature);
593   updateCurrentPreview(theFeature->getKind());
594   //}
595 //  }
596 }
597
598 void PartSet_Module::onStorePoint2D(ObjectPtr theFeature, const std::string& theAttribute)
599 {
600   FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theFeature);
601
602   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(myWorkshop
603       ->operationMgr()->currentOperation());
604   if (!aPreviewOp)
605     return;
606
607   boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
608       aFeature->data()->attribute(theAttribute));
609
610   PartSet_Tools::setConstraints(aPreviewOp->sketch(), aFeature, theAttribute, aPoint->x(),
611                                 aPoint->y());
612 }
613
614 /*bool PartSet_Module::isFeatureEnabled(const QString& theCmdId) const
615  {
616  XGUI_OperationMgr* aOpMgr = myWorkshop->operationMgr();
617  XGUI_ActionsMgr* aActMgr = myWorkshop->actionsMgr();
618
619  ModuleBase_Operation* aOperation = aOpMgr->currentOperation();
620  if (!aOperation)
621  return !aActMgr->isNested(theCmdId);
622
623  PartSet_OperationFeatureEdit* aSketchEdtOp = dynamic_cast<PartSet_OperationFeatureEdit*>(aOperation);
624  if (aSketchEdtOp) {
625  QStringList aConstraintList;
626  aConstraintList<<"SketchConstraintDistance"<<"SketchConstraintLength"
627  <<"SketchConstraintRadius"<<"SketchConstraintParallel"<<"SketchConstraintPerpendicular";
628  return aConstraintList.contains(theCmdId);
629  }
630  QStringList aList = aActMgr->nestedCommands(aOperation->id());
631  return aList.contains(theCmdId);
632  }*/
633
634 QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
635                                             Config_WidgetAPI* theWidgetApi,
636                                             QList<ModuleBase_ModelWidget*>& theModelWidgets)
637 {
638   if (theType == "sketch-start-label") {
639     PartSet_WidgetSketchLabel* aWgt = new PartSet_WidgetSketchLabel(theParent, theWidgetApi, "");
640     aWgt->setOperationsMgr(myWorkshop->operationMgr());
641     theModelWidgets.append(aWgt);
642     return aWgt->getControl();
643   } else
644     return 0;
645 }