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