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>
20 #include <GeomAlgoAPI_FaceBuilder.h>
21 #include <GeomDataAPI_Point.h>
22 #include <GeomDataAPI_Dir.h>
23 #include <GeomAPI_XYZ.h>
25 #include <SketchPlugin_Sketch.h>
27 #include <Precision.hxx>
31 #include <AIS_Shape.hxx>
32 #include <AIS_DimensionSelectionMode.hxx>
34 #include <Config_WidgetAPI.h>
35 #include <Config_PropManager.h>
39 #define PLANE_SIZE "200"
40 #define SKETCH_WIDTH "4"
43 PartSet_WidgetSketchLabel::PartSet_WidgetSketchLabel(QWidget* theParent,
44 const Config_WidgetAPI* theData,
45 const std::string& theParentId)
46 : ModuleBase_ModelWidget(theParent, theData, theParentId), myPreviewDisplayed(false)
48 myText = QString::fromStdString(theData->getProperty("title"));
49 myLabel = new QLabel("", theParent);
50 myLabel->setWordWrap(true);
51 myTooltip = QString::fromStdString(theData->getProperty("tooltip"));
52 myLabel->setToolTip("");
53 myLabel->setIndent(5);
56 PartSet_WidgetSketchLabel::~PartSet_WidgetSketchLabel()
61 QList<QWidget*> PartSet_WidgetSketchLabel::getControls() const
63 return QList<QWidget*>();
66 QWidget* PartSet_WidgetSketchLabel::getControl() const
71 void PartSet_WidgetSketchLabel::onPlaneSelected()
73 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
74 QList<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
75 if (!aSelected.empty()) {
76 ModuleBase_ViewerPrs aPrs = aSelected.first();
77 TopoDS_Shape aShape = aPrs.shape();
78 if (!aShape.IsNull()) {
79 std::shared_ptr<GeomAPI_Dir> aDir = setSketchPlane(aShape);
83 if (aPrs.object() && (feature() != aPrs.object())) {
84 DataPtr aData = feature()->data();
85 AttributeSelectionPtr aSelAttr =
86 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
87 (aData->attribute(SketchPlugin_Feature::EXTERNAL_ID()));
89 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs.object());
91 GeomShapePtr aShapePtr(new GeomAPI_Shape());
92 aShapePtr->setImpl(new TopoDS_Shape(aShape));
93 aSelAttr->setValue(aRes, aShapePtr);
97 myWorkshop->viewer()->setViewProjection(aDir->x(), aDir->y(), aDir->z());
99 // Clear text in the label
100 myLabel->setText("");
101 myLabel->setToolTip("");
102 disconnect(myWorkshop->selector(), SIGNAL(selectionChanged()),
103 this, SLOT(onPlaneSelected()));
105 // Clear selection mode and define sketching mode
106 XGUI_Displayer* aDisp = myWorkshop->displayer();
107 aDisp->removeSelectionFilter(myFaceFilter);
108 aDisp->closeLocalContexts();
109 emit planeSelected(plane());
112 // Update sketcher actions
113 XGUI_ActionsMgr* anActMgr = myWorkshop->actionsMgr();
120 std::shared_ptr<GeomAPI_Pln> PartSet_WidgetSketchLabel::plane() const
122 CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
123 return PartSet_Tools::sketchPlane(aSketch);
127 void PartSet_WidgetSketchLabel::activate()
129 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
133 // We have to select a plane before any operation
136 XGUI_Displayer* aDisp = myWorkshop->displayer();
137 aDisp->openLocalContext();
138 aDisp->activateObjects(QIntList());
139 if (myFaceFilter.IsNull())
140 myFaceFilter = new StdSelect_FaceFilter(StdSelect_Plane);
141 aDisp->addSelectionFilter(myFaceFilter);
143 aModes << TopAbs_FACE;
144 aDisp->activateObjects(aModes);
146 myLabel->setText(myText);
147 myLabel->setToolTip(myTooltip);
149 connect(myWorkshop->selector(), SIGNAL(selectionChanged()), this, SLOT(onPlaneSelected()));
150 aDisp->updateViewer();
154 void PartSet_WidgetSketchLabel::deactivate()
157 XGUI_Displayer* aDisp = myWorkshop->displayer();
158 aDisp->removeSelectionFilter(myFaceFilter);
159 //aDisp->removeSelectionFilter(mySketchFilter);
160 aDisp->closeLocalContexts();
161 erasePreviewPlanes();
164 void PartSet_WidgetSketchLabel::erasePreviewPlanes()
166 if (myPreviewDisplayed) {
167 XGUI_Displayer* aDisp = myWorkshop->displayer();
168 aDisp->eraseAIS(myYZPlane, false);
169 aDisp->eraseAIS(myXZPlane, false);
170 aDisp->eraseAIS(myXYPlane, false);
171 myPreviewDisplayed = false;
175 void PartSet_WidgetSketchLabel::showPreviewPlanes()
177 if (myPreviewDisplayed)
180 if (!myYZPlane) { // If planes are not created
182 std::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
183 std::shared_ptr<GeomAPI_Dir> aYZDir(new GeomAPI_Dir(1, 0, 0));
184 std::shared_ptr<GeomAPI_Dir> aXZDir(new GeomAPI_Dir(0, 1, 0));
185 std::shared_ptr<GeomAPI_Dir> aXYDir(new GeomAPI_Dir(0, 0, 1));
187 int aR[] = {255, 0, 0};
188 int aG[] = {0, 255, 0};
189 int aB[] = {0, 0, 255};
191 myYZPlane = createPreviewPlane(anOrigin, aYZDir, aR);
192 myXZPlane = createPreviewPlane(anOrigin, aXZDir, aG);
193 myXYPlane = createPreviewPlane(anOrigin, aXYDir, aB);
195 XGUI_Displayer* aDisp = myWorkshop->displayer();
196 aDisp->displayAIS(myYZPlane, false);
197 aDisp->displayAIS(myXZPlane, false);
198 aDisp->displayAIS(myXYPlane, false);
199 myPreviewDisplayed = true;
203 AISObjectPtr PartSet_WidgetSketchLabel::createPreviewPlane(std::shared_ptr<GeomAPI_Pnt> theOrigin,
204 std::shared_ptr<GeomAPI_Dir> theNorm,
207 double aSize = Config_PropManager::integer("Sketch planes", "Size of planes", PLANE_SIZE);
208 std::shared_ptr<GeomAPI_Shape> aFace = GeomAlgoAPI_FaceBuilder::square(theOrigin, theNorm, aSize);
209 AISObjectPtr aAIS = AISObjectPtr(new GeomAPI_AISObject());
210 aAIS->createShape(aFace);
211 aAIS->setWidth(Config_PropManager::integer("Sketch planes", "planes_thickness", SKETCH_WIDTH));
212 aAIS->setColor(theRGB[0], theRGB[1], theRGB[2]);
217 std::shared_ptr<GeomAPI_Dir> PartSet_WidgetSketchLabel::setSketchPlane(const TopoDS_Shape& theShape)
219 if (theShape.IsNull())
220 return std::shared_ptr<GeomAPI_Dir>();
222 // get selected shape
223 std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
224 aGShape->setImpl(new TopoDS_Shape(theShape));
226 // get plane parameters
227 std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
229 // set plane parameters to feature
230 std::shared_ptr<ModelAPI_Data> aData = feature()->data();
231 double anA, aB, aC, aD;
232 aPlane->coefficients(anA, aB, aC, aD);
234 // calculate attributes of the sketch
235 std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
236 std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
237 std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
238 aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
239 std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
240 // X axis is preferable to be dirX on the sketch
241 const double tol = Precision::Confusion();
242 bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
243 std::shared_ptr<GeomAPI_Dir> aTempDir(
244 isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
245 std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
246 std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
248 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
249 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
250 anOrigin->setValue(anOrigPnt);
251 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
252 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
253 aNormal->setValue(aNormDir);
254 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
255 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
256 aDirX->setValue(aXDir);
257 std::shared_ptr<GeomDataAPI_Dir> aDirY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
258 aData->attribute(SketchPlugin_Sketch::DIRY_ID()));
259 aDirY->setValue(aYDir);
260 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
265 void PartSet_WidgetSketchLabel::setSketchingMode()
267 XGUI_Displayer* aDisp = myWorkshop->displayer();
269 // Clear standard selection modes if they are defined
270 aDisp->activateObjects(aModes);
271 aDisp->openLocalContext();
274 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
275 double aA, aB, aC, aD;
276 aPlane->coefficients(aA, aB, aC, aD);
277 gp_Pln aPln(aA, aB, aC, aD);
278 // No selection of external objects
279 //mySketchFilter = new ModuleBase_ShapeInPlaneFilter(aPln);
280 //aDisp->addSelectionFilter(mySketchFilter);
282 // Get default selection modes
283 aModes.append(AIS_DSM_Text);
284 aModes.append(AIS_DSM_Line);
285 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_VERTEX));
286 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_EDGE));
288 aDisp->activateObjects(aModes);