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>
10 #include <SketchPlugin_Feature.h>
11 #include <SketchPlugin_Point.h>
12 #include <SketchPlugin_Line.h>
13 #include <SketchPlugin_Circle.h>
14 #include <SketchPlugin_Arc.h>
15 #include <SketchPlugin_ConstraintDistance.h>
16 #include <SketchPlugin_ConstraintLength.h>
17 #include <SketchPlugin_ConstraintRadius.h>
18 #include <SketchPlugin_ConstraintParallel.h>
19 #include <SketchPlugin_ConstraintPerpendicular.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_CIRCLE_KIND ||
59 theId == SKETCH_ARC_KIND ||
60 theId == SKETCH_CONSTRAINT_DISTANCE_KIND ||
61 theId == SKETCH_CONSTRAINT_LENGTH_KIND ||
62 theId == SKETCH_CONSTRAINT_RADIUS_KIND ||
63 theId == SKETCH_CONSTRAINT_PARALLEL_KIND ||
64 theId == SKETCH_CONSTRAINT_PERPENDICULAR_KIND;
67 bool PartSet_OperationFeatureCreate::canBeCommitted() const
69 return !myActiveWidget;
72 bool PartSet_OperationFeatureCreate::isGranted(ModuleBase_IOperation* theOperation) const
74 return theOperation->getDescription()->operationId().toStdString() == PartSet_OperationSketch::Type();
77 std::list<int> PartSet_OperationFeatureCreate::getSelectionModes(FeaturePtr theFeature) const
79 std::list<int> aModes;
80 if (theFeature != feature())
81 aModes = PartSet_OperationSketchBase::getSelectionModes(theFeature);
85 void PartSet_OperationFeatureCreate::init(FeaturePtr theFeature,
86 const std::list<XGUI_ViewerPrs>& /*theSelected*/,
87 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
89 if (!theFeature || theFeature->getKind() != SKETCH_LINE_KIND)
91 myInitFeature = theFeature;
94 FeaturePtr PartSet_OperationFeatureCreate::sketch() const
99 void PartSet_OperationFeatureCreate::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
100 const std::list<XGUI_ViewerPrs>& theSelected,
101 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
103 if (canBeCommitted())
105 // if the point creation is finished, the next mouse release should commit the modification
106 // the next release can happens by double click in the viewer
108 restartOperation(feature()->getKind(), feature());
112 double aX = 0, anY = 0;
114 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
115 if (theSelected.empty()) {
116 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
119 XGUI_ViewerPrs aPrs = theSelected.front();
120 const TopoDS_Shape& aShape = aPrs.shape();
121 if (!aShape.IsNull()) // the point is selected
123 if (aShape.ShapeType() == TopAbs_VERTEX) {
124 const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
125 if (!aVertex.IsNull()) {
126 aPoint = BRep_Tool::Pnt(aVertex);
127 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
129 PartSet_Tools::setConstraints(sketch(), feature(), myActiveWidget->attributeID(),
133 else if (aShape.ShapeType() == TopAbs_EDGE) // the line is selected
135 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
136 // move to selected line
137 if (feature()->getKind() == SKETCH_LINE_KIND) {
138 //FeaturePtr aFeature = aPrs.feature();
139 //projectPointOnLine(aFeature, myPointSelectionMode, aPoint, theView, aX, anY);
144 bool isApplyed = false;
146 isApplyed = setWidgetPoint(aX, anY);
148 if (!theSelected.empty()) {
149 XGUI_ViewerPrs aPrs = theSelected.front();
150 FeaturePtr aFeature = aPrs.feature();
152 isApplyed = setWidgetFeature(aFeature);
157 emit activateNextWidget(myActiveWidget);
161 void PartSet_OperationFeatureCreate::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
163 if (canBeCommitted()) {
165 restartOperation(feature()->getKind(), feature());
169 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
170 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
171 setWidgetPoint(aX, anY);
176 void PartSet_OperationFeatureCreate::keyReleased(std::string theName, QKeyEvent* theEvent)
178 int aKeyType = theEvent->key();
179 // the second point should be activated by any modification in the property panel
180 if (!theName.empty())
182 //setPointSelectionMode(myFeaturePrs->getNextMode(theName), false);
184 keyReleased(theEvent->key());
187 void PartSet_OperationFeatureCreate::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
189 myActiveWidget = theWidget;
191 if (myInitFeature && myActiveWidget) {
192 ModuleBase_WidgetPoint2D* aWgt = dynamic_cast<ModuleBase_WidgetPoint2D*>(myActiveWidget);
194 aWgt->initFromPrevious(myInitFeature);
195 myInitFeature = FeaturePtr();
196 emit activateNextWidget(myActiveWidget);
200 void PartSet_OperationFeatureCreate::keyReleased(const int theKey)
203 case Qt::Key_Return: {
204 if (canBeCommitted())
207 // it start a new line creation at a free point
208 restartOperation(feature()->getKind(), FeaturePtr());
212 case Qt::Key_Escape: {
213 if (canBeCommitted())
227 void PartSet_OperationFeatureCreate::startOperation()
229 PartSet_OperationSketchBase::startOperation();
230 //setPointSelectionMode(!myInitFeature ? SM_FirstPoint : SM_SecondPoint);
232 emit multiSelectionEnabled(false);
235 void PartSet_OperationFeatureCreate::abortOperation()
237 emit featureConstructed(feature(), FM_Hide);
238 PartSet_OperationSketchBase::abortOperation();
241 void PartSet_OperationFeatureCreate::stopOperation()
243 PartSet_OperationSketchBase::stopOperation();
244 emit multiSelectionEnabled(true);
247 void PartSet_OperationFeatureCreate::afterCommitOperation()
249 PartSet_OperationSketchBase::afterCommitOperation();
250 emit featureConstructed(feature(), FM_Deactivation);
253 FeaturePtr PartSet_OperationFeatureCreate::createFeature(const bool theFlushMessage)
255 FeaturePtr aNewFeature = ModuleBase_Operation::createFeature(false);
257 boost::shared_ptr<SketchPlugin_Feature> aFeature =
258 boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
260 aFeature->addSub(aNewFeature);
262 //myFeaturePrs->init(aNewFeature);
263 //myFeaturePrs->setFeature(myInitFeature, SM_FirstPoint);
265 emit featureConstructed(aNewFeature, FM_Activation);
271 bool PartSet_OperationFeatureCreate::isPointWidget() const
273 return dynamic_cast<ModuleBase_WidgetPoint2D*>(myActiveWidget) ||
274 dynamic_cast<ModuleBase_WidgetPoint2dDistance*>(myActiveWidget);
277 bool PartSet_OperationFeatureCreate::setWidgetPoint(double theX, double theY)
279 boost::shared_ptr<GeomAPI_Pnt2d> aPoint(new GeomAPI_Pnt2d(theX, theY));
280 ModuleBase_WidgetPoint2D* aWidget = dynamic_cast<ModuleBase_WidgetPoint2D*>(myActiveWidget);
282 aWidget->setPoint(aPoint);
285 ModuleBase_WidgetPoint2dDistance* aWgt = dynamic_cast<ModuleBase_WidgetPoint2dDistance*>(myActiveWidget);
287 aWgt->setPoint(feature(), aPoint);
294 bool PartSet_OperationFeatureCreate::setWidgetFeature(const FeaturePtr& theFeature)
296 ModuleBase_WidgetFeature* aWidget = dynamic_cast<ModuleBase_WidgetFeature*>(myActiveWidget);
300 return aWidget->setFeature(theFeature);