]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_Module.cpp
Salome HOME
9077aaaff459ec64d90a88cdafb4ab1d71e45282
[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_WidgetMultiSelector.h"
11 #include "PartSet_WidgetEditor.h"
12 #include "PartSet_SketcherMgr.h"
13 #include "PartSet_MenuMgr.h"
14 #include "PartSet_DocumentDataModel.h"
15
16 #include <PartSetPlugin_Remove.h>
17 #include <PartSetPlugin_Part.h>
18
19 #include <ModuleBase_Operation.h>
20 #include <ModuleBase_IViewer.h>
21 #include <ModuleBase_IViewWindow.h>
22 #include <ModuleBase_IPropertyPanel.h>
23 #include <ModuleBase_WidgetEditor.h>
24 #include <ModuleBase_FilterFactory.h>
25 #include <ModuleBase_Tools.h>
26
27 #include <GeomValidators_Edge.h>
28 #include <GeomValidators_EdgeOrVertex.h>
29 #include <GeomValidators_Face.h>
30 #include <GeomValidators_ConstructionComposite.h>
31
32
33 #include <ModelAPI_Object.h>
34 #include <ModelAPI_Events.h>
35 #include <ModelAPI_Validator.h>
36 #include <ModelAPI_Data.h>
37 #include <ModelAPI_Session.h>
38 #include <ModelAPI_ShapeValidator.h>
39
40 #include <GeomDataAPI_Point2D.h>
41 #include <GeomDataAPI_Point.h>
42 #include <GeomDataAPI_Dir.h>
43
44 #include <XGUI_Displayer.h>
45 #include <XGUI_Workshop.h>
46 #include <XGUI_OperationMgr.h>
47 #include <XGUI_PropertyPanel.h>
48 #include <XGUI_ModuleConnector.h>
49 #include <XGUI_ContextMenuMgr.h>
50 #include <XGUI_Tools.h>
51 #include <XGUI_ObjectsBrowser.h>
52
53 #include <SketchPlugin_Feature.h>
54 #include <SketchPlugin_Sketch.h>
55 #include <SketchPlugin_Line.h>
56 #include <SketchPlugin_Arc.h>
57 #include <SketchPlugin_Circle.h>
58 #include <SketchPlugin_Point.h>
59 #include <SketchPlugin_ConstraintLength.h>
60 #include <SketchPlugin_ConstraintDistance.h>
61 #include <SketchPlugin_ConstraintParallel.h>
62 #include <SketchPlugin_ConstraintPerpendicular.h>
63 #include <SketchPlugin_ConstraintRadius.h>
64
65 #include <SketcherPrs_SymbolPrs.h>
66 #include <SketcherPrs_Tools.h>
67
68 #include <Events_Loop.h>
69 #include <Config_PropManager.h>
70 #include <Config_Keywords.h>
71
72 #include <StdSelect_TypeOfFace.hxx>
73 #include <TopoDS_Vertex.hxx>
74 #include <TopoDS.hxx>
75 #include <TopoDS_Shape.hxx>
76 #include <BRep_Tool.hxx>
77 #include <AIS_Dimension.hxx>
78
79 #include <QObject>
80 #include <QMouseEvent>
81 #include <QString>
82 #include <QTimer>
83 #include <QApplication>
84 #include <QMessageBox>
85 #include <QMainWindow>
86 #include <QLineEdit>
87
88 #include <GeomAlgoAPI_FaceBuilder.h>
89 #include <GeomDataAPI_Dir.h>
90
91 #ifdef _DEBUG
92 #include <QDebug>
93 #endif
94
95
96
97 /*!Create and return new instance of XGUI_Module*/
98 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(ModuleBase_IWorkshop* theWshop)
99 {
100   return new PartSet_Module(theWshop);
101 }
102
103 PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
104   : ModuleBase_IModule(theWshop),
105   myRestartingMode(RM_None), myVisualLayerId(0)
106 {
107   mySketchMgr = new PartSet_SketcherMgr(this);
108   myDataModel = new PartSet_DocumentDataModel(this);
109
110   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWshop);
111   XGUI_Workshop* aWorkshop = aConnector->workshop();
112
113   XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
114   connect(anOpMgr, SIGNAL(keyEnterReleased()), this, SLOT(onEnterReleased()));
115   connect(anOpMgr, SIGNAL(operationActivatedByPreselection()),
116           this, SLOT(onOperationActivatedByPreselection()));
117
118   ModuleBase_IViewer* aViewer = theWshop->viewer();
119   connect(aViewer, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)),
120           this, SLOT(onKeyRelease(ModuleBase_IViewWindow*, QKeyEvent*)));
121   connect(aViewer, SIGNAL(viewTransformed(int)),
122           SLOT(onViewTransformed(int)));
123
124   myMenuMgr = new PartSet_MenuMgr(this);
125
126   Events_Loop* aLoop = Events_Loop::loop();
127   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_DOCUMENT_CHANGED));
128 }
129
130 PartSet_Module::~PartSet_Module()
131 {
132   if (!myDocumentShapeFilter.IsNull())
133     myDocumentShapeFilter.Nullify();
134 }
135
136 void PartSet_Module::registerValidators()
137 {
138   //Registering of validators
139   SessionPtr aMgr = ModelAPI_Session::get();
140   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
141   aFactory->registerValidator("PartSet_DistanceSelection", new PartSet_DistanceSelection);
142   aFactory->registerValidator("PartSet_LengthSelection", new PartSet_LengthSelection);
143   aFactory->registerValidator("PartSet_PerpendicularSelection", new PartSet_PerpendicularSelection);
144   aFactory->registerValidator("PartSet_ParallelSelection", new PartSet_ParallelSelection);
145   aFactory->registerValidator("PartSet_RadiusSelection", new PartSet_RadiusSelection);
146   aFactory->registerValidator("PartSet_RigidSelection", new PartSet_RigidSelection);
147   aFactory->registerValidator("PartSet_CoincidentSelection", new PartSet_CoincidentSelection);
148   aFactory->registerValidator("PartSet_HVDirSelection", new PartSet_HVDirSelection);
149   aFactory->registerValidator("PartSet_TangentSelection", new PartSet_TangentSelection);
150   aFactory->registerValidator("PartSet_FilletSelection", new PartSet_FilletSelection);
151
152   aFactory->registerValidator("PartSet_DifferentObjects", new PartSet_DifferentObjectsValidator);
153   aFactory->registerValidator("PartSet_DifferentShapes", new ModelAPI_ShapeValidator);
154
155   aFactory->registerValidator("GeomValidators_Edge", new GeomValidators_Edge);
156   aFactory->registerValidator("GeomValidators_EdgeOrVertex",
157                               new GeomValidators_EdgeOrVertex);
158   aFactory->registerValidator("GeomValidators_Face", new GeomValidators_Face);
159
160   aFactory->registerValidator("GeomValidators_ConstructionComposite",
161                               new GeomValidators_ConstructionComposite);
162
163   aFactory->registerValidator("PartSet_SketchEntityValidator",
164                               new PartSet_SketchEntityValidator);
165
166   aFactory->registerValidator("PartSet_SameTypeAttr",
167                               new PartSet_SameTypeAttrValidator);
168 }
169
170 void PartSet_Module::registerFilters()
171 {
172   //Registering of selection filters
173   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
174   ModuleBase_FilterFactory* aFactory = aConnector->selectionFilters();
175 }
176
177 void PartSet_Module::registerProperties()
178 {
179   Config_PropManager::registerProp("Sketch planes", "planes_size", "Size", Config_Prop::Double,
180                                    PLANE_SIZE);
181   Config_PropManager::registerProp("Sketch planes", "planes_thickness", "Thickness",
182                                    Config_Prop::Integer, SKETCH_WIDTH);
183 }
184
185 void PartSet_Module::operationCommitted(ModuleBase_Operation* theOperation) 
186 {
187   if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
188     mySketchMgr->commitNestedSketch(theOperation);
189   }
190
191   if (theOperation->isEditOperation())
192     return;
193   // the selection is cleared after commit the create operation
194   // in order to do not use the same selected objects in the restarted operation
195   // for common behaviour, the selection is cleared even if the operation is not restarted
196   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
197   if (!aContext.IsNull())
198     aContext->ClearSelected();
199
200   /// Restart sketcher operations automatically
201   FeaturePtr aFeature = theOperation->feature();
202   std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
203             std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
204   if (aSPFeature && (myRestartingMode == RM_LastFeatureUsed ||
205                      myRestartingMode == RM_EmptyFeatureUsed)) {
206     myLastOperationId = theOperation->id();
207     myLastFeature = myRestartingMode == RM_LastFeatureUsed ? theOperation->feature() : FeaturePtr();
208     
209     launchOperation(myLastOperationId);
210   }
211   breakOperationSequence();
212 }
213
214 void PartSet_Module::breakOperationSequence()
215 {
216   myLastOperationId = "";
217   myLastFeature = FeaturePtr();
218   myRestartingMode = RM_None;
219 }
220
221 void PartSet_Module::operationAborted(ModuleBase_Operation* theOperation)
222 {
223   breakOperationSequence();
224 }
225
226 void PartSet_Module::sendOperation(ModuleBase_Operation* theOperation)
227 {
228   // Install the document filter before any other filter
229   if (myDocumentShapeFilter.IsNull())
230     myDocumentShapeFilter = new PartSet_GlobalFilter(myWorkshop);
231   myWorkshop->viewer()->addSelectionFilter(myDocumentShapeFilter);
232
233   ModuleBase_IModule::sendOperation(theOperation);
234 }
235
236 void PartSet_Module::operationStarted(ModuleBase_Operation* theOperation)
237 {
238   if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
239     Handle(V3d_Viewer) aViewer = myWorkshop->viewer()->AISContext()->CurrentViewer();
240     aViewer->AddZLayer(myVisualLayerId);
241     mySketchMgr->startSketch(theOperation);
242   }
243   else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
244     mySketchMgr->startNestedSketch(theOperation);
245   }
246 }
247
248 void PartSet_Module::operationStopped(ModuleBase_Operation* theOperation)
249 {
250   if (PartSet_SketcherMgr::isSketchOperation(theOperation)) {
251     mySketchMgr->stopSketch(theOperation);
252     Handle(V3d_Viewer) aViewer = myWorkshop->viewer()->AISContext()->CurrentViewer();
253     aViewer->RemoveZLayer(myVisualLayerId);
254     myVisualLayerId = 0;
255   }
256   else if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation)) {
257     mySketchMgr->stopNestedSketch(theOperation);
258   }
259   myWorkshop->viewer()->removeSelectionFilter(myDocumentShapeFilter);
260 }
261
262 ModuleBase_Operation* PartSet_Module::currentOperation() const
263 {
264   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
265   XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
266   return anOpMgr->currentOperation();
267 }
268
269 bool PartSet_Module::canUndo() const
270 {
271   bool aCanUndo = false;
272   SessionPtr aMgr = ModelAPI_Session::get();
273   if (aMgr->hasModuleDocument() && aMgr->canUndo()) {
274     aCanUndo = !aMgr->isOperation();
275     if (!aCanUndo) // check the enable state additionally by sketch manager
276       aCanUndo = aMgr->canUndo();
277   }
278   return aCanUndo;
279 }
280
281 bool PartSet_Module::canRedo() const
282 {
283   bool aCanRedo = false;
284   SessionPtr aMgr = ModelAPI_Session::get();
285   if (aMgr->hasModuleDocument() && aMgr->canRedo()) {
286     aCanRedo = !aMgr->isOperation();
287     if (!aCanRedo) // check the enable state additionally by sketch manager
288       aCanRedo = aMgr->canRedo();
289   }
290   return aCanRedo;
291 }
292
293 bool PartSet_Module::canDisplayObject(const ObjectPtr& theObject) const
294 {
295   // the sketch manager put the restriction to the objects display
296   return mySketchMgr->canDisplayObject(theObject);
297 }
298
299
300 bool PartSet_Module::addViewerMenu(QMenu* theMenu, const QMap<QString, QAction*>& theStdActions) const
301 {
302   return myMenuMgr->addViewerMenu(theMenu, theStdActions);
303 }
304
305 bool PartSet_Module::isMouseOverWindow()
306 {
307   return mySketchMgr->isMouseOverWindow();
308 }
309
310 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
311 {
312   ModuleBase_IPropertyPanel* aPanel = theOperation->propertyPanel();
313   if (PartSet_SketcherMgr::isSketchOperation(theOperation) &&  (theOperation->isEditOperation())) {
314     // we have to manually activate the sketch label in edit mode
315       aPanel->activateWidget(aPanel->modelWidgets().first());
316       return;
317   }
318
319   // Restart last operation type 
320   if ((theOperation->id() == myLastOperationId) && myLastFeature) {
321     ModuleBase_ModelWidget* aWgt = aPanel->activeWidget();
322     if (theOperation->id().toStdString() == SketchPlugin_Line::ID()) {
323       // Initialise new line with first point equal to end of previous
324       PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
325       if (aPnt2dWgt) {
326         std::shared_ptr<ModelAPI_Data> aData = myLastFeature->data();
327         std::shared_ptr<GeomDataAPI_Point2D> aPoint = 
328           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
329         if (aPoint) {
330           aPnt2dWgt->setPoint(aPoint->x(), aPoint->y());
331           PartSet_Tools::setConstraints(mySketchMgr->activeSketch(), theOperation->feature(), 
332             aWgt->attributeID(), aPoint->x(), aPoint->y());
333           aPanel->activateNextWidget(aPnt2dWgt);
334         }
335       }
336     }
337   } else {
338     // Start editing constraint
339     if (theOperation->isEditOperation()) {
340       // TODO: #391 - to be removed
341       std::string aId = theOperation->id().toStdString();
342       if (PartSet_SketcherMgr::isNestedSketchOperation(theOperation) &&
343           PartSet_SketcherMgr::isDistanceOperation(theOperation)) {
344         // Find and activate widget for management of point for dimension line position
345         QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
346         foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
347           PartSet_WidgetPoint2D* aPntWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
348           if (aPntWgt) {
349             aPanel->activateWidget(aPntWgt);
350             return;
351           }
352         }
353       }
354     }
355   }
356 }
357
358
359 void PartSet_Module::onSelectionChanged()
360 {
361   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
362   if (!aOperation)
363     return;
364
365   bool isSketcherOp = false;
366   // An edit operation is enable only if the current opeation is the sketch operation
367   if (mySketchMgr->activeSketch()) {
368     if (PartSet_Tools::sketchPlane(mySketchMgr->activeSketch()))
369       isSketcherOp = PartSet_SketcherMgr::isSketchOperation(aOperation);
370   }
371   if (isSketcherOp) {
372     // Editing of constraints can be done on selection
373     ModuleBase_ISelection* aSelect = myWorkshop->selection();
374     QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
375     if (aSelected.size() == 1) {
376       ModuleBase_ViewerPrs aPrs = aSelected.first();
377       ObjectPtr aObject = aPrs.object();
378       FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
379       if (aFeature) {
380         std::string aId = aFeature->getKind();
381         if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
382             (aId == SketchPlugin_ConstraintLength::ID()) || 
383             (aId == SketchPlugin_ConstraintDistance::ID())) {
384           editFeature(aFeature);
385         }
386       }
387     }
388   } 
389 }
390
391 void PartSet_Module::onKeyRelease(ModuleBase_IViewWindow* theWnd, QKeyEvent* theEvent)
392 {
393   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
394   XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
395   anOpMgr->onKeyReleased(theEvent);
396 }
397
398 void PartSet_Module::onEnterReleased()
399 {
400   myRestartingMode = RM_EmptyFeatureUsed;
401 }
402
403 void PartSet_Module::onOperationActivatedByPreselection()
404 {
405   ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
406   if(anOperation && PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
407     // Set final definitions if they are necessary
408     //propertyPanelDefined(aOperation);
409
410     /// Commit sketcher operations automatically
411     anOperation->commit();
412   }
413 }
414
415 void PartSet_Module::onNoMoreWidgets()
416 {
417   ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
418   if (anOperation) {
419     if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
420       if (myRestartingMode != RM_Forbided)
421         myRestartingMode = RM_LastFeatureUsed;
422       anOperation->commit();
423     }
424   }
425 }
426
427 void PartSet_Module::onVertexSelected()
428 {
429   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
430   if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
431     /// If last line finished on vertex the lines creation sequence has to be break
432     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
433     ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
434     const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
435     QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
436     bool aFoundWidget = false;
437     bool aFoundObligatory = false;
438     for (; anIt != aLast && !aFoundObligatory; anIt++) {
439       if (!aFoundWidget)
440         aFoundWidget = *anIt == anActiveWidget;
441       else
442         aFoundObligatory = (*anIt)->isObligatory();
443     }
444     if (!aFoundObligatory)
445       myRestartingMode = RM_Forbided;
446   }
447 }
448
449 ModuleBase_ModelWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
450                                             Config_WidgetAPI* theWidgetApi, std::string theParentId)
451 {
452   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
453   XGUI_Workshop* aWorkshop = aConnector->workshop();
454   ModuleBase_ModelWidget* aWgt = NULL;
455   if (theType == "sketch-start-label") {
456     PartSet_WidgetSketchLabel* aLabelWgt = new PartSet_WidgetSketchLabel(theParent, 
457       theWidgetApi, theParentId, mySketchMgr->isConstraintsShown());
458     aLabelWgt->setWorkshop(aWorkshop);
459     connect(aLabelWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)),
460       mySketchMgr, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
461     connect(aLabelWgt, SIGNAL(showConstraintToggled(bool)),
462       mySketchMgr, SLOT(onShowConstraintsToggle(bool)));
463     aWgt = aLabelWgt;
464   } else if (theType == "sketch-2dpoint_selector") {
465     PartSet_WidgetPoint2D* aPointWgt = new PartSet_WidgetPoint2D(theParent, theWidgetApi, theParentId);
466     aPointWgt->setWorkshop(aWorkshop);
467     aPointWgt->setSketch(mySketchMgr->activeSketch());
468     connect(aPointWgt, SIGNAL(vertexSelected()), this, SLOT(onVertexSelected()));
469     aWgt = aPointWgt;
470   } if (theType == "point2ddistance") {
471     PartSet_WidgetPoint2dDistance* aDistanceWgt = new PartSet_WidgetPoint2dDistance(theParent, theWidgetApi, theParentId);
472     aDistanceWgt->setWorkshop(aWorkshop);
473     aDistanceWgt->setSketch(mySketchMgr->activeSketch());
474     aWgt = aDistanceWgt;
475   } if (theType == "sketch_shape_selector") {
476     PartSet_WidgetShapeSelector* aShapeSelectorWgt =
477       new PartSet_WidgetShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
478     aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
479     aWgt = aShapeSelectorWgt;
480   } if (theType == "sketch_multi_selector") {
481     PartSet_WidgetMultiSelector* aShapeSelectorWgt =
482       new PartSet_WidgetMultiSelector(theParent, workshop(), theWidgetApi, theParentId);
483     aShapeSelectorWgt->setSketcher(mySketchMgr->activeSketch());
484     aWgt = aShapeSelectorWgt;
485   }
486   if (theType == WDG_DOUBLEVALUE_EDITOR) {
487     aWgt = new PartSet_WidgetEditor(theParent, workshop(), theWidgetApi, theParentId);
488   } 
489   return aWgt;
490 }
491
492
493 bool PartSet_Module::deleteObjects()
494 {
495   SessionPtr aMgr = ModelAPI_Session::get();
496   // 1. check whether the delete should be processed in the module
497   ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
498   bool isSketchOp = PartSet_SketcherMgr::isSketchOperation(anOperation),
499        isNestedOp = PartSet_SketcherMgr::isNestedSketchOperation(anOperation);
500   if (isSketchOp && isNestedOp) {
501     // 2. find selected presentations
502     // selected objects should be collected before the current operation abort because
503     // the abort leads to selection lost on constraint objects. It can be corrected after #386 issue
504     XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
505     XGUI_Workshop* aWorkshop = aConnector->workshop();
506     ModuleBase_ISelection* aSel = workshop()->selection();
507     QObjectPtrList aSelectedObj = aSel->selectedPresentations();
508     // if there are no selected objects in the viewer, that means that the selection in another
509     // place cased this method. It is necessary to return the false value to understande in above
510     // method that delete is not processed
511     if (aSelectedObj.count() == 0)
512       return false;
513
514     // avoid delete of the objects, which are not belong to the current sketch
515     // in order to do not delete results of other sketches
516     QObjectPtrList aSketchObjects;
517     QObjectPtrList::const_iterator anIt = aSelectedObj.begin(), aLast = aSelectedObj.end();
518     for ( ; anIt != aLast; anIt++) {
519       ObjectPtr anObject = *anIt;
520       if (mySketchMgr->isObjectOfSketch(anObject))
521         aSketchObjects.append(anObject);
522     }
523     // if the selection contains only local selected presentations from other sketches,
524     // the Delete operation should not be done at all
525     if (aSketchObjects.size() == 0)
526       return true;
527
528     // the active nested sketch operation should be aborted unconditionally
529     if (isNestedOp)
530       anOperation->abort();
531
532     // 3. start operation
533     QString aDescription = aWorkshop->contextMenuMgr()->action("DELETE_CMD")->text();
534     aMgr->startOperation(aDescription.toStdString());
535
536     // 4. delete features
537     // sketch feature should be skipped, only sub-features can be removed
538     // when sketch operation is active
539     std::set<FeaturePtr> anIgnoredFeatures;
540     anIgnoredFeatures.insert(mySketchMgr->activeSketch());
541     aWorkshop->deleteFeatures(aSketchObjects, anIgnoredFeatures);
542   
543     // 5. stop operation
544     aWorkshop->displayer()->updateViewer();
545     aMgr->finishOperation();
546   } else {
547     // Delete part with help of PartSet plugin
548     // TODO: the deleted objects has to be processed by multiselection
549     QObjectPtrList aObjects = myWorkshop->selection()->selectedObjects();
550     if (aObjects.size() == 1) {
551       ObjectPtr aObj = aObjects.first();
552       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
553       if (aFeature.get() && (aFeature->getKind() == PartSetPlugin_Part::ID())) {
554         std::shared_ptr<ModelAPI_Document> aDoc = aMgr->activeDocument();
555         aMgr->startOperation(PartSetPlugin_Remove::ID());
556         FeaturePtr aFeature = aDoc->addFeature(PartSetPlugin_Remove::ID());
557         aFeature->execute();
558         aMgr->finishOperation();
559       }
560     }
561   }
562   return true;
563 }
564
565
566 void PartSet_Module::onObjectDisplayed(ObjectPtr theObject, AISObjectPtr theAIS) 
567 {
568   Handle(AIS_InteractiveObject) anAIS = theAIS->impl<Handle(AIS_InteractiveObject)>();
569   if (!anAIS.IsNull()) {
570     Handle(AIS_InteractiveContext) aCtx = anAIS->GetContext();
571     Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(anAIS);
572     if (!aDim.IsNull()) {
573       aCtx->SetZLayer(aDim, myVisualLayerId);
574     } else {
575       Handle(SketcherPrs_SymbolPrs) aCons = Handle(SketcherPrs_SymbolPrs)::DownCast(anAIS);
576       if (!aCons.IsNull())
577         aCtx->SetZLayer(aCons, myVisualLayerId);
578     }
579   }
580 }
581
582 void PartSet_Module::onViewTransformed(int theTrsfType)
583 {
584   // Set length of arrows constant in pixel size
585   // if the operation is panning or rotate or panglobal then do nothing
586   if ((theTrsfType == 1) || (theTrsfType == 3) || (theTrsfType == 4))
587     return;
588
589   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
590   //Handle(V3d_View) aView = aViewer->activeView();
591
592   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
593   XGUI_Workshop* aWorkshop = aConnector->workshop();
594   XGUI_Displayer* aDisplayer = aWorkshop->displayer();
595   Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
596
597   Handle(V3d_Viewer) aV3dViewer = aContext->CurrentViewer();
598   Handle(V3d_View) aView;
599   double aScale = 0;
600   for (aV3dViewer->InitDefinedViews(); 
601        aV3dViewer->MoreDefinedViews(); 
602        aV3dViewer->NextDefinedViews()) {
603     Handle(V3d_View) aV = aV3dViewer->DefinedView();
604     double aS = aV->Scale();
605     if (aS > aScale) {
606       aScale = aS;
607       aView = aV;
608     }
609   }
610   if (aView.IsNull())
611     return;
612   double aLen = aView->Convert(20);
613
614   SketcherPrs_Tools::setArrowSize(aLen);
615   bool isModified = false;
616   QList<AISObjectPtr> aPrsList = aDisplayer->displayedPresentations();
617   foreach (AISObjectPtr aAIS, aPrsList) {
618     Handle(AIS_InteractiveObject) aAisObj = aAIS->impl<Handle(AIS_InteractiveObject)>();
619
620     Handle(AIS_Dimension) aDim = Handle(AIS_Dimension)::DownCast(aAisObj);
621     if (!aDim.IsNull()) {
622       aDim->DimensionAspect()->ArrowAspect()->SetLength(aLen);
623       aContext->Redisplay(aDim, false);
624       isModified = true;
625     }
626   }
627   if (isModified)
628     aDisplayer->updateViewer();
629 }
630
631 ModuleBase_IDocumentDataModel* PartSet_Module::dataModel() const
632 {
633   return myDataModel;
634 }
635
636
637 void PartSet_Module::addObjectBrowserMenu(QMenu* theMenu) const
638 {
639   QObjectPtrList aObjects = myWorkshop->selection()->selectedObjects();
640   int aSelected = aObjects.size();
641   if (aSelected == 1) {
642     bool hasResult = false;
643     bool hasFeature = false;
644     bool hasParameter = false;
645     ModuleBase_Tools::checkObjects(aObjects, hasResult, hasFeature, hasParameter);
646
647     SessionPtr aMgr = ModelAPI_Session::get();
648     ObjectPtr aObject = aObjects.first();
649     if (aObject) {
650       ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
651       if (aPart) {
652         if (aMgr->activeDocument() == aPart->partDoc())
653           theMenu->addAction(myMenuMgr->action("DEACTIVATE_PART_CMD"));
654         else
655           theMenu->addAction(myMenuMgr->action("ACTIVATE_PART_CMD"));
656       } else if (aObject->document() == aMgr->activeDocument()) {
657         if (hasParameter || hasFeature)
658           theMenu->addAction(myMenuMgr->action("EDIT_CMD"));
659       }
660     } else {  // If feature is 0 the it means that selected root object (document)
661       if (aMgr->activeDocument() != aMgr->moduleDocument())
662         theMenu->addAction(myMenuMgr->action("ACTIVATE_PARTSET_CMD"));
663     }
664   }
665 }
666
667 void PartSet_Module::processEvent(const std::shared_ptr<Events_Message>& theMessage)
668 {
669   if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_DOCUMENT_CHANGED)) {
670     XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
671     XGUI_Workshop* aWorkshop = aConnector->workshop();
672     XGUI_DataTree* aTreeView = aWorkshop->objectBrowser()->treeView();
673     QLineEdit* aLabel = aWorkshop->objectBrowser()->activeDocLabel();
674     QPalette aPalet = aLabel->palette();
675
676     SessionPtr aMgr = ModelAPI_Session::get();
677     DocumentPtr aActiveDoc = aMgr->activeDocument();
678     DocumentPtr aDoc = aMgr->moduleDocument();
679     QModelIndex aOldIndex = myDataModel->activePartIndex();
680     if (aActiveDoc == aDoc) {
681       if (aOldIndex.isValid())
682         aTreeView->setExpanded(aOldIndex, false);
683       myDataModel->deactivatePart();
684       aPalet.setColor(QPalette::Text, QColor(0, 72, 140));
685     } else {
686       std::string aGrpName = ModelAPI_ResultPart::group();
687       for (int i = 0; i < aDoc->size(aGrpName); i++) {
688         ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aDoc->object(aGrpName, i));
689         if (aPart->partDoc() == aActiveDoc) {
690           QModelIndex aIndex = myDataModel->partIndex(aPart);
691           if ((aOldIndex != aIndex) && aOldIndex.isValid()) {
692             aTreeView->setExpanded(aOldIndex, false);
693           }
694           if (myDataModel->activatePart(aIndex)) {
695             aTreeView->setExpanded(aIndex.parent(), true);
696             aTreeView->setExpanded(aIndex, true);
697             aPalet.setColor(QPalette::Text, Qt::black);
698           }
699           break;
700         }
701       }
702     }
703     aLabel->setPalette(aPalet);
704   }
705 }