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"
11 #include "SketchPlugin_SketchEntity.h"
13 #include <XGUI_Workshop.h>
14 #include <XGUI_Displayer.h>
15 #include <XGUI_SelectionMgr.h>
16 #include <XGUI_Selection.h>
17 #include <XGUI_ViewerProxy.h>
18 #include <XGUI_ActionsMgr.h>
19 #include <XGUI_ModuleConnector.h>
21 #include <ModuleBase_Operation.h>
22 #include <ModuleBase_ViewerPrs.h>
23 #include <ModuleBase_Tools.h>
24 #include <ModuleBase_IModule.h>
26 #include <ModelAPI_ResultBody.h>
27 #include <ModelAPI_Tools.h>
29 #include <GeomAlgoAPI_FaceBuilder.h>
30 #include <GeomAlgoAPI_ShapeTools.h>
31 #include <GeomDataAPI_Point.h>
32 #include <GeomDataAPI_Dir.h>
33 #include <GeomAPI_XYZ.h>
35 #include <SketchPlugin_Sketch.h>
36 #include <SketcherPrs_Tools.h>
38 #include <Precision.hxx>
42 #include <AIS_Shape.hxx>
43 #include <AIS_DimensionSelectionMode.hxx>
45 #include <Config_WidgetAPI.h>
46 #include <Config_PropManager.h>
49 #include <QApplication>
50 #include <QVBoxLayout>
53 #include <QPushButton>
54 #include <QStackedWidget>
57 PartSet_WidgetSketchLabel::PartSet_WidgetSketchLabel(QWidget* theParent,
58 ModuleBase_IWorkshop* theWorkshop,
59 const Config_WidgetAPI* theData,
60 const QMap<PartSet_Tools::ConstraintVisibleState, bool>& toShowConstraints)
61 : ModuleBase_WidgetValidated(theParent, theWorkshop, theData),
62 myPreviewDisplayed(false)
64 QVBoxLayout* aLayout = new QVBoxLayout(this);
65 ModuleBase_Tools::zeroMargins(aLayout);
67 myStackWidget = new QStackedWidget(this);
68 myStackWidget->setContentsMargins(0,0,0,0);
69 aLayout->addWidget(myStackWidget);
71 // Define label for plane selection
72 QWidget* aFirstWgt = new QWidget(this);
74 QString aText = QString::fromStdString(theData->getProperty("title"));
75 QLabel* aLabel = new QLabel(aText, aFirstWgt);
76 aLabel->setWordWrap(true);
77 QString aTooltip = QString::fromStdString(theData->getProperty("tooltip"));
78 aLabel->setToolTip(aTooltip);
81 aLayout = new QVBoxLayout(aFirstWgt);
82 ModuleBase_Tools::zeroMargins(aLayout);
83 aLayout->addWidget(aLabel);
85 myStackWidget->addWidget(aFirstWgt);
87 // Define widget for sketch manmagement
88 QWidget* aSecondWgt = new QWidget(this);
89 aLayout = new QVBoxLayout(aSecondWgt);
90 ModuleBase_Tools::zeroMargins(aLayout);
92 QGroupBox* aViewBox = new QGroupBox(tr("Sketcher plane"), this);
93 QVBoxLayout* aViewLayout = new QVBoxLayout(aViewBox);
95 myViewInverted = new QCheckBox(tr("Reversed"), aViewBox);
96 aViewLayout->addWidget(myViewInverted);
98 QPushButton* aSetViewBtn = new QPushButton(QIcon(":icons/plane_view.png"), tr("Set plane view"), aViewBox);
99 connect(aSetViewBtn, SIGNAL(clicked(bool)), this, SLOT(onSetPlaneView()));
100 aViewLayout->addWidget(aSetViewBtn);
102 aLayout->addWidget(aViewBox);
104 QMap<PartSet_Tools::ConstraintVisibleState, QString> aStates;
105 aStates[PartSet_Tools::Geometrical] = tr("Show geometrical constraints");
106 aStates[PartSet_Tools::Dimensional] = tr("Show dimensional constraints");
107 aStates[PartSet_Tools::Expressions] = tr("Show existing expressions");
109 QMap<PartSet_Tools::ConstraintVisibleState, QString>::const_iterator anIt = aStates.begin(),
110 aLast = aStates.end();
111 for (; anIt != aLast; anIt++) {
112 QCheckBox* aShowConstraints = new QCheckBox(anIt.value(), this);
113 connect(aShowConstraints, SIGNAL(toggled(bool)), this, SLOT(onShowConstraint(bool)));
114 aLayout->addWidget(aShowConstraints);
116 PartSet_Tools::ConstraintVisibleState aState = anIt.key();
117 myShowConstraints[aState] = aShowConstraints;
119 if (toShowConstraints.contains(aState))
120 aShowConstraints->setChecked(toShowConstraints[aState]);
123 myStackWidget->addWidget(aSecondWgt);
124 //setLayout(aLayout);
127 PartSet_WidgetSketchLabel::~PartSet_WidgetSketchLabel()
131 bool PartSet_WidgetSketchLabel::setSelection(QList<ModuleBase_ViewerPrs>& theValues,
132 const bool theToValidate)
134 // do not use the given selection if the plane of the sketch has been already set.
135 // If this check is absent, a selected plane in the viewer can be set in the sketch
136 // even if the sketch is built on another plane.
140 ModuleBase_ViewerPrs aPrs = theValues.first();
141 bool aDone = ModuleBase_WidgetValidated::setSelection(theValues, theToValidate);
143 updateByPlaneSelected(aPrs);
148 QList<QWidget*> PartSet_WidgetSketchLabel::getControls() const
150 QList<QWidget*> aResult;
151 aResult << myStackWidget;
155 void PartSet_WidgetSketchLabel::onSelectionChanged()
157 QList<ModuleBase_ViewerPrs> aSelected = getFilteredSelected();
159 if (aSelected.empty())
161 ModuleBase_ViewerPrs aPrs = aSelected.first();
163 bool aDone = ModuleBase_WidgetValidated::setSelection(aSelected, false);
165 updateByPlaneSelected(aPrs);
166 updateObject(myFeature);
170 void PartSet_WidgetSketchLabel::onShowConstraint(bool theOn)
172 QCheckBox* aSenderCheckBox = qobject_cast<QCheckBox*>(sender());
174 QMap<PartSet_Tools::ConstraintVisibleState, QCheckBox*>::const_iterator
175 anIt = myShowConstraints.begin(), aLast = myShowConstraints.end();
177 PartSet_Tools::ConstraintVisibleState aState = PartSet_Tools::Geometrical;
179 for (; anIt != aLast && !aFound; anIt++) {
180 aFound = anIt.value() == aSenderCheckBox;
185 emit showConstraintToggled(aState, theOn);
188 void PartSet_WidgetSketchLabel::blockAttribute(const bool& theToBlock, bool& isFlushesActived,
189 bool& isAttributeSetInitializedBlocked)
191 ModuleBase_WidgetValidated::blockAttribute(theToBlock, isFlushesActived,
192 isAttributeSetInitializedBlocked);
193 // We do not restore the previous state of isAttributeSetInitializedBlocked for each of
194 // attributes. It it is necessary, these states should be append to the method attributes
195 // or stored in the widget
197 std::list<AttributePtr> anAttributes = myFeature->data()->attributes("");
198 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
200 for(; anIt != aLast; anIt++) {
201 AttributePtr anAttribute = *anIt;
203 anAttribute->blockSetInitialized(true);
205 anAttribute->blockSetInitialized(isAttributeSetInitializedBlocked);
209 void PartSet_WidgetSketchLabel::updateByPlaneSelected(const ModuleBase_ViewerPrs& thePrs)
211 // 1. hide main planes if they have been displayed
212 erasePreviewPlanes();
213 // 2. if the planes were displayed, change the view projection
214 const GeomShapePtr& aShape = thePrs.shape();
215 std::shared_ptr<GeomAPI_Shape> aGShape;
216 std::shared_ptr<GeomAPI_Shape> aBaseShape;
218 DataPtr aData = feature()->data();
219 AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
220 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
222 // selection happens in OCC viewer
223 if (aShape.get() && !aShape->isNull()) {
226 if (aSelAttr && aSelAttr->context()) {
227 aBaseShape = aSelAttr->context()->shape();
230 else { // selection happens in OCC viewer(on body) of in the OB browser
232 aGShape = aSelAttr->value();
235 if (aGShape.get() != NULL) {
236 // get plane parameters
237 std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
238 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
239 gp_XYZ aXYZ = aDir->impl<gp_Dir>().XYZ();
242 // orienting projection is not needed: it is done in GeomAlgoAPI_FaceBuilder::plane
243 /*if (aGShape->impl<TopoDS_Shape>().Orientation() == TopAbs_REVERSED) {
247 // Rotate view if the sketcher plane is selected only from preview planes
248 // Preview planes are created only if there is no any shape
249 bool aRotate = Config_PropManager::boolean("Sketch planes", "rotate_to_plane", "false");
251 myWorkshop->viewer()->setViewProjection(aXYZ.X(), aXYZ.Y(), aXYZ.Z(), aTwist);
252 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
254 aModule->onViewTransformed();
257 // 3. Clear text in the label
258 myStackWidget->setCurrentIndex(1);
259 //myLabel->setText("");
260 //myLabel->setToolTip("");
261 disconnect(workshop()->selector(), SIGNAL(selectionChanged()),
262 this, SLOT(onSelectionChanged()));
263 // 4. deactivate face selection filter
264 activateFilters(false);
266 // 5. Clear selection mode and define sketching mode
267 //XGUI_Displayer* aDisp = workshop()->displayer();
268 //aDisp->closeLocalContexts();
269 emit planeSelected(plane());
270 // after the plane is selected in the sketch, the sketch selection should be activated
271 // it can not be performed in the sketch label widget because, we don't need to switch off
272 // the selection by any label deactivation, but need to switch it off by stop the sketch
273 activateSelection(true);
275 // 6. Update sketcher actions
276 XGUI_ActionsMgr* anActMgr = workshop()->actionsMgr();
277 myWorkshop->updateCommandStatus();
278 myWorkshop->viewer()->update();
281 std::shared_ptr<GeomAPI_Pln> PartSet_WidgetSketchLabel::plane() const
283 CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
284 return PartSet_Tools::sketchPlane(aSketch);
287 bool PartSet_WidgetSketchLabel::focusTo()
289 ModuleBase_Tools::setFocus(myStackWidget, "PartSet_WidgetSketchLabel::focusTo()");
293 void PartSet_WidgetSketchLabel::enableFocusProcessing()
295 myStackWidget->installEventFilter(this);
298 void PartSet_WidgetSketchLabel::storeAttributeValue()
300 ModuleBase_WidgetValidated::storeAttributeValue();
303 void PartSet_WidgetSketchLabel::restoreAttributeValue(const bool theValid)
305 ModuleBase_WidgetValidated::restoreAttributeValue(theValid);
307 // it is not necessary to save the previous plane value because the plane is chosen once
308 DataPtr aData = feature()->data();
309 AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
310 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
312 ResultPtr anEmptyResult;
313 GeomShapePtr anEmptyShape;
314 aSelAttr->setValue(anEmptyResult, anEmptyShape);
318 bool PartSet_WidgetSketchLabel::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
320 return fillSketchPlaneBySelection(feature(), thePrs);
323 #include <GeomAPI_Face.h>
324 bool PartSet_WidgetSketchLabel::canFillSketch(const ModuleBase_ViewerPrs& thePrs)
326 bool aCanFillSketch = true;
327 // avoid any selection on sketch object
328 ObjectPtr anObject = thePrs.object();
329 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
331 FeaturePtr aFeature = ModelAPI_Feature::feature(aResult);
332 if (aFeature->getKind() == SketchPlugin_Sketch::ID())
333 aCanFillSketch = false;
335 // check plane or planar face of any non-sketch object
336 if (aCanFillSketch) {
337 std::shared_ptr<GeomAPI_Face> aGeomFace;
338 const TopoDS_Shape aShape = thePrs.shape();
339 if (aShape.IsNull()) {
340 GeomShapePtr aGeomShape = aResult->shape();
341 std::shared_ptr<GeomAPI_Face> aGeomFace(new GeomAPI_Face(aGeomShape));
342 aCanFillSketch = aGeomFace.get() && aGeomFace->isPlanar();
344 else if (aShape.ShapeType() == TopAbs_FACE) {
345 std::shared_ptr<GeomAPI_Face> aGeomFace(new GeomAPI_Face());
346 aGeomFace->setImpl(new TopoDS_Shape(aShape));
347 aCanFillSketch = aGeomFace.get() && aGeomFace->isPlanar();
350 aCanFillSketch = false;
352 return aCanFillSketch;
355 bool PartSet_WidgetSketchLabel::fillSketchPlaneBySelection(const FeaturePtr& theFeature,
356 const ModuleBase_ViewerPrs& thePrs)
358 bool isOwnerSet = false;
360 const GeomShapePtr& aShape = thePrs.shape();
361 std::shared_ptr<GeomAPI_Dir> aDir;
363 if (thePrs.object() && (theFeature != thePrs.object())) {
364 DataPtr aData = theFeature->data();
365 AttributeSelectionPtr aSelAttr =
366 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
367 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
369 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(thePrs.object());
371 GeomShapePtr aShapePtr(new GeomAPI_Shape());
372 if (!aShape.get() || aShape->isNull()) { // selection happens in the OCC viewer
373 aShapePtr = ModelAPI_Tools::shape(aRes);
375 else { // selection happens in OB browser
378 if (aShapePtr.get() != NULL) {
379 aSelAttr->setValue(aRes, aShapePtr);
385 else if (aShape.get() && !aShape->isNull()) {
386 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
387 aDir = setSketchPlane(aTDShape);
388 isOwnerSet = aDir.get();
393 void PartSet_WidgetSketchLabel::activateCustom()
395 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
397 myStackWidget->setCurrentIndex(1);
398 activateSelection(true);
402 myStackWidget->setCurrentIndex(0);
403 bool aBodyIsVisualized = false;
404 XGUI_Displayer* aDisp = workshop()->displayer();
405 QObjectPtrList aDisplayed = aDisp->displayedObjects();
406 foreach (ObjectPtr anObj, aDisplayed) {
407 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
408 if (aResult.get() != NULL) {
409 aBodyIsVisualized = aResult->groupName() == ModelAPI_ResultBody::group();
410 if (aBodyIsVisualized)
415 if (!aBodyIsVisualized) {
416 // We have to select a plane before any operation
419 activateSelection(true);
421 //myLabel->setText(myText);
422 //myLabel->setToolTip(myTooltip);
424 connect(workshop()->selector(), SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
425 activateFilters(true);
428 void PartSet_WidgetSketchLabel::deactivate()
430 ModuleBase_ModelWidget::deactivate();
431 bool aHidePreview = myPreviewDisplayed;
432 erasePreviewPlanes();
433 activateSelection(false);
435 activateFilters(false);
437 myWorkshop->viewer()->update();
440 void PartSet_WidgetSketchLabel::activateSelection(bool toActivate)
444 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
446 myWorkshop->module()->activeSelectionModes(aModes);
449 aModes << TopAbs_FACE;
451 myWorkshop->activateSubShapesSelection(aModes);
453 myWorkshop->deactivateSubShapesSelection();
457 void PartSet_WidgetSketchLabel::erasePreviewPlanes()
459 if (myPreviewDisplayed) {
460 XGUI_Displayer* aDisp = workshop()->displayer();
461 aDisp->eraseAIS(myYZPlane, false);
462 aDisp->eraseAIS(myXZPlane, false);
463 aDisp->eraseAIS(myXYPlane, false);
464 myPreviewDisplayed = false;
468 void PartSet_WidgetSketchLabel::showPreviewPlanes()
470 if (myPreviewDisplayed)
473 if (!myYZPlane) { // If planes are not created
475 std::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
476 std::shared_ptr<GeomAPI_Dir> aYZDir(new GeomAPI_Dir(1, 0, 0));
477 // -1, not 1 for correct internal sketch coords (issue 898)
478 std::shared_ptr<GeomAPI_Dir> aXZDir(new GeomAPI_Dir(0, -1, 0));
479 std::shared_ptr<GeomAPI_Dir> aXYDir(new GeomAPI_Dir(0, 0, 1));
481 std::vector<int> aYZRGB, aXZRGB, aXYRGB;
482 aYZRGB = Config_PropManager::color("Visualization", "yz_plane_color",
484 aXZRGB = Config_PropManager::color("Visualization", "xz_plane_color",
486 aXYRGB = Config_PropManager::color("Visualization", "xy_plane_color",
488 int aR[] = {aYZRGB[0], aYZRGB[1], aYZRGB[2]};
489 int aG[] = {aXZRGB[0], aXZRGB[1], aXZRGB[2]};
490 int aB[] = {aXYRGB[0], aXYRGB[1], aXYRGB[2]};
492 myYZPlane = createPreviewPlane(anOrigin, aYZDir, aR);
493 myXZPlane = createPreviewPlane(anOrigin, aXZDir, aG);
494 myXYPlane = createPreviewPlane(anOrigin, aXYDir, aB);
496 XGUI_Displayer* aDisp = workshop()->displayer();
497 aDisp->displayAIS(myYZPlane, true, false);
498 aDisp->displayAIS(myXZPlane, true, false);
499 aDisp->displayAIS(myXYPlane, true, false);
500 myPreviewDisplayed = true;
504 AISObjectPtr PartSet_WidgetSketchLabel::createPreviewPlane(std::shared_ptr<GeomAPI_Pnt> theOrigin,
505 std::shared_ptr<GeomAPI_Dir> theNorm,
508 double aSize = Config_PropManager::integer("Sketch planes", "planes_size", PLANE_SIZE);
509 std::shared_ptr<GeomAPI_Shape> aFace = GeomAlgoAPI_FaceBuilder::square(theOrigin, theNorm, aSize);
510 AISObjectPtr aAIS = AISObjectPtr(new GeomAPI_AISObject());
511 aAIS->createShape(aFace);
512 aAIS->setWidth(Config_PropManager::integer("Sketch planes", "planes_thickness", SKETCH_WIDTH));
513 aAIS->setColor(theRGB[0], theRGB[1], theRGB[2]);
518 std::shared_ptr<GeomAPI_Dir> PartSet_WidgetSketchLabel::setSketchPlane(const FeaturePtr& theFeature,
519 const TopoDS_Shape& theShape)
521 if (theShape.IsNull())
522 return std::shared_ptr<GeomAPI_Dir>();
524 // get selected shape
525 std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
526 aGShape->setImpl(new TopoDS_Shape(theShape));
530 // get plane parameters
531 std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
533 return std::shared_ptr<GeomAPI_Dir>();
535 // set plane parameters to feature
536 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
537 double anA, aB, aC, aD;
538 aPlane->coefficients(anA, aB, aC, aD);
540 // calculate attributes of the sketch
541 std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
542 std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
543 std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
544 aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
545 std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
546 // X axis is preferable to be dirX on the sketch
547 const double tol = Precision::Confusion();
548 bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
549 std::shared_ptr<GeomAPI_Dir> aTempDir(
550 isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
551 std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
552 std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
554 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
555 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
556 anOrigin->setValue(anOrigPnt);
557 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
558 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
559 aNormal->setValue(aNormDir);
560 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
561 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
562 aDirX->setValue(aXDir);
563 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
567 XGUI_Workshop* PartSet_WidgetSketchLabel::workshop() const
569 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
570 return aConnector->workshop();
574 void PartSet_WidgetSketchLabel::onSetPlaneView()
576 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
578 std::shared_ptr<GeomAPI_Dir> aDirection = aPlane->direction();
579 gp_Dir aDir = aDirection->impl<gp_Dir>();
580 if (myViewInverted->isChecked())
582 myWorkshop->viewer()->setViewProjection(aDir.X(), aDir.Y(), aDir.Z(), 0.);
583 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
585 aModule->onViewTransformed();