1 // Copyright (C) 2014-2021 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 <Bnd_Box.hxx>
68 #include <Config_WidgetAPI.h>
69 #include <Config_PropManager.h>
72 #include <QApplication>
73 #include <QVBoxLayout>
74 #include <QHBoxLayout>
77 #include <QPushButton>
79 #include <QDoubleValidator>
84 #pragma warning(disable : 4456) // for nested foreach
88 #define DBL_MAX 1.7976931348623158e+308
91 PartSet_WidgetSketchLabel::PartSet_WidgetSketchLabel(QWidget* theParent,
92 ModuleBase_IWorkshop* theWorkshop,
93 const Config_WidgetAPI* theData,
94 const QMap<PartSet_Tools::ConstraintVisibleState, bool>& toShowConstraints)
95 : ModuleBase_WidgetValidated(theParent, theWorkshop, theData), myOpenTransaction(false),
98 QVBoxLayout* aLayout = new QVBoxLayout(this);
99 ModuleBase_Tools::zeroMargins(aLayout);
101 myStackWidget = new QStackedWidget(this);
102 myStackWidget->setContentsMargins(0,0,0,0);
103 aLayout->addWidget(myStackWidget);
105 // Define label for plane selection
106 QWidget* aFirstWgt = new QWidget(this);
108 // Size of the View control
109 mySizeOfViewWidget = new QWidget(aFirstWgt);
110 QHBoxLayout* aSizeLayout = new QHBoxLayout(mySizeOfViewWidget);
111 aSizeLayout->addWidget(new QLabel(tr("Size of the view"), mySizeOfViewWidget));
112 mySizeOfView = new QLineEdit(mySizeOfViewWidget);
114 QDoubleValidator* aValidator = new QDoubleValidator(0, DBL_MAX, 12, mySizeOfView);
115 aValidator->setLocale(ModuleBase_Tools::doubleLocale());
116 aValidator->setNotation(QDoubleValidator::StandardNotation);
117 mySizeOfView->setValidator(aValidator);
118 aSizeLayout->addWidget(mySizeOfView);
120 myPartSetMessage = new QDialog(this, Qt::ToolTip);
121 myPartSetMessage->setModal(false);
122 myPartSetMessage->setStyleSheet("background-color:lightyellow;");
123 QVBoxLayout* aMsgLay = new QVBoxLayout(myPartSetMessage);
124 QString aMsg = tr("The Sketch is created in PartSet.\n"
125 "It will be necessary to create a Part in order to use this sketch for body creation");
126 aMsgLay->addWidget(new QLabel(aMsg, myPartSetMessage));
127 myPartSetMessage->hide();
129 mySizeMessage = new QDialog(mySizeOfView, Qt::ToolTip);
130 mySizeMessage->setModal(false);
131 mySizeMessage->setStyleSheet("background-color:lightyellow;");
132 aMsgLay = new QVBoxLayout(mySizeMessage);
133 aMsg = tr("A size of Sketch view can be defined here.");
134 aMsgLay->addWidget(new QLabel(aMsg, mySizeMessage));
135 mySizeMessage->hide();
137 QString aText = translate(theData->getProperty("title"));
138 QLabel* aLabel = new QLabel(aText, aFirstWgt);
139 aLabel->setWordWrap(true);
140 QString aTooltip = translate(theData->getProperty("tooltip"));
141 aLabel->setToolTip(aTooltip);
142 aLabel->setIndent(5);
144 aLayout = new QVBoxLayout(aFirstWgt);
145 ModuleBase_Tools::zeroMargins(aLayout);
146 aLayout->addWidget(mySizeOfViewWidget);
147 aLayout->addWidget(aLabel);
149 myRemoveExternal = new QCheckBox(tr("Remove external dependencies"), aFirstWgt);
150 myRemoveExternal->setChecked(false);
151 aLayout->addWidget(myRemoveExternal);
152 myRemoveExternal->setVisible(false);
154 aLayout->addStretch(1);
156 myStackWidget->addWidget(aFirstWgt);
158 // Define widget for sketch manmagement
159 QWidget* aSecondWgt = new QWidget(this);
160 aLayout = new QVBoxLayout(aSecondWgt);
161 ModuleBase_Tools::zeroMargins(aLayout);
163 QGroupBox* aViewBox = new QGroupBox(tr("Sketcher plane"), this);
164 QGridLayout* aViewLayout = new QGridLayout(aViewBox);
166 myViewInverted = new QCheckBox(tr("Reversed"), aViewBox);
167 aViewLayout->addWidget(myViewInverted, 0, 0);
169 myViewVisible = new QCheckBox(tr("Visible"), aViewBox);
170 myViewVisible->setChecked(true);
171 aViewLayout->addWidget(myViewVisible, 0, 1, Qt::AlignRight);
172 connect(myViewVisible, SIGNAL(toggled(bool)), this, SLOT(onShowViewPlane(bool)));
174 QPushButton* aSetViewBtn =
175 new QPushButton(QIcon(":icons/plane_view.png"), tr("Set plane view"), aViewBox);
176 connect(aSetViewBtn, SIGNAL(clicked(bool)), this, SLOT(onSetPlaneView()));
177 aViewLayout->addWidget(aSetViewBtn, 1, 0, 1, 2);
179 aLayout->addWidget(aViewBox);
181 QMap<PartSet_Tools::ConstraintVisibleState, QString> aStates;
182 aStates[PartSet_Tools::Geometrical] = tr("Show geometrical constraints");
183 aStates[PartSet_Tools::Dimensional] = tr("Show dimensional constraints");
184 aStates[PartSet_Tools::Expressions] = tr("Show existing expressions");
186 QMap<PartSet_Tools::ConstraintVisibleState, QString>::const_iterator anIt = aStates.begin(),
187 aLast = aStates.end();
188 for (; anIt != aLast; anIt++) {
189 QCheckBox* aShowConstraints = new QCheckBox(anIt.value(), this);
190 connect(aShowConstraints, SIGNAL(toggled(bool)), this, SLOT(onShowConstraint(bool)));
191 aLayout->addWidget(aShowConstraints);
193 PartSet_Tools::ConstraintVisibleState aState = anIt.key();
194 myShowConstraints[aState] = aShowConstraints;
196 if (toShowConstraints.contains(aState))
197 aShowConstraints->setChecked(toShowConstraints[aState]);
199 myShowPoints = new QCheckBox(tr("Show free points"), this);
200 connect(myShowPoints, SIGNAL(toggled(bool)), this, SIGNAL(showFreePoints(bool)));
201 aLayout->addWidget(myShowPoints);
203 myAutoConstraints = new QCheckBox(tr("Automatic constraints"), this);
204 myAutoConstraints->setToolTip(tr("Automatic vertical and horizontal constraints"));
205 connect(myAutoConstraints, SIGNAL(toggled(bool)), this, SIGNAL(autoConstraints(bool)));
206 aLayout->addWidget(myAutoConstraints);
208 QPushButton* aPlaneBtn = new QPushButton(tr("Change sketch plane"), aSecondWgt);
209 connect(aPlaneBtn, SIGNAL(clicked(bool)), SLOT(onChangePlane()));
210 aLayout->addWidget(aPlaneBtn);
212 aLayout->addSpacing(15);
214 myDoFLabel = new QLabel("", aSecondWgt);
215 aLayout->addWidget(myDoFLabel);
217 myShowDOFBtn = new QPushButton(tr("Show remaining DoFs"), aSecondWgt);
218 aLayout->addWidget(myShowDOFBtn);
219 myShowDOFBtn->setEnabled(false);
220 connect(myShowDOFBtn, SIGNAL(clicked(bool)), SLOT(onShowDOF()));
222 myStackWidget->addWidget(aSecondWgt);
223 //setLayout(aLayout);
225 myPreviewPlanes = new PartSet_PreviewPlanes();
228 PartSet_WidgetSketchLabel::~PartSet_WidgetSketchLabel()
232 bool PartSet_WidgetSketchLabel::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
233 const bool theToValidate)
235 // do not use the given selection if the plane of the sketch has been already set.
236 // If this check is absent, a selected plane in the viewer can be set in the sketch
237 // even if the sketch is built on another plane.
241 ModuleBase_ViewerPrsPtr aPrs = theValues.first();
242 bool aDone = setSelectionInternal(theValues, theToValidate);
244 updateByPlaneSelected(aPrs);
248 QList<QWidget*> PartSet_WidgetSketchLabel::getControls() const
250 QList<QWidget*> aResult;
251 aResult << myStackWidget;
255 bool PartSet_WidgetSketchLabel::processSelection()
257 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
261 QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
263 if (aSelected.empty())
265 ModuleBase_ViewerPrsPtr aPrs = aSelected.first();
266 bool aDone = setSelectionInternal(aSelected, false);
268 updateByPlaneSelected(aPrs);
269 updateObject(myFeature);
275 void PartSet_WidgetSketchLabel::onShowConstraint(bool theOn)
277 QCheckBox* aSenderCheckBox = qobject_cast<QCheckBox*>(sender());
279 QMap<PartSet_Tools::ConstraintVisibleState, QCheckBox*>::const_iterator
280 anIt = myShowConstraints.begin(), aLast = myShowConstraints.end();
282 PartSet_Tools::ConstraintVisibleState aState = PartSet_Tools::Geometrical;
284 for (; anIt != aLast && !aFound; anIt++) {
285 aFound = anIt.value() == aSenderCheckBox;
290 emit showConstraintToggled(aState, theOn);
293 void PartSet_WidgetSketchLabel::blockAttribute(const AttributePtr& theAttribute,
294 const bool& theToBlock, bool& isFlushesActived,
295 bool& isAttributeSetInitializedBlocked,
296 bool& isAttributeSendUpdatedBlocked)
298 ModuleBase_WidgetValidated::blockAttribute(theAttribute, theToBlock, isFlushesActived,
299 isAttributeSetInitializedBlocked,
300 isAttributeSendUpdatedBlocked);
301 // We do not restore the previous state of isAttributeSetInitializedBlocked for each of
302 // attributes. It it is necessary, these states should be append to the method attributes
303 // or stored in the widget
305 std::list<AttributePtr> anAttributes = myFeature->data()->attributes("");
306 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
308 for(; anIt != aLast; anIt++) {
309 AttributePtr anAttribute = *anIt;
311 anAttribute->blockSetInitialized(true);
313 anAttribute->blockSetInitialized(isAttributeSetInitializedBlocked);
317 bool PartSet_WidgetSketchLabel::setSelectionInternal(
318 const QList<ModuleBase_ViewerPrsPtr>& theValues,
319 const bool theToValidate)
322 if (theValues.empty()) {
323 // In order to make reselection possible, set empty object and shape should be done
324 setSelectionCustom(ModuleBase_ViewerPrsPtr(
325 new ModuleBase_ViewerPrs(ObjectPtr(), GeomShapePtr(), NULL)));
329 // it removes the processed value from the parameters list
330 ModuleBase_ViewerPrsPtr aValue = theValues.first();//.takeFirst();
331 if (!theToValidate || isValidInFilters(aValue)) {
332 myIsSelection = true;
333 aDone = setSelectionCustom(aValue);
334 myIsSelection = false;
341 void PartSet_WidgetSketchLabel::updateByPlaneSelected(const ModuleBase_ViewerPrsPtr& thePrs)
343 // Nullify a temporary remembered plane
344 if (myTmpPlane.get())
347 GeomPlanePtr aPlane = plane();
351 myPartSetMessage->hide();
352 mySizeMessage->hide();
354 // 1. hide main planes if they have been displayed and display sketch preview plane
355 myPreviewPlanes->erasePreviewPlanes(myWorkshop);
357 QString aSizeOfViewStr = mySizeOfView->text();
358 bool isSetSizeOfView = false;
359 double aSizeOfView = 0;
360 if (!aSizeOfViewStr.isEmpty()) {
361 aSizeOfView = aSizeOfViewStr.toDouble(&isSetSizeOfView);
362 if (isSetSizeOfView && aSizeOfView <= 0) {
363 isSetSizeOfView = false;
366 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
368 CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
369 aModule->sketchMgr()->previewSketchPlane()->setSizeOfView(aSizeOfView, isSetSizeOfView);
370 if (myViewVisible->isChecked())
371 aModule->sketchMgr()->previewSketchPlane()->createSketchPlane(aSketch, myWorkshop);
373 aModule->sketchMgr()->previewSketchPlane()->clearPlanePreview();
375 // 2. if the planes were displayed, change the view projection
377 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
378 gp_XYZ aXYZ = aDir->impl<gp_Dir>().XYZ();
381 // Rotate view if the sketcher plane is selected only from preview planes
382 // Preview planes are created only if there is no any shape
383 bool aRotate = Config_PropManager::boolean(SKETCH_TAB_NAME, "rotate_to_plane");
385 myWorkshop->viewer()->setViewProjection(aXYZ.X(), aXYZ.Y(), aXYZ.Z(), aTwist);
387 if (isSetSizeOfView && aSizeOfView > 0) {
388 Handle(V3d_View) aView3d = myWorkshop->viewer()->activeView();
389 if (!aView3d.IsNull()) {
391 double aHalfSize = aSizeOfView/2.0;
392 aBndBox.Update(-aHalfSize, -aHalfSize, -aHalfSize, aHalfSize, aHalfSize, aHalfSize);
393 aView3d->FitAll(aBndBox, 0.01, false);
396 if (myOpenTransaction) {
397 SessionPtr aMgr = ModelAPI_Session::get();
398 aMgr->finishOperation();
399 myOpenTransaction = false;
401 // 3. Clear text in the label
402 myStackWidget->setCurrentIndex(1);
403 //myLabel->setText("");
404 //myLabel->setToolTip("");
405 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
407 // 5. Clear selection mode and define sketching mode
408 emit planeSelected(plane());
409 // after the plane is selected in the sketch, the sketch selection should be activated
410 // it can not be performed in the sketch label widget because, we don't need to switch off
411 // the selection by any label deactivation, but need to switch it off by stop the sketch
412 myWorkshop->selectionActivate()->updateSelectionFilters();
413 myWorkshop->selectionActivate()->updateSelectionModes();
416 aModule->onViewTransformed();
418 myWorkshop->updateCommandStatus();
419 aWorkshop->selector()->clearSelection();
420 myWorkshop->viewer()->update();
422 myRemoveExternal->setVisible(false);
425 std::shared_ptr<GeomAPI_Pln> PartSet_WidgetSketchLabel::plane() const
427 CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
428 return PartSet_Tools::sketchPlane(aSketch);
431 bool PartSet_WidgetSketchLabel::focusTo()
433 ModuleBase_Tools::setFocus(myStackWidget, "PartSet_WidgetSketchLabel::focusTo()");
437 void PartSet_WidgetSketchLabel::enableFocusProcessing()
439 myStackWidget->installEventFilter(this);
442 void PartSet_WidgetSketchLabel::storeAttributeValue(const AttributePtr& theAttribute)
444 ModuleBase_WidgetValidated::storeAttributeValue(theAttribute);
447 void PartSet_WidgetSketchLabel::restoreAttributeValue(const AttributePtr& theAttribute,
450 ModuleBase_WidgetValidated::restoreAttributeValue(theAttribute, theValid);
452 // it is not necessary to save the previous plane value because the plane is chosen once
453 DataPtr aData = feature()->data();
454 AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
455 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
457 ResultPtr anEmptyResult;
458 GeomShapePtr anEmptyShape;
459 aSelAttr->setValue(anEmptyResult, anEmptyShape);
461 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
462 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
464 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
465 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
467 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
468 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
472 bool PartSet_WidgetSketchLabel::setSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
474 if (myIsSelection && myRemoveExternal->isVisible()) {
475 if (myRemoveExternal->isChecked()) {
476 myFeature->customAction(SketchPlugin_Sketch::ACTION_REMOVE_EXTERNAL());
479 return fillSketchPlaneBySelection(thePrs);
482 bool PartSet_WidgetSketchLabel::canFillSketch(const ModuleBase_ViewerPrsPtr& thePrs)
484 bool aCanFillSketch = true;
485 // avoid any selection on sketch object
486 ObjectPtr anObject = thePrs->object();
487 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
489 FeaturePtr aFeature = ModelAPI_Feature::feature(aResult);
490 if (aFeature->getKind() == SketchPlugin_Sketch::ID())
491 aCanFillSketch = false;
493 // check plane or planar face of any non-sketch object
494 if (aCanFillSketch) {
495 GeomShapePtr aGeomShape = thePrs->shape();
496 if ((!aGeomShape.get() || aGeomShape->isNull()) && aResult.get()) {
497 aGeomShape = aResult->shape();
500 if (aGeomShape.get() && aGeomShape->shapeType() == GeomAPI_Shape::FACE) {
501 std::shared_ptr<GeomAPI_Face> aGeomFace(new GeomAPI_Face(aGeomShape));
502 aCanFillSketch = aGeomFace.get() && aGeomFace->isPlanar();
505 aCanFillSketch = false;
507 return aCanFillSketch;
510 bool PartSet_WidgetSketchLabel::fillSketchPlaneBySelection(const ModuleBase_ViewerPrsPtr& thePrs)
512 bool isOwnerSet = false;
514 const GeomShapePtr& aShape = thePrs->shape();
515 std::shared_ptr<GeomAPI_Dir> aDir;
517 if (aShape.get() && !aShape->isNull()) {
518 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
519 aDir = setSketchPlane(aTDShape);
520 isOwnerSet = aDir.get();
522 if (thePrs->object() && (feature() != thePrs->object())) {
523 FeaturePtr aFeature = ModelAPI_Feature::feature(thePrs->object());
524 DataPtr aData = feature()->data();
525 AttributeSelectionPtr aSelAttr =
526 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
527 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
528 if (aSelAttr.get()) {
529 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(thePrs->object());
531 GeomShapePtr aShapePtr;
532 if (!aShape.get() || aShape->isNull()) { // selection happens in the OCC viewer
533 aShapePtr = ModelAPI_Tools::shape(aRes);
535 else { // selection happens in OB browser
538 if (aShapePtr.get() && aShapePtr->isFace()) {
539 const TopoDS_Shape& aTDShape = aShapePtr->impl<TopoDS_Shape>();
540 setSketchPlane(aTDShape);
541 aSelAttr->setValue(aRes, aShapePtr);
546 aSelAttr->setValue(aFeature, GeomShapePtr());
547 GeomShapePtr aSelShape = aSelAttr->value();
548 if (!aSelShape.get() && aSelAttr->contextFeature().get() &&
549 aSelAttr->contextFeature()->firstResult().get()) {
550 aSelShape = aSelAttr->contextFeature()->firstResult()->shape();
552 if (aSelShape.get() && aSelShape->isPlanar()) {
553 const TopoDS_Shape& aTDShape = aSelShape->impl<TopoDS_Shape>();
554 setSketchPlane(aTDShape);
563 void PartSet_WidgetSketchLabel::activateCustom()
565 QWidget* aTopWidget = window();
567 aTopWidget->installEventFilter(this);
569 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
571 bool isBlocked = myAutoConstraints->blockSignals(true);
572 myAutoConstraints->setChecked(aModule->sketchReentranceMgr()->isAutoConstraints());
573 myAutoConstraints->blockSignals(isBlocked);
576 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
578 myStackWidget->setCurrentIndex(1);
582 myStackWidget->setCurrentIndex(0);
583 bool aBodyIsVisualized = myPreviewPlanes->hasVisualizedBodies(myWorkshop);
585 // Clear previous selection mode It is necessary for correct activation of preview planes
586 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
587 XGUI_Displayer* aDisp = aWorkshop->displayer();
588 aWorkshop->selectionActivate()->activateObjects(QIntList(), aDisp->displayedObjects(), false);
590 if (!aBodyIsVisualized) {
591 // We have to select a plane before any operation
592 myPreviewPlanes->showPreviewPlanes(myWorkshop);
593 mySizeOfViewWidget->setVisible(true);
596 mySizeOfViewWidget->setVisible(false);
599 void PartSet_WidgetSketchLabel::showEvent(QShowEvent* theEvent)
601 ModuleBase_WidgetValidated::showEvent(theEvent);
602 QTimer::singleShot(10, this, SLOT(onShowPanel()));
605 void PartSet_WidgetSketchLabel::hideEvent(QHideEvent* theEvent)
607 ModuleBase_WidgetValidated::hideEvent(theEvent);
608 if (myPartSetMessage->isVisible())
609 myPartSetMessage->hide();
610 if (mySizeMessage->isVisible())
611 mySizeMessage->hide();
615 void PartSet_WidgetSketchLabel::onShowPanel()
617 //if (mySizeOfViewWidget->isVisible()) {
618 if (myStackWidget->currentIndex() == 0) {
619 DocumentPtr aDoc = feature()->document();
620 DocumentPtr aModDoc = ModelAPI_Session::get()->moduleDocument();
621 if (aModDoc == aDoc) {
622 myPartSetMessage->move(mapToGlobal(geometry().bottomLeft()));
623 myPartSetMessage->show();
625 if (mySizeOfViewWidget->isVisible()) {
626 QPoint aPnt = mySizeOfView->mapToGlobal(mySizeOfView->geometry().center());
627 mySizeMessage->move(aPnt);
628 mySizeMessage->show();
633 void PartSet_WidgetSketchLabel::deactivate()
635 QWidget* aTopWidget = window();
637 aTopWidget->removeEventFilter(this);
639 if (myTmpPlane.get()) {
640 setSketchPlane(myTmpPlane);
644 ModuleBase_WidgetValidated::deactivate();
645 bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
646 myPreviewPlanes->erasePreviewPlanes(myWorkshop);
649 myWorkshop->viewer()->update();
651 if (myOpenTransaction) {
652 SessionPtr aMgr = ModelAPI_Session::get();
653 aMgr->finishOperation();
654 myOpenTransaction = false;
658 void PartSet_WidgetSketchLabel::selectionModes(int& theModuleSelectionModes, QIntList& theModes)
660 theModuleSelectionModes = -1;
661 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
663 theModes << TopAbs_FACE;
666 void PartSet_WidgetSketchLabel::selectionFilters(QIntList& theModuleSelectionFilters,
667 SelectMgr_ListOfFilter& theSelectionFilters)
669 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
672 return ModuleBase_WidgetValidated::selectionFilters(theModuleSelectionFilters,
673 theSelectionFilters);
676 std::shared_ptr<GeomAPI_Dir>
677 PartSet_WidgetSketchLabel::setSketchPlane(const TopoDS_Shape& theShape)
679 if (theShape.IsNull())
680 return std::shared_ptr<GeomAPI_Dir>();
682 // get selected shape
683 std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
684 aGShape->setImpl(new TopoDS_Shape(theShape));
686 // get plane parameters
687 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aGShape));
688 std::shared_ptr<GeomAPI_Pln> aPlane = aFace->getPlane();
690 return std::shared_ptr<GeomAPI_Dir>();
691 return setSketchPlane(aPlane);
694 std::shared_ptr<GeomAPI_Dir>
695 PartSet_WidgetSketchLabel::setSketchPlane(std::shared_ptr<GeomAPI_Pln> thePlane)
697 // set plane parameters to feature
698 std::shared_ptr<ModelAPI_Data> aData = feature()->data();
699 double anA, aB, aC, aD;
700 thePlane->coefficients(anA, aB, aC, aD);
702 // calculate attributes of the sketch
703 std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
704 std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
705 std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
706 aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
707 std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
708 // X axis is preferable to be dirX on the sketch
709 const double tol = Precision::Confusion();
710 bool isX = fabs(fabs(anA) - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
711 std::shared_ptr<GeomAPI_Dir> aTempDir(
712 isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
713 std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
714 std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
716 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
717 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
718 anOrigin->setValue(anOrigPnt);
719 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
720 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
721 aNormal->setValue(aNormDir);
722 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
723 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
724 aDirX->setValue(aXDir);
725 std::shared_ptr<GeomAPI_Dir> aDir = thePlane->direction();
729 void PartSet_WidgetSketchLabel::onSetPlaneView()
731 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
733 std::shared_ptr<GeomAPI_Dir> aDirection = aPlane->direction();
734 gp_Dir aDir = aDirection->impl<gp_Dir>();
735 if (myViewInverted->isChecked())
737 myWorkshop->viewer()->setViewProjection(aDir.X(), aDir.Y(), aDir.Z(), 0.);
738 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
740 aModule->onViewTransformed();
745 //******************************************************
746 QList<ModuleBase_ViewerPrsPtr> PartSet_WidgetSketchLabel::findCircularEdgesInPlane()
748 QList<std::shared_ptr<ModuleBase_ViewerPrs>> aResult;
749 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
750 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
751 QObjectPtrList aDispObjects = aDisplayer->displayedObjects();
753 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
754 foreach(ObjectPtr aObj, aDispObjects) {
755 ResultPtr aResObj = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
757 GeomShapePtr aShape = aResObj->shape();
759 GeomAPI_ShapeExplorer aExplorer(aShape, GeomAPI_Shape::EDGE);
760 for(; aExplorer.more(); aExplorer.next()) {
761 GeomShapePtr aEdgeShape = aExplorer.current();
762 GeomAPI_Edge anEdge(aEdgeShape);
763 if ((anEdge.isCircle() || anEdge.isArc() || anEdge.isEllipse()) &&
764 anEdge.isInPlane(aPlane)) {
765 bool isContains = false;
766 // Check that edge is not used.
767 // It is possible that the same edge will be taken from different faces
768 foreach(ModuleBase_ViewerPrsPtr aPrs, aResult) {
769 GeomAPI_Edge aUsedEdge(aPrs->shape());
770 if (aUsedEdge.isEqual(aEdgeShape)) {
776 ModuleBase_ViewerPrsPtr 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);
871 // Transfer focus to the current viewport for correct processing of a key event
872 QWidget* aViewPort = myWorkshop->viewer()->activeViewPort();
874 aViewPort->setFocus();
878 bool PartSet_WidgetSketchLabel::eventFilter(QObject* theObj, QEvent* theEvent)
880 if (theObj == window()) {
881 int aType = theEvent->type();
882 if ((aType == QEvent::Hide) || (aType == QEvent::WindowDeactivate)) {
883 if (myPartSetMessage->isVisible())
884 myPartSetMessage->hide();
885 if (mySizeMessage->isVisible())
886 mySizeMessage->hide();
888 else if ((aType == QEvent::Show) || (aType == QEvent::WindowActivate))
891 return ModuleBase_WidgetValidated::eventFilter(theObj, theEvent);
894 void PartSet_WidgetSketchLabel::onShowViewPlane(bool toShow)
896 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
897 PartSet_PreviewSketchPlane* aPreviewPlane = aModule->sketchMgr()->previewSketchPlane();
899 CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
900 if (aPreviewPlane->isPlaneCreated())
901 aPreviewPlane->displaySketchPlane(myWorkshop);
903 aPreviewPlane->createSketchPlane(aSketch, myWorkshop);
906 aPreviewPlane->eraseSketchPlane(myWorkshop, false);
908 myWorkshop->viewer()->update();