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 const Config_WidgetAPI* theData,
56 const std::string& theParentId,
57 bool toShowConstraints)
58 : ModuleBase_WidgetValidated(theParent, theData, theParentId),
59 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)
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 return ModuleBase_WidgetValidated::setSelection(theValues);
97 QList<QWidget*> PartSet_WidgetSketchLabel::getControls() const
99 QList<QWidget*> aResult;
104 void PartSet_WidgetSketchLabel::onSelectionChanged()
106 QList<ModuleBase_ViewerPrs> aSelected = myWorkshop->selector()->selection()->getSelected(
107 ModuleBase_ISelection::AllControls);
108 if (aSelected.empty())
110 ModuleBase_ViewerPrs aPrs = aSelected.first();
112 bool isDone = ModuleBase_WidgetValidated::setSelection(aSelected);
116 // 3. hide main planes if they have been displayed
117 erasePreviewPlanes();
118 // 4. if the planes were displayed, change the view projection
119 TopoDS_Shape aShape = aPrs.shape();
120 std::shared_ptr<GeomAPI_Shape> aGShape;
121 std::shared_ptr<GeomAPI_Shape> aBaseShape;
123 DataPtr aData = feature()->data();
124 AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
125 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
127 // selection happens in OCC viewer
128 if (!aShape.IsNull()) {
129 aGShape = std::make_shared<GeomAPI_Shape>();
130 aGShape->setImpl(new TopoDS_Shape(aShape));
132 if (aSelAttr && aSelAttr->context()) {
133 aBaseShape = aSelAttr->context()->shape();
136 else { // selection happens in OCC viewer(on body) of in the OB browser
138 aGShape = aSelAttr->value();
141 if (aGShape.get() != NULL) {
142 // get plane parameters
143 std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
144 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
145 gp_XYZ aXYZ = aDir->impl<gp_Dir>().XYZ();
147 // orienting projection
148 if(aBaseShape.get() != NULL) {
149 std::shared_ptr<GeomAPI_Pnt> aCenterPnt = GeomAlgoAPI_ShapeProps::centreOfMass(aGShape);
150 gp_Pnt aPnt = aCenterPnt->impl<gp_Pnt>();
151 aPnt.Translate(aDir->impl<gp_Dir>().XYZ() * (10 * Precision::Confusion()));
153 BRepClass3d_SolidClassifier aClassifier;
154 aClassifier.Load(aBaseShape->impl<TopoDS_Shape>());
155 aClassifier.Perform(aPnt, Precision::Confusion());
157 if(aClassifier.State() == TopAbs_IN) {
162 myWorkshop->viewer()->setViewProjection(aXYZ.X(), aXYZ.Y(), aXYZ.Z());
164 // 5. Clear text in the label
165 myLabel->setText("");
166 myLabel->setToolTip("");
167 disconnect(myWorkshop->selector(), SIGNAL(selectionChanged()),
168 this, SLOT(onSelectionChanged()));
169 // 6. deactivate face selection filter
170 activateFilters(myWorkshop->module()->workshop(), false);
172 // 7. Clear selection mode and define sketching mode
173 //XGUI_Displayer* aDisp = myWorkshop->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 // 8. Update sketcher actions
182 XGUI_ActionsMgr* anActMgr = myWorkshop->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);
194 bool PartSet_WidgetSketchLabel::focusTo()
200 void PartSet_WidgetSketchLabel::enableFocusProcessing()
202 myLabel->installEventFilter(this);
205 void PartSet_WidgetSketchLabel::storeAttributeValue()
209 void PartSet_WidgetSketchLabel::restoreAttributeValue(const bool theValid)
211 // it is not necessary to save the previous plane value because the plane is chosen once
212 DataPtr aData = feature()->data();
213 AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
214 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
216 ResultPtr anEmptyResult;
217 GeomShapePtr anEmptyShape;
218 aSelAttr->setValue(anEmptyResult, anEmptyShape);
222 bool PartSet_WidgetSketchLabel::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
224 bool isOwnerSet = false;
226 const TopoDS_Shape& aShape = thePrs.shape();
227 std::shared_ptr<GeomAPI_Dir> aDir;
229 if (thePrs.object() && (feature() != thePrs.object())) {
230 DataPtr aData = feature()->data();
231 AttributeSelectionPtr aSelAttr =
232 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
233 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
235 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(thePrs.object());
237 GeomShapePtr aShapePtr(new GeomAPI_Shape());
238 if (aShape.IsNull()) { // selection happens in the OCC viewer
239 aShapePtr = ModelAPI_Tools::shape(aRes);
241 else { // selection happens in OB browser
242 aShapePtr->setImpl(new TopoDS_Shape(aShape));
244 if (aShapePtr.get() != NULL) {
245 aSelAttr->setValue(aRes, aShapePtr);
251 else if (!aShape.IsNull()) {
252 aDir = setSketchPlane(aShape);
253 isOwnerSet = aDir.get();
258 void PartSet_WidgetSketchLabel::activateCustom()
260 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
262 activateSelection(true);
266 bool aBodyIsVisualized = false;
267 XGUI_Displayer* aDisp = myWorkshop->displayer();
268 QObjectPtrList aDisplayed = aDisp->displayedObjects();
269 foreach (ObjectPtr anObj, aDisplayed) {
270 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
271 if (aResult.get() != NULL) {
272 aBodyIsVisualized = aResult->groupName() == ModelAPI_ResultBody::group();
273 if (aBodyIsVisualized)
278 if (!aBodyIsVisualized) {
279 // We have to select a plane before any operation
282 activateSelection(true);
284 myLabel->setText(myText);
285 myLabel->setToolTip(myTooltip);
287 connect(myWorkshop->selector(), SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
288 activateFilters(myWorkshop->module()->workshop(), true);
290 aDisp->updateViewer();
293 void PartSet_WidgetSketchLabel::deactivate()
295 erasePreviewPlanes();
296 activateSelection(false);
298 activateFilters(myWorkshop->module()->workshop(), false);
301 void PartSet_WidgetSketchLabel::activateSelection(bool toActivate)
305 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
307 myWorkshop->moduleConnector()->module()->activeSelectionModes(aModes);
310 aModes << TopAbs_FACE;
312 myWorkshop->moduleConnector()->activateSubShapesSelection(aModes);
314 myWorkshop->moduleConnector()->deactivateSubShapesSelection();
318 void PartSet_WidgetSketchLabel::erasePreviewPlanes()
320 if (myPreviewDisplayed) {
321 XGUI_Displayer* aDisp = myWorkshop->displayer();
322 aDisp->eraseAIS(myYZPlane, false);
323 aDisp->eraseAIS(myXZPlane, false);
324 aDisp->eraseAIS(myXYPlane, false);
325 myPreviewDisplayed = false;
329 void PartSet_WidgetSketchLabel::showPreviewPlanes()
331 if (myPreviewDisplayed)
334 if (!myYZPlane) { // If planes are not created
336 std::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
337 std::shared_ptr<GeomAPI_Dir> aYZDir(new GeomAPI_Dir(1, 0, 0));
338 std::shared_ptr<GeomAPI_Dir> aXZDir(new GeomAPI_Dir(0, 1, 0));
339 std::shared_ptr<GeomAPI_Dir> aXYDir(new GeomAPI_Dir(0, 0, 1));
341 std::vector<int> aYZRGB, aXZRGB, aXYRGB;
342 aYZRGB = Config_PropManager::color("Visualization", "yz_plane_color",
344 aXZRGB = Config_PropManager::color("Visualization", "xz_plane_color",
346 aXYRGB = Config_PropManager::color("Visualization", "xy_plane_color",
348 int aR[] = {aYZRGB[0], aYZRGB[1], aYZRGB[2]};
349 int aG[] = {aXZRGB[0], aXZRGB[1], aXZRGB[2]};
350 int aB[] = {aXYRGB[0], aXYRGB[1], aXYRGB[2]};
352 myYZPlane = createPreviewPlane(anOrigin, aYZDir, aR);
353 myXZPlane = createPreviewPlane(anOrigin, aXZDir, aG);
354 myXYPlane = createPreviewPlane(anOrigin, aXYDir, aB);
356 XGUI_Displayer* aDisp = myWorkshop->displayer();
357 aDisp->displayAIS(myYZPlane, false);
358 aDisp->displayAIS(myXZPlane, false);
359 aDisp->displayAIS(myXYPlane, false);
360 myPreviewDisplayed = true;
364 AISObjectPtr PartSet_WidgetSketchLabel::createPreviewPlane(std::shared_ptr<GeomAPI_Pnt> theOrigin,
365 std::shared_ptr<GeomAPI_Dir> theNorm,
368 double aSize = Config_PropManager::integer("Sketch planes", "planes_size", PLANE_SIZE);
369 std::shared_ptr<GeomAPI_Shape> aFace = GeomAlgoAPI_FaceBuilder::square(theOrigin, theNorm, aSize);
370 AISObjectPtr aAIS = AISObjectPtr(new GeomAPI_AISObject());
371 aAIS->createShape(aFace);
372 aAIS->setWidth(Config_PropManager::integer("Sketch planes", "planes_thickness", SKETCH_WIDTH));
373 aAIS->setColor(theRGB[0], theRGB[1], theRGB[2]);
378 std::shared_ptr<GeomAPI_Dir> PartSet_WidgetSketchLabel::setSketchPlane(const TopoDS_Shape& theShape)
380 if (theShape.IsNull())
381 return std::shared_ptr<GeomAPI_Dir>();
383 // get selected shape
384 std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
385 aGShape->setImpl(new TopoDS_Shape(theShape));
389 // get plane parameters
390 std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
392 // set plane parameters to feature
393 std::shared_ptr<ModelAPI_Data> aData = feature()->data();
394 double anA, aB, aC, aD;
395 aPlane->coefficients(anA, aB, aC, aD);
397 // calculate attributes of the sketch
398 std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
399 std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
400 std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
401 aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
402 std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
403 // X axis is preferable to be dirX on the sketch
404 const double tol = Precision::Confusion();
405 bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
406 std::shared_ptr<GeomAPI_Dir> aTempDir(
407 isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
408 std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
409 std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
411 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
412 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
413 anOrigin->setValue(anOrigPnt);
414 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
415 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
416 aNormal->setValue(aNormDir);
417 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
418 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
419 aDirX->setValue(aXDir);
420 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
424 void PartSet_WidgetSketchLabel::showConstraints(bool theOn)
426 myShowConstraints->setChecked(theOn);
427 emit showConstraintToggled(theOn);