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