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