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::backupAttributeValue(const bool isBackup)
155 // it is not necessary to save the previous plane value because the plane is chosen once
157 DataPtr aData = feature()->data();
158 AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
159 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
161 ResultPtr anEmptyResult;
162 GeomShapePtr anEmptyShape;
163 aSelAttr->setValue(anEmptyResult, anEmptyShape);
168 bool PartSet_WidgetSketchLabel::setSelection(const Handle_SelectMgr_EntityOwner& theOwner)
170 bool isOwnerSet = false;
172 ModuleBase_ViewerPrs aPrs;
173 myWorkshop->selector()->selection()->fillPresentation(aPrs, theOwner);
175 const TopoDS_Shape& aShape = aPrs.shape();
176 std::shared_ptr<GeomAPI_Dir> aDir;
178 if (aPrs.object() && (feature() != aPrs.object())) {
179 DataPtr aData = feature()->data();
180 AttributeSelectionPtr aSelAttr =
181 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
182 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
184 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs.object());
186 GeomShapePtr aShapePtr(new GeomAPI_Shape());
187 aShapePtr->setImpl(new TopoDS_Shape(aShape));
188 aSelAttr->setValue(aRes, aShapePtr);
193 else if (!aShape.IsNull()) {
194 aDir = setSketchPlane(aShape);
200 void PartSet_WidgetSketchLabel::activateCustom()
202 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
204 //setSketchingMode();
205 // In order to avoid Opening/Closing of context too often
206 // it can be useful for a delay on the property panel filling
207 // it is possible that it is not necessary anymore, but it requires a check
208 mySelectionTimer->start(20);
210 // We have to select a plane before any operation
213 XGUI_Displayer* aDisp = myWorkshop->displayer();
214 //aDisp->openLocalContext();
215 //aDisp->activateObjects(QIntList());
217 aModes << TopAbs_FACE;
218 aDisp->activateObjects(aModes);
220 myLabel->setText(myText);
221 myLabel->setToolTip(myTooltip);
223 connect(myWorkshop->selector(), SIGNAL(selectionChanged()), this, SLOT(onPlaneSelected()));
224 activateFilters(myWorkshop->module()->workshop(), true);
226 aDisp->updateViewer();
230 void PartSet_WidgetSketchLabel::deactivate()
232 // Do not set selection mode if the widget was activated for a small moment
233 mySelectionTimer->stop();
234 //XGUI_Displayer* aDisp = myWorkshop->displayer();
235 //aDisp->closeLocalContexts();
236 erasePreviewPlanes();
239 void PartSet_WidgetSketchLabel::erasePreviewPlanes()
241 if (myPreviewDisplayed) {
242 XGUI_Displayer* aDisp = myWorkshop->displayer();
243 aDisp->eraseAIS(myYZPlane, false);
244 aDisp->eraseAIS(myXZPlane, false);
245 aDisp->eraseAIS(myXYPlane, false);
246 myPreviewDisplayed = false;
250 void PartSet_WidgetSketchLabel::showPreviewPlanes()
252 if (myPreviewDisplayed)
255 if (!myYZPlane) { // If planes are not created
257 std::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
258 std::shared_ptr<GeomAPI_Dir> aYZDir(new GeomAPI_Dir(1, 0, 0));
259 std::shared_ptr<GeomAPI_Dir> aXZDir(new GeomAPI_Dir(0, 1, 0));
260 std::shared_ptr<GeomAPI_Dir> aXYDir(new GeomAPI_Dir(0, 0, 1));
262 std::vector<int> aYZRGB, aXZRGB, aXYRGB;
263 aYZRGB = Config_PropManager::color("Visualization", "yz_plane_color",
265 aXZRGB = Config_PropManager::color("Visualization", "xz_plane_color",
267 aXYRGB = Config_PropManager::color("Visualization", "xy_plane_color",
269 int aR[] = {aYZRGB[0], aYZRGB[1], aYZRGB[2]};
270 int aG[] = {aXZRGB[0], aXZRGB[1], aXZRGB[2]};
271 int aB[] = {aXYRGB[0], aXYRGB[1], aXYRGB[2]};
273 myYZPlane = createPreviewPlane(anOrigin, aYZDir, aR);
274 myXZPlane = createPreviewPlane(anOrigin, aXZDir, aG);
275 myXYPlane = createPreviewPlane(anOrigin, aXYDir, aB);
277 XGUI_Displayer* aDisp = myWorkshop->displayer();
278 aDisp->displayAIS(myYZPlane, false);
279 aDisp->displayAIS(myXZPlane, false);
280 aDisp->displayAIS(myXYPlane, false);
281 myPreviewDisplayed = true;
285 AISObjectPtr PartSet_WidgetSketchLabel::createPreviewPlane(std::shared_ptr<GeomAPI_Pnt> theOrigin,
286 std::shared_ptr<GeomAPI_Dir> theNorm,
289 double aSize = Config_PropManager::integer("Sketch planes", "planes_size", PLANE_SIZE);
290 std::shared_ptr<GeomAPI_Shape> aFace = GeomAlgoAPI_FaceBuilder::square(theOrigin, theNorm, aSize);
291 AISObjectPtr aAIS = AISObjectPtr(new GeomAPI_AISObject());
292 aAIS->createShape(aFace);
293 aAIS->setWidth(Config_PropManager::integer("Sketch planes", "planes_thickness", SKETCH_WIDTH));
294 aAIS->setColor(theRGB[0], theRGB[1], theRGB[2]);
299 std::shared_ptr<GeomAPI_Dir> PartSet_WidgetSketchLabel::setSketchPlane(const TopoDS_Shape& theShape)
301 if (theShape.IsNull())
302 return std::shared_ptr<GeomAPI_Dir>();
304 // get selected shape
305 std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
306 aGShape->setImpl(new TopoDS_Shape(theShape));
308 // get plane parameters
309 std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
311 // set plane parameters to feature
312 std::shared_ptr<ModelAPI_Data> aData = feature()->data();
313 double anA, aB, aC, aD;
314 aPlane->coefficients(anA, aB, aC, aD);
316 // calculate attributes of the sketch
317 std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
318 std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
319 std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
320 aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
321 std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
322 // X axis is preferable to be dirX on the sketch
323 const double tol = Precision::Confusion();
324 bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
325 std::shared_ptr<GeomAPI_Dir> aTempDir(
326 isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
327 std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
328 std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
330 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
331 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
332 anOrigin->setValue(anOrigPnt);
333 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
334 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
335 aNormal->setValue(aNormDir);
336 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
337 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
338 aDirX->setValue(aXDir);
339 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
344 void PartSet_WidgetSketchLabel::setSketchingMode()
346 XGUI_Displayer* aDisp = myWorkshop->displayer();
347 // Clear standard selection modes if they are defined
348 //aDisp->activateObjects(aModes);
349 //aDisp->openLocalContext();
351 // Get default selection modes
353 aModes.append(AIS_DSM_Text);
354 aModes.append(AIS_DSM_Line);
355 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_VERTEX));
356 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_EDGE));
358 aDisp->activateObjects(aModes);