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