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