1 // File: PartSet_OperationFeatureCreate.h
2 // Created: 20 Apr 2014
3 // Author: Natalia ERMOLAEVA
5 #include <PartSet_OperationFeatureCreate.h>
7 #include <PartSet_Tools.h>
8 #include <PartSet_OperationSketch.h>
10 #include <SketchPlugin_Feature.h>
11 #include <SketchPlugin_Point.h>
12 #include <SketchPlugin_Line.h>
13 #include <SketchPlugin_Circle.h>
14 #include <SketchPlugin_Arc.h>
15 #include <SketchPlugin_ConstraintDistance.h>
16 #include <SketchPlugin_ConstraintLength.h>
17 #include <SketchPlugin_ConstraintRadius.h>
18 #include <SketchPlugin_ConstraintParallel.h>
19 #include <SketchPlugin_ConstraintPerpendicular.h>
20 #include <SketchPlugin_ConstraintCoincidence.h>
22 #include <GeomAPI_Pnt2d.h>
24 #include <ModuleBase_OperationDescription.h>
25 #include <ModuleBase_WidgetPoint2D.h>
26 #include <ModuleBase_WidgetFeature.h>
27 #include <ModuleBase_WidgetPoint2dDistance.h>
29 #include <XGUI_ViewerPrs.h>
30 #include <XGUI_Constants.h>
32 #include <V3d_View.hxx>
33 #include <TopoDS_Vertex.hxx>
35 #include <BRep_Tool.hxx>
41 #include <QMouseEvent>
45 PartSet_OperationFeatureCreate::PartSet_OperationFeatureCreate(const QString& theId,
47 FeaturePtr theFeature)
48 : PartSet_OperationSketchBase(theId, theParent), mySketch(theFeature), myActiveWidget(0)
52 PartSet_OperationFeatureCreate::~PartSet_OperationFeatureCreate()
56 bool PartSet_OperationFeatureCreate::canProcessKind(const std::string& theId)
58 return theId == SKETCH_LINE_KIND || theId == SKETCH_POINT_KIND ||
59 theId == SKETCH_CIRCLE_KIND ||
60 theId == SKETCH_ARC_KIND ||
61 theId == SKETCH_CONSTRAINT_DISTANCE_KIND ||
62 theId == SKETCH_CONSTRAINT_LENGTH_KIND ||
63 theId == SKETCH_CONSTRAINT_RADIUS_KIND ||
64 theId == SKETCH_CONSTRAINT_PARALLEL_KIND ||
65 theId == SKETCH_CONSTRAINT_PERPENDICULAR_KIND ||
66 theId == SKETCH_CONSTRAINT_COINCIDENCE_KIND;
69 bool PartSet_OperationFeatureCreate::canBeCommitted() const
71 return !myActiveWidget;
74 bool PartSet_OperationFeatureCreate::isGranted(ModuleBase_IOperation* theOperation) const
76 return theOperation->getDescription()->operationId().toStdString() == PartSet_OperationSketch::Type();
79 std::list<int> PartSet_OperationFeatureCreate::getSelectionModes(FeaturePtr theFeature) const
81 std::list<int> aModes;
82 if (theFeature != feature())
83 aModes = PartSet_OperationSketchBase::getSelectionModes(theFeature);
87 void PartSet_OperationFeatureCreate::initSelection(const std::list<XGUI_ViewerPrs>& theSelected,
88 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
92 void PartSet_OperationFeatureCreate::initFeature(FeaturePtr theFeature)
94 //if (!theFeature || theFeature->getKind() != SKETCH_LINE_KIND)
96 myInitFeature = theFeature;
100 FeaturePtr PartSet_OperationFeatureCreate::sketch() const
105 void PartSet_OperationFeatureCreate::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
106 const std::list<XGUI_ViewerPrs>& theSelected,
107 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
109 if (canBeCommitted())
111 // if the point creation is finished, the next mouse release should commit the modification
112 // the next release can happens by double click in the viewer
114 restartOperation(feature()->getKind(), feature());
118 double aX = 0, anY = 0;
120 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
121 if (theSelected.empty()) {
122 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
125 XGUI_ViewerPrs aPrs = theSelected.front();
126 const TopoDS_Shape& aShape = aPrs.shape();
127 if (!aShape.IsNull()) // the point is selected
129 if (aShape.ShapeType() == TopAbs_VERTEX) {
130 const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
131 if (!aVertex.IsNull()) {
132 aPoint = BRep_Tool::Pnt(aVertex);
133 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
135 PartSet_Tools::setConstraints(sketch(), feature(), myActiveWidget->attributeID(),
139 else if (aShape.ShapeType() == TopAbs_EDGE) // the line is selected
141 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
142 // move to selected line
143 if (feature()->getKind() == SKETCH_LINE_KIND) {
144 //FeaturePtr aFeature = aPrs.feature();
145 //projectPointOnLine(aFeature, myPointSelectionMode, aPoint, theView, aX, anY);
150 bool isApplyed = false;
152 isApplyed = setWidgetPoint(aX, anY);
154 if (!theSelected.empty()) {
155 XGUI_ViewerPrs aPrs = theSelected.front();
156 FeaturePtr aFeature = aPrs.feature();
158 isApplyed = setWidgetFeature(aFeature);
163 emit activateNextWidget(myActiveWidget);
167 void PartSet_OperationFeatureCreate::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
169 if (canBeCommitted()) {
171 restartOperation(feature()->getKind(), feature());
175 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
176 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
177 setWidgetPoint(aX, anY);
182 void PartSet_OperationFeatureCreate::keyReleased(std::string theName, QKeyEvent* theEvent)
184 int aKeyType = theEvent->key();
185 // the second point should be activated by any modification in the property panel
186 if (!theName.empty())
188 //setPointSelectionMode(myFeaturePrs->getNextMode(theName), false);
190 keyReleased(theEvent->key());
193 void PartSet_OperationFeatureCreate::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
195 myActiveWidget = theWidget;
197 if (myInitFeature && myActiveWidget) {
198 ModuleBase_WidgetPoint2D* aWgt = dynamic_cast<ModuleBase_WidgetPoint2D*>(myActiveWidget);
199 if (aWgt && aWgt->initFromPrevious(myInitFeature)) {
200 myInitFeature = FeaturePtr();
201 emit activateNextWidget(myActiveWidget);
206 void PartSet_OperationFeatureCreate::keyReleased(const int theKey)
209 case Qt::Key_Return: {
210 if (canBeCommitted())
213 // it start a new line creation at a free point
214 restartOperation(feature()->getKind(), FeaturePtr());
218 case Qt::Key_Escape: {
219 if (canBeCommitted())
233 void PartSet_OperationFeatureCreate::startOperation()
235 PartSet_OperationSketchBase::startOperation();
236 //setPointSelectionMode(!myInitFeature ? SM_FirstPoint : SM_SecondPoint);
238 emit multiSelectionEnabled(false);
241 void PartSet_OperationFeatureCreate::abortOperation()
243 emit featureConstructed(feature(), FM_Hide);
244 PartSet_OperationSketchBase::abortOperation();
247 void PartSet_OperationFeatureCreate::stopOperation()
249 PartSet_OperationSketchBase::stopOperation();
250 emit multiSelectionEnabled(true);
253 void PartSet_OperationFeatureCreate::afterCommitOperation()
255 PartSet_OperationSketchBase::afterCommitOperation();
256 emit featureConstructed(feature(), FM_Deactivation);
259 FeaturePtr PartSet_OperationFeatureCreate::createFeature(const bool theFlushMessage)
261 FeaturePtr aNewFeature = ModuleBase_Operation::createFeature(false);
263 boost::shared_ptr<SketchPlugin_Feature> aFeature =
264 boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
266 aFeature->addSub(aNewFeature);
268 //myFeaturePrs->init(aNewFeature);
269 //myFeaturePrs->setFeature(myInitFeature, SM_FirstPoint);
271 emit featureConstructed(aNewFeature, FM_Activation);
277 bool PartSet_OperationFeatureCreate::isPointWidget() const
279 return dynamic_cast<ModuleBase_WidgetPoint2D*>(myActiveWidget) ||
280 dynamic_cast<ModuleBase_WidgetPoint2dDistance*>(myActiveWidget);
283 bool PartSet_OperationFeatureCreate::setWidgetPoint(double theX, double theY)
285 boost::shared_ptr<GeomAPI_Pnt2d> aPoint(new GeomAPI_Pnt2d(theX, theY));
286 ModuleBase_WidgetPoint2D* aWidget = dynamic_cast<ModuleBase_WidgetPoint2D*>(myActiveWidget);
288 aWidget->setPoint(aPoint);
291 ModuleBase_WidgetPoint2dDistance* aWgt = dynamic_cast<ModuleBase_WidgetPoint2dDistance*>(myActiveWidget);
293 aWgt->setPoint(feature(), aPoint);
300 bool PartSet_OperationFeatureCreate::setWidgetFeature(const FeaturePtr& theFeature)
302 ModuleBase_WidgetFeature* aWidget = dynamic_cast<ModuleBase_WidgetFeature*>(myActiveWidget);
306 return aWidget->setFeature(theFeature);