Salome HOME
d5aa73b6bde91d841f5e823131761af5955e28b4
[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
39 #define PLANE_SIZE          "200"     
40 #define SKETCH_WIDTH        "4"
41
42
43 PartSet_WidgetSketchLabel::PartSet_WidgetSketchLabel(QWidget* theParent,
44                                                      const Config_WidgetAPI* theData,
45                                                      const std::string& theParentId)
46     : ModuleBase_ModelWidget(theParent, theData, theParentId), myPreviewDisplayed(false)
47 {
48   myText = QString::fromStdString(theData->getProperty("title"));
49   myLabel = new QLabel("", theParent);
50   myLabel->setWordWrap(true);
51   myTooltip = QString::fromStdString(theData->getProperty("tooltip"));
52   myLabel->setToolTip("");
53   myLabel->setIndent(5);
54 }
55
56 PartSet_WidgetSketchLabel::~PartSet_WidgetSketchLabel()
57 {
58   erasePreviewPlanes();
59 }
60
61 QList<QWidget*> PartSet_WidgetSketchLabel::getControls() const
62 {
63   return QList<QWidget*>();
64 }
65
66 QWidget* PartSet_WidgetSketchLabel::getControl() const
67 {
68   return myLabel;
69 }
70
71 void PartSet_WidgetSketchLabel::onPlaneSelected()
72 {
73   XGUI_Selection* aSelection = myWorkshop->selector()->selection();
74   QList<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
75   if (!aSelected.empty()) {
76     ModuleBase_ViewerPrs aPrs = aSelected.first();
77     TopoDS_Shape aShape = aPrs.shape();
78     if (!aShape.IsNull()) {
79       std::shared_ptr<GeomAPI_Dir> aDir = setSketchPlane(aShape);
80       if (aDir) {
81         erasePreviewPlanes();
82
83         if (aPrs.object() && (feature() != aPrs.object())) {
84           DataPtr aData = feature()->data();
85           AttributeSelectionPtr aSelAttr = 
86             std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
87             (aData->attribute(SketchPlugin_Feature::EXTERNAL_ID()));
88           if (aSelAttr) {
89             ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs.object());
90             if (aRes) {
91               GeomShapePtr aShapePtr(new GeomAPI_Shape());
92               aShapePtr->setImpl(new TopoDS_Shape(aShape));
93               aSelAttr->setValue(aRes, aShapePtr);
94             }
95           }
96         } else
97           myWorkshop->viewer()->setViewProjection(aDir->x(), aDir->y(), aDir->z());
98
99         // Clear text in the label
100         myLabel->setText("");
101         myLabel->setToolTip("");
102         disconnect(myWorkshop->selector(), SIGNAL(selectionChanged()), 
103                    this, SLOT(onPlaneSelected()));
104
105         // Clear selection mode and define sketching mode
106         XGUI_Displayer* aDisp = myWorkshop->displayer();
107         aDisp->removeSelectionFilter(myFaceFilter);
108         aDisp->closeLocalContexts();
109         emit planeSelected(plane());
110         setSketchingMode();
111
112         // Update sketcher actions
113         XGUI_ActionsMgr* anActMgr = myWorkshop->actionsMgr();
114         anActMgr->update();
115       }
116     }
117   }
118 }
119
120 std::shared_ptr<GeomAPI_Pln> PartSet_WidgetSketchLabel::plane() const
121 {
122   CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
123   return PartSet_Tools::sketchPlane(aSketch);
124
125 }
126
127 void PartSet_WidgetSketchLabel::activate()
128 {
129   std::shared_ptr<GeomAPI_Pln> aPlane = plane();
130   if (aPlane) {
131     setSketchingMode();
132   } else {
133     // We have to select a plane before any operation
134     showPreviewPlanes();
135
136     XGUI_Displayer* aDisp = myWorkshop->displayer();
137     aDisp->openLocalContext();
138     aDisp->activateObjects(QIntList());
139     if (myFaceFilter.IsNull())
140       myFaceFilter = new StdSelect_FaceFilter(StdSelect_Plane);
141     aDisp->addSelectionFilter(myFaceFilter);
142     QIntList aModes;
143     aModes << TopAbs_FACE;
144     aDisp->activateObjects(aModes);
145
146     myLabel->setText(myText);
147     myLabel->setToolTip(myTooltip);
148
149     connect(myWorkshop->selector(), SIGNAL(selectionChanged()), this, SLOT(onPlaneSelected()));
150     aDisp->updateViewer();
151   }
152 }
153
154 void PartSet_WidgetSketchLabel::deactivate()
155 {
156
157   XGUI_Displayer* aDisp = myWorkshop->displayer();
158   aDisp->removeSelectionFilter(myFaceFilter);
159   //aDisp->removeSelectionFilter(mySketchFilter);
160   aDisp->closeLocalContexts();
161   erasePreviewPlanes();
162 }
163
164 void PartSet_WidgetSketchLabel::erasePreviewPlanes()
165 {
166   if (myPreviewDisplayed) {
167     XGUI_Displayer* aDisp = myWorkshop->displayer();
168     aDisp->eraseAIS(myYZPlane, false);
169     aDisp->eraseAIS(myXZPlane, false);
170     aDisp->eraseAIS(myXYPlane, false);
171     myPreviewDisplayed = false;
172   }
173 }
174
175 void PartSet_WidgetSketchLabel::showPreviewPlanes()
176 {
177   if (myPreviewDisplayed)
178     return;
179
180   if (!myYZPlane) { // If planes are not created
181     // Create Preview
182     std::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
183     std::shared_ptr<GeomAPI_Dir> aYZDir(new GeomAPI_Dir(1, 0, 0));
184     std::shared_ptr<GeomAPI_Dir> aXZDir(new GeomAPI_Dir(0, 1, 0));
185     std::shared_ptr<GeomAPI_Dir> aXYDir(new GeomAPI_Dir(0, 0, 1));
186
187     int aR[] = {255, 0, 0};
188     int aG[] = {0, 255, 0};
189     int aB[] = {0, 0, 255};
190
191     myYZPlane = createPreviewPlane(anOrigin, aYZDir, aR);
192     myXZPlane = createPreviewPlane(anOrigin, aXZDir, aG);
193     myXYPlane = createPreviewPlane(anOrigin, aXYDir, aB);
194   }
195   XGUI_Displayer* aDisp = myWorkshop->displayer();
196   aDisp->displayAIS(myYZPlane, false);
197   aDisp->displayAIS(myXZPlane, false);
198   aDisp->displayAIS(myXYPlane, false);
199   myPreviewDisplayed = true;
200 }
201
202
203 AISObjectPtr PartSet_WidgetSketchLabel::createPreviewPlane(std::shared_ptr<GeomAPI_Pnt> theOrigin, 
204                                                            std::shared_ptr<GeomAPI_Dir> theNorm, 
205                                                            const int theRGB[3])
206 {
207   double aSize = Config_PropManager::integer("Sketch planes", "Size of planes", PLANE_SIZE);
208   std::shared_ptr<GeomAPI_Shape> aFace = GeomAlgoAPI_FaceBuilder::square(theOrigin, theNorm, aSize);
209   AISObjectPtr aAIS = AISObjectPtr(new GeomAPI_AISObject());
210   aAIS->createShape(aFace);
211   aAIS->setWidth(Config_PropManager::integer("Sketch planes", "planes_thickness", SKETCH_WIDTH));
212   aAIS->setColor(theRGB[0], theRGB[1], theRGB[2]);
213   return aAIS;
214 }
215
216
217 std::shared_ptr<GeomAPI_Dir> PartSet_WidgetSketchLabel::setSketchPlane(const TopoDS_Shape& theShape)
218 {
219   if (theShape.IsNull())
220     return std::shared_ptr<GeomAPI_Dir>();
221
222   // get selected shape
223   std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
224   aGShape->setImpl(new TopoDS_Shape(theShape));
225
226   // get plane parameters
227   std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
228
229   // set plane parameters to feature
230   std::shared_ptr<ModelAPI_Data> aData = feature()->data();
231   double anA, aB, aC, aD;
232   aPlane->coefficients(anA, aB, aC, aD);
233
234   // calculate attributes of the sketch
235   std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
236   std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
237   std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
238   aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
239   std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
240   // X axis is preferable to be dirX on the sketch
241   const double tol = Precision::Confusion();
242   bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
243   std::shared_ptr<GeomAPI_Dir> aTempDir(
244       isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
245   std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
246   std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
247
248   std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
249       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
250   anOrigin->setValue(anOrigPnt);
251   std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
252       aData->attribute(SketchPlugin_Sketch::NORM_ID()));
253   aNormal->setValue(aNormDir);
254   std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
255       aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
256   aDirX->setValue(aXDir);
257   std::shared_ptr<GeomDataAPI_Dir> aDirY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
258       aData->attribute(SketchPlugin_Sketch::DIRY_ID()));
259   aDirY->setValue(aYDir);
260   std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
261   return aDir;
262 }
263
264
265 void PartSet_WidgetSketchLabel::setSketchingMode()
266 {
267   XGUI_Displayer* aDisp = myWorkshop->displayer();
268   QIntList aModes;
269   // Clear standard selection modes if they are defined
270   aDisp->activateObjects(aModes);
271   aDisp->openLocalContext();
272
273   // Set filter
274   std::shared_ptr<GeomAPI_Pln> aPlane = plane();
275   double aA, aB, aC, aD;
276   aPlane->coefficients(aA, aB, aC, aD);
277   gp_Pln aPln(aA, aB, aC, aD);
278   // No selection of external objects
279   //mySketchFilter = new ModuleBase_ShapeInPlaneFilter(aPln);
280   //aDisp->addSelectionFilter(mySketchFilter);
281
282   // Get default selection modes
283   aModes.append(AIS_DSM_Text);
284   aModes.append(AIS_DSM_Line);
285   aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_VERTEX));
286   aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_EDGE));
287
288   aDisp->activateObjects(aModes);
289 }