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