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>
47 PartSet_WidgetSketchLabel::PartSet_WidgetSketchLabel(QWidget* theParent,
48 const Config_WidgetAPI* theData,
49 const std::string& theParentId)
50 : ModuleBase_WidgetValidated(theParent, theData, theParentId),
51 myPreviewDisplayed(false),
54 myText = QString::fromStdString(theData->getProperty("title"));
55 myLabel = new QLabel("", theParent);
56 myLabel->setWordWrap(true);
57 myTooltip = QString::fromStdString(theData->getProperty("tooltip"));
58 myLabel->setToolTip("");
59 myLabel->setIndent(5);
61 mySelectionTimer = new QTimer(this);
62 connect(mySelectionTimer, SIGNAL(timeout()), SLOT(setSketchingMode()));
63 mySelectionTimer->setSingleShot(true);
65 QVBoxLayout* aLayout = new QVBoxLayout(this);
66 ModuleBase_Tools::zeroMargins(aLayout);
67 aLayout->addWidget(myLabel);
71 PartSet_WidgetSketchLabel::~PartSet_WidgetSketchLabel()
76 QList<QWidget*> PartSet_WidgetSketchLabel::getControls() const
78 QList<QWidget*> aResult;
83 void PartSet_WidgetSketchLabel::onPlaneSelected()
86 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
87 QList<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
88 if (!aSelected.empty()) {
89 ModuleBase_ViewerPrs aPrs = aSelected.first();
90 Handle(SelectMgr_EntityOwner) anOwner = aSelected.first().owner();
91 if (isValid(anOwner)) {
92 setSelection(anOwner);
94 TopoDS_Shape aShape = aPrs.shape();
95 if (!aShape.IsNull()) {
97 DataPtr aData = feature()->data();
98 AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
99 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
101 GeomShapePtr aShapePtr = aSelAttr->value();
102 if (aShapePtr.get() == NULL || aShapePtr->isNull()) {
103 std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
104 aGShape->setImpl(new TopoDS_Shape(aShape));
105 // get plane parameters
106 std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
107 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
109 myWorkshop->viewer()->setViewProjection(aDir->x(), aDir->y(), aDir->z());
113 // Clear text in the label
114 myLabel->setText("");
115 myLabel->setToolTip("");
116 disconnect(myWorkshop->selector(), SIGNAL(selectionChanged()),
117 this, SLOT(onPlaneSelected()));
118 activateFilters(myWorkshop->module()->workshop(), false);
120 // Clear selection mode and define sketching mode
121 //XGUI_Displayer* aDisp = myWorkshop->displayer();
122 //aDisp->closeLocalContexts();
123 emit planeSelected(plane());
126 // Update sketcher actions
127 XGUI_ActionsMgr* anActMgr = myWorkshop->actionsMgr();
129 myWorkshop->viewer()->update();
135 std::shared_ptr<GeomAPI_Pln> PartSet_WidgetSketchLabel::plane() const
137 CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
138 return PartSet_Tools::sketchPlane(aSketch);
142 bool PartSet_WidgetSketchLabel::focusTo()
148 void PartSet_WidgetSketchLabel::enableFocusProcessing()
150 myLabel->installEventFilter(this);
153 void PartSet_WidgetSketchLabel::storeAttributeValue()
157 void PartSet_WidgetSketchLabel::restoreAttributeValue(const bool theValid)
159 // it is not necessary to save the previous plane value because the plane is chosen once
160 DataPtr aData = feature()->data();
161 AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
162 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
164 ResultPtr anEmptyResult;
165 GeomShapePtr anEmptyShape;
166 aSelAttr->setValue(anEmptyResult, anEmptyShape);
170 bool PartSet_WidgetSketchLabel::setSelection(const Handle_SelectMgr_EntityOwner& theOwner)
172 bool isOwnerSet = false;
174 ModuleBase_ViewerPrs aPrs;
175 myWorkshop->selector()->selection()->fillPresentation(aPrs, theOwner);
177 const TopoDS_Shape& aShape = aPrs.shape();
178 std::shared_ptr<GeomAPI_Dir> aDir;
180 if (aPrs.object() && (feature() != aPrs.object())) {
181 DataPtr aData = feature()->data();
182 AttributeSelectionPtr aSelAttr =
183 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
184 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
186 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs.object());
188 GeomShapePtr aShapePtr(new GeomAPI_Shape());
189 aShapePtr->setImpl(new TopoDS_Shape(aShape));
190 aSelAttr->setValue(aRes, aShapePtr);
195 else if (!aShape.IsNull()) {
196 aDir = setSketchPlane(aShape);
202 void PartSet_WidgetSketchLabel::activateCustom()
204 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
206 //setSketchingMode();
207 // In order to avoid Opening/Closing of context too often
208 // it can be useful for a delay on the property panel filling
209 // it is possible that it is not necessary anymore, but it requires a check
210 mySelectionTimer->start(20);
212 // We have to select a plane before any operation
215 XGUI_Displayer* aDisp = myWorkshop->displayer();
216 //aDisp->openLocalContext();
217 //aDisp->activateObjects(QIntList());
219 aModes << TopAbs_FACE;
220 aDisp->activateObjects(aModes);
222 myLabel->setText(myText);
223 myLabel->setToolTip(myTooltip);
225 connect(myWorkshop->selector(), SIGNAL(selectionChanged()), this, SLOT(onPlaneSelected()));
226 activateFilters(myWorkshop->module()->workshop(), true);
228 aDisp->updateViewer();
232 void PartSet_WidgetSketchLabel::deactivate()
234 // Do not set selection mode if the widget was activated for a small moment
235 mySelectionTimer->stop();
236 //XGUI_Displayer* aDisp = myWorkshop->displayer();
237 //aDisp->closeLocalContexts();
238 erasePreviewPlanes();
241 void PartSet_WidgetSketchLabel::erasePreviewPlanes()
243 if (myPreviewDisplayed) {
244 XGUI_Displayer* aDisp = myWorkshop->displayer();
245 aDisp->eraseAIS(myYZPlane, false);
246 aDisp->eraseAIS(myXZPlane, false);
247 aDisp->eraseAIS(myXYPlane, false);
248 myPreviewDisplayed = false;
252 void PartSet_WidgetSketchLabel::showPreviewPlanes()
254 if (myPreviewDisplayed)
257 if (!myYZPlane) { // If planes are not created
259 std::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
260 std::shared_ptr<GeomAPI_Dir> aYZDir(new GeomAPI_Dir(1, 0, 0));
261 std::shared_ptr<GeomAPI_Dir> aXZDir(new GeomAPI_Dir(0, 1, 0));
262 std::shared_ptr<GeomAPI_Dir> aXYDir(new GeomAPI_Dir(0, 0, 1));
264 std::vector<int> aYZRGB, aXZRGB, aXYRGB;
265 aYZRGB = Config_PropManager::color("Visualization", "yz_plane_color",
267 aXZRGB = Config_PropManager::color("Visualization", "xz_plane_color",
269 aXYRGB = Config_PropManager::color("Visualization", "xy_plane_color",
271 int aR[] = {aYZRGB[0], aYZRGB[1], aYZRGB[2]};
272 int aG[] = {aXZRGB[0], aXZRGB[1], aXZRGB[2]};
273 int aB[] = {aXYRGB[0], aXYRGB[1], aXYRGB[2]};
275 myYZPlane = createPreviewPlane(anOrigin, aYZDir, aR);
276 myXZPlane = createPreviewPlane(anOrigin, aXZDir, aG);
277 myXYPlane = createPreviewPlane(anOrigin, aXYDir, aB);
279 XGUI_Displayer* aDisp = myWorkshop->displayer();
280 aDisp->displayAIS(myYZPlane, false);
281 aDisp->displayAIS(myXZPlane, false);
282 aDisp->displayAIS(myXYPlane, false);
283 myPreviewDisplayed = true;
287 AISObjectPtr PartSet_WidgetSketchLabel::createPreviewPlane(std::shared_ptr<GeomAPI_Pnt> theOrigin,
288 std::shared_ptr<GeomAPI_Dir> theNorm,
291 double aSize = Config_PropManager::integer("Sketch planes", "planes_size", PLANE_SIZE);
292 std::shared_ptr<GeomAPI_Shape> aFace = GeomAlgoAPI_FaceBuilder::square(theOrigin, theNorm, aSize);
293 AISObjectPtr aAIS = AISObjectPtr(new GeomAPI_AISObject());
294 aAIS->createShape(aFace);
295 aAIS->setWidth(Config_PropManager::integer("Sketch planes", "planes_thickness", SKETCH_WIDTH));
296 aAIS->setColor(theRGB[0], theRGB[1], theRGB[2]);
301 std::shared_ptr<GeomAPI_Dir> PartSet_WidgetSketchLabel::setSketchPlane(const TopoDS_Shape& theShape)
303 if (theShape.IsNull())
304 return std::shared_ptr<GeomAPI_Dir>();
306 // get selected shape
307 std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
308 aGShape->setImpl(new TopoDS_Shape(theShape));
310 // get plane parameters
311 std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
313 // set plane parameters to feature
314 std::shared_ptr<ModelAPI_Data> aData = feature()->data();
315 double anA, aB, aC, aD;
316 aPlane->coefficients(anA, aB, aC, aD);
318 // calculate attributes of the sketch
319 std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
320 std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
321 std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
322 aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
323 std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
324 // X axis is preferable to be dirX on the sketch
325 const double tol = Precision::Confusion();
326 bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
327 std::shared_ptr<GeomAPI_Dir> aTempDir(
328 isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
329 std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
330 std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
332 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
333 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
334 anOrigin->setValue(anOrigPnt);
335 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
336 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
337 aNormal->setValue(aNormDir);
338 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
339 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
340 aDirX->setValue(aXDir);
341 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
346 void PartSet_WidgetSketchLabel::setSketchingMode()
348 XGUI_Displayer* aDisp = myWorkshop->displayer();
349 // Clear standard selection modes if they are defined
350 //aDisp->activateObjects(aModes);
351 //aDisp->openLocalContext();
353 // Get default selection modes
355 aModes.append(AIS_DSM_Text);
356 aModes.append(AIS_DSM_Line);
357 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_VERTEX));
358 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_EDGE));
360 aDisp->activateObjects(aModes);