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>
19 #include <ModuleBase_Operation.h>
20 #include <ModuleBase_ViewerPrs.h>
21 #include <ModuleBase_Tools.h>
22 #include <ModuleBase_IModule.h>
24 #include <GeomAlgoAPI_FaceBuilder.h>
25 #include <GeomDataAPI_Point.h>
26 #include <GeomDataAPI_Dir.h>
27 #include <GeomAPI_XYZ.h>
29 #include <SketchPlugin_Sketch.h>
30 #include <SketcherPrs_Tools.h>
32 #include <Precision.hxx>
36 #include <AIS_Shape.hxx>
37 #include <AIS_DimensionSelectionMode.hxx>
39 #include <Config_WidgetAPI.h>
40 #include <Config_PropManager.h>
44 #include <QApplication>
45 #include <QVBoxLayout>
49 PartSet_WidgetSketchLabel::PartSet_WidgetSketchLabel(QWidget* theParent,
50 const Config_WidgetAPI* theData,
51 const std::string& theParentId,
52 bool toShowConstraints)
53 : ModuleBase_WidgetValidated(theParent, theData, theParentId),
54 myPreviewDisplayed(false),
57 myText = QString::fromStdString(theData->getProperty("title"));
58 myLabel = new QLabel("", theParent);
59 myLabel->setWordWrap(true);
60 myTooltip = QString::fromStdString(theData->getProperty("tooltip"));
61 myLabel->setToolTip("");
62 myLabel->setIndent(5);
64 mySelectionTimer = new QTimer(this);
65 connect(mySelectionTimer, SIGNAL(timeout()), SLOT(setSketchingMode()));
66 mySelectionTimer->setSingleShot(true);
68 QVBoxLayout* aLayout = new QVBoxLayout(this);
69 ModuleBase_Tools::zeroMargins(aLayout);
70 aLayout->addWidget(myLabel);
72 myShowConstraints = new QCheckBox(tr("Show constraints"), this);
73 aLayout->addWidget(myShowConstraints);
76 connect(myShowConstraints, SIGNAL(toggled(bool)), this, SIGNAL(showConstraintToggled(bool)));
77 myShowConstraints->setChecked(toShowConstraints);
80 PartSet_WidgetSketchLabel::~PartSet_WidgetSketchLabel()
85 QList<QWidget*> PartSet_WidgetSketchLabel::getControls() const
87 QList<QWidget*> aResult;
92 void PartSet_WidgetSketchLabel::onPlaneSelected()
95 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
96 QList<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
97 if (!aSelected.empty()) {
98 ModuleBase_ViewerPrs aPrs = aSelected.first();
99 Handle(SelectMgr_EntityOwner) anOwner = aSelected.first().owner();
100 if (isValid(anOwner)) {
101 setSelection(anOwner);
103 TopoDS_Shape aShape = aPrs.shape();
104 if (!aShape.IsNull()) {
105 erasePreviewPlanes();
106 DataPtr aData = feature()->data();
107 AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
108 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
110 GeomShapePtr aShapePtr = aSelAttr->value();
111 if (aShapePtr.get() == NULL || aShapePtr->isNull()) {
112 std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
113 aGShape->setImpl(new TopoDS_Shape(aShape));
114 // get plane parameters
115 std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
116 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
118 myWorkshop->viewer()->setViewProjection(aDir->x(), aDir->y(), aDir->z());
122 // Clear text in the label
123 myLabel->setText("");
124 myLabel->setToolTip("");
125 disconnect(myWorkshop->selector(), SIGNAL(selectionChanged()),
126 this, SLOT(onPlaneSelected()));
127 activateFilters(myWorkshop->module()->workshop(), false);
129 // Clear selection mode and define sketching mode
130 //XGUI_Displayer* aDisp = myWorkshop->displayer();
131 //aDisp->closeLocalContexts();
132 emit planeSelected(plane());
135 // Update sketcher actions
136 XGUI_ActionsMgr* anActMgr = myWorkshop->actionsMgr();
138 myWorkshop->viewer()->update();
144 std::shared_ptr<GeomAPI_Pln> PartSet_WidgetSketchLabel::plane() const
146 CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
147 return PartSet_Tools::sketchPlane(aSketch);
151 bool PartSet_WidgetSketchLabel::focusTo()
157 void PartSet_WidgetSketchLabel::enableFocusProcessing()
159 myLabel->installEventFilter(this);
162 void PartSet_WidgetSketchLabel::storeAttributeValue()
166 void PartSet_WidgetSketchLabel::restoreAttributeValue(const bool theValid)
168 // it is not necessary to save the previous plane value because the plane is chosen once
169 DataPtr aData = feature()->data();
170 AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
171 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
173 ResultPtr anEmptyResult;
174 GeomShapePtr anEmptyShape;
175 aSelAttr->setValue(anEmptyResult, anEmptyShape);
179 bool PartSet_WidgetSketchLabel::setSelection(const Handle_SelectMgr_EntityOwner& theOwner)
181 bool isOwnerSet = false;
183 ModuleBase_ViewerPrs aPrs;
184 myWorkshop->selector()->selection()->fillPresentation(aPrs, theOwner);
186 const TopoDS_Shape& aShape = aPrs.shape();
187 std::shared_ptr<GeomAPI_Dir> aDir;
189 if (aPrs.object() && (feature() != aPrs.object())) {
190 DataPtr aData = feature()->data();
191 AttributeSelectionPtr aSelAttr =
192 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
193 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
195 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs.object());
197 GeomShapePtr aShapePtr(new GeomAPI_Shape());
198 aShapePtr->setImpl(new TopoDS_Shape(aShape));
199 aSelAttr->setValue(aRes, aShapePtr);
204 else if (!aShape.IsNull()) {
205 aDir = setSketchPlane(aShape);
211 void PartSet_WidgetSketchLabel::activateCustom()
213 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
215 //setSketchingMode();
216 // In order to avoid Opening/Closing of context too often
217 // it can be useful for a delay on the property panel filling
218 // it is possible that it is not necessary anymore, but it requires a check
219 mySelectionTimer->start(20);
221 // We have to select a plane before any operation
224 XGUI_Displayer* aDisp = myWorkshop->displayer();
225 //aDisp->openLocalContext();
226 //aDisp->activateObjects(QIntList());
228 aModes << TopAbs_FACE;
229 aDisp->activateObjects(aModes);
231 myLabel->setText(myText);
232 myLabel->setToolTip(myTooltip);
234 connect(myWorkshop->selector(), SIGNAL(selectionChanged()), this, SLOT(onPlaneSelected()));
235 activateFilters(myWorkshop->module()->workshop(), true);
237 aDisp->updateViewer();
241 void PartSet_WidgetSketchLabel::deactivate()
243 // Do not set selection mode if the widget was activated for a small moment
244 mySelectionTimer->stop();
245 //XGUI_Displayer* aDisp = myWorkshop->displayer();
246 //aDisp->closeLocalContexts();
247 erasePreviewPlanes();
248 activateFilters(myWorkshop->module()->workshop(), false);
251 void PartSet_WidgetSketchLabel::erasePreviewPlanes()
253 if (myPreviewDisplayed) {
254 XGUI_Displayer* aDisp = myWorkshop->displayer();
255 aDisp->eraseAIS(myYZPlane, false);
256 aDisp->eraseAIS(myXZPlane, false);
257 aDisp->eraseAIS(myXYPlane, false);
258 myPreviewDisplayed = false;
262 void PartSet_WidgetSketchLabel::showPreviewPlanes()
264 if (myPreviewDisplayed)
267 if (!myYZPlane) { // If planes are not created
269 std::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
270 std::shared_ptr<GeomAPI_Dir> aYZDir(new GeomAPI_Dir(1, 0, 0));
271 std::shared_ptr<GeomAPI_Dir> aXZDir(new GeomAPI_Dir(0, 1, 0));
272 std::shared_ptr<GeomAPI_Dir> aXYDir(new GeomAPI_Dir(0, 0, 1));
274 std::vector<int> aYZRGB, aXZRGB, aXYRGB;
275 aYZRGB = Config_PropManager::color("Visualization", "yz_plane_color",
277 aXZRGB = Config_PropManager::color("Visualization", "xz_plane_color",
279 aXYRGB = Config_PropManager::color("Visualization", "xy_plane_color",
281 int aR[] = {aYZRGB[0], aYZRGB[1], aYZRGB[2]};
282 int aG[] = {aXZRGB[0], aXZRGB[1], aXZRGB[2]};
283 int aB[] = {aXYRGB[0], aXYRGB[1], aXYRGB[2]};
285 myYZPlane = createPreviewPlane(anOrigin, aYZDir, aR);
286 myXZPlane = createPreviewPlane(anOrigin, aXZDir, aG);
287 myXYPlane = createPreviewPlane(anOrigin, aXYDir, aB);
289 XGUI_Displayer* aDisp = myWorkshop->displayer();
290 aDisp->displayAIS(myYZPlane, false);
291 aDisp->displayAIS(myXZPlane, false);
292 aDisp->displayAIS(myXYPlane, false);
293 myPreviewDisplayed = true;
297 AISObjectPtr PartSet_WidgetSketchLabel::createPreviewPlane(std::shared_ptr<GeomAPI_Pnt> theOrigin,
298 std::shared_ptr<GeomAPI_Dir> theNorm,
301 double aSize = Config_PropManager::integer("Sketch planes", "planes_size", PLANE_SIZE);
302 std::shared_ptr<GeomAPI_Shape> aFace = GeomAlgoAPI_FaceBuilder::square(theOrigin, theNorm, aSize);
303 AISObjectPtr aAIS = AISObjectPtr(new GeomAPI_AISObject());
304 aAIS->createShape(aFace);
305 aAIS->setWidth(Config_PropManager::integer("Sketch planes", "planes_thickness", SKETCH_WIDTH));
306 aAIS->setColor(theRGB[0], theRGB[1], theRGB[2]);
311 std::shared_ptr<GeomAPI_Dir> PartSet_WidgetSketchLabel::setSketchPlane(const TopoDS_Shape& theShape)
313 if (theShape.IsNull())
314 return std::shared_ptr<GeomAPI_Dir>();
316 // get selected shape
317 std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
318 aGShape->setImpl(new TopoDS_Shape(theShape));
320 // get plane parameters
321 std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
323 // set plane parameters to feature
324 std::shared_ptr<ModelAPI_Data> aData = feature()->data();
325 double anA, aB, aC, aD;
326 aPlane->coefficients(anA, aB, aC, aD);
328 // calculate attributes of the sketch
329 std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
330 std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
331 std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
332 aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
333 std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
334 // X axis is preferable to be dirX on the sketch
335 const double tol = Precision::Confusion();
336 bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
337 std::shared_ptr<GeomAPI_Dir> aTempDir(
338 isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
339 std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
340 std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
342 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
343 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
344 anOrigin->setValue(anOrigPnt);
345 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
346 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
347 aNormal->setValue(aNormDir);
348 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
349 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
350 aDirX->setValue(aXDir);
351 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
356 void PartSet_WidgetSketchLabel::setSketchingMode()
358 XGUI_Displayer* aDisp = myWorkshop->displayer();
359 // Clear standard selection modes if they are defined
360 //aDisp->activateObjects(aModes);
361 //aDisp->openLocalContext();
363 // Get default selection modes
365 aModes.append(SketcherPrs_Tools::Sel_Dimension_Text);
366 aModes.append(SketcherPrs_Tools::Sel_Dimension_Line);
367 aModes.append(SketcherPrs_Tools::Sel_Constraint);
368 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_VERTEX));
369 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_EDGE));
371 aDisp->activateObjects(aModes);
374 void PartSet_WidgetSketchLabel::showConstraints(bool theOn)
376 myShowConstraints->setChecked(theOn);
377 emit showConstraintToggled(theOn);