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