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