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