1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: PartSet_WidgetSketchLabel.cpp
4 // Created: 07 July 2014
5 // Author: Vitaly SMETANNIKOV
7 #include "PartSet_WidgetSketchLabel.h"
8 #include "PartSet_Tools.h"
9 #include "PartSet_Module.h"
10 #include "PartSet_PreviewPlanes.h"
12 #include "SketchPlugin_SketchEntity.h"
14 #include <XGUI_Workshop.h>
15 #include <XGUI_Displayer.h>
16 #include <XGUI_SelectionMgr.h>
17 #include <XGUI_Selection.h>
18 #include <XGUI_ViewerProxy.h>
19 #include <XGUI_ActionsMgr.h>
20 #include <XGUI_Tools.h>
21 #include <XGUI_ModuleConnector.h>
23 #include <ModuleBase_Operation.h>
24 #include <ModuleBase_ViewerPrs.h>
25 #include <ModuleBase_Tools.h>
26 #include <ModuleBase_IModule.h>
28 #include <ModelAPI_ResultBody.h>
29 #include <ModelAPI_Tools.h>
31 #include <GeomAlgoAPI_FaceBuilder.h>
32 #include <GeomAlgoAPI_ShapeTools.h>
33 #include <GeomDataAPI_Point.h>
34 #include <GeomDataAPI_Dir.h>
35 #include <GeomAPI_XYZ.h>
37 #include <SketchPlugin_Sketch.h>
38 #include <SketcherPrs_Tools.h>
40 #include <Precision.hxx>
44 #include <AIS_Shape.hxx>
45 #include <AIS_DimensionSelectionMode.hxx>
47 #include <Config_WidgetAPI.h>
48 #include <Config_PropManager.h>
51 #include <QApplication>
52 #include <QVBoxLayout>
55 #include <QPushButton>
56 #include <QStackedWidget>
59 PartSet_WidgetSketchLabel::PartSet_WidgetSketchLabel(QWidget* theParent,
60 ModuleBase_IWorkshop* theWorkshop,
61 const Config_WidgetAPI* theData,
62 const QMap<PartSet_Tools::ConstraintVisibleState, bool>& toShowConstraints)
63 : ModuleBase_WidgetValidated(theParent, theWorkshop, theData)
65 QVBoxLayout* aLayout = new QVBoxLayout(this);
66 ModuleBase_Tools::zeroMargins(aLayout);
68 myStackWidget = new QStackedWidget(this);
69 myStackWidget->setContentsMargins(0,0,0,0);
70 aLayout->addWidget(myStackWidget);
72 // Define label for plane selection
73 QWidget* aFirstWgt = new QWidget(this);
75 QString aText = QString::fromStdString(theData->getProperty("title"));
76 QLabel* aLabel = new QLabel(aText, aFirstWgt);
77 aLabel->setWordWrap(true);
78 QString aTooltip = QString::fromStdString(theData->getProperty("tooltip"));
79 aLabel->setToolTip(aTooltip);
82 aLayout = new QVBoxLayout(aFirstWgt);
83 ModuleBase_Tools::zeroMargins(aLayout);
84 aLayout->addWidget(aLabel);
86 myStackWidget->addWidget(aFirstWgt);
88 // Define widget for sketch manmagement
89 QWidget* aSecondWgt = new QWidget(this);
90 aLayout = new QVBoxLayout(aSecondWgt);
91 ModuleBase_Tools::zeroMargins(aLayout);
93 QGroupBox* aViewBox = new QGroupBox(tr("Sketcher plane"), this);
94 QVBoxLayout* aViewLayout = new QVBoxLayout(aViewBox);
96 myViewInverted = new QCheckBox(tr("Reversed"), aViewBox);
97 aViewLayout->addWidget(myViewInverted);
99 QPushButton* aSetViewBtn = new QPushButton(QIcon(":icons/plane_view.png"), tr("Set plane view"), aViewBox);
100 connect(aSetViewBtn, SIGNAL(clicked(bool)), this, SLOT(onSetPlaneView()));
101 aViewLayout->addWidget(aSetViewBtn);
103 aLayout->addWidget(aViewBox);
105 QMap<PartSet_Tools::ConstraintVisibleState, QString> aStates;
106 aStates[PartSet_Tools::Geometrical] = tr("Show geometrical constraints");
107 aStates[PartSet_Tools::Dimensional] = tr("Show dimensional constraints");
108 aStates[PartSet_Tools::Expressions] = tr("Show existing expressions");
110 QMap<PartSet_Tools::ConstraintVisibleState, QString>::const_iterator anIt = aStates.begin(),
111 aLast = aStates.end();
112 for (; anIt != aLast; anIt++) {
113 QCheckBox* aShowConstraints = new QCheckBox(anIt.value(), this);
114 connect(aShowConstraints, SIGNAL(toggled(bool)), this, SLOT(onShowConstraint(bool)));
115 aLayout->addWidget(aShowConstraints);
117 PartSet_Tools::ConstraintVisibleState aState = anIt.key();
118 myShowConstraints[aState] = aShowConstraints;
120 if (toShowConstraints.contains(aState))
121 aShowConstraints->setChecked(toShowConstraints[aState]);
124 myStackWidget->addWidget(aSecondWgt);
125 //setLayout(aLayout);
127 myPreviewPlanes = new PartSet_PreviewPlanes();
130 PartSet_WidgetSketchLabel::~PartSet_WidgetSketchLabel()
134 bool PartSet_WidgetSketchLabel::setSelection(QList<ModuleBase_ViewerPrs>& theValues,
135 const bool theToValidate)
137 // do not use the given selection if the plane of the sketch has been already set.
138 // If this check is absent, a selected plane in the viewer can be set in the sketch
139 // even if the sketch is built on another plane.
143 ModuleBase_ViewerPrs aPrs = theValues.first();
144 bool aDone = ModuleBase_WidgetValidated::setSelection(theValues, theToValidate);
146 updateByPlaneSelected(aPrs);
151 QList<QWidget*> PartSet_WidgetSketchLabel::getControls() const
153 QList<QWidget*> aResult;
154 aResult << myStackWidget;
158 void PartSet_WidgetSketchLabel::onSelectionChanged()
160 QList<ModuleBase_ViewerPrs> aSelected = getFilteredSelected();
162 if (aSelected.empty())
164 ModuleBase_ViewerPrs aPrs = aSelected.first();
166 bool aDone = ModuleBase_WidgetValidated::setSelection(aSelected, false);
168 updateByPlaneSelected(aPrs);
169 updateObject(myFeature);
173 void PartSet_WidgetSketchLabel::onShowConstraint(bool theOn)
175 QCheckBox* aSenderCheckBox = qobject_cast<QCheckBox*>(sender());
177 QMap<PartSet_Tools::ConstraintVisibleState, QCheckBox*>::const_iterator
178 anIt = myShowConstraints.begin(), aLast = myShowConstraints.end();
180 PartSet_Tools::ConstraintVisibleState aState = PartSet_Tools::Geometrical;
182 for (; anIt != aLast && !aFound; anIt++) {
183 aFound = anIt.value() == aSenderCheckBox;
188 emit showConstraintToggled(aState, theOn);
191 void PartSet_WidgetSketchLabel::blockAttribute(const bool& theToBlock, bool& isFlushesActived,
192 bool& isAttributeSetInitializedBlocked)
194 ModuleBase_WidgetValidated::blockAttribute(theToBlock, isFlushesActived,
195 isAttributeSetInitializedBlocked);
196 // We do not restore the previous state of isAttributeSetInitializedBlocked for each of
197 // attributes. It it is necessary, these states should be append to the method attributes
198 // or stored in the widget
200 std::list<AttributePtr> anAttributes = myFeature->data()->attributes("");
201 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
203 for(; anIt != aLast; anIt++) {
204 AttributePtr anAttribute = *anIt;
206 anAttribute->blockSetInitialized(true);
208 anAttribute->blockSetInitialized(isAttributeSetInitializedBlocked);
212 void PartSet_WidgetSketchLabel::updateByPlaneSelected(const ModuleBase_ViewerPrs& thePrs)
214 // 1. hide main planes if they have been displayed
215 myPreviewPlanes->erasePreviewPlanes(myWorkshop);
216 // 2. if the planes were displayed, change the view projection
217 const GeomShapePtr& aShape = thePrs.shape();
218 std::shared_ptr<GeomAPI_Shape> aGShape;
219 std::shared_ptr<GeomAPI_Shape> aBaseShape;
221 DataPtr aData = feature()->data();
222 AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
223 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
225 // selection happens in OCC viewer
226 if (aShape.get() && !aShape->isNull()) {
229 if (aSelAttr && aSelAttr->context()) {
230 aBaseShape = aSelAttr->context()->shape();
233 else { // selection happens in OCC viewer(on body) of in the OB browser
235 aGShape = aSelAttr->value();
238 if (aGShape.get() != NULL) {
239 // get plane parameters
240 std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
241 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
242 gp_XYZ aXYZ = aDir->impl<gp_Dir>().XYZ();
245 // orienting projection is not needed: it is done in GeomAlgoAPI_FaceBuilder::plane
246 /*if (aGShape->impl<TopoDS_Shape>().Orientation() == TopAbs_REVERSED) {
250 // Rotate view if the sketcher plane is selected only from preview planes
251 // Preview planes are created only if there is no any shape
252 bool aRotate = Config_PropManager::boolean("Sketch planes", "rotate_to_plane", "false");
254 myWorkshop->viewer()->setViewProjection(aXYZ.X(), aXYZ.Y(), aXYZ.Z(), aTwist);
255 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
257 aModule->onViewTransformed();
260 // 3. Clear text in the label
261 myStackWidget->setCurrentIndex(1);
262 //myLabel->setText("");
263 //myLabel->setToolTip("");
264 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
265 disconnect(aWorkshop->selector(), SIGNAL(selectionChanged()),
266 this, SLOT(onSelectionChanged()));
267 // 4. deactivate face selection filter
268 activateFilters(false);
270 // 5. Clear selection mode and define sketching mode
271 //XGUI_Displayer* aDisp = aWorkshop->displayer();
272 //aDisp->closeLocalContexts();
273 emit planeSelected(plane());
274 // after the plane is selected in the sketch, the sketch selection should be activated
275 // it can not be performed in the sketch label widget because, we don't need to switch off
276 // the selection by any label deactivation, but need to switch it off by stop the sketch
277 activateSelection(true);
279 // 6. Update sketcher actions
280 XGUI_ActionsMgr* anActMgr = aWorkshop->actionsMgr();
281 myWorkshop->updateCommandStatus();
282 myWorkshop->viewer()->update();
285 std::shared_ptr<GeomAPI_Pln> PartSet_WidgetSketchLabel::plane() const
287 CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
288 return PartSet_Tools::sketchPlane(aSketch);
291 bool PartSet_WidgetSketchLabel::focusTo()
293 ModuleBase_Tools::setFocus(myStackWidget, "PartSet_WidgetSketchLabel::focusTo()");
297 void PartSet_WidgetSketchLabel::enableFocusProcessing()
299 myStackWidget->installEventFilter(this);
302 void PartSet_WidgetSketchLabel::storeAttributeValue()
304 ModuleBase_WidgetValidated::storeAttributeValue();
307 void PartSet_WidgetSketchLabel::restoreAttributeValue(const bool theValid)
309 ModuleBase_WidgetValidated::restoreAttributeValue(theValid);
311 // it is not necessary to save the previous plane value because the plane is chosen once
312 DataPtr aData = feature()->data();
313 AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
314 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
316 ResultPtr anEmptyResult;
317 GeomShapePtr anEmptyShape;
318 aSelAttr->setValue(anEmptyResult, anEmptyShape);
322 bool PartSet_WidgetSketchLabel::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
324 return fillSketchPlaneBySelection(feature(), thePrs);
327 #include <GeomAPI_Face.h>
328 bool PartSet_WidgetSketchLabel::canFillSketch(const ModuleBase_ViewerPrs& thePrs)
330 bool aCanFillSketch = true;
331 // avoid any selection on sketch object
332 ObjectPtr anObject = thePrs.object();
333 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
335 FeaturePtr aFeature = ModelAPI_Feature::feature(aResult);
336 if (aFeature->getKind() == SketchPlugin_Sketch::ID())
337 aCanFillSketch = false;
339 // check plane or planar face of any non-sketch object
340 if (aCanFillSketch) {
341 std::shared_ptr<GeomAPI_Face> aGeomFace;
342 const TopoDS_Shape aShape = thePrs.shape();
343 if (aShape.IsNull()) {
344 GeomShapePtr aGeomShape = aResult->shape();
345 std::shared_ptr<GeomAPI_Face> aGeomFace(new GeomAPI_Face(aGeomShape));
346 aCanFillSketch = aGeomFace.get() && aGeomFace->isPlanar();
348 else if (aShape.ShapeType() == TopAbs_FACE) {
349 std::shared_ptr<GeomAPI_Face> aGeomFace(new GeomAPI_Face());
350 aGeomFace->setImpl(new TopoDS_Shape(aShape));
351 aCanFillSketch = aGeomFace.get() && aGeomFace->isPlanar();
354 aCanFillSketch = false;
356 return aCanFillSketch;
359 bool PartSet_WidgetSketchLabel::fillSketchPlaneBySelection(const FeaturePtr& theFeature,
360 const ModuleBase_ViewerPrs& thePrs)
362 bool isOwnerSet = false;
364 const GeomShapePtr& aShape = thePrs.shape();
365 std::shared_ptr<GeomAPI_Dir> aDir;
367 if (thePrs.object() && (theFeature != thePrs.object())) {
368 DataPtr aData = theFeature->data();
369 AttributeSelectionPtr aSelAttr =
370 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
371 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
373 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(thePrs.object());
375 GeomShapePtr aShapePtr(new GeomAPI_Shape());
376 if (!aShape.get() || aShape->isNull()) { // selection happens in the OCC viewer
377 aShapePtr = ModelAPI_Tools::shape(aRes);
379 else { // selection happens in OB browser
382 if (aShapePtr.get() != NULL) {
383 aSelAttr->setValue(aRes, aShapePtr);
389 else if (aShape.get() && !aShape->isNull()) {
390 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
391 aDir = setSketchPlane(aTDShape);
392 isOwnerSet = aDir.get();
397 void PartSet_WidgetSketchLabel::activateCustom()
399 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
401 myStackWidget->setCurrentIndex(1);
402 activateSelection(true);
406 myStackWidget->setCurrentIndex(0);
407 bool aBodyIsVisualized = myPreviewPlanes->hasVisualizedBodies(myWorkshop);
408 if (!aBodyIsVisualized) {
409 // We have to select a plane before any operation
410 myPreviewPlanes->showPreviewPlanes(myWorkshop);
412 activateSelection(true);
414 //myLabel->setText(myText);
415 //myLabel->setToolTip(myTooltip);
417 connect(XGUI_Tools::workshop(myWorkshop)->selector(), SIGNAL(selectionChanged()),
418 this, SLOT(onSelectionChanged()));
419 activateFilters(true);
422 void PartSet_WidgetSketchLabel::deactivate()
424 ModuleBase_ModelWidget::deactivate();
425 bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
426 myPreviewPlanes->erasePreviewPlanes(myWorkshop);
427 activateSelection(false);
429 activateFilters(false);
431 myWorkshop->viewer()->update();
434 void PartSet_WidgetSketchLabel::activateSelection(bool toActivate)
438 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
440 myWorkshop->module()->activeSelectionModes(aModes);
443 aModes << TopAbs_FACE;
445 myWorkshop->activateSubShapesSelection(aModes);
447 myWorkshop->deactivateSubShapesSelection();
452 std::shared_ptr<GeomAPI_Dir> PartSet_WidgetSketchLabel::setSketchPlane(const FeaturePtr& theFeature,
453 const TopoDS_Shape& theShape)
455 if (theShape.IsNull())
456 return std::shared_ptr<GeomAPI_Dir>();
458 // get selected shape
459 std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
460 aGShape->setImpl(new TopoDS_Shape(theShape));
464 // get plane parameters
465 std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
467 return std::shared_ptr<GeomAPI_Dir>();
469 // set plane parameters to feature
470 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
471 double anA, aB, aC, aD;
472 aPlane->coefficients(anA, aB, aC, aD);
474 // calculate attributes of the sketch
475 std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
476 std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
477 std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
478 aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
479 std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
480 // X axis is preferable to be dirX on the sketch
481 const double tol = Precision::Confusion();
482 bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
483 std::shared_ptr<GeomAPI_Dir> aTempDir(
484 isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
485 std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
486 std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
488 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
489 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
490 anOrigin->setValue(anOrigPnt);
491 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
492 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
493 aNormal->setValue(aNormDir);
494 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
495 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
496 aDirX->setValue(aXDir);
497 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
501 void PartSet_WidgetSketchLabel::onSetPlaneView()
503 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
505 std::shared_ptr<GeomAPI_Dir> aDirection = aPlane->direction();
506 gp_Dir aDir = aDirection->impl<gp_Dir>();
507 if (myViewInverted->isChecked())
509 myWorkshop->viewer()->setViewProjection(aDir.X(), aDir.Y(), aDir.Z(), 0.);
510 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
512 aModule->onViewTransformed();