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