1 // File: PartSet_OperationCreateFeature.h
2 // Created: 20 Apr 2014
3 // Author: Natalia ERMOLAEVA
5 #include <PartSet_OperationCreateFeature.h>
7 #include <PartSet_Tools.h>
8 #include <PartSet_OperationSketch.h>
9 #include <PartSet_FeaturePointPrs.h>
10 #include <PartSet_FeatureLinePrs.h>
11 #include <PartSet_FeatureCirclePrs.h>
12 #include <PartSet_FeatureArcPrs.h>
14 #include <SketchPlugin_Feature.h>
15 #include <SketchPlugin_Point.h>
16 #include <SketchPlugin_Line.h>
17 #include <SketchPlugin_Circle.h>
18 #include <SketchPlugin_Arc.h>
20 #include <ModuleBase_OperationDescription.h>
22 #include <XGUI_ViewerPrs.h>
23 #include <XGUI_Constants.h>
25 #include <V3d_View.hxx>
26 #include <TopoDS_Vertex.hxx>
28 #include <BRep_Tool.hxx>
34 #include <QMouseEvent>
38 PartSet_OperationCreateFeature::PartSet_OperationCreateFeature(const QString& theId,
40 FeaturePtr theFeature)
41 : PartSet_OperationSketchBase(theId, theParent),
42 myPointSelectionMode(SM_FirstPoint)
44 std::string aKind = theId.toStdString();
45 myFeaturePrs = PartSet_Tools::createFeaturePrs(aKind, theFeature);
48 PartSet_OperationCreateFeature::~PartSet_OperationCreateFeature()
52 bool PartSet_OperationCreateFeature::canProcessKind(const std::string& theId)
54 return theId == SKETCH_LINE_KIND || theId == SKETCH_POINT_KIND || theId == SKETCH_CIRCLE_KIND ||
55 theId == SKETCH_ARC_KIND;
58 bool PartSet_OperationCreateFeature::canBeCommitted() const
60 return myPointSelectionMode == SM_DonePoint;
63 bool PartSet_OperationCreateFeature::isGranted(ModuleBase_IOperation* theOperation) const
65 return theOperation->getDescription()->operationId().toStdString() == PartSet_OperationSketch::Type();
68 std::list<int> PartSet_OperationCreateFeature::getSelectionModes(FeaturePtr theFeature) const
70 std::list<int> aModes;
71 if (theFeature != feature())
72 aModes = PartSet_OperationSketchBase::getSelectionModes(theFeature);
76 void PartSet_OperationCreateFeature::init(FeaturePtr theFeature,
77 const std::list<XGUI_ViewerPrs>& /*theSelected*/,
78 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
80 if (!theFeature || theFeature->getKind() != SKETCH_LINE_KIND)
82 myInitFeature = theFeature;
85 FeaturePtr PartSet_OperationCreateFeature::sketch() const
87 return myFeaturePrs->sketch();
90 void PartSet_OperationCreateFeature::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
91 const std::list<XGUI_ViewerPrs>& theSelected,
92 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
94 if (myPointSelectionMode == SM_DonePoint)
96 // if the point creation is finished, the next mouse release should commit the modification
97 // the next release can happens by double click in the viewer
99 restartOperation(feature()->getKind(), feature());
105 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
106 if (theSelected.empty()) {
107 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
110 XGUI_ViewerPrs aPrs = theSelected.front();
111 const TopoDS_Shape& aShape = aPrs.shape();
112 if (!aShape.IsNull()) // the point is selected
114 if (aShape.ShapeType() == TopAbs_VERTEX) {
115 const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
116 if (!aVertex.IsNull()) {
117 aPoint = BRep_Tool::Pnt(aVertex);
118 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
120 //myFeaturePrs->setConstraints(aX, anY, myPointSelectionMode);
123 else if (aShape.ShapeType() == TopAbs_EDGE) // the line is selected
125 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
126 // move to selected line
127 if (feature()->getKind() == SKETCH_LINE_KIND) {
128 boost::shared_ptr<PartSet_FeatureLinePrs> aLinePrs =
129 boost::dynamic_pointer_cast<PartSet_FeatureLinePrs>(myFeaturePrs);
131 FeaturePtr aFeature = aPrs.feature();
132 aLinePrs->projectPointOnLine(aFeature, myPointSelectionMode, aPoint, theView, aX, anY);
139 switch (myPointSelectionMode)
143 case SM_ThirdPoint: {
144 if (feature()->getKind() == SKETCH_ARC_KIND) {
145 boost::shared_ptr<PartSet_FeatureArcPrs> anArcPrs =
146 boost::dynamic_pointer_cast<PartSet_FeatureArcPrs>(myFeaturePrs);
148 anArcPrs->projectPointOnFeature(feature(), sketch(), aPoint, theView, aX, anY);
151 PartSet_SelectionMode aMode = myFeaturePrs->setPoint(aX, anY, myPointSelectionMode);
153 setPointSelectionMode(aMode);
161 void PartSet_OperationCreateFeature::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
163 switch (myPointSelectionMode)
170 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
171 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
172 if (myPointSelectionMode == SM_ThirdPoint) {
173 if (feature()->getKind() == SKETCH_ARC_KIND) {
174 boost::shared_ptr<PartSet_FeatureArcPrs> anArcPrs =
175 boost::dynamic_pointer_cast<PartSet_FeatureArcPrs>(myFeaturePrs);
177 anArcPrs->projectPointOnFeature(feature(), sketch(), aPoint, theView, aX, anY);
181 myFeaturePrs->setPoint(aX, anY, myPointSelectionMode);
184 emit focusActivated(myFeaturePrs->getAttribute(myPointSelectionMode));
190 restartOperation(feature()->getKind(), feature());
197 void PartSet_OperationCreateFeature::keyReleased(std::string theName, QKeyEvent* theEvent)
199 int aKeyType = theEvent->key();
200 // the second point should be activated by any modification in the property panel
201 if (!theName.empty() /*&& aKeyType == Qt::Key_Return*/)
203 setPointSelectionMode(myFeaturePrs->getNextMode(theName), false);
205 keyReleased(theEvent->key());
208 void PartSet_OperationCreateFeature::keyReleased(const int theKey)
211 case Qt::Key_Return: {
212 if (myPointSelectionMode == SM_DonePoint)
215 // it start a new line creation at a free point
216 restartOperation(feature()->getKind(), FeaturePtr()/*feature()*/);
220 //restartOperation(feature()->getKind(), FeaturePtr());
223 case Qt::Key_Escape: {
224 if (myPointSelectionMode == SM_DonePoint)
238 void PartSet_OperationCreateFeature::startOperation()
240 PartSet_OperationSketchBase::startOperation();
241 setPointSelectionMode(!myInitFeature ? SM_FirstPoint : SM_SecondPoint);
243 emit multiSelectionEnabled(false);
246 void PartSet_OperationCreateFeature::abortOperation()
248 emit featureConstructed(feature(), FM_Hide);
249 PartSet_OperationSketchBase::abortOperation();
252 void PartSet_OperationCreateFeature::stopOperation()
254 PartSet_OperationSketchBase::stopOperation();
255 emit multiSelectionEnabled(true);
258 void PartSet_OperationCreateFeature::afterCommitOperation()
260 PartSet_OperationSketchBase::afterCommitOperation();
261 emit featureConstructed(feature(), FM_Deactivation);
264 FeaturePtr PartSet_OperationCreateFeature::createFeature(const bool theFlushMessage)
266 FeaturePtr aNewFeature = ModuleBase_Operation::createFeature(false);
268 boost::shared_ptr<SketchPlugin_Feature> aFeature =
269 boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
271 aFeature->addSub(aNewFeature);
273 myFeaturePrs->init(aNewFeature);
274 myFeaturePrs->setFeature(myInitFeature, SM_FirstPoint);
276 emit featureConstructed(aNewFeature, FM_Activation);
282 void PartSet_OperationCreateFeature::setPointSelectionMode(const PartSet_SelectionMode& theMode,
283 const bool isToEmitSignal)
285 myPointSelectionMode = theMode;
286 if (isToEmitSignal) {
287 std::string aName = myFeaturePrs->getAttribute(theMode);
288 if (aName.empty() && theMode == SM_DonePoint) {
289 aName = XGUI::PROP_PANEL_OK;
291 emit focusActivated(aName);