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>
23 #include <GeomAlgoAPI_FaceBuilder.h>
24 #include <GeomDataAPI_Point.h>
25 #include <GeomDataAPI_Dir.h>
26 #include <GeomAPI_XYZ.h>
28 #include <SketchPlugin_Sketch.h>
30 #include <Precision.hxx>
34 #include <AIS_Shape.hxx>
35 #include <AIS_DimensionSelectionMode.hxx>
37 #include <Config_WidgetAPI.h>
38 #include <Config_PropManager.h>
42 #include <QApplication>
43 #include <QVBoxLayout>
46 PartSet_WidgetSketchLabel::PartSet_WidgetSketchLabel(QWidget* theParent,
47 const Config_WidgetAPI* theData,
48 const std::string& theParentId)
49 : ModuleBase_ModelWidget(theParent, theData, theParentId),
50 myPreviewDisplayed(false),
53 myText = QString::fromStdString(theData->getProperty("title"));
54 myLabel = new QLabel("", theParent);
55 myLabel->setWordWrap(true);
56 myTooltip = QString::fromStdString(theData->getProperty("tooltip"));
57 myLabel->setToolTip("");
58 myLabel->setIndent(5);
60 mySelectionTimer = new QTimer(this);
61 connect(mySelectionTimer, SIGNAL(timeout()), SLOT(setSketchingMode()));
62 mySelectionTimer->setSingleShot(true);
64 QVBoxLayout* aLayout = new QVBoxLayout(this);
65 ModuleBase_Tools::zeroMargins(aLayout);
66 aLayout->addWidget(myLabel);
70 PartSet_WidgetSketchLabel::~PartSet_WidgetSketchLabel()
75 QList<QWidget*> PartSet_WidgetSketchLabel::getControls() const
77 return QList<QWidget*>();
80 void PartSet_WidgetSketchLabel::onPlaneSelected()
82 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
83 QList<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
84 if (!aSelected.empty()) {
85 ModuleBase_ViewerPrs aPrs = aSelected.first();
86 TopoDS_Shape aShape = aPrs.shape();
87 if (!aShape.IsNull()) {
88 std::shared_ptr<GeomAPI_Dir> aDir = setSketchPlane(aShape);
92 if (aPrs.object() && (feature() != aPrs.object())) {
93 DataPtr aData = feature()->data();
94 AttributeSelectionPtr aSelAttr =
95 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
96 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
98 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs.object());
100 GeomShapePtr aShapePtr(new GeomAPI_Shape());
101 aShapePtr->setImpl(new TopoDS_Shape(aShape));
102 aSelAttr->setValue(aRes, aShapePtr);
106 myWorkshop->viewer()->setViewProjection(aDir->x(), aDir->y(), aDir->z());
108 // Clear text in the label
109 myLabel->setText("");
110 myLabel->setToolTip("");
111 disconnect(myWorkshop->selector(), SIGNAL(selectionChanged()),
112 this, SLOT(onPlaneSelected()));
114 // Clear selection mode and define sketching mode
115 XGUI_Displayer* aDisp = myWorkshop->displayer();
116 aDisp->removeSelectionFilter(myFaceFilter);
117 //aDisp->closeLocalContexts();
118 emit planeSelected(plane());
121 // Update sketcher actions
122 XGUI_ActionsMgr* anActMgr = myWorkshop->actionsMgr();
124 myWorkshop->viewer()->update();
130 std::shared_ptr<GeomAPI_Pln> PartSet_WidgetSketchLabel::plane() const
132 CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
133 return PartSet_Tools::sketchPlane(aSketch);
137 void PartSet_WidgetSketchLabel::activateCustom()
139 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
141 //setSketchingMode();
142 // In order to avoid Opening/Closing of context too often
143 mySelectionTimer->start(20);
145 // We have to select a plane before any operation
148 XGUI_Displayer* aDisp = myWorkshop->displayer();
149 //aDisp->openLocalContext();
150 //aDisp->activateObjects(QIntList());
151 if (myFaceFilter.IsNull())
152 myFaceFilter = new StdSelect_FaceFilter(StdSelect_Plane);
153 aDisp->addSelectionFilter(myFaceFilter);
155 aModes << TopAbs_FACE;
156 aDisp->activateObjects(aModes);
158 myLabel->setText(myText);
159 myLabel->setToolTip(myTooltip);
161 connect(myWorkshop->selector(), SIGNAL(selectionChanged()), this, SLOT(onPlaneSelected()));
162 aDisp->updateViewer();
166 void PartSet_WidgetSketchLabel::deactivate()
168 // Do not set selection mode if the widget was activated for a small moment
169 mySelectionTimer->stop();
170 XGUI_Displayer* aDisp = myWorkshop->displayer();
171 aDisp->removeSelectionFilter(myFaceFilter);
172 //aDisp->removeSelectionFilter(mySketchFilter);
173 //aDisp->closeLocalContexts();
174 erasePreviewPlanes();
177 void PartSet_WidgetSketchLabel::erasePreviewPlanes()
179 if (myPreviewDisplayed) {
180 XGUI_Displayer* aDisp = myWorkshop->displayer();
181 aDisp->eraseAIS(myYZPlane, false);
182 aDisp->eraseAIS(myXZPlane, false);
183 aDisp->eraseAIS(myXYPlane, false);
184 myPreviewDisplayed = false;
188 void PartSet_WidgetSketchLabel::showPreviewPlanes()
190 if (myPreviewDisplayed)
193 if (!myYZPlane) { // If planes are not created
195 std::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
196 std::shared_ptr<GeomAPI_Dir> aYZDir(new GeomAPI_Dir(1, 0, 0));
197 std::shared_ptr<GeomAPI_Dir> aXZDir(new GeomAPI_Dir(0, 1, 0));
198 std::shared_ptr<GeomAPI_Dir> aXYDir(new GeomAPI_Dir(0, 0, 1));
200 std::vector<int> aYZRGB, aXZRGB, aXYRGB;
201 aYZRGB = Config_PropManager::color("Visualization", "yz_plane_color",
203 aXZRGB = Config_PropManager::color("Visualization", "xz_plane_color",
205 aXYRGB = Config_PropManager::color("Visualization", "xy_plane_color",
207 int aR[] = {aYZRGB[0], aYZRGB[1], aYZRGB[2]};
208 int aG[] = {aXZRGB[0], aXZRGB[1], aXZRGB[2]};
209 int aB[] = {aXYRGB[0], aXYRGB[1], aXYRGB[2]};
211 myYZPlane = createPreviewPlane(anOrigin, aYZDir, aR);
212 myXZPlane = createPreviewPlane(anOrigin, aXZDir, aG);
213 myXYPlane = createPreviewPlane(anOrigin, aXYDir, aB);
215 XGUI_Displayer* aDisp = myWorkshop->displayer();
216 aDisp->displayAIS(myYZPlane, false);
217 aDisp->displayAIS(myXZPlane, false);
218 aDisp->displayAIS(myXYPlane, false);
219 myPreviewDisplayed = true;
223 AISObjectPtr PartSet_WidgetSketchLabel::createPreviewPlane(std::shared_ptr<GeomAPI_Pnt> theOrigin,
224 std::shared_ptr<GeomAPI_Dir> theNorm,
227 double aSize = Config_PropManager::integer("Sketch planes", "planes_size", PLANE_SIZE);
228 std::shared_ptr<GeomAPI_Shape> aFace = GeomAlgoAPI_FaceBuilder::square(theOrigin, theNorm, aSize);
229 AISObjectPtr aAIS = AISObjectPtr(new GeomAPI_AISObject());
230 aAIS->createShape(aFace);
231 aAIS->setWidth(Config_PropManager::integer("Sketch planes", "planes_thickness", SKETCH_WIDTH));
232 aAIS->setColor(theRGB[0], theRGB[1], theRGB[2]);
237 std::shared_ptr<GeomAPI_Dir> PartSet_WidgetSketchLabel::setSketchPlane(const TopoDS_Shape& theShape)
239 if (theShape.IsNull())
240 return std::shared_ptr<GeomAPI_Dir>();
242 // get selected shape
243 std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
244 aGShape->setImpl(new TopoDS_Shape(theShape));
246 // get plane parameters
247 std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
249 // set plane parameters to feature
250 std::shared_ptr<ModelAPI_Data> aData = feature()->data();
251 double anA, aB, aC, aD;
252 aPlane->coefficients(anA, aB, aC, aD);
254 // calculate attributes of the sketch
255 std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
256 std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
257 std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
258 aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
259 std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
260 // X axis is preferable to be dirX on the sketch
261 const double tol = Precision::Confusion();
262 bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
263 std::shared_ptr<GeomAPI_Dir> aTempDir(
264 isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
265 std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
266 std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
268 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
269 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
270 anOrigin->setValue(anOrigPnt);
271 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
272 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
273 aNormal->setValue(aNormDir);
274 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
275 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
276 aDirX->setValue(aXDir);
277 std::shared_ptr<GeomDataAPI_Dir> aDirY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
278 aData->attribute(SketchPlugin_Sketch::DIRY_ID()));
279 aDirY->setValue(aYDir);
280 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
285 void PartSet_WidgetSketchLabel::setSketchingMode()
287 XGUI_Displayer* aDisp = myWorkshop->displayer();
288 // Clear standard selection modes if they are defined
289 //aDisp->activateObjects(aModes);
290 //aDisp->openLocalContext();
292 // Get default selection modes
294 aModes.append(AIS_DSM_Text);
295 aModes.append(AIS_DSM_Line);
296 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_VERTEX));
297 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_EDGE));
299 aDisp->activateObjects(aModes);