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>
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>
19 #include <SketchPlugin_ConstraintDistance.h>
21 #include <GeomAPI_Pnt2d.h>
23 #include <ModuleBase_OperationDescription.h>
24 #include <ModuleBase_WidgetPoint2D.h>
25 #include <ModuleBase_WidgetFeature.h>
26 #include <ModuleBase_WidgetPoint2dDistance.h>
28 #include <XGUI_ViewerPrs.h>
29 #include <XGUI_Constants.h>
31 #include <V3d_View.hxx>
32 #include <TopoDS_Vertex.hxx>
34 #include <BRep_Tool.hxx>
40 #include <QMouseEvent>
44 PartSet_OperationFeatureCreate::PartSet_OperationFeatureCreate(const QString& theId,
46 FeaturePtr theFeature)
47 : PartSet_OperationSketchBase(theId, theParent), mySketch(theFeature), myActiveWidget(0)
51 PartSet_OperationFeatureCreate::~PartSet_OperationFeatureCreate()
55 bool PartSet_OperationFeatureCreate::canProcessKind(const std::string& theId)
57 return theId == SKETCH_LINE_KIND || theId == SKETCH_POINT_KIND ||
58 theId == SKETCH_CONSTRAINT_DISTANCE_KIND || theId == SKETCH_CIRCLE_KIND /*||
59 theId == SKETCH_ARC_KIND*/;
62 bool PartSet_OperationFeatureCreate::canBeCommitted() const
64 return !myActiveWidget;
67 bool PartSet_OperationFeatureCreate::isGranted(ModuleBase_IOperation* theOperation) const
69 return theOperation->getDescription()->operationId().toStdString() == PartSet_OperationSketch::Type();
72 std::list<int> PartSet_OperationFeatureCreate::getSelectionModes(FeaturePtr theFeature) const
74 std::list<int> aModes;
75 if (theFeature != feature())
76 aModes = PartSet_OperationSketchBase::getSelectionModes(theFeature);
80 void PartSet_OperationFeatureCreate::init(FeaturePtr theFeature,
81 const std::list<XGUI_ViewerPrs>& /*theSelected*/,
82 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
84 if (!theFeature || theFeature->getKind() != SKETCH_LINE_KIND)
86 myInitFeature = theFeature;
89 FeaturePtr PartSet_OperationFeatureCreate::sketch() const
94 void PartSet_OperationFeatureCreate::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
95 const std::list<XGUI_ViewerPrs>& theSelected,
96 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
100 // if the point creation is finished, the next mouse release should commit the modification
101 // the next release can happens by double click in the viewer
103 restartOperation(feature()->getKind(), feature());
109 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
110 if (theSelected.empty()) {
111 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
114 XGUI_ViewerPrs aPrs = theSelected.front();
115 const TopoDS_Shape& aShape = aPrs.shape();
116 if (!aShape.IsNull()) // the point is selected
118 if (aShape.ShapeType() == TopAbs_VERTEX) {
119 const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
120 if (!aVertex.IsNull()) {
121 aPoint = BRep_Tool::Pnt(aVertex);
122 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
124 PartSet_Tools::setConstraints(sketch(), feature(), myActiveWidget->attributeID(),
128 else if (aShape.ShapeType() == TopAbs_EDGE) // the line is selected
130 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
131 // move to selected line
132 if (feature()->getKind() == SKETCH_LINE_KIND) {
133 //boost::shared_ptr<PartSet_FeatureLinePrs> aLinePrs =
134 // boost::dynamic_pointer_cast<PartSet_FeatureLinePrs>(myFeaturePrs);
136 // FeaturePtr aFeature = aPrs.feature();
137 //aLinePrs->projectPointOnLine(aFeature, myPointSelectionMode, aPoint, theView, aX, anY);
143 /*if (feature()->getKind() == SKETCH_ARC_KIND) {
144 boost::shared_ptr<PartSet_FeatureArcPrs> anArcPrs =
145 boost::dynamic_pointer_cast<PartSet_FeatureArcPrs>(myFeaturePrs);
147 anArcPrs->projectPointOnFeature(feature(), sketch(), 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);
162 emit activateNextWidget(myActiveWidget);
165 void PartSet_OperationFeatureCreate::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
167 if (canBeCommitted()) {
169 restartOperation(feature()->getKind(), feature());
173 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
174 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
175 /*if (myPointSelectionMode == SM_ThirdPoint) {
176 if (feature()->getKind() == SKETCH_ARC_KIND) {
177 boost::shared_ptr<PartSet_FeatureArcPrs> anArcPrs =
178 boost::dynamic_pointer_cast<PartSet_FeatureArcPrs>(myFeaturePrs);
180 anArcPrs->projectPointOnFeature(feature(), sketch(), aPoint, theView, aX, anY);
184 setWidgetPoint(aX, anY);
189 void PartSet_OperationFeatureCreate::keyReleased(std::string theName, QKeyEvent* theEvent)
191 int aKeyType = theEvent->key();
192 // the second point should be activated by any modification in the property panel
193 if (!theName.empty())
195 //setPointSelectionMode(myFeaturePrs->getNextMode(theName), false);
197 keyReleased(theEvent->key());
200 void PartSet_OperationFeatureCreate::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
202 myActiveWidget = theWidget;
204 if (myInitFeature && myActiveWidget) {
205 ModuleBase_WidgetPoint2D* aWgt = dynamic_cast<ModuleBase_WidgetPoint2D*>(myActiveWidget);
207 aWgt->initFromPrevious(myInitFeature);
208 myInitFeature = FeaturePtr();
209 emit activateNextWidget(myActiveWidget);
213 void PartSet_OperationFeatureCreate::keyReleased(const int theKey)
216 case Qt::Key_Return: {
217 if (canBeCommitted())
220 // it start a new line creation at a free point
221 restartOperation(feature()->getKind(), FeaturePtr());
225 case Qt::Key_Escape: {
226 if (canBeCommitted())
240 void PartSet_OperationFeatureCreate::startOperation()
242 PartSet_OperationSketchBase::startOperation();
243 //setPointSelectionMode(!myInitFeature ? SM_FirstPoint : SM_SecondPoint);
245 emit multiSelectionEnabled(false);
248 void PartSet_OperationFeatureCreate::abortOperation()
250 emit featureConstructed(feature(), FM_Hide);
251 PartSet_OperationSketchBase::abortOperation();
254 void PartSet_OperationFeatureCreate::stopOperation()
256 PartSet_OperationSketchBase::stopOperation();
257 emit multiSelectionEnabled(true);
260 void PartSet_OperationFeatureCreate::afterCommitOperation()
262 PartSet_OperationSketchBase::afterCommitOperation();
263 emit featureConstructed(feature(), FM_Deactivation);
266 FeaturePtr PartSet_OperationFeatureCreate::createFeature(const bool theFlushMessage)
268 FeaturePtr aNewFeature = ModuleBase_Operation::createFeature(false);
270 boost::shared_ptr<SketchPlugin_Feature> aFeature =
271 boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
273 aFeature->addSub(aNewFeature);
275 //myFeaturePrs->init(aNewFeature);
276 //myFeaturePrs->setFeature(myInitFeature, SM_FirstPoint);
278 emit featureConstructed(aNewFeature, FM_Activation);
284 /*void PartSet_OperationFeatureCreate::setPointSelectionMode(const PartSet_SelectionMode& theMode,
285 const bool isToEmitSignal)
287 myPointSelectionMode = theMode;
288 if (isToEmitSignal) {
289 std::string aName = myFeaturePrs->getAttribute(theMode);
290 if (aName.empty() && theMode == SM_DonePoint) {
291 aName = XGUI::PROP_PANEL_OK;
293 emit focusActivated(aName);
297 bool PartSet_OperationFeatureCreate::isPointWidget() const
299 return dynamic_cast<ModuleBase_WidgetPoint2D*>(myActiveWidget);
302 bool PartSet_OperationFeatureCreate::setWidgetPoint(double theX, double theY)
304 boost::shared_ptr<GeomAPI_Pnt2d> aPoint(new GeomAPI_Pnt2d(theX, theY));
305 ModuleBase_WidgetPoint2D* aWidget = dynamic_cast<ModuleBase_WidgetPoint2D*>(myActiveWidget);
307 aWidget->setPoint(aPoint);
310 ModuleBase_WidgetPoint2dDistance* aWgt = dynamic_cast<ModuleBase_WidgetPoint2dDistance*>(myActiveWidget);
312 aWgt->setPoint(feature(), aPoint);
319 bool PartSet_OperationFeatureCreate::setWidgetFeature(const FeaturePtr& theFeature)
321 ModuleBase_WidgetFeature* aWidget = dynamic_cast<ModuleBase_WidgetFeature*>(myActiveWidget);
325 return aWidget->setFeature(theFeature);