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>
84 #define DBL_MAX 1.7976931348623158e+308
87 PartSet_WidgetSketchLabel::PartSet_WidgetSketchLabel(QWidget* theParent,
88 ModuleBase_IWorkshop* theWorkshop,
89 const Config_WidgetAPI* theData,
90 const QMap<PartSet_Tools::ConstraintVisibleState, bool>& toShowConstraints)
91 : ModuleBase_WidgetValidated(theParent, theWorkshop, theData), myOpenTransaction(false),
94 QVBoxLayout* aLayout = new QVBoxLayout(this);
95 ModuleBase_Tools::zeroMargins(aLayout);
97 myStackWidget = new QStackedWidget(this);
98 myStackWidget->setContentsMargins(0,0,0,0);
99 aLayout->addWidget(myStackWidget);
101 // Define label for plane selection
102 QWidget* aFirstWgt = new QWidget(this);
104 // Size of the View control
105 mySizeOfViewWidget = new QWidget(aFirstWgt);
106 QHBoxLayout* aSizeLayout = new QHBoxLayout(mySizeOfViewWidget);
107 aSizeLayout->addWidget(new QLabel(tr("Size of the view"), mySizeOfViewWidget));
108 mySizeOfView = new QLineEdit(mySizeOfViewWidget);
110 QDoubleValidator* aValidator = new QDoubleValidator(0, DBL_MAX, 12, mySizeOfView);
111 aValidator->setLocale(ModuleBase_Tools::doubleLocale());
112 aValidator->setNotation(QDoubleValidator::StandardNotation);
113 mySizeOfView->setValidator(aValidator);
114 aSizeLayout->addWidget(mySizeOfView);
116 myPartSetMessage = new QDialog(this, Qt::ToolTip);
117 myPartSetMessage->setModal(false);
118 myPartSetMessage->setStyleSheet("background-color:lightyellow;");
119 QVBoxLayout* aMsgLay = new QVBoxLayout(myPartSetMessage);
120 QString aMsg = tr("The Sketch is created in PartSet.\n"
121 "It will be necessary to create a Part in order to use this sketch for body creation");
122 aMsgLay->addWidget(new QLabel(aMsg, myPartSetMessage));
123 myPartSetMessage->hide();
125 mySizeMessage = new QDialog(mySizeOfView, Qt::ToolTip);
126 mySizeMessage->setModal(false);
127 mySizeMessage->setStyleSheet("background-color:lightyellow;");
128 aMsgLay = new QVBoxLayout(mySizeMessage);
129 aMsg = tr("A size of Sketch view can be defined here.");
130 aMsgLay->addWidget(new QLabel(aMsg, mySizeMessage));
131 mySizeMessage->hide();
133 QString aText = translate(theData->getProperty("title"));
134 QLabel* aLabel = new QLabel(aText, aFirstWgt);
135 aLabel->setWordWrap(true);
136 QString aTooltip = translate(theData->getProperty("tooltip"));
137 aLabel->setToolTip(aTooltip);
138 aLabel->setIndent(5);
140 aLayout = new QVBoxLayout(aFirstWgt);
141 ModuleBase_Tools::zeroMargins(aLayout);
142 aLayout->addWidget(mySizeOfViewWidget);
143 aLayout->addWidget(aLabel);
145 myRemoveExternal = new QCheckBox(tr("Remove external dependencies"), aFirstWgt);
146 myRemoveExternal->setChecked(false);
147 aLayout->addWidget(myRemoveExternal);
148 myRemoveExternal->setVisible(false);
150 aLayout->addStretch(1);
152 myStackWidget->addWidget(aFirstWgt);
154 // Define widget for sketch manmagement
155 QWidget* aSecondWgt = new QWidget(this);
156 aLayout = new QVBoxLayout(aSecondWgt);
157 ModuleBase_Tools::zeroMargins(aLayout);
159 QGroupBox* aViewBox = new QGroupBox(tr("Sketcher plane"), this);
160 QVBoxLayout* aViewLayout = new QVBoxLayout(aViewBox);
162 myViewInverted = new QCheckBox(tr("Reversed"), aViewBox);
163 aViewLayout->addWidget(myViewInverted);
165 QPushButton* aSetViewBtn =
166 new QPushButton(QIcon(":icons/plane_view.png"), tr("Set plane view"), aViewBox);
167 connect(aSetViewBtn, SIGNAL(clicked(bool)), this, SLOT(onSetPlaneView()));
168 aViewLayout->addWidget(aSetViewBtn);
170 aLayout->addWidget(aViewBox);
172 QMap<PartSet_Tools::ConstraintVisibleState, QString> aStates;
173 aStates[PartSet_Tools::Geometrical] = tr("Show geometrical constraints");
174 aStates[PartSet_Tools::Dimensional] = tr("Show dimensional constraints");
175 aStates[PartSet_Tools::Expressions] = tr("Show existing expressions");
177 QMap<PartSet_Tools::ConstraintVisibleState, QString>::const_iterator anIt = aStates.begin(),
178 aLast = aStates.end();
179 for (; anIt != aLast; anIt++) {
180 QCheckBox* aShowConstraints = new QCheckBox(anIt.value(), this);
181 connect(aShowConstraints, SIGNAL(toggled(bool)), this, SLOT(onShowConstraint(bool)));
182 aLayout->addWidget(aShowConstraints);
184 PartSet_Tools::ConstraintVisibleState aState = anIt.key();
185 myShowConstraints[aState] = aShowConstraints;
187 if (toShowConstraints.contains(aState))
188 aShowConstraints->setChecked(toShowConstraints[aState]);
190 myShowPoints = new QCheckBox(tr("Show free points"), this);
191 connect(myShowPoints, SIGNAL(toggled(bool)), this, SIGNAL(showFreePoints(bool)));
192 aLayout->addWidget(myShowPoints);
194 myAutoConstraints = new QCheckBox(tr("Automatic constraints"), this);
195 myAutoConstraints->setToolTip(tr("Automatic vertical and horizontal constraints"));
196 connect(myAutoConstraints, SIGNAL(toggled(bool)), this, SIGNAL(autoConstraints(bool)));
197 aLayout->addWidget(myAutoConstraints);
199 QPushButton* aPlaneBtn = new QPushButton(tr("Change sketch plane"), aSecondWgt);
200 connect(aPlaneBtn, SIGNAL(clicked(bool)), SLOT(onChangePlane()));
201 aLayout->addWidget(aPlaneBtn);
203 aLayout->addSpacing(15);
205 myDoFLabel = new QLabel("", aSecondWgt);
206 aLayout->addWidget(myDoFLabel);
208 myShowDOFBtn = new QPushButton(tr("Show remaining DoFs"), aSecondWgt);
209 aLayout->addWidget(myShowDOFBtn);
210 myShowDOFBtn->setEnabled(false);
211 connect(myShowDOFBtn, SIGNAL(clicked(bool)), SLOT(onShowDOF()));
213 myStackWidget->addWidget(aSecondWgt);
214 //setLayout(aLayout);
216 myPreviewPlanes = new PartSet_PreviewPlanes();
219 PartSet_WidgetSketchLabel::~PartSet_WidgetSketchLabel()
223 bool PartSet_WidgetSketchLabel::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
224 const bool theToValidate)
226 // do not use the given selection if the plane of the sketch has been already set.
227 // If this check is absent, a selected plane in the viewer can be set in the sketch
228 // even if the sketch is built on another plane.
232 ModuleBase_ViewerPrsPtr aPrs = theValues.first();
233 bool aDone = setSelectionInternal(theValues, theToValidate);
235 updateByPlaneSelected(aPrs);
239 QList<QWidget*> PartSet_WidgetSketchLabel::getControls() const
241 QList<QWidget*> aResult;
242 aResult << myStackWidget;
246 bool PartSet_WidgetSketchLabel::processSelection()
248 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
252 QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
254 if (aSelected.empty())
256 ModuleBase_ViewerPrsPtr aPrs = aSelected.first();
257 bool aDone = setSelectionInternal(aSelected, false);
259 updateByPlaneSelected(aPrs);
260 updateObject(myFeature);
266 void PartSet_WidgetSketchLabel::onShowConstraint(bool theOn)
268 QCheckBox* aSenderCheckBox = qobject_cast<QCheckBox*>(sender());
270 QMap<PartSet_Tools::ConstraintVisibleState, QCheckBox*>::const_iterator
271 anIt = myShowConstraints.begin(), aLast = myShowConstraints.end();
273 PartSet_Tools::ConstraintVisibleState aState = PartSet_Tools::Geometrical;
275 for (; anIt != aLast && !aFound; anIt++) {
276 aFound = anIt.value() == aSenderCheckBox;
281 emit showConstraintToggled(aState, theOn);
284 void PartSet_WidgetSketchLabel::blockAttribute(const AttributePtr& theAttribute,
285 const bool& theToBlock, bool& isFlushesActived,
286 bool& isAttributeSetInitializedBlocked,
287 bool& isAttributeSendUpdatedBlocked)
289 ModuleBase_WidgetValidated::blockAttribute(theAttribute, theToBlock, isFlushesActived,
290 isAttributeSetInitializedBlocked,
291 isAttributeSendUpdatedBlocked);
292 // We do not restore the previous state of isAttributeSetInitializedBlocked for each of
293 // attributes. It it is necessary, these states should be append to the method attributes
294 // or stored in the widget
296 std::list<AttributePtr> anAttributes = myFeature->data()->attributes("");
297 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
299 for(; anIt != aLast; anIt++) {
300 AttributePtr anAttribute = *anIt;
302 anAttribute->blockSetInitialized(true);
304 anAttribute->blockSetInitialized(isAttributeSetInitializedBlocked);
308 bool PartSet_WidgetSketchLabel::setSelectionInternal(
309 const QList<ModuleBase_ViewerPrsPtr>& theValues,
310 const bool theToValidate)
313 if (theValues.empty()) {
314 // In order to make reselection possible, set empty object and shape should be done
315 setSelectionCustom(std::shared_ptr<ModuleBase_ViewerPrs>(
316 new ModuleBase_ViewerPrs(ObjectPtr(), GeomShapePtr(), NULL)));
320 // it removes the processed value from the parameters list
321 ModuleBase_ViewerPrsPtr aValue = theValues.first();//.takeFirst();
322 if (!theToValidate || isValidInFilters(aValue)) {
323 myIsSelection = true;
324 aDone = setSelectionCustom(aValue);
325 myIsSelection = false;
332 void PartSet_WidgetSketchLabel::updateByPlaneSelected(const ModuleBase_ViewerPrsPtr& thePrs)
334 GeomPlanePtr aPlane = plane();
338 myPartSetMessage->hide();
339 mySizeMessage->hide();
341 // 1. hide main planes if they have been displayed and display sketch preview plane
342 myPreviewPlanes->erasePreviewPlanes(myWorkshop);
344 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
346 CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
347 bool isSetSizeOfView = false;
348 double aSizeOfView = 0;
349 QString aSizeOfViewStr = mySizeOfView->text();
350 if (!aSizeOfViewStr.isEmpty()) {
351 aSizeOfView = aSizeOfViewStr.toDouble(&isSetSizeOfView);
352 if (isSetSizeOfView && aSizeOfView <= 0) {
353 isSetSizeOfView = false;
356 aModule->sketchMgr()->previewSketchPlane()->setSizeOfView(aSizeOfView, isSetSizeOfView);
357 aModule->sketchMgr()->previewSketchPlane()->createSketchPlane(aSketch, myWorkshop);
359 // 2. if the planes were displayed, change the view projection
361 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
362 gp_XYZ aXYZ = aDir->impl<gp_Dir>().XYZ();
365 // Rotate view if the sketcher plane is selected only from preview planes
366 // Preview planes are created only if there is no any shape
367 bool aRotate = Config_PropManager::boolean(SKETCH_TAB_NAME, "rotate_to_plane");
369 myWorkshop->viewer()->setViewProjection(aXYZ.X(), aXYZ.Y(), aXYZ.Z(), aTwist);
371 QString aSizeOfViewStr = mySizeOfView->text();
372 if (!aSizeOfViewStr.isEmpty()) {
374 double aSizeOfView = aSizeOfViewStr.toDouble(&isOk);
375 if (isOk && aSizeOfView > 0) {
376 Handle(V3d_View) aView3d = myWorkshop->viewer()->activeView();
377 if (!aView3d.IsNull()) {
379 double aHalfSize = aSizeOfView/2.0;
380 aBndBox.Update(-aHalfSize, -aHalfSize, -aHalfSize, aHalfSize, aHalfSize, aHalfSize);
381 aView3d->FitAll(aBndBox, 0.01, false);
385 if (myOpenTransaction) {
386 SessionPtr aMgr = ModelAPI_Session::get();
387 aMgr->finishOperation();
388 myOpenTransaction = false;
390 // 3. Clear text in the label
391 myStackWidget->setCurrentIndex(1);
392 //myLabel->setText("");
393 //myLabel->setToolTip("");
394 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
396 // 5. Clear selection mode and define sketching mode
397 emit planeSelected(plane());
398 // after the plane is selected in the sketch, the sketch selection should be activated
399 // it can not be performed in the sketch label widget because, we don't need to switch off
400 // the selection by any label deactivation, but need to switch it off by stop the sketch
401 myWorkshop->selectionActivate()->updateSelectionFilters();
402 myWorkshop->selectionActivate()->updateSelectionModes();
405 aModule->onViewTransformed();
407 // 6. Update sketcher actions
408 XGUI_ActionsMgr* anActMgr = aWorkshop->actionsMgr();
410 myWorkshop->updateCommandStatus();
411 aWorkshop->selector()->clearSelection();
412 myWorkshop->viewer()->update();
414 myRemoveExternal->setVisible(false);
417 std::shared_ptr<GeomAPI_Pln> PartSet_WidgetSketchLabel::plane() const
419 CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
420 return PartSet_Tools::sketchPlane(aSketch);
423 bool PartSet_WidgetSketchLabel::focusTo()
425 ModuleBase_Tools::setFocus(myStackWidget, "PartSet_WidgetSketchLabel::focusTo()");
429 void PartSet_WidgetSketchLabel::enableFocusProcessing()
431 myStackWidget->installEventFilter(this);
434 void PartSet_WidgetSketchLabel::storeAttributeValue(const AttributePtr& theAttribute)
436 ModuleBase_WidgetValidated::storeAttributeValue(theAttribute);
439 void PartSet_WidgetSketchLabel::restoreAttributeValue(const AttributePtr& theAttribute,
442 ModuleBase_WidgetValidated::restoreAttributeValue(theAttribute, theValid);
444 // it is not necessary to save the previous plane value because the plane is chosen once
445 DataPtr aData = feature()->data();
446 AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
447 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
449 ResultPtr anEmptyResult;
450 GeomShapePtr anEmptyShape;
451 aSelAttr->setValue(anEmptyResult, anEmptyShape);
455 bool PartSet_WidgetSketchLabel::setSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
457 if (myIsSelection && myRemoveExternal->isVisible()) {
458 if (myRemoveExternal->isChecked()) {
459 myFeature->customAction(SketchPlugin_Sketch::ACTION_REMOVE_EXTERNAL());
462 return fillSketchPlaneBySelection(thePrs);
465 bool PartSet_WidgetSketchLabel::canFillSketch(const ModuleBase_ViewerPrsPtr& thePrs)
467 bool aCanFillSketch = true;
468 // avoid any selection on sketch object
469 ObjectPtr anObject = thePrs->object();
470 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
472 FeaturePtr aFeature = ModelAPI_Feature::feature(aResult);
473 if (aFeature->getKind() == SketchPlugin_Sketch::ID())
474 aCanFillSketch = false;
476 // check plane or planar face of any non-sketch object
477 if (aCanFillSketch) {
478 std::shared_ptr<GeomAPI_Face> aGeomFace;
480 GeomShapePtr aGeomShape = thePrs->shape();
481 if ((!aGeomShape.get() || aGeomShape->isNull()) && aResult.get()) {
482 aGeomShape = aResult->shape();
485 if (aGeomShape.get() && aGeomShape->shapeType() == GeomAPI_Shape::FACE) {
486 std::shared_ptr<GeomAPI_Face> aGeomFace(new GeomAPI_Face(aGeomShape));
487 aCanFillSketch = aGeomFace.get() && aGeomFace->isPlanar();
490 aCanFillSketch = false;
492 return aCanFillSketch;
495 bool PartSet_WidgetSketchLabel::fillSketchPlaneBySelection(const ModuleBase_ViewerPrsPtr& thePrs)
497 bool isOwnerSet = false;
499 const GeomShapePtr& aShape = thePrs->shape();
500 std::shared_ptr<GeomAPI_Dir> aDir;
502 if (aShape.get() && !aShape->isNull()) {
503 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
504 aDir = setSketchPlane(aTDShape);
505 isOwnerSet = aDir.get();
507 if (thePrs->object() && (feature() != thePrs->object())) {
508 FeaturePtr aFeature = ModelAPI_Feature::feature(thePrs->object());
509 DataPtr aData = feature()->data();
510 AttributeSelectionPtr aSelAttr =
511 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
512 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
513 if (aSelAttr.get()) {
514 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(thePrs->object());
516 GeomShapePtr aShapePtr;
517 if (!aShape.get() || aShape->isNull()) { // selection happens in the OCC viewer
518 aShapePtr = ModelAPI_Tools::shape(aRes);
520 else { // selection happens in OB browser
523 if (aShapePtr.get() && aShapePtr->isFace()) {
524 const TopoDS_Shape& aTDShape = aShapePtr->impl<TopoDS_Shape>();
525 setSketchPlane(aTDShape);
526 aSelAttr->setValue(aRes, aShapePtr);
531 aSelAttr->setValue(aFeature, GeomShapePtr());
532 GeomShapePtr aShape = aSelAttr->value();
533 if (!aShape.get() && aSelAttr->contextFeature().get() &&
534 aSelAttr->contextFeature()->firstResult().get()) {
535 aShape = aSelAttr->contextFeature()->firstResult()->shape();
537 if (aShape.get() && aShape->isPlanar()) {
538 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
539 setSketchPlane(aTDShape);
548 void PartSet_WidgetSketchLabel::activateCustom()
550 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
552 bool isBlocked = myAutoConstraints->blockSignals(true);
553 myAutoConstraints->setChecked(aModule->sketchReentranceMgr()->isAutoConstraints());
554 myAutoConstraints->blockSignals(isBlocked);
557 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
559 myStackWidget->setCurrentIndex(1);
563 myStackWidget->setCurrentIndex(0);
564 bool aBodyIsVisualized = myPreviewPlanes->hasVisualizedBodies(myWorkshop);
566 // Clear previous selection mode It is necessary for correct activation of preview planes
567 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
568 XGUI_Displayer* aDisp = aWorkshop->displayer();
569 aWorkshop->selectionActivate()->activateObjects(QIntList(), aDisp->displayedObjects(), false);
571 if (!aBodyIsVisualized) {
572 // We have to select a plane before any operation
573 myPreviewPlanes->showPreviewPlanes(myWorkshop);
574 mySizeOfViewWidget->setVisible(true);
577 mySizeOfViewWidget->setVisible(false);
580 void PartSet_WidgetSketchLabel::showEvent(QShowEvent* theEvent)
582 if (mySizeOfViewWidget->isVisible()) {
583 DocumentPtr aDoc = feature()->document();
584 DocumentPtr aModDoc = ModelAPI_Session::get()->moduleDocument();
585 if (aModDoc == aDoc) {
586 myPartSetMessage->move(mapToGlobal(geometry().bottomLeft()));
587 myPartSetMessage->show();
589 mySizeMessage->move(mySizeOfView->mapToGlobal(mySizeOfView->geometry().center()));
590 mySizeMessage->show();
595 void PartSet_WidgetSketchLabel::deactivate()
597 ModuleBase_WidgetValidated::deactivate();
598 bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
599 myPreviewPlanes->erasePreviewPlanes(myWorkshop);
602 myWorkshop->viewer()->update();
604 if (myOpenTransaction) {
605 SessionPtr aMgr = ModelAPI_Session::get();
606 aMgr->finishOperation();
607 myOpenTransaction = false;
611 void PartSet_WidgetSketchLabel::selectionModes(int& theModuleSelectionModes, QIntList& theModes)
613 theModuleSelectionModes = -1;
614 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
616 theModes << TopAbs_FACE;
619 void PartSet_WidgetSketchLabel::selectionFilters(QIntList& theModuleSelectionFilters,
620 SelectMgr_ListOfFilter& theSelectionFilters)
622 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
625 return ModuleBase_WidgetValidated::selectionFilters(theModuleSelectionFilters,
626 theSelectionFilters);
629 std::shared_ptr<GeomAPI_Dir>
630 PartSet_WidgetSketchLabel::setSketchPlane(const TopoDS_Shape& theShape)
632 if (theShape.IsNull())
633 return std::shared_ptr<GeomAPI_Dir>();
635 // get selected shape
636 std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
637 aGShape->setImpl(new TopoDS_Shape(theShape));
639 // get plane parameters
640 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aGShape));
641 std::shared_ptr<GeomAPI_Pln> aPlane = aFace->getPlane();
643 return std::shared_ptr<GeomAPI_Dir>();
644 return setSketchPlane(aPlane);
647 std::shared_ptr<GeomAPI_Dir>
648 PartSet_WidgetSketchLabel::setSketchPlane(std::shared_ptr<GeomAPI_Pln> thePlane)
650 // set plane parameters to feature
651 std::shared_ptr<ModelAPI_Data> aData = feature()->data();
652 double anA, aB, aC, aD;
653 thePlane->coefficients(anA, aB, aC, aD);
655 // calculate attributes of the sketch
656 std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
657 std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
658 std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
659 aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
660 std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
661 // X axis is preferable to be dirX on the sketch
662 const double tol = Precision::Confusion();
663 bool isX = fabs(fabs(anA) - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
664 std::shared_ptr<GeomAPI_Dir> aTempDir(
665 isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
666 std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
667 std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
669 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
670 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
671 anOrigin->setValue(anOrigPnt);
672 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
673 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
674 aNormal->setValue(aNormDir);
675 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
676 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
677 aDirX->setValue(aXDir);
678 std::shared_ptr<GeomAPI_Dir> aDir = thePlane->direction();
682 void PartSet_WidgetSketchLabel::onSetPlaneView()
684 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
686 std::shared_ptr<GeomAPI_Dir> aDirection = aPlane->direction();
687 gp_Dir aDir = aDirection->impl<gp_Dir>();
688 if (myViewInverted->isChecked())
690 myWorkshop->viewer()->setViewProjection(aDir.X(), aDir.Y(), aDir.Z(), 0.);
691 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
693 aModule->onViewTransformed();
698 //******************************************************
699 QList<std::shared_ptr<ModuleBase_ViewerPrs>> PartSet_WidgetSketchLabel::findCircularEdgesInPlane()
701 QList<std::shared_ptr<ModuleBase_ViewerPrs>> aResult;
702 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
703 XGUI_Displayer* aDisplayer = aWorkshop->displayer();
704 QObjectPtrList aDispObjects = aDisplayer->displayedObjects();
706 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
707 foreach(ObjectPtr aObj, aDispObjects) {
708 ResultPtr aResObj = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
710 GeomShapePtr aShape = aResObj->shape();
712 GeomAPI_ShapeExplorer aExplorer(aShape, GeomAPI_Shape::EDGE);
713 for(; aExplorer.more(); aExplorer.next()) {
714 GeomShapePtr aEdgeShape = aExplorer.current();
715 GeomAPI_Edge anEdge(aEdgeShape);
716 if ((anEdge.isCircle() || anEdge.isArc() || anEdge.isEllipse()) &&
717 anEdge.isInPlane(aPlane)) {
718 bool isContains = false;
719 // Check that edge is not used.
720 // It is possible that the same edge will be taken from different faces
721 foreach(std::shared_ptr<ModuleBase_ViewerPrs> aPrs, aResult) {
722 GeomAPI_Edge aUsedEdge(aPrs->shape());
723 if (aUsedEdge.isEqual(aEdgeShape)) {
729 std::shared_ptr<ModuleBase_ViewerPrs>
730 aPrs(new ModuleBase_ViewerPrs(aResObj, aEdgeShape));
731 aResult.append(aPrs);
741 //******************************************************
742 void PartSet_WidgetSketchLabel::onChangePlane()
744 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
746 mySizeOfViewWidget->setVisible(false);
747 myRemoveExternal->setVisible(true);
748 myStackWidget->setCurrentIndex(0);
750 bool aBodyIsVisualized = myPreviewPlanes->hasVisualizedBodies(myWorkshop);
752 if (!aBodyIsVisualized) {
753 // We have to select a plane before any operation
754 myPreviewPlanes->showPreviewPlanes(myWorkshop);
757 CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
758 PartSet_Tools::nullifySketchPlane(aSketch);
760 Handle(SelectMgr_Filter) aFilter = aModule->selectionFilter(SF_SketchPlaneFilter);
761 if (!aFilter.IsNull()) {
762 std::shared_ptr<GeomAPI_Pln> aPln;
763 Handle(ModuleBase_ShapeInPlaneFilter)::DownCast(aFilter)->setPlane(aPln);
765 XGUI_Workshop* aWorkshop = aModule->getWorkshop();
767 aWorkshop->selectionActivate()->updateSelectionFilters();
768 aWorkshop->selectionActivate()->updateSelectionModes();
770 SessionPtr aMgr = ModelAPI_Session::get();
771 aMgr->startOperation("Change Sketch plane");
772 myOpenTransaction = true;
773 myWorkshop->viewer()->update();
777 void PartSet_WidgetSketchLabel::setShowPointsState(bool theState)
779 bool aBlock = myShowPoints->blockSignals(true);
780 myShowPoints->setChecked(theState);
781 myShowPoints->blockSignals(aBlock);
784 bool PartSet_WidgetSketchLabel::restoreValueCustom()
786 if (myFeature.get()) {
787 CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
788 if (aSketch.get() && (aSketch->numberOfSubs() > 0)) {
789 AttributeStringPtr aDOFStr = aSketch->string("SolverDOF");
791 QString aVal(aDOFStr->value().c_str());
792 if (aVal.contains('=')) {
793 // to support old data
794 aVal = aVal.right(aVal.length() - aVal.lastIndexOf('='));
796 int aDoF = aVal.toInt();
798 myDoFLabel->setText(tr("Sketch is fully fixed (DoF = 0)"));
799 myShowDOFBtn->setEnabled(false);
801 myDoFLabel->setText(tr("DoF (degrees of freedom) = ") + aVal);
802 myShowDOFBtn->setEnabled(true);
807 myDoFLabel->setText("");
808 myShowDOFBtn->setEnabled(false);
815 void PartSet_WidgetSketchLabel::onShowDOF()
817 CompositeFeaturePtr aCompFeature =
818 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
819 if (aCompFeature.get()) {
820 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_GET_DOF_OBJECTS);
821 ModelAPI_EventCreator::get()->sendUpdated(aCompFeature, anEvent);
822 Events_Loop::loop()->flush(anEvent);