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 // Rotate view if the sketcher plane is selected only from preview planes
163 // Preview planes are created only if there is no any shape
164 if (myYZPlane.get()) {
165 myWorkshop->viewer()->setViewProjection(aXYZ.X(), aXYZ.Y(), aXYZ.Z(), aTwist);
168 // 3. Clear text in the label
169 myLabel->setText("");
170 myLabel->setToolTip("");
171 disconnect(workshop()->selector(), SIGNAL(selectionChanged()),
172 this, SLOT(onSelectionChanged()));
173 // 4. deactivate face selection filter
174 activateFilters(false);
176 // 5. Clear selection mode and define sketching mode
177 //XGUI_Displayer* aDisp = workshop()->displayer();
178 //aDisp->closeLocalContexts();
179 emit planeSelected(plane());
180 // after the plane is selected in the sketch, the sketch selection should be activated
181 // it can not be performed in the sketch label widget because, we don't need to switch off
182 // the selection by any label deactivation, but need to switch it off by stop the sketch
183 activateSelection(true);
185 // 6. Update sketcher actions
186 XGUI_ActionsMgr* anActMgr = workshop()->actionsMgr();
188 //VSV myWorkshop->viewer()->update();
191 std::shared_ptr<GeomAPI_Pln> PartSet_WidgetSketchLabel::plane() const
193 CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
194 return PartSet_Tools::sketchPlane(aSketch);
197 bool PartSet_WidgetSketchLabel::focusTo()
203 void PartSet_WidgetSketchLabel::enableFocusProcessing()
205 myLabel->installEventFilter(this);
208 void PartSet_WidgetSketchLabel::storeAttributeValue()
210 ModuleBase_WidgetValidated::storeAttributeValue();
213 void PartSet_WidgetSketchLabel::restoreAttributeValue(const bool theValid)
215 ModuleBase_WidgetValidated::restoreAttributeValue(theValid);
217 // it is not necessary to save the previous plane value because the plane is chosen once
218 DataPtr aData = feature()->data();
219 AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
220 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
222 ResultPtr anEmptyResult;
223 GeomShapePtr anEmptyShape;
224 aSelAttr->setValue(anEmptyResult, anEmptyShape);
228 bool PartSet_WidgetSketchLabel::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
230 bool isOwnerSet = false;
232 const TopoDS_Shape& aShape = thePrs.shape();
233 std::shared_ptr<GeomAPI_Dir> aDir;
235 if (thePrs.object() && (feature() != thePrs.object())) {
236 DataPtr aData = feature()->data();
237 AttributeSelectionPtr aSelAttr =
238 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
239 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
241 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(thePrs.object());
243 GeomShapePtr aShapePtr(new GeomAPI_Shape());
244 if (aShape.IsNull()) { // selection happens in the OCC viewer
245 aShapePtr = ModelAPI_Tools::shape(aRes);
247 else { // selection happens in OB browser
248 aShapePtr->setImpl(new TopoDS_Shape(aShape));
250 if (aShapePtr.get() != NULL) {
251 aSelAttr->setValue(aRes, aShapePtr);
257 else if (!aShape.IsNull()) {
258 aDir = setSketchPlane(aShape);
259 isOwnerSet = aDir.get();
264 void PartSet_WidgetSketchLabel::activateCustom()
266 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
268 activateSelection(true);
272 bool aBodyIsVisualized = false;
273 XGUI_Displayer* aDisp = workshop()->displayer();
274 QObjectPtrList aDisplayed = aDisp->displayedObjects();
275 foreach (ObjectPtr anObj, aDisplayed) {
276 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
277 if (aResult.get() != NULL) {
278 aBodyIsVisualized = aResult->groupName() == ModelAPI_ResultBody::group();
279 if (aBodyIsVisualized)
284 if (!aBodyIsVisualized) {
285 // We have to select a plane before any operation
288 activateSelection(true);
290 myLabel->setText(myText);
291 myLabel->setToolTip(myTooltip);
293 connect(workshop()->selector(), SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
294 activateFilters(true);
297 void PartSet_WidgetSketchLabel::deactivate()
299 ModuleBase_ModelWidget::deactivate();
300 erasePreviewPlanes();
301 activateSelection(false);
303 activateFilters(false);
306 void PartSet_WidgetSketchLabel::activateSelection(bool toActivate)
310 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
312 myWorkshop->module()->activeSelectionModes(aModes);
315 aModes << TopAbs_FACE;
317 myWorkshop->activateSubShapesSelection(aModes);
319 myWorkshop->deactivateSubShapesSelection();
323 void PartSet_WidgetSketchLabel::erasePreviewPlanes()
325 if (myPreviewDisplayed) {
326 XGUI_Displayer* aDisp = workshop()->displayer();
327 aDisp->eraseAIS(myYZPlane, false);
328 aDisp->eraseAIS(myXZPlane, false);
329 aDisp->eraseAIS(myXYPlane, false);
330 myPreviewDisplayed = false;
334 void PartSet_WidgetSketchLabel::showPreviewPlanes()
336 if (myPreviewDisplayed)
339 if (!myYZPlane) { // If planes are not created
341 std::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
342 std::shared_ptr<GeomAPI_Dir> aYZDir(new GeomAPI_Dir(1, 0, 0));
343 // -1, not 1 for correct internal sketch coords (issue 898)
344 std::shared_ptr<GeomAPI_Dir> aXZDir(new GeomAPI_Dir(0, -1, 0));
345 std::shared_ptr<GeomAPI_Dir> aXYDir(new GeomAPI_Dir(0, 0, 1));
347 std::vector<int> aYZRGB, aXZRGB, aXYRGB;
348 aYZRGB = Config_PropManager::color("Visualization", "yz_plane_color",
350 aXZRGB = Config_PropManager::color("Visualization", "xz_plane_color",
352 aXYRGB = Config_PropManager::color("Visualization", "xy_plane_color",
354 int aR[] = {aYZRGB[0], aYZRGB[1], aYZRGB[2]};
355 int aG[] = {aXZRGB[0], aXZRGB[1], aXZRGB[2]};
356 int aB[] = {aXYRGB[0], aXYRGB[1], aXYRGB[2]};
358 myYZPlane = createPreviewPlane(anOrigin, aYZDir, aR);
359 myXZPlane = createPreviewPlane(anOrigin, aXZDir, aG);
360 myXYPlane = createPreviewPlane(anOrigin, aXYDir, aB);
362 XGUI_Displayer* aDisp = workshop()->displayer();
363 aDisp->displayAIS(myYZPlane, true, false);
364 aDisp->displayAIS(myXZPlane, true, false);
365 aDisp->displayAIS(myXYPlane, true, false);
366 myPreviewDisplayed = true;
370 AISObjectPtr PartSet_WidgetSketchLabel::createPreviewPlane(std::shared_ptr<GeomAPI_Pnt> theOrigin,
371 std::shared_ptr<GeomAPI_Dir> theNorm,
374 double aSize = Config_PropManager::integer("Sketch planes", "planes_size", PLANE_SIZE);
375 std::shared_ptr<GeomAPI_Shape> aFace = GeomAlgoAPI_FaceBuilder::square(theOrigin, theNorm, aSize);
376 AISObjectPtr aAIS = AISObjectPtr(new GeomAPI_AISObject());
377 aAIS->createShape(aFace);
378 aAIS->setWidth(Config_PropManager::integer("Sketch planes", "planes_thickness", SKETCH_WIDTH));
379 aAIS->setColor(theRGB[0], theRGB[1], theRGB[2]);
384 std::shared_ptr<GeomAPI_Dir> PartSet_WidgetSketchLabel::setSketchPlane(const TopoDS_Shape& theShape)
386 if (theShape.IsNull())
387 return std::shared_ptr<GeomAPI_Dir>();
389 // get selected shape
390 std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
391 aGShape->setImpl(new TopoDS_Shape(theShape));
395 // get plane parameters
396 std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
398 // set plane parameters to feature
399 std::shared_ptr<ModelAPI_Data> aData = feature()->data();
400 double anA, aB, aC, aD;
401 aPlane->coefficients(anA, aB, aC, aD);
403 // calculate attributes of the sketch
404 std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
405 std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
406 std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
407 aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
408 std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
409 // X axis is preferable to be dirX on the sketch
410 const double tol = Precision::Confusion();
411 bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
412 std::shared_ptr<GeomAPI_Dir> aTempDir(
413 isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
414 std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
415 std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
417 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
418 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
419 anOrigin->setValue(anOrigPnt);
420 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
421 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
422 aNormal->setValue(aNormDir);
423 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
424 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
425 aDirX->setValue(aXDir);
426 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
430 void PartSet_WidgetSketchLabel::showConstraints(bool theOn)
432 myShowConstraints->setChecked(theOn);
433 emit showConstraintToggled(theOn);
436 XGUI_Workshop* PartSet_WidgetSketchLabel::workshop() const
438 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
439 return aConnector->workshop();