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