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 <XGUI_Workshop.h>
11 #include <XGUI_Displayer.h>
12 #include <XGUI_SelectionMgr.h>
13 #include <XGUI_Selection.h>
14 #include <XGUI_ViewerProxy.h>
15 #include <XGUI_ActionsMgr.h>
17 #include <ModuleBase_Operation.h>
18 #include <ModuleBase_ViewerPrs.h>
19 #include <ModuleBase_Tools.h>
21 #include <GeomAlgoAPI_FaceBuilder.h>
22 #include <GeomDataAPI_Point.h>
23 #include <GeomDataAPI_Dir.h>
24 #include <GeomAPI_XYZ.h>
26 #include <SketchPlugin_Sketch.h>
28 #include <Precision.hxx>
32 #include <AIS_Shape.hxx>
33 #include <AIS_DimensionSelectionMode.hxx>
35 #include <Config_WidgetAPI.h>
36 #include <Config_PropManager.h>
40 #include <QApplication>
41 #include <QVBoxLayout>
44 PartSet_WidgetSketchLabel::PartSet_WidgetSketchLabel(QWidget* theParent,
45 const Config_WidgetAPI* theData,
46 const std::string& theParentId)
47 : ModuleBase_ModelWidget(theParent, theData, theParentId),
48 myPreviewDisplayed(false),
51 myText = QString::fromStdString(theData->getProperty("title"));
52 myLabel = new QLabel("", theParent);
53 myLabel->setWordWrap(true);
54 myTooltip = QString::fromStdString(theData->getProperty("tooltip"));
55 myLabel->setToolTip("");
56 myLabel->setIndent(5);
58 mySelectionTimer = new QTimer(this);
59 connect(mySelectionTimer, SIGNAL(timeout()), SLOT(setSketchingMode()));
60 mySelectionTimer->setSingleShot(true);
62 QVBoxLayout* aLayout = new QVBoxLayout(this);
63 ModuleBase_Tools::zeroMargins(aLayout);
64 aLayout->addWidget(myLabel);
68 PartSet_WidgetSketchLabel::~PartSet_WidgetSketchLabel()
73 QList<QWidget*> PartSet_WidgetSketchLabel::getControls() const
75 return QList<QWidget*>();
78 void PartSet_WidgetSketchLabel::onPlaneSelected()
80 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
81 QList<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
82 if (!aSelected.empty()) {
83 ModuleBase_ViewerPrs aPrs = aSelected.first();
84 TopoDS_Shape aShape = aPrs.shape();
85 if (!aShape.IsNull()) {
86 std::shared_ptr<GeomAPI_Dir> aDir = setSketchPlane(aShape);
90 if (aPrs.object() && (feature() != aPrs.object())) {
91 DataPtr aData = feature()->data();
92 AttributeSelectionPtr aSelAttr =
93 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
94 (aData->attribute(SketchPlugin_Feature::EXTERNAL_ID()));
96 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs.object());
98 GeomShapePtr aShapePtr(new GeomAPI_Shape());
99 aShapePtr->setImpl(new TopoDS_Shape(aShape));
100 aSelAttr->setValue(aRes, aShapePtr);
104 myWorkshop->viewer()->setViewProjection(aDir->x(), aDir->y(), aDir->z());
106 // Clear text in the label
107 myLabel->setText("");
108 myLabel->setToolTip("");
109 disconnect(myWorkshop->selector(), SIGNAL(selectionChanged()),
110 this, SLOT(onPlaneSelected()));
112 // Clear selection mode and define sketching mode
113 XGUI_Displayer* aDisp = myWorkshop->displayer();
114 aDisp->removeSelectionFilter(myFaceFilter);
115 //aDisp->closeLocalContexts();
116 emit planeSelected(plane());
119 // Update sketcher actions
120 XGUI_ActionsMgr* anActMgr = myWorkshop->actionsMgr();
122 myWorkshop->viewer()->update();
128 std::shared_ptr<GeomAPI_Pln> PartSet_WidgetSketchLabel::plane() const
130 CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
131 return PartSet_Tools::sketchPlane(aSketch);
135 void PartSet_WidgetSketchLabel::activateCustom()
137 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
139 //setSketchingMode();
140 // In order to avoid Opening/Closing of context too often
141 mySelectionTimer->start(20);
143 // We have to select a plane before any operation
146 XGUI_Displayer* aDisp = myWorkshop->displayer();
147 //aDisp->openLocalContext();
148 //aDisp->activateObjects(QIntList());
149 if (myFaceFilter.IsNull())
150 myFaceFilter = new StdSelect_FaceFilter(StdSelect_Plane);
151 aDisp->addSelectionFilter(myFaceFilter);
153 aModes << TopAbs_FACE;
154 aDisp->activateObjects(aModes);
156 myLabel->setText(myText);
157 myLabel->setToolTip(myTooltip);
159 connect(myWorkshop->selector(), SIGNAL(selectionChanged()), this, SLOT(onPlaneSelected()));
160 aDisp->updateViewer();
164 void PartSet_WidgetSketchLabel::deactivate()
166 // Do not set selection mode if the widget was activated for a small moment
167 mySelectionTimer->stop();
168 XGUI_Displayer* aDisp = myWorkshop->displayer();
169 aDisp->removeSelectionFilter(myFaceFilter);
170 //aDisp->removeSelectionFilter(mySketchFilter);
171 //aDisp->closeLocalContexts();
172 erasePreviewPlanes();
175 void PartSet_WidgetSketchLabel::erasePreviewPlanes()
177 if (myPreviewDisplayed) {
178 XGUI_Displayer* aDisp = myWorkshop->displayer();
179 aDisp->eraseAIS(myYZPlane, false);
180 aDisp->eraseAIS(myXZPlane, false);
181 aDisp->eraseAIS(myXYPlane, false);
182 myPreviewDisplayed = false;
186 void PartSet_WidgetSketchLabel::showPreviewPlanes()
188 if (myPreviewDisplayed)
191 if (!myYZPlane) { // If planes are not created
193 std::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
194 std::shared_ptr<GeomAPI_Dir> aYZDir(new GeomAPI_Dir(1, 0, 0));
195 std::shared_ptr<GeomAPI_Dir> aXZDir(new GeomAPI_Dir(0, 1, 0));
196 std::shared_ptr<GeomAPI_Dir> aXYDir(new GeomAPI_Dir(0, 0, 1));
198 std::vector<int> aYZRGB, aXZRGB, aXYRGB;
199 aYZRGB = Config_PropManager::color("Visualization", "yz_plane_color",
201 aXZRGB = Config_PropManager::color("Visualization", "xz_plane_color",
203 aXYRGB = Config_PropManager::color("Visualization", "xy_plane_color",
205 int aR[] = {aYZRGB[0], aYZRGB[1], aYZRGB[2]};
206 int aG[] = {aXZRGB[0], aXZRGB[1], aXZRGB[2]};
207 int aB[] = {aXYRGB[0], aXYRGB[1], aXYRGB[2]};
209 myYZPlane = createPreviewPlane(anOrigin, aYZDir, aR);
210 myXZPlane = createPreviewPlane(anOrigin, aXZDir, aG);
211 myXYPlane = createPreviewPlane(anOrigin, aXYDir, aB);
213 XGUI_Displayer* aDisp = myWorkshop->displayer();
214 aDisp->displayAIS(myYZPlane, false);
215 aDisp->displayAIS(myXZPlane, false);
216 aDisp->displayAIS(myXYPlane, false);
217 myPreviewDisplayed = true;
221 AISObjectPtr PartSet_WidgetSketchLabel::createPreviewPlane(std::shared_ptr<GeomAPI_Pnt> theOrigin,
222 std::shared_ptr<GeomAPI_Dir> theNorm,
225 double aSize = Config_PropManager::integer("Sketch planes", "planes_size", PLANE_SIZE);
226 std::shared_ptr<GeomAPI_Shape> aFace = GeomAlgoAPI_FaceBuilder::square(theOrigin, theNorm, aSize);
227 AISObjectPtr aAIS = AISObjectPtr(new GeomAPI_AISObject());
228 aAIS->createShape(aFace);
229 aAIS->setWidth(Config_PropManager::integer("Sketch planes", "planes_thickness", SKETCH_WIDTH));
230 aAIS->setColor(theRGB[0], theRGB[1], theRGB[2]);
235 std::shared_ptr<GeomAPI_Dir> PartSet_WidgetSketchLabel::setSketchPlane(const TopoDS_Shape& theShape)
237 if (theShape.IsNull())
238 return std::shared_ptr<GeomAPI_Dir>();
240 // get selected shape
241 std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
242 aGShape->setImpl(new TopoDS_Shape(theShape));
244 // get plane parameters
245 std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
247 // set plane parameters to feature
248 std::shared_ptr<ModelAPI_Data> aData = feature()->data();
249 double anA, aB, aC, aD;
250 aPlane->coefficients(anA, aB, aC, aD);
252 // calculate attributes of the sketch
253 std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
254 std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
255 std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
256 aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
257 std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
258 // X axis is preferable to be dirX on the sketch
259 const double tol = Precision::Confusion();
260 bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
261 std::shared_ptr<GeomAPI_Dir> aTempDir(
262 isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
263 std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
264 std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
266 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
267 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
268 anOrigin->setValue(anOrigPnt);
269 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
270 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
271 aNormal->setValue(aNormDir);
272 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
273 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
274 aDirX->setValue(aXDir);
275 std::shared_ptr<GeomDataAPI_Dir> aDirY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
276 aData->attribute(SketchPlugin_Sketch::DIRY_ID()));
277 aDirY->setValue(aYDir);
278 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
283 void PartSet_WidgetSketchLabel::setSketchingMode()
285 XGUI_Displayer* aDisp = myWorkshop->displayer();
286 // Clear standard selection modes if they are defined
287 //aDisp->activateObjects(aModes);
288 //aDisp->openLocalContext();
290 // Get default selection modes
292 aModes.append(AIS_DSM_Text);
293 aModes.append(AIS_DSM_Line);
294 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_VERTEX));
295 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_EDGE));
297 aDisp->activateObjects(aModes);