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>
40 #define PLANE_SIZE "200"
41 #define SKETCH_WIDTH "4"
44 PartSet_WidgetSketchLabel::PartSet_WidgetSketchLabel(QWidget* theParent,
45 const Config_WidgetAPI* theData,
46 const std::string& theParentId)
47 : ModuleBase_ModelWidget(theParent, theData, theParentId), myPreviewDisplayed(false)
49 myText = QString::fromStdString(theData->getProperty("title"));
50 myLabel = new QLabel("", theParent);
51 myLabel->setWordWrap(true);
52 myTooltip = QString::fromStdString(theData->getProperty("tooltip"));
53 myLabel->setToolTip("");
54 myLabel->setIndent(5);
56 mySelectionTimer = new QTimer(this);
57 connect(mySelectionTimer, SIGNAL(timeout()), SLOT(setSketchingMode()));
58 mySelectionTimer->setSingleShot(true);
61 PartSet_WidgetSketchLabel::~PartSet_WidgetSketchLabel()
66 QList<QWidget*> PartSet_WidgetSketchLabel::getControls() const
68 return QList<QWidget*>();
71 QWidget* PartSet_WidgetSketchLabel::getControl() const
76 void PartSet_WidgetSketchLabel::onPlaneSelected()
78 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
79 QList<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
80 if (!aSelected.empty()) {
81 ModuleBase_ViewerPrs aPrs = aSelected.first();
82 TopoDS_Shape aShape = aPrs.shape();
83 if (!aShape.IsNull()) {
84 std::shared_ptr<GeomAPI_Dir> aDir = setSketchPlane(aShape);
88 if (aPrs.object() && (feature() != aPrs.object())) {
89 DataPtr aData = feature()->data();
90 AttributeSelectionPtr aSelAttr =
91 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
92 (aData->attribute(SketchPlugin_Feature::EXTERNAL_ID()));
94 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs.object());
96 GeomShapePtr aShapePtr(new GeomAPI_Shape());
97 aShapePtr->setImpl(new TopoDS_Shape(aShape));
98 aSelAttr->setValue(aRes, aShapePtr);
102 myWorkshop->viewer()->setViewProjection(aDir->x(), aDir->y(), aDir->z());
104 // Clear text in the label
105 myLabel->setText("");
106 myLabel->setToolTip("");
107 disconnect(myWorkshop->selector(), SIGNAL(selectionChanged()),
108 this, SLOT(onPlaneSelected()));
110 // Clear selection mode and define sketching mode
111 XGUI_Displayer* aDisp = myWorkshop->displayer();
112 aDisp->removeSelectionFilter(myFaceFilter);
113 //aDisp->closeLocalContexts();
114 emit planeSelected(plane());
117 // Update sketcher actions
118 XGUI_ActionsMgr* anActMgr = myWorkshop->actionsMgr();
125 std::shared_ptr<GeomAPI_Pln> PartSet_WidgetSketchLabel::plane() const
127 CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
128 return PartSet_Tools::sketchPlane(aSketch);
132 void PartSet_WidgetSketchLabel::activate()
134 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
136 //setSketchingMode();
137 // In order to avoid Opening/Closing of context too often
138 mySelectionTimer->start(20);
140 // We have to select a plane before any operation
143 XGUI_Displayer* aDisp = myWorkshop->displayer();
144 //aDisp->openLocalContext();
145 //aDisp->activateObjects(QIntList());
146 if (myFaceFilter.IsNull())
147 myFaceFilter = new StdSelect_FaceFilter(StdSelect_Plane);
148 aDisp->addSelectionFilter(myFaceFilter);
150 aModes << TopAbs_FACE;
151 aDisp->activateObjects(aModes);
153 myLabel->setText(myText);
154 myLabel->setToolTip(myTooltip);
156 connect(myWorkshop->selector(), SIGNAL(selectionChanged()), this, SLOT(onPlaneSelected()));
157 aDisp->updateViewer();
161 void PartSet_WidgetSketchLabel::deactivate()
163 // Do not set selection mode if the widget was activated for a small moment
164 mySelectionTimer->stop();
165 XGUI_Displayer* aDisp = myWorkshop->displayer();
166 aDisp->removeSelectionFilter(myFaceFilter);
167 //aDisp->removeSelectionFilter(mySketchFilter);
168 //aDisp->closeLocalContexts();
169 erasePreviewPlanes();
172 void PartSet_WidgetSketchLabel::erasePreviewPlanes()
174 if (myPreviewDisplayed) {
175 XGUI_Displayer* aDisp = myWorkshop->displayer();
176 aDisp->eraseAIS(myYZPlane, false);
177 aDisp->eraseAIS(myXZPlane, false);
178 aDisp->eraseAIS(myXYPlane, false);
179 myPreviewDisplayed = false;
183 void PartSet_WidgetSketchLabel::showPreviewPlanes()
185 if (myPreviewDisplayed)
188 if (!myYZPlane) { // If planes are not created
190 std::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
191 std::shared_ptr<GeomAPI_Dir> aYZDir(new GeomAPI_Dir(1, 0, 0));
192 std::shared_ptr<GeomAPI_Dir> aXZDir(new GeomAPI_Dir(0, 1, 0));
193 std::shared_ptr<GeomAPI_Dir> aXYDir(new GeomAPI_Dir(0, 0, 1));
195 int aR[] = {255, 0, 0};
196 int aG[] = {0, 255, 0};
197 int aB[] = {0, 0, 255};
199 myYZPlane = createPreviewPlane(anOrigin, aYZDir, aR);
200 myXZPlane = createPreviewPlane(anOrigin, aXZDir, aG);
201 myXYPlane = createPreviewPlane(anOrigin, aXYDir, aB);
203 XGUI_Displayer* aDisp = myWorkshop->displayer();
204 aDisp->displayAIS(myYZPlane, false);
205 aDisp->displayAIS(myXZPlane, false);
206 aDisp->displayAIS(myXYPlane, false);
207 myPreviewDisplayed = true;
211 AISObjectPtr PartSet_WidgetSketchLabel::createPreviewPlane(std::shared_ptr<GeomAPI_Pnt> theOrigin,
212 std::shared_ptr<GeomAPI_Dir> theNorm,
215 double aSize = Config_PropManager::integer("Sketch planes", "Size of planes", PLANE_SIZE);
216 std::shared_ptr<GeomAPI_Shape> aFace = GeomAlgoAPI_FaceBuilder::square(theOrigin, theNorm, aSize);
217 AISObjectPtr aAIS = AISObjectPtr(new GeomAPI_AISObject());
218 aAIS->createShape(aFace);
219 aAIS->setWidth(Config_PropManager::integer("Sketch planes", "planes_thickness", SKETCH_WIDTH));
220 aAIS->setColor(theRGB[0], theRGB[1], theRGB[2]);
225 std::shared_ptr<GeomAPI_Dir> PartSet_WidgetSketchLabel::setSketchPlane(const TopoDS_Shape& theShape)
227 if (theShape.IsNull())
228 return std::shared_ptr<GeomAPI_Dir>();
230 // get selected shape
231 std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
232 aGShape->setImpl(new TopoDS_Shape(theShape));
234 // get plane parameters
235 std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
237 // set plane parameters to feature
238 std::shared_ptr<ModelAPI_Data> aData = feature()->data();
239 double anA, aB, aC, aD;
240 aPlane->coefficients(anA, aB, aC, aD);
242 // calculate attributes of the sketch
243 std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
244 std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
245 std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
246 aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
247 std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
248 // X axis is preferable to be dirX on the sketch
249 const double tol = Precision::Confusion();
250 bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
251 std::shared_ptr<GeomAPI_Dir> aTempDir(
252 isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
253 std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
254 std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
256 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
257 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
258 anOrigin->setValue(anOrigPnt);
259 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
260 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
261 aNormal->setValue(aNormDir);
262 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
263 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
264 aDirX->setValue(aXDir);
265 std::shared_ptr<GeomDataAPI_Dir> aDirY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
266 aData->attribute(SketchPlugin_Sketch::DIRY_ID()));
267 aDirY->setValue(aYDir);
268 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
273 void PartSet_WidgetSketchLabel::setSketchingMode()
275 qDebug("### Set sketching mode");
277 XGUI_Displayer* aDisp = myWorkshop->displayer();
278 // Clear standard selection modes if they are defined
279 //aDisp->activateObjects(aModes);
280 //aDisp->openLocalContext();
282 // Get default selection modes
284 aModes.append(AIS_DSM_Text);
285 aModes.append(AIS_DSM_Line);
286 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_VERTEX));
287 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_EDGE));
289 aDisp->activateObjects(aModes);