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