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