]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_WidgetSketchLabel.cpp
Salome HOME
Debug preferences of sketch planes
[modules/shaper.git] / src / PartSet / PartSet_WidgetSketchLabel.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        PartSet_WidgetSketchLabel.cpp
4 // Created:     07 July 2014
5 // Author:      Vitaly SMETANNIKOV
6
7 #include "PartSet_WidgetSketchLabel.h"
8 #include "PartSet_Tools.h"
9
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>
16
17 #include <ModuleBase_Operation.h>
18 #include <ModuleBase_ViewerPrs.h>
19
20 #include <GeomAlgoAPI_FaceBuilder.h>
21 #include <GeomDataAPI_Point.h>
22 #include <GeomDataAPI_Dir.h>
23 #include <GeomAPI_XYZ.h>
24
25 #include <SketchPlugin_Sketch.h>
26
27 #include <Precision.hxx>
28 #include <gp_Pln.hxx>
29 #include <gp_Pnt.hxx>
30 #include <gp_Dir.hxx>
31 #include <AIS_Shape.hxx>
32 #include <AIS_DimensionSelectionMode.hxx>
33
34 #include <Config_WidgetAPI.h>
35 #include <Config_PropManager.h>
36
37 #include <QLabel>
38 #include <QTimer>
39 #include <QApplication>
40
41
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)
46 {
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);
53
54   mySelectionTimer = new QTimer(this);
55   connect(mySelectionTimer, SIGNAL(timeout()), SLOT(setSketchingMode()));
56   mySelectionTimer->setSingleShot(true);
57 }
58
59 PartSet_WidgetSketchLabel::~PartSet_WidgetSketchLabel()
60 {
61   erasePreviewPlanes();
62 }
63
64 QList<QWidget*> PartSet_WidgetSketchLabel::getControls() const
65 {
66   return QList<QWidget*>();
67 }
68
69 QWidget* PartSet_WidgetSketchLabel::getControl() const
70 {
71   return myLabel;
72 }
73
74 void PartSet_WidgetSketchLabel::onPlaneSelected()
75 {
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);
83       if (aDir) {
84         erasePreviewPlanes();
85
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()));
91           if (aSelAttr) {
92             ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs.object());
93             if (aRes) {
94               GeomShapePtr aShapePtr(new GeomAPI_Shape());
95               aShapePtr->setImpl(new TopoDS_Shape(aShape));
96               aSelAttr->setValue(aRes, aShapePtr);
97             }
98           }
99         } else
100           myWorkshop->viewer()->setViewProjection(aDir->x(), aDir->y(), aDir->z());
101
102         // Clear text in the label
103         myLabel->setText("");
104         myLabel->setToolTip("");
105         disconnect(myWorkshop->selector(), SIGNAL(selectionChanged()), 
106                    this, SLOT(onPlaneSelected()));
107
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());
113         setSketchingMode();
114
115         // Update sketcher actions
116         XGUI_ActionsMgr* anActMgr = myWorkshop->actionsMgr();
117         anActMgr->update();
118         myWorkshop->viewer()->update();
119       }
120     }
121   }
122 }
123
124 std::shared_ptr<GeomAPI_Pln> PartSet_WidgetSketchLabel::plane() const
125 {
126   CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
127   return PartSet_Tools::sketchPlane(aSketch);
128
129 }
130
131 void PartSet_WidgetSketchLabel::activateCustom()
132 {
133   std::shared_ptr<GeomAPI_Pln> aPlane = plane();
134   if (aPlane) {
135     //setSketchingMode();
136     // In order to avoid Opening/Closing of context too often
137     mySelectionTimer->start(20);
138   } else {
139     // We have to select a plane before any operation
140     showPreviewPlanes();
141
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);
148     QIntList aModes;
149     aModes << TopAbs_FACE;
150     aDisp->activateObjects(aModes);
151
152     myLabel->setText(myText);
153     myLabel->setToolTip(myTooltip);
154
155     connect(myWorkshop->selector(), SIGNAL(selectionChanged()), this, SLOT(onPlaneSelected()));
156     aDisp->updateViewer();
157   }
158 }
159
160 void PartSet_WidgetSketchLabel::deactivate()
161 {
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();
169 }
170
171 void PartSet_WidgetSketchLabel::erasePreviewPlanes()
172 {
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;
179   }
180 }
181
182 void PartSet_WidgetSketchLabel::showPreviewPlanes()
183 {
184   if (myPreviewDisplayed)
185     return;
186
187   if (!myYZPlane) { // If planes are not created
188     // Create Preview
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));
193
194     int aR[] = {255, 0, 0};
195     int aG[] = {0, 255, 0};
196     int aB[] = {0, 0, 255};
197
198     myYZPlane = createPreviewPlane(anOrigin, aYZDir, aR);
199     myXZPlane = createPreviewPlane(anOrigin, aXZDir, aG);
200     myXYPlane = createPreviewPlane(anOrigin, aXYDir, aB);
201   }
202   XGUI_Displayer* aDisp = myWorkshop->displayer();
203   aDisp->displayAIS(myYZPlane, false);
204   aDisp->displayAIS(myXZPlane, false);
205   aDisp->displayAIS(myXYPlane, false);
206   myPreviewDisplayed = true;
207 }
208
209
210 AISObjectPtr PartSet_WidgetSketchLabel::createPreviewPlane(std::shared_ptr<GeomAPI_Pnt> theOrigin, 
211                                                            std::shared_ptr<GeomAPI_Dir> theNorm, 
212                                                            const int theRGB[3])
213 {
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]);
220   return aAIS;
221 }
222
223
224 std::shared_ptr<GeomAPI_Dir> PartSet_WidgetSketchLabel::setSketchPlane(const TopoDS_Shape& theShape)
225 {
226   if (theShape.IsNull())
227     return std::shared_ptr<GeomAPI_Dir>();
228
229   // get selected shape
230   std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
231   aGShape->setImpl(new TopoDS_Shape(theShape));
232
233   // get plane parameters
234   std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
235
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);
240
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)));
254
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();
268   return aDir;
269 }
270
271
272 void PartSet_WidgetSketchLabel::setSketchingMode()
273 {
274   XGUI_Displayer* aDisp = myWorkshop->displayer();
275   // Clear standard selection modes if they are defined
276   //aDisp->activateObjects(aModes);
277   //aDisp->openLocalContext();
278
279   // Get default selection modes
280   QIntList aModes;
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));
285
286   aDisp->activateObjects(aModes);
287 }