Salome HOME
e49ff2ce18881bf3b16dc84a8ef8fb2ce8da6ae7
[modules/shaper.git] / src / PartSet / PartSet_Module.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 #include "PartSet_Module.h"
4 #include "PartSet_WidgetSketchLabel.h"
5 #include "PartSet_Validators.h"
6 #include "PartSet_Tools.h"
7 #include "PartSet_WidgetPoint2d.h"
8 #include "PartSet_WidgetPoint2dDistance.h"
9 #include "PartSet_WidgetPoint2DFlyout.h"
10 #include "PartSet_WidgetShapeSelector.h"
11 #include "PartSet_WidgetMultiSelector.h"
12 #include "PartSet_WidgetEditor.h"
13 #include "PartSet_WidgetFileSelector.h"
14 #include "PartSet_WidgetSketchCreator.h"
15 #include "PartSet_SketcherMgr.h"
16 #include "PartSet_SketcherReetntrantMgr.h"
17 #include "PartSet_MenuMgr.h"
18 #include "PartSet_CustomPrs.h"
19 #include "PartSet_IconFactory.h"
20 #include "PartSet_WidgetChoice.h"
21 #include "PartSet_OverconstraintListener.h"
22
23 #include "PartSet_Filters.h"
24 #include "PartSet_FilterInfinite.h"
25
26 #include <PartSetPlugin_Remove.h>
27 #include <PartSetPlugin_Part.h>
28 #include <PartSetPlugin_Duplicate.h>
29
30 #include <ModuleBase_Operation.h>
31 #include <ModuleBase_OperationAction.h>
32 #include <ModuleBase_IViewer.h>
33 #include <ModuleBase_IViewWindow.h>
34 #include <ModuleBase_IPropertyPanel.h>
35 #include <ModuleBase_WidgetEditor.h>
36 #include <ModuleBase_WidgetValidated.h>
37 #include <ModuleBase_FilterFactory.h>
38 #include <ModuleBase_Tools.h>
39 #include <ModuleBase_OperationFeature.h>
40
41 #include <ModelAPI_Object.h>
42 #include <ModelAPI_Events.h>
43 #include <ModelAPI_Validator.h>
44 #include <ModelAPI_Data.h>
45 #include <ModelAPI_Session.h>
46 #include <ModelAPI_ResultBody.h>
47 #include <ModelAPI_AttributeString.h>
48
49 #include <GeomDataAPI_Point2D.h>
50 #include <GeomDataAPI_Point.h>
51 #include <GeomDataAPI_Dir.h>
52
53 #include <XGUI_Displayer.h>
54 #include <XGUI_Workshop.h>
55 #include <XGUI_OperationMgr.h>
56 #include <XGUI_PropertyPanel.h>
57 #include <XGUI_ModuleConnector.h>
58 #include <XGUI_ContextMenuMgr.h>
59 #include <XGUI_Tools.h>
60 #include <XGUI_ObjectsBrowser.h>
61 #include <XGUI_SelectionMgr.h>
62 #include <XGUI_DataModel.h>
63 #include <XGUI_ErrorMgr.h>
64 #include <XGUI_CustomPrs.h>
65
66 #include <SketchPlugin_Feature.h>
67 #include <SketchPlugin_Sketch.h>
68 #include <SketchPlugin_ConstraintAngle.h>
69 #include <SketchPlugin_ConstraintLength.h>
70 #include <SketchPlugin_ConstraintDistance.h>
71 #include <SketchPlugin_ConstraintParallel.h>
72 #include <SketchPlugin_ConstraintPerpendicular.h>
73 #include <SketchPlugin_ConstraintRadius.h>
74
75 #include <SketcherPrs_SymbolPrs.h>
76 #include <SketcherPrs_Tools.h>
77
78 #include <Events_Loop.h>
79 #include <Config_PropManager.h>
80 #include <Config_Keywords.h>
81
82 #include <StdSelect_TypeOfFace.hxx>
83 #include <TopoDS_Vertex.hxx>
84 #include <TopoDS.hxx>
85 #include <TopoDS_Shape.hxx>
86 #include <BRep_Tool.hxx>
87 #include <AIS_Dimension.hxx>
88
89 #include <QObject>
90 #include <QMouseEvent>
91 #include <QString>
92 #include <QTimer>
93 #include <QApplication>
94 #include <QMessageBox>
95 #include <QMainWindow>
96 #include <QLineEdit>
97
98 #include <GeomAlgoAPI_FaceBuilder.h>
99 #include <GeomDataAPI_Dir.h>
100
101 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
102
103 #ifdef _DEBUG
104 #include <QDebug>
105 #endif
106
107 /*!Create and return new instance of XGUI_Module*/
108 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(ModuleBase_IWorkshop* theWshop)
109 {
110   return new PartSet_Module(theWshop);
111 }
112
113 PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
114 : ModuleBase_IModule(theWshop),
115   myVisualLayerId(0)
116 {
117   new PartSet_IconFactory();
118
119   mySketchMgr = new PartSet_SketcherMgr(this);
120   mySketchReentrantMgr = new PartSet_SketcherReetntrantMgr(theWshop);
121
122   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWshop);
123   XGUI_Workshop* aWorkshop = aConnector->workshop();
124
125   XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
126   connect(anOpMgr, SIGNAL(operationActivatedByPreselection()),
127           this, SLOT(onOperationActivatedByPreselection()));
128
129   ModuleBase_IViewer* aViewer = theWshop->viewer();
130   connect(aViewer, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)),
131           this, SLOT(onKeyRelease(ModuleBase_IViewWindow*, QKeyEvent*)));
132   connect(aViewer, SIGNAL(viewTransformed(int)),
133           SLOT(onViewTransformed(int)));
134   connect(aViewer, SIGNAL(viewCreated(ModuleBase_IViewWindow*)),
135           SLOT(onViewCreated(ModuleBase_IViewWindow*)));
136   myMenuMgr = new PartSet_MenuMgr(this);
137   myCustomPrs = new PartSet_CustomPrs(theWshop);
138
139   myOverconstraintListener = new PartSet_OverconstraintListener(theWshop);
140
141   Events_Loop* aLoop = Events_Loop::loop();
142   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED));
143
144   mySelectionFilters.Append(new PartSet_GlobalFilter(myWorkshop));
145   mySelectionFilters.Append(new PartSet_FilterInfinite(myWorkshop));
146
147   myHasConstraintShown[PartSet_Tools::Geometrical] = true;
148   myHasConstraintShown[PartSet_Tools::Dimensional] = true;
149
150   Config_PropManager::registerProp("Visualization", "operation_parameter_color",
151                           "Reference shape wireframe color in operation", Config_Prop::Color,
152                           PartSet_CustomPrs::OPERATION_PARAMETER_COLOR());
153   Config_PropManager::registerProp("Visualization", "operation_result_color",
154                           "Result shape wireframe color in operation", Config_Prop::Color,
155                           PartSet_CustomPrs::OPERATION_RESULT_COLOR());
156   Config_PropManager::registerProp("Visualization", "operation_highlight_color",
157                           "Multi selector item color in operation", Config_Prop::Color,
158                           PartSet_CustomPrs::OPERATION_HIGHLIGHT_COLOR());
159 }
160
161 PartSet_Module::~PartSet_Module()
162 {
163   SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
164   for (; aIt.More(); aIt.Next()) {
165     Handle(SelectMgr_Filter) aFilter = aIt.Value();
166     if (!aFilter.IsNull())
167       aFilter.Nullify();
168   }
169   delete myCustomPrs;
170   delete myOverconstraintListener;
171 }
172
173 void PartSet_Module::activateSelectionFilters()
174 {
175   SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
176   for (; aIt.More(); aIt.Next()) {
177     Handle(SelectMgr_Filter) aFilter = aIt.Value();
178     if (!aFilter.IsNull())
179       myWorkshop->viewer()->addSelectionFilter(aFilter);
180   }
181 }
182
183 void PartSet_Module::deactivateSelectionFilters()
184 {
185   SelectMgr_ListIteratorOfListOfFilter aIt(mySelectionFilters);
186   for (; aIt.More(); aIt.Next()) {
187     Handle(SelectMgr_Filter) aFilter = aIt.Value();
188     if (!aFilter.IsNull())
189       myWorkshop->viewer()->removeSelectionFilter(aFilter);
190   }
191 }
192
193 void PartSet_Module::registerValidators()
194 {
195   //Registering of validators
196   SessionPtr aMgr = ModelAPI_Session::get();
197   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
198   aFactory->registerValidator("PartSet_DistanceSelection", new PartSet_DistanceSelection);
199   aFactory->registerValidator("PartSet_LengthSelection", new PartSet_LengthSelection);
200   aFactory->registerValidator("PartSet_PerpendicularSelection", new PartSet_PerpendicularSelection);
201   aFactory->registerValidator("PartSet_ParallelSelection", new PartSet_ParallelSelection);
202   aFactory->registerValidator("PartSet_RadiusSelection", new PartSet_RadiusSelection);
203   aFactory->registerValidator("PartSet_RigidSelection", new PartSet_RigidSelection);
204   aFactory->registerValidator("PartSet_CoincidentSelection", new PartSet_CoincidentSelection);
205   aFactory->registerValidator("PartSet_HVDirSelection", new PartSet_HVDirSelection);
206   aFactory->registerValidator("PartSet_TangentSelection", new PartSet_TangentSelection);
207   aFactory->registerValidator("PartSet_FilletSelection", new PartSet_FilletSelection);
208   aFactory->registerValidator("PartSet_AngleSelection", new PartSet_AngleSelection);
209   aFactory->registerValidator("PartSet_EqualSelection", new PartSet_EqualSelection);
210   aFactory->registerValidator("PartSet_CollinearSelection", new PartSet_CollinearSelection);
211   aFactory->registerValidator("PartSet_MiddlePointSelection", new PartSet_MiddlePointSelection);
212   aFactory->registerValidator("PartSet_DifferentObjects", new PartSet_DifferentObjectsValidator);
213   aFactory->registerValidator("PartSet_CoincidentAttr", new PartSet_CoincidentAttr);
214   aFactory->registerValidator("PartSet_SketchEntityValidator", new PartSet_SketchEntityValidator);
215 }
216
217 void PartSet_Module::registerFilters()
218 {
219   //Registering of selection filters
220   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
221   ModuleBase_FilterFactory* aFactory = aConnector->selectionFilters();
222 }
223
224 void PartSet_Module::registerProperties()
225 {
226   Config_PropManager::registerProp("Sketch planes", "planes_size", "Size", Config_Prop::Double,
227                                    PLANE_SIZE);
228   Config_PropManager::registerProp("Sketch planes", "planes_thickness", "Thickness",
229                                    Config_Prop::Integer, SKETCH_WIDTH);
230   Config_PropManager::registerProp("Sketch planes", "rotate_to_plane", "Rotate to plane when selected",
231     Config_Prop::Boolean, "false");
232 }
233
234 void PartSet_Module::connectToPropertyPanel(ModuleBase_ModelWidget* theWidget, const bool isToConnect)
235 {
236   mySketchMgr->connectToPropertyPanel(theWidget, isToConnect);
237 }
238
239 void PartSet_Module::operationCommitted(ModuleBase_Operation* theOperation) 
240 {
241   if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
242     mySketchMgr->commitNestedSketch(theOperation);
243   }
244
245   /// Restart sketcher operations automatically
246   if (!mySketchReentrantMgr->operationCommitted(theOperation)) {
247
248     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
249     if (aFOperation && !aFOperation->isEditOperation()) {
250       // the selection is cleared after commit the create operation
251       // in order to do not use the same selected objects in the restarted operation
252       // for common behaviour, the selection is cleared even if the operation is not restarted
253       XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
254       XGUI_Workshop* aWorkshop = aConnector->workshop();
255       aWorkshop->selector()->clearSelection();
256     }
257   }
258 }
259
260 void PartSet_Module::operationAborted(ModuleBase_Operation* theOperation)
261 {
262   /// Restart sketcher operations automatically
263   mySketchReentrantMgr->operationAborted(theOperation);
264 }
265
266 void PartSet_Module::operationStarted(ModuleBase_Operation* theOperation)
267 {
268   /// Restart sketcher operations automatically
269   mySketchReentrantMgr->operationStarted(theOperation);
270
271   if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
272     mySketchMgr->startSketch(theOperation);
273   }
274   else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
275     mySketchMgr->startNestedSketch(theOperation);
276   }
277
278   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
279   if (aFOperation) {
280     myCustomPrs->activate(aFOperation->feature(), ModuleBase_IModule::CustomizeArguments, true);
281     myCustomPrs->activate(aFOperation->feature(), ModuleBase_IModule::CustomizeResults, true);
282   }
283 }
284
285 void PartSet_Module::operationResumed(ModuleBase_Operation* theOperation)
286 {
287   ModuleBase_IModule::operationResumed(theOperation);
288
289   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
290   if (aFOperation) {
291     myCustomPrs->activate(aFOperation->feature(), ModuleBase_IModule::CustomizeArguments, true);
292     myCustomPrs->activate(aFOperation->feature(), ModuleBase_IModule::CustomizeResults, true);
293   }
294 }
295
296 void PartSet_Module::operationStopped(ModuleBase_Operation* theOperation)
297 {
298   bool isModified = myCustomPrs->deactivate(ModuleBase_IModule::CustomizeArguments, false) ||
299                     myCustomPrs->deactivate(ModuleBase_IModule::CustomizeResults, false);
300
301   if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
302     mySketchMgr->stopNestedSketch(theOperation);
303   }
304
305   //VSV: Viewer is updated on feature update and redisplay
306   if (isModified) {
307     XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
308     XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
309     aDisplayer->updateViewer();
310   }
311
312   QMap<PartSet_Tools::ConstraintVisibleState, bool>::const_iterator anIt = myHasConstraintShown.begin(),
313                                                                     aLast = myHasConstraintShown.end();
314   for (; anIt != aLast; anIt++) {
315     myHasConstraintShown[anIt.key()];
316     mySketchMgr->onShowConstraintsToggle(anIt.value(), myHasConstraintShown[anIt.key()]);
317   }
318 }
319
320 ModuleBase_Operation* PartSet_Module::currentOperation() const
321 {
322   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
323   XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
324   return anOpMgr->currentOperation();
325 }
326
327 bool PartSet_Module::canUndo() const
328 {
329   bool aCanUndo = false;
330   SessionPtr aMgr = ModelAPI_Session::get();
331   if (aMgr->hasModuleDocument() && aMgr->canUndo()) {
332     aCanUndo = !aMgr->isOperation();
333     if (!aCanUndo) // check the enable state additionally by sketch manager
334       aCanUndo = aMgr->canUndo();
335   }
336   return aCanUndo;
337 }
338
339 bool PartSet_Module::canRedo() const
340 {
341   bool aCanRedo = false;
342   SessionPtr aMgr = ModelAPI_Session::get();
343   if (aMgr->hasModuleDocument() && aMgr->canRedo()) {
344     aCanRedo = !aMgr->isOperation();
345     if (!aCanRedo) // check the enable state additionally by sketch manager
346       aCanRedo = aMgr->canRedo();
347   }
348   return aCanRedo;
349 }
350
351 bool PartSet_Module::canApplyAction(const ObjectPtr& theObject, const QString& theActionId) const
352 {
353   bool aValid = true;
354   if (theActionId == "MOVE_CMD") {
355     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
356     if (aFeature) {
357       // part features can not be moved in the history.
358       if (aFeature->getKind() == PartSetPlugin_Part::ID())
359         aValid = false;
360     }
361   }
362   return aValid;
363 }
364
365 bool PartSet_Module::canEraseObject(const ObjectPtr& theObject) const
366 {
367   // the sketch manager put the restriction to the objects erase
368   return mySketchMgr->canEraseObject(theObject);
369 }
370
371 bool PartSet_Module::canDisplayObject(const ObjectPtr& theObject) const
372 {
373   // the sketch manager put the restriction to the objects display
374   return mySketchMgr->canDisplayObject(theObject);
375 }
376
377 void PartSet_Module::processHiddenObject(const std::list<ObjectPtr>& theObjects)
378 {
379   mySketchMgr->processHiddenObject(theObjects);
380 }
381
382 bool PartSet_Module::canActivateSelection(const ObjectPtr& theObject) const
383 {
384   bool aCanActivate = ModuleBase_IModule::canActivateSelection(theObject);
385
386   ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
387   bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation),
388        isNestedOp = PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
389   if (isSketchOp || isNestedOp) {
390     // in active sketch operation it is possible to activate operation object in selection
391     // in the edit operation, e.g. points of the line can be moved when the line is edited
392     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(anOperation);
393     aCanActivate = aCanActivate || (aFOperation && aFOperation->isEditOperation());
394   }
395   return aCanActivate;
396 }
397
398 bool PartSet_Module::addViewerMenu(QMenu* theMenu, const QMap<QString, QAction*>& theStdActions) const
399 {
400   return myMenuMgr->addViewerMenu(theMenu, theStdActions);
401 }
402
403 void PartSet_Module::updateViewerMenu(const QMap<QString, QAction*>& theStdActions)
404 {
405   myMenuMgr->updateViewerMenu(theStdActions);
406 }
407
408 QString PartSet_Module::getFeatureError(const FeaturePtr& theFeature)
409 {
410   QString anError = ModuleBase_IModule::getFeatureError(theFeature);
411   if (anError.isEmpty())
412     anError = sketchMgr()->getFeatureError(theFeature);
413
414   return anError;
415 }
416
417 void PartSet_Module::grantedOperationIds(ModuleBase_Operation* theOperation,
418                                          QStringList& theIds) const
419 {
420   myMenuMgr->grantedOperationIds(theOperation, theIds);
421
422   if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
423     XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
424     XGUI_Workshop* aWorkshop = aConnector->workshop();
425
426     theIds.append(aWorkshop->contextMenuMgr()->action("DELETE_CMD")->text());
427   }
428 }
429
430 void PartSet_Module::activeSelectionModes(QIntList& theModes)
431 {
432   theModes.clear();
433   if (mySketchMgr->activeSketch().get())
434     PartSet_SketcherMgr::sketchSelectionModes(theModes);
435 }
436
437 bool PartSet_Module::isMouseOverWindow()
438 {
439   return mySketchMgr->isMouseOverWindow();
440 }
441
442 void PartSet_Module::closeDocument()
443 {
444   clearViewer();
445 }
446
447 void PartSet_Module::clearViewer()
448 {
449   myCustomPrs->clearPrs();
450
451   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
452   XGUI_Workshop* aWorkshop = aConnector->workshop();
453   XGUI_Displayer* aDisplayer = aWorkshop->displayer();
454   aDisplayer->deactivateSelectionFilters();
455 }
456
457 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
458 {
459   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
460   if (!aFOperation)
461     return;
462
463   ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
464   // we have to manually activate the sketch label in edit mode
465   if (PartSet_SketcherMgr::isSketchOperation(aFOperation) &&  (aFOperation->isEditOperation()))
466     aPanel->activateWidget(aPanel->modelWidgets().first());
467 }
468
469
470 void PartSet_Module::onSelectionChanged()
471 {
472   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
473   if (!aOperation)
474     return;
475
476   bool isSketcherOp = false;
477   // An edit operation is enable only if the current opeation is the sketch operation
478   if (mySketchMgr->activeSketch()) {
479     if (PartSet_Tools::sketchPlane(mySketchMgr->activeSketch()))
480       isSketcherOp = PartSet_SketcherMgr::isSketchOperation(aOperation);
481   }
482   if (isSketcherOp) {
483     // Editing of constraints can be done on selection
484     ModuleBase_ISelection* aSelect = myWorkshop->selection();
485     QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
486     if (aSelected.size() == 1) {
487       ModuleBase_ViewerPrs aPrs = aSelected.first();
488       ObjectPtr aObject = aPrs.object();
489       FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
490       if (aFeature) {
491         std::string aId = aFeature->getKind();
492         if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
493             (aId == SketchPlugin_ConstraintLength::ID()) || 
494             (aId == SketchPlugin_ConstraintDistance::ID()) ||
495             (aId == SketchPlugin_ConstraintAngle::ID())) {
496           editFeature(aFeature);
497         }
498       }
499     }
500   } 
501 }
502
503 void PartSet_Module::onKeyRelease(ModuleBase_IViewWindow* theWnd, QKeyEvent* theEvent)
504 {
505   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
506   XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
507   anOpMgr->onKeyReleased(theWnd->viewPort(), theEvent);
508 }
509
510 void PartSet_Module::onOperationActivatedByPreselection()
511 {
512   if (!mySketchReentrantMgr->canBeCommittedByPreselection())
513     return;
514   mySketchMgr->operationActivatedByPreselection();
515 }
516
517 ModuleBase_ModelWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
518                                             Config_WidgetAPI* theWidgetApi, std::string theParentId)
519 {
520   ModuleBase_IWorkshop* aWorkshop = workshop();
521   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
522   XGUI_Workshop* aXUIWorkshop = aConnector->workshop();
523   ModuleBase_ModelWidget* aWgt = NULL;
524   if (theType == "sketch-start-label") {
525     PartSet_WidgetSketchLabel* aLabelWgt = new PartSet_WidgetSketchLabel(theParent, aWorkshop,
526       theWidgetApi, theParentId, mySketchMgr->showConstraintStates());
527     connect(aLabelWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)),
528       mySketchMgr, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
529     connect(aLabelWgt, SIGNAL(showConstraintToggled(bool, int)),
530       mySketchMgr, SLOT(onShowConstraintsToggle(bool, int)));
531     aWgt = aLabelWgt;
532   } else if (theType == "sketch-2dpoint_selector") {
533     PartSet_WidgetPoint2D* aPointWgt = new PartSet_WidgetPoint2D(theParent, aWorkshop,
534                                                                  theWidgetApi, theParentId);
535     aPointWgt->setSketch(mySketchMgr->activeSketch());
536     connect(aPointWgt, SIGNAL(vertexSelected()), sketchReentranceMgr(), SLOT(onVertexSelected()));
537     aWgt = aPointWgt;
538   }else if (theType == "sketch-2dpoint_flyout_selector") {
539     PartSet_WidgetPoint2DFlyout* aPointWgt = new PartSet_WidgetPoint2DFlyout(theParent, aWorkshop,
540                                                                  theWidgetApi, theParentId);
541     aPointWgt->setSketch(mySketchMgr->activeSketch());
542     connect(aPointWgt, SIGNAL(vertexSelected()), sketchReentranceMgr(), SLOT(onVertexSelected()));
543     aWgt = aPointWgt;
544   } else if (theType == "point2ddistance") {
545     PartSet_WidgetPoint2dDistance* aDistanceWgt = new PartSet_WidgetPoint2dDistance(theParent,
546                                                         aWorkshop, theWidgetApi, theParentId);
547     aDistanceWgt->setSketch(mySketchMgr->activeSketch());
548     aWgt = aDistanceWgt;
549   } else if (theType == "sketch_shape_selector") {
550     PartSet_WidgetShapeSelector* aShapeSelectorWgt =
551       new PartSet_WidgetShapeSelector(theParent, aWorkshop, theWidgetApi, theParentId);
552     aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
553     aWgt = aShapeSelectorWgt;
554   } else if (theType == "sketch_multi_selector") {
555     PartSet_WidgetMultiSelector* aShapeSelectorWgt =
556       new PartSet_WidgetMultiSelector(theParent, aWorkshop, theWidgetApi, theParentId);
557     aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
558     aWgt = aShapeSelectorWgt;
559   } else if (theType == WDG_DOUBLEVALUE_EDITOR) {
560     aWgt = new PartSet_WidgetEditor(theParent, aWorkshop, theWidgetApi, theParentId);
561   } else if (theType == "export_file_selector") {
562     aWgt = new PartSet_WidgetFileSelector(theParent, aWorkshop, theWidgetApi, theParentId);
563   } else if (theType == "sketch_launcher") {
564     aWgt = new PartSet_WidgetSketchCreator(theParent, this, theWidgetApi, theParentId);
565   } else if (theType == "module_choice") {
566     aWgt = new PartSet_WidgetChoice(theParent, theWidgetApi, theParentId);
567     connect(aWgt, SIGNAL(itemSelected(int)), SLOT(onBooleanOperationChange(int)));
568   }
569   return aWgt;
570 }
571
572 ModuleBase_ModelWidget* PartSet_Module::activeWidget() const
573 {
574   ModuleBase_ModelWidget* anActiveWidget = 0;
575
576   anActiveWidget = mySketchReentrantMgr->internalActiveWidget();
577   if (!anActiveWidget) {
578     ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
579     if (aOperation) {
580       ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
581       anActiveWidget = aPanel->activeWidget();
582     }
583   }
584   return anActiveWidget;
585 }
586
587 bool PartSet_Module::deleteObjects()
588 {
589   bool isProcessed = false;
590
591   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
592   XGUI_Workshop* aWorkshop = aConnector->workshop();
593   XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
594
595   //SessionPtr aMgr = ModelAPI_Session::get();
596   // 1. check whether the delete should be processed in the module
597   ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
598   bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation),
599        isNestedOp = PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
600   if (isSketchOp || isNestedOp) {
601     isProcessed = true;
602     // 2. find selected presentations
603     // selected objects should be collected before the current operation abort because
604     // the abort leads to selection lost on constraint objects. It can be corrected after #386 issue
605     ModuleBase_ISelection* aSel = workshop()->selection();
606     QObjectPtrList aSelectedObj = aSel->selectedPresentations();
607     // if there are no selected objects in the viewer, that means that the selection in another
608     // place cased this method. It is necessary to return the false value to understande in above
609     // method that delete is not processed
610     if (aSelectedObj.count() == 0)
611       return false;
612
613     // avoid delete of the objects, which are not belong to the current sketch
614     // in order to do not delete results of other sketches
615     QObjectPtrList aSketchObjects;
616     QObjectPtrList::const_iterator anIt = aSelectedObj.begin(), aLast = aSelectedObj.end();
617     for ( ; anIt != aLast; anIt++) {
618       ObjectPtr anObject = *anIt;
619       if (mySketchMgr->isObjectOfSketch(anObject)) {
620         // sketch feature should be used in this list because workshop deletes features only
621         // results are skipped
622         FeaturePtr aSketchFeature = ModelAPI_Feature::feature(anObject);
623         aSketchObjects.append(aSketchFeature);
624       }
625     }
626     // if the selection contains only local selected presentations from other sketches,
627     // the Delete operation should not be done at all
628     if (aSketchObjects.size() == 0)
629       return true;
630
631     // 3. start operation
632     QString aDescription = aWorkshop->contextMenuMgr()->action("DELETE_CMD")->text();
633     ModuleBase_OperationAction* anOpAction = new ModuleBase_OperationAction(aDescription, this);
634
635     // the active nested sketch operation should be aborted unconditionally
636     // the Delete action should be additionally granted for the Sketch operation
637     // in order to do not abort/commit it
638     if (!anOpMgr->canStartOperation(anOpAction->id()))
639       return true; // the objects are processed but can not be deleted
640
641     anOpMgr->startOperation(anOpAction);
642
643     // 4. delete features
644     // sketch feature should be skipped, only sub-features can be removed
645     // when sketch operation is active
646     aWorkshop->deleteFeatures(aSketchObjects);
647     // 5. stop operation
648     anOpMgr->commitOperation();
649   }
650   return isProcessed;
651 }
652
653 void PartSet_Module::onFeatureTriggered()
654 {
655   QAction* aCmd = dynamic_cast<QAction*>(sender());
656   if (aCmd->isCheckable() && aCmd->isChecked()) {
657     // 1. check whether the delete should be processed in the module
658     ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
659     bool isNestedOp = PartSet_SketcherMgr::isNestedCreateOperation(anOperation);
660     if (isNestedOp) {
661       // in case if in the viewer nothing is displayed, the create operation should not be
662       // comitted even if all values of the feature are initialized
663       if (!mySketchMgr->canDisplayCurrentCreatedFeature()) {
664         // the action information should be saved before the operation is aborted
665         // because this abort leads to update command status, which unchecks this action
666         anOperation->abort();
667         launchOperation(aCmd->data().toString());
668       }
669     }
670   }
671   ModuleBase_IModule::onFeatureTriggered();
672 }
673
674 void PartSet_Module::launchOperation(const QString& theCmdId)
675 {
676   if (myWorkshop->currentOperation() && 
677       myWorkshop->currentOperation()->id().toStdString() == SketchPlugin_Sketch::ID()) {
678       const QMap<PartSet_Tools::ConstraintVisibleState, bool>& aShownStates = mySketchMgr->showConstraintStates();
679       myHasConstraintShown = aShownStates;
680   }
681   if (PartSet_SketcherMgr::constraintsIdList().contains(theCmdId)) {
682     // Show constraints if a constraint was anOperation
683     mySketchMgr->onShowConstraintsToggle(true, PartSet_Tools::Geometrical);
684     mySketchMgr->onShowConstraintsToggle(true, PartSet_Tools::Dimensional);
685   }
686   ModuleBase_IModule::launchOperation(theCmdId);
687 }
688
689
690 void PartSet_Module::onObjectDisplayed(ObjectPtr theObject, AISObjectPtr theAIS) 
691 {
692   Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
693   if (!anAIS.IsNull()) {
694     Handle(AIS_InteractiveContext) aCtx = anAIS->GetContext();
695     Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(anAIS);
696     if (!aDim.IsNull()) {
697       aCtx->SetZLayer(aDim, myVisualLayerId);
698     } else {
699       Handle(SketcherPrs_SymbolPrs) aCons = Handle(SketcherPrs_SymbolPrs)::DownCast(anAIS);
700       if (!aCons.IsNull())
701         aCtx->SetZLayer(aCons, myVisualLayerId);
702     }
703   }
704 }
705
706 void PartSet_Module::onBeforeObjectErase(ObjectPtr theObject, AISObjectPtr theAIS)
707 {
708   // this is obsolete
709   // it should be recomputed in order to disappear in the viewer if the corresponded object
710   // is erased
711   //if (myCustomPrs->isActive())
712   //  myCustomPrs->redisplay(theObject, false);
713 }
714
715 void PartSet_Module::onViewTransformed(int theTrsfType)
716 {
717   // Set length of arrows constant in pixel size
718   // if the operation is panning or rotate or panglobal then do nothing
719   if ((theTrsfType == 1) || (theTrsfType == 3) || (theTrsfType == 4))
720     return;
721   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
722   Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
723   if (aContext.IsNull())
724     return;
725
726   //Handle(V3d_View) aView = aViewer->activeView();
727
728   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
729   XGUI_Workshop* aWorkshop = aConnector->workshop();
730   XGUI_Displayer* aDisplayer = aWorkshop->displayer();
731   Handle(V3d_Viewer) aV3dViewer = aContext->CurrentViewer();
732   Handle(V3d_View) aView;
733   double aScale = 0;
734   for (aV3dViewer->InitDefinedViews(); 
735        aV3dViewer->MoreDefinedViews(); 
736        aV3dViewer->NextDefinedViews()) {
737     Handle(V3d_View) aV = aV3dViewer->DefinedView();
738     double aS = aV->Scale();
739     if (aS > aScale) {
740       aScale = aS;
741       aView = aV;
742     }
743   }
744   if (aView.IsNull())
745     return;
746   double aLen = aView->Convert(20);
747
748   double aPrevLen = SketcherPrs_Tools::getArrowSize();
749   SketcherPrs_Tools::setArrowSize(aLen);
750   const double aPrevScale = aViewer->Scale(aViewer->activeView());
751   const double aCurScale = aViewer->activeView()->Camera()->Scale();
752   aViewer->SetScale(aViewer->activeView(), aCurScale);
753   SketcherPrs_Tools::setTextHeight (aCurScale / aPrevScale * SketcherPrs_Tools::getTextHeight());
754   bool isModified = false;
755   QList<AISObjectPtr> aPrsList = aDisplayer->displayedPresentations();
756   foreach (AISObjectPtr aAIS, aPrsList) {
757     Handle(AIS_InteractiveObject) aAisObj = aAIS->impl<Handle(AIS_InteractiveObject)>();
758
759     Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(aAisObj);
760     if (!aDim.IsNull()) {
761       aDim->DimensionAspect()->ArrowAspect()->SetLength(aLen);
762       aContext->Redisplay(aDim, false);
763       isModified = true;
764     }
765   }
766   if (isModified)
767     aDisplayer->updateViewer();
768 }
769
770 void PartSet_Module::activateCustomPrs(const FeaturePtr& theFeature, const ModuleBase_CustomizeFlag& theFlag,
771                                        const bool theUpdateViewer)
772 {
773   myCustomPrs->activate(theFeature, theFlag, theUpdateViewer);
774 }
775
776 void PartSet_Module::deactivateCustomPrs(const ModuleBase_CustomizeFlag& theFlag,
777                                          const bool theUpdateViewer)
778 {
779   myCustomPrs->deactivate(theFlag, theUpdateViewer);
780 }
781
782 bool PartSet_Module::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
783                                            std::shared_ptr<GeomAPI_ICustomPrs> theCustomPrs)
784 {
785   bool aCustomized = false;
786
787   if (theResult.get())
788     return aCustomized;
789
790   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
791   XGUI_Workshop* aWorkshop = aConnector->workshop();
792   XGUI_Displayer* aDisplayer = aWorkshop->displayer();
793   ObjectPtr anObject = aDisplayer->getObject(thePrs);
794   if (anObject.get()) {
795     bool isConflicting = myOverconstraintListener->isConflictingObject(anObject);
796     // customize sketcy symbol presentation
797     if (thePrs.get()) {
798       Handle(AIS_InteractiveObject) anAISIO = thePrs->impl<Handle(AIS_InteractiveObject)>();
799       if (!anAISIO.IsNull()) {
800         if (!Handle(SketcherPrs_SymbolPrs)::DownCast(anAISIO).IsNull()) {
801           Handle(SketcherPrs_SymbolPrs) aPrs = Handle(SketcherPrs_SymbolPrs)::DownCast(anAISIO);
802           if (!aPrs.IsNull()) {
803             std::vector<int> aColor;
804             myOverconstraintListener->getConflictingColor(aColor);
805             aPrs->SetConflictingConstraint(isConflicting, aColor);
806             aCustomized = true;
807           }
808         }
809       }
810     }
811     // customize sketch dimension constraint presentation
812     if (!aCustomized) {
813       std::vector<int> aColor;
814       if (isConflicting) {
815         myOverconstraintListener->getConflictingColor(aColor);
816       }
817       if (aColor.empty())
818         XGUI_CustomPrs::getDefaultColor(anObject, true, aColor);
819       if (!aColor.empty()) {
820         thePrs->setColor(aColor[0], aColor[1], aColor[2]);
821         aCustomized = true;
822       }
823     }
824   }
825
826   return aCustomized;
827 }
828
829 bool PartSet_Module::customizeObject(ObjectPtr theObject, const ModuleBase_CustomizeFlag& theFlag,
830                                      const bool theUpdateViewer)
831 {
832   bool isRedisplayed = false;
833   if (myCustomPrs->isActive(theFlag))
834     isRedisplayed = myCustomPrs->redisplay(theObject, theFlag, theUpdateViewer);
835
836   return isRedisplayed;
837 }
838
839 void PartSet_Module::customizeObjectBrowser(QWidget* theObjectBrowser)
840 {
841   XGUI_ObjectsBrowser* aOB = dynamic_cast<XGUI_ObjectsBrowser*>(theObjectBrowser);
842   if (aOB) {
843     QLabel* aLabel = aOB->activeDocLabel();
844     aLabel->installEventFilter(myMenuMgr);
845     connect(aLabel, SIGNAL(customContextMenuRequested(const QPoint&)), 
846           SLOT(onActiveDocPopup(const QPoint&)));
847     //QPalette aPalet = aLabel->palette();
848     //aPalet.setColor(QPalette::Text, QColor(0, 72, 140));
849     //aLabel->setPalette(aPalet);
850     aOB->treeView()->setExpandsOnDoubleClick(false);
851     connect(aOB->treeView(), SIGNAL(doubleClicked(const QModelIndex&)), 
852       SLOT(onTreeViewDoubleClick(const QModelIndex&)));
853   }
854 }
855
856 void PartSet_Module::onActiveDocPopup(const QPoint& thePnt)
857 {
858   SessionPtr aMgr = ModelAPI_Session::get();
859   QAction* aActivatePartAction = myMenuMgr->action("ACTIVATE_PARTSET_CMD");
860
861   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
862   XGUI_Workshop* aWorkshop = aConnector->workshop();
863   QLabel* aHeader = aWorkshop->objectBrowser()->activeDocLabel();
864
865   aActivatePartAction->setEnabled((aMgr->activeDocument() != aMgr->moduleDocument()));
866
867   QMenu aMenu;
868   aMenu.addAction(aActivatePartAction);
869   aMenu.exec(aHeader->mapToGlobal(thePnt));
870 }
871
872
873 ObjectPtr PartSet_Module::findPresentedObject(const AISObjectPtr& theAIS) const
874 {
875   ObjectPtr anObject;
876   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
877   if (aOperation) {
878     /// If last line finished on vertex the lines creation sequence has to be break
879     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
880     if (aPanel) {
881       ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
882       // if there is an active widget, find the presented object in it
883       if (!anActiveWidget)
884         anActiveWidget = aPanel->preselectionWidget();
885     
886       ModuleBase_WidgetValidated* aWidgetValidated = dynamic_cast<ModuleBase_WidgetValidated*>
887                                                                              (anActiveWidget);
888       if (aWidgetValidated)
889         anObject = aWidgetValidated->findPresentedObject(theAIS);
890     }
891   }
892   return anObject;
893 }
894
895 void PartSet_Module::addObjectBrowserMenu(QMenu* theMenu) const
896 {
897   QObjectPtrList aObjects = myWorkshop->selection()->selectedObjects();
898   int aSelected = aObjects.size();
899   SessionPtr aMgr = ModelAPI_Session::get();
900   QAction* aActivatePartAction = myMenuMgr->action("ACTIVATE_PART_CMD");
901
902   ModuleBase_Operation* aCurrentOp = myWorkshop->currentOperation();
903   if (aSelected == 1) {
904     bool hasResult = false;
905     bool hasFeature = false;
906     bool hasParameter = false;
907     bool hasSubFeature = false;
908     ModuleBase_Tools::checkObjects(aObjects, hasResult, hasFeature, hasParameter, hasSubFeature);
909
910     ObjectPtr aObject = aObjects.first();
911     if (aObject) {
912       ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
913       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
914       bool isPart = aPart.get() || 
915         (aFeature.get() && (aFeature->getKind() == PartSetPlugin_Part::ID()));
916       if (isPart) {
917         DocumentPtr aPartDoc;
918         if (!aPart.get()) {
919           aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aFeature->firstResult());
920         }
921         if (aPart.get()) // this may be null is Part feature is disabled
922           aPartDoc = aPart->partDoc();
923           
924         theMenu->addAction(aActivatePartAction);
925         aActivatePartAction->setEnabled((aMgr->activeDocument() != aPartDoc));
926
927       } else if (aObject->document() == aMgr->activeDocument()) {
928         if (hasParameter || hasFeature) {
929           myMenuMgr->action("EDIT_CMD")->setEnabled(true);
930           theMenu->addAction(myMenuMgr->action("EDIT_CMD"));
931           if (aCurrentOp && aFeature.get()) {
932             if (aCurrentOp->id().toStdString() == aFeature->getKind())
933               myMenuMgr->action("EDIT_CMD")->setEnabled(false);
934           }
935         }
936       }
937
938       ResultBodyPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aObject);
939       if( aResult.get() )
940         theMenu->addAction(myMenuMgr->action("SELECT_PARENT_CMD"));
941     }
942   }
943   bool aNotDeactivate = (aCurrentOp == 0);
944   if (!aNotDeactivate) {
945     aActivatePartAction->setEnabled(false);
946   }
947 }
948
949 void PartSet_Module::processEvent(const std::shared_ptr<Events_Message>& theMessage)
950 {
951   if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
952     // Do not change activation of parts if an operation active
953     static QStringList aAllowActivationList;
954     if (aAllowActivationList.isEmpty())
955       aAllowActivationList << 
956       QString(PartSetPlugin_Part::ID().c_str()) << 
957       QString(PartSetPlugin_Duplicate::ID().c_str()) <<
958       QString(PartSetPlugin_Remove::ID().c_str());
959     if (myWorkshop->currentOperation() && 
960       (!aAllowActivationList.contains(myWorkshop->currentOperation()->id())))
961       return;
962     XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
963     XGUI_Workshop* aWorkshop = aConnector->workshop();
964     XGUI_DataTree* aTreeView = aWorkshop->objectBrowser()->treeView();
965     QLabel* aLabel = aWorkshop->objectBrowser()->activeDocLabel();
966     QPalette aPalet = aLabel->palette();
967
968     SessionPtr aMgr = ModelAPI_Session::get();
969     DocumentPtr aActiveDoc = aMgr->activeDocument();
970     if (aActivePartIndex.isValid())
971       aTreeView->setExpanded(aActivePartIndex, false);
972     XGUI_DataModel* aDataModel = aWorkshop->objectBrowser()->dataModel();
973     aActivePartIndex = aDataModel->documentRootIndex(aActiveDoc);
974     if (aActivePartIndex.isValid())
975       aTreeView->setExpanded(aActivePartIndex, true);
976
977     aLabel->setPalette(aPalet);
978     aWorkshop->updateCommandStatus();
979
980     // Update displayed objects in order to update active color
981     XGUI_Displayer* aDisplayer = aWorkshop->displayer();
982     QObjectPtrList aObjects = aDisplayer->displayedObjects();
983     bool aHidden;
984     foreach(ObjectPtr aObj, aObjects) {
985       //TODO: replace by redisplay event.
986       aHidden = !aObj->data() || !aObj->data()->isValid() || 
987         aObj->isDisabled() || (!aObj->isDisplayed());
988       if (!aHidden)
989         aDisplayer->redisplay(aObj, false);
990     }
991     aDisplayer->updateViewer();
992   }
993 }
994
995 void PartSet_Module::onTreeViewDoubleClick(const QModelIndex& theIndex)
996 {
997   if (myWorkshop->currentOperation()) // Do not change activation of parts if an operation active
998     return;
999   SessionPtr aMgr = ModelAPI_Session::get();
1000   if (!theIndex.isValid()) {
1001     aMgr->setActiveDocument(aMgr->moduleDocument());
1002     return;
1003   }
1004   if (theIndex.column() != 0) // Use only first column
1005     return;
1006
1007   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
1008   XGUI_Workshop* aWorkshop = aConnector->workshop();
1009   XGUI_DataModel* aDataModel = aWorkshop->objectBrowser()->dataModel();
1010   // De not use non editable Indexes
1011   if ((aDataModel->flags(theIndex) & Qt::ItemIsSelectable) == 0)
1012     return;
1013   ObjectPtr aObj = aDataModel->object(theIndex);
1014
1015   ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObj);
1016   if (!aPart.get()) { // Probably this is Feature
1017     FeaturePtr aPartFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
1018     if (aPartFeature.get() && (aPartFeature->getKind() == PartSetPlugin_Part::ID())) {
1019       aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aPartFeature->firstResult());
1020     }
1021   }
1022   if (aPart.get()) { // if this is a part
1023     if (aPart->partDoc() == aMgr->activeDocument()) {
1024       myMenuMgr->activatePartSet();
1025     } else {
1026       aPart->activate();
1027     }
1028   }
1029 }
1030
1031
1032 void PartSet_Module::onViewCreated(ModuleBase_IViewWindow*)
1033 {
1034   // z layer is created for all started operations in order to visualize operation AIS presentation
1035   // over the object
1036   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
1037   if (aContext.IsNull())
1038     return;
1039
1040   Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
1041   if (myVisualLayerId == 0) {
1042     if (myVisualLayerId == 0)
1043       aViewer->AddZLayer(myVisualLayerId);
1044   } else {
1045     TColStd_SequenceOfInteger aZList;
1046     aViewer->GetAllZLayers(aZList);
1047     bool aFound = false;
1048     for (int i = 1; i <= aZList.Length(); i++) {
1049       if (aZList(i) == myVisualLayerId) {
1050         aFound = true;
1051         break;
1052       }
1053     }
1054     if (!aFound)
1055       aViewer->AddZLayer(myVisualLayerId);
1056   }
1057   // if there is an active operation with validated widget,
1058   // the filters of this widget should be activated in the created view
1059   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
1060   if (aOperation) {
1061     ModuleBase_ModelWidget* anActiveWidget = activeWidget();
1062     if (anActiveWidget) {
1063       ModuleBase_WidgetSelector* aWSelector = dynamic_cast<ModuleBase_WidgetSelector*>(anActiveWidget);
1064       if (aWSelector)
1065         aWSelector->activateSelectionAndFilters(true);
1066     }
1067   }
1068 }
1069
1070 //******************************************************
1071 void PartSet_Module::widgetStateChanged(int thePreviousState)
1072 {
1073   mySketchMgr->widgetStateChanged(thePreviousState);
1074 }
1075
1076 bool PartSet_Module::processEnter(const std::string& thePreviousAttributeID)
1077 {
1078   return mySketchReentrantMgr->processEnter(thePreviousAttributeID);
1079 }
1080
1081 //******************************************************
1082 void PartSet_Module::beforeOperationStarted(ModuleBase_Operation* theOperation)
1083 {
1084 }
1085
1086 //******************************************************
1087 void PartSet_Module::beforeOperationStopped(ModuleBase_Operation* theOperation)
1088 {
1089   if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
1090     mySketchMgr->stopSketch(theOperation);
1091   }
1092 }
1093
1094 //******************************************************
1095 GeomShapePtr PartSet_Module::findShape(const AttributePtr& theAttribute)
1096 {
1097   GeomShapePtr aGeomShape;
1098
1099   ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
1100   if (anOperation && PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
1101     aGeomShape = PartSet_Tools::findShapeBy2DPoint(theAttribute, myWorkshop);
1102   }
1103   return aGeomShape;
1104 }
1105
1106 //******************************************************
1107 AttributePtr PartSet_Module::findAttribute(const ObjectPtr& theObject,
1108                                            const GeomShapePtr& theGeomShape)
1109 {
1110   AttributePtr anAttribute;
1111   GeomShapePtr aGeomShape = theGeomShape;
1112   if (!aGeomShape.get()) {
1113     // processing shape of result, e.g. sketch circle center is selected, this is a result
1114     // the geom shape is empty, the shape of result should be used
1115     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1116     if (aResult.get()) {
1117       aGeomShape = aResult->shape();
1118     }
1119   }
1120
1121   if (aGeomShape.get()) {
1122     TopoDS_Shape aTDSShape = aGeomShape->impl<TopoDS_Shape>();
1123     return PartSet_Tools::findAttributeBy2dPoint(theObject, aTDSShape, 
1124                                                  mySketchMgr->activeSketch());
1125   }
1126   return anAttribute;
1127 }
1128
1129 //******************************************************
1130 void PartSet_Module::getColor(const ObjectPtr& theObject, std::vector<int>& theColor)
1131 {
1132   if (myOverconstraintListener->isConflictingObject(theObject)) {
1133     myOverconstraintListener->getConflictingColor(theColor);
1134   }
1135 }
1136
1137 //******************************************************
1138 void PartSet_Module::onBooleanOperationChange(int theOperation)
1139 {
1140   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
1141   if (!aOperation)
1142     return;
1143   ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
1144   switch (theOperation) {
1145   case 0:
1146     aPanel->setWindowTitle(tr("Cut"));
1147     break;
1148   case 1:
1149     aPanel->setWindowTitle(tr("Fuse"));
1150     break;
1151   case 2:
1152     aPanel->setWindowTitle(tr("Common"));
1153     break;
1154   }
1155 }