Salome HOME
3916b3d0a0245fa9150f2a89323df5a18e135961
[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_OperationFeatureEdit.h>
5 #include <PartSet_Listener.h>
6 #include <PartSet_TestOCC.h>
7 #include <PartSet_WidgetSketchLabel.h>
8 #include <PartSet_Validators.h>
9 #include <PartSet_Tools.h>
10
11 #include <ModuleBase_Operation.h>
12 #include <ModuleBase_OperationDescription.h>
13 #include <ModuleBase_WidgetFactory.h>
14 #include <ModuleBase_Operation.h>
15
16 #include <ModelAPI_Object.h>
17 #include <ModelAPI_Events.h>
18 #include <ModelAPI_Validator.h>
19 #include <ModelAPI_Data.h>
20 #include <ModelAPI_Session.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         aDisplayer->clearSelected();
189       }
190     }
191   }// else {
192     // Activate results of current feature for selection
193     //FeaturePtr aFeature = theOperation->feature();
194     //XGUI_Displayer* aDisplayer = aXWshp->displayer();
195     //std::list<ResultPtr> aResults = aFeature->results();
196     //std::list<ResultPtr>::const_iterator aIt;
197     //for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
198     //  aDisplayer->activate(*aIt);
199     //}    
200   //}
201 }
202
203 void PartSet_Module::onContextMenuCommand(const QString& theId, bool isChecked)
204 {
205   QList<ObjectPtr> aFeatures = workshop()->selection()->selectedObjects();
206   if (theId == "EDIT_CMD" && (aFeatures.size() > 0)) {
207     FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aFeatures.first());
208     if (aFeature)
209       editFeature(aFeature);
210   }
211 }
212
213 void PartSet_Module::onMousePressed(QMouseEvent* theEvent)
214 {
215   XGUI_Workshop* aXWshp = xWorkshop();
216   PartSet_OperationSketchBase* aPreviewOp = 
217     dynamic_cast<PartSet_OperationSketchBase*>(workshop()->currentOperation());
218   if (aPreviewOp) {
219     ModuleBase_ISelection* aSelection = workshop()->selection();
220     aPreviewOp->mousePressed(theEvent, myWorkshop->viewer(), aSelection);
221   }
222 }
223
224 void PartSet_Module::onMouseReleased(QMouseEvent* theEvent)
225 {
226   PartSet_OperationSketchBase* aPreviewOp = 
227     dynamic_cast<PartSet_OperationSketchBase*>(myWorkshop->currentOperation());
228   if (aPreviewOp) {
229     ModuleBase_ISelection* aSelection = workshop()->selection();
230     // Initialise operation with preliminary selection
231     aPreviewOp->mouseReleased(theEvent, myWorkshop->viewer(), aSelection);
232   }
233 }
234
235 void PartSet_Module::onMouseMoved(QMouseEvent* theEvent)
236 {
237   PartSet_OperationSketchBase* aPreviewOp = 
238     dynamic_cast<PartSet_OperationSketchBase*>(myWorkshop->currentOperation());
239   if (aPreviewOp)
240     aPreviewOp->mouseMoved(theEvent, myWorkshop->viewer());
241 }
242
243 void PartSet_Module::onKeyRelease(QKeyEvent* theEvent)
244 {
245   ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
246   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
247   if (aPreviewOp) {
248     aPreviewOp->keyReleased(theEvent->key());
249   }
250 }
251
252 void PartSet_Module::onMouseDoubleClick(QMouseEvent* theEvent)
253 {
254   PartSet_OperationSketchBase* aPreviewOp = 
255     dynamic_cast<PartSet_OperationSketchBase*>(myWorkshop->currentOperation());
256   Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
257   if (aPreviewOp && (!aView.IsNull())) {
258     ModuleBase_ISelection* aSelection = workshop()->selection();
259     // Initialise operation with preliminary selection
260     aPreviewOp->mouseDoubleClick(theEvent, aView, aSelection);
261   }
262 }
263
264 void PartSet_Module::onPlaneSelected(double theX, double theY, double theZ)
265 {
266   myWorkshop->viewer()->setViewProjection(theX, theY, theZ);
267   xWorkshop()->actionsMgr()->update();
268   // Set working plane
269   ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
270   FeaturePtr aSketch = anOperation->feature();
271   setSketchingMode(getSketchPlane(aSketch));
272 }
273
274 void PartSet_Module::onFitAllView()
275 {
276   myWorkshop->viewer()->fitAll();
277 }
278
279 void PartSet_Module::onRestartOperation(std::string theName, ObjectPtr theObject)
280 {
281   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
282
283   std::string aKind = aFeature ? aFeature->getKind() : "";
284   ModuleBase_Operation* anOperation = createOperation(theName, aKind);
285
286   PartSet_OperationSketchBase* aSketchOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
287   if (aSketchOp) {
288     PartSet_OperationFeatureCreate* aCreateOp = dynamic_cast<PartSet_OperationFeatureCreate*>(anOperation);
289     if (aCreateOp)
290       aCreateOp->initFeature(aFeature);
291     else {
292       anOperation->setFeature(aFeature);
293     }
294     ModuleBase_ISelection* aSelection = workshop()->selection();
295     // Initialise operation with preliminary selection
296     aSketchOp->initSelection(aSelection, myWorkshop->viewer());
297   } else if (aFeature) { // In case of edit operation: set the previously created feature to the operation
298     anOperation->setFeature(aFeature);
299     ////Deactivate result of current feature in order to avoid its selection
300     XGUI_Displayer* aDisplayer = xWorkshop()->displayer();
301     std::list<ResultPtr> aResults = aFeature->results();
302     std::list<ResultPtr>::const_iterator aIt;
303     for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
304       aDisplayer->deactivate(*aIt);
305     }
306   }
307   sendOperation(anOperation);
308   xWorkshop()->actionsMgr()->updateCheckState();
309 }
310
311 void PartSet_Module::onMultiSelectionEnabled(bool theEnabled)
312 {
313   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
314   aViewer->enableMultiselection(theEnabled);
315 }
316
317 void PartSet_Module::onStopSelection(const QList<ObjectPtr>& theFeatures, const bool isStop)
318 {
319   XGUI_Displayer* aDisplayer = xWorkshop()->displayer();
320   //if (!isStop) {
321   //  foreach(ObjectPtr aObject, theFeatures) {
322   //    activateFeature(aObject);
323   //  }
324   //}
325   aDisplayer->stopSelection(theFeatures, isStop, false);
326
327   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
328   aViewer->enableSelection(!isStop);
329
330   aDisplayer->updateViewer();
331 }
332
333 void PartSet_Module::onSetSelection(const QList<ObjectPtr>& theFeatures)
334 {
335   XGUI_Displayer* aDisplayer = xWorkshop()->displayer();
336   aDisplayer->setSelected(theFeatures, false);
337   aDisplayer->updateViewer();
338 }
339
340 void PartSet_Module::setSketchingMode(const gp_Pln& thePln)
341 {
342   XGUI_Displayer* aDisplayer = xWorkshop()->displayer();
343   if (!myPlaneFilter.IsNull()) {
344     aDisplayer->removeSelectionFilter(myPlaneFilter);
345     myPlaneFilter.Nullify();
346   }
347   QIntList aModes;
348   // Clear standard selection modes
349   aDisplayer->setSelectionModes(aModes);
350   aDisplayer->openLocalContext();
351
352   // Set filter
353   mySketchFilter = new ModuleBase_ShapeInPlaneFilter(thePln);
354   aDisplayer->addSelectionFilter(mySketchFilter);
355
356   // Get default selection modes
357   aModes = sketchSelectionModes(ObjectPtr());
358   aDisplayer->activateObjectsOutOfContext(aModes);
359 }
360
361 void PartSet_Module::onFeatureConstructed(ObjectPtr theFeature, int theMode)
362 {
363   bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Hide;
364   ModuleBase_Operation* aCurOperation = myWorkshop->currentOperation();
365   PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
366   if (aPrevOp) {
367     std::list<FeaturePtr> aList = aPrevOp->subFeatures();
368     XGUI_Displayer* aDisplayer = xWorkshop()->displayer();
369     QIntList aModes = sketchSelectionModes(aPrevOp->feature());
370     std::list<FeaturePtr>::iterator aSFIt;
371     for (aSFIt = aList.begin(); aSFIt != aList.end(); ++aSFIt) {
372       std::list<ResultPtr> aResults = (*aSFIt)->results();
373       std::list<ResultPtr>::iterator aIt;
374       for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
375         if (!isDisplay)
376           aDisplayer->erase((*aIt), false);
377       }
378       if (!isDisplay)
379         aDisplayer->erase((*aSFIt), false);
380     }
381     //aDisplayer->deactivateObjectsOutOfContext();
382   }
383   if (isDisplay)
384     ModelAPI_EventCreator::get()->sendUpdated(
385         theFeature, Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
386 }
387
388 ModuleBase_Operation* PartSet_Module::createOperation(const std::string& theCmdId,
389                                                       const std::string& theFeatureKind)
390 {
391   // create the operation
392   ModuleBase_Operation* anOperation = 0;
393   if (theCmdId == PartSet_OperationSketch::Type()) {
394     anOperation = new PartSet_OperationSketch(theCmdId.c_str(), this);
395   } else {
396     ModuleBase_Operation* aCurOperation = myWorkshop->currentOperation();
397     CompositeFeaturePtr aSketch;
398     PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
399     if (aPrevOp) {
400       aSketch = aPrevOp->sketch();
401     }
402     if (PartSet_OperationFeatureCreate::canProcessKind(theCmdId)) {
403       anOperation = new PartSet_OperationFeatureCreate(theCmdId.c_str(), this, aSketch);
404     } else if (theCmdId == PartSet_OperationFeatureEdit::Type()) {
405       anOperation = new PartSet_OperationFeatureEdit(theCmdId.c_str(), this, aSketch);
406     }
407   }
408
409   if (!anOperation) {
410     anOperation = new ModuleBase_Operation(theCmdId.c_str(), this);
411   }
412
413   // set operation description and list of widgets corresponding to the feature xml definition
414   std::string aFeatureKind = theFeatureKind.empty() ? theCmdId : theFeatureKind;
415
416   std::string aPluginFileName = featureFile(aFeatureKind);
417   Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
418   aWdgReader.readAll();
419   std::string aXmlCfg = aWdgReader.featureWidgetCfg(aFeatureKind);
420   std::string aDescription = aWdgReader.featureDescription(aFeatureKind);
421
422   anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
423   anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
424
425   // connect the operation
426   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
427   if (aPreviewOp) {
428     connect(aPreviewOp, SIGNAL(featureConstructed(ObjectPtr, int)), this,
429             SLOT(onFeatureConstructed(ObjectPtr, int)));
430     connect(aPreviewOp, SIGNAL(restartRequired(std::string, ObjectPtr)), this,
431             SLOT(onRestartOperation(std::string, ObjectPtr)));
432     // If manage multi selection the it will be impossible to select more then one
433     // object under operation Edit
434 //    connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)), this,
435 //            SLOT(onMultiSelectionEnabled(bool)));
436
437     connect(aPreviewOp, SIGNAL(stopSelection(const QList<ObjectPtr>&, const bool)), this,
438             SLOT(onStopSelection(const QList<ObjectPtr>&, const bool)));
439     connect(aPreviewOp, SIGNAL(setSelection(const QList<ObjectPtr>&)), this,
440             SLOT(onSetSelection(const QList<ObjectPtr>&)));
441
442     PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
443     if (aSketchOp) {
444       connect(aSketchOp, SIGNAL(planeSelected(double, double, double)), this,
445               SLOT(onPlaneSelected(double, double, double)));
446       connect(aSketchOp, SIGNAL(fitAllView()), this, SLOT(onFitAllView()));
447     }
448   }
449
450   return anOperation;
451 }
452
453
454 void PartSet_Module::updateCurrentPreview(const std::string& theCmdId)
455 {
456   ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
457   if (!anOperation)
458     return;
459
460   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
461   if (!aPreviewOp)
462     return;
463
464   FeaturePtr aFeature = aPreviewOp->feature();
465   if (!aFeature || aFeature->getKind() != theCmdId)
466     return;
467
468   XGUI_Displayer* aDisplayer = xWorkshop()->displayer();
469   // Hide result of sketch
470   std::list<ResultPtr> aResults = aFeature->results();
471   std::list<ResultPtr>::const_iterator aIt;
472   for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt)
473     aDisplayer->erase(*aIt, false);
474
475   std::list<FeaturePtr> aList = aPreviewOp->subFeatures();
476
477   std::list<FeaturePtr>::const_iterator anIt = aList.begin(), aLast = aList.end();
478   for (; anIt != aLast; anIt++) {
479     boost::shared_ptr<SketchPlugin_Feature> aSPFeature = boost::dynamic_pointer_cast<
480         SketchPlugin_Feature>(*anIt);
481     if (!aSPFeature)
482       continue;
483     std::list<ResultPtr> aResults = aSPFeature->results();
484     std::list<ResultPtr>::const_iterator aRIt;
485     for (aRIt = aResults.cbegin(); aRIt != aResults.cend(); ++aRIt) {
486       aDisplayer->display((*aRIt), false);
487       aDisplayer->activate((*aRIt), sketchSelectionModes((*aRIt)));
488     }
489     aDisplayer->display(aSPFeature, false);
490     aDisplayer->activate(aSPFeature, sketchSelectionModes(aSPFeature));
491   }
492   aDisplayer->updateViewer();
493 }
494
495 void PartSet_Module::editFeature(FeaturePtr theFeature)
496 {
497   if (!theFeature)
498     return;
499
500 //  if (theFeature->getKind() == SKETCH_KIND) {
501   //FeaturePtr aFeature = theFeature;
502   //if (XGUI_Tools::isModelObject(aFeature)) {
503   //  ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
504   //  aFeature = aObject->featureRef();
505   //}
506
507   //if (aFeature) {
508   onRestartOperation(theFeature->getKind(), theFeature);
509   updateCurrentPreview(theFeature->getKind());
510   //}
511 //  }
512 }
513
514 void PartSet_Module::onStorePoint2D(ObjectPtr theFeature, const std::string& theAttribute)
515 {
516   FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theFeature);
517
518   PartSet_OperationSketchBase* aPreviewOp = 
519     dynamic_cast<PartSet_OperationSketchBase*>(myWorkshop->currentOperation());
520   if (!aPreviewOp)
521     return;
522
523   boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
524       aFeature->data()->attribute(theAttribute));
525
526   PartSet_Tools::setConstraints(aPreviewOp->sketch(), aFeature, theAttribute, aPoint->x(),
527                                 aPoint->y());
528 }
529
530 QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
531                                             Config_WidgetAPI* theWidgetApi,
532                                             QList<ModuleBase_ModelWidget*>& theModelWidgets)
533 {
534   if (theType == "sketch-start-label") {
535     PartSet_WidgetSketchLabel* aWgt = new PartSet_WidgetSketchLabel(theParent, theWidgetApi, "");
536     aWgt->setOperationsMgr(xWorkshop()->operationMgr());
537     theModelWidgets.append(aWgt);
538     return aWgt->getControl();
539   } else
540     return 0;
541 }
542
543
544 XGUI_Workshop* PartSet_Module::xWorkshop() const
545 {
546   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
547   if (aConnector) {
548     return aConnector->workshop();
549   }
550   return 0;
551 }
552
553
554 QIntList PartSet_Module::sketchSelectionModes(ObjectPtr theFeature)
555 {
556   QIntList aModes;
557   FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theFeature);
558   if (aFeature) {
559     if (aFeature->getKind() == SketchPlugin_Sketch::ID()) {
560       aModes.append(TopAbs_FACE);
561       return aModes;
562     } else if (PartSet_Tools::isConstraintFeature(aFeature->getKind())) {
563       aModes.append(AIS_DSM_Text);
564       aModes.append(AIS_DSM_Line);
565       return aModes;
566     }
567   } 
568   aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_VERTEX));
569   aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_EDGE));
570   return aModes;
571 }
572
573
574 gp_Pln PartSet_Module::getSketchPlane(FeaturePtr theSketch) const
575 {
576   DataPtr aData = theSketch->data();
577   boost::shared_ptr<GeomDataAPI_Point> anOrigin = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
578       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
579   boost::shared_ptr<GeomDataAPI_Dir> aNorm = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
580       aData->attribute(SketchPlugin_Sketch::NORM_ID()));
581   gp_Pnt aOrig(anOrigin->x(), anOrigin->y(), anOrigin->z());
582   gp_Dir aDir(aNorm->x(), aNorm->y(), aNorm->z());
583   return gp_Pln(aOrig, aDir);
584 }
585
586
587 void PartSet_Module::onSelectionChanged()
588 {
589   ModuleBase_ISelection* aSelect = myWorkshop->selection();
590   QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
591   // We need to stop edit operation if selection is cleared
592   if (aSelected.size() == 0) {
593     // do not perform commit of the current edit operation here, because
594     // this functionality is realized inside this operation
595     /*PartSet_OperationFeatureEdit* anEditOp = 
596       dynamic_cast<PartSet_OperationFeatureEdit*>(myWorkshop->currentOperation());
597     if (!anEditOp)
598       return;
599     anEditOp->commit();*/
600   } else {
601     PartSet_OperationSketchBase* aSketchOp = 
602       dynamic_cast<PartSet_OperationSketchBase*>(myWorkshop->currentOperation());
603     if (aSketchOp) {
604       aSketchOp->selectionChanged(aSelect);
605     }
606   }
607 }