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