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"
10 #include "SketchPlugin_SketchEntity.h"
12 #include <XGUI_Workshop.h>
13 #include <XGUI_Displayer.h>
14 #include <XGUI_SelectionMgr.h>
15 #include <XGUI_Selection.h>
16 #include <XGUI_ViewerProxy.h>
17 #include <XGUI_ActionsMgr.h>
18 #include <XGUI_ModuleConnector.h>
20 #include <ModuleBase_Operation.h>
21 #include <ModuleBase_ViewerPrs.h>
22 #include <ModuleBase_Tools.h>
23 #include <ModuleBase_IModule.h>
25 #include <ModelAPI_ResultBody.h>
26 #include <ModelAPI_Tools.h>
28 #include <GeomAlgoAPI_FaceBuilder.h>
29 #include <GeomAlgoAPI_ShapeTools.h>
30 #include <GeomDataAPI_Point.h>
31 #include <GeomDataAPI_Dir.h>
32 #include <GeomAPI_XYZ.h>
34 #include <SketchPlugin_Sketch.h>
35 #include <SketcherPrs_Tools.h>
37 #include <Precision.hxx>
41 #include <AIS_Shape.hxx>
42 #include <AIS_DimensionSelectionMode.hxx>
44 #include <Config_WidgetAPI.h>
45 #include <Config_PropManager.h>
48 #include <QApplication>
49 #include <QVBoxLayout>
52 #include <QPushButton>
53 #include <QStackedWidget>
56 PartSet_WidgetSketchLabel::PartSet_WidgetSketchLabel(QWidget* theParent,
57 ModuleBase_IWorkshop* theWorkshop,
58 const Config_WidgetAPI* theData,
59 const std::string& theParentId,
60 bool toShowConstraints)
61 : ModuleBase_WidgetValidated(theParent, theWorkshop, theData, theParentId),
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 myShowConstraints = new QCheckBox(tr("Show constraints"), this);
105 connect(myShowConstraints, SIGNAL(toggled(bool)), this, SIGNAL(showConstraintToggled(bool)));
106 myShowConstraints->setChecked(toShowConstraints);
107 aLayout->addWidget(myShowConstraints);
109 myStackWidget->addWidget(aSecondWgt);
110 //setLayout(aLayout);
113 PartSet_WidgetSketchLabel::~PartSet_WidgetSketchLabel()
117 bool PartSet_WidgetSketchLabel::setSelection(QList<ModuleBase_ViewerPrs>& theValues,
118 const bool theToValidate)
120 // do not use the given selection if the plane of the sketch has been already set.
121 // If this check is absent, a selected plane in the viewer can be set in the sketch
122 // even if the sketch is built on another plane.
126 ModuleBase_ViewerPrs aPrs = theValues.first();
127 bool aDone = ModuleBase_WidgetValidated::setSelection(theValues, theToValidate);
129 updateByPlaneSelected(aPrs);
134 QList<QWidget*> PartSet_WidgetSketchLabel::getControls() const
136 QList<QWidget*> aResult;
137 aResult << myStackWidget;
141 void PartSet_WidgetSketchLabel::onSelectionChanged()
143 QList<ModuleBase_ViewerPrs> aSelected = getFilteredSelected();
145 if (aSelected.empty())
147 ModuleBase_ViewerPrs aPrs = aSelected.first();
149 bool aDone = ModuleBase_WidgetValidated::setSelection(aSelected, false);
151 updateByPlaneSelected(aPrs);
152 updateObject(myFeature);
156 void PartSet_WidgetSketchLabel::updateByPlaneSelected(const ModuleBase_ViewerPrs& thePrs)
158 // 1. hide main planes if they have been displayed
159 erasePreviewPlanes();
160 // 2. if the planes were displayed, change the view projection
161 TopoDS_Shape aShape = thePrs.shape();
162 std::shared_ptr<GeomAPI_Shape> aGShape;
163 std::shared_ptr<GeomAPI_Shape> aBaseShape;
165 DataPtr aData = feature()->data();
166 AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
167 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
169 // selection happens in OCC viewer
170 if (!aShape.IsNull()) {
171 aGShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
172 aGShape->setImpl(new TopoDS_Shape(aShape));
174 if (aSelAttr && aSelAttr->context()) {
175 aBaseShape = aSelAttr->context()->shape();
178 else { // selection happens in OCC viewer(on body) of in the OB browser
180 aGShape = aSelAttr->value();
183 if (aGShape.get() != NULL) {
184 // get plane parameters
185 std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
186 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
187 gp_XYZ aXYZ = aDir->impl<gp_Dir>().XYZ();
190 // orienting projection is not needed: it is done in GeomAlgoAPI_FaceBuilder::plane
191 /*if (aGShape->impl<TopoDS_Shape>().Orientation() == TopAbs_REVERSED) {
195 // Rotate view if the sketcher plane is selected only from preview planes
196 // Preview planes are created only if there is no any shape
197 bool aRotate = Config_PropManager::boolean("Sketch planes", "rotate_to_plane", "false");
199 myWorkshop->viewer()->setViewProjection(aXYZ.X(), aXYZ.Y(), aXYZ.Z(), aTwist);
201 // 3. Clear text in the label
202 myStackWidget->setCurrentIndex(1);
203 //myLabel->setText("");
204 //myLabel->setToolTip("");
205 disconnect(workshop()->selector(), SIGNAL(selectionChanged()),
206 this, SLOT(onSelectionChanged()));
207 // 4. deactivate face selection filter
208 activateFilters(false);
210 // 5. Clear selection mode and define sketching mode
211 //XGUI_Displayer* aDisp = workshop()->displayer();
212 //aDisp->closeLocalContexts();
213 emit planeSelected(plane());
214 // after the plane is selected in the sketch, the sketch selection should be activated
215 // it can not be performed in the sketch label widget because, we don't need to switch off
216 // the selection by any label deactivation, but need to switch it off by stop the sketch
217 activateSelection(true);
219 // 6. Update sketcher actions
220 XGUI_ActionsMgr* anActMgr = workshop()->actionsMgr();
222 myWorkshop->viewer()->update();
225 std::shared_ptr<GeomAPI_Pln> PartSet_WidgetSketchLabel::plane() const
227 CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
228 return PartSet_Tools::sketchPlane(aSketch);
231 bool PartSet_WidgetSketchLabel::focusTo()
233 myStackWidget->setFocus();
237 void PartSet_WidgetSketchLabel::enableFocusProcessing()
239 myStackWidget->installEventFilter(this);
242 void PartSet_WidgetSketchLabel::storeAttributeValue()
244 ModuleBase_WidgetValidated::storeAttributeValue();
247 void PartSet_WidgetSketchLabel::restoreAttributeValue(const bool theValid)
249 ModuleBase_WidgetValidated::restoreAttributeValue(theValid);
251 // it is not necessary to save the previous plane value because the plane is chosen once
252 DataPtr aData = feature()->data();
253 AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
254 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
256 ResultPtr anEmptyResult;
257 GeomShapePtr anEmptyShape;
258 aSelAttr->setValue(anEmptyResult, anEmptyShape);
262 bool PartSet_WidgetSketchLabel::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
264 bool isOwnerSet = false;
266 const TopoDS_Shape& aShape = thePrs.shape();
267 std::shared_ptr<GeomAPI_Dir> aDir;
269 if (thePrs.object() && (feature() != thePrs.object())) {
270 DataPtr aData = feature()->data();
271 AttributeSelectionPtr aSelAttr =
272 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
273 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
275 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(thePrs.object());
277 GeomShapePtr aShapePtr(new GeomAPI_Shape());
278 if (aShape.IsNull()) { // selection happens in the OCC viewer
279 aShapePtr = ModelAPI_Tools::shape(aRes);
281 else { // selection happens in OB browser
282 aShapePtr->setImpl(new TopoDS_Shape(aShape));
284 if (aShapePtr.get() != NULL) {
285 aSelAttr->setValue(aRes, aShapePtr);
291 else if (!aShape.IsNull()) {
292 aDir = setSketchPlane(aShape);
293 isOwnerSet = aDir.get();
298 void PartSet_WidgetSketchLabel::activateCustom()
300 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
302 myStackWidget->setCurrentIndex(1);
303 activateSelection(true);
307 myStackWidget->setCurrentIndex(0);
308 bool aBodyIsVisualized = false;
309 XGUI_Displayer* aDisp = workshop()->displayer();
310 QObjectPtrList aDisplayed = aDisp->displayedObjects();
311 foreach (ObjectPtr anObj, aDisplayed) {
312 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
313 if (aResult.get() != NULL) {
314 aBodyIsVisualized = aResult->groupName() == ModelAPI_ResultBody::group();
315 if (aBodyIsVisualized)
320 if (!aBodyIsVisualized) {
321 // We have to select a plane before any operation
324 activateSelection(true);
326 //myLabel->setText(myText);
327 //myLabel->setToolTip(myTooltip);
329 connect(workshop()->selector(), SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
330 activateFilters(true);
333 void PartSet_WidgetSketchLabel::deactivate()
335 ModuleBase_ModelWidget::deactivate();
336 erasePreviewPlanes();
337 activateSelection(false);
339 activateFilters(false);
342 void PartSet_WidgetSketchLabel::activateSelection(bool toActivate)
346 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
348 myWorkshop->module()->activeSelectionModes(aModes);
351 aModes << TopAbs_FACE;
353 myWorkshop->activateSubShapesSelection(aModes);
355 myWorkshop->deactivateSubShapesSelection();
359 void PartSet_WidgetSketchLabel::erasePreviewPlanes()
361 if (myPreviewDisplayed) {
362 XGUI_Displayer* aDisp = workshop()->displayer();
363 aDisp->eraseAIS(myYZPlane, false);
364 aDisp->eraseAIS(myXZPlane, false);
365 aDisp->eraseAIS(myXYPlane, false);
366 myPreviewDisplayed = false;
370 void PartSet_WidgetSketchLabel::showPreviewPlanes()
372 if (myPreviewDisplayed)
375 if (!myYZPlane) { // If planes are not created
377 std::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
378 std::shared_ptr<GeomAPI_Dir> aYZDir(new GeomAPI_Dir(1, 0, 0));
379 // -1, not 1 for correct internal sketch coords (issue 898)
380 std::shared_ptr<GeomAPI_Dir> aXZDir(new GeomAPI_Dir(0, -1, 0));
381 std::shared_ptr<GeomAPI_Dir> aXYDir(new GeomAPI_Dir(0, 0, 1));
383 std::vector<int> aYZRGB, aXZRGB, aXYRGB;
384 aYZRGB = Config_PropManager::color("Visualization", "yz_plane_color",
386 aXZRGB = Config_PropManager::color("Visualization", "xz_plane_color",
388 aXYRGB = Config_PropManager::color("Visualization", "xy_plane_color",
390 int aR[] = {aYZRGB[0], aYZRGB[1], aYZRGB[2]};
391 int aG[] = {aXZRGB[0], aXZRGB[1], aXZRGB[2]};
392 int aB[] = {aXYRGB[0], aXYRGB[1], aXYRGB[2]};
394 myYZPlane = createPreviewPlane(anOrigin, aYZDir, aR);
395 myXZPlane = createPreviewPlane(anOrigin, aXZDir, aG);
396 myXYPlane = createPreviewPlane(anOrigin, aXYDir, aB);
398 XGUI_Displayer* aDisp = workshop()->displayer();
399 aDisp->displayAIS(myYZPlane, true, false);
400 aDisp->displayAIS(myXZPlane, true, false);
401 aDisp->displayAIS(myXYPlane, true, false);
402 myPreviewDisplayed = true;
406 AISObjectPtr PartSet_WidgetSketchLabel::createPreviewPlane(std::shared_ptr<GeomAPI_Pnt> theOrigin,
407 std::shared_ptr<GeomAPI_Dir> theNorm,
410 double aSize = Config_PropManager::integer("Sketch planes", "planes_size", PLANE_SIZE);
411 std::shared_ptr<GeomAPI_Shape> aFace = GeomAlgoAPI_FaceBuilder::square(theOrigin, theNorm, aSize);
412 AISObjectPtr aAIS = AISObjectPtr(new GeomAPI_AISObject());
413 aAIS->createShape(aFace);
414 aAIS->setWidth(Config_PropManager::integer("Sketch planes", "planes_thickness", SKETCH_WIDTH));
415 aAIS->setColor(theRGB[0], theRGB[1], theRGB[2]);
420 std::shared_ptr<GeomAPI_Dir> PartSet_WidgetSketchLabel::setSketchPlane(const TopoDS_Shape& theShape)
422 if (theShape.IsNull())
423 return std::shared_ptr<GeomAPI_Dir>();
425 // get selected shape
426 std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
427 aGShape->setImpl(new TopoDS_Shape(theShape));
431 // get plane parameters
432 std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
434 return std::shared_ptr<GeomAPI_Dir>();
436 // set plane parameters to feature
437 std::shared_ptr<ModelAPI_Data> aData = feature()->data();
438 double anA, aB, aC, aD;
439 aPlane->coefficients(anA, aB, aC, aD);
441 // calculate attributes of the sketch
442 std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
443 std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
444 std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
445 aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
446 std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
447 // X axis is preferable to be dirX on the sketch
448 const double tol = Precision::Confusion();
449 bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
450 std::shared_ptr<GeomAPI_Dir> aTempDir(
451 isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
452 std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
453 std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
455 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
456 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
457 anOrigin->setValue(anOrigPnt);
458 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
459 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
460 aNormal->setValue(aNormDir);
461 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
462 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
463 aDirX->setValue(aXDir);
464 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
468 void PartSet_WidgetSketchLabel::showConstraints(bool theOn)
470 myShowConstraints->setChecked(theOn);
471 emit showConstraintToggled(theOn);
474 XGUI_Workshop* PartSet_WidgetSketchLabel::workshop() const
476 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
477 return aConnector->workshop();
481 void PartSet_WidgetSketchLabel::onSetPlaneView()
483 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
485 std::shared_ptr<GeomAPI_Dir> aDirection = aPlane->direction();
486 gp_Dir aDir = aDirection->impl<gp_Dir>();
487 if (myViewInverted->isChecked())
489 myWorkshop->viewer()->setViewProjection(aDir.X(), aDir.Y(), aDir.Z(), 0.);