Salome HOME
ad4d263244ddf2490650a00f9cd90b54a5b2ef55
[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       } else 
244         aSketchOp->mouseReleased(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
245     } else
246       aPreviewOp->mouseReleased(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
247   }
248 }
249
250 void PartSet_Module::onMouseMoved(QMouseEvent* theEvent)
251 {
252   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
253                                        myWorkshop->operationMgr()->currentOperation());
254   if (aPreviewOp)
255     aPreviewOp->mouseMoved(theEvent, myWorkshop->viewer()->activeView());
256 }
257
258 void PartSet_Module::onKeyRelease(QKeyEvent* theEvent)
259 {
260   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
261   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
262   if (aPreviewOp) {
263     aPreviewOp->keyReleased(theEvent->key());
264   }
265 }
266
267 void PartSet_Module::onMouseDoubleClick(QMouseEvent* theEvent)
268 {
269   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
270                                        myWorkshop->operationMgr()->currentOperation());
271   if (aPreviewOp)
272   {
273     XGUI_Selection* aSelection = myWorkshop->selector()->selection();
274     // Initialise operation with preliminary selection
275     std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
276     std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
277     aPreviewOp->mouseDoubleClick(theEvent, myWorkshop->viewer()->activeView(), aSelected,
278                                  aHighlighted);
279   }
280 }
281
282 void PartSet_Module::onPlaneSelected(double theX, double theY, double theZ)
283 {
284   erasePlanes();
285   myWorkshop->viewer()->setViewProjection(theX, theY, theZ);
286   myWorkshop->actionsMgr()->update();
287
288   //PartSet_TestOCC::testSelection(myWorkshop);
289 }
290
291 void PartSet_Module::onFitAllView()
292 {
293   myWorkshop->viewer()->fitAll();
294 }
295
296 void PartSet_Module::onLaunchOperation(std::string theName, ObjectPtr theFeature)
297 {
298   FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theFeature);
299   if (!aFeature) {
300     qDebug("Warning! Restart operation without feature!");
301     return;
302   }
303   ModuleBase_Operation* anOperation = createOperation(theName.c_str(),
304                                                       theFeature ? aFeature->getKind() : "");
305   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
306   if (aPreviewOp)
307   {
308     XGUI_Selection* aSelection = myWorkshop->selector()->selection();
309     // Initialise operation with preliminary selection
310     std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
311     std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
312     aPreviewOp->initFeature(aFeature);
313     aPreviewOp->initSelection(aSelected, aHighlighted);
314   } else {
315     anOperation->setEditingFeature(aFeature);
316   }
317   sendOperation(anOperation);
318   myWorkshop->actionsMgr()->updateCheckState();
319 }
320
321 void PartSet_Module::onMultiSelectionEnabled(bool theEnabled)
322 {
323   XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
324   aViewer->enableMultiselection(theEnabled);
325 }
326
327 void PartSet_Module::onStopSelection(const QList<ObjectPtr>& theFeatures, const bool isStop)
328 {
329   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
330   if (!isStop) {
331     foreach(ObjectPtr aObject, theFeatures) {
332       activateFeature(aObject, false);
333     }
334   }
335   //ObjectPtr aResults;
336   //foreach(ObjectPtr aFeature, theFeatures) {
337 /* TODO    if (aFeature->results().size() > 0) {
338       const std::list<ResultPtr>& aResList = aFeature->results();
339       std::list<ResultPtr>::const_iterator aIt;
340       for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt)
341         aResults.append(*aIt);
342     }
343   }*/
344   aDisplayer->stopSelection(theFeatures, isStop, false);
345
346   XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
347   aViewer->enableSelection(!isStop);
348
349   aDisplayer->updateViewer();
350 }
351
352 void PartSet_Module::onSetSelection(const QList<ObjectPtr>& theFeatures)
353 {
354   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
355   aDisplayer->setSelected(theFeatures, false);
356   aDisplayer->updateViewer();
357 }
358
359 void PartSet_Module::onCloseLocalContext()
360 {
361   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
362   aDisplayer->closeLocalContexts();
363 }
364
365 void PartSet_Module::onFeatureConstructed(FeaturePtr theFeature, int theMode)
366 {
367   bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Hide;
368   // TODO visualizePreview(theFeature, isDisplay, false);
369   if (!isDisplay) {
370     ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
371     FeaturePtr aSketch;
372     PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
373     if (aPrevOp) {
374       std::list<FeaturePtr> aList = aPrevOp->subFeatures();
375       XGUI_Displayer* aDisplayer = myWorkshop->displayer();
376       std::list<int> aModes = aPrevOp->getSelectionModes(aPrevOp->feature());
377
378       std::list<FeaturePtr>::const_iterator anIt = aList.begin(),
379                                             aLast = aList.end();
380       for (; anIt != aLast; anIt++)
381         visualizePreview((*anIt), false, false);
382       aDisplayer->updateViewer();
383     }
384   }
385
386   if (theMode == PartSet_OperationSketchBase::FM_Activation ||
387       theMode == PartSet_OperationSketchBase::FM_Deactivation)
388     activateFeature(theFeature, true);
389 }
390
391 ModuleBase_Operation* PartSet_Module::createOperation(const std::string& theCmdId,
392                                                       const std::string& theFeatureKind)
393 {
394   // create the operation
395   ModuleBase_Operation* anOperation = 0;
396   if (theCmdId == PartSet_OperationSketch::Type()) {
397     anOperation = new PartSet_OperationSketch(theCmdId.c_str(), this);
398   }
399   else {
400     ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
401     FeaturePtr aSketch;
402     PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
403     if (aPrevOp)
404       aSketch = aPrevOp->sketch();
405     if (PartSet_OperationFeatureCreate::canProcessKind(theCmdId))
406       anOperation = new PartSet_OperationFeatureCreate(theCmdId.c_str(), this, aSketch);
407     else if (theCmdId == PartSet_OperationFeatureEditMulti::Type())
408                 anOperation = new PartSet_OperationFeatureEditMulti(theCmdId.c_str(), this, aSketch);
409     else if (theCmdId == PartSet_OperationFeatureEdit::Type())
410       anOperation = new PartSet_OperationFeatureEdit(theCmdId.c_str(), this, aSketch);
411   }
412
413   if (!anOperation) {
414     anOperation = new ModuleBase_Operation(theCmdId.c_str(), this);
415   }
416
417   // set operation description and list of widgets corresponding to the feature xml definition
418   std::string aFeatureKind = theFeatureKind.empty() ? theCmdId : theFeatureKind;
419
420   std::string aPluginFileName = featureFile(aFeatureKind);
421   Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
422   aWdgReader.readAll();
423   std::string aXmlCfg = aWdgReader.featureWidgetCfg(aFeatureKind);
424   std::string aDescription = aWdgReader.featureDescription(aFeatureKind);
425
426   //QString aXmlRepr = QString::fromStdString(aXmlCfg);
427   //ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aXmlRepr.toStdString(),
428   //                                                             myWorkshop->moduleConnector());
429   //QWidget* aContent = myWorkshop->propertyPanel()->contentWidget();
430   //qDeleteAll(aContent->children());
431   //aFactory.createWidget(aContent);
432
433   anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
434   anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
435
436   //anOperation->setModelWidgets(aXmlRepr.toStdString(), aFactory.getModelWidgets());
437
438   // connect the operation
439   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
440   if (aPreviewOp) {
441     connect(aPreviewOp, SIGNAL(featureConstructed(FeaturePtr, int)),
442             this, SLOT(onFeatureConstructed(FeaturePtr, int)));
443     connect(aPreviewOp, SIGNAL(launchOperation(std::string, ObjectPtr)),
444             this, SLOT(onLaunchOperation(std::string, ObjectPtr)));
445     connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
446             this, SLOT(onMultiSelectionEnabled(bool)));
447
448     connect(aPreviewOp, SIGNAL(stopSelection(const QList<ObjectPtr>&, const bool)),
449             this, SLOT(onStopSelection(const QList<ObjectPtr>&, const bool)));
450     connect(aPreviewOp, SIGNAL(setSelection(const QList<ObjectPtr>&)),
451             this, SLOT(onSetSelection(const QList<ObjectPtr>&)));
452
453      connect(aPreviewOp, SIGNAL(closeLocalContext()),
454              this, SLOT(onCloseLocalContext()));
455
456     PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
457     if (aSketchOp) {
458       connect(aSketchOp, SIGNAL(planeSelected(double, double, double)),
459               this, SLOT(onPlaneSelected(double, double, double)));
460       connect(aSketchOp, SIGNAL(fitAllView()),
461               this, SLOT(onFitAllView()));
462     }
463   }
464
465   return anOperation;
466 }
467
468 void PartSet_Module::sendOperation(ModuleBase_Operation* theOperation)
469 {
470   static Events_ID aModuleEvent = Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED);
471   Config_PointerMessage aMessage(aModuleEvent, this);
472   aMessage.setPointer(theOperation);
473   Events_Loop::loop()->send(aMessage);
474 }
475
476 boost::shared_ptr<GeomAPI_Shape> getPlane(double theX, double theY, double theZ)
477 {
478   boost::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
479   boost::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(theX, theY, theZ));
480   return GeomAlgoAPI_FaceBuilder::square(anOrigin, aNormal, PLANE_SIZE);
481 }
482
483 void PartSet_Module::showPlanes()
484 {
485   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
486   // Show selection planes
487   if (!myXPlane) {
488     boost::shared_ptr<GeomAPI_Shape> aPlaneX = getPlane(1, 0, 0);
489     myXPlane = boost::shared_ptr<GeomAPI_AISObject>(new GeomAPI_AISObject());
490     myXPlane->createShape(aPlaneX);
491     myXPlane->setColor(Colors::COLOR_RED);
492     myXPlane->setWidth(SKETCH_WIDTH);
493   }
494   if (!myYPlane) {
495     boost::shared_ptr<GeomAPI_Shape> aPlaneY = getPlane(0, 1, 0);
496     myYPlane = boost::shared_ptr<GeomAPI_AISObject>(new GeomAPI_AISObject());
497     myYPlane->createShape(aPlaneY);
498     myYPlane->setColor(Colors::COLOR_GREEN);
499     myYPlane->setWidth(SKETCH_WIDTH);
500   }
501   if (!myZPlane) {
502     boost::shared_ptr<GeomAPI_Shape> aPlaneZ = getPlane(0, 0, 1);
503     myZPlane = boost::shared_ptr<GeomAPI_AISObject>(new GeomAPI_AISObject());
504     myZPlane->createShape(aPlaneZ);
505     myZPlane->setColor(Colors::COLOR_BLUE);
506     myZPlane->setWidth(SKETCH_WIDTH);
507   }
508   aDisplayer->display(myXPlane, false);
509   aDisplayer->display(myYPlane, false);
510   aDisplayer->display(myZPlane, false);
511   aDisplayer->updateViewer();
512 }
513
514 void PartSet_Module::erasePlanes()
515 {
516   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
517   aDisplayer->erase(myXPlane, false);
518   aDisplayer->erase(myYPlane, false);
519   aDisplayer->erase(myZPlane, false);
520   aDisplayer->updateViewer();
521 }
522
523 void PartSet_Module::visualizePreview(FeaturePtr theFeature, bool isDisplay,
524                                       const bool isUpdateViewer)
525 {
526   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
527   if (!anOperation)
528     return;
529
530   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
531   if (!aPreviewOp)
532     return;
533
534   ResultPtr aResult = theFeature->firstResult();
535   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
536   if (isDisplay) {
537     boost::shared_ptr<SketchPlugin_Feature> aSPFeature = 
538       boost::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature);
539     if (aSPFeature) {
540       PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
541       if (aSketchOp && !aSketchOp->hasSketchPlane())
542         showPlanes();
543     }
544   }
545   else
546     aDisplayer->erase(aResult, false);
547
548   if (isUpdateViewer)
549     aDisplayer->updateViewer();
550 }
551
552 void PartSet_Module::activateFeature(ObjectPtr theFeature, const bool isUpdateViewer)
553 {
554   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
555   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
556   if (aPreviewOp) {
557     PartSet_OperationSketch* aOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
558     if (!aOp) {
559       XGUI_Displayer* aDisplayer = myWorkshop->displayer();
560       aDisplayer->activateInLocalContext(theFeature, aPreviewOp->getSelectionModes(theFeature),
561                                          isUpdateViewer);
562     }
563   }
564 }
565
566 void PartSet_Module::updateCurrentPreview(const std::string& theCmdId)
567 {
568   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
569   if (!anOperation)
570     return;
571
572   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
573   if (!aPreviewOp)
574     return;
575
576   FeaturePtr aFeature = aPreviewOp->feature();
577   if (!aFeature || aFeature->getKind() != theCmdId)
578     return;
579
580   std::list<FeaturePtr> aList = aPreviewOp->subFeatures();
581   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
582   std::list<int> aModes = aPreviewOp->getSelectionModes(aPreviewOp->feature());
583
584   std::list<FeaturePtr>::const_iterator anIt = aList.begin(), 
585                                         aLast = aList.end();
586   for (; anIt != aLast; anIt++) {
587     boost::shared_ptr<SketchPlugin_Feature> aSPFeature = 
588       boost::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
589     if (!aSPFeature)
590       continue;
591     visualizePreview((*anIt), true, false);
592     aDisplayer->activateInLocalContext((*anIt)->firstResult(), aModes, false);
593   }
594   aDisplayer->updateViewer();
595 }
596
597 void PartSet_Module::editFeature(FeaturePtr theFeature)
598 {
599   if (!theFeature)
600     return;
601
602 //  if (theFeature->getKind() == SKETCH_KIND) {
603     //FeaturePtr aFeature = theFeature;
604     //if (XGUI_Tools::isModelObject(aFeature)) {
605     //  ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
606     //  aFeature = aObject->featureRef();
607     //}
608
609     //if (aFeature) {
610       onLaunchOperation(theFeature->getKind(), theFeature);
611       updateCurrentPreview(theFeature->getKind());
612     //}
613 //  }
614 }
615
616 void PartSet_Module::onStorePoint2D(FeaturePtr theFeature, const std::string& theAttribute)
617 {
618   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
619                                        myWorkshop->operationMgr()->currentOperation());
620   if (!aPreviewOp)
621     return;
622
623   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
624         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theFeature->data()->attribute(theAttribute));
625
626   PartSet_Tools::setConstraints(aPreviewOp->sketch(), theFeature, theAttribute,
627                                 aPoint->x(), aPoint->y());
628 }
629
630 /*bool PartSet_Module::isFeatureEnabled(const QString& theCmdId) const
631 {
632   XGUI_OperationMgr* aOpMgr = myWorkshop->operationMgr();
633   XGUI_ActionsMgr* aActMgr = myWorkshop->actionsMgr();
634
635   ModuleBase_Operation* aOperation = aOpMgr->currentOperation();
636   if (!aOperation)
637     return !aActMgr->isNested(theCmdId);
638
639   PartSet_OperationFeatureEdit* aSketchEdtOp = dynamic_cast<PartSet_OperationFeatureEdit*>(aOperation);
640   if (aSketchEdtOp) {
641     QStringList aConstraintList;
642     aConstraintList<<"SketchConstraintDistance"<<"SketchConstraintLength"
643       <<"SketchConstraintRadius"<<"SketchConstraintParallel"<<"SketchConstraintPerpendicular";
644     return aConstraintList.contains(theCmdId);
645   }
646   QStringList aList = aActMgr->nestedCommands(aOperation->id());
647   return aList.contains(theCmdId);
648 }*/
649
650 QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent, 
651                          Config_WidgetAPI* theWidgetApi, QList<ModuleBase_ModelWidget*>& theModelWidgets)
652 {
653   if (theType == "sketch-start-label") {
654     PartSet_WidgetSketchLabel* aWgt = new PartSet_WidgetSketchLabel(theParent, theWidgetApi);
655     aWgt->setOperationsMgr(myWorkshop->operationMgr());
656     theModelWidgets.append(aWgt);
657     return aWgt->getControl();
658   } else
659     return 0;
660 }