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