Salome HOME
Shape plane filter should process shapes of objects selected in object browser.
[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 "SketchPlugin_SketchEntity.h"
11
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>
18 #include <XGUI_ModuleConnector.h>
19
20 #include <ModuleBase_Operation.h>
21 #include <ModuleBase_ViewerPrs.h>
22 #include <ModuleBase_Tools.h>
23 #include <ModuleBase_IModule.h>
24
25 #include <ModelAPI_ResultBody.h>
26 #include <ModelAPI_Tools.h>
27
28 #include <GeomAlgoAPI_FaceBuilder.h>
29 #include <GeomAlgoAPI_ShapeProps.h>
30 #include <GeomDataAPI_Point.h>
31 #include <GeomDataAPI_Dir.h>
32 #include <GeomAPI_XYZ.h>
33
34 #include <SketchPlugin_Sketch.h>
35 #include <SketcherPrs_Tools.h>
36
37 #include <BRepClass3d_SolidClassifier.hxx>
38 #include <Precision.hxx>
39 #include <gp_Pln.hxx>
40 #include <gp_Pnt.hxx>
41 #include <gp_Dir.hxx>
42 #include <AIS_Shape.hxx>
43 #include <AIS_DimensionSelectionMode.hxx>
44
45 #include <Config_WidgetAPI.h>
46 #include <Config_PropManager.h>
47
48 #include <QLabel>
49 #include <QApplication>
50 #include <QVBoxLayout>
51 #include <QCheckBox>
52
53
54 PartSet_WidgetSketchLabel::PartSet_WidgetSketchLabel(QWidget* theParent,
55                                                      ModuleBase_IWorkshop* theWorkshop,
56                                                      const Config_WidgetAPI* theData,
57                                                      const std::string& theParentId,
58                                                      bool toShowConstraints)
59 : ModuleBase_WidgetValidated(theParent, theWorkshop, theData, theParentId),
60   myPreviewDisplayed(false)
61 {
62   myText = QString::fromStdString(theData->getProperty("title"));
63   myLabel = new QLabel("", theParent);
64   myLabel->setWordWrap(true);
65   myTooltip = QString::fromStdString(theData->getProperty("tooltip"));
66   myLabel->setToolTip("");
67   myLabel->setIndent(5);
68
69   QVBoxLayout* aLayout = new QVBoxLayout(this);
70   ModuleBase_Tools::zeroMargins(aLayout);
71   aLayout->addWidget(myLabel);
72
73   myShowConstraints = new QCheckBox(tr("Show constraints"), this);
74   aLayout->addWidget(myShowConstraints);
75
76   setLayout(aLayout);
77   connect(myShowConstraints, SIGNAL(toggled(bool)), this, SIGNAL(showConstraintToggled(bool)));
78   myShowConstraints->setChecked(toShowConstraints);
79 }
80
81 PartSet_WidgetSketchLabel::~PartSet_WidgetSketchLabel()
82 {
83   erasePreviewPlanes();
84 }
85
86 bool PartSet_WidgetSketchLabel::setSelection(QList<ModuleBase_ViewerPrs>& theValues,
87                                              const bool theToValidate)
88 {
89   // do not use the given selection if the plane of the sketch has been already set.
90   // If this check is absent, a selected plane in the viewer can be set in the sketch
91   // even if the sketch is built on another plane.
92   if (plane().get())
93     return true;
94
95   return ModuleBase_WidgetValidated::setSelection(theValues, theToValidate);
96 }
97
98 QList<QWidget*> PartSet_WidgetSketchLabel::getControls() const
99 {
100   QList<QWidget*> aResult;
101   aResult << myLabel;
102   return aResult;
103 }
104
105 void PartSet_WidgetSketchLabel::onSelectionChanged()
106 {
107   QList<ModuleBase_ViewerPrs> aSelected = myWorkshop->selection()->getSelected(
108                                                            ModuleBase_ISelection::AllControls);
109   if (aSelected.empty())
110     return;
111   ModuleBase_ViewerPrs aPrs = aSelected.first();
112
113   bool isDone = ModuleBase_WidgetValidated::setSelection(aSelected, true);
114   if (!isDone)
115     return;
116
117   // 3. hide main planes if they have been displayed
118   erasePreviewPlanes();
119   // 4. if the planes were displayed, change the view projection
120   TopoDS_Shape aShape = aPrs.shape();
121   std::shared_ptr<GeomAPI_Shape> aGShape;
122   std::shared_ptr<GeomAPI_Shape> aBaseShape;
123
124   DataPtr aData = feature()->data();
125   AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
126                             (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
127
128   // selection happens in OCC viewer
129   if (!aShape.IsNull()) {
130     aGShape =  std::make_shared<GeomAPI_Shape>();
131     aGShape->setImpl(new TopoDS_Shape(aShape));
132
133     if (aSelAttr && aSelAttr->context()) {
134       aBaseShape = aSelAttr->context()->shape();
135     }
136   }
137   else { // selection happens in OCC viewer(on body) of in the OB browser
138     if (aSelAttr) {
139       aGShape = aSelAttr->value();
140     }
141   }
142   if (aGShape.get() != NULL) {
143     // get plane parameters
144     std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
145     std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
146     gp_XYZ aXYZ = aDir->impl<gp_Dir>().XYZ();
147
148     // orienting projection
149     if(aBaseShape.get() != NULL) {
150       std::shared_ptr<GeomAPI_Pnt> aCenterPnt = GeomAlgoAPI_ShapeProps::centreOfMass(aGShape);
151       gp_Pnt aPnt = aCenterPnt->impl<gp_Pnt>();
152       aPnt.Translate(aDir->impl<gp_Dir>().XYZ() * (10 * Precision::Confusion()));
153
154       BRepClass3d_SolidClassifier aClassifier;
155       aClassifier.Load(aBaseShape->impl<TopoDS_Shape>());
156       aClassifier.Perform(aPnt, Precision::Confusion());
157
158       if(aClassifier.State() == TopAbs_IN) {
159         aXYZ.Reverse();
160       }
161     }
162
163     myWorkshop->viewer()->setViewProjection(aXYZ.X(), aXYZ.Y(), aXYZ.Z());
164   }
165   // 5. Clear text in the label
166   myLabel->setText("");
167   myLabel->setToolTip("");
168   disconnect(workshop()->selector(), SIGNAL(selectionChanged()), 
169               this, SLOT(onSelectionChanged()));
170   // 6. deactivate face selection filter
171   activateFilters(false);
172
173   // 7. Clear selection mode and define sketching mode
174   //XGUI_Displayer* aDisp = workshop()->displayer();
175   //aDisp->closeLocalContexts();
176   emit planeSelected(plane());
177   // after the plane is selected in the sketch, the sketch selection should be activated
178   // it can not be performed in the sketch label widget because, we don't need to switch off
179   // the selection by any label deactivation, but need to switch it off by stop the sketch
180   activateSelection(true);
181
182   // 8. Update sketcher actions
183   XGUI_ActionsMgr* anActMgr = workshop()->actionsMgr();
184   anActMgr->update();
185   myWorkshop->viewer()->update();
186 }
187
188 std::shared_ptr<GeomAPI_Pln> PartSet_WidgetSketchLabel::plane() const
189 {
190   CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
191   return PartSet_Tools::sketchPlane(aSketch);
192
193 }
194
195 bool PartSet_WidgetSketchLabel::focusTo()
196 {
197   myLabel->setFocus();
198   return true;
199 }
200
201 void PartSet_WidgetSketchLabel::enableFocusProcessing()
202 {
203   myLabel->installEventFilter(this);
204 }
205
206 void PartSet_WidgetSketchLabel::storeAttributeValue()
207 {
208 }
209
210 void PartSet_WidgetSketchLabel::restoreAttributeValue(const bool theValid)
211 {
212   // it is not necessary to save the previous plane value because the plane is chosen once
213   DataPtr aData = feature()->data();
214   AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
215     (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
216   if (aSelAttr) {
217     ResultPtr anEmptyResult;
218     GeomShapePtr anEmptyShape;
219     aSelAttr->setValue(anEmptyResult, anEmptyShape);
220   }
221 }
222
223 bool PartSet_WidgetSketchLabel::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
224 {
225   bool isOwnerSet = false;
226
227   const TopoDS_Shape& aShape = thePrs.shape();
228   std::shared_ptr<GeomAPI_Dir> aDir;
229
230   if (thePrs.object() && (feature() != thePrs.object())) {
231     DataPtr aData = feature()->data();
232     AttributeSelectionPtr aSelAttr = 
233       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
234       (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
235     if (aSelAttr) {
236       ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(thePrs.object());
237       if (aRes) {
238         GeomShapePtr aShapePtr(new GeomAPI_Shape());
239         if (aShape.IsNull()) {  // selection happens in the OCC viewer
240           aShapePtr = ModelAPI_Tools::shape(aRes);
241         }
242         else { // selection happens in OB browser
243           aShapePtr->setImpl(new TopoDS_Shape(aShape));
244         }
245         if (aShapePtr.get() != NULL) {
246           aSelAttr->setValue(aRes, aShapePtr);
247           isOwnerSet = true;
248         }
249       }
250     }
251   }
252   else if (!aShape.IsNull()) {
253     aDir = setSketchPlane(aShape);
254     isOwnerSet = aDir.get();
255   }
256   return isOwnerSet;
257 }
258
259 void PartSet_WidgetSketchLabel::activateCustom()
260 {
261   std::shared_ptr<GeomAPI_Pln> aPlane = plane();
262   if (aPlane.get()) {
263     activateSelection(true);
264     return;
265   }
266
267   bool aBodyIsVisualized = false;
268   XGUI_Displayer* aDisp = workshop()->displayer();
269   QObjectPtrList aDisplayed = aDisp->displayedObjects();
270   foreach (ObjectPtr anObj, aDisplayed) {
271     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
272     if (aResult.get() != NULL) {
273       aBodyIsVisualized = aResult->groupName() == ModelAPI_ResultBody::group();
274       if (aBodyIsVisualized)
275         break;
276     }
277   }
278
279   if (!aBodyIsVisualized) {
280     // We have to select a plane before any operation
281     showPreviewPlanes();
282   }
283   activateSelection(true);
284
285   myLabel->setText(myText);
286   myLabel->setToolTip(myTooltip);
287
288   connect(workshop()->selector(), SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
289   activateFilters(true);
290
291   aDisp->updateViewer();
292 }
293
294 void PartSet_WidgetSketchLabel::deactivate()
295 {
296   erasePreviewPlanes();
297   activateSelection(false);
298
299   activateFilters(false);
300 }
301
302 void PartSet_WidgetSketchLabel::activateSelection(bool toActivate)
303 {
304   if (toActivate) {
305     QIntList aModes;
306     std::shared_ptr<GeomAPI_Pln> aPlane = plane();
307     if (aPlane.get()) {
308       myWorkshop->module()->activeSelectionModes(aModes);
309     }
310     else {
311       aModes << TopAbs_FACE;
312     }
313     myWorkshop->activateSubShapesSelection(aModes);
314   } else {
315     myWorkshop->deactivateSubShapesSelection();
316   }
317 }
318
319 void PartSet_WidgetSketchLabel::erasePreviewPlanes()
320 {
321   if (myPreviewDisplayed) {
322     XGUI_Displayer* aDisp = workshop()->displayer();
323     aDisp->eraseAIS(myYZPlane, false);
324     aDisp->eraseAIS(myXZPlane, false);
325     aDisp->eraseAIS(myXYPlane, false);
326     myPreviewDisplayed = false;
327   }
328 }
329
330 void PartSet_WidgetSketchLabel::showPreviewPlanes()
331 {
332   if (myPreviewDisplayed)
333     return;
334
335   if (!myYZPlane) { // If planes are not created
336     // Create Preview
337     std::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
338     std::shared_ptr<GeomAPI_Dir> aYZDir(new GeomAPI_Dir(1, 0, 0));
339     std::shared_ptr<GeomAPI_Dir> aXZDir(new GeomAPI_Dir(0, 1, 0));
340     std::shared_ptr<GeomAPI_Dir> aXYDir(new GeomAPI_Dir(0, 0, 1));
341
342     std::vector<int> aYZRGB, aXZRGB, aXYRGB;
343     aYZRGB = Config_PropManager::color("Visualization", "yz_plane_color",
344                                                         YZ_PLANE_COLOR);
345     aXZRGB = Config_PropManager::color("Visualization", "xz_plane_color",
346                                                         XZ_PLANE_COLOR);
347     aXYRGB = Config_PropManager::color("Visualization", "xy_plane_color",
348                                                         XY_PLANE_COLOR);
349     int aR[] = {aYZRGB[0], aYZRGB[1], aYZRGB[2]};
350     int aG[] = {aXZRGB[0], aXZRGB[1], aXZRGB[2]};
351     int aB[] = {aXYRGB[0], aXYRGB[1], aXYRGB[2]};
352
353     myYZPlane = createPreviewPlane(anOrigin, aYZDir, aR);
354     myXZPlane = createPreviewPlane(anOrigin, aXZDir, aG);
355     myXYPlane = createPreviewPlane(anOrigin, aXYDir, aB);
356   }
357   XGUI_Displayer* aDisp = workshop()->displayer();
358   aDisp->displayAIS(myYZPlane, false);
359   aDisp->displayAIS(myXZPlane, false);
360   aDisp->displayAIS(myXYPlane, false);
361   myPreviewDisplayed = true;
362 }
363
364
365 AISObjectPtr PartSet_WidgetSketchLabel::createPreviewPlane(std::shared_ptr<GeomAPI_Pnt> theOrigin, 
366                                                            std::shared_ptr<GeomAPI_Dir> theNorm, 
367                                                            const int theRGB[3])
368 {
369   double aSize = Config_PropManager::integer("Sketch planes", "planes_size", PLANE_SIZE);
370   std::shared_ptr<GeomAPI_Shape> aFace = GeomAlgoAPI_FaceBuilder::square(theOrigin, theNorm, aSize);
371   AISObjectPtr aAIS = AISObjectPtr(new GeomAPI_AISObject());
372   aAIS->createShape(aFace);
373   aAIS->setWidth(Config_PropManager::integer("Sketch planes", "planes_thickness", SKETCH_WIDTH));
374   aAIS->setColor(theRGB[0], theRGB[1], theRGB[2]);
375   return aAIS;
376 }
377
378
379 std::shared_ptr<GeomAPI_Dir> PartSet_WidgetSketchLabel::setSketchPlane(const TopoDS_Shape& theShape)
380 {
381   if (theShape.IsNull())
382     return std::shared_ptr<GeomAPI_Dir>();
383
384   // get selected shape
385   std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
386   aGShape->setImpl(new TopoDS_Shape(theShape));
387
388
389
390   // get plane parameters
391   std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
392
393   // set plane parameters to feature
394   std::shared_ptr<ModelAPI_Data> aData = feature()->data();
395   double anA, aB, aC, aD;
396   aPlane->coefficients(anA, aB, aC, aD);
397
398   // calculate attributes of the sketch
399   std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
400   std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
401   std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
402   aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
403   std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
404   // X axis is preferable to be dirX on the sketch
405   const double tol = Precision::Confusion();
406   bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
407   std::shared_ptr<GeomAPI_Dir> aTempDir(
408       isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
409   std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
410   std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
411
412   std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
413       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
414   anOrigin->setValue(anOrigPnt);
415   std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
416       aData->attribute(SketchPlugin_Sketch::NORM_ID()));
417   aNormal->setValue(aNormDir);
418   std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
419       aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
420   aDirX->setValue(aXDir);
421   std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
422   return aDir;
423 }
424
425 void PartSet_WidgetSketchLabel::showConstraints(bool theOn)
426 {
427   myShowConstraints->setChecked(theOn);
428   emit showConstraintToggled(theOn);
429 }
430
431 XGUI_Workshop* PartSet_WidgetSketchLabel::workshop() const
432 {
433   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
434   return aConnector->workshop();
435 }