Salome HOME
7f4fcd8ae40db8fe0092621b4d602aeb30a95a3a
[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
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 bool PartSet_WidgetSketchLabel::processSelection()
197 {
198   std::shared_ptr<GeomAPI_Pln> aPlane = plane();
199   if (aPlane.get())
200     return false;
201
202   QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
203
204   if (aSelected.empty())
205     return false;
206   ModuleBase_ViewerPrsPtr aPrs = aSelected.first();
207   bool aDone = setSelectionInternal(aSelected, false);
208   if (aDone) {
209     updateByPlaneSelected(aPrs);
210     updateObject(myFeature);
211   }
212
213   return aDone;
214 }
215
216 void PartSet_WidgetSketchLabel::onShowConstraint(bool theOn)
217 {
218   QCheckBox* aSenderCheckBox = qobject_cast<QCheckBox*>(sender());
219
220   QMap<PartSet_Tools::ConstraintVisibleState, QCheckBox*>::const_iterator
221                           anIt = myShowConstraints.begin(), aLast = myShowConstraints.end();
222
223   PartSet_Tools::ConstraintVisibleState aState = PartSet_Tools::Geometrical;
224   bool aFound = false;
225   for (; anIt != aLast && !aFound; anIt++) {
226     aFound = anIt.value() == aSenderCheckBox;
227     if (aFound)
228       aState = anIt.key();
229   }
230   if (aFound)
231     emit showConstraintToggled(aState, theOn);
232 }
233
234 void PartSet_WidgetSketchLabel::blockAttribute(const AttributePtr& theAttribute,
235                                                const bool& theToBlock, bool& isFlushesActived,
236                                                bool& isAttributeSetInitializedBlocked,
237                                                bool& isAttributeSendUpdatedBlocked)
238 {
239   ModuleBase_WidgetValidated::blockAttribute(theAttribute, theToBlock, isFlushesActived,
240                                              isAttributeSetInitializedBlocked,
241                                              isAttributeSendUpdatedBlocked);
242   // We do not restore the previous state of isAttributeSetInitializedBlocked for each of
243   // attributes. It it is necessary, these states should be append to the method attributes
244   // or stored in the widget
245
246   std::list<AttributePtr> anAttributes = myFeature->data()->attributes("");
247   std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
248   QStringList aValues;
249   for(; anIt != aLast; anIt++) {
250     AttributePtr anAttribute = *anIt;
251     if (theToBlock)
252       anAttribute->blockSetInitialized(true);
253     else
254       anAttribute->blockSetInitialized(isAttributeSetInitializedBlocked);
255   }
256 }
257
258 bool PartSet_WidgetSketchLabel::setSelectionInternal(
259                                           const QList<ModuleBase_ViewerPrsPtr>& theValues,
260                                           const bool theToValidate)
261 {
262   bool aDone = false;
263   if (theValues.empty()) {
264     // In order to make reselection possible, set empty object and shape should be done
265     setSelectionCustom(std::shared_ptr<ModuleBase_ViewerPrs>(
266                               new ModuleBase_ViewerPrs(ObjectPtr(), GeomShapePtr(), NULL)));
267     aDone = false;
268   }
269   else {
270     // it removes the processed value from the parameters list
271     ModuleBase_ViewerPrsPtr aValue = theValues.first();//.takeFirst();
272     if (!theToValidate || isValidInFilters(aValue))
273       aDone = setSelectionCustom(aValue);
274   }
275
276   return aDone;
277 }
278
279 void PartSet_WidgetSketchLabel::updateByPlaneSelected(const ModuleBase_ViewerPrsPtr& thePrs)
280 {
281   GeomPlanePtr aPlane = plane();
282   if (!aPlane.get())
283     return;
284   // 1. hide main planes if they have been displayed and display sketch preview plane
285   myPreviewPlanes->erasePreviewPlanes(myWorkshop);
286
287   PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
288   if (aModule) {
289     CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
290     bool isSetSizeOfView = false;
291     double aSizeOfView = 0;
292     QString aSizeOfViewStr = mySizeOfView->text();
293     if (!aSizeOfViewStr.isEmpty()) {
294       aSizeOfView = aSizeOfViewStr.toDouble(&isSetSizeOfView);
295       if (isSetSizeOfView && aSizeOfView <= 0) {
296         isSetSizeOfView = false;
297       }
298     }
299     if (isSetSizeOfView)
300       aModule->sketchMgr()->previewSketchPlane()->setSizeOfView(aSizeOfView, true);
301     aModule->sketchMgr()->previewSketchPlane()->createSketchPlane(aSketch, myWorkshop);
302     if (isSetSizeOfView)
303       aModule->sketchMgr()->previewSketchPlane()->setSizeOfView(aSizeOfView, false);
304   }
305   // 2. if the planes were displayed, change the view projection
306
307   std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
308   gp_XYZ aXYZ = aDir->impl<gp_Dir>().XYZ();
309   double aTwist = 0.0;
310
311   // Rotate view if the sketcher plane is selected only from preview planes
312   // Preview planes are created only if there is no any shape
313   bool aRotate = Config_PropManager::boolean(SKETCH_TAB_NAME, "rotate_to_plane");
314   if (aRotate) {
315     myWorkshop->viewer()->setViewProjection(aXYZ.X(), aXYZ.Y(), aXYZ.Z(), aTwist);
316   }
317   QString aSizeOfViewStr = mySizeOfView->text();
318   if (!aSizeOfViewStr.isEmpty()) {
319     bool isOk;
320     double aSizeOfView = aSizeOfViewStr.toDouble(&isOk);
321     if (isOk && aSizeOfView > 0) {
322       Handle(V3d_View) aView3d = myWorkshop->viewer()->activeView();
323       if (!aView3d.IsNull()) {
324         Bnd_Box aBndBox;
325         double aHalfSize = aSizeOfView/2.0;
326         aBndBox.Update(-aHalfSize, -aHalfSize, -aHalfSize, aHalfSize, aHalfSize, aHalfSize);
327         aView3d->FitAll(aBndBox, 0.01, false);
328       }
329     }
330   }
331   if (aModule)
332     aModule->onViewTransformed();
333
334   // 3. Clear text in the label
335   myStackWidget->setCurrentIndex(1);
336   //myLabel->setText("");
337   //myLabel->setToolTip("");
338   XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
339
340   // 5. Clear selection mode and define sketching mode
341   emit planeSelected(plane());
342   // after the plane is selected in the sketch, the sketch selection should be activated
343   // it can not be performed in the sketch label widget because, we don't need to switch off
344   // the selection by any label deactivation, but need to switch it off by stop the sketch
345   myWorkshop->selectionActivate()->updateSelectionFilters();
346   myWorkshop->selectionActivate()->updateSelectionModes();
347
348   // 6. Update sketcher actions
349   XGUI_ActionsMgr* anActMgr = aWorkshop->actionsMgr();
350
351   myWorkshop->updateCommandStatus();
352   aWorkshop->selector()->clearSelection();
353   myWorkshop->viewer()->update();
354 }
355
356 std::shared_ptr<GeomAPI_Pln> PartSet_WidgetSketchLabel::plane() const
357 {
358   CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
359   return PartSet_Tools::sketchPlane(aSketch);
360 }
361
362 bool PartSet_WidgetSketchLabel::focusTo()
363 {
364   ModuleBase_Tools::setFocus(myStackWidget, "PartSet_WidgetSketchLabel::focusTo()");
365   return true;
366 }
367
368 void PartSet_WidgetSketchLabel::enableFocusProcessing()
369 {
370   myStackWidget->installEventFilter(this);
371 }
372
373 void PartSet_WidgetSketchLabel::storeAttributeValue(const AttributePtr& theAttribute)
374 {
375   ModuleBase_WidgetValidated::storeAttributeValue(theAttribute);
376 }
377
378 void PartSet_WidgetSketchLabel::restoreAttributeValue(const AttributePtr& theAttribute,
379                                                       const bool theValid)
380 {
381   ModuleBase_WidgetValidated::restoreAttributeValue(theAttribute, theValid);
382
383   // it is not necessary to save the previous plane value because the plane is chosen once
384   DataPtr aData = feature()->data();
385   AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
386     (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
387   if (aSelAttr) {
388     ResultPtr anEmptyResult;
389     GeomShapePtr anEmptyShape;
390     aSelAttr->setValue(anEmptyResult, anEmptyShape);
391   }
392 }
393
394 bool PartSet_WidgetSketchLabel::setSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
395 {
396   return fillSketchPlaneBySelection(thePrs);
397 }
398
399 bool PartSet_WidgetSketchLabel::canFillSketch(const ModuleBase_ViewerPrsPtr& thePrs)
400 {
401   bool aCanFillSketch = true;
402   // avoid any selection on sketch object
403   ObjectPtr anObject = thePrs->object();
404   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
405   if (aResult.get()) {
406     FeaturePtr aFeature = ModelAPI_Feature::feature(aResult);
407     if (aFeature->getKind() == SketchPlugin_Sketch::ID())
408       aCanFillSketch = false;
409   }
410   // check plane or planar face of any non-sketch object
411   if (aCanFillSketch) {
412     std::shared_ptr<GeomAPI_Face> aGeomFace;
413
414     GeomShapePtr aGeomShape = thePrs->shape();
415     if ((!aGeomShape.get() || aGeomShape->isNull()) && aResult.get()) {
416       aGeomShape = aResult->shape();
417     }
418
419     if (aGeomShape.get() && aGeomShape->shapeType() == GeomAPI_Shape::FACE) {
420       std::shared_ptr<GeomAPI_Face> aGeomFace(new GeomAPI_Face(aGeomShape));
421       aCanFillSketch = aGeomFace.get() && aGeomFace->isPlanar();
422     }
423     else
424       aCanFillSketch = false;
425   }
426   return aCanFillSketch;
427 }
428
429 bool PartSet_WidgetSketchLabel::fillSketchPlaneBySelection(const ModuleBase_ViewerPrsPtr& thePrs)
430 {
431   bool isOwnerSet = false;
432
433   const GeomShapePtr& aShape = thePrs->shape();
434   std::shared_ptr<GeomAPI_Dir> aDir;
435
436   if (aShape.get() && !aShape->isNull()) {
437     const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
438     aDir = setSketchPlane(aTDShape);
439     isOwnerSet = aDir.get();
440   }
441   if (thePrs->object() && (feature() != thePrs->object())) {
442     FeaturePtr aFeature = ModelAPI_Feature::feature(thePrs->object());
443     DataPtr aData = feature()->data();
444     AttributeSelectionPtr aSelAttr =
445       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
446       (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
447     if (aSelAttr.get()) {
448       ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(thePrs->object());
449       if (aRes.get()) {
450         GeomShapePtr aShapePtr;
451         if (!aShape.get() || aShape->isNull()) {  // selection happens in the OCC viewer
452           aShapePtr = ModelAPI_Tools::shape(aRes);
453         }
454         else { // selection happens in OB browser
455           aShapePtr = aShape;
456         }
457         if (aShapePtr.get() && aShapePtr->isFace()) {
458           const TopoDS_Shape& aTDShape = aShapePtr->impl<TopoDS_Shape>();
459           setSketchPlane(aTDShape);
460           aSelAttr->setValue(aRes, aShapePtr);
461           isOwnerSet = true;
462         }
463       }
464       else {
465         aSelAttr->setValue(aFeature, GeomShapePtr());
466         GeomShapePtr aShape = aSelAttr->value();
467         if (!aShape.get() && aSelAttr->contextFeature().get() &&
468           aSelAttr->contextFeature()->firstResult().get()) {
469           aShape = aSelAttr->contextFeature()->firstResult()->shape();
470         }
471         if (aShape.get() && aShape->isPlanar()) {
472           const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
473           setSketchPlane(aTDShape);
474           isOwnerSet = true;
475         }
476       }
477     }
478   }
479   return isOwnerSet;
480 }
481
482 void PartSet_WidgetSketchLabel::activateCustom()
483 {
484   std::shared_ptr<GeomAPI_Pln> aPlane = plane();
485   if (aPlane.get()) {
486     myStackWidget->setCurrentIndex(1);
487     return;
488   }
489
490   myStackWidget->setCurrentIndex(0);
491   bool aBodyIsVisualized = myPreviewPlanes->hasVisualizedBodies(myWorkshop);
492
493   // Clear previous selection mode It is necessary for correct activation of preview planes
494   XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
495   XGUI_Displayer* aDisp = aWorkshop->displayer();
496   aWorkshop->selectionActivate()->activateObjects(QIntList(), aDisp->displayedObjects(), false);
497
498   if (!aBodyIsVisualized) {
499     // We have to select a plane before any operation
500     myPreviewPlanes->showPreviewPlanes(myWorkshop);
501     mySizeOfViewWidget->setVisible(true);
502   }
503   else
504     mySizeOfViewWidget->setVisible(false);
505 }
506
507 void PartSet_WidgetSketchLabel::deactivate()
508 {
509   ModuleBase_WidgetValidated::deactivate();
510   bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
511   myPreviewPlanes->erasePreviewPlanes(myWorkshop);
512
513   if (aHidePreview)
514     myWorkshop->viewer()->update();
515 }
516
517 void PartSet_WidgetSketchLabel::selectionModes(int& theModuleSelectionModes, QIntList& theModes)
518 {
519   theModuleSelectionModes = -1;
520   std::shared_ptr<GeomAPI_Pln> aPlane = plane();
521   if (!aPlane.get())
522     theModes << TopAbs_FACE;
523 }
524
525 void PartSet_WidgetSketchLabel::selectionFilters(QIntList& theModuleSelectionFilters,
526                                                  SelectMgr_ListOfFilter& theSelectionFilters)
527 {
528   std::shared_ptr<GeomAPI_Pln> aPlane = plane();
529   if (aPlane.get())
530     return;
531   return ModuleBase_WidgetValidated::selectionFilters(theModuleSelectionFilters,
532                                                       theSelectionFilters);
533 }
534
535 std::shared_ptr<GeomAPI_Dir>
536   PartSet_WidgetSketchLabel::setSketchPlane(const TopoDS_Shape& theShape)
537 {
538   if (theShape.IsNull())
539     return std::shared_ptr<GeomAPI_Dir>();
540
541   // get selected shape
542   std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
543   aGShape->setImpl(new TopoDS_Shape(theShape));
544
545   // get plane parameters
546   std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aGShape));
547   std::shared_ptr<GeomAPI_Pln> aPlane = aFace->getPlane();
548   if (!aPlane.get())
549     return std::shared_ptr<GeomAPI_Dir>();
550   return setSketchPlane(aPlane);
551 }
552
553 std::shared_ptr<GeomAPI_Dir>
554   PartSet_WidgetSketchLabel::setSketchPlane(std::shared_ptr<GeomAPI_Pln> thePlane)
555 {
556   // set plane parameters to feature
557   std::shared_ptr<ModelAPI_Data> aData = feature()->data();
558   double anA, aB, aC, aD;
559   thePlane->coefficients(anA, aB, aC, aD);
560
561   // calculate attributes of the sketch
562   std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
563   std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
564   std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
565   aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
566   std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
567   // X axis is preferable to be dirX on the sketch
568   const double tol = Precision::Confusion();
569   bool isX = fabs(fabs(anA) - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
570   std::shared_ptr<GeomAPI_Dir> aTempDir(
571       isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
572   std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
573   std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
574
575   std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
576       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
577   anOrigin->setValue(anOrigPnt);
578   std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
579       aData->attribute(SketchPlugin_Sketch::NORM_ID()));
580   aNormal->setValue(aNormDir);
581   std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
582       aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
583   aDirX->setValue(aXDir);
584   std::shared_ptr<GeomAPI_Dir> aDir = thePlane->direction();
585   return aDir;
586 }
587
588 void PartSet_WidgetSketchLabel::onSetPlaneView()
589 {
590   std::shared_ptr<GeomAPI_Pln> aPlane = plane();
591   if (aPlane.get()) {
592     std::shared_ptr<GeomAPI_Dir> aDirection = aPlane->direction();
593     gp_Dir aDir = aDirection->impl<gp_Dir>();
594     if (myViewInverted->isChecked())
595       aDir.Reverse();
596     myWorkshop->viewer()->setViewProjection(aDir.X(), aDir.Y(), aDir.Z(), 0.);
597     PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
598     if (aModule)
599       aModule->onViewTransformed();
600   }
601 }
602
603
604 //******************************************************
605 QList<std::shared_ptr<ModuleBase_ViewerPrs>> PartSet_WidgetSketchLabel::findCircularEdgesInPlane()
606 {
607   QList<std::shared_ptr<ModuleBase_ViewerPrs>> aResult;
608   XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
609   XGUI_Displayer* aDisplayer = aWorkshop->displayer();
610   QObjectPtrList aDispObjects = aDisplayer->displayedObjects();
611
612   std::shared_ptr<GeomAPI_Pln> aPlane = plane();
613   foreach(ObjectPtr aObj, aDispObjects) {
614     ResultPtr aResObj = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
615     if (aResObj.get()) {
616       GeomShapePtr aShape = aResObj->shape();
617       if (aShape.get()) {
618         GeomAPI_ShapeExplorer aExplorer(aShape, GeomAPI_Shape::EDGE);
619         for(; aExplorer.more(); aExplorer.next()) {
620           GeomShapePtr aEdgeShape = aExplorer.current();
621           GeomAPI_Edge anEdge(aEdgeShape);
622           if ((anEdge.isCircle() || anEdge.isArc() || anEdge.isEllipse()) &&
623                anEdge.isInPlane(aPlane)) {
624             bool isContains = false;
625             // Check that edge is not used.
626             // It is possible that the same edge will be taken from different faces
627             foreach(std::shared_ptr<ModuleBase_ViewerPrs> aPrs, aResult) {
628               GeomAPI_Edge aUsedEdge(aPrs->shape());
629               if (aUsedEdge.isEqual(aEdgeShape)) {
630                 isContains = true;
631                 break;
632               }
633             }
634             if (!isContains) {
635               std::shared_ptr<ModuleBase_ViewerPrs>
636                 aPrs(new ModuleBase_ViewerPrs(aResObj, aEdgeShape));
637               aResult.append(aPrs);
638             }
639           }
640         }
641       }
642     }
643   }
644   return aResult;
645 }