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