1 // File: PartSet_OperationCreateConstraint.h
2 // Created: 20 Apr 2014
3 // Author: Natalia ERMOLAEVA
5 #include <PartSet_OperationCreateConstraint.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 <SketchPlugin_ConstraintLength.h>
22 #include <ModuleBase_OperationDescription.h>
24 #include <XGUI_ViewerPrs.h>
25 #include <XGUI_Constants.h>
27 #include <V3d_View.hxx>
28 #include <TopoDS_Vertex.hxx>
30 #include <BRep_Tool.hxx>
36 #include <QMouseEvent>
40 PartSet_OperationCreateConstraint::PartSet_OperationCreateConstraint(const QString& theId,
42 FeaturePtr theFeature)
43 : PartSet_OperationSketchBase(theId, theParent),
44 myPointSelectionMode(SM_FirstPoint)
46 std::string aKind = theId.toStdString();
47 myFeaturePrs = PartSet_Tools::createFeaturePrs(aKind, theFeature);
50 PartSet_OperationCreateConstraint::~PartSet_OperationCreateConstraint()
54 bool PartSet_OperationCreateConstraint::canProcessKind(const std::string& theId)
56 return /*theId == SKETCH_LINE_KIND || theId == SKETCH_POINT_KIND || theId == SKETCH_CIRCLE_KIND ||
57 theId == SKETCH_ARC_KIND || */theId == SKETCH_CONSTRAINT_LENGTH_KIND;
60 bool PartSet_OperationCreateConstraint::canBeCommitted() const
62 return myPointSelectionMode == SM_DonePoint;
65 bool PartSet_OperationCreateConstraint::isGranted(ModuleBase_IOperation* theOperation) const
67 return theOperation->getDescription()->operationId().toStdString() == PartSet_OperationSketch::Type();
70 std::list<int> PartSet_OperationCreateConstraint::getSelectionModes(FeaturePtr theFeature) const
72 std::list<int> aModes;
73 if (theFeature != feature())
74 aModes = PartSet_OperationSketchBase::getSelectionModes(theFeature);
78 void PartSet_OperationCreateConstraint::init(FeaturePtr theFeature,
79 const std::list<XGUI_ViewerPrs>& /*theSelected*/,
80 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
82 if (!theFeature || theFeature->getKind() != SKETCH_LINE_KIND)
84 myInitFeature = theFeature;
87 FeaturePtr PartSet_OperationCreateConstraint::sketch() const
89 return myFeaturePrs->sketch();
92 void PartSet_OperationCreateConstraint::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
93 const std::list<XGUI_ViewerPrs>& theSelected,
94 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
96 if (theSelected.empty()) {
100 XGUI_ViewerPrs aPrs = theSelected.front();
101 FeaturePtr aFeature = aPrs.feature();
103 myFeaturePrs->init(feature(), aFeature);
108 void PartSet_OperationCreateConstraint::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
110 switch (myPointSelectionMode)
117 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
118 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
119 /*if (myPointSelectionMode == SM_ThirdPoint) {
120 if (feature()->getKind() == SKETCH_ARC_KIND) {
121 boost::shared_ptr<PartSet_FeatureArcPrs> anArcPrs =
122 boost::dynamic_pointer_cast<PartSet_FeatureArcPrs>(myFeaturePrs);
124 anArcPrs->projectPointOnArc(aPoint, theView, aX, anY);
128 myFeaturePrs->setPoint(aX, anY, myPointSelectionMode);
131 emit focusActivated(myFeaturePrs->getAttribute(myPointSelectionMode));
137 restartOperation(feature()->getKind(), feature());
144 void PartSet_OperationCreateConstraint::keyReleased(std::string theName, QKeyEvent* theEvent)
146 int aKeyType = theEvent->key();
147 // the second point should be activated by any modification in the property panel
148 if (!theName.empty() /*&& aKeyType == Qt::Key_Return*/)
150 setPointSelectionMode(myFeaturePrs->getNextMode(theName), false);
152 keyReleased(theEvent->key());
155 void PartSet_OperationCreateConstraint::keyReleased(const int theKey)
158 case Qt::Key_Return: {
159 if (myPointSelectionMode == SM_DonePoint)
162 // it start a new line creation at a free point
163 restartOperation(feature()->getKind(), FeaturePtr()/*feature()*/);
167 //restartOperation(feature()->getKind(), FeaturePtr());
170 case Qt::Key_Escape: {
171 if (myPointSelectionMode == SM_DonePoint)
185 void PartSet_OperationCreateConstraint::startOperation()
187 PartSet_OperationSketchBase::startOperation();
188 setPointSelectionMode(!myInitFeature ? SM_FirstPoint : SM_SecondPoint);
190 emit multiSelectionEnabled(false);
193 void PartSet_OperationCreateConstraint::abortOperation()
195 emit featureConstructed(feature(), FM_Hide);
196 PartSet_OperationSketchBase::abortOperation();
199 void PartSet_OperationCreateConstraint::stopOperation()
201 PartSet_OperationSketchBase::stopOperation();
202 emit multiSelectionEnabled(true);
205 void PartSet_OperationCreateConstraint::afterCommitOperation()
207 PartSet_OperationSketchBase::afterCommitOperation();
208 emit featureConstructed(feature(), FM_Deactivation);
211 FeaturePtr PartSet_OperationCreateConstraint::createFeature(const bool theFlushMessage)
213 FeaturePtr aNewFeature = ModuleBase_Operation::createFeature(false);
215 boost::shared_ptr<SketchPlugin_Feature> aFeature =
216 boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
218 aFeature->addSub(aNewFeature);
220 myFeaturePrs->init(aNewFeature, myInitFeature);
222 emit featureConstructed(aNewFeature, FM_Activation);
228 void PartSet_OperationCreateConstraint::setPointSelectionMode(const PartSet_SelectionMode& theMode,
229 const bool isToEmitSignal)
231 myPointSelectionMode = theMode;
232 if (isToEmitSignal) {
233 std::string aName = myFeaturePrs->getAttribute(theMode);
234 if (aName.empty() && theMode == SM_DonePoint) {
235 aName = XGUI::PROP_PANEL_OK;
237 emit focusActivated(aName);