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