1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: PartSet_WidgetSketchLabel.cpp
4 // Created: 07 July 2014
5 // Author: Vitaly SMETANNIKOV
7 #include "PartSet_WidgetSketchLabel.h"
8 #include "PartSet_Tools.h"
10 #include "SketchPlugin_SketchEntity.h"
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>
19 #include <ModuleBase_Operation.h>
20 #include <ModuleBase_ViewerPrs.h>
21 #include <ModuleBase_Tools.h>
22 #include <ModuleBase_IModule.h>
24 #include <ModelAPI_ResultBody.h>
25 #include <ModelAPI_Tools.h>
27 #include <GeomAlgoAPI_FaceBuilder.h>
28 #include <GeomDataAPI_Point.h>
29 #include <GeomDataAPI_Dir.h>
30 #include <GeomAPI_XYZ.h>
32 #include <SketchPlugin_Sketch.h>
33 #include <SketcherPrs_Tools.h>
35 #include <Precision.hxx>
39 #include <AIS_Shape.hxx>
40 #include <AIS_DimensionSelectionMode.hxx>
42 #include <Config_WidgetAPI.h>
43 #include <Config_PropManager.h>
47 #include <QApplication>
48 #include <QVBoxLayout>
52 PartSet_WidgetSketchLabel::PartSet_WidgetSketchLabel(QWidget* theParent,
53 const Config_WidgetAPI* theData,
54 const std::string& theParentId,
55 bool toShowConstraints)
56 : ModuleBase_WidgetValidated(theParent, theData, theParentId),
57 myPreviewDisplayed(false),
60 myText = QString::fromStdString(theData->getProperty("title"));
61 myLabel = new QLabel("", theParent);
62 myLabel->setWordWrap(true);
63 myTooltip = QString::fromStdString(theData->getProperty("tooltip"));
64 myLabel->setToolTip("");
65 myLabel->setIndent(5);
67 //mySelectionTimer = new QTimer(this);
68 //connect(mySelectionTimer, SIGNAL(timeout()), SLOT(setSketchingMode()));
69 //mySelectionTimer->setSingleShot(true);
71 QVBoxLayout* aLayout = new QVBoxLayout(this);
72 ModuleBase_Tools::zeroMargins(aLayout);
73 aLayout->addWidget(myLabel);
75 myShowConstraints = new QCheckBox(tr("Show constraints"), this);
76 aLayout->addWidget(myShowConstraints);
79 connect(myShowConstraints, SIGNAL(toggled(bool)), this, SIGNAL(showConstraintToggled(bool)));
80 myShowConstraints->setChecked(toShowConstraints);
83 PartSet_WidgetSketchLabel::~PartSet_WidgetSketchLabel()
88 QList<QWidget*> PartSet_WidgetSketchLabel::getControls() const
90 QList<QWidget*> aResult;
95 void PartSet_WidgetSketchLabel::onSelectionChanged()
97 ModuleBase_ViewerPrs aPrs;
98 // 1. find selected presentation either in the viewer or in OB
99 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
100 QList<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
101 // the selection in OCC viewer - the selection of a face in the viewer
102 // it can be th main plane's face of a face on a visualized body
103 if (!aSelected.empty()) {
104 aPrs = aSelected.first();
107 // the selection in Object Browser: the plane object can be used as sketch plane
108 QObjectPtrList anObjects = aSelection->selectedObjects();
109 if (!anObjects.empty()) {
110 aPrs.setObject(anObjects.first());
116 if (!isValidSelection(aPrs))
119 // 2. set the selection to sketch
120 setSelectionCustom(aPrs);
121 // 3. hide main planes if they have been displayed
122 erasePreviewPlanes();
123 // 4. if the planes were displayed, change the view projection
124 TopoDS_Shape aShape = aPrs.shape();
125 std::shared_ptr<GeomAPI_Shape> aGShape;
126 // selection happens in OCC viewer
127 if (!aShape.IsNull()) {
128 aGShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
129 aGShape->setImpl(new TopoDS_Shape(aShape));
131 else { // selection happens in OCC viewer(on body) of in the OB browser
132 DataPtr aData = feature()->data();
133 AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
134 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
136 aGShape = aSelAttr->value();
139 if (aGShape.get() != NULL) {
140 // get plane parameters
141 std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
142 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
144 myWorkshop->viewer()->setViewProjection(aDir->x(), aDir->y(), aDir->z());
146 // 5. Clear text in the label
147 myLabel->setText("");
148 myLabel->setToolTip("");
149 disconnect(myWorkshop->selector(), SIGNAL(selectionChanged()),
150 this, SLOT(onSelectionChanged()));
151 // 6. deactivate face selection filter
152 activateFilters(myWorkshop->module()->workshop(), false);
154 // 7. Clear selection mode and define sketching mode
155 //XGUI_Displayer* aDisp = myWorkshop->displayer();
156 //aDisp->closeLocalContexts();
157 emit planeSelected(plane());
158 //setSketchingMode();
160 // 8. Update sketcher actions
161 XGUI_ActionsMgr* anActMgr = myWorkshop->actionsMgr();
163 myWorkshop->viewer()->update();
166 std::shared_ptr<GeomAPI_Pln> PartSet_WidgetSketchLabel::plane() const
168 CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
169 return PartSet_Tools::sketchPlane(aSketch);
173 bool PartSet_WidgetSketchLabel::focusTo()
179 void PartSet_WidgetSketchLabel::enableFocusProcessing()
181 myLabel->installEventFilter(this);
184 void PartSet_WidgetSketchLabel::storeAttributeValue()
188 void PartSet_WidgetSketchLabel::restoreAttributeValue(const bool theValid)
190 // it is not necessary to save the previous plane value because the plane is chosen once
191 DataPtr aData = feature()->data();
192 AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
193 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
195 ResultPtr anEmptyResult;
196 GeomShapePtr anEmptyShape;
197 aSelAttr->setValue(anEmptyResult, anEmptyShape);
201 bool PartSet_WidgetSketchLabel::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
203 bool isOwnerSet = false;
205 const TopoDS_Shape& aShape = thePrs.shape();
206 std::shared_ptr<GeomAPI_Dir> aDir;
208 if (thePrs.object() && (feature() != thePrs.object())) {
209 DataPtr aData = feature()->data();
210 AttributeSelectionPtr aSelAttr =
211 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
212 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
214 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(thePrs.object());
216 GeomShapePtr aShapePtr(new GeomAPI_Shape());
217 if (aShape.IsNull()) { // selection happens in the OCC viewer
218 aShapePtr = ModelAPI_Tools::shape(aRes);
220 else { // selection happens in OB browser
221 aShapePtr->setImpl(new TopoDS_Shape(aShape));
223 if (aShapePtr.get() != NULL) {
224 aSelAttr->setValue(aRes, aShapePtr);
230 else if (!aShape.IsNull()) {
231 aDir = setSketchPlane(aShape);
237 void PartSet_WidgetSketchLabel::activateCustom()
239 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
243 bool aBodyIsVisualized = false;
244 XGUI_Displayer* aDisp = myWorkshop->displayer();
245 QObjectPtrList aDisplayed = aDisp->displayedObjects();
246 foreach (ObjectPtr anObj, aDisplayed) {
247 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
248 if (aResult.get() != NULL) {
249 aBodyIsVisualized = aResult->groupName() == ModelAPI_ResultBody::group();
250 if (aBodyIsVisualized)
255 if (!aBodyIsVisualized) {
256 // We have to select a plane before any operation
260 aModes << TopAbs_FACE;
261 aDisp->activateObjects(aModes);
263 myLabel->setText(myText);
264 myLabel->setToolTip(myTooltip);
266 connect(myWorkshop->selector(), SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
267 activateFilters(myWorkshop->module()->workshop(), true);
269 aDisp->updateViewer();
272 void PartSet_WidgetSketchLabel::deactivate()
274 // Do not set selection mode if the widget was activated for a small moment
275 //mySelectionTimer->stop();
276 //XGUI_Displayer* aDisp = myWorkshop->displayer();
277 //aDisp->closeLocalContexts();
278 erasePreviewPlanes();
279 activateFilters(myWorkshop->module()->workshop(), false);
282 void PartSet_WidgetSketchLabel::erasePreviewPlanes()
284 if (myPreviewDisplayed) {
285 XGUI_Displayer* aDisp = myWorkshop->displayer();
286 aDisp->eraseAIS(myYZPlane, false);
287 aDisp->eraseAIS(myXZPlane, false);
288 aDisp->eraseAIS(myXYPlane, false);
289 myPreviewDisplayed = false;
293 void PartSet_WidgetSketchLabel::showPreviewPlanes()
295 if (myPreviewDisplayed)
298 if (!myYZPlane) { // If planes are not created
300 std::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
301 std::shared_ptr<GeomAPI_Dir> aYZDir(new GeomAPI_Dir(1, 0, 0));
302 std::shared_ptr<GeomAPI_Dir> aXZDir(new GeomAPI_Dir(0, 1, 0));
303 std::shared_ptr<GeomAPI_Dir> aXYDir(new GeomAPI_Dir(0, 0, 1));
305 std::vector<int> aYZRGB, aXZRGB, aXYRGB;
306 aYZRGB = Config_PropManager::color("Visualization", "yz_plane_color",
308 aXZRGB = Config_PropManager::color("Visualization", "xz_plane_color",
310 aXYRGB = Config_PropManager::color("Visualization", "xy_plane_color",
312 int aR[] = {aYZRGB[0], aYZRGB[1], aYZRGB[2]};
313 int aG[] = {aXZRGB[0], aXZRGB[1], aXZRGB[2]};
314 int aB[] = {aXYRGB[0], aXYRGB[1], aXYRGB[2]};
316 myYZPlane = createPreviewPlane(anOrigin, aYZDir, aR);
317 myXZPlane = createPreviewPlane(anOrigin, aXZDir, aG);
318 myXYPlane = createPreviewPlane(anOrigin, aXYDir, aB);
320 XGUI_Displayer* aDisp = myWorkshop->displayer();
321 aDisp->displayAIS(myYZPlane, false);
322 aDisp->displayAIS(myXZPlane, false);
323 aDisp->displayAIS(myXYPlane, false);
324 myPreviewDisplayed = true;
328 AISObjectPtr PartSet_WidgetSketchLabel::createPreviewPlane(std::shared_ptr<GeomAPI_Pnt> theOrigin,
329 std::shared_ptr<GeomAPI_Dir> theNorm,
332 double aSize = Config_PropManager::integer("Sketch planes", "planes_size", PLANE_SIZE);
333 std::shared_ptr<GeomAPI_Shape> aFace = GeomAlgoAPI_FaceBuilder::square(theOrigin, theNorm, aSize);
334 AISObjectPtr aAIS = AISObjectPtr(new GeomAPI_AISObject());
335 aAIS->createShape(aFace);
336 aAIS->setWidth(Config_PropManager::integer("Sketch planes", "planes_thickness", SKETCH_WIDTH));
337 aAIS->setColor(theRGB[0], theRGB[1], theRGB[2]);
342 std::shared_ptr<GeomAPI_Dir> PartSet_WidgetSketchLabel::setSketchPlane(const TopoDS_Shape& theShape)
344 if (theShape.IsNull())
345 return std::shared_ptr<GeomAPI_Dir>();
347 // get selected shape
348 std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
349 aGShape->setImpl(new TopoDS_Shape(theShape));
351 // get plane parameters
352 std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
354 // set plane parameters to feature
355 std::shared_ptr<ModelAPI_Data> aData = feature()->data();
356 double anA, aB, aC, aD;
357 aPlane->coefficients(anA, aB, aC, aD);
359 // calculate attributes of the sketch
360 std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
361 std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
362 std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
363 aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
364 std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
365 // X axis is preferable to be dirX on the sketch
366 const double tol = Precision::Confusion();
367 bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
368 std::shared_ptr<GeomAPI_Dir> aTempDir(
369 isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
370 std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
371 std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
373 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
374 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
375 anOrigin->setValue(anOrigPnt);
376 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
377 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
378 aNormal->setValue(aNormDir);
379 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
380 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
381 aDirX->setValue(aXDir);
382 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
387 /*void PartSet_WidgetSketchLabel::setSketchingMode()
389 XGUI_Displayer* aDisp = myWorkshop->displayer();
390 // Clear standard selection modes if they are defined
391 //aDisp->activateObjects(aModes);
392 //aDisp->openLocalContext();
394 // Get default selection modes
397 aModes.append(SketcherPrs_Tools::Sel_Dimension_Text);
398 aModes.append(SketcherPrs_Tools::Sel_Dimension_Line);
399 aModes.append(SketcherPrs_Tools::Sel_Constraint);
400 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_VERTEX));
401 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_EDGE));
403 aDisp->activateObjects(aModes);
406 void PartSet_WidgetSketchLabel::showConstraints(bool theOn)
408 myShowConstraints->setChecked(theOn);
409 emit showConstraintToggled(theOn);