Salome HOME
Issue #2961: Show selection of planes for sketcher on plane change
[modules/shaper.git] / src / PartSet / PartSet_WidgetSketchLabel.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "PartSet_WidgetSketchLabel.h"
21 #include "PartSet_Tools.h"
22 #include "PartSet_Module.h"
23 #include "PartSet_PreviewPlanes.h"
24
25 #include "SketchPlugin_SketchEntity.h"
26
27 #include <XGUI_ActionsMgr.h>
28 #include <XGUI_Displayer.h>
29 #include <XGUI_ModuleConnector.h>
30 #include <XGUI_SelectionActivate.h>
31 #include <XGUI_Selection.h>
32 #include <XGUI_SelectionMgr.h>
33 #include <XGUI_Tools.h>
34 #include <XGUI_ViewerProxy.h>
35 #include <XGUI_Workshop.h>
36
37 #include <ModelAPI_ResultBody.h>
38 #include <ModelAPI_Tools.h>
39
40 #include <ModuleBase_Operation.h>
41 #include <ModuleBase_ViewerPrs.h>
42 #include <ModuleBase_Tools.h>
43 #include <ModuleBase_IModule.h>
44 #include <ModuleBase_IPropertyPanel.h>
45
46 #include <GeomAlgoAPI_FaceBuilder.h>
47 #include <GeomAlgoAPI_ShapeTools.h>
48 #include <GeomDataAPI_Point.h>
49 #include <GeomDataAPI_Dir.h>
50 #include <GeomAPI_XYZ.h>
51 #include <GeomAPI_Face.h>
52 #include <GeomAPI_Edge.h>
53 #include <GeomAPI_ShapeExplorer.h>
54
55 #include <SketchPlugin_Sketch.h>
56 #include <SketcherPrs_Tools.h>
57
58 #include <Precision.hxx>
59 #include <gp_Pln.hxx>
60 #include <gp_Pnt.hxx>
61 #include <gp_Dir.hxx>
62 #include <AIS_Shape.hxx>
63 #include <AIS_DimensionSelectionMode.hxx>
64 #include <Bnd_Box.hxx>
65
66 #include <Config_WidgetAPI.h>
67 #include <Config_PropManager.h>
68
69 #include <QLabel>
70 #include <QApplication>
71 #include <QVBoxLayout>
72 #include <QHBoxLayout>
73 #include <QCheckBox>
74 #include <QGroupBox>
75 #include <QPushButton>
76 #include <QStackedWidget>
77 #include <QLineEdit>
78 #include <QDoubleValidator>
79
80 #ifndef DBL_MAX
81 #define DBL_MAX 1.7976931348623158e+308
82 #endif
83
84 PartSet_WidgetSketchLabel::PartSet_WidgetSketchLabel(QWidget* theParent,
85                         ModuleBase_IWorkshop* theWorkshop,
86                         const Config_WidgetAPI* theData,
87                         const QMap<PartSet_Tools::ConstraintVisibleState, bool>& toShowConstraints)
88 : ModuleBase_WidgetValidated(theParent, theWorkshop, theData), myOpenTransaction(false),
89 myIsSelection(false)
90 {
91   QVBoxLayout* aLayout = new QVBoxLayout(this);
92   ModuleBase_Tools::zeroMargins(aLayout);
93
94   myStackWidget = new QStackedWidget(this);
95   myStackWidget->setContentsMargins(0,0,0,0);
96   aLayout->addWidget(myStackWidget);
97
98   // Define label for plane selection
99   QWidget* aFirstWgt = new QWidget(this);
100
101   // Size of the View control
102   mySizeOfViewWidget = new QWidget(aFirstWgt);
103   QHBoxLayout* aSizeLayout = new QHBoxLayout(mySizeOfViewWidget);
104   aSizeLayout->addWidget(new QLabel("Size of the view", mySizeOfViewWidget));
105   mySizeOfView = new QLineEdit(mySizeOfViewWidget);
106
107   QDoubleValidator* aValidator = new QDoubleValidator(0, DBL_MAX, 12, mySizeOfView);
108   aValidator->setLocale(ModuleBase_Tools::doubleLocale());
109   aValidator->setNotation(QDoubleValidator::StandardNotation);
110   mySizeOfView->setValidator(aValidator);
111   aSizeLayout->addWidget(mySizeOfView);
112
113   QString aText = QString::fromStdString(theData->getProperty("title"));
114   QLabel* aLabel = new QLabel(aText, aFirstWgt);
115   aLabel->setWordWrap(true);
116   QString aTooltip = QString::fromStdString(theData->getProperty("tooltip"));
117   aLabel->setToolTip(aTooltip);
118   aLabel->setIndent(5);
119
120   aLayout = new QVBoxLayout(aFirstWgt);
121   ModuleBase_Tools::zeroMargins(aLayout);
122   aLayout->addWidget(mySizeOfViewWidget);
123   aLayout->addWidget(aLabel);
124
125   myRemoveExternal = new QCheckBox(tr("Remove external dependencies"), aFirstWgt);
126   myRemoveExternal->setChecked(false);
127   aLayout->addWidget(myRemoveExternal);
128   myRemoveExternal->setVisible(false);
129
130   aLayout->addStretch(1);
131
132   myStackWidget->addWidget(aFirstWgt);
133
134   // Define widget for sketch manmagement
135   QWidget* aSecondWgt = new QWidget(this);
136   aLayout = new QVBoxLayout(aSecondWgt);
137   ModuleBase_Tools::zeroMargins(aLayout);
138
139   QGroupBox* aViewBox = new QGroupBox(tr("Sketcher plane"), this);
140   QVBoxLayout* aViewLayout = new QVBoxLayout(aViewBox);
141
142   myViewInverted = new QCheckBox(tr("Reversed"), aViewBox);
143   aViewLayout->addWidget(myViewInverted);
144
145   QPushButton* aSetViewBtn =
146     new QPushButton(QIcon(":icons/plane_view.png"), tr("Set plane view"), aViewBox);
147   connect(aSetViewBtn, SIGNAL(clicked(bool)), this, SLOT(onSetPlaneView()));
148   aViewLayout->addWidget(aSetViewBtn);
149
150   aLayout->addWidget(aViewBox);
151
152   QMap<PartSet_Tools::ConstraintVisibleState, QString> aStates;
153   aStates[PartSet_Tools::Geometrical] = tr("Show geometrical constraints");
154   aStates[PartSet_Tools::Dimensional] = tr("Show dimensional constraints");
155   aStates[PartSet_Tools::Expressions] = tr("Show existing expressions");
156
157   QMap<PartSet_Tools::ConstraintVisibleState, QString>::const_iterator anIt = aStates.begin(),
158                                                         aLast = aStates.end();
159   for (; anIt != aLast; anIt++) {
160     QCheckBox* aShowConstraints = new QCheckBox(anIt.value(), this);
161     connect(aShowConstraints, SIGNAL(toggled(bool)), this, SLOT(onShowConstraint(bool)));
162     aLayout->addWidget(aShowConstraints);
163
164     PartSet_Tools::ConstraintVisibleState aState = anIt.key();
165     myShowConstraints[aState] = aShowConstraints;
166
167     if (toShowConstraints.contains(aState))
168       aShowConstraints->setChecked(toShowConstraints[aState]);
169   }
170   myShowPoints = new QCheckBox(tr("Show free points"), this);
171   connect(myShowPoints, SIGNAL(toggled(bool)), this, SIGNAL(showFreePoints(bool)));
172   aLayout->addWidget(myShowPoints);
173
174   QPushButton* aPlaneBtn = new QPushButton(tr("Change sketch plane"), aSecondWgt);
175   connect(aPlaneBtn, SIGNAL(clicked(bool)), SLOT(onChangePlane()));
176   aLayout->addWidget(aPlaneBtn);
177
178   myStackWidget->addWidget(aSecondWgt);
179   //setLayout(aLayout);
180
181   myPreviewPlanes = new PartSet_PreviewPlanes();
182 }
183
184 PartSet_WidgetSketchLabel::~PartSet_WidgetSketchLabel()
185 {
186 }
187
188 bool PartSet_WidgetSketchLabel::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
189                                              const bool theToValidate)
190 {
191   // do not use the given selection if the plane of the sketch has been already set.
192   // If this check is absent, a selected plane in the viewer can be set in the sketch
193   // even if the sketch is built on another plane.
194   if (plane().get())
195     return true;
196
197   ModuleBase_ViewerPrsPtr aPrs = theValues.first();
198   bool aDone = setSelectionInternal(theValues, theToValidate);
199   if (aDone)
200     updateByPlaneSelected(aPrs);
201   return aDone;
202 }
203
204 QList<QWidget*> PartSet_WidgetSketchLabel::getControls() const
205 {
206   QList<QWidget*> aResult;
207   aResult << myStackWidget;
208   return aResult;
209 }
210
211 bool PartSet_WidgetSketchLabel::processSelection()
212 {
213   std::shared_ptr<GeomAPI_Pln> aPlane = plane();
214   if (aPlane.get())
215     return false;
216
217   QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
218
219   if (aSelected.empty())
220     return false;
221   ModuleBase_ViewerPrsPtr aPrs = aSelected.first();
222   bool aDone = setSelectionInternal(aSelected, false);
223   if (aDone) {
224     updateByPlaneSelected(aPrs);
225     updateObject(myFeature);
226   }
227
228   return aDone;
229 }
230
231 void PartSet_WidgetSketchLabel::onShowConstraint(bool theOn)
232 {
233   QCheckBox* aSenderCheckBox = qobject_cast<QCheckBox*>(sender());
234
235   QMap<PartSet_Tools::ConstraintVisibleState, QCheckBox*>::const_iterator
236                           anIt = myShowConstraints.begin(), aLast = myShowConstraints.end();
237
238   PartSet_Tools::ConstraintVisibleState aState = PartSet_Tools::Geometrical;
239   bool aFound = false;
240   for (; anIt != aLast && !aFound; anIt++) {
241     aFound = anIt.value() == aSenderCheckBox;
242     if (aFound)
243       aState = anIt.key();
244   }
245   if (aFound)
246     emit showConstraintToggled(aState, theOn);
247 }
248
249 void PartSet_WidgetSketchLabel::blockAttribute(const AttributePtr& theAttribute,
250                                                const bool& theToBlock, bool& isFlushesActived,
251                                                bool& isAttributeSetInitializedBlocked,
252                                                bool& isAttributeSendUpdatedBlocked)
253 {
254   ModuleBase_WidgetValidated::blockAttribute(theAttribute, theToBlock, isFlushesActived,
255                                              isAttributeSetInitializedBlocked,
256                                              isAttributeSendUpdatedBlocked);
257   // We do not restore the previous state of isAttributeSetInitializedBlocked for each of
258   // attributes. It it is necessary, these states should be append to the method attributes
259   // or stored in the widget
260
261   std::list<AttributePtr> anAttributes = myFeature->data()->attributes("");
262   std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
263   QStringList aValues;
264   for(; anIt != aLast; anIt++) {
265     AttributePtr anAttribute = *anIt;
266     if (theToBlock)
267       anAttribute->blockSetInitialized(true);
268     else
269       anAttribute->blockSetInitialized(isAttributeSetInitializedBlocked);
270   }
271 }
272
273 bool PartSet_WidgetSketchLabel::setSelectionInternal(
274                                           const QList<ModuleBase_ViewerPrsPtr>& theValues,
275                                           const bool theToValidate)
276 {
277   bool aDone = false;
278   if (theValues.empty()) {
279     // In order to make reselection possible, set empty object and shape should be done
280     setSelectionCustom(std::shared_ptr<ModuleBase_ViewerPrs>(
281                               new ModuleBase_ViewerPrs(ObjectPtr(), GeomShapePtr(), NULL)));
282     aDone = false;
283   }
284   else {
285     // it removes the processed value from the parameters list
286     ModuleBase_ViewerPrsPtr aValue = theValues.first();//.takeFirst();
287     if (!theToValidate || isValidInFilters(aValue)) {
288       myIsSelection = true;
289       aDone = setSelectionCustom(aValue);
290       myIsSelection = false;
291     }
292   }
293
294   return aDone;
295 }
296
297 void PartSet_WidgetSketchLabel::updateByPlaneSelected(const ModuleBase_ViewerPrsPtr& thePrs)
298 {
299   GeomPlanePtr aPlane = plane();
300   if (!aPlane.get())
301     return;
302   // 1. hide main planes if they have been displayed and display sketch preview plane
303   myPreviewPlanes->erasePreviewPlanes(myWorkshop);
304
305   PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
306   if (aModule) {
307     CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
308     bool isSetSizeOfView = false;
309     double aSizeOfView = 0;
310     QString aSizeOfViewStr = mySizeOfView->text();
311     if (!aSizeOfViewStr.isEmpty()) {
312       aSizeOfView = aSizeOfViewStr.toDouble(&isSetSizeOfView);
313       if (isSetSizeOfView && aSizeOfView <= 0) {
314         isSetSizeOfView = false;
315       }
316     }
317     aModule->sketchMgr()->previewSketchPlane()->setSizeOfView(aSizeOfView, isSetSizeOfView);
318     aModule->sketchMgr()->previewSketchPlane()->createSketchPlane(aSketch, myWorkshop);
319   }
320   // 2. if the planes were displayed, change the view projection
321
322   std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
323   gp_XYZ aXYZ = aDir->impl<gp_Dir>().XYZ();
324   double aTwist = 0.0;
325
326   // Rotate view if the sketcher plane is selected only from preview planes
327   // Preview planes are created only if there is no any shape
328   bool aRotate = Config_PropManager::boolean(SKETCH_TAB_NAME, "rotate_to_plane");
329   if (aRotate) {
330     myWorkshop->viewer()->setViewProjection(aXYZ.X(), aXYZ.Y(), aXYZ.Z(), aTwist);
331   }
332   QString aSizeOfViewStr = mySizeOfView->text();
333   if (!aSizeOfViewStr.isEmpty()) {
334     bool isOk;
335     double aSizeOfView = aSizeOfViewStr.toDouble(&isOk);
336     if (isOk && aSizeOfView > 0) {
337       Handle(V3d_View) aView3d = myWorkshop->viewer()->activeView();
338       if (!aView3d.IsNull()) {
339         Bnd_Box aBndBox;
340         double aHalfSize = aSizeOfView/2.0;
341         aBndBox.Update(-aHalfSize, -aHalfSize, -aHalfSize, aHalfSize, aHalfSize, aHalfSize);
342         aView3d->FitAll(aBndBox, 0.01, false);
343       }
344     }
345   }
346   if (myOpenTransaction) {
347     SessionPtr aMgr = ModelAPI_Session::get();
348     aMgr->finishOperation();
349     myOpenTransaction = false;
350   }
351   // 3. Clear text in the label
352   myStackWidget->setCurrentIndex(1);
353   //myLabel->setText("");
354   //myLabel->setToolTip("");
355   XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
356
357   // 5. Clear selection mode and define sketching mode
358   emit planeSelected(plane());
359   // after the plane is selected in the sketch, the sketch selection should be activated
360   // it can not be performed in the sketch label widget because, we don't need to switch off
361   // the selection by any label deactivation, but need to switch it off by stop the sketch
362   myWorkshop->selectionActivate()->updateSelectionFilters();
363   myWorkshop->selectionActivate()->updateSelectionModes();
364
365   if (aModule)
366     aModule->onViewTransformed();
367
368   // 6. Update sketcher actions
369   XGUI_ActionsMgr* anActMgr = aWorkshop->actionsMgr();
370
371   myWorkshop->updateCommandStatus();
372   aWorkshop->selector()->clearSelection();
373   myWorkshop->viewer()->update();
374
375   myRemoveExternal->setVisible(false);
376 }
377
378 std::shared_ptr<GeomAPI_Pln> PartSet_WidgetSketchLabel::plane() const
379 {
380   CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
381   return PartSet_Tools::sketchPlane(aSketch);
382 }
383
384 bool PartSet_WidgetSketchLabel::focusTo()
385 {
386   ModuleBase_Tools::setFocus(myStackWidget, "PartSet_WidgetSketchLabel::focusTo()");
387   return true;
388 }
389
390 void PartSet_WidgetSketchLabel::enableFocusProcessing()
391 {
392   myStackWidget->installEventFilter(this);
393 }
394
395 void PartSet_WidgetSketchLabel::storeAttributeValue(const AttributePtr& theAttribute)
396 {
397   ModuleBase_WidgetValidated::storeAttributeValue(theAttribute);
398 }
399
400 void PartSet_WidgetSketchLabel::restoreAttributeValue(const AttributePtr& theAttribute,
401                                                       const bool theValid)
402 {
403   ModuleBase_WidgetValidated::restoreAttributeValue(theAttribute, theValid);
404
405   // it is not necessary to save the previous plane value because the plane is chosen once
406   DataPtr aData = feature()->data();
407   AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
408     (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
409   if (aSelAttr) {
410     ResultPtr anEmptyResult;
411     GeomShapePtr anEmptyShape;
412     aSelAttr->setValue(anEmptyResult, anEmptyShape);
413   }
414 }
415
416 bool PartSet_WidgetSketchLabel::setSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
417 {
418   if (myIsSelection && myRemoveExternal->isVisible()) {
419     if (myRemoveExternal->isChecked()) {
420       myFeature->customAction(SketchPlugin_Sketch::ACTION_REMOVE_EXTERNAL());
421     }
422   }
423   return fillSketchPlaneBySelection(thePrs);
424 }
425
426 bool PartSet_WidgetSketchLabel::canFillSketch(const ModuleBase_ViewerPrsPtr& thePrs)
427 {
428   bool aCanFillSketch = true;
429   // avoid any selection on sketch object
430   ObjectPtr anObject = thePrs->object();
431   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
432   if (aResult.get()) {
433     FeaturePtr aFeature = ModelAPI_Feature::feature(aResult);
434     if (aFeature->getKind() == SketchPlugin_Sketch::ID())
435       aCanFillSketch = false;
436   }
437   // check plane or planar face of any non-sketch object
438   if (aCanFillSketch) {
439     std::shared_ptr<GeomAPI_Face> aGeomFace;
440
441     GeomShapePtr aGeomShape = thePrs->shape();
442     if ((!aGeomShape.get() || aGeomShape->isNull()) && aResult.get()) {
443       aGeomShape = aResult->shape();
444     }
445
446     if (aGeomShape.get() && aGeomShape->shapeType() == GeomAPI_Shape::FACE) {
447       std::shared_ptr<GeomAPI_Face> aGeomFace(new GeomAPI_Face(aGeomShape));
448       aCanFillSketch = aGeomFace.get() && aGeomFace->isPlanar();
449     }
450     else
451       aCanFillSketch = false;
452   }
453   return aCanFillSketch;
454 }
455
456 bool PartSet_WidgetSketchLabel::fillSketchPlaneBySelection(const ModuleBase_ViewerPrsPtr& thePrs)
457 {
458   bool isOwnerSet = false;
459
460   const GeomShapePtr& aShape = thePrs->shape();
461   std::shared_ptr<GeomAPI_Dir> aDir;
462
463   if (aShape.get() && !aShape->isNull()) {
464     const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
465     aDir = setSketchPlane(aTDShape);
466     isOwnerSet = aDir.get();
467   }
468   if (thePrs->object() && (feature() != thePrs->object())) {
469     FeaturePtr aFeature = ModelAPI_Feature::feature(thePrs->object());
470     DataPtr aData = feature()->data();
471     AttributeSelectionPtr aSelAttr =
472       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
473       (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
474     if (aSelAttr.get()) {
475       ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(thePrs->object());
476       if (aRes.get()) {
477         GeomShapePtr aShapePtr;
478         if (!aShape.get() || aShape->isNull()) {  // selection happens in the OCC viewer
479           aShapePtr = ModelAPI_Tools::shape(aRes);
480         }
481         else { // selection happens in OB browser
482           aShapePtr = aShape;
483         }
484         if (aShapePtr.get() && aShapePtr->isFace()) {
485           const TopoDS_Shape& aTDShape = aShapePtr->impl<TopoDS_Shape>();
486           setSketchPlane(aTDShape);
487           aSelAttr->setValue(aRes, aShapePtr);
488           isOwnerSet = true;
489         }
490       }
491       else {
492         aSelAttr->setValue(aFeature, GeomShapePtr());
493         GeomShapePtr aShape = aSelAttr->value();
494         if (!aShape.get() && aSelAttr->contextFeature().get() &&
495           aSelAttr->contextFeature()->firstResult().get()) {
496           aShape = aSelAttr->contextFeature()->firstResult()->shape();
497         }
498         if (aShape.get() && aShape->isPlanar()) {
499           const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
500           setSketchPlane(aTDShape);
501           isOwnerSet = true;
502         }
503       }
504     }
505   }
506   return isOwnerSet;
507 }
508
509 void PartSet_WidgetSketchLabel::activateCustom()
510 {
511   std::shared_ptr<GeomAPI_Pln> aPlane = plane();
512   if (aPlane.get()) {
513     myStackWidget->setCurrentIndex(1);
514     return;
515   }
516
517   myStackWidget->setCurrentIndex(0);
518   bool aBodyIsVisualized = myPreviewPlanes->hasVisualizedBodies(myWorkshop);
519
520   // Clear previous selection mode It is necessary for correct activation of preview planes
521   XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
522   XGUI_Displayer* aDisp = aWorkshop->displayer();
523   aWorkshop->selectionActivate()->activateObjects(QIntList(), aDisp->displayedObjects(), false);
524
525   if (!aBodyIsVisualized) {
526     // We have to select a plane before any operation
527     myPreviewPlanes->showPreviewPlanes(myWorkshop);
528     mySizeOfViewWidget->setVisible(true);
529   }
530   else
531     mySizeOfViewWidget->setVisible(false);
532 }
533
534 void PartSet_WidgetSketchLabel::deactivate()
535 {
536   ModuleBase_WidgetValidated::deactivate();
537   bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
538   myPreviewPlanes->erasePreviewPlanes(myWorkshop);
539
540   if (aHidePreview)
541     myWorkshop->viewer()->update();
542
543   if (myOpenTransaction) {
544     SessionPtr aMgr = ModelAPI_Session::get();
545     aMgr->finishOperation();
546     myOpenTransaction = false;
547   }
548 }
549
550 void PartSet_WidgetSketchLabel::selectionModes(int& theModuleSelectionModes, QIntList& theModes)
551 {
552   theModuleSelectionModes = -1;
553   std::shared_ptr<GeomAPI_Pln> aPlane = plane();
554   if (!aPlane.get())
555     theModes << TopAbs_FACE;
556 }
557
558 void PartSet_WidgetSketchLabel::selectionFilters(QIntList& theModuleSelectionFilters,
559                                                  SelectMgr_ListOfFilter& theSelectionFilters)
560 {
561   std::shared_ptr<GeomAPI_Pln> aPlane = plane();
562   if (aPlane.get())
563     return;
564   return ModuleBase_WidgetValidated::selectionFilters(theModuleSelectionFilters,
565                                                       theSelectionFilters);
566 }
567
568 std::shared_ptr<GeomAPI_Dir>
569   PartSet_WidgetSketchLabel::setSketchPlane(const TopoDS_Shape& theShape)
570 {
571   if (theShape.IsNull())
572     return std::shared_ptr<GeomAPI_Dir>();
573
574   // get selected shape
575   std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
576   aGShape->setImpl(new TopoDS_Shape(theShape));
577
578   // get plane parameters
579   std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aGShape));
580   std::shared_ptr<GeomAPI_Pln> aPlane = aFace->getPlane();
581   if (!aPlane.get())
582     return std::shared_ptr<GeomAPI_Dir>();
583   return setSketchPlane(aPlane);
584 }
585
586 std::shared_ptr<GeomAPI_Dir>
587   PartSet_WidgetSketchLabel::setSketchPlane(std::shared_ptr<GeomAPI_Pln> thePlane)
588 {
589   // set plane parameters to feature
590   std::shared_ptr<ModelAPI_Data> aData = feature()->data();
591   double anA, aB, aC, aD;
592   thePlane->coefficients(anA, aB, aC, aD);
593
594   // calculate attributes of the sketch
595   std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
596   std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
597   std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
598   aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
599   std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
600   // X axis is preferable to be dirX on the sketch
601   const double tol = Precision::Confusion();
602   bool isX = fabs(fabs(anA) - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
603   std::shared_ptr<GeomAPI_Dir> aTempDir(
604       isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
605   std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
606   std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
607
608   std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
609       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
610   anOrigin->setValue(anOrigPnt);
611   std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
612       aData->attribute(SketchPlugin_Sketch::NORM_ID()));
613   aNormal->setValue(aNormDir);
614   std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
615       aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
616   aDirX->setValue(aXDir);
617   std::shared_ptr<GeomAPI_Dir> aDir = thePlane->direction();
618   return aDir;
619 }
620
621 void PartSet_WidgetSketchLabel::onSetPlaneView()
622 {
623   std::shared_ptr<GeomAPI_Pln> aPlane = plane();
624   if (aPlane.get()) {
625     std::shared_ptr<GeomAPI_Dir> aDirection = aPlane->direction();
626     gp_Dir aDir = aDirection->impl<gp_Dir>();
627     if (myViewInverted->isChecked())
628       aDir.Reverse();
629     myWorkshop->viewer()->setViewProjection(aDir.X(), aDir.Y(), aDir.Z(), 0.);
630     PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
631     if (aModule)
632       aModule->onViewTransformed();
633   }
634 }
635
636
637 //******************************************************
638 QList<std::shared_ptr<ModuleBase_ViewerPrs>> PartSet_WidgetSketchLabel::findCircularEdgesInPlane()
639 {
640   QList<std::shared_ptr<ModuleBase_ViewerPrs>> aResult;
641   XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
642   XGUI_Displayer* aDisplayer = aWorkshop->displayer();
643   QObjectPtrList aDispObjects = aDisplayer->displayedObjects();
644
645   std::shared_ptr<GeomAPI_Pln> aPlane = plane();
646   foreach(ObjectPtr aObj, aDispObjects) {
647     ResultPtr aResObj = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
648     if (aResObj.get()) {
649       GeomShapePtr aShape = aResObj->shape();
650       if (aShape.get()) {
651         GeomAPI_ShapeExplorer aExplorer(aShape, GeomAPI_Shape::EDGE);
652         for(; aExplorer.more(); aExplorer.next()) {
653           GeomShapePtr aEdgeShape = aExplorer.current();
654           GeomAPI_Edge anEdge(aEdgeShape);
655           if ((anEdge.isCircle() || anEdge.isArc() || anEdge.isEllipse()) &&
656                anEdge.isInPlane(aPlane)) {
657             bool isContains = false;
658             // Check that edge is not used.
659             // It is possible that the same edge will be taken from different faces
660             foreach(std::shared_ptr<ModuleBase_ViewerPrs> aPrs, aResult) {
661               GeomAPI_Edge aUsedEdge(aPrs->shape());
662               if (aUsedEdge.isEqual(aEdgeShape)) {
663                 isContains = true;
664                 break;
665               }
666             }
667             if (!isContains) {
668               std::shared_ptr<ModuleBase_ViewerPrs>
669                 aPrs(new ModuleBase_ViewerPrs(aResObj, aEdgeShape));
670               aResult.append(aPrs);
671             }
672           }
673         }
674       }
675     }
676   }
677   return aResult;
678 }
679
680 //******************************************************
681 void PartSet_WidgetSketchLabel::onChangePlane()
682 {
683   PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
684   if (aModule) {
685     mySizeOfViewWidget->setVisible(false);
686     myRemoveExternal->setVisible(true);
687     myStackWidget->setCurrentIndex(0);
688
689     bool aBodyIsVisualized = myPreviewPlanes->hasVisualizedBodies(myWorkshop);
690
691     if (!aBodyIsVisualized) {
692       // We have to select a plane before any operation
693       myPreviewPlanes->showPreviewPlanes(myWorkshop);
694     }
695
696     CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
697     PartSet_Tools::nullifySketchPlane(aSketch);
698
699     Handle(SelectMgr_Filter) aFilter = aModule->selectionFilter(SF_SketchPlaneFilter);
700     if (!aFilter.IsNull()) {
701       std::shared_ptr<GeomAPI_Pln> aPln;
702       Handle(ModuleBase_ShapeInPlaneFilter)::DownCast(aFilter)->setPlane(aPln);
703     }
704     XGUI_Workshop* aWorkshop = aModule->getWorkshop();
705
706     aWorkshop->selectionActivate()->updateSelectionFilters();
707     aWorkshop->selectionActivate()->updateSelectionModes();
708
709     SessionPtr aMgr = ModelAPI_Session::get();
710     aMgr->startOperation("Change Sketch plane");
711     myOpenTransaction = true;
712   }
713 }
714
715 void PartSet_WidgetSketchLabel::setShowPointsState(bool theState)
716 {
717   bool aBlock = myShowPoints->blockSignals(true);
718   myShowPoints->setChecked(theState);
719   myShowPoints->blockSignals(aBlock);
720 }