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