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 <XGUI_Workshop.h>
11 #include <XGUI_Displayer.h>
12 #include <XGUI_SelectionMgr.h>
13 #include <XGUI_Selection.h>
14 #include <XGUI_ViewerProxy.h>
15 #include <XGUI_ActionsMgr.h>
17 #include <ModuleBase_Operation.h>
18 #include <ModuleBase_ViewerPrs.h>
20 #include <GeomAlgoAPI_FaceBuilder.h>
21 #include <GeomDataAPI_Point.h>
22 #include <GeomDataAPI_Dir.h>
23 #include <GeomAPI_XYZ.h>
25 #include <SketchPlugin_Sketch.h>
27 #include <Precision.hxx>
31 #include <AIS_Shape.hxx>
32 #include <AIS_DimensionSelectionMode.hxx>
34 #include <Config_WidgetAPI.h>
35 #include <Config_PropManager.h>
39 #include <QApplication>
42 PartSet_WidgetSketchLabel::PartSet_WidgetSketchLabel(QWidget* theParent,
43 const Config_WidgetAPI* theData,
44 const std::string& theParentId)
45 : ModuleBase_ModelWidget(theParent, theData, theParentId), myPreviewDisplayed(false)
47 myText = QString::fromStdString(theData->getProperty("title"));
48 myLabel = new QLabel("", theParent);
49 myLabel->setWordWrap(true);
50 myTooltip = QString::fromStdString(theData->getProperty("tooltip"));
51 myLabel->setToolTip("");
52 myLabel->setIndent(5);
54 mySelectionTimer = new QTimer(this);
55 connect(mySelectionTimer, SIGNAL(timeout()), SLOT(setSketchingMode()));
56 mySelectionTimer->setSingleShot(true);
59 PartSet_WidgetSketchLabel::~PartSet_WidgetSketchLabel()
64 QList<QWidget*> PartSet_WidgetSketchLabel::getControls() const
66 return QList<QWidget*>();
69 QWidget* PartSet_WidgetSketchLabel::getControl() const
74 void PartSet_WidgetSketchLabel::onPlaneSelected()
76 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
77 QList<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
78 if (!aSelected.empty()) {
79 ModuleBase_ViewerPrs aPrs = aSelected.first();
80 TopoDS_Shape aShape = aPrs.shape();
81 if (!aShape.IsNull()) {
82 std::shared_ptr<GeomAPI_Dir> aDir = setSketchPlane(aShape);
86 if (aPrs.object() && (feature() != aPrs.object())) {
87 DataPtr aData = feature()->data();
88 AttributeSelectionPtr aSelAttr =
89 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
90 (aData->attribute(SketchPlugin_Feature::EXTERNAL_ID()));
92 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs.object());
94 GeomShapePtr aShapePtr(new GeomAPI_Shape());
95 aShapePtr->setImpl(new TopoDS_Shape(aShape));
96 aSelAttr->setValue(aRes, aShapePtr);
100 myWorkshop->viewer()->setViewProjection(aDir->x(), aDir->y(), aDir->z());
102 // Clear text in the label
103 myLabel->setText("");
104 myLabel->setToolTip("");
105 disconnect(myWorkshop->selector(), SIGNAL(selectionChanged()),
106 this, SLOT(onPlaneSelected()));
108 // Clear selection mode and define sketching mode
109 XGUI_Displayer* aDisp = myWorkshop->displayer();
110 aDisp->removeSelectionFilter(myFaceFilter);
111 //aDisp->closeLocalContexts();
112 emit planeSelected(plane());
115 // Update sketcher actions
116 XGUI_ActionsMgr* anActMgr = myWorkshop->actionsMgr();
118 myWorkshop->viewer()->update();
124 std::shared_ptr<GeomAPI_Pln> PartSet_WidgetSketchLabel::plane() const
126 CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
127 return PartSet_Tools::sketchPlane(aSketch);
131 void PartSet_WidgetSketchLabel::activateCustom()
133 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
135 //setSketchingMode();
136 // In order to avoid Opening/Closing of context too often
137 mySelectionTimer->start(20);
139 // We have to select a plane before any operation
142 XGUI_Displayer* aDisp = myWorkshop->displayer();
143 //aDisp->openLocalContext();
144 //aDisp->activateObjects(QIntList());
145 if (myFaceFilter.IsNull())
146 myFaceFilter = new StdSelect_FaceFilter(StdSelect_Plane);
147 aDisp->addSelectionFilter(myFaceFilter);
149 aModes << TopAbs_FACE;
150 aDisp->activateObjects(aModes);
152 myLabel->setText(myText);
153 myLabel->setToolTip(myTooltip);
155 connect(myWorkshop->selector(), SIGNAL(selectionChanged()), this, SLOT(onPlaneSelected()));
156 aDisp->updateViewer();
160 void PartSet_WidgetSketchLabel::deactivate()
162 // Do not set selection mode if the widget was activated for a small moment
163 mySelectionTimer->stop();
164 XGUI_Displayer* aDisp = myWorkshop->displayer();
165 aDisp->removeSelectionFilter(myFaceFilter);
166 //aDisp->removeSelectionFilter(mySketchFilter);
167 //aDisp->closeLocalContexts();
168 erasePreviewPlanes();
171 void PartSet_WidgetSketchLabel::erasePreviewPlanes()
173 if (myPreviewDisplayed) {
174 XGUI_Displayer* aDisp = myWorkshop->displayer();
175 aDisp->eraseAIS(myYZPlane, false);
176 aDisp->eraseAIS(myXZPlane, false);
177 aDisp->eraseAIS(myXYPlane, false);
178 myPreviewDisplayed = false;
182 void PartSet_WidgetSketchLabel::showPreviewPlanes()
184 if (myPreviewDisplayed)
187 if (!myYZPlane) { // If planes are not created
189 std::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
190 std::shared_ptr<GeomAPI_Dir> aYZDir(new GeomAPI_Dir(1, 0, 0));
191 std::shared_ptr<GeomAPI_Dir> aXZDir(new GeomAPI_Dir(0, 1, 0));
192 std::shared_ptr<GeomAPI_Dir> aXYDir(new GeomAPI_Dir(0, 0, 1));
194 int aR[] = {255, 0, 0};
195 int aG[] = {0, 255, 0};
196 int aB[] = {0, 0, 255};
198 myYZPlane = createPreviewPlane(anOrigin, aYZDir, aR);
199 myXZPlane = createPreviewPlane(anOrigin, aXZDir, aG);
200 myXYPlane = createPreviewPlane(anOrigin, aXYDir, aB);
202 XGUI_Displayer* aDisp = myWorkshop->displayer();
203 aDisp->displayAIS(myYZPlane, false);
204 aDisp->displayAIS(myXZPlane, false);
205 aDisp->displayAIS(myXYPlane, false);
206 myPreviewDisplayed = true;
210 AISObjectPtr PartSet_WidgetSketchLabel::createPreviewPlane(std::shared_ptr<GeomAPI_Pnt> theOrigin,
211 std::shared_ptr<GeomAPI_Dir> theNorm,
214 double aSize = Config_PropManager::integer("Sketch planes", "planes_size", PLANE_SIZE);
215 std::shared_ptr<GeomAPI_Shape> aFace = GeomAlgoAPI_FaceBuilder::square(theOrigin, theNorm, aSize);
216 AISObjectPtr aAIS = AISObjectPtr(new GeomAPI_AISObject());
217 aAIS->createShape(aFace);
218 aAIS->setWidth(Config_PropManager::integer("Sketch planes", "planes_thickness", SKETCH_WIDTH));
219 aAIS->setColor(theRGB[0], theRGB[1], theRGB[2]);
224 std::shared_ptr<GeomAPI_Dir> PartSet_WidgetSketchLabel::setSketchPlane(const TopoDS_Shape& theShape)
226 if (theShape.IsNull())
227 return std::shared_ptr<GeomAPI_Dir>();
229 // get selected shape
230 std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
231 aGShape->setImpl(new TopoDS_Shape(theShape));
233 // get plane parameters
234 std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
236 // set plane parameters to feature
237 std::shared_ptr<ModelAPI_Data> aData = feature()->data();
238 double anA, aB, aC, aD;
239 aPlane->coefficients(anA, aB, aC, aD);
241 // calculate attributes of the sketch
242 std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
243 std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
244 std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
245 aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
246 std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
247 // X axis is preferable to be dirX on the sketch
248 const double tol = Precision::Confusion();
249 bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
250 std::shared_ptr<GeomAPI_Dir> aTempDir(
251 isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
252 std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
253 std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
255 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
256 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
257 anOrigin->setValue(anOrigPnt);
258 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
259 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
260 aNormal->setValue(aNormDir);
261 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
262 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
263 aDirX->setValue(aXDir);
264 std::shared_ptr<GeomDataAPI_Dir> aDirY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
265 aData->attribute(SketchPlugin_Sketch::DIRY_ID()));
266 aDirY->setValue(aYDir);
267 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
272 void PartSet_WidgetSketchLabel::setSketchingMode()
274 XGUI_Displayer* aDisp = myWorkshop->displayer();
275 // Clear standard selection modes if they are defined
276 //aDisp->activateObjects(aModes);
277 //aDisp->openLocalContext();
279 // Get default selection modes
281 aModes.append(AIS_DSM_Text);
282 aModes.append(AIS_DSM_Line);
283 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_VERTEX));
284 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_EDGE));
286 aDisp->activateObjects(aModes);