1 // File: PartSet_WidgetSketchLabel.cpp
2 // Created: 07 July 2014
3 // Author: Vitaly SMETANNIKOV
5 #include "PartSet_WidgetSketchLabel.h"
6 #include "PartSet_Tools.h"
8 #include <XGUI_Workshop.h>
9 #include <XGUI_Displayer.h>
10 #include <XGUI_SelectionMgr.h>
11 #include <XGUI_Selection.h>
12 #include <XGUI_ViewerProxy.h>
13 #include <XGUI_ActionsMgr.h>
15 #include <ModuleBase_Operation.h>
16 #include <ModuleBase_ViewerPrs.h>
18 #include <GeomAlgoAPI_FaceBuilder.h>
19 #include <GeomDataAPI_Point.h>
20 #include <GeomDataAPI_Dir.h>
21 #include <GeomAPI_XYZ.h>
23 #include <SketchPlugin_Sketch.h>
25 #include <Precision.hxx>
29 #include <AIS_Shape.hxx>
30 #include <AIS_DimensionSelectionMode.hxx>
32 #include <Config_WidgetAPI.h>
33 #include <Config_PropManager.h>
37 #define PLANE_SIZE "200"
38 #define SKETCH_WIDTH "4"
41 PartSet_WidgetSketchLabel::PartSet_WidgetSketchLabel(QWidget* theParent,
42 const Config_WidgetAPI* theData,
43 const std::string& theParentId)
44 : ModuleBase_ModelWidget(theParent, theData, theParentId), myPreviewDisplayed(false)
46 myText = QString::fromStdString(theData->getProperty("title"));
47 myLabel = new QLabel("", theParent);
48 myLabel->setWordWrap(true);
49 myTooltip = QString::fromStdString(theData->getProperty("tooltip"));
50 myLabel->setToolTip("");
51 myLabel->setIndent(5);
54 PartSet_WidgetSketchLabel::~PartSet_WidgetSketchLabel()
59 QList<QWidget*> PartSet_WidgetSketchLabel::getControls() const
61 return QList<QWidget*>();
64 QWidget* PartSet_WidgetSketchLabel::getControl() const
69 void PartSet_WidgetSketchLabel::onPlaneSelected()
71 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
72 QList<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
73 if (!aSelected.empty()) {
74 ModuleBase_ViewerPrs aPrs = aSelected.first();
75 TopoDS_Shape aShape = aPrs.shape();
76 if (!aShape.IsNull()) {
77 std::shared_ptr<GeomAPI_Dir> aDir = setSketchPlane(aShape);
81 if (aPrs.object() && (feature() != aPrs.object())) {
82 DataPtr aData = feature()->data();
83 AttributeSelectionPtr aSelAttr =
84 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
85 (aData->attribute(SketchPlugin_Feature::EXTERNAL_ID()));
87 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs.object());
89 GeomShapePtr aShapePtr(new GeomAPI_Shape());
90 aShapePtr->setImpl(new TopoDS_Shape(aShape));
91 aSelAttr->setValue(aRes, aShapePtr);
95 myWorkshop->viewer()->setViewProjection(aDir->x(), aDir->y(), aDir->z());
97 // Clear text in the label
99 myLabel->setToolTip("");
100 disconnect(myWorkshop->selector(), SIGNAL(selectionChanged()),
101 this, SLOT(onPlaneSelected()));
103 // Clear selection mode and define sketching mode
104 XGUI_Displayer* aDisp = myWorkshop->displayer();
105 aDisp->removeSelectionFilter(myFaceFilter);
106 aDisp->closeLocalContexts();
107 emit planeSelected(plane());
110 // Update sketcher actions
111 XGUI_ActionsMgr* anActMgr = myWorkshop->actionsMgr();
118 std::shared_ptr<GeomAPI_Pln> PartSet_WidgetSketchLabel::plane() const
120 CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
121 return PartSet_Tools::sketchPlane(aSketch);
125 void PartSet_WidgetSketchLabel::activate()
127 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
131 // We have to select a plane before any operation
134 XGUI_Displayer* aDisp = myWorkshop->displayer();
135 aDisp->openLocalContext();
136 aDisp->activateObjects(QIntList());
137 if (myFaceFilter.IsNull())
138 myFaceFilter = new StdSelect_FaceFilter(StdSelect_Plane);
139 aDisp->addSelectionFilter(myFaceFilter);
141 aModes << TopAbs_FACE;
142 aDisp->activateObjects(aModes);
144 myLabel->setText(myText);
145 myLabel->setToolTip(myTooltip);
147 connect(myWorkshop->selector(), SIGNAL(selectionChanged()), this, SLOT(onPlaneSelected()));
148 aDisp->updateViewer();
152 void PartSet_WidgetSketchLabel::deactivate()
155 XGUI_Displayer* aDisp = myWorkshop->displayer();
156 aDisp->removeSelectionFilter(myFaceFilter);
157 //aDisp->removeSelectionFilter(mySketchFilter);
158 aDisp->closeLocalContexts();
159 erasePreviewPlanes();
162 void PartSet_WidgetSketchLabel::erasePreviewPlanes()
164 if (myPreviewDisplayed) {
165 XGUI_Displayer* aDisp = myWorkshop->displayer();
166 aDisp->eraseAIS(myYZPlane, false);
167 aDisp->eraseAIS(myXZPlane, false);
168 aDisp->eraseAIS(myXYPlane, false);
169 myPreviewDisplayed = false;
173 void PartSet_WidgetSketchLabel::showPreviewPlanes()
175 if (myPreviewDisplayed)
178 if (!myYZPlane) { // If planes are not created
180 std::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
181 std::shared_ptr<GeomAPI_Dir> aYZDir(new GeomAPI_Dir(1, 0, 0));
182 std::shared_ptr<GeomAPI_Dir> aXZDir(new GeomAPI_Dir(0, 1, 0));
183 std::shared_ptr<GeomAPI_Dir> aXYDir(new GeomAPI_Dir(0, 0, 1));
185 int aR[] = {255, 0, 0};
186 int aG[] = {0, 255, 0};
187 int aB[] = {0, 0, 255};
189 myYZPlane = createPreviewPlane(anOrigin, aYZDir, aR);
190 myXZPlane = createPreviewPlane(anOrigin, aXZDir, aG);
191 myXYPlane = createPreviewPlane(anOrigin, aXYDir, aB);
193 XGUI_Displayer* aDisp = myWorkshop->displayer();
194 aDisp->displayAIS(myYZPlane, false);
195 aDisp->displayAIS(myXZPlane, false);
196 aDisp->displayAIS(myXYPlane, false);
197 myPreviewDisplayed = true;
201 AISObjectPtr PartSet_WidgetSketchLabel::createPreviewPlane(std::shared_ptr<GeomAPI_Pnt> theOrigin,
202 std::shared_ptr<GeomAPI_Dir> theNorm,
205 double aSize = Config_PropManager::integer("Sketch planes", "Size of planes", PLANE_SIZE);
206 std::shared_ptr<GeomAPI_Shape> aFace = GeomAlgoAPI_FaceBuilder::square(theOrigin, theNorm, aSize);
207 AISObjectPtr aAIS = AISObjectPtr(new GeomAPI_AISObject());
208 aAIS->createShape(aFace);
209 aAIS->setWidth(Config_PropManager::integer("Sketch planes", "planes_thickness", SKETCH_WIDTH));
210 aAIS->setColor(theRGB[0], theRGB[1], theRGB[2]);
215 std::shared_ptr<GeomAPI_Dir> PartSet_WidgetSketchLabel::setSketchPlane(const TopoDS_Shape& theShape)
217 if (theShape.IsNull())
218 return std::shared_ptr<GeomAPI_Dir>();
220 // get selected shape
221 std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
222 aGShape->setImpl(new TopoDS_Shape(theShape));
224 // get plane parameters
225 std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
227 // set plane parameters to feature
228 std::shared_ptr<ModelAPI_Data> aData = feature()->data();
229 double anA, aB, aC, aD;
230 aPlane->coefficients(anA, aB, aC, aD);
232 // calculate attributes of the sketch
233 std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
234 std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
235 std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
236 aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
237 std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
238 // X axis is preferable to be dirX on the sketch
239 const double tol = Precision::Confusion();
240 bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
241 std::shared_ptr<GeomAPI_Dir> aTempDir(
242 isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
243 std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
244 std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
246 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
247 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
248 anOrigin->setValue(anOrigPnt);
249 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
250 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
251 aNormal->setValue(aNormDir);
252 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
253 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
254 aDirX->setValue(aXDir);
255 std::shared_ptr<GeomDataAPI_Dir> aDirY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
256 aData->attribute(SketchPlugin_Sketch::DIRY_ID()));
257 aDirY->setValue(aYDir);
258 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
263 void PartSet_WidgetSketchLabel::setSketchingMode()
265 XGUI_Displayer* aDisp = myWorkshop->displayer();
267 // Clear standard selection modes if they are defined
268 aDisp->activateObjects(aModes);
269 aDisp->openLocalContext();
272 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
273 double aA, aB, aC, aD;
274 aPlane->coefficients(aA, aB, aC, aD);
275 gp_Pln aPln(aA, aB, aC, aD);
276 // No selection of external objects
277 //mySketchFilter = new ModuleBase_ShapeInPlaneFilter(aPln);
278 //aDisp->addSelectionFilter(mySketchFilter);
280 // Get default selection modes
281 aModes.append(AIS_DSM_Text);
282 aModes.append(AIS_DSM_Line);
283 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_VERTEX));
284 aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_EDGE));
286 aDisp->activateObjects(aModes);