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