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