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 <GeomAlgoAPI_ShapeTools.h>
30 #include <GeomDataAPI_Point.h>
31 #include <GeomDataAPI_Dir.h>
32 #include <GeomAPI_XYZ.h>
34 #include <SketchPlugin_Sketch.h>
35 #include <SketcherPrs_Tools.h>
37 #include <Precision.hxx>
41 #include <AIS_Shape.hxx>
42 #include <AIS_DimensionSelectionMode.hxx>
44 #include <Config_WidgetAPI.h>
45 #include <Config_PropManager.h>
48 #include <QApplication>
49 #include <QVBoxLayout>
53 PartSet_WidgetSketchLabel::PartSet_WidgetSketchLabel(QWidget* theParent,
54 ModuleBase_IWorkshop* theWorkshop,
55 const Config_WidgetAPI* theData,
56 const std::string& theParentId,
57 bool toShowConstraints)
58 : ModuleBase_WidgetValidated(theParent, theWorkshop, theData, theParentId),
59 myPreviewDisplayed(false)
61 myText = QString::fromStdString(theData->getProperty("title"));
62 myLabel = new QLabel("", theParent);
63 myLabel->setWordWrap(true);
64 myTooltip = QString::fromStdString(theData->getProperty("tooltip"));
65 myLabel->setToolTip("");
66 myLabel->setIndent(5);
68 QVBoxLayout* aLayout = new QVBoxLayout(this);
69 ModuleBase_Tools::zeroMargins(aLayout);
70 aLayout->addWidget(myLabel);
72 myShowConstraints = new QCheckBox(tr("Show constraints"), this);
73 aLayout->addWidget(myShowConstraints);
76 connect(myShowConstraints, SIGNAL(toggled(bool)), this, SIGNAL(showConstraintToggled(bool)));
77 myShowConstraints->setChecked(toShowConstraints);
80 PartSet_WidgetSketchLabel::~PartSet_WidgetSketchLabel()
84 bool PartSet_WidgetSketchLabel::setSelection(QList<ModuleBase_ViewerPrs>& theValues,
85 const bool theToValidate)
87 // do not use the given selection if the plane of the sketch has been already set.
88 // If this check is absent, a selected plane in the viewer can be set in the sketch
89 // even if the sketch is built on another plane.
93 ModuleBase_ViewerPrs aPrs = theValues.first();
94 bool aDone = ModuleBase_WidgetValidated::setSelection(theValues, theToValidate);
96 updateByPlaneSelected(aPrs);
101 QList<QWidget*> PartSet_WidgetSketchLabel::getControls() const
103 QList<QWidget*> aResult;
108 void PartSet_WidgetSketchLabel::onSelectionChanged()
110 QList<ModuleBase_ViewerPrs> aSelected = getFilteredSelected();
112 if (aSelected.empty())
114 ModuleBase_ViewerPrs aPrs = aSelected.first();
116 bool aDone = ModuleBase_WidgetValidated::setSelection(aSelected, false);
118 updateByPlaneSelected(aPrs);
119 updateObject(myFeature);
123 void PartSet_WidgetSketchLabel::updateByPlaneSelected(const ModuleBase_ViewerPrs& thePrs)
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;
132 DataPtr aData = feature()->data();
133 AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
134 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
136 // selection happens in OCC viewer
137 if (!aShape.IsNull()) {
138 aGShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
139 aGShape->setImpl(new TopoDS_Shape(aShape));
141 if (aSelAttr && aSelAttr->context()) {
142 aBaseShape = aSelAttr->context()->shape();
145 else { // selection happens in OCC viewer(on body) of in the OB browser
147 aGShape = aSelAttr->value();
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();
157 // orienting projection is not needed: it is done in GeomAlgoAPI_FaceBuilder::plane
158 /*if (aGShape->impl<TopoDS_Shape>().Orientation() == TopAbs_REVERSED) {
162 myWorkshop->viewer()->setViewProjection(aXYZ.X(), aXYZ.Y(), aXYZ.Z(), aTwist);
164 // 3. Clear text in the label
165 myLabel->setText("");
166 myLabel->setToolTip("");
167 disconnect(workshop()->selector(), SIGNAL(selectionChanged()),
168 this, SLOT(onSelectionChanged()));
169 // 4. deactivate face selection filter
170 activateFilters(false);
172 // 5. Clear selection mode and define sketching mode
173 //XGUI_Displayer* aDisp = workshop()->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);
181 // 6. Update sketcher actions
182 XGUI_ActionsMgr* anActMgr = workshop()->actionsMgr();
184 myWorkshop->viewer()->update();
187 std::shared_ptr<GeomAPI_Pln> PartSet_WidgetSketchLabel::plane() const
189 CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
190 return PartSet_Tools::sketchPlane(aSketch);
193 bool PartSet_WidgetSketchLabel::focusTo()
199 void PartSet_WidgetSketchLabel::enableFocusProcessing()
201 myLabel->installEventFilter(this);
204 void PartSet_WidgetSketchLabel::storeAttributeValue()
206 ModuleBase_WidgetValidated::storeAttributeValue();
209 void PartSet_WidgetSketchLabel::restoreAttributeValue(const bool theValid)
211 ModuleBase_WidgetValidated::restoreAttributeValue(theValid);
213 // it is not necessary to save the previous plane value because the plane is chosen once
214 DataPtr aData = feature()->data();
215 AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
216 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
218 ResultPtr anEmptyResult;
219 GeomShapePtr anEmptyShape;
220 aSelAttr->setValue(anEmptyResult, anEmptyShape);
224 bool PartSet_WidgetSketchLabel::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
226 bool isOwnerSet = false;
228 const TopoDS_Shape& aShape = thePrs.shape();
229 std::shared_ptr<GeomAPI_Dir> aDir;
231 if (thePrs.object() && (feature() != thePrs.object())) {
232 DataPtr aData = feature()->data();
233 AttributeSelectionPtr aSelAttr =
234 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
235 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
237 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(thePrs.object());
239 GeomShapePtr aShapePtr(new GeomAPI_Shape());
240 if (aShape.IsNull()) { // selection happens in the OCC viewer
241 aShapePtr = ModelAPI_Tools::shape(aRes);
243 else { // selection happens in OB browser
244 aShapePtr->setImpl(new TopoDS_Shape(aShape));
246 if (aShapePtr.get() != NULL) {
247 aSelAttr->setValue(aRes, aShapePtr);
253 else if (!aShape.IsNull()) {
254 aDir = setSketchPlane(aShape);
255 isOwnerSet = aDir.get();
260 void PartSet_WidgetSketchLabel::activateCustom()
262 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
264 activateSelection(true);
268 bool aBodyIsVisualized = false;
269 XGUI_Displayer* aDisp = workshop()->displayer();
270 QObjectPtrList aDisplayed = aDisp->displayedObjects();
271 foreach (ObjectPtr anObj, aDisplayed) {
272 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
273 if (aResult.get() != NULL) {
274 aBodyIsVisualized = aResult->groupName() == ModelAPI_ResultBody::group();
275 if (aBodyIsVisualized)
280 if (!aBodyIsVisualized) {
281 // We have to select a plane before any operation
284 activateSelection(true);
286 myLabel->setText(myText);
287 myLabel->setToolTip(myTooltip);
289 connect(workshop()->selector(), SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
290 activateFilters(true);
292 aDisp->updateViewer();
295 void PartSet_WidgetSketchLabel::deactivate()
297 erasePreviewPlanes();
298 activateSelection(false);
300 activateFilters(false);
303 void PartSet_WidgetSketchLabel::activateSelection(bool toActivate)
307 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
309 myWorkshop->module()->activeSelectionModes(aModes);
312 aModes << TopAbs_FACE;
314 myWorkshop->activateSubShapesSelection(aModes);
316 myWorkshop->deactivateSubShapesSelection();
320 void PartSet_WidgetSketchLabel::erasePreviewPlanes()
322 if (myPreviewDisplayed) {
323 XGUI_Displayer* aDisp = workshop()->displayer();
324 aDisp->eraseAIS(myYZPlane, false);
325 aDisp->eraseAIS(myXZPlane, false);
326 aDisp->eraseAIS(myXYPlane, false);
327 myPreviewDisplayed = false;
331 void PartSet_WidgetSketchLabel::showPreviewPlanes()
333 if (myPreviewDisplayed)
336 if (!myYZPlane) { // If planes are not created
338 std::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
339 std::shared_ptr<GeomAPI_Dir> aYZDir(new GeomAPI_Dir(1, 0, 0));
340 // -1, not 1 for correct internal sketch coords (issue 898)
341 std::shared_ptr<GeomAPI_Dir> aXZDir(new GeomAPI_Dir(0, -1, 0));
342 std::shared_ptr<GeomAPI_Dir> aXYDir(new GeomAPI_Dir(0, 0, 1));
344 std::vector<int> aYZRGB, aXZRGB, aXYRGB;
345 aYZRGB = Config_PropManager::color("Visualization", "yz_plane_color",
347 aXZRGB = Config_PropManager::color("Visualization", "xz_plane_color",
349 aXYRGB = Config_PropManager::color("Visualization", "xy_plane_color",
351 int aR[] = {aYZRGB[0], aYZRGB[1], aYZRGB[2]};
352 int aG[] = {aXZRGB[0], aXZRGB[1], aXZRGB[2]};
353 int aB[] = {aXYRGB[0], aXYRGB[1], aXYRGB[2]};
355 myYZPlane = createPreviewPlane(anOrigin, aYZDir, aR);
356 myXZPlane = createPreviewPlane(anOrigin, aXZDir, aG);
357 myXYPlane = createPreviewPlane(anOrigin, aXYDir, aB);
359 XGUI_Displayer* aDisp = workshop()->displayer();
360 aDisp->displayAIS(myYZPlane, true, false);
361 aDisp->displayAIS(myXZPlane, true, false);
362 aDisp->displayAIS(myXYPlane, true, false);
363 myPreviewDisplayed = true;
367 AISObjectPtr PartSet_WidgetSketchLabel::createPreviewPlane(std::shared_ptr<GeomAPI_Pnt> theOrigin,
368 std::shared_ptr<GeomAPI_Dir> theNorm,
371 double aSize = Config_PropManager::integer("Sketch planes", "planes_size", PLANE_SIZE);
372 std::shared_ptr<GeomAPI_Shape> aFace = GeomAlgoAPI_FaceBuilder::square(theOrigin, theNorm, aSize);
373 AISObjectPtr aAIS = AISObjectPtr(new GeomAPI_AISObject());
374 aAIS->createShape(aFace);
375 aAIS->setWidth(Config_PropManager::integer("Sketch planes", "planes_thickness", SKETCH_WIDTH));
376 aAIS->setColor(theRGB[0], theRGB[1], theRGB[2]);
381 std::shared_ptr<GeomAPI_Dir> PartSet_WidgetSketchLabel::setSketchPlane(const TopoDS_Shape& theShape)
383 if (theShape.IsNull())
384 return std::shared_ptr<GeomAPI_Dir>();
386 // get selected shape
387 std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
388 aGShape->setImpl(new TopoDS_Shape(theShape));
392 // get plane parameters
393 std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
395 // set plane parameters to feature
396 std::shared_ptr<ModelAPI_Data> aData = feature()->data();
397 double anA, aB, aC, aD;
398 aPlane->coefficients(anA, aB, aC, aD);
400 // calculate attributes of the sketch
401 std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
402 std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
403 std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
404 aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
405 std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
406 // X axis is preferable to be dirX on the sketch
407 const double tol = Precision::Confusion();
408 bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
409 std::shared_ptr<GeomAPI_Dir> aTempDir(
410 isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
411 std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
412 std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
414 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
415 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
416 anOrigin->setValue(anOrigPnt);
417 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
418 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
419 aNormal->setValue(aNormDir);
420 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
421 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
422 aDirX->setValue(aXDir);
423 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
427 void PartSet_WidgetSketchLabel::showConstraints(bool theOn)
429 myShowConstraints->setChecked(theOn);
430 emit showConstraintToggled(theOn);
433 XGUI_Workshop* PartSet_WidgetSketchLabel::workshop() const
435 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
436 return aConnector->workshop();