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 <BRepClass3d_SolidClassifier.hxx>
38 #include <Precision.hxx>
42 #include <AIS_Shape.hxx>
43 #include <AIS_DimensionSelectionMode.hxx>
45 #include <Config_WidgetAPI.h>
46 #include <Config_PropManager.h>
49 #include <QApplication>
50 #include <QVBoxLayout>
54 PartSet_WidgetSketchLabel::PartSet_WidgetSketchLabel(QWidget* theParent,
55 ModuleBase_IWorkshop* theWorkshop,
56 const Config_WidgetAPI* theData,
57 const std::string& theParentId,
58 bool toShowConstraints)
59 : ModuleBase_WidgetValidated(theParent, theWorkshop, theData, theParentId),
60 myPreviewDisplayed(false)
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);
69 QVBoxLayout* aLayout = new QVBoxLayout(this);
70 ModuleBase_Tools::zeroMargins(aLayout);
71 aLayout->addWidget(myLabel);
73 myShowConstraints = new QCheckBox(tr("Show constraints"), this);
74 aLayout->addWidget(myShowConstraints);
77 connect(myShowConstraints, SIGNAL(toggled(bool)), this, SIGNAL(showConstraintToggled(bool)));
78 myShowConstraints->setChecked(toShowConstraints);
81 PartSet_WidgetSketchLabel::~PartSet_WidgetSketchLabel()
85 bool PartSet_WidgetSketchLabel::setSelection(QList<ModuleBase_ViewerPrs>& theValues,
86 const bool theToValidate)
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.
94 ModuleBase_ViewerPrs aPrs = theValues.first();
95 bool aDone = ModuleBase_WidgetValidated::setSelection(theValues, theToValidate);
97 updateByPlaneSelected(aPrs);
102 QList<QWidget*> PartSet_WidgetSketchLabel::getControls() const
104 QList<QWidget*> aResult;
109 void PartSet_WidgetSketchLabel::onSelectionChanged()
111 QList<ModuleBase_ViewerPrs> aSelected = getFilteredSelected();
113 if (aSelected.empty())
115 ModuleBase_ViewerPrs aPrs = aSelected.first();
117 bool aDone = ModuleBase_WidgetValidated::setSelection(aSelected, false);
119 updateByPlaneSelected(aPrs);
120 updateObject(myFeature);
124 void PartSet_WidgetSketchLabel::updateByPlaneSelected(const ModuleBase_ViewerPrs& thePrs)
126 // 1. hide main planes if they have been displayed
127 erasePreviewPlanes();
128 // 2. if the planes were displayed, change the view projection
129 TopoDS_Shape aShape = thePrs.shape();
130 std::shared_ptr<GeomAPI_Shape> aGShape;
131 std::shared_ptr<GeomAPI_Shape> aBaseShape;
133 DataPtr aData = feature()->data();
134 AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
135 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
137 // selection happens in OCC viewer
138 if (!aShape.IsNull()) {
139 aGShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
140 aGShape->setImpl(new TopoDS_Shape(aShape));
142 if (aSelAttr && aSelAttr->context()) {
143 aBaseShape = aSelAttr->context()->shape();
146 else { // selection happens in OCC viewer(on body) of in the OB browser
148 aGShape = aSelAttr->value();
151 if (aGShape.get() != NULL) {
152 // get plane parameters
153 std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
154 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
155 gp_XYZ aXYZ = aDir->impl<gp_Dir>().XYZ();
158 // orienting projection
159 if(aBaseShape.get() != NULL) {
160 std::shared_ptr<GeomAPI_Pnt> aCenterPnt = GeomAlgoAPI_ShapeTools::centreOfMass(aGShape);
161 gp_Pnt aPnt = aCenterPnt->impl<gp_Pnt>();
162 aPnt.Translate(aDir->impl<gp_Dir>().XYZ() * (10 * Precision::Confusion()));
164 BRepClass3d_SolidClassifier aClassifier;
165 aClassifier.Load(aBaseShape->impl<TopoDS_Shape>());
166 aClassifier.Perform(aPnt, Precision::Confusion());
168 if(aClassifier.State() == TopAbs_IN) {
173 myWorkshop->viewer()->setViewProjection(aXYZ.X(), aXYZ.Y(), aXYZ.Z(), aTwist);
175 // 3. Clear text in the label
176 myLabel->setText("");
177 myLabel->setToolTip("");
178 disconnect(workshop()->selector(), SIGNAL(selectionChanged()),
179 this, SLOT(onSelectionChanged()));
180 // 4. deactivate face selection filter
181 activateFilters(false);
183 // 5. Clear selection mode and define sketching mode
184 //XGUI_Displayer* aDisp = workshop()->displayer();
185 //aDisp->closeLocalContexts();
186 emit planeSelected(plane());
187 // after the plane is selected in the sketch, the sketch selection should be activated
188 // it can not be performed in the sketch label widget because, we don't need to switch off
189 // the selection by any label deactivation, but need to switch it off by stop the sketch
190 activateSelection(true);
192 // 6. Update sketcher actions
193 XGUI_ActionsMgr* anActMgr = workshop()->actionsMgr();
195 myWorkshop->viewer()->update();
198 std::shared_ptr<GeomAPI_Pln> PartSet_WidgetSketchLabel::plane() const
200 CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
201 return PartSet_Tools::sketchPlane(aSketch);
204 bool PartSet_WidgetSketchLabel::focusTo()
210 void PartSet_WidgetSketchLabel::enableFocusProcessing()
212 myLabel->installEventFilter(this);
215 void PartSet_WidgetSketchLabel::storeAttributeValue()
217 ModuleBase_WidgetValidated::storeAttributeValue();
220 void PartSet_WidgetSketchLabel::restoreAttributeValue(const bool theValid)
222 ModuleBase_WidgetValidated::restoreAttributeValue(theValid);
224 // it is not necessary to save the previous plane value because the plane is chosen once
225 DataPtr aData = feature()->data();
226 AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
227 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
229 ResultPtr anEmptyResult;
230 GeomShapePtr anEmptyShape;
231 aSelAttr->setValue(anEmptyResult, anEmptyShape);
235 bool PartSet_WidgetSketchLabel::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
237 bool isOwnerSet = false;
239 const TopoDS_Shape& aShape = thePrs.shape();
240 std::shared_ptr<GeomAPI_Dir> aDir;
242 if (thePrs.object() && (feature() != thePrs.object())) {
243 DataPtr aData = feature()->data();
244 AttributeSelectionPtr aSelAttr =
245 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
246 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
248 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(thePrs.object());
250 GeomShapePtr aShapePtr(new GeomAPI_Shape());
251 if (aShape.IsNull()) { // selection happens in the OCC viewer
252 aShapePtr = ModelAPI_Tools::shape(aRes);
254 else { // selection happens in OB browser
255 aShapePtr->setImpl(new TopoDS_Shape(aShape));
257 if (aShapePtr.get() != NULL) {
258 aSelAttr->setValue(aRes, aShapePtr);
264 else if (!aShape.IsNull()) {
265 aDir = setSketchPlane(aShape);
266 isOwnerSet = aDir.get();
271 void PartSet_WidgetSketchLabel::activateCustom()
273 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
275 activateSelection(true);
279 bool aBodyIsVisualized = false;
280 XGUI_Displayer* aDisp = workshop()->displayer();
281 QObjectPtrList aDisplayed = aDisp->displayedObjects();
282 foreach (ObjectPtr anObj, aDisplayed) {
283 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
284 if (aResult.get() != NULL) {
285 aBodyIsVisualized = aResult->groupName() == ModelAPI_ResultBody::group();
286 if (aBodyIsVisualized)
291 if (!aBodyIsVisualized) {
292 // We have to select a plane before any operation
295 activateSelection(true);
297 myLabel->setText(myText);
298 myLabel->setToolTip(myTooltip);
300 connect(workshop()->selector(), SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
301 activateFilters(true);
303 aDisp->updateViewer();
306 void PartSet_WidgetSketchLabel::deactivate()
308 erasePreviewPlanes();
309 activateSelection(false);
311 activateFilters(false);
314 void PartSet_WidgetSketchLabel::activateSelection(bool toActivate)
318 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
320 myWorkshop->module()->activeSelectionModes(aModes);
323 aModes << TopAbs_FACE;
325 myWorkshop->activateSubShapesSelection(aModes);
327 myWorkshop->deactivateSubShapesSelection();
331 void PartSet_WidgetSketchLabel::erasePreviewPlanes()
333 if (myPreviewDisplayed) {
334 XGUI_Displayer* aDisp = workshop()->displayer();
335 aDisp->eraseAIS(myYZPlane, false);
336 aDisp->eraseAIS(myXZPlane, false);
337 aDisp->eraseAIS(myXYPlane, false);
338 myPreviewDisplayed = false;
342 void PartSet_WidgetSketchLabel::showPreviewPlanes()
344 if (myPreviewDisplayed)
347 if (!myYZPlane) { // If planes are not created
349 std::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
350 std::shared_ptr<GeomAPI_Dir> aYZDir(new GeomAPI_Dir(1, 0, 0));
351 std::shared_ptr<GeomAPI_Dir> aXZDir(new GeomAPI_Dir(0, 1, 0));
352 std::shared_ptr<GeomAPI_Dir> aXYDir(new GeomAPI_Dir(0, 0, 1));
354 std::vector<int> aYZRGB, aXZRGB, aXYRGB;
355 aYZRGB = Config_PropManager::color("Visualization", "yz_plane_color",
357 aXZRGB = Config_PropManager::color("Visualization", "xz_plane_color",
359 aXYRGB = Config_PropManager::color("Visualization", "xy_plane_color",
361 int aR[] = {aYZRGB[0], aYZRGB[1], aYZRGB[2]};
362 int aG[] = {aXZRGB[0], aXZRGB[1], aXZRGB[2]};
363 int aB[] = {aXYRGB[0], aXYRGB[1], aXYRGB[2]};
365 myYZPlane = createPreviewPlane(anOrigin, aYZDir, aR);
366 myXZPlane = createPreviewPlane(anOrigin, aXZDir, aG);
367 myXYPlane = createPreviewPlane(anOrigin, aXYDir, aB);
369 XGUI_Displayer* aDisp = workshop()->displayer();
370 aDisp->displayAIS(myYZPlane, true, false);
371 aDisp->displayAIS(myXZPlane, true, false);
372 aDisp->displayAIS(myXYPlane, true, false);
373 myPreviewDisplayed = true;
377 AISObjectPtr PartSet_WidgetSketchLabel::createPreviewPlane(std::shared_ptr<GeomAPI_Pnt> theOrigin,
378 std::shared_ptr<GeomAPI_Dir> theNorm,
381 double aSize = Config_PropManager::integer("Sketch planes", "planes_size", PLANE_SIZE);
382 std::shared_ptr<GeomAPI_Shape> aFace = GeomAlgoAPI_FaceBuilder::square(theOrigin, theNorm, aSize);
383 AISObjectPtr aAIS = AISObjectPtr(new GeomAPI_AISObject());
384 aAIS->createShape(aFace);
385 aAIS->setWidth(Config_PropManager::integer("Sketch planes", "planes_thickness", SKETCH_WIDTH));
386 aAIS->setColor(theRGB[0], theRGB[1], theRGB[2]);
391 std::shared_ptr<GeomAPI_Dir> PartSet_WidgetSketchLabel::setSketchPlane(const TopoDS_Shape& theShape)
393 if (theShape.IsNull())
394 return std::shared_ptr<GeomAPI_Dir>();
396 // get selected shape
397 std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
398 aGShape->setImpl(new TopoDS_Shape(theShape));
402 // get plane parameters
403 std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
405 // set plane parameters to feature
406 std::shared_ptr<ModelAPI_Data> aData = feature()->data();
407 double anA, aB, aC, aD;
408 aPlane->coefficients(anA, aB, aC, aD);
410 // calculate attributes of the sketch
411 std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
412 std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
413 std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
414 aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
415 std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
416 // X axis is preferable to be dirX on the sketch
417 const double tol = Precision::Confusion();
418 bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
419 std::shared_ptr<GeomAPI_Dir> aTempDir(
420 isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
421 std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
422 std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
424 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
425 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
426 anOrigin->setValue(anOrigPnt);
427 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
428 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
429 aNormal->setValue(aNormDir);
430 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
431 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
432 aDirX->setValue(aXDir);
433 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
437 void PartSet_WidgetSketchLabel::showConstraints(bool theOn)
439 myShowConstraints->setChecked(theOn);
440 emit showConstraintToggled(theOn);
443 XGUI_Workshop* PartSet_WidgetSketchLabel::workshop() const
445 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
446 return aConnector->workshop();