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