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