1 // Copyright (C) 2014-2020 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 QGridLayout* aViewLayout = new QGridLayout(aViewBox);
163 myViewInverted = new QCheckBox(tr("Reversed"), aViewBox);
164 aViewLayout->addWidget(myViewInverted, 0, 0);
166 myViewVisible = new QCheckBox(tr("Visible"), aViewBox);
167 myViewVisible->setChecked(true);
168 aViewLayout->addWidget(myViewVisible, 0, 1, Qt::AlignRight);
169 connect(myViewVisible, SIGNAL(toggled(bool)), this, SLOT(onShowViewPlane(bool)));
171 QPushButton* aSetViewBtn =
172 new QPushButton(QIcon(":icons/plane_view.png"), tr("Set plane view"), aViewBox);
173 connect(aSetViewBtn, SIGNAL(clicked(bool)), this, SLOT(onSetPlaneView()));
174 aViewLayout->addWidget(aSetViewBtn, 1, 0, 1, 2);
176 aLayout->addWidget(aViewBox);
178 QMap<PartSet_Tools::ConstraintVisibleState, QString> aStates;
179 aStates[PartSet_Tools::Geometrical] = tr("Show geometrical constraints");
180 aStates[PartSet_Tools::Dimensional] = tr("Show dimensional constraints");
181 aStates[PartSet_Tools::Expressions] = tr("Show existing expressions");
183 QMap<PartSet_Tools::ConstraintVisibleState, QString>::const_iterator anIt = aStates.begin(),
184 aLast = aStates.end();
185 for (; anIt != aLast; anIt++) {
186 QCheckBox* aShowConstraints = new QCheckBox(anIt.value(), this);
187 connect(aShowConstraints, SIGNAL(toggled(bool)), this, SLOT(onShowConstraint(bool)));
188 aLayout->addWidget(aShowConstraints);
190 PartSet_Tools::ConstraintVisibleState aState = anIt.key();
191 myShowConstraints[aState] = aShowConstraints;
193 if (toShowConstraints.contains(aState))
194 aShowConstraints->setChecked(toShowConstraints[aState]);
196 myShowPoints = new QCheckBox(tr("Show free points"), this);
197 connect(myShowPoints, SIGNAL(toggled(bool)), this, SIGNAL(showFreePoints(bool)));
198 aLayout->addWidget(myShowPoints);
200 myAutoConstraints = new QCheckBox(tr("Automatic constraints"), this);
201 myAutoConstraints->setToolTip(tr("Automatic vertical and horizontal constraints"));
202 connect(myAutoConstraints, SIGNAL(toggled(bool)), this, SIGNAL(autoConstraints(bool)));
203 aLayout->addWidget(myAutoConstraints);
205 QPushButton* aPlaneBtn = new QPushButton(tr("Change sketch plane"), aSecondWgt);
206 connect(aPlaneBtn, SIGNAL(clicked(bool)), SLOT(onChangePlane()));
207 aLayout->addWidget(aPlaneBtn);
209 aLayout->addSpacing(15);
211 myDoFLabel = new QLabel("", aSecondWgt);
212 aLayout->addWidget(myDoFLabel);
214 myShowDOFBtn = new QPushButton(tr("Show remaining DoFs"), aSecondWgt);
215 aLayout->addWidget(myShowDOFBtn);
216 myShowDOFBtn->setEnabled(false);
217 connect(myShowDOFBtn, SIGNAL(clicked(bool)), SLOT(onShowDOF()));
219 myStackWidget->addWidget(aSecondWgt);
220 //setLayout(aLayout);
222 myPreviewPlanes = new PartSet_PreviewPlanes();
225 PartSet_WidgetSketchLabel::~PartSet_WidgetSketchLabel()
229 bool PartSet_WidgetSketchLabel::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
230 const bool theToValidate)
232 // do not use the given selection if the plane of the sketch has been already set.
233 // If this check is absent, a selected plane in the viewer can be set in the sketch
234 // even if the sketch is built on another plane.
238 ModuleBase_ViewerPrsPtr aPrs = theValues.first();
239 bool aDone = setSelectionInternal(theValues, theToValidate);
241 updateByPlaneSelected(aPrs);
245 QList<QWidget*> PartSet_WidgetSketchLabel::getControls() const
247 QList<QWidget*> aResult;
248 aResult << myStackWidget;
252 bool PartSet_WidgetSketchLabel::processSelection()
254 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
258 QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
260 if (aSelected.empty())
262 ModuleBase_ViewerPrsPtr aPrs = aSelected.first();
263 bool aDone = setSelectionInternal(aSelected, false);
265 updateByPlaneSelected(aPrs);
266 updateObject(myFeature);
272 void PartSet_WidgetSketchLabel::onShowConstraint(bool theOn)
274 QCheckBox* aSenderCheckBox = qobject_cast<QCheckBox*>(sender());
276 QMap<PartSet_Tools::ConstraintVisibleState, QCheckBox*>::const_iterator
277 anIt = myShowConstraints.begin(), aLast = myShowConstraints.end();
279 PartSet_Tools::ConstraintVisibleState aState = PartSet_Tools::Geometrical;
281 for (; anIt != aLast && !aFound; anIt++) {
282 aFound = anIt.value() == aSenderCheckBox;
287 emit showConstraintToggled(aState, theOn);
290 void PartSet_WidgetSketchLabel::blockAttribute(const AttributePtr& theAttribute,
291 const bool& theToBlock, bool& isFlushesActived,
292 bool& isAttributeSetInitializedBlocked,
293 bool& isAttributeSendUpdatedBlocked)
295 ModuleBase_WidgetValidated::blockAttribute(theAttribute, theToBlock, isFlushesActived,
296 isAttributeSetInitializedBlocked,
297 isAttributeSendUpdatedBlocked);
298 // We do not restore the previous state of isAttributeSetInitializedBlocked for each of
299 // attributes. It it is necessary, these states should be append to the method attributes
300 // or stored in the widget
302 std::list<AttributePtr> anAttributes = myFeature->data()->attributes("");
303 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
305 for(; anIt != aLast; anIt++) {
306 AttributePtr anAttribute = *anIt;
308 anAttribute->blockSetInitialized(true);
310 anAttribute->blockSetInitialized(isAttributeSetInitializedBlocked);
314 bool PartSet_WidgetSketchLabel::setSelectionInternal(
315 const QList<ModuleBase_ViewerPrsPtr>& theValues,
316 const bool theToValidate)
319 if (theValues.empty()) {
320 // In order to make reselection possible, set empty object and shape should be done
321 setSelectionCustom(ModuleBase_ViewerPrsPtr(
322 new ModuleBase_ViewerPrs(ObjectPtr(), GeomShapePtr(), NULL)));
326 // it removes the processed value from the parameters list
327 ModuleBase_ViewerPrsPtr aValue = theValues.first();//.takeFirst();
328 if (!theToValidate || isValidInFilters(aValue)) {
329 myIsSelection = true;
330 aDone = setSelectionCustom(aValue);
331 myIsSelection = false;
338 void PartSet_WidgetSketchLabel::updateByPlaneSelected(const ModuleBase_ViewerPrsPtr& thePrs)
340 // Nullify a temporary remembered plane
341 if (myTmpPlane.get())
344 GeomPlanePtr aPlane = plane();
348 myPartSetMessage->hide();
349 mySizeMessage->hide();
351 // 1. hide main planes if they have been displayed and display sketch preview plane
352 myPreviewPlanes->erasePreviewPlanes(myWorkshop);
354 QString aSizeOfViewStr = mySizeOfView->text();
355 bool isSetSizeOfView = false;
356 double aSizeOfView = 0;
357 if (!aSizeOfViewStr.isEmpty()) {
358 aSizeOfView = aSizeOfViewStr.toDouble(&isSetSizeOfView);
359 if (isSetSizeOfView && aSizeOfView <= 0) {
360 isSetSizeOfView = false;
363 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
365 CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
366 aModule->sketchMgr()->previewSketchPlane()->setSizeOfView(aSizeOfView, isSetSizeOfView);
367 // Call of createSketchPlane is managed by events Loop
368 //if (myViewVisible->isChecked())
369 // aModule->sketchMgr()->previewSketchPlane()->createSketchPlane(aSketch, myWorkshop);
371 // 2. if the planes were displayed, change the view projection
373 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
374 gp_XYZ aXYZ = aDir->impl<gp_Dir>().XYZ();
377 // Rotate view if the sketcher plane is selected only from preview planes
378 // Preview planes are created only if there is no any shape
379 bool aRotate = Config_PropManager::boolean(SKETCH_TAB_NAME, "rotate_to_plane");
381 myWorkshop->viewer()->setViewProjection(aXYZ.X(), aXYZ.Y(), aXYZ.Z(), aTwist);
383 if (isSetSizeOfView && aSizeOfView > 0) {
384 Handle(V3d_View) aView3d = myWorkshop->viewer()->activeView();
385 if (!aView3d.IsNull()) {
387 double aHalfSize = aSizeOfView/2.0;
388 aBndBox.Update(-aHalfSize, -aHalfSize, -aHalfSize, aHalfSize, aHalfSize, aHalfSize);
389 aView3d->FitAll(aBndBox, 0.01, false);
392 if (myOpenTransaction) {
393 SessionPtr aMgr = ModelAPI_Session::get();
394 aMgr->finishOperation();
395 myOpenTransaction = false;
397 // 3. Clear text in the label
398 myStackWidget->setCurrentIndex(1);
399 //myLabel->setText("");
400 //myLabel->setToolTip("");
401 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
403 // 5. Clear selection mode and define sketching mode
404 emit planeSelected(plane());
405 // after the plane is selected in the sketch, the sketch selection should be activated
406 // it can not be performed in the sketch label widget because, we don't need to switch off
407 // the selection by any label deactivation, but need to switch it off by stop the sketch
408 myWorkshop->selectionActivate()->updateSelectionFilters();
409 myWorkshop->selectionActivate()->updateSelectionModes();
412 aModule->onViewTransformed();
414 myWorkshop->updateCommandStatus();
415 aWorkshop->selector()->clearSelection();
416 myWorkshop->viewer()->update();
418 myRemoveExternal->setVisible(false);
421 std::shared_ptr<GeomAPI_Pln> PartSet_WidgetSketchLabel::plane() const
423 CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
424 return PartSet_Tools::sketchPlane(aSketch);
427 bool PartSet_WidgetSketchLabel::focusTo()
429 ModuleBase_Tools::setFocus(myStackWidget, "PartSet_WidgetSketchLabel::focusTo()");
433 void PartSet_WidgetSketchLabel::enableFocusProcessing()
435 myStackWidget->installEventFilter(this);
438 void PartSet_WidgetSketchLabel::storeAttributeValue(const AttributePtr& theAttribute)
440 ModuleBase_WidgetValidated::storeAttributeValue(theAttribute);
443 void PartSet_WidgetSketchLabel::restoreAttributeValue(const AttributePtr& theAttribute,
446 ModuleBase_WidgetValidated::restoreAttributeValue(theAttribute, theValid);
448 // it is not necessary to save the previous plane value because the plane is chosen once
449 DataPtr aData = feature()->data();
450 AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
451 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
453 ResultPtr anEmptyResult;
454 GeomShapePtr anEmptyShape;
455 aSelAttr->setValue(anEmptyResult, anEmptyShape);
457 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
458 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
460 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
461 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
463 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
464 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
468 bool PartSet_WidgetSketchLabel::setSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
470 if (myIsSelection && myRemoveExternal->isVisible()) {
471 if (myRemoveExternal->isChecked()) {
472 myFeature->customAction(SketchPlugin_Sketch::ACTION_REMOVE_EXTERNAL());
475 return fillSketchPlaneBySelection(thePrs);
478 bool PartSet_WidgetSketchLabel::canFillSketch(const ModuleBase_ViewerPrsPtr& thePrs)
480 bool aCanFillSketch = true;
481 // avoid any selection on sketch object
482 ObjectPtr anObject = thePrs->object();
483 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
485 FeaturePtr aFeature = ModelAPI_Feature::feature(aResult);
486 if (aFeature->getKind() == SketchPlugin_Sketch::ID())
487 aCanFillSketch = false;
489 // check plane or planar face of any non-sketch object
490 if (aCanFillSketch) {
491 GeomShapePtr aGeomShape = thePrs->shape();
492 if ((!aGeomShape.get() || aGeomShape->isNull()) && aResult.get()) {
493 aGeomShape = aResult->shape();
496 if (aGeomShape.get() && aGeomShape->shapeType() == GeomAPI_Shape::FACE) {
497 std::shared_ptr<GeomAPI_Face> aGeomFace(new GeomAPI_Face(aGeomShape));
498 aCanFillSketch = aGeomFace.get() && aGeomFace->isPlanar();
501 aCanFillSketch = false;
503 return aCanFillSketch;
506 bool PartSet_WidgetSketchLabel::fillSketchPlaneBySelection(const ModuleBase_ViewerPrsPtr& thePrs)
508 bool isOwnerSet = false;
510 const GeomShapePtr& aShape = thePrs->shape();
511 std::shared_ptr<GeomAPI_Dir> aDir;
513 if (aShape.get() && !aShape->isNull()) {
514 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
515 aDir = setSketchPlane(aTDShape);
516 isOwnerSet = aDir.get();
518 if (thePrs->object() && (feature() != thePrs->object())) {
519 FeaturePtr aFeature = ModelAPI_Feature::feature(thePrs->object());
520 DataPtr aData = feature()->data();
521 AttributeSelectionPtr aSelAttr =
522 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
523 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
524 if (aSelAttr.get()) {
525 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(thePrs->object());
527 GeomShapePtr aShapePtr;
528 if (!aShape.get() || aShape->isNull()) { // selection happens in the OCC viewer
529 aShapePtr = ModelAPI_Tools::shape(aRes);
531 else { // selection happens in OB browser
534 if (aShapePtr.get() && aShapePtr->isFace()) {
535 const TopoDS_Shape& aTDShape = aShapePtr->impl<TopoDS_Shape>();
536 setSketchPlane(aTDShape);
537 aSelAttr->setValue(aRes, aShapePtr);
542 aSelAttr->setValue(aFeature, GeomShapePtr());
543 GeomShapePtr aSelShape = aSelAttr->value();
544 if (!aSelShape.get() && aSelAttr->contextFeature().get() &&
545 aSelAttr->contextFeature()->firstResult().get()) {
546 aSelShape = aSelAttr->contextFeature()->firstResult()->shape();
548 if (aSelShape.get() && aSelShape->isPlanar()) {
549 const TopoDS_Shape& aTDShape = aSelShape->impl<TopoDS_Shape>();
550 setSketchPlane(aTDShape);
559 void PartSet_WidgetSketchLabel::activateCustom()
561 QWidget* aTopWidget = window();
563 aTopWidget->installEventFilter(this);
565 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
567 bool isBlocked = myAutoConstraints->blockSignals(true);
568 myAutoConstraints->setChecked(aModule->sketchReentranceMgr()->isAutoConstraints());
569 myAutoConstraints->blockSignals(isBlocked);
572 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
574 myStackWidget->setCurrentIndex(1);
578 myStackWidget->setCurrentIndex(0);
579 bool aBodyIsVisualized = myPreviewPlanes->hasVisualizedBodies(myWorkshop);
581 // Clear previous selection mode It is necessary for correct activation of preview planes
582 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
583 XGUI_Displayer* aDisp = aWorkshop->displayer();
584 aWorkshop->selectionActivate()->activateObjects(QIntList(), aDisp->displayedObjects(), false);
586 if (!aBodyIsVisualized) {
587 // We have to select a plane before any operation
588 myPreviewPlanes->showPreviewPlanes(myWorkshop);
589 mySizeOfViewWidget->setVisible(true);
592 mySizeOfViewWidget->setVisible(false);
595 void PartSet_WidgetSketchLabel::showEvent(QShowEvent* theEvent)
597 ModuleBase_WidgetValidated::showEvent(theEvent);
598 QTimer::singleShot(10, this, SLOT(onShowPanel()));
601 void PartSet_WidgetSketchLabel::hideEvent(QHideEvent* theEvent)
603 ModuleBase_WidgetValidated::hideEvent(theEvent);
604 if (myPartSetMessage->isVisible())
605 myPartSetMessage->hide();
606 if (mySizeMessage->isVisible())
607 mySizeMessage->hide();
611 void PartSet_WidgetSketchLabel::onShowPanel()
613 //if (mySizeOfViewWidget->isVisible()) {
614 if (myStackWidget->currentIndex() == 0) {
615 DocumentPtr aDoc = feature()->document();
616 DocumentPtr aModDoc = ModelAPI_Session::get()->moduleDocument();
617 if (aModDoc == aDoc) {
618 myPartSetMessage->move(mapToGlobal(geometry().bottomLeft()));
619 myPartSetMessage->show();
621 if (mySizeOfViewWidget->isVisible()) {
622 QPoint aPnt = mySizeOfView->mapToGlobal(mySizeOfView->geometry().center());
623 mySizeMessage->move(aPnt);
624 mySizeMessage->show();
629 void PartSet_WidgetSketchLabel::deactivate()
631 QWidget* aTopWidget = window();
633 aTopWidget->removeEventFilter(this);
635 if (myTmpPlane.get()) {
636 setSketchPlane(myTmpPlane);
640 ModuleBase_WidgetValidated::deactivate();
641 bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
642 myPreviewPlanes->erasePreviewPlanes(myWorkshop);
645 myWorkshop->viewer()->update();
647 if (myOpenTransaction) {
648 SessionPtr aMgr = ModelAPI_Session::get();
649 aMgr->finishOperation();
650 myOpenTransaction = false;
654 void PartSet_WidgetSketchLabel::selectionModes(int& theModuleSelectionModes, QIntList& theModes)
656 theModuleSelectionModes = -1;
657 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
659 theModes << TopAbs_FACE;
662 void PartSet_WidgetSketchLabel::selectionFilters(QIntList& theModuleSelectionFilters,
663 SelectMgr_ListOfFilter& theSelectionFilters)
665 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
668 return ModuleBase_WidgetValidated::selectionFilters(theModuleSelectionFilters,
669 theSelectionFilters);
672 std::shared_ptr<GeomAPI_Dir>
673 PartSet_WidgetSketchLabel::setSketchPlane(const TopoDS_Shape& theShape)
675 if (theShape.IsNull())
676 return std::shared_ptr<GeomAPI_Dir>();
678 // get selected shape
679 std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
680 aGShape->setImpl(new TopoDS_Shape(theShape));
682 // get plane parameters
683 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aGShape));
684 std::shared_ptr<GeomAPI_Pln> aPlane = aFace->getPlane();
686 return std::shared_ptr<GeomAPI_Dir>();
687 return setSketchPlane(aPlane);
690 std::shared_ptr<GeomAPI_Dir>
691 PartSet_WidgetSketchLabel::setSketchPlane(std::shared_ptr<GeomAPI_Pln> thePlane)
693 // set plane parameters to feature
694 std::shared_ptr<ModelAPI_Data> aData = feature()->data();
695 double anA, aB, aC, aD;
696 thePlane->coefficients(anA, aB, aC, aD);
698 // calculate attributes of the sketch
699 std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
700 std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
701 std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
702 aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
703 std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
704 // X axis is preferable to be dirX on the sketch
705 const double tol = Precision::Confusion();
706 bool isX = fabs(fabs(anA) - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
707 std::shared_ptr<GeomAPI_Dir> aTempDir(
708 isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
709 std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
710 std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
712 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
713 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
714 anOrigin->setValue(anOrigPnt);
715 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
716 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
717 aNormal->setValue(aNormDir);
718 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
719 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
720 aDirX->setValue(aXDir);
721 std::shared_ptr<GeomAPI_Dir> aDir = thePlane->direction();
725 void PartSet_WidgetSketchLabel::onSetPlaneView()
727 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
729 std::shared_ptr<GeomAPI_Dir> aDirection = aPlane->direction();
730 gp_Dir aDir = aDirection->impl<gp_Dir>();
731 if (myViewInverted->isChecked())
733 myWorkshop->viewer()->setViewProjection(aDir.X(), aDir.Y(), aDir.Z(), 0.);
734 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
736 aModule->onViewTransformed();
741 //******************************************************
742 QList<ModuleBase_ViewerPrsPtr> PartSet_WidgetSketchLabel::findCircularEdgesInPlane()
744 QList<std::shared_ptr<ModuleBase_ViewerPrs>> aResult;
745 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
746 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
747 QObjectPtrList aDispObjects = aDisplayer->displayedObjects();
749 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
750 foreach(ObjectPtr aObj, aDispObjects) {
751 ResultPtr aResObj = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
753 GeomShapePtr aShape = aResObj->shape();
755 GeomAPI_ShapeExplorer aExplorer(aShape, GeomAPI_Shape::EDGE);
756 for(; aExplorer.more(); aExplorer.next()) {
757 GeomShapePtr aEdgeShape = aExplorer.current();
758 GeomAPI_Edge anEdge(aEdgeShape);
759 if ((anEdge.isCircle() || anEdge.isArc() || anEdge.isEllipse()) &&
760 anEdge.isInPlane(aPlane)) {
761 bool isContains = false;
762 // Check that edge is not used.
763 // It is possible that the same edge will be taken from different faces
764 foreach(ModuleBase_ViewerPrsPtr aPrs, aResult) {
765 GeomAPI_Edge aUsedEdge(aPrs->shape());
766 if (aUsedEdge.isEqual(aEdgeShape)) {
772 ModuleBase_ViewerPrsPtr aPrs(new ModuleBase_ViewerPrs(aResObj, aEdgeShape));
773 aResult.append(aPrs);
783 //******************************************************
784 void PartSet_WidgetSketchLabel::onChangePlane()
786 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
788 mySizeOfViewWidget->setVisible(false);
789 myRemoveExternal->setVisible(true);
790 myStackWidget->setCurrentIndex(0);
792 bool aBodyIsVisualized = myPreviewPlanes->hasVisualizedBodies(myWorkshop);
794 if (!aBodyIsVisualized) {
795 // We have to select a plane before any operation
796 myPreviewPlanes->showPreviewPlanes(myWorkshop);
799 CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
800 myTmpPlane = PartSet_Tools::sketchPlane(aSketch);
801 PartSet_Tools::nullifySketchPlane(aSketch);
803 Handle(SelectMgr_Filter) aFilter = aModule->selectionFilter(SF_SketchPlaneFilter);
804 if (!aFilter.IsNull()) {
805 std::shared_ptr<GeomAPI_Pln> aPln;
806 Handle(ModuleBase_ShapeInPlaneFilter)::DownCast(aFilter)->setPlane(aPln);
808 XGUI_Workshop* aWorkshop = aModule->getWorkshop();
810 aWorkshop->selectionActivate()->updateSelectionFilters();
811 aWorkshop->selectionActivate()->updateSelectionModes();
813 SessionPtr aMgr = ModelAPI_Session::get();
814 aMgr->startOperation("Change Sketch plane");
815 myOpenTransaction = true;
816 myWorkshop->viewer()->update();
820 void PartSet_WidgetSketchLabel::setShowPointsState(bool theState)
822 bool aBlock = myShowPoints->blockSignals(true);
823 myShowPoints->setChecked(theState);
824 myShowPoints->blockSignals(aBlock);
827 bool PartSet_WidgetSketchLabel::restoreValueCustom()
829 if (myFeature.get()) {
830 CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
831 if (aSketch.get() && (aSketch->numberOfSubs() > 0)) {
832 AttributeStringPtr aDOFStr = aSketch->string("SolverDOF");
834 QString aVal(aDOFStr->value().c_str());
835 if (aVal.contains('=')) {
836 // to support old data
837 aVal = aVal.right(aVal.length() - aVal.lastIndexOf('='));
839 int aDoF = aVal.toInt();
841 myDoFLabel->setText(tr("Sketch is fully fixed (DoF = 0)"));
842 myShowDOFBtn->setEnabled(false);
844 myDoFLabel->setText(tr("DoF (degrees of freedom) = ") + aVal);
845 myShowDOFBtn->setEnabled(true);
850 myDoFLabel->setText("");
851 myShowDOFBtn->setEnabled(false);
858 void PartSet_WidgetSketchLabel::onShowDOF()
860 CompositeFeaturePtr aCompFeature =
861 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
862 if (aCompFeature.get()) {
863 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_GET_DOF_OBJECTS);
864 ModelAPI_EventCreator::get()->sendUpdated(aCompFeature, anEvent);
865 Events_Loop::loop()->flush(anEvent);
867 // Transfer focus to the current viewport for correct processing of a key event
868 QWidget* aViewPort = myWorkshop->viewer()->activeViewPort();
870 aViewPort->setFocus();
874 bool PartSet_WidgetSketchLabel::eventFilter(QObject* theObj, QEvent* theEvent)
876 if (theObj == window()) {
877 int aType = theEvent->type();
878 if ((aType == QEvent::Hide) || (aType == QEvent::WindowDeactivate)) {
879 if (myPartSetMessage->isVisible())
880 myPartSetMessage->hide();
881 if (mySizeMessage->isVisible())
882 mySizeMessage->hide();
884 else if ((aType == QEvent::Show) || (aType == QEvent::WindowActivate))
887 return ModuleBase_WidgetValidated::eventFilter(theObj, theEvent);
890 void PartSet_WidgetSketchLabel::onShowViewPlane(bool toShow)
892 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
894 CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
895 aModule->sketchMgr()->previewSketchPlane()->createSketchPlane(aSketch, myWorkshop);
898 aModule->sketchMgr()->previewSketchPlane()->eraseSketchPlane(myWorkshop, false);
900 myWorkshop->viewer()->update();