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>
18 #include <XGUI_ModuleConnector.h>
20 #include <ModuleBase_Operation.h>
21 #include <ModuleBase_ViewerPrs.h>
22 #include <ModuleBase_Tools.h>
23 #include <ModuleBase_IModule.h>
25 #include <ModelAPI_ResultBody.h>
26 #include <ModelAPI_Tools.h>
28 #include <GeomAlgoAPI_FaceBuilder.h>
29 #include <GeomDataAPI_Point.h>
30 #include <GeomDataAPI_Dir.h>
31 #include <GeomAPI_XYZ.h>
33 #include <SketchPlugin_Sketch.h>
34 #include <SketcherPrs_Tools.h>
36 #include <Precision.hxx>
40 #include <AIS_Shape.hxx>
41 #include <AIS_DimensionSelectionMode.hxx>
43 #include <Config_WidgetAPI.h>
44 #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 QVBoxLayout* aLayout = new QVBoxLayout(this);
68 ModuleBase_Tools::zeroMargins(aLayout);
69 aLayout->addWidget(myLabel);
71 myShowConstraints = new QCheckBox(tr("Show constraints"), this);
72 aLayout->addWidget(myShowConstraints);
75 connect(myShowConstraints, SIGNAL(toggled(bool)), this, SIGNAL(showConstraintToggled(bool)));
76 myShowConstraints->setChecked(toShowConstraints);
79 PartSet_WidgetSketchLabel::~PartSet_WidgetSketchLabel()
84 bool PartSet_WidgetSketchLabel::setSelection(QList<ModuleBase_ViewerPrs>& theValues)
86 // do not use the given selection if the plane of the sketch has been already set.
87 // If this check is absent, a selected plane in the viewer can be set in the sketch
88 // even if the sketch is built on another plane.
92 return ModuleBase_WidgetValidated::setSelection(theValues);
95 QList<QWidget*> PartSet_WidgetSketchLabel::getControls() const
97 QList<QWidget*> aResult;
102 void PartSet_WidgetSketchLabel::onSelectionChanged()
104 QList<ModuleBase_ViewerPrs> aSelectedPrs = myWorkshop->selector()->selection()->getSelected(
105 ModuleBase_ISelection::AllControls);
106 if (aSelectedPrs.empty())
108 ModuleBase_ViewerPrs aPrs = aSelectedPrs.first();
109 if (aPrs.isEmpty() || !isValidSelection(aPrs))
112 // 2. set the selection to sketch
113 setSelectionCustom(aPrs);
114 // 3. hide main planes if they have been displayed
115 erasePreviewPlanes();
116 // 4. if the planes were displayed, change the view projection
117 TopoDS_Shape aShape = aPrs.shape();
118 std::shared_ptr<GeomAPI_Shape> aGShape;
119 // selection happens in OCC viewer
120 if (!aShape.IsNull()) {
121 aGShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
122 aGShape->setImpl(new TopoDS_Shape(aShape));
124 else { // selection happens in OCC viewer(on body) of in the OB browser
125 DataPtr aData = feature()->data();
126 AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
127 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
129 aGShape = aSelAttr->value();
132 if (aGShape.get() != NULL) {
133 // get plane parameters
134 std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
135 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
137 myWorkshop->viewer()->setViewProjection(aDir->x(), aDir->y(), aDir->z());
139 // 5. Clear text in the label
140 myLabel->setText("");
141 myLabel->setToolTip("");
142 disconnect(myWorkshop->selector(), SIGNAL(selectionChanged()),
143 this, SLOT(onSelectionChanged()));
144 // 6. deactivate face selection filter
145 activateFilters(myWorkshop->module()->workshop(), false);
147 // 7. Clear selection mode and define sketching mode
148 //XGUI_Displayer* aDisp = myWorkshop->displayer();
149 //aDisp->closeLocalContexts();
150 emit planeSelected(plane());
151 // after the plane is selected in the sketch, the sketch selection should be activated
152 // it can not be performed in the sketch label widget because, we don't need to switch off
153 // the selection by any label deactivation, but need to switch it off by stop the sketch
154 activateSelection(true);
156 // 8. Update sketcher actions
157 XGUI_ActionsMgr* anActMgr = myWorkshop->actionsMgr();
159 myWorkshop->viewer()->update();
162 std::shared_ptr<GeomAPI_Pln> PartSet_WidgetSketchLabel::plane() const
164 CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
165 return PartSet_Tools::sketchPlane(aSketch);
169 bool PartSet_WidgetSketchLabel::focusTo()
175 void PartSet_WidgetSketchLabel::enableFocusProcessing()
177 myLabel->installEventFilter(this);
180 void PartSet_WidgetSketchLabel::storeAttributeValue()
184 void PartSet_WidgetSketchLabel::restoreAttributeValue(const bool theValid)
186 // it is not necessary to save the previous plane value because the plane is chosen once
187 DataPtr aData = feature()->data();
188 AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
189 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
191 ResultPtr anEmptyResult;
192 GeomShapePtr anEmptyShape;
193 aSelAttr->setValue(anEmptyResult, anEmptyShape);
197 bool PartSet_WidgetSketchLabel::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
199 bool isOwnerSet = false;
201 const TopoDS_Shape& aShape = thePrs.shape();
202 std::shared_ptr<GeomAPI_Dir> aDir;
204 if (thePrs.object() && (feature() != thePrs.object())) {
205 DataPtr aData = feature()->data();
206 AttributeSelectionPtr aSelAttr =
207 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
208 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
210 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(thePrs.object());
212 GeomShapePtr aShapePtr(new GeomAPI_Shape());
213 if (aShape.IsNull()) { // selection happens in the OCC viewer
214 aShapePtr = ModelAPI_Tools::shape(aRes);
216 else { // selection happens in OB browser
217 aShapePtr->setImpl(new TopoDS_Shape(aShape));
219 if (aShapePtr.get() != NULL) {
220 aSelAttr->setValue(aRes, aShapePtr);
226 else if (!aShape.IsNull()) {
227 aDir = setSketchPlane(aShape);
228 isOwnerSet = aDir.get();
233 void PartSet_WidgetSketchLabel::activateCustom()
235 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
237 activateSelection(true);
241 bool aBodyIsVisualized = false;
242 XGUI_Displayer* aDisp = myWorkshop->displayer();
243 QObjectPtrList aDisplayed = aDisp->displayedObjects();
244 foreach (ObjectPtr anObj, aDisplayed) {
245 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
246 if (aResult.get() != NULL) {
247 aBodyIsVisualized = aResult->groupName() == ModelAPI_ResultBody::group();
248 if (aBodyIsVisualized)
253 if (!aBodyIsVisualized) {
254 // We have to select a plane before any operation
257 activateSelection(true);
259 myLabel->setText(myText);
260 myLabel->setToolTip(myTooltip);
262 connect(myWorkshop->selector(), SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
263 activateFilters(myWorkshop->module()->workshop(), true);
265 aDisp->updateViewer();
268 void PartSet_WidgetSketchLabel::deactivate()
270 erasePreviewPlanes();
271 activateSelection(false);
273 activateFilters(myWorkshop->module()->workshop(), false);
276 void PartSet_WidgetSketchLabel::activateSelection(bool toActivate)
280 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
282 myWorkshop->moduleConnector()->module()->activeSelectionModes(aModes);
285 aModes << TopAbs_FACE;
287 myWorkshop->moduleConnector()->activateSubShapesSelection(aModes);
289 myWorkshop->moduleConnector()->deactivateSubShapesSelection();
293 void PartSet_WidgetSketchLabel::erasePreviewPlanes()
295 if (myPreviewDisplayed) {
296 XGUI_Displayer* aDisp = myWorkshop->displayer();
297 aDisp->eraseAIS(myYZPlane, false);
298 aDisp->eraseAIS(myXZPlane, false);
299 aDisp->eraseAIS(myXYPlane, false);
300 myPreviewDisplayed = false;
304 void PartSet_WidgetSketchLabel::showPreviewPlanes()
306 if (myPreviewDisplayed)
309 if (!myYZPlane) { // If planes are not created
311 std::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
312 std::shared_ptr<GeomAPI_Dir> aYZDir(new GeomAPI_Dir(1, 0, 0));
313 std::shared_ptr<GeomAPI_Dir> aXZDir(new GeomAPI_Dir(0, 1, 0));
314 std::shared_ptr<GeomAPI_Dir> aXYDir(new GeomAPI_Dir(0, 0, 1));
316 std::vector<int> aYZRGB, aXZRGB, aXYRGB;
317 aYZRGB = Config_PropManager::color("Visualization", "yz_plane_color",
319 aXZRGB = Config_PropManager::color("Visualization", "xz_plane_color",
321 aXYRGB = Config_PropManager::color("Visualization", "xy_plane_color",
323 int aR[] = {aYZRGB[0], aYZRGB[1], aYZRGB[2]};
324 int aG[] = {aXZRGB[0], aXZRGB[1], aXZRGB[2]};
325 int aB[] = {aXYRGB[0], aXYRGB[1], aXYRGB[2]};
327 myYZPlane = createPreviewPlane(anOrigin, aYZDir, aR);
328 myXZPlane = createPreviewPlane(anOrigin, aXZDir, aG);
329 myXYPlane = createPreviewPlane(anOrigin, aXYDir, aB);
331 XGUI_Displayer* aDisp = myWorkshop->displayer();
332 aDisp->displayAIS(myYZPlane, false);
333 aDisp->displayAIS(myXZPlane, false);
334 aDisp->displayAIS(myXYPlane, false);
335 myPreviewDisplayed = true;
339 AISObjectPtr PartSet_WidgetSketchLabel::createPreviewPlane(std::shared_ptr<GeomAPI_Pnt> theOrigin,
340 std::shared_ptr<GeomAPI_Dir> theNorm,
343 double aSize = Config_PropManager::integer("Sketch planes", "planes_size", PLANE_SIZE);
344 std::shared_ptr<GeomAPI_Shape> aFace = GeomAlgoAPI_FaceBuilder::square(theOrigin, theNorm, aSize);
345 AISObjectPtr aAIS = AISObjectPtr(new GeomAPI_AISObject());
346 aAIS->createShape(aFace);
347 aAIS->setWidth(Config_PropManager::integer("Sketch planes", "planes_thickness", SKETCH_WIDTH));
348 aAIS->setColor(theRGB[0], theRGB[1], theRGB[2]);
353 std::shared_ptr<GeomAPI_Dir> PartSet_WidgetSketchLabel::setSketchPlane(const TopoDS_Shape& theShape)
355 if (theShape.IsNull())
356 return std::shared_ptr<GeomAPI_Dir>();
358 // get selected shape
359 std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
360 aGShape->setImpl(new TopoDS_Shape(theShape));
364 // get plane parameters
365 std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
367 // set plane parameters to feature
368 std::shared_ptr<ModelAPI_Data> aData = feature()->data();
369 double anA, aB, aC, aD;
370 aPlane->coefficients(anA, aB, aC, aD);
372 // calculate attributes of the sketch
373 std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
374 std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
375 std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
376 aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
377 std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
378 // X axis is preferable to be dirX on the sketch
379 const double tol = Precision::Confusion();
380 bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
381 std::shared_ptr<GeomAPI_Dir> aTempDir(
382 isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
383 std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
384 std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
386 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
387 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
388 anOrigin->setValue(anOrigPnt);
389 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
390 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
391 aNormal->setValue(aNormDir);
392 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
393 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
394 aDirX->setValue(aXDir);
395 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
399 void PartSet_WidgetSketchLabel::showConstraints(bool theOn)
401 myShowConstraints->setChecked(theOn);
402 emit showConstraintToggled(theOn);