1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
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"
26 #include "SketchPlugin_SketchEntity.h"
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>
38 #include <ModelAPI_ResultBody.h>
39 #include <ModelAPI_Tools.h>
40 #include <ModelAPI_AttributeString.h>
41 #include <ModelAPI_Events.h>
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>
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>
58 #include <SketchPlugin_Sketch.h>
59 #include <SketcherPrs_Tools.h>
61 #include <Precision.hxx>
65 #include <AIS_Shape.hxx>
66 #include <AIS_DimensionSelectionMode.hxx>
67 #include <Bnd_Box.hxx>
69 #include <Config_WidgetAPI.h>
70 #include <Config_PropManager.h>
73 #include <QApplication>
74 #include <QVBoxLayout>
75 #include <QHBoxLayout>
78 #include <QPushButton>
80 #include <QDoubleValidator>
85 #define DBL_MAX 1.7976931348623158e+308
88 PartSet_WidgetSketchLabel::PartSet_WidgetSketchLabel(QWidget* theParent,
89 ModuleBase_IWorkshop* theWorkshop,
90 const Config_WidgetAPI* theData,
91 const QMap<PartSet_Tools::ConstraintVisibleState, bool>& toShowConstraints)
92 : ModuleBase_WidgetValidated(theParent, theWorkshop, theData), myOpenTransaction(false),
95 QVBoxLayout* aLayout = new QVBoxLayout(this);
96 ModuleBase_Tools::zeroMargins(aLayout);
98 myStackWidget = new QStackedWidget(this);
99 myStackWidget->setContentsMargins(0,0,0,0);
100 aLayout->addWidget(myStackWidget);
102 // Define label for plane selection
103 QWidget* aFirstWgt = new QWidget(this);
105 // Size of the View control
106 mySizeOfViewWidget = new QWidget(aFirstWgt);
107 QHBoxLayout* aSizeLayout = new QHBoxLayout(mySizeOfViewWidget);
108 aSizeLayout->addWidget(new QLabel(tr("Size of the view"), mySizeOfViewWidget));
109 mySizeOfView = new QLineEdit(mySizeOfViewWidget);
111 QDoubleValidator* aValidator = new QDoubleValidator(0, DBL_MAX, 12, mySizeOfView);
112 aValidator->setLocale(ModuleBase_Tools::doubleLocale());
113 aValidator->setNotation(QDoubleValidator::StandardNotation);
114 mySizeOfView->setValidator(aValidator);
115 aSizeLayout->addWidget(mySizeOfView);
117 myPartSetMessage = new QDialog(this, Qt::ToolTip);
118 myPartSetMessage->setModal(false);
119 myPartSetMessage->setStyleSheet("background-color:lightyellow;");
120 QVBoxLayout* aMsgLay = new QVBoxLayout(myPartSetMessage);
121 QString aMsg = tr("The Sketch is created in PartSet.\n"
122 "It will be necessary to create a Part in order to use this sketch for body creation");
123 aMsgLay->addWidget(new QLabel(aMsg, myPartSetMessage));
124 myPartSetMessage->hide();
126 mySizeMessage = new QDialog(mySizeOfView, Qt::ToolTip);
127 mySizeMessage->setModal(false);
128 mySizeMessage->setStyleSheet("background-color:lightyellow;");
129 aMsgLay = new QVBoxLayout(mySizeMessage);
130 aMsg = tr("A size of Sketch view can be defined here.");
131 aMsgLay->addWidget(new QLabel(aMsg, mySizeMessage));
132 mySizeMessage->hide();
134 QString aText = translate(theData->getProperty("title"));
135 QLabel* aLabel = new QLabel(aText, aFirstWgt);
136 aLabel->setWordWrap(true);
137 QString aTooltip = translate(theData->getProperty("tooltip"));
138 aLabel->setToolTip(aTooltip);
139 aLabel->setIndent(5);
141 aLayout = new QVBoxLayout(aFirstWgt);
142 ModuleBase_Tools::zeroMargins(aLayout);
143 aLayout->addWidget(mySizeOfViewWidget);
144 aLayout->addWidget(aLabel);
146 myRemoveExternal = new QCheckBox(tr("Remove external dependencies"), aFirstWgt);
147 myRemoveExternal->setChecked(false);
148 aLayout->addWidget(myRemoveExternal);
149 myRemoveExternal->setVisible(false);
151 aLayout->addStretch(1);
153 myStackWidget->addWidget(aFirstWgt);
155 // Define widget for sketch manmagement
156 QWidget* aSecondWgt = new QWidget(this);
157 aLayout = new QVBoxLayout(aSecondWgt);
158 ModuleBase_Tools::zeroMargins(aLayout);
160 QGroupBox* aViewBox = new QGroupBox(tr("Sketcher plane"), this);
161 QVBoxLayout* aViewLayout = new QVBoxLayout(aViewBox);
163 myViewInverted = new QCheckBox(tr("Reversed"), aViewBox);
164 aViewLayout->addWidget(myViewInverted);
166 QPushButton* aSetViewBtn =
167 new QPushButton(QIcon(":icons/plane_view.png"), tr("Set plane view"), aViewBox);
168 connect(aSetViewBtn, SIGNAL(clicked(bool)), this, SLOT(onSetPlaneView()));
169 aViewLayout->addWidget(aSetViewBtn);
171 aLayout->addWidget(aViewBox);
173 QMap<PartSet_Tools::ConstraintVisibleState, QString> aStates;
174 aStates[PartSet_Tools::Geometrical] = tr("Show geometrical constraints");
175 aStates[PartSet_Tools::Dimensional] = tr("Show dimensional constraints");
176 aStates[PartSet_Tools::Expressions] = tr("Show existing expressions");
178 QMap<PartSet_Tools::ConstraintVisibleState, QString>::const_iterator anIt = aStates.begin(),
179 aLast = aStates.end();
180 for (; anIt != aLast; anIt++) {
181 QCheckBox* aShowConstraints = new QCheckBox(anIt.value(), this);
182 connect(aShowConstraints, SIGNAL(toggled(bool)), this, SLOT(onShowConstraint(bool)));
183 aLayout->addWidget(aShowConstraints);
185 PartSet_Tools::ConstraintVisibleState aState = anIt.key();
186 myShowConstraints[aState] = aShowConstraints;
188 if (toShowConstraints.contains(aState))
189 aShowConstraints->setChecked(toShowConstraints[aState]);
191 myShowPoints = new QCheckBox(tr("Show free points"), this);
192 connect(myShowPoints, SIGNAL(toggled(bool)), this, SIGNAL(showFreePoints(bool)));
193 aLayout->addWidget(myShowPoints);
195 myAutoConstraints = new QCheckBox(tr("Automatic constraints"), this);
196 myAutoConstraints->setToolTip(tr("Automatic vertical and horizontal constraints"));
197 connect(myAutoConstraints, SIGNAL(toggled(bool)), this, SIGNAL(autoConstraints(bool)));
198 aLayout->addWidget(myAutoConstraints);
200 QPushButton* aPlaneBtn = new QPushButton(tr("Change sketch plane"), aSecondWgt);
201 connect(aPlaneBtn, SIGNAL(clicked(bool)), SLOT(onChangePlane()));
202 aLayout->addWidget(aPlaneBtn);
204 aLayout->addSpacing(15);
206 myDoFLabel = new QLabel("", aSecondWgt);
207 aLayout->addWidget(myDoFLabel);
209 myShowDOFBtn = new QPushButton(tr("Show remaining DoFs"), aSecondWgt);
210 aLayout->addWidget(myShowDOFBtn);
211 myShowDOFBtn->setEnabled(false);
212 connect(myShowDOFBtn, SIGNAL(clicked(bool)), SLOT(onShowDOF()));
214 myStackWidget->addWidget(aSecondWgt);
215 //setLayout(aLayout);
217 myPreviewPlanes = new PartSet_PreviewPlanes();
220 PartSet_WidgetSketchLabel::~PartSet_WidgetSketchLabel()
224 bool PartSet_WidgetSketchLabel::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
225 const bool theToValidate)
227 // do not use the given selection if the plane of the sketch has been already set.
228 // If this check is absent, a selected plane in the viewer can be set in the sketch
229 // even if the sketch is built on another plane.
233 ModuleBase_ViewerPrsPtr aPrs = theValues.first();
234 bool aDone = setSelectionInternal(theValues, theToValidate);
236 updateByPlaneSelected(aPrs);
240 QList<QWidget*> PartSet_WidgetSketchLabel::getControls() const
242 QList<QWidget*> aResult;
243 aResult << myStackWidget;
247 bool PartSet_WidgetSketchLabel::processSelection()
249 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
253 QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
255 if (aSelected.empty())
257 ModuleBase_ViewerPrsPtr aPrs = aSelected.first();
258 bool aDone = setSelectionInternal(aSelected, false);
260 updateByPlaneSelected(aPrs);
261 updateObject(myFeature);
267 void PartSet_WidgetSketchLabel::onShowConstraint(bool theOn)
269 QCheckBox* aSenderCheckBox = qobject_cast<QCheckBox*>(sender());
271 QMap<PartSet_Tools::ConstraintVisibleState, QCheckBox*>::const_iterator
272 anIt = myShowConstraints.begin(), aLast = myShowConstraints.end();
274 PartSet_Tools::ConstraintVisibleState aState = PartSet_Tools::Geometrical;
276 for (; anIt != aLast && !aFound; anIt++) {
277 aFound = anIt.value() == aSenderCheckBox;
282 emit showConstraintToggled(aState, theOn);
285 void PartSet_WidgetSketchLabel::blockAttribute(const AttributePtr& theAttribute,
286 const bool& theToBlock, bool& isFlushesActived,
287 bool& isAttributeSetInitializedBlocked,
288 bool& isAttributeSendUpdatedBlocked)
290 ModuleBase_WidgetValidated::blockAttribute(theAttribute, theToBlock, isFlushesActived,
291 isAttributeSetInitializedBlocked,
292 isAttributeSendUpdatedBlocked);
293 // We do not restore the previous state of isAttributeSetInitializedBlocked for each of
294 // attributes. It it is necessary, these states should be append to the method attributes
295 // or stored in the widget
297 std::list<AttributePtr> anAttributes = myFeature->data()->attributes("");
298 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
300 for(; anIt != aLast; anIt++) {
301 AttributePtr anAttribute = *anIt;
303 anAttribute->blockSetInitialized(true);
305 anAttribute->blockSetInitialized(isAttributeSetInitializedBlocked);
309 bool PartSet_WidgetSketchLabel::setSelectionInternal(
310 const QList<ModuleBase_ViewerPrsPtr>& theValues,
311 const bool theToValidate)
314 if (theValues.empty()) {
315 // In order to make reselection possible, set empty object and shape should be done
316 setSelectionCustom(std::shared_ptr<ModuleBase_ViewerPrs>(
317 new ModuleBase_ViewerPrs(ObjectPtr(), GeomShapePtr(), NULL)));
321 // it removes the processed value from the parameters list
322 ModuleBase_ViewerPrsPtr aValue = theValues.first();//.takeFirst();
323 if (!theToValidate || isValidInFilters(aValue)) {
324 myIsSelection = true;
325 aDone = setSelectionCustom(aValue);
326 myIsSelection = false;
333 void PartSet_WidgetSketchLabel::updateByPlaneSelected(const ModuleBase_ViewerPrsPtr& thePrs)
335 // Nullify a temporary remembered plane
336 if (myTmpPlane.get())
339 GeomPlanePtr aPlane = plane();
343 myPartSetMessage->hide();
344 mySizeMessage->hide();
346 // 1. hide main planes if they have been displayed and display sketch preview plane
347 myPreviewPlanes->erasePreviewPlanes(myWorkshop);
349 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
351 CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
352 bool isSetSizeOfView = false;
353 double aSizeOfView = 0;
354 QString aSizeOfViewStr = mySizeOfView->text();
355 if (!aSizeOfViewStr.isEmpty()) {
356 aSizeOfView = aSizeOfViewStr.toDouble(&isSetSizeOfView);
357 if (isSetSizeOfView && aSizeOfView <= 0) {
358 isSetSizeOfView = false;
361 aModule->sketchMgr()->previewSketchPlane()->setSizeOfView(aSizeOfView, isSetSizeOfView);
362 aModule->sketchMgr()->previewSketchPlane()->createSketchPlane(aSketch, myWorkshop);
364 // 2. if the planes were displayed, change the view projection
366 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
367 gp_XYZ aXYZ = aDir->impl<gp_Dir>().XYZ();
370 // Rotate view if the sketcher plane is selected only from preview planes
371 // Preview planes are created only if there is no any shape
372 bool aRotate = Config_PropManager::boolean(SKETCH_TAB_NAME, "rotate_to_plane");
374 myWorkshop->viewer()->setViewProjection(aXYZ.X(), aXYZ.Y(), aXYZ.Z(), aTwist);
376 QString aSizeOfViewStr = mySizeOfView->text();
377 if (!aSizeOfViewStr.isEmpty()) {
379 double aSizeOfView = aSizeOfViewStr.toDouble(&isOk);
380 if (isOk && aSizeOfView > 0) {
381 Handle(V3d_View) aView3d = myWorkshop->viewer()->activeView();
382 if (!aView3d.IsNull()) {
384 double aHalfSize = aSizeOfView/2.0;
385 aBndBox.Update(-aHalfSize, -aHalfSize, -aHalfSize, aHalfSize, aHalfSize, aHalfSize);
386 aView3d->FitAll(aBndBox, 0.01, false);
390 if (myOpenTransaction) {
391 SessionPtr aMgr = ModelAPI_Session::get();
392 aMgr->finishOperation();
393 myOpenTransaction = false;
395 // 3. Clear text in the label
396 myStackWidget->setCurrentIndex(1);
397 //myLabel->setText("");
398 //myLabel->setToolTip("");
399 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
401 // 5. Clear selection mode and define sketching mode
402 emit planeSelected(plane());
403 // after the plane is selected in the sketch, the sketch selection should be activated
404 // it can not be performed in the sketch label widget because, we don't need to switch off
405 // the selection by any label deactivation, but need to switch it off by stop the sketch
406 myWorkshop->selectionActivate()->updateSelectionFilters();
407 myWorkshop->selectionActivate()->updateSelectionModes();
410 aModule->onViewTransformed();
412 // 6. Update sketcher actions
413 XGUI_ActionsMgr* anActMgr = aWorkshop->actionsMgr();
415 myWorkshop->updateCommandStatus();
416 aWorkshop->selector()->clearSelection();
417 myWorkshop->viewer()->update();
419 myRemoveExternal->setVisible(false);
422 std::shared_ptr<GeomAPI_Pln> PartSet_WidgetSketchLabel::plane() const
424 CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
425 return PartSet_Tools::sketchPlane(aSketch);
428 bool PartSet_WidgetSketchLabel::focusTo()
430 ModuleBase_Tools::setFocus(myStackWidget, "PartSet_WidgetSketchLabel::focusTo()");
434 void PartSet_WidgetSketchLabel::enableFocusProcessing()
436 myStackWidget->installEventFilter(this);
439 void PartSet_WidgetSketchLabel::storeAttributeValue(const AttributePtr& theAttribute)
441 ModuleBase_WidgetValidated::storeAttributeValue(theAttribute);
444 void PartSet_WidgetSketchLabel::restoreAttributeValue(const AttributePtr& theAttribute,
447 ModuleBase_WidgetValidated::restoreAttributeValue(theAttribute, theValid);
449 // it is not necessary to save the previous plane value because the plane is chosen once
450 DataPtr aData = feature()->data();
451 AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
452 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
454 ResultPtr anEmptyResult;
455 GeomShapePtr anEmptyShape;
456 aSelAttr->setValue(anEmptyResult, anEmptyShape);
458 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
459 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
461 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
462 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
464 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
465 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
469 bool PartSet_WidgetSketchLabel::setSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
471 if (myIsSelection && myRemoveExternal->isVisible()) {
472 if (myRemoveExternal->isChecked()) {
473 myFeature->customAction(SketchPlugin_Sketch::ACTION_REMOVE_EXTERNAL());
476 return fillSketchPlaneBySelection(thePrs);
479 bool PartSet_WidgetSketchLabel::canFillSketch(const ModuleBase_ViewerPrsPtr& thePrs)
481 bool aCanFillSketch = true;
482 // avoid any selection on sketch object
483 ObjectPtr anObject = thePrs->object();
484 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
486 FeaturePtr aFeature = ModelAPI_Feature::feature(aResult);
487 if (aFeature->getKind() == SketchPlugin_Sketch::ID())
488 aCanFillSketch = false;
490 // check plane or planar face of any non-sketch object
491 if (aCanFillSketch) {
492 std::shared_ptr<GeomAPI_Face> aGeomFace;
494 GeomShapePtr aGeomShape = thePrs->shape();
495 if ((!aGeomShape.get() || aGeomShape->isNull()) && aResult.get()) {
496 aGeomShape = aResult->shape();
499 if (aGeomShape.get() && aGeomShape->shapeType() == GeomAPI_Shape::FACE) {
500 std::shared_ptr<GeomAPI_Face> aGeomFace(new GeomAPI_Face(aGeomShape));
501 aCanFillSketch = aGeomFace.get() && aGeomFace->isPlanar();
504 aCanFillSketch = false;
506 return aCanFillSketch;
509 bool PartSet_WidgetSketchLabel::fillSketchPlaneBySelection(const ModuleBase_ViewerPrsPtr& thePrs)
511 bool isOwnerSet = false;
513 const GeomShapePtr& aShape = thePrs->shape();
514 std::shared_ptr<GeomAPI_Dir> aDir;
516 if (aShape.get() && !aShape->isNull()) {
517 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
518 aDir = setSketchPlane(aTDShape);
519 isOwnerSet = aDir.get();
521 if (thePrs->object() && (feature() != thePrs->object())) {
522 FeaturePtr aFeature = ModelAPI_Feature::feature(thePrs->object());
523 DataPtr aData = feature()->data();
524 AttributeSelectionPtr aSelAttr =
525 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
526 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
527 if (aSelAttr.get()) {
528 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(thePrs->object());
530 GeomShapePtr aShapePtr;
531 if (!aShape.get() || aShape->isNull()) { // selection happens in the OCC viewer
532 aShapePtr = ModelAPI_Tools::shape(aRes);
534 else { // selection happens in OB browser
537 if (aShapePtr.get() && aShapePtr->isFace()) {
538 const TopoDS_Shape& aTDShape = aShapePtr->impl<TopoDS_Shape>();
539 setSketchPlane(aTDShape);
540 aSelAttr->setValue(aRes, aShapePtr);
545 aSelAttr->setValue(aFeature, GeomShapePtr());
546 GeomShapePtr aShape = aSelAttr->value();
547 if (!aShape.get() && aSelAttr->contextFeature().get() &&
548 aSelAttr->contextFeature()->firstResult().get()) {
549 aShape = aSelAttr->contextFeature()->firstResult()->shape();
551 if (aShape.get() && aShape->isPlanar()) {
552 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
553 setSketchPlane(aTDShape);
562 void PartSet_WidgetSketchLabel::activateCustom()
564 QWidget* aTopWidget = window();
566 aTopWidget->installEventFilter(this);
568 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
570 bool isBlocked = myAutoConstraints->blockSignals(true);
571 myAutoConstraints->setChecked(aModule->sketchReentranceMgr()->isAutoConstraints());
572 myAutoConstraints->blockSignals(isBlocked);
575 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
577 myStackWidget->setCurrentIndex(1);
581 myStackWidget->setCurrentIndex(0);
582 bool aBodyIsVisualized = myPreviewPlanes->hasVisualizedBodies(myWorkshop);
584 // Clear previous selection mode It is necessary for correct activation of preview planes
585 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
586 XGUI_Displayer* aDisp = aWorkshop->displayer();
587 aWorkshop->selectionActivate()->activateObjects(QIntList(), aDisp->displayedObjects(), false);
589 if (!aBodyIsVisualized) {
590 // We have to select a plane before any operation
591 myPreviewPlanes->showPreviewPlanes(myWorkshop);
592 mySizeOfViewWidget->setVisible(true);
595 mySizeOfViewWidget->setVisible(false);
598 void PartSet_WidgetSketchLabel::showEvent(QShowEvent* theEvent)
600 ModuleBase_WidgetValidated::showEvent(theEvent);
601 QTimer::singleShot(10, this, SLOT(onShowPanel()));
604 void PartSet_WidgetSketchLabel::hideEvent(QHideEvent* theEvent)
606 ModuleBase_WidgetValidated::hideEvent(theEvent);
607 if (myPartSetMessage->isVisible())
608 myPartSetMessage->hide();
609 if (mySizeMessage->isVisible())
610 mySizeMessage->hide();
614 void PartSet_WidgetSketchLabel::onShowPanel()
616 //if (mySizeOfViewWidget->isVisible()) {
617 if (myStackWidget->currentIndex() == 0) {
618 DocumentPtr aDoc = feature()->document();
619 DocumentPtr aModDoc = ModelAPI_Session::get()->moduleDocument();
620 if (aModDoc == aDoc) {
621 myPartSetMessage->move(mapToGlobal(geometry().bottomLeft()));
622 myPartSetMessage->show();
624 if (mySizeOfViewWidget->isVisible()) {
625 QPoint aPnt = mySizeOfView->mapToGlobal(mySizeOfView->geometry().center());
626 mySizeMessage->move(aPnt);
627 mySizeMessage->show();
632 void PartSet_WidgetSketchLabel::deactivate()
634 QWidget* aTopWidget = window();
636 aTopWidget->removeEventFilter(this);
638 if (myTmpPlane.get()) {
639 setSketchPlane(myTmpPlane);
643 ModuleBase_WidgetValidated::deactivate();
644 bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
645 myPreviewPlanes->erasePreviewPlanes(myWorkshop);
648 myWorkshop->viewer()->update();
650 if (myOpenTransaction) {
651 SessionPtr aMgr = ModelAPI_Session::get();
652 aMgr->finishOperation();
653 myOpenTransaction = false;
657 void PartSet_WidgetSketchLabel::selectionModes(int& theModuleSelectionModes, QIntList& theModes)
659 theModuleSelectionModes = -1;
660 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
662 theModes << TopAbs_FACE;
665 void PartSet_WidgetSketchLabel::selectionFilters(QIntList& theModuleSelectionFilters,
666 SelectMgr_ListOfFilter& theSelectionFilters)
668 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
671 return ModuleBase_WidgetValidated::selectionFilters(theModuleSelectionFilters,
672 theSelectionFilters);
675 std::shared_ptr<GeomAPI_Dir>
676 PartSet_WidgetSketchLabel::setSketchPlane(const TopoDS_Shape& theShape)
678 if (theShape.IsNull())
679 return std::shared_ptr<GeomAPI_Dir>();
681 // get selected shape
682 std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
683 aGShape->setImpl(new TopoDS_Shape(theShape));
685 // get plane parameters
686 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aGShape));
687 std::shared_ptr<GeomAPI_Pln> aPlane = aFace->getPlane();
689 return std::shared_ptr<GeomAPI_Dir>();
690 return setSketchPlane(aPlane);
693 std::shared_ptr<GeomAPI_Dir>
694 PartSet_WidgetSketchLabel::setSketchPlane(std::shared_ptr<GeomAPI_Pln> thePlane)
696 // set plane parameters to feature
697 std::shared_ptr<ModelAPI_Data> aData = feature()->data();
698 double anA, aB, aC, aD;
699 thePlane->coefficients(anA, aB, aC, aD);
701 // calculate attributes of the sketch
702 std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
703 std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
704 std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
705 aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
706 std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
707 // X axis is preferable to be dirX on the sketch
708 const double tol = Precision::Confusion();
709 bool isX = fabs(fabs(anA) - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
710 std::shared_ptr<GeomAPI_Dir> aTempDir(
711 isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
712 std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
713 std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
715 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
716 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
717 anOrigin->setValue(anOrigPnt);
718 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
719 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
720 aNormal->setValue(aNormDir);
721 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
722 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
723 aDirX->setValue(aXDir);
724 std::shared_ptr<GeomAPI_Dir> aDir = thePlane->direction();
728 void PartSet_WidgetSketchLabel::onSetPlaneView()
730 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
732 std::shared_ptr<GeomAPI_Dir> aDirection = aPlane->direction();
733 gp_Dir aDir = aDirection->impl<gp_Dir>();
734 if (myViewInverted->isChecked())
736 myWorkshop->viewer()->setViewProjection(aDir.X(), aDir.Y(), aDir.Z(), 0.);
737 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
739 aModule->onViewTransformed();
744 //******************************************************
745 QList<std::shared_ptr<ModuleBase_ViewerPrs>> PartSet_WidgetSketchLabel::findCircularEdgesInPlane()
747 QList<std::shared_ptr<ModuleBase_ViewerPrs>> aResult;
748 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
749 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
750 QObjectPtrList aDispObjects = aDisplayer->displayedObjects();
752 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
753 foreach(ObjectPtr aObj, aDispObjects) {
754 ResultPtr aResObj = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
756 GeomShapePtr aShape = aResObj->shape();
758 GeomAPI_ShapeExplorer aExplorer(aShape, GeomAPI_Shape::EDGE);
759 for(; aExplorer.more(); aExplorer.next()) {
760 GeomShapePtr aEdgeShape = aExplorer.current();
761 GeomAPI_Edge anEdge(aEdgeShape);
762 if ((anEdge.isCircle() || anEdge.isArc() || anEdge.isEllipse()) &&
763 anEdge.isInPlane(aPlane)) {
764 bool isContains = false;
765 // Check that edge is not used.
766 // It is possible that the same edge will be taken from different faces
767 foreach(std::shared_ptr<ModuleBase_ViewerPrs> aPrs, aResult) {
768 GeomAPI_Edge aUsedEdge(aPrs->shape());
769 if (aUsedEdge.isEqual(aEdgeShape)) {
775 std::shared_ptr<ModuleBase_ViewerPrs>
776 aPrs(new ModuleBase_ViewerPrs(aResObj, aEdgeShape));
777 aResult.append(aPrs);
787 //******************************************************
788 void PartSet_WidgetSketchLabel::onChangePlane()
790 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
792 mySizeOfViewWidget->setVisible(false);
793 myRemoveExternal->setVisible(true);
794 myStackWidget->setCurrentIndex(0);
796 bool aBodyIsVisualized = myPreviewPlanes->hasVisualizedBodies(myWorkshop);
798 if (!aBodyIsVisualized) {
799 // We have to select a plane before any operation
800 myPreviewPlanes->showPreviewPlanes(myWorkshop);
803 CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
804 myTmpPlane = PartSet_Tools::sketchPlane(aSketch);
805 PartSet_Tools::nullifySketchPlane(aSketch);
807 Handle(SelectMgr_Filter) aFilter = aModule->selectionFilter(SF_SketchPlaneFilter);
808 if (!aFilter.IsNull()) {
809 std::shared_ptr<GeomAPI_Pln> aPln;
810 Handle(ModuleBase_ShapeInPlaneFilter)::DownCast(aFilter)->setPlane(aPln);
812 XGUI_Workshop* aWorkshop = aModule->getWorkshop();
814 aWorkshop->selectionActivate()->updateSelectionFilters();
815 aWorkshop->selectionActivate()->updateSelectionModes();
817 SessionPtr aMgr = ModelAPI_Session::get();
818 aMgr->startOperation("Change Sketch plane");
819 myOpenTransaction = true;
820 myWorkshop->viewer()->update();
824 void PartSet_WidgetSketchLabel::setShowPointsState(bool theState)
826 bool aBlock = myShowPoints->blockSignals(true);
827 myShowPoints->setChecked(theState);
828 myShowPoints->blockSignals(aBlock);
831 bool PartSet_WidgetSketchLabel::restoreValueCustom()
833 if (myFeature.get()) {
834 CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
835 if (aSketch.get() && (aSketch->numberOfSubs() > 0)) {
836 AttributeStringPtr aDOFStr = aSketch->string("SolverDOF");
838 QString aVal(aDOFStr->value().c_str());
839 if (aVal.contains('=')) {
840 // to support old data
841 aVal = aVal.right(aVal.length() - aVal.lastIndexOf('='));
843 int aDoF = aVal.toInt();
845 myDoFLabel->setText(tr("Sketch is fully fixed (DoF = 0)"));
846 myShowDOFBtn->setEnabled(false);
848 myDoFLabel->setText(tr("DoF (degrees of freedom) = ") + aVal);
849 myShowDOFBtn->setEnabled(true);
854 myDoFLabel->setText("");
855 myShowDOFBtn->setEnabled(false);
862 void PartSet_WidgetSketchLabel::onShowDOF()
864 CompositeFeaturePtr aCompFeature =
865 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
866 if (aCompFeature.get()) {
867 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_GET_DOF_OBJECTS);
868 ModelAPI_EventCreator::get()->sendUpdated(aCompFeature, anEvent);
869 Events_Loop::loop()->flush(anEvent);
870 QWidget* aViewPort = myWorkshop->viewer()->activeViewPort();
872 aViewPort->setFocus();
876 bool PartSet_WidgetSketchLabel::eventFilter(QObject* theObj, QEvent* theEvent)
878 if (theObj == window()) {
879 int aType = theEvent->type();
880 if ((aType == QEvent::Hide) || (aType == QEvent::WindowDeactivate)) {
881 if (myPartSetMessage->isVisible())
882 myPartSetMessage->hide();
883 if (mySizeMessage->isVisible())
884 mySizeMessage->hide();
886 else if ((aType == QEvent::Show) || (aType == QEvent::WindowActivate))
889 return ModuleBase_WidgetValidated::eventFilter(theObj, theEvent);