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 // TODO: to be realized in the custom point selector. The last point values of the init feature
206 // should be to to the start point of a new feature
207 //myActiveWidget->init(myInitFeature);
208 //PartSet_FeatureLinePrs::setFeature(myInitFeature, SM_FirstPoint);
209 myInitFeature = FeaturePtr();
210 emit activateNextWidget(myActiveWidget);
214 void PartSet_OperationFeatureCreate::keyReleased(const int theKey)
217 case Qt::Key_Return: {
218 if (canBeCommitted())
221 // it start a new line creation at a free point
222 restartOperation(feature()->getKind(), FeaturePtr());
226 case Qt::Key_Escape: {
227 if (canBeCommitted())
241 void PartSet_OperationFeatureCreate::startOperation()
243 PartSet_OperationSketchBase::startOperation();
244 //setPointSelectionMode(!myInitFeature ? SM_FirstPoint : SM_SecondPoint);
246 emit multiSelectionEnabled(false);
249 void PartSet_OperationFeatureCreate::abortOperation()
251 emit featureConstructed(feature(), FM_Hide);
252 PartSet_OperationSketchBase::abortOperation();
255 void PartSet_OperationFeatureCreate::stopOperation()
257 PartSet_OperationSketchBase::stopOperation();
258 emit multiSelectionEnabled(true);
261 void PartSet_OperationFeatureCreate::afterCommitOperation()
263 PartSet_OperationSketchBase::afterCommitOperation();
264 emit featureConstructed(feature(), FM_Deactivation);
267 FeaturePtr PartSet_OperationFeatureCreate::createFeature(const bool theFlushMessage)
269 FeaturePtr aNewFeature = ModuleBase_Operation::createFeature(false);
271 boost::shared_ptr<SketchPlugin_Feature> aFeature =
272 boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
274 aFeature->addSub(aNewFeature);
276 //myFeaturePrs->init(aNewFeature);
277 //myFeaturePrs->setFeature(myInitFeature, SM_FirstPoint);
279 emit featureConstructed(aNewFeature, FM_Activation);
285 /*void PartSet_OperationFeatureCreate::setPointSelectionMode(const PartSet_SelectionMode& theMode,
286 const bool isToEmitSignal)
288 myPointSelectionMode = theMode;
289 if (isToEmitSignal) {
290 std::string aName = myFeaturePrs->getAttribute(theMode);
291 if (aName.empty() && theMode == SM_DonePoint) {
292 aName = XGUI::PROP_PANEL_OK;
294 emit focusActivated(aName);
298 bool PartSet_OperationFeatureCreate::isPointWidget() const
300 return dynamic_cast<ModuleBase_WidgetPoint2D*>(myActiveWidget);
303 bool PartSet_OperationFeatureCreate::setWidgetPoint(double theX, double theY)
305 boost::shared_ptr<GeomAPI_Pnt2d> aPoint(new GeomAPI_Pnt2d(theX, theY));
306 ModuleBase_WidgetPoint2D* aWidget = dynamic_cast<ModuleBase_WidgetPoint2D*>(myActiveWidget);
308 aWidget->setPoint(aPoint);
311 ModuleBase_WidgetPoint2dDistance* aWgt = dynamic_cast<ModuleBase_WidgetPoint2dDistance*>(myActiveWidget);
313 aWgt->setPoint(feature(), aPoint);
320 bool PartSet_OperationFeatureCreate::setWidgetFeature(const FeaturePtr& theFeature)
322 ModuleBase_WidgetFeature* aWidget = dynamic_cast<ModuleBase_WidgetFeature*>(myActiveWidget);
326 return aWidget->setFeature(theFeature);