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>
20 #include <SketchPlugin_ConstraintLength.h>
21 #include <SketchPlugin_ConstraintRadius.h>
22 #include <SketchPlugin_ConstraintParallel.h>
23 #include <SketchPlugin_ConstraintPerpendicular.h>
25 #include <GeomAPI_Pnt2d.h>
27 #include <ModuleBase_OperationDescription.h>
28 #include <ModuleBase_WidgetPoint2D.h>
29 #include <ModuleBase_WidgetFeature.h>
30 #include <ModuleBase_WidgetPoint2dDistance.h>
32 #include <XGUI_ViewerPrs.h>
33 #include <XGUI_Constants.h>
35 #include <V3d_View.hxx>
36 #include <TopoDS_Vertex.hxx>
38 #include <BRep_Tool.hxx>
44 #include <QMouseEvent>
48 PartSet_OperationFeatureCreate::PartSet_OperationFeatureCreate(const QString& theId,
50 FeaturePtr theFeature)
51 : PartSet_OperationSketchBase(theId, theParent), mySketch(theFeature), myActiveWidget(0)
55 PartSet_OperationFeatureCreate::~PartSet_OperationFeatureCreate()
59 bool PartSet_OperationFeatureCreate::canProcessKind(const std::string& theId)
61 return theId == SKETCH_LINE_KIND || theId == SKETCH_POINT_KIND ||
62 theId == SKETCH_CIRCLE_KIND /*||
63 theId == SKETCH_ARC_KIND*/ ||
64 theId == SKETCH_CONSTRAINT_DISTANCE_KIND ||
65 theId == SKETCH_CONSTRAINT_LENGTH_KIND ||
66 theId == SKETCH_CONSTRAINT_RADIUS_KIND ||
67 theId == SKETCH_CONSTRAINT_PARALLEL_KIND ||
68 theId == SKETCH_CONSTRAINT_PERPENDICULAR_KIND;
71 bool PartSet_OperationFeatureCreate::canBeCommitted() const
73 return !myActiveWidget;
76 bool PartSet_OperationFeatureCreate::isGranted(ModuleBase_IOperation* theOperation) const
78 return theOperation->getDescription()->operationId().toStdString() == PartSet_OperationSketch::Type();
81 std::list<int> PartSet_OperationFeatureCreate::getSelectionModes(FeaturePtr theFeature) const
83 std::list<int> aModes;
84 if (theFeature != feature())
85 aModes = PartSet_OperationSketchBase::getSelectionModes(theFeature);
89 void PartSet_OperationFeatureCreate::init(FeaturePtr theFeature,
90 const std::list<XGUI_ViewerPrs>& /*theSelected*/,
91 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
93 if (!theFeature || theFeature->getKind() != SKETCH_LINE_KIND)
95 myInitFeature = theFeature;
98 FeaturePtr PartSet_OperationFeatureCreate::sketch() const
103 void PartSet_OperationFeatureCreate::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
104 const std::list<XGUI_ViewerPrs>& theSelected,
105 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
107 if (canBeCommitted())
109 // if the point creation is finished, the next mouse release should commit the modification
110 // the next release can happens by double click in the viewer
112 restartOperation(feature()->getKind(), feature());
118 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
119 if (theSelected.empty()) {
120 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
123 XGUI_ViewerPrs aPrs = theSelected.front();
124 const TopoDS_Shape& aShape = aPrs.shape();
125 if (!aShape.IsNull()) // the point is selected
127 if (aShape.ShapeType() == TopAbs_VERTEX) {
128 const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
129 if (!aVertex.IsNull()) {
130 aPoint = BRep_Tool::Pnt(aVertex);
131 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
133 PartSet_Tools::setConstraints(sketch(), feature(), myActiveWidget->attributeID(),
137 else if (aShape.ShapeType() == TopAbs_EDGE) // the line is selected
139 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
140 // move to selected line
141 if (feature()->getKind() == SKETCH_LINE_KIND) {
142 //boost::shared_ptr<PartSet_FeatureLinePrs> aLinePrs =
143 // boost::dynamic_pointer_cast<PartSet_FeatureLinePrs>(myFeaturePrs);
145 // FeaturePtr aFeature = aPrs.feature();
146 //aLinePrs->projectPointOnLine(aFeature, myPointSelectionMode, aPoint, theView, aX, anY);
152 /*if (feature()->getKind() == SKETCH_ARC_KIND) {
153 boost::shared_ptr<PartSet_FeatureArcPrs> anArcPrs =
154 boost::dynamic_pointer_cast<PartSet_FeatureArcPrs>(myFeaturePrs);
156 anArcPrs->projectPointOnFeature(feature(), sketch(), aPoint, theView, aX, anY);
159 bool isApplyed = false;
161 isApplyed = setWidgetPoint(aX, anY);
163 if (!theSelected.empty()) {
164 XGUI_ViewerPrs aPrs = theSelected.front();
165 FeaturePtr aFeature = aPrs.feature();
167 isApplyed = setWidgetFeature(aFeature);
171 emit activateNextWidget(myActiveWidget);
174 void PartSet_OperationFeatureCreate::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
176 if (canBeCommitted()) {
178 restartOperation(feature()->getKind(), feature());
182 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
183 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
184 /*if (myPointSelectionMode == SM_ThirdPoint) {
185 if (feature()->getKind() == SKETCH_ARC_KIND) {
186 boost::shared_ptr<PartSet_FeatureArcPrs> anArcPrs =
187 boost::dynamic_pointer_cast<PartSet_FeatureArcPrs>(myFeaturePrs);
189 anArcPrs->projectPointOnFeature(feature(), sketch(), aPoint, theView, aX, anY);
193 setWidgetPoint(aX, anY);
198 void PartSet_OperationFeatureCreate::keyReleased(std::string theName, QKeyEvent* theEvent)
200 int aKeyType = theEvent->key();
201 // the second point should be activated by any modification in the property panel
202 if (!theName.empty())
204 //setPointSelectionMode(myFeaturePrs->getNextMode(theName), false);
206 keyReleased(theEvent->key());
209 void PartSet_OperationFeatureCreate::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
211 myActiveWidget = theWidget;
213 if (myInitFeature && myActiveWidget) {
214 ModuleBase_WidgetPoint2D* aWgt = dynamic_cast<ModuleBase_WidgetPoint2D*>(myActiveWidget);
216 aWgt->initFromPrevious(myInitFeature);
217 myInitFeature = FeaturePtr();
218 emit activateNextWidget(myActiveWidget);
222 void PartSet_OperationFeatureCreate::keyReleased(const int theKey)
225 case Qt::Key_Return: {
226 if (canBeCommitted())
229 // it start a new line creation at a free point
230 restartOperation(feature()->getKind(), FeaturePtr());
234 case Qt::Key_Escape: {
235 if (canBeCommitted())
249 void PartSet_OperationFeatureCreate::startOperation()
251 PartSet_OperationSketchBase::startOperation();
252 //setPointSelectionMode(!myInitFeature ? SM_FirstPoint : SM_SecondPoint);
254 emit multiSelectionEnabled(false);
257 void PartSet_OperationFeatureCreate::abortOperation()
259 emit featureConstructed(feature(), FM_Hide);
260 PartSet_OperationSketchBase::abortOperation();
263 void PartSet_OperationFeatureCreate::stopOperation()
265 PartSet_OperationSketchBase::stopOperation();
266 emit multiSelectionEnabled(true);
269 void PartSet_OperationFeatureCreate::afterCommitOperation()
271 PartSet_OperationSketchBase::afterCommitOperation();
272 emit featureConstructed(feature(), FM_Deactivation);
275 FeaturePtr PartSet_OperationFeatureCreate::createFeature(const bool theFlushMessage)
277 FeaturePtr aNewFeature = ModuleBase_Operation::createFeature(false);
279 boost::shared_ptr<SketchPlugin_Feature> aFeature =
280 boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
282 aFeature->addSub(aNewFeature);
284 //myFeaturePrs->init(aNewFeature);
285 //myFeaturePrs->setFeature(myInitFeature, SM_FirstPoint);
287 emit featureConstructed(aNewFeature, FM_Activation);
293 /*void PartSet_OperationFeatureCreate::setPointSelectionMode(const PartSet_SelectionMode& theMode,
294 const bool isToEmitSignal)
296 myPointSelectionMode = theMode;
297 if (isToEmitSignal) {
298 std::string aName = myFeaturePrs->getAttribute(theMode);
299 if (aName.empty() && theMode == SM_DonePoint) {
300 aName = XGUI::PROP_PANEL_OK;
302 emit focusActivated(aName);
306 bool PartSet_OperationFeatureCreate::isPointWidget() const
308 return dynamic_cast<ModuleBase_WidgetPoint2D*>(myActiveWidget);
311 bool PartSet_OperationFeatureCreate::setWidgetPoint(double theX, double theY)
313 boost::shared_ptr<GeomAPI_Pnt2d> aPoint(new GeomAPI_Pnt2d(theX, theY));
314 ModuleBase_WidgetPoint2D* aWidget = dynamic_cast<ModuleBase_WidgetPoint2D*>(myActiveWidget);
316 aWidget->setPoint(aPoint);
319 ModuleBase_WidgetPoint2dDistance* aWgt = dynamic_cast<ModuleBase_WidgetPoint2dDistance*>(myActiveWidget);
321 aWgt->setPoint(feature(), aPoint);
328 bool PartSet_OperationFeatureCreate::setWidgetFeature(const FeaturePtr& theFeature)
330 ModuleBase_WidgetFeature* aWidget = dynamic_cast<ModuleBase_WidgetFeature*>(myActiveWidget);
334 return aWidget->setFeature(theFeature);