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>
31 #include <Precision.hxx>
35 #include <AIS_Shape.hxx>
36 #include <AIS_DimensionSelectionMode.hxx>
38 #include <Config_WidgetAPI.h>
39 #include <Config_PropManager.h>
43 #include <QApplication>
44 #include <QVBoxLayout>
48 PartSet_WidgetSketchLabel::PartSet_WidgetSketchLabel(QWidget* theParent,
49 const Config_WidgetAPI* theData,
50 const std::string& theParentId,
51 bool toShowConstraints)
52 : ModuleBase_WidgetValidated(theParent, theData, theParentId),
53 myPreviewDisplayed(false),
56 myText = QString::fromStdString(theData->getProperty("title"));
57 myLabel = new QLabel("", theParent);
58 myLabel->setWordWrap(true);
59 myTooltip = QString::fromStdString(theData->getProperty("tooltip"));
60 myLabel->setToolTip("");
61 myLabel->setIndent(5);
63 mySelectionTimer = new QTimer(this);
64 connect(mySelectionTimer, SIGNAL(timeout()), SLOT(setSketchingMode()));
65 mySelectionTimer->setSingleShot(true);
67 QVBoxLayout* aLayout = new QVBoxLayout(this);
68 ModuleBase_Tools::zeroMargins(aLayout);
69 aLayout->addWidget(myLabel);
71 myShowConstraints = new QCheckBox(tr("Show constraints"), this);
72 aLayout->addWidget(myShowConstraints);
75 connect(myShowConstraints, SIGNAL(toggled(bool)), this, SIGNAL(showConstraintToggled(bool)));
76 myShowConstraints->setChecked(toShowConstraints);
79 PartSet_WidgetSketchLabel::~PartSet_WidgetSketchLabel()
84 QList<QWidget*> PartSet_WidgetSketchLabel::getControls() const
86 QList<QWidget*> aResult;
91 void PartSet_WidgetSketchLabel::onPlaneSelected()
94 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
95 QList<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
96 if (!aSelected.empty()) {
97 ModuleBase_ViewerPrs aPrs = aSelected.first();
98 Handle(SelectMgr_EntityOwner) anOwner = aSelected.first().owner();
99 if (isValid(anOwner)) {
100 setSelection(anOwner);
102 TopoDS_Shape aShape = aPrs.shape();
103 if (!aShape.IsNull()) {
104 erasePreviewPlanes();
105 DataPtr aData = feature()->data();
106 AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
107 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
109 GeomShapePtr aShapePtr = aSelAttr->value();
110 if (aShapePtr.get() == NULL || aShapePtr->isNull()) {
111 std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
112 aGShape->setImpl(new TopoDS_Shape(aShape));
113 // get plane parameters
114 std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
115 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
117 myWorkshop->viewer()->setViewProjection(aDir->x(), aDir->y(), aDir->z());
121 // Clear text in the label
122 myLabel->setText("");
123 myLabel->setToolTip("");
124 disconnect(myWorkshop->selector(), SIGNAL(selectionChanged()),
125 this, SLOT(onPlaneSelected()));
126 activateFilters(myWorkshop->module()->workshop(), false);
128 // Clear selection mode and define sketching mode
129 //XGUI_Displayer* aDisp = myWorkshop->displayer();
130 //aDisp->closeLocalContexts();
131 emit planeSelected(plane());
134 // Update sketcher actions
135 XGUI_ActionsMgr* anActMgr = myWorkshop->actionsMgr();
137 myWorkshop->viewer()->update();
143 std::shared_ptr<GeomAPI_Pln> PartSet_WidgetSketchLabel::plane() const
145 CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
146 return PartSet_Tools::sketchPlane(aSketch);
150 bool PartSet_WidgetSketchLabel::focusTo()
156 void PartSet_WidgetSketchLabel::enableFocusProcessing()
158 myLabel->installEventFilter(this);
161 void PartSet_WidgetSketchLabel::storeAttributeValue()
165 void PartSet_WidgetSketchLabel::restoreAttributeValue(const bool theValid)
167 // it is not necessary to save the previous plane value because the plane is chosen once
168 DataPtr aData = feature()->data();
169 AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
170 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
172 ResultPtr anEmptyResult;
173 GeomShapePtr anEmptyShape;
174 aSelAttr->setValue(anEmptyResult, anEmptyShape);
178 bool PartSet_WidgetSketchLabel::setSelection(const Handle_SelectMgr_EntityOwner& theOwner)
180 bool isOwnerSet = false;
182 ModuleBase_ViewerPrs aPrs;
183 myWorkshop->selector()->selection()->fillPresentation(aPrs, theOwner);
185 const TopoDS_Shape& aShape = aPrs.shape();
186 std::shared_ptr<GeomAPI_Dir> aDir;
188 if (aPrs.object() && (feature() != aPrs.object())) {
189 DataPtr aData = feature()->data();
190 AttributeSelectionPtr aSelAttr =
191 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
192 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
194 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs.object());
196 GeomShapePtr aShapePtr(new GeomAPI_Shape());
197 aShapePtr->setImpl(new TopoDS_Shape(aShape));
198 aSelAttr->setValue(aRes, aShapePtr);
203 else if (!aShape.IsNull()) {
204 aDir = setSketchPlane(aShape);
210 void PartSet_WidgetSketchLabel::activateCustom()
212 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
214 //setSketchingMode();
215 // In order to avoid Opening/Closing of context too often
216 // it can be useful for a delay on the property panel filling
217 // it is possible that it is not necessary anymore, but it requires a check
218 mySelectionTimer->start(20);
220 // We have to select a plane before any operation
223 XGUI_Displayer* aDisp = myWorkshop->displayer();
224 //aDisp->openLocalContext();
225 //aDisp->activateObjects(QIntList());
227 aModes << TopAbs_FACE;
228 aDisp->activateObjects(aModes);
230 myLabel->setText(myText);
231 myLabel->setToolTip(myTooltip);
233 connect(myWorkshop->selector(), SIGNAL(selectionChanged()), this, SLOT(onPlaneSelected()));
234 activateFilters(myWorkshop->module()->workshop(), true);
236 aDisp->updateViewer();
240 void PartSet_WidgetSketchLabel::deactivate()
242 // Do not set selection mode if the widget was activated for a small moment
243 mySelectionTimer->stop();
244 //XGUI_Displayer* aDisp = myWorkshop->displayer();
245 //aDisp->closeLocalContexts();
246 erasePreviewPlanes();
249 void PartSet_WidgetSketchLabel::erasePreviewPlanes()
251 if (myPreviewDisplayed) {
252 XGUI_Displayer* aDisp = myWorkshop->displayer();
253 aDisp->eraseAIS(myYZPlane, false);
254 aDisp->eraseAIS(myXZPlane, false);
255 aDisp->eraseAIS(myXYPlane, false);
256 myPreviewDisplayed = false;
260 void PartSet_WidgetSketchLabel::showPreviewPlanes()
262 if (myPreviewDisplayed)
265 if (!myYZPlane) { // If planes are not created
267 std::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
268 std::shared_ptr<GeomAPI_Dir> aYZDir(new GeomAPI_Dir(1, 0, 0));
269 std::shared_ptr<GeomAPI_Dir> aXZDir(new GeomAPI_Dir(0, 1, 0));
270 std::shared_ptr<GeomAPI_Dir> aXYDir(new GeomAPI_Dir(0, 0, 1));
272 std::vector<int> aYZRGB, aXZRGB, aXYRGB;
273 aYZRGB = Config_PropManager::color("Visualization", "yz_plane_color",
275 aXZRGB = Config_PropManager::color("Visualization", "xz_plane_color",
277 aXYRGB = Config_PropManager::color("Visualization", "xy_plane_color",
279 int aR[] = {aYZRGB[0], aYZRGB[1], aYZRGB[2]};
280 int aG[] = {aXZRGB[0], aXZRGB[1], aXZRGB[2]};
281 int aB[] = {aXYRGB[0], aXYRGB[1], aXYRGB[2]};
283 myYZPlane = createPreviewPlane(anOrigin, aYZDir, aR);
284 myXZPlane = createPreviewPlane(anOrigin, aXZDir, aG);
285 myXYPlane = createPreviewPlane(anOrigin, aXYDir, aB);
287 XGUI_Displayer* aDisp = myWorkshop->displayer();
288 aDisp->displayAIS(myYZPlane, false);
289 aDisp->displayAIS(myXZPlane, false);
290 aDisp->displayAIS(myXYPlane, false);
291 myPreviewDisplayed = true;
295 AISObjectPtr PartSet_WidgetSketchLabel::createPreviewPlane(std::shared_ptr<GeomAPI_Pnt> theOrigin,
296 std::shared_ptr<GeomAPI_Dir> theNorm,
299 double aSize = Config_PropManager::integer("Sketch planes", "planes_size", PLANE_SIZE);
300 std::shared_ptr<GeomAPI_Shape> aFace = GeomAlgoAPI_FaceBuilder::square(theOrigin, theNorm, aSize);
301 AISObjectPtr aAIS = AISObjectPtr(new GeomAPI_AISObject());
302 aAIS->createShape(aFace);
303 aAIS->setWidth(Config_PropManager::integer("Sketch planes", "planes_thickness", SKETCH_WIDTH));
304 aAIS->setColor(theRGB[0], theRGB[1], theRGB[2]);
309 std::shared_ptr<GeomAPI_Dir> PartSet_WidgetSketchLabel::setSketchPlane(const TopoDS_Shape& theShape)
311 if (theShape.IsNull())
312 return std::shared_ptr<GeomAPI_Dir>();
314 // get selected shape
315 std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
316 aGShape->setImpl(new TopoDS_Shape(theShape));
318 // get plane parameters
319 std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
321 // set plane parameters to feature
322 std::shared_ptr<ModelAPI_Data> aData = feature()->data();
323 double anA, aB, aC, aD;
324 aPlane->coefficients(anA, aB, aC, aD);
326 // calculate attributes of the sketch
327 std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
328 std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
329 std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
330 aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
331 std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
332 // X axis is preferable to be dirX on the sketch
333 const double tol = Precision::Confusion();
334 bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
335 std::shared_ptr<GeomAPI_Dir> aTempDir(
336 isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
337 std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
338 std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
340 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
341 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
342 anOrigin->setValue(anOrigPnt);
343 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
344 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
345 aNormal->setValue(aNormDir);
346 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
347 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
348 aDirX->setValue(aXDir);
349 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
354 void PartSet_WidgetSketchLabel::setSketchingMode()
356 XGUI_Displayer* aDisp = myWorkshop->displayer();
357 // Clear standard selection modes if they are defined
358 //aDisp->activateObjects(aModes);
359 //aDisp->openLocalContext();
361 // Get default selection modes
363 aModes.append(AIS_DSM_Text);
364 aModes.append(AIS_DSM_Line);
365 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_VERTEX));
366 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_EDGE));
368 aDisp->activateObjects(aModes);
371 void PartSet_WidgetSketchLabel::showConstraints(bool theOn)
373 myShowConstraints->setChecked(theOn);
374 emit showConstraintToggled(theOn);