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 QList<QWidget*> aResult;
82 void PartSet_WidgetSketchLabel::onPlaneSelected()
84 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
85 QList<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
86 if (!aSelected.empty()) {
87 ModuleBase_ViewerPrs aPrs = aSelected.first();
88 TopoDS_Shape aShape = aPrs.shape();
89 if (!aShape.IsNull()) {
90 std::shared_ptr<GeomAPI_Dir> aDir = setSketchPlane(aShape);
94 if (aPrs.object() && (feature() != aPrs.object())) {
95 DataPtr aData = feature()->data();
96 AttributeSelectionPtr aSelAttr =
97 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
98 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
100 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs.object());
102 GeomShapePtr aShapePtr(new GeomAPI_Shape());
103 aShapePtr->setImpl(new TopoDS_Shape(aShape));
104 aSelAttr->setValue(aRes, aShapePtr);
108 myWorkshop->viewer()->setViewProjection(aDir->x(), aDir->y(), aDir->z());
110 // Clear text in the label
111 myLabel->setText("");
112 myLabel->setToolTip("");
113 disconnect(myWorkshop->selector(), SIGNAL(selectionChanged()),
114 this, SLOT(onPlaneSelected()));
116 // Clear selection mode and define sketching mode
117 XGUI_Displayer* aDisp = myWorkshop->displayer();
118 aDisp->removeSelectionFilter(myFaceFilter);
119 //aDisp->closeLocalContexts();
120 emit planeSelected(plane());
123 // Update sketcher actions
124 XGUI_ActionsMgr* anActMgr = myWorkshop->actionsMgr();
126 myWorkshop->viewer()->update();
132 std::shared_ptr<GeomAPI_Pln> PartSet_WidgetSketchLabel::plane() const
134 CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
135 return PartSet_Tools::sketchPlane(aSketch);
139 bool PartSet_WidgetSketchLabel::focusTo()
145 void PartSet_WidgetSketchLabel::enableFocusProcessing()
147 myLabel->installEventFilter(this);
150 void PartSet_WidgetSketchLabel::activateCustom()
152 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
154 //setSketchingMode();
155 // In order to avoid Opening/Closing of context too often
156 mySelectionTimer->start(20);
158 // We have to select a plane before any operation
161 XGUI_Displayer* aDisp = myWorkshop->displayer();
162 //aDisp->openLocalContext();
163 //aDisp->activateObjects(QIntList());
164 if (myFaceFilter.IsNull())
165 myFaceFilter = new StdSelect_FaceFilter(StdSelect_Plane);
166 aDisp->addSelectionFilter(myFaceFilter);
168 aModes << TopAbs_FACE;
169 aDisp->activateObjects(aModes);
171 myLabel->setText(myText);
172 myLabel->setToolTip(myTooltip);
174 connect(myWorkshop->selector(), SIGNAL(selectionChanged()), this, SLOT(onPlaneSelected()));
175 aDisp->updateViewer();
179 void PartSet_WidgetSketchLabel::deactivate()
181 // Do not set selection mode if the widget was activated for a small moment
182 mySelectionTimer->stop();
183 XGUI_Displayer* aDisp = myWorkshop->displayer();
184 aDisp->removeSelectionFilter(myFaceFilter);
185 //aDisp->removeSelectionFilter(mySketchFilter);
186 //aDisp->closeLocalContexts();
187 erasePreviewPlanes();
190 void PartSet_WidgetSketchLabel::erasePreviewPlanes()
192 if (myPreviewDisplayed) {
193 XGUI_Displayer* aDisp = myWorkshop->displayer();
194 aDisp->eraseAIS(myYZPlane, false);
195 aDisp->eraseAIS(myXZPlane, false);
196 aDisp->eraseAIS(myXYPlane, false);
197 myPreviewDisplayed = false;
201 void PartSet_WidgetSketchLabel::showPreviewPlanes()
203 if (myPreviewDisplayed)
206 if (!myYZPlane) { // If planes are not created
208 std::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
209 std::shared_ptr<GeomAPI_Dir> aYZDir(new GeomAPI_Dir(1, 0, 0));
210 std::shared_ptr<GeomAPI_Dir> aXZDir(new GeomAPI_Dir(0, 1, 0));
211 std::shared_ptr<GeomAPI_Dir> aXYDir(new GeomAPI_Dir(0, 0, 1));
213 std::vector<int> aYZRGB, aXZRGB, aXYRGB;
214 aYZRGB = Config_PropManager::color("Visualization", "yz_plane_color",
216 aXZRGB = Config_PropManager::color("Visualization", "xz_plane_color",
218 aXYRGB = Config_PropManager::color("Visualization", "xy_plane_color",
220 int aR[] = {aYZRGB[0], aYZRGB[1], aYZRGB[2]};
221 int aG[] = {aXZRGB[0], aXZRGB[1], aXZRGB[2]};
222 int aB[] = {aXYRGB[0], aXYRGB[1], aXYRGB[2]};
224 myYZPlane = createPreviewPlane(anOrigin, aYZDir, aR);
225 myXZPlane = createPreviewPlane(anOrigin, aXZDir, aG);
226 myXYPlane = createPreviewPlane(anOrigin, aXYDir, aB);
228 XGUI_Displayer* aDisp = myWorkshop->displayer();
229 aDisp->displayAIS(myYZPlane, false);
230 aDisp->displayAIS(myXZPlane, false);
231 aDisp->displayAIS(myXYPlane, false);
232 myPreviewDisplayed = true;
236 AISObjectPtr PartSet_WidgetSketchLabel::createPreviewPlane(std::shared_ptr<GeomAPI_Pnt> theOrigin,
237 std::shared_ptr<GeomAPI_Dir> theNorm,
240 double aSize = Config_PropManager::integer("Sketch planes", "planes_size", PLANE_SIZE);
241 std::shared_ptr<GeomAPI_Shape> aFace = GeomAlgoAPI_FaceBuilder::square(theOrigin, theNorm, aSize);
242 AISObjectPtr aAIS = AISObjectPtr(new GeomAPI_AISObject());
243 aAIS->createShape(aFace);
244 aAIS->setWidth(Config_PropManager::integer("Sketch planes", "planes_thickness", SKETCH_WIDTH));
245 aAIS->setColor(theRGB[0], theRGB[1], theRGB[2]);
250 std::shared_ptr<GeomAPI_Dir> PartSet_WidgetSketchLabel::setSketchPlane(const TopoDS_Shape& theShape)
252 if (theShape.IsNull())
253 return std::shared_ptr<GeomAPI_Dir>();
255 // get selected shape
256 std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
257 aGShape->setImpl(new TopoDS_Shape(theShape));
259 // get plane parameters
260 std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
262 // set plane parameters to feature
263 std::shared_ptr<ModelAPI_Data> aData = feature()->data();
264 double anA, aB, aC, aD;
265 aPlane->coefficients(anA, aB, aC, aD);
267 // calculate attributes of the sketch
268 std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
269 std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
270 std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
271 aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
272 std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
273 // X axis is preferable to be dirX on the sketch
274 const double tol = Precision::Confusion();
275 bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
276 std::shared_ptr<GeomAPI_Dir> aTempDir(
277 isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
278 std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
279 std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
281 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
282 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
283 anOrigin->setValue(anOrigPnt);
284 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
285 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
286 aNormal->setValue(aNormDir);
287 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
288 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
289 aDirX->setValue(aXDir);
290 std::shared_ptr<GeomDataAPI_Dir> aDirY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
291 aData->attribute(SketchPlugin_Sketch::DIRY_ID()));
292 aDirY->setValue(aYDir);
293 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
298 void PartSet_WidgetSketchLabel::setSketchingMode()
300 XGUI_Displayer* aDisp = myWorkshop->displayer();
301 // Clear standard selection modes if they are defined
302 //aDisp->activateObjects(aModes);
303 //aDisp->openLocalContext();
305 // Get default selection modes
307 aModes.append(AIS_DSM_Text);
308 aModes.append(AIS_DSM_Line);
309 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_VERTEX));
310 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_EDGE));
312 aDisp->activateObjects(aModes);