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>
27 #include <XGUI_ViewerPrs.h>
28 #include <XGUI_Constants.h>
30 #include <V3d_View.hxx>
31 #include <TopoDS_Vertex.hxx>
33 #include <BRep_Tool.hxx>
39 #include <QMouseEvent>
43 PartSet_OperationFeatureCreate::PartSet_OperationFeatureCreate(const QString& theId,
45 FeaturePtr theFeature)
46 : PartSet_OperationSketchBase(theId, theParent), mySketch(theFeature), myActiveWidget(0)
50 PartSet_OperationFeatureCreate::~PartSet_OperationFeatureCreate()
54 bool PartSet_OperationFeatureCreate::canProcessKind(const std::string& theId)
56 return theId == SKETCH_LINE_KIND || theId == SKETCH_POINT_KIND /*||
57 theId == SKETCH_CONSTRAINT_DISTANCE_KIND/*|| theId == SKETCH_CIRCLE_KIND ||
58 theId == SKETCH_ARC_KIND*/;
61 bool PartSet_OperationFeatureCreate::canBeCommitted() const
63 return !myActiveWidget;
66 bool PartSet_OperationFeatureCreate::isGranted(ModuleBase_IOperation* theOperation) const
68 return theOperation->getDescription()->operationId().toStdString() == PartSet_OperationSketch::Type();
71 std::list<int> PartSet_OperationFeatureCreate::getSelectionModes(FeaturePtr theFeature) const
73 std::list<int> aModes;
74 if (theFeature != feature())
75 aModes = PartSet_OperationSketchBase::getSelectionModes(theFeature);
79 void PartSet_OperationFeatureCreate::init(FeaturePtr theFeature,
80 const std::list<XGUI_ViewerPrs>& /*theSelected*/,
81 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
83 if (!theFeature || theFeature->getKind() != SKETCH_LINE_KIND)
85 myInitFeature = theFeature;
88 FeaturePtr PartSet_OperationFeatureCreate::sketch() const
93 void PartSet_OperationFeatureCreate::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
94 const std::list<XGUI_ViewerPrs>& theSelected,
95 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
99 // if the point creation is finished, the next mouse release should commit the modification
100 // the next release can happens by double click in the viewer
102 restartOperation(feature()->getKind(), feature());
108 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
109 if (theSelected.empty()) {
110 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
113 XGUI_ViewerPrs aPrs = theSelected.front();
114 const TopoDS_Shape& aShape = aPrs.shape();
115 if (!aShape.IsNull()) // the point is selected
117 if (aShape.ShapeType() == TopAbs_VERTEX) {
118 const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
119 if (!aVertex.IsNull()) {
120 aPoint = BRep_Tool::Pnt(aVertex);
121 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
123 PartSet_Tools::setConstraints(sketch(), feature(), myActiveWidget->attributeID(),
127 else if (aShape.ShapeType() == TopAbs_EDGE) // the line is selected
129 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
130 // move to selected line
131 if (feature()->getKind() == SKETCH_LINE_KIND) {
132 //boost::shared_ptr<PartSet_FeatureLinePrs> aLinePrs =
133 // boost::dynamic_pointer_cast<PartSet_FeatureLinePrs>(myFeaturePrs);
135 // FeaturePtr aFeature = aPrs.feature();
136 //aLinePrs->projectPointOnLine(aFeature, myPointSelectionMode, aPoint, theView, aX, anY);
142 /*if (feature()->getKind() == SKETCH_ARC_KIND) {
143 boost::shared_ptr<PartSet_FeatureArcPrs> anArcPrs =
144 boost::dynamic_pointer_cast<PartSet_FeatureArcPrs>(myFeaturePrs);
146 anArcPrs->projectPointOnFeature(feature(), sketch(), aPoint, theView, aX, anY);
149 bool isApplyed = false;
151 isApplyed = setWidgetPoint(aX, anY);
153 if (!theSelected.empty()) {
154 XGUI_ViewerPrs aPrs = theSelected.front();
155 FeaturePtr aFeature = aPrs.feature();
157 isApplyed = setWidgetFeature(aFeature);
161 emit activateNextWidget(myActiveWidget);
164 void PartSet_OperationFeatureCreate::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
166 if (canBeCommitted()) {
168 restartOperation(feature()->getKind(), feature());
172 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
173 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
174 /*if (myPointSelectionMode == SM_ThirdPoint) {
175 if (feature()->getKind() == SKETCH_ARC_KIND) {
176 boost::shared_ptr<PartSet_FeatureArcPrs> anArcPrs =
177 boost::dynamic_pointer_cast<PartSet_FeatureArcPrs>(myFeaturePrs);
179 anArcPrs->projectPointOnFeature(feature(), sketch(), aPoint, theView, aX, anY);
183 setWidgetPoint(aX, anY);
188 void PartSet_OperationFeatureCreate::keyReleased(std::string theName, QKeyEvent* theEvent)
190 int aKeyType = theEvent->key();
191 // the second point should be activated by any modification in the property panel
192 if (!theName.empty())
194 //setPointSelectionMode(myFeaturePrs->getNextMode(theName), false);
196 keyReleased(theEvent->key());
199 void PartSet_OperationFeatureCreate::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
201 myActiveWidget = theWidget;
203 if (myInitFeature && myActiveWidget) {
204 // TODO: to be realized in the custom point selector. The last point values of the init feature
205 // should be to to the start point of a new feature
206 //myActiveWidget->init(myInitFeature);
207 //PartSet_FeatureLinePrs::setFeature(myInitFeature, SM_FirstPoint);
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 ModuleBase_WidgetPoint2D* aWidget = dynamic_cast<ModuleBase_WidgetPoint2D*>(myActiveWidget);
308 aWidget->setPoint(boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
312 bool PartSet_OperationFeatureCreate::setWidgetFeature(const FeaturePtr& theFeature)
314 ModuleBase_WidgetFeature* aWidget = dynamic_cast<ModuleBase_WidgetFeature*>(myActiveWidget);
318 return aWidget->setFeature(theFeature);