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_ShapeProps.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()
86 bool PartSet_WidgetSketchLabel::setSelection(QList<ModuleBase_ViewerPrs>& theValues,
87 const bool theToValidate)
89 // do not use the given selection if the plane of the sketch has been already set.
90 // If this check is absent, a selected plane in the viewer can be set in the sketch
91 // even if the sketch is built on another plane.
95 return ModuleBase_WidgetValidated::setSelection(theValues, theToValidate);
98 QList<QWidget*> PartSet_WidgetSketchLabel::getControls() const
100 QList<QWidget*> aResult;
105 void PartSet_WidgetSketchLabel::onSelectionChanged()
107 QList<ModuleBase_ViewerPrs> aSelected = getFilteredSelected();
109 if (aSelected.empty())
111 ModuleBase_ViewerPrs aPrs = aSelected.first();
113 bool isDone = ModuleBase_WidgetValidated::setSelection(aSelected, false);
117 // 3. hide main planes if they have been displayed
118 erasePreviewPlanes();
119 // 4. if the planes were displayed, change the view projection
120 TopoDS_Shape aShape = aPrs.shape();
121 std::shared_ptr<GeomAPI_Shape> aGShape;
122 std::shared_ptr<GeomAPI_Shape> aBaseShape;
124 DataPtr aData = feature()->data();
125 AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
126 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
128 // selection happens in OCC viewer
129 if (!aShape.IsNull()) {
130 aGShape = std::make_shared<GeomAPI_Shape>();
131 aGShape->setImpl(new TopoDS_Shape(aShape));
133 if (aSelAttr && aSelAttr->context()) {
134 aBaseShape = aSelAttr->context()->shape();
137 else { // selection happens in OCC viewer(on body) of in the OB browser
139 aGShape = aSelAttr->value();
142 if (aGShape.get() != NULL) {
143 // get plane parameters
144 std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
145 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
146 gp_XYZ aXYZ = aDir->impl<gp_Dir>().XYZ();
148 // orienting projection
149 if(aBaseShape.get() != NULL) {
150 std::shared_ptr<GeomAPI_Pnt> aCenterPnt = GeomAlgoAPI_ShapeProps::centreOfMass(aGShape);
151 gp_Pnt aPnt = aCenterPnt->impl<gp_Pnt>();
152 aPnt.Translate(aDir->impl<gp_Dir>().XYZ() * (10 * Precision::Confusion()));
154 BRepClass3d_SolidClassifier aClassifier;
155 aClassifier.Load(aBaseShape->impl<TopoDS_Shape>());
156 aClassifier.Perform(aPnt, Precision::Confusion());
158 if(aClassifier.State() == TopAbs_IN) {
163 myWorkshop->viewer()->setViewProjection(aXYZ.X(), aXYZ.Y(), aXYZ.Z());
165 // 5. Clear text in the label
166 myLabel->setText("");
167 myLabel->setToolTip("");
168 disconnect(workshop()->selector(), SIGNAL(selectionChanged()),
169 this, SLOT(onSelectionChanged()));
170 // 6. deactivate face selection filter
171 activateFilters(false);
173 // 7. Clear selection mode and define sketching mode
174 //XGUI_Displayer* aDisp = workshop()->displayer();
175 //aDisp->closeLocalContexts();
176 emit planeSelected(plane());
177 // after the plane is selected in the sketch, the sketch selection should be activated
178 // it can not be performed in the sketch label widget because, we don't need to switch off
179 // the selection by any label deactivation, but need to switch it off by stop the sketch
180 activateSelection(true);
182 // 8. Update sketcher actions
183 XGUI_ActionsMgr* anActMgr = workshop()->actionsMgr();
185 myWorkshop->viewer()->update();
188 std::shared_ptr<GeomAPI_Pln> PartSet_WidgetSketchLabel::plane() const
190 CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
191 return PartSet_Tools::sketchPlane(aSketch);
195 bool PartSet_WidgetSketchLabel::focusTo()
201 void PartSet_WidgetSketchLabel::enableFocusProcessing()
203 myLabel->installEventFilter(this);
206 void PartSet_WidgetSketchLabel::storeAttributeValue()
210 void PartSet_WidgetSketchLabel::restoreAttributeValue(const bool theValid)
212 // it is not necessary to save the previous plane value because the plane is chosen once
213 DataPtr aData = feature()->data();
214 AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
215 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
217 ResultPtr anEmptyResult;
218 GeomShapePtr anEmptyShape;
219 aSelAttr->setValue(anEmptyResult, anEmptyShape);
223 bool PartSet_WidgetSketchLabel::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
225 bool isOwnerSet = false;
227 const TopoDS_Shape& aShape = thePrs.shape();
228 std::shared_ptr<GeomAPI_Dir> aDir;
230 if (thePrs.object() && (feature() != thePrs.object())) {
231 DataPtr aData = feature()->data();
232 AttributeSelectionPtr aSelAttr =
233 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
234 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
236 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(thePrs.object());
238 GeomShapePtr aShapePtr(new GeomAPI_Shape());
239 if (aShape.IsNull()) { // selection happens in the OCC viewer
240 aShapePtr = ModelAPI_Tools::shape(aRes);
242 else { // selection happens in OB browser
243 aShapePtr->setImpl(new TopoDS_Shape(aShape));
245 if (aShapePtr.get() != NULL) {
246 aSelAttr->setValue(aRes, aShapePtr);
252 else if (!aShape.IsNull()) {
253 aDir = setSketchPlane(aShape);
254 isOwnerSet = aDir.get();
259 void PartSet_WidgetSketchLabel::activateCustom()
261 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
263 activateSelection(true);
267 bool aBodyIsVisualized = false;
268 XGUI_Displayer* aDisp = workshop()->displayer();
269 QObjectPtrList aDisplayed = aDisp->displayedObjects();
270 foreach (ObjectPtr anObj, aDisplayed) {
271 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
272 if (aResult.get() != NULL) {
273 aBodyIsVisualized = aResult->groupName() == ModelAPI_ResultBody::group();
274 if (aBodyIsVisualized)
279 if (!aBodyIsVisualized) {
280 // We have to select a plane before any operation
283 activateSelection(true);
285 myLabel->setText(myText);
286 myLabel->setToolTip(myTooltip);
288 connect(workshop()->selector(), SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
289 activateFilters(true);
291 aDisp->updateViewer();
294 void PartSet_WidgetSketchLabel::deactivate()
296 erasePreviewPlanes();
297 activateSelection(false);
299 activateFilters(false);
302 void PartSet_WidgetSketchLabel::activateSelection(bool toActivate)
306 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
308 myWorkshop->module()->activeSelectionModes(aModes);
311 aModes << TopAbs_FACE;
313 myWorkshop->activateSubShapesSelection(aModes);
315 myWorkshop->deactivateSubShapesSelection();
319 void PartSet_WidgetSketchLabel::erasePreviewPlanes()
321 if (myPreviewDisplayed) {
322 XGUI_Displayer* aDisp = workshop()->displayer();
323 aDisp->eraseAIS(myYZPlane, false);
324 aDisp->eraseAIS(myXZPlane, false);
325 aDisp->eraseAIS(myXYPlane, false);
326 myPreviewDisplayed = false;
330 void PartSet_WidgetSketchLabel::showPreviewPlanes()
332 if (myPreviewDisplayed)
335 if (!myYZPlane) { // If planes are not created
337 std::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
338 std::shared_ptr<GeomAPI_Dir> aYZDir(new GeomAPI_Dir(1, 0, 0));
339 std::shared_ptr<GeomAPI_Dir> aXZDir(new GeomAPI_Dir(0, 1, 0));
340 std::shared_ptr<GeomAPI_Dir> aXYDir(new GeomAPI_Dir(0, 0, 1));
342 std::vector<int> aYZRGB, aXZRGB, aXYRGB;
343 aYZRGB = Config_PropManager::color("Visualization", "yz_plane_color",
345 aXZRGB = Config_PropManager::color("Visualization", "xz_plane_color",
347 aXYRGB = Config_PropManager::color("Visualization", "xy_plane_color",
349 int aR[] = {aYZRGB[0], aYZRGB[1], aYZRGB[2]};
350 int aG[] = {aXZRGB[0], aXZRGB[1], aXZRGB[2]};
351 int aB[] = {aXYRGB[0], aXYRGB[1], aXYRGB[2]};
353 myYZPlane = createPreviewPlane(anOrigin, aYZDir, aR);
354 myXZPlane = createPreviewPlane(anOrigin, aXZDir, aG);
355 myXYPlane = createPreviewPlane(anOrigin, aXYDir, aB);
357 XGUI_Displayer* aDisp = workshop()->displayer();
358 aDisp->displayAIS(myYZPlane, false);
359 aDisp->displayAIS(myXZPlane, false);
360 aDisp->displayAIS(myXYPlane, false);
361 myPreviewDisplayed = true;
365 AISObjectPtr PartSet_WidgetSketchLabel::createPreviewPlane(std::shared_ptr<GeomAPI_Pnt> theOrigin,
366 std::shared_ptr<GeomAPI_Dir> theNorm,
369 double aSize = Config_PropManager::integer("Sketch planes", "planes_size", PLANE_SIZE);
370 std::shared_ptr<GeomAPI_Shape> aFace = GeomAlgoAPI_FaceBuilder::square(theOrigin, theNorm, aSize);
371 AISObjectPtr aAIS = AISObjectPtr(new GeomAPI_AISObject());
372 aAIS->createShape(aFace);
373 aAIS->setWidth(Config_PropManager::integer("Sketch planes", "planes_thickness", SKETCH_WIDTH));
374 aAIS->setColor(theRGB[0], theRGB[1], theRGB[2]);
379 std::shared_ptr<GeomAPI_Dir> PartSet_WidgetSketchLabel::setSketchPlane(const TopoDS_Shape& theShape)
381 if (theShape.IsNull())
382 return std::shared_ptr<GeomAPI_Dir>();
384 // get selected shape
385 std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
386 aGShape->setImpl(new TopoDS_Shape(theShape));
390 // get plane parameters
391 std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
393 // set plane parameters to feature
394 std::shared_ptr<ModelAPI_Data> aData = feature()->data();
395 double anA, aB, aC, aD;
396 aPlane->coefficients(anA, aB, aC, aD);
398 // calculate attributes of the sketch
399 std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
400 std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
401 std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
402 aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
403 std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
404 // X axis is preferable to be dirX on the sketch
405 const double tol = Precision::Confusion();
406 bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
407 std::shared_ptr<GeomAPI_Dir> aTempDir(
408 isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
409 std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
410 std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
412 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
413 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
414 anOrigin->setValue(anOrigPnt);
415 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
416 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
417 aNormal->setValue(aNormDir);
418 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
419 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
420 aDirX->setValue(aXDir);
421 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
425 void PartSet_WidgetSketchLabel::showConstraints(bool theOn)
427 myShowConstraints->setChecked(theOn);
428 emit showConstraintToggled(theOn);
431 XGUI_Workshop* PartSet_WidgetSketchLabel::workshop() const
433 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
434 return aConnector->workshop();