Salome HOME
Issue #3069: Do not ask confirmation for operation closing if it doesn't produce...
[modules/shaper.git] / src / PartSet / PartSet_Module.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "PartSet_Module.h"
21 #include "PartSet_WidgetSketchLabel.h"
22 #include "PartSet_Validators.h"
23 #include "PartSet_Tools.h"
24 #include "PartSet_PreviewPlanes.h"
25 #include "PartSet_WidgetPoint2d.h"
26 #include "PartSet_WidgetPoint2DFlyout.h"
27 #include "PartSet_WidgetShapeSelector.h"
28 #include "PartSet_WidgetMultiSelector.h"
29 #include "PartSet_WidgetFeaturePointSelector.h"
30 #include "PartSet_WidgetEditor.h"
31 #include "PartSet_WidgetFileSelector.h"
32 #include "PartSet_WidgetSketchCreator.h"
33 #include "PartSet_SketcherMgr.h"
34 #include "PartSet_SketcherReentrantMgr.h"
35 #include "PartSet_ResultSketchPrs.h"
36 #include "PartSet_MenuMgr.h"
37 #include "PartSet_CustomPrs.h"
38 #include "PartSet_IconFactory.h"
39 #include "PartSet_OverconstraintListener.h"
40 #include "PartSet_TreeNodes.h"
41 #include "PartSet_FieldStepPrs.h"
42
43 #include "PartSet_Filters.h"
44 #include "PartSet_FilterInfinite.h"
45
46 #ifdef _DEBUG
47 #include <QDebug>
48 #endif
49
50 #include <PartSetPlugin_Remove.h>
51 #include <PartSetPlugin_Part.h>
52 #include <PartSetPlugin_Duplicate.h>
53
54 #include <SketchPlugin_ConstraintCoincidence.h>
55
56 #include <ModuleBase_Operation.h>
57 #include <ModuleBase_IViewer.h>
58 #include <ModuleBase_IViewWindow.h>
59 #include <ModuleBase_IPropertyPanel.h>
60 #include <ModuleBase_ISelectionActivate.h>
61 #include <ModuleBase_WidgetChoice.h>
62 #include <ModuleBase_WidgetEditor.h>
63 #include <ModuleBase_WidgetValidated.h>
64 #include <ModuleBase_Tools.h>
65 #include <ModuleBase_OperationFeature.h>
66 #include <ModuleBase_WidgetFactory.h>
67 #include <ModuleBase_OperationDescription.h>
68 #include <ModuleBase_ViewerPrs.h>
69 #include <ModelAPI_ResultField.h>
70
71 #include <ModelAPI_Object.h>
72 #include <ModelAPI_Events.h>
73 #include <ModelAPI_Validator.h>
74 #include <ModelAPI_Data.h>
75 #include <ModelAPI_Session.h>
76 #include <ModelAPI_ResultBody.h>
77 #include <ModelAPI_AttributeString.h>
78 #include <ModelAPI_AttributeSelectionList.h>
79 #include <ModelAPI_Tools.h>
80 #include <ModelAPI_ResultConstruction.h>
81 #include <ModelAPI_AttributeIntArray.h>
82
83 #include <GeomDataAPI_Point2D.h>
84 #include <GeomDataAPI_Point.h>
85 #include <GeomDataAPI_Dir.h>
86
87 #include <XGUI_ActiveControlMgr.h>
88 #include <XGUI_ActiveControlSelector.h>
89 #include <XGUI_ActionsMgr.h>
90 #include <XGUI_ContextMenuMgr.h>
91 #include <XGUI_CustomPrs.h>
92 #include <XGUI_DataModel.h>
93 #include <XGUI_Displayer.h>
94 #include <XGUI_ErrorMgr.h>
95 #include <XGUI_FacesPanelSelector.h>
96 #include <XGUI_ModuleConnector.h>
97 #include <XGUI_ObjectsBrowser.h>
98 #include <XGUI_OperationMgr.h>
99 #include <XGUI_PropertyPanel.h>
100 #include <XGUI_SelectionMgr.h>
101 #include <XGUI_Tools.h>
102 #include <XGUI_Workshop.h>
103
104 #include <SketchPlugin_ConstraintAngle.h>
105 #include <SketchPlugin_ConstraintLength.h>
106 #include <SketchPlugin_ConstraintDistance.h>
107 #include <SketchPlugin_ConstraintParallel.h>
108 #include <SketchPlugin_ConstraintPerpendicular.h>
109 #include <SketchPlugin_ConstraintRadius.h>
110 #include <SketchPlugin_Feature.h>
111 #include <SketchPlugin_Projection.h>
112 #include <SketchPlugin_Sketch.h>
113
114 #include <SketcherPrs_SymbolPrs.h>
115 #include <SketcherPrs_PositionMgr.h>
116 #include <SketcherPrs_Coincident.h>
117 #include <SketcherPrs_Tools.h>
118
119 #include <Events_Loop.h>
120 #include <Config_PropManager.h>
121 #include <Config_Keywords.h>
122
123 #include <AIS_Dimension.hxx>
124 #include <AIS_InteractiveObject.hxx>
125 #include <StdSelect_TypeOfFace.hxx>
126 #include <TopoDS_Vertex.hxx>
127 #include <TopoDS.hxx>
128 #include <TopoDS_Shape.hxx>
129 #include <BRep_Tool.hxx>
130
131 #include <QObject>
132 #include <QMouseEvent>
133 #include <QString>
134 #include <QTimer>
135 #include <QApplication>
136 #include <QMessageBox>
137 #include <QMainWindow>
138 #include <QLineEdit>
139 #include <QString>
140
141 #include <GeomAlgoAPI_FaceBuilder.h>
142 #include <GeomDataAPI_Dir.h>
143
144 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
145
146 /*!Create and return new instance of XGUI_Module*/
147 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(ModuleBase_IWorkshop* theWshop)
148 {
149   return new PartSet_Module(theWshop);
150 }
151
152 //******************************************************
153 PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
154 : ModuleBase_IModule(theWshop),
155   myVisualLayerId(0),
156   myRoot(0),
157   myIsOperationIsLaunched(false)
158 {
159   new PartSet_IconFactory(this);
160
161   mySketchMgr = new PartSet_SketcherMgr(this);
162   mySketchReentrantMgr = new PartSet_SketcherReentrantMgr(theWshop);
163
164   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWshop);
165   XGUI_Workshop* aWorkshop = aConnector->workshop();
166
167   ModuleBase_IViewer* aViewer = theWshop->viewer();
168   connect(aViewer, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)),
169           this, SLOT(onKeyRelease(ModuleBase_IViewWindow*, QKeyEvent*)));
170   connect(aViewer, SIGNAL(viewTransformed(int)),
171           SLOT(onViewTransformed(int)));
172   connect(aViewer, SIGNAL(viewCreated(ModuleBase_IViewWindow*)),
173           SLOT(onViewCreated(ModuleBase_IViewWindow*)));
174   myMenuMgr = new PartSet_MenuMgr(this);
175   myCustomPrs = new PartSet_CustomPrs(theWshop);
176
177   myOverconstraintListener = new PartSet_OverconstraintListener(theWshop);
178
179   Events_Loop* aLoop = Events_Loop::loop();
180   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED));
181   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
182
183   registerSelectionFilter(SF_GlobalFilter, new PartSet_GlobalFilter(myWorkshop));
184   registerSelectionFilter(SF_FilterInfinite, new PartSet_FilterInfinite(myWorkshop));
185   Handle(PartSet_ResultGroupNameFilter) aCRFilter = new PartSet_ResultGroupNameFilter(myWorkshop);
186   std::set<std::string> aCRGroupNames;
187   aCRGroupNames.insert(ModelAPI_ResultConstruction::group());
188   aCRFilter->setGroupNames(aCRGroupNames);
189   registerSelectionFilter(SF_ResultGroupNameFilter, aCRFilter);
190
191   setDefaultConstraintShown();
192
193   Config_PropManager::registerProp("Visualization", "operation_parameter_color",
194                           "Reference shape wireframe color in operation", Config_Prop::Color,
195                           PartSet_CustomPrs::OPERATION_PARAMETER_COLOR());
196   Config_PropManager::registerProp("Visualization", "operation_result_color",
197                           "Result shape wireframe color in operation", Config_Prop::Color,
198                           PartSet_CustomPrs::OPERATION_RESULT_COLOR());
199   Config_PropManager::registerProp("Visualization", "operation_highlight_color",
200                           "Multi selector item color in operation", Config_Prop::Color,
201                           PartSet_CustomPrs::OPERATION_HIGHLIGHT_COLOR());
202   Config_PropManager::registerProp("Visualization", "operation_remove_feature_color",
203                           "Color of removed feature in operation", Config_Prop::Color,
204                           PartSet_CustomPrs::OPERATION_REMOVE_FEATURE_COLOR());
205   Config_PropManager::registerProp("Visualization", "sketch_preview_plane",
206                           "Color of sketch plane", Config_Prop::Color,
207                           PartSet_CustomPrs::OPERATION_SKETCH_PLANE());
208
209   Config_PropManager::registerProp("Visualization", "hidden_face_transparency",
210                                    "Hidden faces transparency",
211                                    Config_Prop::DblSpin,
212                                    "0.8");
213   std::ostringstream aStream;
214   aStream << SketcherPrs_Tools::getDefaultArrowSize();
215   Config_PropManager::registerProp("Visualization", "dimension_arrow_size",
216     "Dimension arrow size", Config_Prop::IntSpin, aStream.str());
217
218   Config_PropManager::registerProp("Visualization", "dimension_font", "Dimension font",
219     Config_Prop::String, "Times-bold");
220
221   aStream.str("");
222   aStream.clear();
223   aStream << SketcherPrs_Tools::getDefaultTextHeight();
224   Config_PropManager::registerProp("Visualization", "dimension_value_size",
225     "Dimension value size", Config_Prop::IntSpin, aStream.str());
226
227   Config_PropManager::registerProp("Visualization", "sketch_dimension_color",
228     "Dimension color",
229     Config_Prop::Color, SKETCH_DIMENSION_COLOR);
230
231   Config_PropManager::registerProp("Shortcuts", "add_parameter_shortcut",
232     "Add parameter in parameters manager dialog",
233     Config_Prop::Shortcut, "Ctrl+A");
234 }
235
236 //******************************************************
237 PartSet_Module::~PartSet_Module()
238 {
239   delete myCustomPrs;
240   delete myOverconstraintListener;
241   delete myRoot;
242 }
243
244 //******************************************************
245 void PartSet_Module::createFeatures()
246 {
247   ModuleBase_IModule::createFeatures();
248   myRoot = new PartSet_RootNode();
249   myRoot->setWorkshop(workshop());
250 }
251
252
253 //******************************************************
254 void PartSet_Module::storeSelection()
255 {
256   // cash is used only to restore selection, so it should be filled in storeSelection and
257   // after applying immediatelly cleared in restoreSelection
258   myCurrentSelection.clear();
259   sketchMgr()->storeSelection(PartSet_SketcherMgr::ST_SelectType, myCurrentSelection);
260 }
261
262 //******************************************************
263 void PartSet_Module::restoreSelection()
264 {
265   // cash is used only to restore selection, so it should be filled in storeSelection and
266   // after applying immediatelly cleared in restoreSelection
267   sketchMgr()->restoreSelection(myCurrentSelection);
268   myCurrentSelection.clear();
269 }
270
271 //******************************************************
272 void PartSet_Module::registerValidators()
273 {
274   //Registering of validators
275   SessionPtr aMgr = ModelAPI_Session::get();
276   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
277   aFactory->registerValidator("PartSet_DistanceSelection", new PartSet_DistanceSelection);
278   aFactory->registerValidator("PartSet_LengthSelection", new PartSet_LengthSelection);
279   aFactory->registerValidator("PartSet_PerpendicularSelection", new PartSet_PerpendicularSelection);
280   aFactory->registerValidator("PartSet_ParallelSelection", new PartSet_ParallelSelection);
281   aFactory->registerValidator("PartSet_RadiusSelection", new PartSet_RadiusSelection);
282   aFactory->registerValidator("PartSet_RigidSelection", new PartSet_RigidSelection);
283   aFactory->registerValidator("PartSet_CoincidentSelection", new PartSet_CoincidentSelection);
284   aFactory->registerValidator("PartSet_HVDirSelection", new PartSet_HVDirSelection);
285   aFactory->registerValidator("PartSet_TangentSelection", new PartSet_TangentSelection);
286   aFactory->registerValidator("PartSet_FilletSelection", new PartSet_FilletSelection);
287   aFactory->registerValidator("PartSet_AngleSelection", new PartSet_AngleSelection);
288   aFactory->registerValidator("PartSet_EqualSelection", new PartSet_EqualSelection);
289   aFactory->registerValidator("PartSet_CollinearSelection", new PartSet_CollinearSelection);
290   aFactory->registerValidator("PartSet_MiddlePointSelection", new PartSet_MiddlePointSelection);
291   aFactory->registerValidator("PartSet_DifferentObjects", new PartSet_DifferentObjectsValidator);
292   aFactory->registerValidator("PartSet_DifferentPoints", new PartSet_DifferentPointsValidator);
293   aFactory->registerValidator("PartSet_CoincidentAttr", new PartSet_CoincidentAttr);
294   aFactory->registerValidator("PartSet_MultyTranslationSelection",
295     new PartSet_MultyTranslationSelection);
296   aFactory->registerValidator("PartSet_SplitSelection", new PartSet_SplitSelection);
297   aFactory->registerValidator("PartSet_ProjectionSelection", new PartSet_ProjectionSelection);
298   aFactory->registerValidator("PartSet_IntersectionSelection", new PartSet_IntersectionSelection);
299 }
300
301 //******************************************************
302 void PartSet_Module::connectToPropertyPanel(ModuleBase_ModelWidget* theWidget,
303                                             const bool isToConnect)
304 {
305   mySketchMgr->connectToPropertyPanel(theWidget, isToConnect);
306 }
307
308 //******************************************************
309 void PartSet_Module::operationCommitted(ModuleBase_Operation* theOperation)
310 {
311   if (sketchMgr()->isNestedSketchOperation(theOperation)) {
312     mySketchMgr->commitNestedSketch(theOperation);
313   }
314   /// deactivate of overconstraint listener should be performed after Sketch commit (#2176)
315   if (PartSet_SketcherMgr::isSketchOperation(theOperation))
316     overconstraintListener()->setActive(false);
317
318   /// Restart sketcher operations automatically
319   if (!mySketchReentrantMgr->operationCommitted(theOperation)) {
320
321     ModuleBase_OperationFeature* aFOperation =
322       dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
323     if (aFOperation && !aFOperation->isEditOperation()) {
324       // the selection is cleared after commit the create operation
325       // in order to do not use the same selected objects in the restarted operation
326       // for common behaviour, the selection is cleared even if the operation is not restarted
327       getWorkshop()->selector()->clearSelection();
328     }
329   }
330 }
331
332 //******************************************************
333 void PartSet_Module::operationAborted(ModuleBase_Operation* theOperation)
334 {
335   /// Restart sketcher operations automatically
336   mySketchReentrantMgr->operationAborted(theOperation);
337   /// deactivate of overconstraint listener should be performed after Sketch abort (#2176)
338   if (PartSet_SketcherMgr::isSketchOperation(theOperation))
339     overconstraintListener()->setActive(false);
340 }
341
342 //******************************************************
343 void PartSet_Module::operationStarted(ModuleBase_Operation* theOperation)
344 {
345   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
346                                                                                 (theOperation);
347   if (aFOperation) {
348     XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(workshop());
349     XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
350
351     ModuleBase_ModelWidget* aFilledWidget = 0;
352     bool aPostonedWidgetActivation = false;
353
354     FeaturePtr aFeature = aFOperation->feature();
355     /// Restart sketcher operations automatically
356     /// it is important to call method of sketch reentrant manager before filling of PP
357     /// because it fills some created feature attributes, these new values should be used
358     /// to fill the property panel
359     mySketchReentrantMgr->operationStarted(theOperation);
360
361     aWorkshop->fillPropertyPanel(aFOperation);
362     // filling the operation values by the current selection
363     // if the operation can be committed after the controls filling, the method perform should
364     // be stopped. Otherwise unnecessary presentations can be shown(e.g. operation prs in sketch)
365     bool isOperationCommitted = false;
366     if (!aFOperation->isEditOperation()) {
367       std::string aGreedAttributeId = ModuleBase_Tools::findGreedAttribute(workshop(), aFeature);
368       // if there is a greed attribute, automatic commit by preselection
369       // for this feature is prohibited
370       aFilledWidget = aFOperation->activateByPreselection(aGreedAttributeId);
371       if (currentOperation() != aFOperation)
372         isOperationCommitted = true;
373       else {
374         if (aGreedAttributeId.empty()) {
375           // a signal should be emitted before the next widget activation
376           // because, the activation of the next widget will give a focus to the widget.
377           // As a result the value of the widget is initialized.
378           // And commit may happens until the value is entered.
379           if (aFilledWidget) {
380             if (mySketchReentrantMgr->canBeCommittedByPreselection())
381               isOperationCommitted = mySketchMgr->operationActivatedByPreselection();
382             // activate the next obligatory widget
383             if (!isOperationCommitted)
384               aPropertyPanel->activateNextWidget(aFilledWidget);
385           }
386         }
387         else { // there is a greed widget
388           const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
389           std::string aFirstAttributeId = aWidgets.front()->attributeID();
390           // activate next widget after greeded if it is the first widget in the panel
391           // else the first panel widget is already activated by operation start
392           if (aFirstAttributeId == aGreedAttributeId)
393             aPostonedWidgetActivation = true;
394         }
395       }
396     } if (!isOperationCommitted) {
397       aWorkshop->connectToPropertyPanel(true);
398       updateSketcherOnStart(aFOperation);
399       updatePresentationsOnStart(aFOperation);
400
401       // the objects of the current operation should be deactivated
402       QObjectPtrList anObjects;
403       anObjects.append(aFeature);
404       std::list<ResultPtr> aResults;
405       ModelAPI_Tools::allResults(aFeature, aResults);
406       std::list<ResultPtr>::const_iterator aIt;
407       for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
408         anObjects.append(*aIt);
409       }
410       QObjectPtrList::const_iterator anIt = anObjects.begin(), aLast = anObjects.end();
411       for (; anIt != aLast; anIt++)
412         aWorkshop->deactivateActiveObject(*anIt, false);
413       if (anObjects.size() > 0) {
414         XGUI_Displayer* aDisplayer = aWorkshop->displayer();
415         aDisplayer->updateViewer();
416       }
417       /// state of command actions should be updated after displayed objects modification because
418       /// deactivation(for example) of objects may influence on selection in the viewer
419       /// State of command actions may depend on selection in the viewer(e.g. Sketch)
420       workshop()->updateCommandStatus();
421     }
422     if (aPostonedWidgetActivation) {
423       // if the widget is an empty in the chain of activated widgets, the current operation
424       // is restarted. It should be performed after functionality of the operation starting
425       aPropertyPanel->activateNextWidget(aFilledWidget);
426     }
427   }
428 }
429
430 //******************************************************
431 void PartSet_Module::updateSketcherOnStart(ModuleBase_Operation* theOperation)
432 {
433   if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
434     mySketchMgr->startSketch(theOperation);
435   }
436   // It is switched off because of
437   // Task #3067: 5.2.2 Drawing in the sketcher: change the mouse cursor arrow
438   //else if (sketchMgr()->isNestedSketchOperation(theOperation)) {
439   //  mySketchMgr->startNestedSketch(theOperation);
440   //}
441 }
442
443 //******************************************************
444 void PartSet_Module::updatePresentationsOnStart(ModuleBase_Operation* theOperation)
445 {
446   ModuleBase_OperationFeature* aFOperation =
447     dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
448   if (aFOperation) {
449     myCustomPrs->activate(aFOperation->feature(), ModuleBase_IModule::CustomizeArguments, false);
450     myCustomPrs->activate(aFOperation->feature(), ModuleBase_IModule::CustomizeResults, false);
451   }
452 }
453
454 //******************************************************
455 void PartSet_Module::operationResumed(ModuleBase_Operation* theOperation)
456 {
457   ModuleBase_IModule::operationResumed(theOperation);
458
459   ModuleBase_OperationFeature* aFOperation =
460     dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
461   if (aFOperation) {
462     myCustomPrs->activate(aFOperation->feature(), ModuleBase_IModule::CustomizeArguments, false);
463     myCustomPrs->activate(aFOperation->feature(), ModuleBase_IModule::CustomizeResults, false);
464   }
465 }
466
467 //******************************************************
468 void PartSet_Module::operationStopped(ModuleBase_Operation* theOperation)
469 {
470   bool isModifiedArgs = myCustomPrs->deactivate(ModuleBase_IModule::CustomizeArguments, false);
471   bool isModifiedResults = myCustomPrs->deactivate(ModuleBase_IModule::CustomizeResults, false);
472   bool isModified = isModifiedArgs || isModifiedResults;
473
474   if (sketchMgr()->isNestedSketchOperation(theOperation)) {
475     mySketchMgr->stopNestedSketch(theOperation);
476   }
477   else if (PartSet_SketcherMgr::isSketchOperation(theOperation))
478     setDefaultConstraintShown();
479
480   //VSV: Viewer is updated on feature update and redisplay
481   if (isModified) {
482     XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
483     XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
484     aDisplayer->updateViewer();
485   }
486
487   QMap<PartSet_Tools::ConstraintVisibleState, bool>::const_iterator
488     anIt = myHasConstraintShown.begin(), aLast = myHasConstraintShown.end();
489   for (; anIt != aLast; anIt++) {
490     mySketchMgr->updateBySketchParameters(anIt.key(), anIt.value());
491   }
492 }
493
494 //******************************************************
495 ModuleBase_Operation* PartSet_Module::currentOperation() const
496 {
497   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
498   XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
499   return anOpMgr->currentOperation();
500 }
501
502 //******************************************************
503 bool PartSet_Module::canUndo() const
504 {
505   bool aCanUndo = false;
506   SessionPtr aMgr = ModelAPI_Session::get();
507   if (aMgr->hasModuleDocument() && aMgr->canUndo()) {
508     aCanUndo = !aMgr->isOperation();
509     if (!aCanUndo) // check the enable state additionally by sketch manager
510       aCanUndo = aMgr->canUndo();
511   }
512   return aCanUndo;
513 }
514
515 //******************************************************
516 bool PartSet_Module::canRedo() const
517 {
518   bool aCanRedo = false;
519   SessionPtr aMgr = ModelAPI_Session::get();
520   if (aMgr->hasModuleDocument() && aMgr->canRedo()) {
521     aCanRedo = !aMgr->isOperation();
522     if (!aCanRedo) // check the enable state additionally by sketch manager
523       aCanRedo = aMgr->canRedo();
524   }
525   return aCanRedo;
526 }
527
528 //******************************************************
529 bool PartSet_Module::canApplyAction(const ObjectPtr& theObject, const QString& theActionId) const
530 {
531   bool aValid = true;
532   if (theActionId == "MOVE_CMD") {
533     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
534     if (aFeature) {
535       ResultPtr aResult = ModuleBase_Tools::firstResult(aFeature);
536       // part features can not be moved in the history.
537       if (aResult.get() && aResult->groupName() == ModelAPI_ResultPart::group())
538         aValid = false;
539     }
540   }
541   return aValid;
542 }
543
544 //******************************************************
545 bool PartSet_Module::canEraseObject(const ObjectPtr& theObject) const
546 {
547   // the sketch manager put the restriction to the objects erase
548   return mySketchMgr->canEraseObject(theObject);
549 }
550
551 //******************************************************
552 bool PartSet_Module::canDisplayObject(const ObjectPtr& theObject) const
553 {
554   // the sketch manager put the restriction to the objects display
555   return mySketchMgr->canDisplayObject(theObject);
556 }
557
558 //******************************************************
559 bool PartSet_Module::canUsePreselection(const QString& thePreviousOperationKind,
560                                         const QString& theStartedOperationKind)
561 {
562   if (ModuleBase_IModule::canUsePreselection(thePreviousOperationKind, theStartedOperationKind))
563     return true;
564
565   return mySketchMgr->isNestedSketchFeature(theStartedOperationKind);
566 }
567
568 /*void PartSet_Module::processHiddenObject(const std::list<ObjectPtr>& theObjects)
569 {
570   mySketchMgr->processHiddenObject(theObjects);
571 }*/
572
573 //******************************************************
574 bool PartSet_Module::canActivateSelection(const ObjectPtr& theObject) const
575 {
576   bool aCanActivate = ModuleBase_IModule::canActivateSelection(theObject);
577
578   ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
579   bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation),
580        isNestedOp = sketchMgr()->isNestedSketchOperation(anOperation);
581   if (isSketchOp || isNestedOp) {
582     // in active sketch operation it is possible to activate operation object in selection
583     // in the edit operation, e.g. points of the line can be moved when the line is edited
584     ModuleBase_OperationFeature* aFOperation =
585       dynamic_cast<ModuleBase_OperationFeature*>(anOperation);
586     aCanActivate = aCanActivate || (aFOperation && aFOperation->isEditOperation());
587   }
588   return aCanActivate;
589 }
590
591 //******************************************************
592 bool PartSet_Module::addViewerMenu(const QMap<QString, QAction*>& theStdActions,
593                                    QWidget* theParent,
594                                    QMap<int, QAction*>& theMenuActions) const
595 {
596   return myMenuMgr->addViewerMenu(theStdActions, theParent, theMenuActions);
597 }
598
599 //******************************************************
600 void PartSet_Module::updateViewerMenu(const QMap<QString, QAction*>& theStdActions)
601 {
602   myMenuMgr->updateViewerMenu(theStdActions);
603 }
604
605 //******************************************************
606 bool PartSet_Module::isActionEnableStateFixed(const int theActionId) const
607 {
608   bool isEnabledFixed = false;
609   if (theActionId == XGUI_ActionsMgr::AcceptAll &&
610       mySketchReentrantMgr->isInternalEditActive())
611     isEnabledFixed = true;
612   return isEnabledFixed;
613 }
614
615 //******************************************************
616 QString PartSet_Module::getFeatureError(const FeaturePtr& theFeature)
617 {
618   QString anError = ModuleBase_IModule::getFeatureError(theFeature);
619   if (anError.isEmpty())
620     anError = sketchMgr()->getFeatureError(theFeature);
621
622   return anError;
623 }
624
625 //******************************************************
626 void PartSet_Module::grantedOperationIds(ModuleBase_Operation* theOperation,
627                                          QStringList& theIds) const
628 {
629   myMenuMgr->grantedOperationIds(theOperation, theIds);
630
631   if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
632     XGUI_Workshop* aWorkshop = getWorkshop();
633     theIds.append(aWorkshop->contextMenuMgr()->action("DELETE_CMD")->text());
634   }
635 }
636
637 //******************************************************
638 void PartSet_Module::activeSelectionModes(QIntList& theModes)
639 {
640   if (mySketchMgr->activeSketch().get())
641     PartSet_SketcherMgr::sketchSelectionModes(mySketchMgr->activeSketch(), theModes);
642   else
643     theModes = XGUI_Tools::workshop(myWorkshop)->viewerSelectionModes();
644 }
645
646 //******************************************************
647 void PartSet_Module::moduleSelectionModes(int theModesType, QIntList& theModes)
648 {
649   customSubShapesSelectionModes(theModes);
650   //theModes.append(XGUI_Tools::workshop(myWorkshop)->viewerSelectionModes());
651   //myWorkshop->module()->activeSelectionModes(theModes);
652 }
653
654 //******************************************************
655 void PartSet_Module::moduleSelectionFilters(const QIntList& theFilterTypes,
656                                             SelectMgr_ListOfFilter& theSelectionFilters)
657 {
658   bool isSketchActive = mySketchMgr->activeSketch().get();
659
660   std::map<ModuleBase_SelectionFilterType, Handle(SelectMgr_Filter)>::const_iterator aFiltersIt =
661     mySelectionFilters.begin();
662   for (; aFiltersIt != mySelectionFilters.end(); aFiltersIt++) {
663     int aFilterType = aFiltersIt->first;
664     // do not add not participating filters in given parameters
665     if (!theFilterTypes.contains(aFilterType))
666       continue;
667
668     // using sketch filters only if sketch operation is active
669     if (!isSketchActive &&
670         mySketchMgr->sketchSelectionFilter((ModuleBase_SelectionFilterType)aFilterType))
671       continue;
672
673     // using filtering of construction results only from faces panel
674     if (aFilterType == SF_ResultGroupNameFilter)
675       continue;
676
677     theSelectionFilters.Append(aFiltersIt->second);
678   }
679 }
680
681 //******************************************************
682 void PartSet_Module::customSubShapesSelectionModes(QIntList& theModes)
683 {
684   if (theModes.contains(TopAbs_FACE))
685     theModes.append(SketcherPrs_Tools::Sel_Sketch_Face);
686   if (theModes.contains(TopAbs_WIRE))
687     theModes.append(SketcherPrs_Tools::Sel_Sketch_Wire);
688
689   if (mySketchMgr->activeSketch().get())
690     PartSet_SketcherMgr::sketchSelectionModes(mySketchMgr->activeSketch(), theModes);
691 }
692
693 //******************************************************
694 void PartSet_Module::getGeomSelection(const std::shared_ptr<ModuleBase_ViewerPrs>& theSelected,
695                                       ObjectPtr& theObject, AttributePtr& theAttribute)
696 {
697   ObjectPtr anObject = theSelected->object();
698   GeomShapePtr aShape = theSelected->shape();
699
700   theAttribute = findAttribute(anObject, aShape);
701   // TODO: try to create result if object is an external object
702   theObject = anObject;
703 }
704
705 //******************************************************
706 bool PartSet_Module::isMouseOverWindow()
707 {
708   return mySketchMgr->isMouseOverWindow();
709 }
710
711 //******************************************************
712 bool PartSet_Module::isSketchNeutralPointActivated() const
713 {
714   bool isNeutralPoint = true;
715   if (sketchReentranceMgr()->isInternalEditActive())
716     isNeutralPoint = false;
717   if (myIsOperationIsLaunched)
718     isNeutralPoint = false;
719
720   return isNeutralPoint;
721 }
722
723 //******************************************************
724 void PartSet_Module::closeDocument()
725 {
726   myActivePartIndex = QModelIndex();
727   SketcherPrs_PositionMgr::get()->clearAll();
728 }
729
730 //******************************************************
731 void PartSet_Module::clearViewer()
732 {
733   myCustomPrs->clearPrs();
734
735   XGUI_Workshop* aWorkshop = getWorkshop();
736   XGUI_Displayer* aDisplayer = aWorkshop->displayer();
737   aDisplayer->deactivateSelectionFilters(false);
738 }
739
740 //******************************************************
741 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
742 {
743   ModuleBase_OperationFeature* aFOperation =
744     dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
745   if (!aFOperation)
746     return;
747
748   ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
749   // we have to manually activate the sketch label in edit mode
750   if (PartSet_SketcherMgr::isSketchOperation(aFOperation) &&  (aFOperation->isEditOperation()))
751     aPanel->activateWidget(aPanel->modelWidgets().first());
752 }
753
754 //******************************************************
755 bool PartSet_Module::createWidgets(const FeaturePtr& theFeature, const QString& theXmlRepr,
756                                    QList<ModuleBase_ModelWidget*>& theWidgets) const
757 {
758   bool aProcessed = false;
759
760   XGUI_Workshop* aWorkshop = getWorkshop();
761   XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
762   if (mySketchMgr->activeSketch().get() && aPropertyPanel) {
763     ModuleBase_ISelection* aSelection = workshop()->selection();
764     // click on a point in sketch leads here with the point is highlighted, not yet selected
765     QList<ModuleBase_ViewerPrsPtr> aPreselection = aSelection->getHighlighted();
766     if (aPreselection.size() == 1) {
767       ModuleBase_ViewerPrsPtr aSelectedPrs = aPreselection[0];
768       ObjectPtr anObject = aSelectedPrs->object();
769
770       FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
771       GeomShapePtr aShape = aSelectedPrs->shape();
772       // click on the digit of dimension constrain comes here
773       // with an empty shape, so we need the check
774       if (aFeature == theFeature && aShape.get() && !aShape->isNull()) {
775         // if feature has only one result and shape of result is equal to selected shape
776         // this attribute is not processed. It is a case of Sketch Point.
777         if (aFeature->results().size() == 1) {
778           ResultPtr aResult = aFeature->results().front();
779           if (aResult.get() && aResult->shape()->isEqual(aShape))
780             return aProcessed;
781         }
782         const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
783         AttributePtr anAttribute = PartSet_Tools::findAttributeBy2dPoint(anObject, aTDShape,
784                                                                mySketchMgr->activeSketch());
785         if (anAttribute.get()) {
786           ModuleBase_WidgetFactory aFactory(theXmlRepr.toStdString(), workshop());
787
788           const std::string anAttributeId = anAttribute->id();
789           aFactory.createWidget(aPropertyPanel->contentWidget(), anAttributeId);
790
791           theWidgets = aFactory.getModelWidgets();
792           // it is possible that the point does not present in XML definition,
793           // in this case, we assume that it is not processed by this module
794           // e.g. "Intersection point" feature
795           aProcessed = !theWidgets.isEmpty();
796         }
797       }
798     }
799   }
800   return aProcessed;
801 }
802
803 //******************************************************
804 void PartSet_Module::onSelectionChanged()
805 {
806   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
807   if (!aOperation)
808     return;
809
810   bool isSketcherOp = false;
811   // An edit operation is enable only if the current opeation is the sketch operation
812   if (mySketchMgr->activeSketch()) {
813     if (PartSet_Tools::sketchPlane(mySketchMgr->activeSketch()))
814       isSketcherOp = PartSet_SketcherMgr::isSketchOperation(aOperation);
815   }
816   if (isSketcherOp) {
817     // Editing of constraints can be done on selection
818     ModuleBase_ISelection* aSelect = myWorkshop->selection();
819     QList<ModuleBase_ViewerPrsPtr> aSelected = aSelect->getSelected();
820     if (aSelected.size() == 1) {
821       ModuleBase_ViewerPrsPtr aPrs = aSelected.first();
822       ObjectPtr aObject = aPrs->object();
823       FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
824       if (aFeature) {
825         std::string aId = aFeature->getKind();
826         if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
827             (aId == SketchPlugin_ConstraintLength::ID()) ||
828             (aId == SketchPlugin_ConstraintDistance::ID()) ||
829             (aId == SketchPlugin_ConstraintAngle::ID())) {
830           editFeature(aFeature);
831         }
832       }
833     }
834   }
835 }
836
837 //******************************************************
838 void PartSet_Module::onKeyRelease(ModuleBase_IViewWindow* theWnd, QKeyEvent* theEvent)
839 {
840   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
841   XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
842   anOpMgr->onKeyReleased(theWnd->viewPort(), theEvent);
843 }
844
845 //******************************************************
846 ModuleBase_ModelWidget* PartSet_Module::createWidgetByType(const std::string& theType,
847                                                            QWidget* theParent,
848                                                            Config_WidgetAPI* theWidgetApi)
849 {
850   ModuleBase_IWorkshop* aWorkshop = workshop();
851   XGUI_Workshop* aXUIWorkshop = getWorkshop();
852   ModuleBase_ModelWidget* aWgt = NULL;
853   if (theType == "sketch-start-label") {
854     PartSet_WidgetSketchLabel* aLabelWgt = new PartSet_WidgetSketchLabel(theParent, aWorkshop,
855                                                                theWidgetApi, myHasConstraintShown);
856     connect(aLabelWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)),
857       mySketchMgr, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
858     connect(aLabelWgt, SIGNAL(showConstraintToggled(int, bool)),
859       mySketchMgr, SLOT(onShowConstraintsToggle(int, bool)));
860     connect(aLabelWgt, SIGNAL(showFreePoints(bool)), mySketchMgr, SLOT(onShowPoints(bool)));
861     connect(aLabelWgt, SIGNAL(autoConstraints(bool)),
862       sketchReentranceMgr(), SLOT(onAutoConstraints(bool)));
863     aLabelWgt->setShowPointsState(mySketchMgr->isShowFreePointsShown());
864     aWgt = aLabelWgt;
865   } else if (theType == "sketch-2dpoint_selector") {
866     PartSet_WidgetPoint2D* aPointWgt = new PartSet_WidgetPoint2D(theParent, aWorkshop,
867                                                                  theWidgetApi);
868     aPointWgt->setSketch(mySketchMgr->activeSketch());
869     connect(aPointWgt, SIGNAL(vertexSelected()), sketchReentranceMgr(), SLOT(onVertexSelected()));
870     aWgt = aPointWgt;
871   }else if (theType == "sketch-2dpoint_flyout_selector") {
872     PartSet_WidgetPoint2DFlyout* aPointWgt = new PartSet_WidgetPoint2DFlyout(theParent, aWorkshop,
873                                                                              theWidgetApi);
874     aPointWgt->setSketch(mySketchMgr->activeSketch());
875     connect(aPointWgt, SIGNAL(vertexSelected()), sketchReentranceMgr(), SLOT(onVertexSelected()));
876     aWgt = aPointWgt;
877   } else if (theType == "sketch_shape_selector") {
878     PartSet_WidgetShapeSelector* aShapeSelectorWgt =
879                           new PartSet_WidgetShapeSelector(theParent, aWorkshop, theWidgetApi);
880     aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
881     aWgt = aShapeSelectorWgt;
882   } else if (theType == "sketch_multi_selector") {
883     PartSet_WidgetMultiSelector* aShapeSelectorWgt =
884                           new PartSet_WidgetMultiSelector(theParent, aWorkshop, theWidgetApi);
885     aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
886     aWgt = aShapeSelectorWgt;
887   }
888   else if (theType == "sketch_feature_point_selector") {
889     PartSet_WidgetFeaturePointSelector* aPointSelectorWgt =
890             new PartSet_WidgetFeaturePointSelector(theParent, aWorkshop, theWidgetApi);
891     aPointSelectorWgt->setSketcher(mySketchMgr->activeSketch());
892     aWgt = aPointSelectorWgt;
893   }
894   else if (theType == WDG_DOUBLEVALUE_EDITOR) {
895     aWgt = new PartSet_WidgetEditor(theParent, aWorkshop, theWidgetApi);
896   } else if (theType == "export_file_selector") {
897     aWgt = new PartSet_WidgetFileSelector(theParent, aWorkshop, theWidgetApi);
898   } else if (theType == "sketch_launcher") {
899     aWgt = new PartSet_WidgetSketchCreator(theParent, this, theWidgetApi);
900   } else if (theType == "module_choice") {
901     aWgt = new ModuleBase_WidgetChoice(theParent, theWidgetApi);
902     connect(aWgt, SIGNAL(itemSelected(ModuleBase_ModelWidget*, int)),
903             this, SLOT(onChoiceChanged(ModuleBase_ModelWidget*, int)));
904   }
905   return aWgt;
906 }
907
908 //******************************************************
909 ModuleBase_ModelWidget* PartSet_Module::activeWidget() const
910 {
911   ModuleBase_ModelWidget* anActiveWidget = 0;
912
913   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
914   if (!aOperation)
915     return anActiveWidget;
916
917   ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
918   if (!aPanel)
919     return anActiveWidget;
920
921   return aPanel->activeWidget(true);
922 }
923
924 //******************************************************
925 bool PartSet_Module::deleteObjects()
926 {
927   bool isProcessed = false;
928
929   XGUI_Workshop* aWorkshop = getWorkshop();
930   XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
931
932   //SessionPtr aMgr = ModelAPI_Session::get();
933   // 1. check whether the delete should be processed in the module
934   ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
935   bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation),
936        isNestedOp = sketchMgr()->isNestedSketchOperation(anOperation);
937   if (isSketchOp || isNestedOp) {
938     isProcessed = true;
939     // 2. find selected presentations
940     // selected objects should be collected before the current operation abort because
941     // the abort leads to selection lost on constraint objects. It can be corrected after #386 issue
942     ModuleBase_ISelection* aSel = workshop()->selection();
943     QObjectPtrList aSelectedObj = aSel->selectedPresentations();
944     // if there are no selected objects in the viewer, that means that the selection in another
945     // place cased this method. It is necessary to return the false value to understande in above
946     // method that delete is not processed
947     if (aSelectedObj.count() == 0)
948       return false;
949
950     // avoid delete of the objects, which are not belong to the current sketch
951     // in order to do not delete results of other sketches
952     QObjectPtrList aSketchObjects;
953     QObjectPtrList::const_iterator anIt = aSelectedObj.begin(), aLast = aSelectedObj.end();
954     for ( ; anIt != aLast; anIt++) {
955       ObjectPtr anObject = *anIt;
956       if (mySketchMgr->isObjectOfSketch(anObject)) {
957         // sketch feature should be used in this list because workshop deletes features only
958         // results are skipped
959         FeaturePtr aSketchFeature = ModelAPI_Feature::feature(anObject);
960         aSketchObjects.append(aSketchFeature);
961       }
962     }
963     // if the selection contains only local selected presentations from other sketches,
964     // the Delete operation should not be done at all
965     if (aSketchObjects.size() == 0)
966       return true;
967
968     // 3. start operation
969     QString aDescription = aWorkshop->contextMenuMgr()->action("DELETE_CMD")->text();
970     ModuleBase_Operation* anOpAction = new ModuleBase_Operation(aDescription, this);
971
972     // the active nested sketch operation should be aborted unconditionally
973     // the Delete action should be additionally granted for the Sketch operation
974     // in order to do not abort/commit it
975     bool isCommitted;
976     if (!anOpMgr->canStartOperation(anOpAction->id(), isCommitted))
977       return true; // the objects are processed but can not be deleted
978
979     anOpMgr->startOperation(anOpAction);
980
981     // WORKAROUND, should be done to avoid viewer highlight update after deletetion of objects
982     // the problem is in AIS Dimensions recompute
983     // if a line and the dim are removed, line is the first
984     // it causes the AIS recompute, where the base line is null,
985     // the result is empty AIS in the viewer
986     XGUI_Tools::workshop(myWorkshop)->selector()->clearSelection();
987
988     // 4. delete features
989     // sketch feature should be skipped, only sub-features can be removed
990     // when sketch operation is active
991     aWorkshop->deleteFeatures(aSketchObjects);
992     // 5. stop operation
993     anOpMgr->commitOperation();
994   }
995   return isProcessed;
996 }
997
998 //******************************************************
999 void PartSet_Module::editFeature(FeaturePtr theFeature)
1000 {
1001   storeConstraintsState(theFeature->getKind());
1002   ModuleBase_IModule::editFeature(theFeature);
1003 }
1004
1005 //******************************************************
1006 bool PartSet_Module::canCommitOperation() const
1007 {
1008   return true;
1009 }
1010
1011 //******************************************************
1012 void PartSet_Module::launchOperation(const QString& theCmdId, const bool& isStartAfterCommitOnly)
1013 {
1014   myIsOperationIsLaunched = true;
1015   storeConstraintsState(theCmdId.toStdString());
1016   updateConstraintsState(theCmdId.toStdString());
1017
1018   ModuleBase_IModule::launchOperation(theCmdId, isStartAfterCommitOnly);
1019
1020   myIsOperationIsLaunched = false;
1021 }
1022
1023 //******************************************************
1024 void PartSet_Module::storeConstraintsState(const std::string& theFeatureKind)
1025 {
1026   if (myWorkshop->currentOperation() &&
1027       myWorkshop->currentOperation()->id().toStdString() == SketchPlugin_Sketch::ID()) {
1028     const QMap<PartSet_Tools::ConstraintVisibleState, bool>& aShownStates =
1029                                                   mySketchMgr->showConstraintStates();
1030     myHasConstraintShown = aShownStates;
1031   }
1032 }
1033
1034 //******************************************************
1035 void PartSet_Module::updateConstraintsState(const std::string& theFeatureKind)
1036 {
1037   if (PartSet_SketcherMgr::constraintsIdList().contains(theFeatureKind.c_str()) ||
1038       PartSet_SketcherMgr::replicationsIdList().contains(theFeatureKind.c_str())) {
1039     // Show constraints if a constraint was anOperation
1040     mySketchMgr->updateBySketchParameters(PartSet_Tools::Geometrical, true);
1041     mySketchMgr->updateBySketchParameters(PartSet_Tools::Dimensional, true);
1042     mySketchMgr->updateBySketchParameters(PartSet_Tools::Expressions,
1043                                           myHasConstraintShown[PartSet_Tools::Expressions]);
1044   }
1045 }
1046
1047 //******************************************************
1048 void PartSet_Module::onObjectDisplayed(ObjectPtr theObject, AISObjectPtr theAIS)
1049 {
1050   Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
1051   if (!anAIS.IsNull()) {
1052     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1053     if (aFeature.get()) {
1054       bool aToUseZLayer = false;
1055       if (PartSet_Tools::findRefsToMeFeature(aFeature,SketchPlugin_Projection::ID()))
1056         aToUseZLayer = true;
1057       else {
1058         CompositeFeaturePtr aParent = ModelAPI_Tools::compositeOwner(aFeature);
1059         aToUseZLayer = (aParent.get() && (aParent->getKind() == SketchPlugin_Sketch::ID()));
1060       }
1061       if (aToUseZLayer) {
1062         Handle(AIS_InteractiveContext) aCtx = anAIS->GetContext();
1063         if (aFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID())
1064           aCtx->SetZLayer(anAIS, Graphic3d_ZLayerId_Top);
1065         else
1066           aCtx->SetZLayer(anAIS, myVisualLayerId);
1067       }
1068     }
1069   }
1070 }
1071
1072 //******************************************************
1073 void PartSet_Module::onBeforeObjectErase(ObjectPtr theObject, AISObjectPtr theAIS)
1074 {
1075   // this is obsolete
1076   // it should be recomputed in order to disappear in the viewer if the corresponded object
1077   // is erased
1078   //if (myCustomPrs->isActive())
1079   //  myCustomPrs->redisplay(theObject, false);
1080 }
1081
1082 //******************************************************
1083 void PartSet_Module::onViewTransformed(int theTrsfType)
1084 {
1085   // Set length of arrows constant in pixel size
1086   // if the operation is panning or rotate or panglobal then do nothing
1087   if ((theTrsfType == 1) || (theTrsfType == 3) || (theTrsfType == 4))
1088     return;
1089   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
1090   Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
1091   if (aContext.IsNull())
1092     return;
1093
1094   //Handle(V3d_View) aView = aViewer->activeView();
1095
1096   XGUI_Workshop* aWorkshop = getWorkshop();
1097   XGUI_Displayer* aDisplayer = aWorkshop->displayer();
1098   Handle(V3d_Viewer) aV3dViewer = aContext->CurrentViewer();
1099   Handle(V3d_View) aView;
1100   double aScale = 0;
1101   for (aV3dViewer->InitDefinedViews();
1102        aV3dViewer->MoreDefinedViews();
1103        aV3dViewer->NextDefinedViews()) {
1104     Handle(V3d_View) aV = aV3dViewer->DefinedView();
1105     double aS = aV->Scale();
1106     if (aS > aScale) {
1107       aScale = aS;
1108       aView = aV;
1109     }
1110   }
1111   if (aView.IsNull())
1112     return;
1113
1114   bool isModified = false;
1115   ModuleBase_Operation* aCurrentOperation = myWorkshop->currentOperation();
1116   if (aCurrentOperation &&
1117     (PartSet_SketcherMgr::isSketchOperation(aCurrentOperation) ||
1118      sketchMgr()->isNestedSketchOperation(aCurrentOperation) ||
1119      (aCurrentOperation->id() == "Measurement")))
1120   {
1121     double aLen = aView->Convert(SketcherPrs_Tools::getConfigArrowSize());
1122
1123     double aPrevLen = SketcherPrs_Tools::getArrowSize();
1124     SketcherPrs_Tools::setArrowSize(aLen);
1125     const double aCurScale = aViewer->activeView()->Camera()->Scale();
1126     aViewer->SetScale(aViewer->activeView(), aCurScale);
1127 #ifdef OPTIMIZE_PRS
1128     QList<Handle(AIS_InteractiveObject)> aPrsList = aDisplayer->displayedPresentations();
1129     foreach(Handle(AIS_InteractiveObject) aAisObj, aPrsList) {
1130 #else
1131     QList<AISObjectPtr> aPrsList = aDisplayer->displayedPresentations();
1132     foreach(AISObjectPtr aAIS, aPrsList) {
1133       Handle(AIS_InteractiveObject) aAisObj = aAIS->impl<Handle(AIS_InteractiveObject)>();
1134 #endif
1135       Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(aAisObj);
1136       if (!aDim.IsNull()) {
1137         aDim->DimensionAspect()->ArrowAspect()->SetLength(aLen);
1138         aContext->Redisplay(aDim, false);
1139         isModified = true;
1140       }
1141     }
1142     if (isModified)
1143       aDisplayer->updateViewer();
1144   }
1145
1146 }
1147
1148 //******************************************************
1149 bool PartSet_Module::isCustomPrsActivated(const ModuleBase_CustomizeFlag& theFlag) const
1150 {
1151   return myCustomPrs->isActive(theFlag);
1152 }
1153
1154 //******************************************************
1155 void PartSet_Module::activateCustomPrs(const FeaturePtr& theFeature,
1156                                        const ModuleBase_CustomizeFlag& theFlag,
1157                                        const bool theUpdateViewer)
1158 {
1159   myCustomPrs->activate(theFeature, theFlag, theUpdateViewer);
1160 }
1161
1162 //******************************************************
1163 void PartSet_Module::deactivateCustomPrs(const ModuleBase_CustomizeFlag& theFlag,
1164                                          const bool theUpdateViewer)
1165 {
1166   myCustomPrs->deactivate(theFlag, theUpdateViewer);
1167 }
1168
1169 //******************************************************
1170 bool PartSet_Module::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
1171                                            std::shared_ptr<GeomAPI_ICustomPrs> theCustomPrs)
1172 {
1173   bool aCustomized = false;
1174
1175   XGUI_Workshop* aWorkshop = getWorkshop();
1176   XGUI_Displayer* aDisplayer = aWorkshop->displayer();
1177   ObjectPtr anObject = aDisplayer->getObject(thePrs);
1178   if (!anObject)
1179     return aCustomized;
1180
1181   if (!theResult.get()) {
1182     std::vector<int> aColor;
1183     XGUI_CustomPrs::getDefaultColor(anObject, true, aColor);
1184     if (!aColor.empty()) {
1185       aCustomized = thePrs->setColor(aColor[0], aColor[1], aColor[2]);
1186     }
1187   }
1188   // customize dimentional constrains
1189   sketchMgr()->customizePresentation(anObject);
1190
1191   return aCustomized;
1192 }
1193
1194 //******************************************************
1195 bool PartSet_Module::afterCustomisePresentation(std::shared_ptr<ModelAPI_Result> theResult,
1196                                                 AISObjectPtr thePrs,
1197                                                 GeomCustomPrsPtr theCustomPrs)
1198 {
1199   bool aCustomized = false;
1200
1201   XGUI_Workshop* aWorkshop = getWorkshop();
1202   XGUI_Displayer* aDisplayer = aWorkshop->displayer();
1203   ObjectPtr anObject = aDisplayer->getObject(thePrs);
1204   if (!anObject)
1205     return aCustomized;
1206
1207   std::vector<int> aColor;
1208   bool aUseCustomColor = true;
1209   if (aUseCustomColor)
1210     myOverconstraintListener->getCustomColor(anObject, aColor);
1211   // customize sketch symbol presentation
1212   Handle(AIS_InteractiveObject) anAISIO = thePrs->impl<Handle(AIS_InteractiveObject)>();
1213   if (!anAISIO.IsNull()) {
1214     if (!Handle(SketcherPrs_SymbolPrs)::DownCast(anAISIO).IsNull()) {
1215       Handle(SketcherPrs_SymbolPrs) aPrs = Handle(SketcherPrs_SymbolPrs)::DownCast(anAISIO);
1216       if (!aPrs.IsNull()) {
1217         aPrs->SetCustomColor(aColor);
1218         aCustomized = true;
1219       }
1220     } else if (!Handle(SketcherPrs_Coincident)::DownCast(anAISIO).IsNull()) {
1221       Handle(SketcherPrs_Coincident) aPrs = Handle(SketcherPrs_Coincident)::DownCast(anAISIO);
1222       if (!aPrs.IsNull()) {
1223         aPrs->SetCustomColor(aColor);
1224         aCustomized = true;
1225       }
1226     }
1227   }
1228   // customize sketch dimension constraint presentation
1229   if (!aCustomized) {
1230     if (!aColor.empty()) { // otherwise presentation has the default color
1231       aCustomized = thePrs->setColor(aColor[0], aColor[1], aColor[2]);
1232     }
1233   }
1234   return aCustomized;
1235 }
1236
1237 //******************************************************
1238 bool PartSet_Module::customizeObject(ObjectPtr theObject, const ModuleBase_CustomizeFlag& theFlag,
1239                                      const bool theUpdateViewer)
1240 {
1241   bool isRedisplayed = false;
1242   if (myCustomPrs->isActive(theFlag))
1243     isRedisplayed = myCustomPrs->redisplay(theObject, theFlag, theUpdateViewer);
1244
1245   return isRedisplayed;
1246 }
1247
1248 //******************************************************
1249 void PartSet_Module::customizeObjectBrowser(QWidget* theObjectBrowser)
1250 {
1251   XGUI_ObjectsBrowser* aOB = dynamic_cast<XGUI_ObjectsBrowser*>(theObjectBrowser);
1252   if (aOB) {
1253     QLabel* aLabel = aOB->activeDocLabel();
1254     aLabel->installEventFilter(myMenuMgr);
1255     connect(aLabel, SIGNAL(customContextMenuRequested(const QPoint&)),
1256           SLOT(onActiveDocPopup(const QPoint&)));
1257     aOB->treeView()->setExpandsOnDoubleClick(false);
1258     connect(aOB->treeView(), SIGNAL(doubleClicked(const QModelIndex&)),
1259       SLOT(onTreeViewDoubleClick(const QModelIndex&)));
1260
1261     Events_Loop* aLoop = Events_Loop::loop();
1262     aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
1263   }
1264 }
1265
1266 //******************************************************
1267 void PartSet_Module::onActiveDocPopup(const QPoint& thePnt)
1268 {
1269   SessionPtr aMgr = ModelAPI_Session::get();
1270   QAction* aActivatePartAction = myMenuMgr->action("ACTIVATE_PARTSET_CMD");
1271
1272   XGUI_Workshop* aWorkshop = getWorkshop();
1273   QLabel* aHeader = aWorkshop->objectBrowser()->activeDocLabel();
1274
1275   aActivatePartAction->setEnabled((aMgr->activeDocument() != aMgr->moduleDocument()));
1276
1277   QMenu aMenu;
1278   aMenu.addAction(aActivatePartAction);
1279   aMenu.exec(aHeader->mapToGlobal(thePnt));
1280 }
1281
1282 //******************************************************
1283 Handle(AIS_InteractiveObject) PartSet_Module::createPresentation(const ObjectPtr& theObject)
1284 {
1285   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1286   if (aResult.get())
1287     return mySketchMgr->createPresentation(aResult);
1288   else {
1289     FieldStepPtr aStep =
1290       std::dynamic_pointer_cast<ModelAPI_ResultField::ModelAPI_FieldStep>(theObject);
1291     if (aStep.get()) {
1292       return new PartSet_FieldStepPrs(aStep);
1293     }
1294   }
1295   return Handle(AIS_InteractiveObject)();
1296 }
1297
1298 //******************************************************
1299 ObjectPtr PartSet_Module::findPresentedObject(const AISObjectPtr& theAIS) const
1300 {
1301   ObjectPtr anObject;
1302   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
1303   if (aOperation) {
1304     /// If last line finished on vertex the lines creation sequence has to be break
1305     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
1306     if (aPanel) {
1307       ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
1308       // if there is an active widget, find the presented object in it
1309       if (!anActiveWidget)
1310         anActiveWidget = aPanel->preselectionWidget();
1311
1312       ModuleBase_WidgetValidated* aWidgetValidated = dynamic_cast<ModuleBase_WidgetValidated*>
1313                                                                              (anActiveWidget);
1314       if (aWidgetValidated)
1315         anObject = aWidgetValidated->findPresentedObject(theAIS);
1316     }
1317   }
1318   return anObject;
1319 }
1320
1321 //******************************************************
1322 bool PartSet_Module::canBeShaded(Handle(AIS_InteractiveObject) theAIS) const
1323 {
1324   bool aCanBeShaged = true;
1325
1326   Handle(PartSet_ResultSketchPrs) aPrs = Handle(PartSet_ResultSketchPrs)::DownCast(theAIS);
1327   if (!aPrs.IsNull())
1328     aCanBeShaged = false;
1329
1330   return aCanBeShaged;
1331 }
1332
1333 //******************************************************
1334 void PartSet_Module::addObjectBrowserMenu(QMenu* theMenu) const
1335 {
1336   QObjectPtrList aObjects = myWorkshop->selection()->selectedObjects();
1337   int aSelected = aObjects.size();
1338   SessionPtr aMgr = ModelAPI_Session::get();
1339   QAction* aActivatePartAction = myMenuMgr->action("ACTIVATE_PART_CMD");
1340
1341   bool hasResult = false;
1342   bool hasFeature = false;
1343   bool hasParameter = false;
1344   bool hasCompositeOwner = false;
1345   bool hasResultInHistory = false;
1346   bool hasFolder = false;
1347   ModuleBase_Tools::checkObjects(aObjects, hasResult, hasFeature, hasParameter,
1348                                   hasCompositeOwner, hasResultInHistory, hasFolder);
1349
1350   ModuleBase_Operation* aCurrentOp = myWorkshop->currentOperation();
1351   if (aSelected == 1) {
1352     ObjectPtr aObject = aObjects.first();
1353     if (aObject) {
1354       ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
1355       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
1356       bool isPart = aPart.get() ||
1357         (aFeature.get() && (aFeature->getKind() == PartSetPlugin_Part::ID()));
1358       if (isPart) {
1359         DocumentPtr aPartDoc;
1360         if (!aPart.get()) {
1361           aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aFeature->firstResult());
1362         }
1363         if (aPart.get()) // this may be null is Part feature is disabled
1364           aPartDoc = aPart->partDoc();
1365
1366         theMenu->addAction(aActivatePartAction);
1367         aActivatePartAction->setEnabled((aMgr->activeDocument() != aPartDoc));
1368
1369       } else if (aObject->document() == aMgr->activeDocument()) {
1370         if (hasParameter || hasFeature) {
1371           myMenuMgr->action("EDIT_CMD")->setEnabled(true);
1372           theMenu->addAction(myMenuMgr->action("EDIT_CMD"));
1373           if (aCurrentOp && aFeature.get()) {
1374             if (aCurrentOp->id().toStdString() == aFeature->getKind())
1375               myMenuMgr->action("EDIT_CMD")->setEnabled(false);
1376           }
1377         }
1378       }
1379     }
1380   } else {
1381     if (hasFeature) {
1382       myMenuMgr->action("EDIT_CMD")->setEnabled(aCurrentOp == 0);
1383       theMenu->addAction(myMenuMgr->action("EDIT_CMD"));
1384       theMenu->addSeparator();
1385     }
1386   }
1387   bool aNotDeactivate = (aCurrentOp == 0);
1388   if (!aNotDeactivate) {
1389     aActivatePartAction->setEnabled(false);
1390   }
1391 }
1392
1393 //******************************************************
1394 #define EXPAND_PARENT(OBJ) \
1395 QModelIndex aObjIndex = aDataModel->objectIndex(OBJ); \
1396 if (aObjIndex.isValid()) { \
1397   QModelIndex aParent = aObjIndex.parent(); \
1398   int aCount = aDataModel->rowCount(aParent); \
1399   if (aCount == 1) \
1400     aTreeView->setExpanded(aParent, true); \
1401 }
1402
1403 //******************************************************
1404 void PartSet_Module::processEvent(const std::shared_ptr<Events_Message>& theMessage)
1405 {
1406   if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
1407     // Do not change activation of parts if an operation active
1408     static QStringList aAllowActivationList;
1409     if (aAllowActivationList.isEmpty())
1410       aAllowActivationList <<
1411       QString(PartSetPlugin_Part::ID().c_str()) <<
1412       QString(PartSetPlugin_Duplicate::ID().c_str()) <<
1413       QString(PartSetPlugin_Remove::ID().c_str());
1414     if (myWorkshop->currentOperation() &&
1415       (!aAllowActivationList.contains(myWorkshop->currentOperation()->id())))
1416       return;
1417     XGUI_Workshop* aWorkshop = getWorkshop();
1418     XGUI_DataTree* aTreeView = aWorkshop->objectBrowser()->treeView();
1419     QLabel* aLabel = aWorkshop->objectBrowser()->activeDocLabel();
1420     QPalette aPalet = aLabel->palette();
1421
1422     SessionPtr aMgr = ModelAPI_Session::get();
1423     DocumentPtr aActiveDoc = aMgr->activeDocument();
1424
1425     // Clear active part index if there is no Part documents
1426     // It could be not null if document was closed and opened a new
1427     // without closeDocument call
1428     if (aMgr->allOpenedDocuments().size() <= 1)
1429       myActivePartIndex = QModelIndex();
1430
1431     XGUI_DataModel* aDataModel = aWorkshop->objectBrowser()->dataModel();
1432     QModelIndex aOldActive = myActivePartIndex;
1433     myActivePartIndex = aDataModel->documentRootIndex(aActiveDoc, 0);
1434     bool needUpdate = false;
1435     if (myActivePartIndex.isValid()) {
1436       needUpdate = aTreeView->isExpanded(myActivePartIndex);
1437       if (!needUpdate)
1438         aTreeView->setExpanded(myActivePartIndex, true);
1439     }
1440     if ((aOldActive != myActivePartIndex) && (aOldActive.isValid()))
1441       aTreeView->setExpanded(aOldActive, false);
1442
1443     aLabel->setPalette(aPalet);
1444     aWorkshop->updateCommandStatus();
1445
1446     // Update displayed objects in order to update active color
1447     XGUI_Displayer* aDisplayer = aWorkshop->displayer();
1448     QObjectPtrList aObjects = aDisplayer->displayedObjects();
1449     bool aHidden;
1450     bool aUpdateViewer = false;
1451     foreach(ObjectPtr aObj, aObjects) {
1452       aHidden = !aObj->data() || !aObj->data()->isValid() ||
1453         aObj->isDisabled() || (!aObj->isDisplayed());
1454       if (!aHidden) {
1455         aDisplayer->redisplay(aObj, false);
1456         aUpdateViewer = true;
1457       }
1458     }
1459     if (aUpdateViewer)
1460      aDisplayer->updateViewer();
1461     // Update tree items if they are expanded
1462     if (needUpdate) {
1463       aTreeView->viewport()->update(aTreeView->viewport()->rect());
1464     }
1465   } else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED)) {
1466     std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
1467         std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
1468     std::set<ObjectPtr> aObjects = aUpdMsg->objects();
1469
1470     ObjectPtr aConstrObj;
1471     ObjectPtr aResultObj;
1472     std::set<ObjectPtr>::const_iterator aIt;
1473     DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
1474     for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
1475       ObjectPtr aObject = (*aIt);
1476       if ((!aResultObj.get()) && (aObject->groupName() == ModelAPI_ResultBody::group())
1477           && (aObject->document() != aRootDoc))
1478         aResultObj = aObject;
1479       if ((!aConstrObj.get()) && (aObject->groupName() == ModelAPI_ResultConstruction::group())
1480           && (aObject->document() != aRootDoc))
1481         aConstrObj = aObject;
1482       if (aResultObj.get() && aConstrObj.get())
1483         break;
1484     }
1485
1486     if (aResultObj.get() || aConstrObj.get()) {
1487       XGUI_Workshop* aWorkshop = getWorkshop();
1488       XGUI_DataTree* aTreeView = aWorkshop->objectBrowser()->treeView();
1489       XGUI_DataModel* aDataModel = aWorkshop->objectBrowser()->dataModel();
1490
1491       if (aResultObj.get()) {
1492         EXPAND_PARENT(aResultObj)
1493       }
1494       if (aConstrObj.get()) {
1495         EXPAND_PARENT(aConstrObj)
1496       }
1497     }
1498   }
1499   else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY)) {
1500     CompositeFeaturePtr aSketch = mySketchMgr->activeSketch();
1501     if (aSketch.get()) {
1502       ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
1503       if (PartSet_SketcherMgr::isSketchOperation(anOperation))
1504         mySketchMgr->previewSketchPlane()->createSketchPlane(aSketch, myWorkshop);
1505     }
1506   }
1507 }
1508
1509 //******************************************************
1510 void PartSet_Module::onTreeViewDoubleClick(const QModelIndex& theIndex)
1511 {
1512   if (myWorkshop->currentOperation()) // Do not change activation of parts if an operation active
1513     return;
1514   SessionPtr aMgr = ModelAPI_Session::get();
1515   if (!theIndex.isValid()) {
1516     // It seems that this code is obsolete
1517     //aMgr->setActiveDocument(aMgr->moduleDocument());
1518     return;
1519   }
1520   if (theIndex.column() != 1) // Use only first column
1521     return;
1522
1523   XGUI_Workshop* aWorkshop = getWorkshop();
1524   XGUI_DataModel* aDataModel = aWorkshop->objectBrowser()->dataModel();
1525   // De not use non editable Indexes
1526   if ((aDataModel->flags(theIndex) & Qt::ItemIsSelectable) == 0)
1527     return;
1528   ObjectPtr aObj = aDataModel->object(theIndex);
1529
1530   ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
1531   if (!aPart.get()) { // Probably this is Feature
1532     FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
1533     if (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID())) {
1534       aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
1535     }
1536   }
1537   if (aPart.get()) { // if this is a part
1538     if (aPart->partDoc() == aMgr->activeDocument()) {
1539       myMenuMgr->activatePartSet();
1540     } else {
1541       myMenuMgr->activatePart(aPart);
1542     }
1543   }
1544 }
1545
1546 //******************************************************
1547 void PartSet_Module::onViewCreated(ModuleBase_IViewWindow*)
1548 {
1549   // z layer is created for all started operations in order to visualize operation AIS presentation
1550   // over the object
1551   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
1552   if (aContext.IsNull())
1553     return;
1554
1555   Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1556   if (myVisualLayerId == 0) {
1557     if (myVisualLayerId == 0)
1558       aViewer->AddZLayer(myVisualLayerId);
1559   } else {
1560     TColStd_SequenceOfInteger aZList;
1561     aViewer->GetAllZLayers(aZList);
1562     bool aFound = false;
1563     for (int i = 1; i <= aZList.Length(); i++) {
1564       if (aZList(i) == myVisualLayerId) {
1565         aFound = true;
1566         break;
1567       }
1568     }
1569     if (!aFound)
1570       aViewer->AddZLayer(myVisualLayerId);
1571   }
1572   // if there is an active operation with validated widget,
1573   // the filters of this widget should be activated in the created view
1574   myWorkshop->selectionActivate()->updateSelectionFilters();
1575   myWorkshop->selectionActivate()->updateSelectionModes();
1576 }
1577
1578 //******************************************************
1579 void PartSet_Module::widgetStateChanged(int thePreviousState)
1580 {
1581   mySketchMgr->widgetStateChanged(thePreviousState);
1582 }
1583
1584 //******************************************************
1585 bool PartSet_Module::processEnter(const std::string& thePreviousAttributeID)
1586 {
1587   return mySketchReentrantMgr->processEnter(thePreviousAttributeID);
1588 }
1589
1590 //******************************************************
1591 void PartSet_Module::beforeOperationStopped(ModuleBase_Operation* theOperation)
1592 {
1593   if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
1594     mySketchMgr->stopSketch(theOperation);
1595   }
1596 }
1597
1598 //******************************************************
1599 GeomShapePtr PartSet_Module::findShape(const AttributePtr& theAttribute)
1600 {
1601   GeomShapePtr aGeomShape;
1602
1603   ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
1604   if (anOperation && sketchMgr()->isNestedSketchOperation(anOperation)) {
1605     aGeomShape = PartSet_Tools::findShapeBy2DPoint(theAttribute, myWorkshop);
1606   }
1607   return aGeomShape;
1608 }
1609
1610 //******************************************************
1611 AttributePtr PartSet_Module::findAttribute(const ObjectPtr& theObject,
1612                                            const GeomShapePtr& theGeomShape)
1613 {
1614   AttributePtr anAttribute;
1615   GeomShapePtr aGeomShape = theGeomShape;
1616   if (!aGeomShape.get()) {
1617     // processing shape of result, e.g. sketch circle center is selected, this is a result
1618     // the geom shape is empty, the shape of result should be used
1619     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1620     if (aResult.get()) {
1621       aGeomShape = aResult->shape();
1622     }
1623   }
1624
1625   if (aGeomShape.get()) {
1626     TopoDS_Shape aTDSShape = aGeomShape->impl<TopoDS_Shape>();
1627     return PartSet_Tools::findAttributeBy2dPoint(theObject, aTDSShape,
1628                                                  mySketchMgr->activeSketch());
1629   }
1630   return anAttribute;
1631 }
1632
1633 //******************************************************
1634 std::shared_ptr<Events_Message> PartSet_Module::reentrantMessage()
1635 {
1636   return sketchReentranceMgr()->reentrantMessage();
1637 }
1638
1639 //******************************************************
1640 void PartSet_Module::setReentrantPreSelection(const std::shared_ptr<Events_Message>& theMessage)
1641 {
1642   sketchReentranceMgr()->setReentrantPreSelection(theMessage);
1643 }
1644
1645 //******************************************************
1646 void PartSet_Module::onChoiceChanged(ModuleBase_ModelWidget* theWidget,
1647                                      int theIndex)
1648 {
1649   ModuleBase_WidgetChoice* aChoiceWidget = dynamic_cast<ModuleBase_WidgetChoice*>(theWidget);
1650   if (!aChoiceWidget)
1651     return;
1652
1653   QString aChoiceTitle = aChoiceWidget->getPropertyPanelTitle(theIndex);
1654   if (!aChoiceTitle.isEmpty()) {
1655     ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
1656     if (!aOperation)
1657       return;
1658     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
1659     if (aPanel)
1660       aPanel->setWindowTitle(aChoiceTitle);
1661   }
1662 }
1663
1664 //******************************************************
1665 XGUI_Workshop* PartSet_Module::getWorkshop() const
1666 {
1667   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
1668   return aConnector->workshop();
1669 }
1670
1671 void PartSet_Module::setDefaultConstraintShown()
1672 {
1673   myHasConstraintShown[PartSet_Tools::Geometrical] = true;
1674   myHasConstraintShown[PartSet_Tools::Dimensional] = true;
1675   myHasConstraintShown[PartSet_Tools::Expressions] = false;
1676 }
1677
1678 //******************************************************
1679 ModuleBase_ITreeNode* PartSet_Module::rootNode() const
1680 {
1681   return myRoot;
1682 }
1683
1684 //******************************************************
1685 void PartSet_Module::disableCustomMode(ModuleBase_CustomizeFlag theMode) {
1686   myCustomPrs->disableCustomMode(theMode);
1687 }
1688
1689 //******************************************************
1690 void PartSet_Module::enableCustomModes() {
1691   myCustomPrs->enableCustomModes();
1692 }